blob: 62982e671d1808f5dc594b3cdbe403764a16fc7f [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 */
318 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700319 if (e) {
320 if (fsync_done && ni->nid == ni->ino)
321 set_nat_flag(e, HAS_FSYNCED_INODE, true);
322 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
323 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800324 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900325}
326
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900327int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900328{
329 struct f2fs_nm_info *nm_i = NM_I(sbi);
330
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900331 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900332 return 0;
333
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800334 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900335 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
336 struct nat_entry *ne;
337 ne = list_first_entry(&nm_i->nat_entries,
338 struct nat_entry, list);
339 __del_from_nat_cache(nm_i, ne);
340 nr_shrink--;
341 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800342 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900343 return nr_shrink;
344}
345
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900346/*
arter97e1c42042014-08-06 23:22:50 +0900347 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900348 */
349void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
350{
351 struct f2fs_nm_info *nm_i = NM_I(sbi);
352 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
353 struct f2fs_summary_block *sum = curseg->sum_blk;
354 nid_t start_nid = START_NID(nid);
355 struct f2fs_nat_block *nat_blk;
356 struct page *page = NULL;
357 struct f2fs_nat_entry ne;
358 struct nat_entry *e;
359 int i;
360
361 ni->nid = nid;
362
363 /* Check nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800364 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900365 e = __lookup_nat_cache(nm_i, nid);
366 if (e) {
367 ni->ino = nat_get_ino(e);
368 ni->blk_addr = nat_get_blkaddr(e);
369 ni->version = nat_get_version(e);
370 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800371 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900372 if (e)
373 return;
374
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800375 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
376
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900377 /* Check current segment summary */
378 mutex_lock(&curseg->curseg_mutex);
379 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
380 if (i >= 0) {
381 ne = nat_in_journal(sum, i);
382 node_info_from_raw_nat(ni, &ne);
383 }
384 mutex_unlock(&curseg->curseg_mutex);
385 if (i >= 0)
386 goto cache;
387
388 /* Fill node_info from nat page */
389 page = get_current_nat_page(sbi, start_nid);
390 nat_blk = (struct f2fs_nat_block *)page_address(page);
391 ne = nat_blk->entries[nid - start_nid];
392 node_info_from_raw_nat(ni, &ne);
393 f2fs_put_page(page, 1);
394cache:
395 /* cache nat entry */
396 cache_nat_entry(NM_I(sbi), nid, &ne);
397}
398
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900399/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900400 * The maximum depth is four.
401 * Offset[0] will have raw inode offset.
402 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900403static int get_node_path(struct f2fs_inode_info *fi, long block,
404 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900405{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900406 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900407 const long direct_blks = ADDRS_PER_BLOCK;
408 const long dptrs_per_blk = NIDS_PER_BLOCK;
409 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
410 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
411 int n = 0;
412 int level = 0;
413
414 noffset[0] = 0;
415
416 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900417 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900418 goto got;
419 }
420 block -= direct_index;
421 if (block < direct_blks) {
422 offset[n++] = NODE_DIR1_BLOCK;
423 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900424 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900425 level = 1;
426 goto got;
427 }
428 block -= direct_blks;
429 if (block < direct_blks) {
430 offset[n++] = NODE_DIR2_BLOCK;
431 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900432 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900433 level = 1;
434 goto got;
435 }
436 block -= direct_blks;
437 if (block < indirect_blks) {
438 offset[n++] = NODE_IND1_BLOCK;
439 noffset[n] = 3;
440 offset[n++] = block / direct_blks;
441 noffset[n] = 4 + 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 < indirect_blks) {
448 offset[n++] = NODE_IND2_BLOCK;
449 noffset[n] = 4 + dptrs_per_blk;
450 offset[n++] = block / direct_blks;
451 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900452 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900453 level = 2;
454 goto got;
455 }
456 block -= indirect_blks;
457 if (block < dindirect_blks) {
458 offset[n++] = NODE_DIND_BLOCK;
459 noffset[n] = 5 + (dptrs_per_blk * 2);
460 offset[n++] = block / indirect_blks;
461 noffset[n] = 6 + (dptrs_per_blk * 2) +
462 offset[n - 1] * (dptrs_per_blk + 1);
463 offset[n++] = (block / direct_blks) % dptrs_per_blk;
464 noffset[n] = 7 + (dptrs_per_blk * 2) +
465 offset[n - 2] * (dptrs_per_blk + 1) +
466 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900467 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900468 level = 3;
469 goto got;
470 } else {
471 BUG();
472 }
473got:
474 return level;
475}
476
477/*
478 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800479 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
480 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900481 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900482 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900483int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900484{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700485 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900486 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800487 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900488 int offset[4];
489 unsigned int noffset[4];
490 nid_t nids[4];
491 int level, i;
492 int err = 0;
493
Jaegeuk Kimde936532013-08-12 21:08:03 +0900494 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900495
496 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900497 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900498
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900499 if (!npage[0]) {
500 npage[0] = get_node_page(sbi, nids[0]);
501 if (IS_ERR(npage[0]))
502 return PTR_ERR(npage[0]);
503 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800504
505 /* if inline_data is set, should not report any block indices */
506 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800507 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800508 f2fs_put_page(npage[0], 1);
509 goto release_out;
510 }
511
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900513 if (level != 0)
514 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515 dn->inode_page = npage[0];
516 dn->inode_page_locked = true;
517
518 /* get indirect or direct nodes */
519 for (i = 1; i <= level; i++) {
520 bool done = false;
521
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900522 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900523 /* alloc new node */
524 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900525 err = -ENOSPC;
526 goto release_pages;
527 }
528
529 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900530 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900531 if (IS_ERR(npage[i])) {
532 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900533 err = PTR_ERR(npage[i]);
534 goto release_pages;
535 }
536
537 set_nid(parent, offset[i - 1], nids[i], i == 1);
538 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900539 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900540 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900541 npage[i] = get_node_page_ra(parent, offset[i - 1]);
542 if (IS_ERR(npage[i])) {
543 err = PTR_ERR(npage[i]);
544 goto release_pages;
545 }
546 done = true;
547 }
548 if (i == 1) {
549 dn->inode_page_locked = false;
550 unlock_page(parent);
551 } else {
552 f2fs_put_page(parent, 1);
553 }
554
555 if (!done) {
556 npage[i] = get_node_page(sbi, nids[i]);
557 if (IS_ERR(npage[i])) {
558 err = PTR_ERR(npage[i]);
559 f2fs_put_page(npage[0], 0);
560 goto release_out;
561 }
562 }
563 if (i < level) {
564 parent = npage[i];
565 nids[i + 1] = get_nid(parent, offset[i], false);
566 }
567 }
568 dn->nid = nids[level];
569 dn->ofs_in_node = offset[level];
570 dn->node_page = npage[level];
571 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
572 return 0;
573
574release_pages:
575 f2fs_put_page(parent, 1);
576 if (i > 1)
577 f2fs_put_page(npage[0], 0);
578release_out:
579 dn->inode_page = NULL;
580 dn->node_page = NULL;
581 return err;
582}
583
584static void truncate_node(struct dnode_of_data *dn)
585{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700586 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900587 struct node_info ni;
588
589 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900590 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700591 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900592 goto invalidate;
593 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700594 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900595
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900596 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900597 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800598 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900599 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900600
601 if (dn->nid == dn->inode->i_ino) {
602 remove_orphan_inode(sbi, dn->nid);
603 dec_valid_inode_count(sbi);
604 } else {
605 sync_inode_page(dn);
606 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900607invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900608 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800609 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900610
611 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900612
613 invalidate_mapping_pages(NODE_MAPPING(sbi),
614 dn->node_page->index, dn->node_page->index);
615
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900616 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900617 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900618}
619
620static int truncate_dnode(struct dnode_of_data *dn)
621{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900622 struct page *page;
623
624 if (dn->nid == 0)
625 return 1;
626
627 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700628 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900629 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
630 return 1;
631 else if (IS_ERR(page))
632 return PTR_ERR(page);
633
634 /* Make dnode_of_data for parameter */
635 dn->node_page = page;
636 dn->ofs_in_node = 0;
637 truncate_data_blocks(dn);
638 truncate_node(dn);
639 return 1;
640}
641
642static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
643 int ofs, int depth)
644{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900645 struct dnode_of_data rdn = *dn;
646 struct page *page;
647 struct f2fs_node *rn;
648 nid_t child_nid;
649 unsigned int child_nofs;
650 int freed = 0;
651 int i, ret;
652
653 if (dn->nid == 0)
654 return NIDS_PER_BLOCK + 1;
655
Namjae Jeon51dd6242013-04-20 01:28:52 +0900656 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
657
Jaegeuk Kim40813632014-09-02 15:31:18 -0700658 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900659 if (IS_ERR(page)) {
660 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900661 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900662 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900663
Gu Zheng45590712013-07-15 17:57:38 +0800664 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900665 if (depth < 3) {
666 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
667 child_nid = le32_to_cpu(rn->in.nid[i]);
668 if (child_nid == 0)
669 continue;
670 rdn.nid = child_nid;
671 ret = truncate_dnode(&rdn);
672 if (ret < 0)
673 goto out_err;
674 set_nid(page, i, 0, false);
675 }
676 } else {
677 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
678 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
679 child_nid = le32_to_cpu(rn->in.nid[i]);
680 if (child_nid == 0) {
681 child_nofs += NIDS_PER_BLOCK + 1;
682 continue;
683 }
684 rdn.nid = child_nid;
685 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
686 if (ret == (NIDS_PER_BLOCK + 1)) {
687 set_nid(page, i, 0, false);
688 child_nofs += ret;
689 } else if (ret < 0 && ret != -ENOENT) {
690 goto out_err;
691 }
692 }
693 freed = child_nofs;
694 }
695
696 if (!ofs) {
697 /* remove current indirect node */
698 dn->node_page = page;
699 truncate_node(dn);
700 freed++;
701 } else {
702 f2fs_put_page(page, 1);
703 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900704 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900705 return freed;
706
707out_err:
708 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900709 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900710 return ret;
711}
712
713static int truncate_partial_nodes(struct dnode_of_data *dn,
714 struct f2fs_inode *ri, int *offset, int depth)
715{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716 struct page *pages[2];
717 nid_t nid[3];
718 nid_t child_nid;
719 int err = 0;
720 int i;
721 int idx = depth - 2;
722
723 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
724 if (!nid[0])
725 return 0;
726
727 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800728 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900729 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700730 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900731 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800733 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900734 goto fail;
735 }
736 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
737 }
738
739 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800740 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900741 child_nid = get_nid(pages[idx], i, false);
742 if (!child_nid)
743 continue;
744 dn->nid = child_nid;
745 err = truncate_dnode(dn);
746 if (err < 0)
747 goto fail;
748 set_nid(pages[idx], i, 0, false);
749 }
750
shifei10.gea225dca2013-10-29 15:32:34 +0800751 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900752 dn->node_page = pages[idx];
753 dn->nid = nid[idx];
754 truncate_node(dn);
755 } else {
756 f2fs_put_page(pages[idx], 1);
757 }
758 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800759 offset[idx + 1] = 0;
760 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900761fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800762 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900763 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900764
765 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
766
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900767 return err;
768}
769
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900770/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900771 * All the block addresses of data and nodes should be nullified.
772 */
773int truncate_inode_blocks(struct inode *inode, pgoff_t from)
774{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700775 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900776 int err = 0, cont = 1;
777 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900778 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900779 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900780 struct dnode_of_data dn;
781 struct page *page;
782
Namjae Jeon51dd6242013-04-20 01:28:52 +0900783 trace_f2fs_truncate_inode_blocks_enter(inode, from);
784
Jaegeuk Kimde936532013-08-12 21:08:03 +0900785 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900786restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900787 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900788 if (IS_ERR(page)) {
789 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900790 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900791 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900792
793 set_new_dnode(&dn, inode, page, NULL, 0);
794 unlock_page(page);
795
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900796 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900797 switch (level) {
798 case 0:
799 case 1:
800 nofs = noffset[1];
801 break;
802 case 2:
803 nofs = noffset[1];
804 if (!offset[level - 1])
805 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900806 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900807 if (err < 0 && err != -ENOENT)
808 goto fail;
809 nofs += 1 + NIDS_PER_BLOCK;
810 break;
811 case 3:
812 nofs = 5 + 2 * NIDS_PER_BLOCK;
813 if (!offset[level - 1])
814 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900815 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900816 if (err < 0 && err != -ENOENT)
817 goto fail;
818 break;
819 default:
820 BUG();
821 }
822
823skip_partial:
824 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900825 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900826 switch (offset[0]) {
827 case NODE_DIR1_BLOCK:
828 case NODE_DIR2_BLOCK:
829 err = truncate_dnode(&dn);
830 break;
831
832 case NODE_IND1_BLOCK:
833 case NODE_IND2_BLOCK:
834 err = truncate_nodes(&dn, nofs, offset[1], 2);
835 break;
836
837 case NODE_DIND_BLOCK:
838 err = truncate_nodes(&dn, nofs, offset[1], 3);
839 cont = 0;
840 break;
841
842 default:
843 BUG();
844 }
845 if (err < 0 && err != -ENOENT)
846 goto fail;
847 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900848 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900849 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900850 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900851 f2fs_put_page(page, 1);
852 goto restart;
853 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900854 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900855 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900856 set_page_dirty(page);
857 unlock_page(page);
858 }
859 offset[1] = 0;
860 offset[0]++;
861 nofs += err;
862 }
863fail:
864 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900865 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900866 return err > 0 ? 0 : err;
867}
868
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900869int truncate_xattr_node(struct inode *inode, struct page *page)
870{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700871 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900872 nid_t nid = F2FS_I(inode)->i_xattr_nid;
873 struct dnode_of_data dn;
874 struct page *npage;
875
876 if (!nid)
877 return 0;
878
879 npage = get_node_page(sbi, nid);
880 if (IS_ERR(npage))
881 return PTR_ERR(npage);
882
883 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900884
885 /* need to do checkpoint during fsync */
886 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
887
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900888 set_new_dnode(&dn, inode, page, npage, nid);
889
890 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800891 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900892 truncate_node(&dn);
893 return 0;
894}
895
Jaegeuk Kim39936832012-11-22 16:21:29 +0900896/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800897 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
898 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900899 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800900void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900901{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900902 struct dnode_of_data dn;
903
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700904 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
905 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800906 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900907
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700908 if (truncate_xattr_node(inode, dn.inode_page)) {
909 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800910 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900911 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700912
913 /* remove potential inline_data blocks */
914 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
915 S_ISLNK(inode->i_mode))
916 truncate_data_blocks_range(&dn, 1);
917
arter97e1c42042014-08-06 23:22:50 +0900918 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700919 f2fs_bug_on(F2FS_I_SB(inode),
920 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700921
922 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900923 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900924}
925
Jaegeuk Kima014e032014-06-20 21:44:02 -0700926struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900927{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900928 struct dnode_of_data dn;
929
930 /* allocate inode page for new inode */
931 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900932
933 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900934 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900935}
936
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900937struct page *new_node_page(struct dnode_of_data *dn,
938 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900939{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700940 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900941 struct node_info old_ni, new_ni;
942 struct page *page;
943 int err;
944
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900945 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900946 return ERR_PTR(-EPERM);
947
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900948 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900949 if (!page)
950 return ERR_PTR(-ENOMEM);
951
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900952 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900953 err = -ENOSPC;
954 goto fail;
955 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900956
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900957 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958
959 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700960 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900961 new_ni = old_ni;
962 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900963 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900964
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900965 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900966 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900967 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900968 SetPageUptodate(page);
969 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900970
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800971 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900972 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
973
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900974 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900975 if (ipage)
976 update_inode(dn->inode, ipage);
977 else
978 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979 if (ofs == 0)
980 inc_valid_inode_count(sbi);
981
982 return page;
983
984fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900985 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900986 f2fs_put_page(page, 1);
987 return ERR_PTR(err);
988}
989
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900990/*
991 * Caller should do after getting the following values.
992 * 0: f2fs_put_page(page, 0)
993 * LOCKED_PAGE: f2fs_put_page(page, 1)
994 * error: nothing
995 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900996static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900997{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700998 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001000 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001001 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001002 .type = NODE,
1003 .rw = rw,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001004 .page = page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001005 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006
1007 get_node_info(sbi, page->index, &ni);
1008
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001009 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001010 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001011 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001012 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001013 }
1014
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001015 if (PageUptodate(page))
1016 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001017
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001018 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001019 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001020}
1021
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001022/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001023 * Readahead a node page
1024 */
1025void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1026{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001027 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001028 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001030 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001031 if (apage && PageUptodate(apage)) {
1032 f2fs_put_page(apage, 0);
1033 return;
1034 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001035 f2fs_put_page(apage, 0);
1036
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001037 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001038 if (!apage)
1039 return;
1040
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001041 err = read_node_page(apage, READA);
1042 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001043 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001044 else if (err == LOCKED_PAGE)
1045 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001046}
1047
1048struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1049{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001050 struct page *page;
1051 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001052repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001053 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001054 if (!page)
1055 return ERR_PTR(-ENOMEM);
1056
1057 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001058 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001059 return ERR_PTR(err);
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001060 else if (err != LOCKED_PAGE)
1061 lock_page(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001062
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001063 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001064 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001065 f2fs_put_page(page, 1);
1066 return ERR_PTR(-EIO);
1067 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001068 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001069 f2fs_put_page(page, 1);
1070 goto repeat;
1071 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001072 return page;
1073}
1074
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001075/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001076 * Return a locked page for the desired node page.
1077 * And, readahead MAX_RA_NODE number of node pages.
1078 */
1079struct page *get_node_page_ra(struct page *parent, int start)
1080{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001081 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001082 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001083 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001084 int err, i, end;
1085 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001086
1087 /* First, try getting the desired direct node. */
1088 nid = get_nid(parent, start, false);
1089 if (!nid)
1090 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001091repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001092 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001093 if (!page)
1094 return ERR_PTR(-ENOMEM);
1095
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001096 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001097 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001098 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001099 else if (err == LOCKED_PAGE)
1100 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001101
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001102 blk_start_plug(&plug);
1103
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001104 /* Then, try readahead for siblings of the desired node */
1105 end = start + MAX_RA_NODE;
1106 end = min(end, NIDS_PER_BLOCK);
1107 for (i = start + 1; i < end; i++) {
1108 nid = get_nid(parent, i, false);
1109 if (!nid)
1110 continue;
1111 ra_node_page(sbi, nid);
1112 }
1113
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001114 blk_finish_plug(&plug);
1115
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001116 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001117 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001118 f2fs_put_page(page, 1);
1119 goto repeat;
1120 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001121page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001122 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001123 f2fs_put_page(page, 1);
1124 return ERR_PTR(-EIO);
1125 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001126 return page;
1127}
1128
1129void sync_inode_page(struct dnode_of_data *dn)
1130{
1131 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1132 update_inode(dn->inode, dn->node_page);
1133 } else if (dn->inode_page) {
1134 if (!dn->inode_page_locked)
1135 lock_page(dn->inode_page);
1136 update_inode(dn->inode, dn->inode_page);
1137 if (!dn->inode_page_locked)
1138 unlock_page(dn->inode_page);
1139 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001140 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001141 }
1142}
1143
1144int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1145 struct writeback_control *wbc)
1146{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001147 pgoff_t index, end;
1148 struct pagevec pvec;
1149 int step = ino ? 2 : 0;
1150 int nwritten = 0, wrote = 0;
1151
1152 pagevec_init(&pvec, 0);
1153
1154next_step:
1155 index = 0;
1156 end = LONG_MAX;
1157
1158 while (index <= end) {
1159 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001160 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001161 PAGECACHE_TAG_DIRTY,
1162 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1163 if (nr_pages == 0)
1164 break;
1165
1166 for (i = 0; i < nr_pages; i++) {
1167 struct page *page = pvec.pages[i];
1168
1169 /*
1170 * flushing sequence with step:
1171 * 0. indirect nodes
1172 * 1. dentry dnodes
1173 * 2. file dnodes
1174 */
1175 if (step == 0 && IS_DNODE(page))
1176 continue;
1177 if (step == 1 && (!IS_DNODE(page) ||
1178 is_cold_node(page)))
1179 continue;
1180 if (step == 2 && (!IS_DNODE(page) ||
1181 !is_cold_node(page)))
1182 continue;
1183
1184 /*
1185 * If an fsync mode,
1186 * we should not skip writing node pages.
1187 */
1188 if (ino && ino_of_node(page) == ino)
1189 lock_page(page);
1190 else if (!trylock_page(page))
1191 continue;
1192
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001193 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001194continue_unlock:
1195 unlock_page(page);
1196 continue;
1197 }
1198 if (ino && ino_of_node(page) != ino)
1199 goto continue_unlock;
1200
1201 if (!PageDirty(page)) {
1202 /* someone wrote it for us */
1203 goto continue_unlock;
1204 }
1205
1206 if (!clear_page_dirty_for_io(page))
1207 goto continue_unlock;
1208
1209 /* called by fsync() */
1210 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001211 set_fsync_mark(page, 1);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -07001212 if (IS_INODE(page))
1213 set_dentry_mark(page,
1214 need_dentry_mark(sbi, ino));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001215 nwritten++;
1216 } else {
1217 set_fsync_mark(page, 0);
1218 set_dentry_mark(page, 0);
1219 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001220
1221 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1222 unlock_page(page);
1223 else
1224 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001225
1226 if (--wbc->nr_to_write == 0)
1227 break;
1228 }
1229 pagevec_release(&pvec);
1230 cond_resched();
1231
1232 if (wbc->nr_to_write == 0) {
1233 step = 2;
1234 break;
1235 }
1236 }
1237
1238 if (step < 2) {
1239 step++;
1240 goto next_step;
1241 }
1242
1243 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001244 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001245 return nwritten;
1246}
1247
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001248int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1249{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001250 pgoff_t index = 0, end = LONG_MAX;
1251 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001252 int ret2 = 0, ret = 0;
1253
1254 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001255
1256 while (index <= end) {
1257 int i, nr_pages;
1258 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1259 PAGECACHE_TAG_WRITEBACK,
1260 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1261 if (nr_pages == 0)
1262 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001263
1264 for (i = 0; i < nr_pages; i++) {
1265 struct page *page = pvec.pages[i];
1266
1267 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001268 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001269 continue;
1270
Chao Yu4bf08ff2013-11-04 10:28:33 +08001271 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001272 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001273 if (TestClearPageError(page))
1274 ret = -EIO;
1275 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001276 }
1277 pagevec_release(&pvec);
1278 cond_resched();
1279 }
1280
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001281 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001282 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001283 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001284 ret2 = -EIO;
1285 if (!ret)
1286 ret = ret2;
1287 return ret;
1288}
1289
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001290static int f2fs_write_node_page(struct page *page,
1291 struct writeback_control *wbc)
1292{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001293 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001295 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001296 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001297 .sbi = sbi,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001298 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001299 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001300 .page = page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001301 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001302
Chao Yuecda0de2014-05-06 16:48:26 +08001303 trace_f2fs_writepage(page, NODE);
1304
Chao Yucaf00472015-01-28 17:48:42 +08001305 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001306 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001307 if (unlikely(f2fs_cp_error(sbi)))
1308 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001309
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001310 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001311
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001312 /* get old block addr of this node page */
1313 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001314 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001315
1316 get_node_info(sbi, nid, &ni);
1317
1318 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001319 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001320 ClearPageUptodate(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001321 dec_page_count(sbi, F2FS_DIRTY_NODES);
1322 unlock_page(page);
1323 return 0;
1324 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001325
Jaegeuk Kim2f97c322014-11-06 17:21:24 -08001326 if (wbc->for_reclaim) {
1327 if (!down_read_trylock(&sbi->node_write))
1328 goto redirty_out;
1329 } else {
1330 down_read(&sbi->node_write);
1331 }
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001332
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001334 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001335 write_node_page(nid, &fio);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001336 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001337 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001338 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001339 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001340
1341 if (wbc->for_reclaim)
1342 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1343
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001345
1346redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001347 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001348 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349}
1350
1351static int f2fs_write_node_pages(struct address_space *mapping,
1352 struct writeback_control *wbc)
1353{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001354 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001355 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001356
Chao Yue5748432014-05-06 16:51:24 +08001357 trace_f2fs_writepages(mapping->host, wbc, NODE);
1358
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001359 /* balancing f2fs's metadata in background */
1360 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001361
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001362 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001363 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001364 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001365
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001366 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001367 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001368 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001369 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001370 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001371
1372skip_write:
1373 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1374 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375}
1376
1377static int f2fs_set_node_page_dirty(struct page *page)
1378{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001379 trace_f2fs_set_page_dirty(page, NODE);
1380
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 SetPageUptodate(page);
1382 if (!PageDirty(page)) {
1383 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001384 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001385 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001386 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001387 return 1;
1388 }
1389 return 0;
1390}
1391
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001392/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001393 * Structure of the f2fs node operations
1394 */
1395const struct address_space_operations f2fs_node_aops = {
1396 .writepage = f2fs_write_node_page,
1397 .writepages = f2fs_write_node_pages,
1398 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001399 .invalidatepage = f2fs_invalidate_page,
1400 .releasepage = f2fs_release_page,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001401};
1402
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001403static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1404 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001405{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001406 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001407}
1408
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001409static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1410 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001411{
1412 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001413 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001414}
1415
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001416static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001417{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001418 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001419 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001420 struct nat_entry *ne;
1421 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001423 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001424 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001425
1426 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001427 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001428 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001429
Gu Zheng7bd59382013-10-22 14:52:26 +08001430 if (build) {
1431 /* do not add allocated nids */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001432 down_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001433 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001434 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001435 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1436 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001437 allocated = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001438 up_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001439 if (allocated)
1440 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001441 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001442
1443 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444 i->nid = nid;
1445 i->state = NID_NEW;
1446
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001447 if (radix_tree_preload(GFP_NOFS)) {
1448 kmem_cache_free(free_nid_slab, i);
1449 return 0;
1450 }
1451
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001452 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001453 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001454 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001455 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001456 kmem_cache_free(free_nid_slab, i);
1457 return 0;
1458 }
1459 list_add_tail(&i->list, &nm_i->free_nid_list);
1460 nm_i->fcnt++;
1461 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001462 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001463 return 1;
1464}
1465
1466static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1467{
1468 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001469 bool need_free = false;
1470
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001471 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001472 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001473 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001474 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001475 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001476 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001477 }
1478 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001479
1480 if (need_free)
1481 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001482}
1483
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001484static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001485 struct page *nat_page, nid_t start_nid)
1486{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001487 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001488 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1489 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001490 int i;
1491
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001492 i = start_nid % NAT_ENTRY_PER_BLOCK;
1493
1494 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001495
Chao Yucfb271d2013-12-05 17:15:22 +08001496 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001497 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001498
1499 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001500 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001501 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001502 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001503 break;
1504 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001505 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001506}
1507
1508static void build_free_nids(struct f2fs_sb_info *sbi)
1509{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001510 struct f2fs_nm_info *nm_i = NM_I(sbi);
1511 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1512 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001513 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001514 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001515
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001516 /* Enough entries */
1517 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1518 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001519
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001520 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001521 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001522
1523 while (1) {
1524 struct page *page = get_current_nat_page(sbi, nid);
1525
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001526 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001527 f2fs_put_page(page, 1);
1528
1529 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001530 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001531 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001532
1533 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001534 break;
1535 }
1536
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001537 /* go to the next free nat pages to find free nids abundantly */
1538 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001539
1540 /* find free nids from current sum_pages */
1541 mutex_lock(&curseg->curseg_mutex);
1542 for (i = 0; i < nats_in_cursum(sum); i++) {
1543 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1544 nid = le32_to_cpu(nid_in_journal(sum, i));
1545 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001546 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001547 else
1548 remove_free_nid(nm_i, nid);
1549 }
1550 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001551}
1552
1553/*
1554 * If this function returns success, caller can obtain a new nid
1555 * from second parameter of this function.
1556 * The returned nid could be used ino as well as nid when inode is created.
1557 */
1558bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1559{
1560 struct f2fs_nm_info *nm_i = NM_I(sbi);
1561 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001562retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001563 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001564 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001565
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001566 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001567
1568 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001569 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001570 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001571 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001572 if (i->state == NID_NEW)
1573 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001574
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001575 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001576 *nid = i->nid;
1577 i->state = NID_ALLOC;
1578 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001579 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001580 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001581 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001582 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001583
1584 /* Let's scan nat pages and its caches to get free nids */
1585 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001586 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001587 mutex_unlock(&nm_i->build_lock);
1588 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001589}
1590
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001591/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001592 * alloc_nid() should be called prior to this function.
1593 */
1594void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1595{
1596 struct f2fs_nm_info *nm_i = NM_I(sbi);
1597 struct free_nid *i;
1598
1599 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001600 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001601 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001602 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001603 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001604
1605 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001606}
1607
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001608/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001609 * alloc_nid() should be called prior to this function.
1610 */
1611void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1612{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001613 struct f2fs_nm_info *nm_i = NM_I(sbi);
1614 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001615 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001616
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001617 if (!nid)
1618 return;
1619
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001620 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001621 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001622 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001623 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001624 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001625 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001626 } else {
1627 i->state = NID_NEW;
1628 nm_i->fcnt++;
1629 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001630 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001631
1632 if (need_free)
1633 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001634}
1635
Chao Yu70cfed82014-08-02 15:26:04 +08001636void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001637{
Chao Yu28cdce02014-03-11 13:37:38 +08001638 void *src_addr, *dst_addr;
1639 size_t inline_size;
1640 struct page *ipage;
1641 struct f2fs_inode *ri;
1642
Jaegeuk Kim40813632014-09-02 15:31:18 -07001643 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001644 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001645
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001646 ri = F2FS_INODE(page);
1647 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1648 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1649 goto update_inode;
1650 }
1651
Chao Yu28cdce02014-03-11 13:37:38 +08001652 dst_addr = inline_xattr_addr(ipage);
1653 src_addr = inline_xattr_addr(page);
1654 inline_size = inline_xattr_size(inode);
1655
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001656 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001657 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001658update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001659 update_inode(inode, ipage);
1660 f2fs_put_page(ipage, 1);
1661}
1662
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001663void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001664{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001665 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001666 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1667 nid_t new_xnid = nid_of_node(page);
1668 struct node_info ni;
1669
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001670 /* 1: invalidate the previous xattr nid */
1671 if (!prev_xnid)
1672 goto recover_xnid;
1673
1674 /* Deallocate node address */
1675 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001676 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001677 invalidate_blocks(sbi, ni.blk_addr);
1678 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001679 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001680
1681recover_xnid:
1682 /* 2: allocate new xattr nid */
1683 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001684 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001685
1686 remove_free_nid(NM_I(sbi), new_xnid);
1687 get_node_info(sbi, new_xnid, &ni);
1688 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001689 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001690 F2FS_I(inode)->i_xattr_nid = new_xnid;
1691
1692 /* 3: update xattr blkaddr */
1693 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001694 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001695
1696 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001697}
1698
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001699int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1700{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001701 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001702 nid_t ino = ino_of_node(page);
1703 struct node_info old_ni, new_ni;
1704 struct page *ipage;
1705
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001706 get_node_info(sbi, ino, &old_ni);
1707
1708 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1709 return -EINVAL;
1710
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001711 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001712 if (!ipage)
1713 return -ENOMEM;
1714
arter97e1c42042014-08-06 23:22:50 +09001715 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001716 remove_free_nid(NM_I(sbi), ino);
1717
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001718 SetPageUptodate(ipage);
1719 fill_node_footer(ipage, ino, ino, 0, true);
1720
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001721 src = F2FS_INODE(page);
1722 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001723
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001724 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1725 dst->i_size = 0;
1726 dst->i_blocks = cpu_to_le64(1);
1727 dst->i_links = cpu_to_le32(1);
1728 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001729 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001730
1731 new_ni = old_ni;
1732 new_ni.ino = ino;
1733
Chao Yucfb271d2013-12-05 17:15:22 +08001734 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001735 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001736 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001737 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001738 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001739 f2fs_put_page(ipage, 1);
1740 return 0;
1741}
1742
1743int restore_node_summary(struct f2fs_sb_info *sbi,
1744 unsigned int segno, struct f2fs_summary_block *sum)
1745{
1746 struct f2fs_node *rn;
1747 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001748 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001749 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yu9ecf4b82014-12-18 18:29:05 +08001750 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001751
1752 /* scan the node segment */
1753 last_offset = sbi->blocks_per_seg;
1754 addr = START_BLOCK(sbi, segno);
1755 sum_entry = &sum->entries[0];
1756
Chao Yu9ecf4b82014-12-18 18:29:05 +08001757 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001758 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001759
arter97e1c42042014-08-06 23:22:50 +09001760 /* readahead node pages */
Chao Yu9ecf4b82014-12-18 18:29:05 +08001761 ra_meta_pages(sbi, addr, nrpages, META_POR);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001762
Chao Yu9ecf4b82014-12-18 18:29:05 +08001763 for (idx = addr; idx < addr + nrpages; idx++) {
1764 struct page *page = get_meta_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08001765
Chao Yu9ecf4b82014-12-18 18:29:05 +08001766 rn = F2FS_NODE(page);
1767 sum_entry->nid = rn->footer.nid;
1768 sum_entry->version = 0;
1769 sum_entry->ofs_in_node = 0;
1770 sum_entry++;
1771 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08001772 }
Chao Yubac4eef2014-05-27 08:41:07 +08001773
Chao Yu9ecf4b82014-12-18 18:29:05 +08001774 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08001775 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001776 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08001777 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001778}
1779
Chao Yuaec71382014-06-24 09:18:20 +08001780static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001781{
1782 struct f2fs_nm_info *nm_i = NM_I(sbi);
1783 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1784 struct f2fs_summary_block *sum = curseg->sum_blk;
1785 int i;
1786
1787 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001788 for (i = 0; i < nats_in_cursum(sum); i++) {
1789 struct nat_entry *ne;
1790 struct f2fs_nat_entry raw_ne;
1791 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1792
1793 raw_ne = nat_in_journal(sum, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001794
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001795 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001796 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001797 if (!ne) {
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001798 ne = grab_nat_entry(nm_i, nid);
1799 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001800 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001801 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001802 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001803 }
1804 update_nats_in_cursum(sum, -i);
1805 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001806}
1807
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001808static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1809 struct list_head *head, int max)
1810{
1811 struct nat_entry_set *cur;
1812
1813 if (nes->entry_cnt >= max)
1814 goto add_out;
1815
1816 list_for_each_entry(cur, head, set_list) {
1817 if (cur->entry_cnt >= nes->entry_cnt) {
1818 list_add(&nes->set_list, cur->set_list.prev);
1819 return;
1820 }
1821 }
1822add_out:
1823 list_add_tail(&nes->set_list, head);
1824}
1825
1826static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1827 struct nat_entry_set *set)
1828{
1829 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1830 struct f2fs_summary_block *sum = curseg->sum_blk;
1831 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1832 bool to_journal = true;
1833 struct f2fs_nat_block *nat_blk;
1834 struct nat_entry *ne, *cur;
1835 struct page *page = NULL;
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001836 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001837
1838 /*
1839 * there are two steps to flush nat entries:
1840 * #1, flush nat entries to journal in current hot data summary block.
1841 * #2, flush nat entries to nat page.
1842 */
1843 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1844 to_journal = false;
1845
1846 if (to_journal) {
1847 mutex_lock(&curseg->curseg_mutex);
1848 } else {
1849 page = get_next_nat_page(sbi, start_nid);
1850 nat_blk = page_address(page);
1851 f2fs_bug_on(sbi, !nat_blk);
1852 }
1853
1854 /* flush dirty nats in nat entry set */
1855 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1856 struct f2fs_nat_entry *raw_ne;
1857 nid_t nid = nat_get_nid(ne);
1858 int offset;
1859
1860 if (nat_get_blkaddr(ne) == NEW_ADDR)
1861 continue;
1862
1863 if (to_journal) {
1864 offset = lookup_journal_in_cursum(sum,
1865 NAT_JOURNAL, nid, 1);
1866 f2fs_bug_on(sbi, offset < 0);
1867 raw_ne = &nat_in_journal(sum, offset);
1868 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1869 } else {
1870 raw_ne = &nat_blk->entries[nid - start_nid];
1871 }
1872 raw_nat_from_node_info(raw_ne, &ne->ni);
1873
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001874 down_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001875 nat_reset_flag(ne);
1876 __clear_nat_cache_dirty(NM_I(sbi), ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001877 up_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001878
1879 if (nat_get_blkaddr(ne) == NULL_ADDR)
1880 add_free_nid(sbi, nid, false);
1881 }
1882
1883 if (to_journal)
1884 mutex_unlock(&curseg->curseg_mutex);
1885 else
1886 f2fs_put_page(page, 1);
1887
Changman Lee80ec2e92014-11-25 12:44:24 +09001888 f2fs_bug_on(sbi, set->entry_cnt);
1889
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001890 down_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001891 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001892 up_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001893 kmem_cache_free(nat_entry_set_slab, set);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001894}
1895
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001896/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001897 * This function is called during the checkpointing process.
1898 */
1899void flush_nat_entries(struct f2fs_sb_info *sbi)
1900{
1901 struct f2fs_nm_info *nm_i = NM_I(sbi);
1902 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1903 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001904 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001905 struct nat_entry_set *set, *tmp;
1906 unsigned int found;
1907 nid_t set_idx = 0;
1908 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001909
Changman Lee20d047c2014-11-25 12:44:23 +09001910 if (!nm_i->dirty_nat_cnt)
1911 return;
Chao Yuaec71382014-06-24 09:18:20 +08001912 /*
1913 * if there are no enough space in journal to store dirty nat
1914 * entries, remove all entries from journal and merge them
1915 * into nat entry set.
1916 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001917 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001918 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001919
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001920 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001921 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001922 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001923 unsigned idx;
1924 set_idx = setvec[found - 1]->set + 1;
1925 for (idx = 0; idx < found; idx++)
1926 __adjust_nat_entry_set(setvec[idx], &sets,
1927 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001928 }
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001929 up_write(&nm_i->nat_tree_lock);
Chao Yuaec71382014-06-24 09:18:20 +08001930
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001931 /* flush dirty nats in nat entry set */
1932 list_for_each_entry_safe(set, tmp, &sets, set_list)
1933 __flush_nat_entry_set(sbi, set);
1934
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001935 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001936}
1937
1938static int init_node_manager(struct f2fs_sb_info *sbi)
1939{
1940 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1941 struct f2fs_nm_info *nm_i = NM_I(sbi);
1942 unsigned char *version_bitmap;
1943 unsigned int nat_segs, nat_blocks;
1944
1945 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1946
1947 /* segment_count_nat includes pair segment so divide to 2. */
1948 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1949 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001950
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001951 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1952
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001953 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001954 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001955 nm_i->fcnt = 0;
1956 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001957 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001958
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001959 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001960 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001961 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
1962 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001963 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001964
1965 mutex_init(&nm_i->build_lock);
1966 spin_lock_init(&nm_i->free_nid_list_lock);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001967 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001968
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001969 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001970 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001971 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1972 if (!version_bitmap)
1973 return -EFAULT;
1974
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001975 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1976 GFP_KERNEL);
1977 if (!nm_i->nat_bitmap)
1978 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001979 return 0;
1980}
1981
1982int build_node_manager(struct f2fs_sb_info *sbi)
1983{
1984 int err;
1985
1986 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1987 if (!sbi->nm_info)
1988 return -ENOMEM;
1989
1990 err = init_node_manager(sbi);
1991 if (err)
1992 return err;
1993
1994 build_free_nids(sbi);
1995 return 0;
1996}
1997
1998void destroy_node_manager(struct f2fs_sb_info *sbi)
1999{
2000 struct f2fs_nm_info *nm_i = NM_I(sbi);
2001 struct free_nid *i, *next_i;
2002 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002003 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002004 nid_t nid = 0;
2005 unsigned int found;
2006
2007 if (!nm_i)
2008 return;
2009
2010 /* destroy free nid list */
2011 spin_lock(&nm_i->free_nid_list_lock);
2012 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002013 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002014 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002015 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002016 spin_unlock(&nm_i->free_nid_list_lock);
2017 kmem_cache_free(free_nid_slab, i);
2018 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002019 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002020 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002021 spin_unlock(&nm_i->free_nid_list_lock);
2022
2023 /* destroy nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002024 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002025 while ((found = __gang_lookup_nat_cache(nm_i,
2026 nid, NATVEC_SIZE, natvec))) {
2027 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002028
Gu Zhengb6ce3912014-03-07 18:43:24 +08002029 nid = nat_get_nid(natvec[found - 1]) + 1;
2030 for (idx = 0; idx < found; idx++)
2031 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002032 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002033 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002034
2035 /* destroy nat set cache */
2036 nid = 0;
2037 while ((found = __gang_lookup_nat_set(nm_i,
2038 nid, SETVEC_SIZE, setvec))) {
2039 unsigned idx;
2040
2041 nid = setvec[found - 1]->set + 1;
2042 for (idx = 0; idx < found; idx++) {
2043 /* entry_cnt is not zero, when cp_error was occurred */
2044 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2045 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2046 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2047 }
2048 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002049 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002050
2051 kfree(nm_i->nat_bitmap);
2052 sbi->nm_info = NULL;
2053 kfree(nm_i);
2054}
2055
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002056int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002057{
2058 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002059 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002060 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002061 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002062
2063 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002064 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002065 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002066 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002067
2068 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2069 sizeof(struct nat_entry_set));
2070 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002071 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002072 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002073
Markus Elfringce3e6d22014-11-24 15:52:00 +01002074destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002075 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002076destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002077 kmem_cache_destroy(nat_entry_slab);
2078fail:
2079 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002080}
2081
2082void destroy_node_manager_caches(void)
2083{
Chao Yuaec71382014-06-24 09:18:20 +08002084 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002085 kmem_cache_destroy(free_nid_slab);
2086 kmem_cache_destroy(nat_entry_slab);
2087}