blob: d19d6b18cd4ea387f51d8471552e42ebb99d085c [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"
Namjae Jeon51dd6242013-04-20 01:28:52 +090022#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090023
Gu Zhengf978f5a2014-02-21 18:08:29 +080024#define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
25
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090026static struct kmem_cache *nat_entry_slab;
27static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080028static struct kmem_cache *nat_entry_set_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090029
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090030bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090031{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090032 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090033 struct sysinfo val;
34 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090035 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090036
37 si_meminfo(&val);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090038 /* give 25%, 25%, 50% memory for each components respectively */
39 if (type == FREE_NIDS) {
40 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12;
41 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
42 } else if (type == NAT_ENTRIES) {
43 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12;
44 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
45 } else if (type == DIRTY_DENTS) {
Jaegeuk Kim2743f862014-06-28 01:00:41 +090046 if (sbi->sb->s_bdi->dirty_exceeded)
47 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090048 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
49 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1);
50 }
51 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090052}
53
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090054static void clear_node_page_dirty(struct page *page)
55{
56 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090057 unsigned int long flags;
58
59 if (PageDirty(page)) {
60 spin_lock_irqsave(&mapping->tree_lock, flags);
61 radix_tree_tag_clear(&mapping->page_tree,
62 page_index(page),
63 PAGECACHE_TAG_DIRTY);
64 spin_unlock_irqrestore(&mapping->tree_lock, flags);
65
66 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070067 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090068 }
69 ClearPageUptodate(page);
70}
71
72static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
73{
74 pgoff_t index = current_nat_addr(sbi, nid);
75 return get_meta_page(sbi, index);
76}
77
78static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
79{
80 struct page *src_page;
81 struct page *dst_page;
82 pgoff_t src_off;
83 pgoff_t dst_off;
84 void *src_addr;
85 void *dst_addr;
86 struct f2fs_nm_info *nm_i = NM_I(sbi);
87
88 src_off = current_nat_addr(sbi, nid);
89 dst_off = next_nat_addr(sbi, src_off);
90
91 /* get current nat block page with lock */
92 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090093 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -070094 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090095
96 src_addr = page_address(src_page);
97 dst_addr = page_address(dst_page);
98 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
99 set_page_dirty(dst_page);
100 f2fs_put_page(src_page, 1);
101
102 set_to_next_nat(nm_i, nid);
103
104 return dst_page;
105}
106
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900107static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
108{
109 return radix_tree_lookup(&nm_i->nat_root, n);
110}
111
112static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
113 nid_t start, unsigned int nr, struct nat_entry **ep)
114{
115 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
116}
117
118static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
119{
120 list_del(&e->list);
121 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
122 nm_i->nat_cnt--;
123 kmem_cache_free(nat_entry_slab, e);
124}
125
126int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
127{
128 struct f2fs_nm_info *nm_i = NM_I(sbi);
129 struct nat_entry *e;
130 int is_cp = 1;
131
132 read_lock(&nm_i->nat_tree_lock);
133 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700134 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900135 is_cp = 0;
136 read_unlock(&nm_i->nat_tree_lock);
137 return is_cp;
138}
139
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900140bool fsync_mark_done(struct f2fs_sb_info *sbi, nid_t nid)
141{
142 struct f2fs_nm_info *nm_i = NM_I(sbi);
143 struct nat_entry *e;
144 bool fsync_done = false;
145
146 read_lock(&nm_i->nat_tree_lock);
147 e = __lookup_nat_cache(nm_i, nid);
148 if (e)
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700149 fsync_done = get_nat_flag(e, HAS_FSYNC_MARK);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900150 read_unlock(&nm_i->nat_tree_lock);
151 return fsync_done;
152}
153
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900154void fsync_mark_clear(struct f2fs_sb_info *sbi, nid_t nid)
155{
156 struct f2fs_nm_info *nm_i = NM_I(sbi);
157 struct nat_entry *e;
158
159 write_lock(&nm_i->nat_tree_lock);
160 e = __lookup_nat_cache(nm_i, nid);
161 if (e)
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700162 set_nat_flag(e, HAS_FSYNC_MARK, false);
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900163 write_unlock(&nm_i->nat_tree_lock);
164}
165
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900166static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
167{
168 struct nat_entry *new;
169
170 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
171 if (!new)
172 return NULL;
173 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
174 kmem_cache_free(nat_entry_slab, new);
175 return NULL;
176 }
177 memset(new, 0, sizeof(struct nat_entry));
178 nat_set_nid(new, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700179 set_nat_flag(new, IS_CHECKPOINTED, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900180 list_add_tail(&new->list, &nm_i->nat_entries);
181 nm_i->nat_cnt++;
182 return new;
183}
184
185static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
186 struct f2fs_nat_entry *ne)
187{
188 struct nat_entry *e;
189retry:
190 write_lock(&nm_i->nat_tree_lock);
191 e = __lookup_nat_cache(nm_i, nid);
192 if (!e) {
193 e = grab_nat_entry(nm_i, nid);
194 if (!e) {
195 write_unlock(&nm_i->nat_tree_lock);
196 goto retry;
197 }
Chao Yu94dac222014-04-17 10:51:05 +0800198 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900199 }
200 write_unlock(&nm_i->nat_tree_lock);
201}
202
203static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900204 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900205{
206 struct f2fs_nm_info *nm_i = NM_I(sbi);
207 struct nat_entry *e;
208retry:
209 write_lock(&nm_i->nat_tree_lock);
210 e = __lookup_nat_cache(nm_i, ni->nid);
211 if (!e) {
212 e = grab_nat_entry(nm_i, ni->nid);
213 if (!e) {
214 write_unlock(&nm_i->nat_tree_lock);
215 goto retry;
216 }
217 e->ni = *ni;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700218 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900219 } else if (new_blkaddr == NEW_ADDR) {
220 /*
221 * when nid is reallocated,
222 * previous nat entry can be remained in nat cache.
223 * So, reinitialize it with new information.
224 */
225 e->ni = *ni;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700226 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900227 }
228
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900229 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700230 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
231 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900232 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700233 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900234 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700235 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900236 nat_get_blkaddr(e) != NULL_ADDR &&
237 new_blkaddr == NEW_ADDR);
238
arter97e1c42042014-08-06 23:22:50 +0900239 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900240 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
241 unsigned char version = nat_get_version(e);
242 nat_set_version(e, inc_node_version(version));
243 }
244
245 /* change address */
246 nat_set_blkaddr(e, new_blkaddr);
247 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900248
249 /* update fsync_mark if its inode nat entry is still alive */
250 e = __lookup_nat_cache(nm_i, ni->ino);
251 if (e)
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700252 set_nat_flag(e, HAS_FSYNC_MARK, fsync_done);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900253 write_unlock(&nm_i->nat_tree_lock);
254}
255
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900256int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900257{
258 struct f2fs_nm_info *nm_i = NM_I(sbi);
259
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900260 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900261 return 0;
262
263 write_lock(&nm_i->nat_tree_lock);
264 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
265 struct nat_entry *ne;
266 ne = list_first_entry(&nm_i->nat_entries,
267 struct nat_entry, list);
268 __del_from_nat_cache(nm_i, ne);
269 nr_shrink--;
270 }
271 write_unlock(&nm_i->nat_tree_lock);
272 return nr_shrink;
273}
274
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900275/*
arter97e1c42042014-08-06 23:22:50 +0900276 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900277 */
278void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
279{
280 struct f2fs_nm_info *nm_i = NM_I(sbi);
281 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
282 struct f2fs_summary_block *sum = curseg->sum_blk;
283 nid_t start_nid = START_NID(nid);
284 struct f2fs_nat_block *nat_blk;
285 struct page *page = NULL;
286 struct f2fs_nat_entry ne;
287 struct nat_entry *e;
288 int i;
289
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900290 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900291 ni->nid = nid;
292
293 /* Check nat cache */
294 read_lock(&nm_i->nat_tree_lock);
295 e = __lookup_nat_cache(nm_i, nid);
296 if (e) {
297 ni->ino = nat_get_ino(e);
298 ni->blk_addr = nat_get_blkaddr(e);
299 ni->version = nat_get_version(e);
300 }
301 read_unlock(&nm_i->nat_tree_lock);
302 if (e)
303 return;
304
305 /* Check current segment summary */
306 mutex_lock(&curseg->curseg_mutex);
307 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
308 if (i >= 0) {
309 ne = nat_in_journal(sum, i);
310 node_info_from_raw_nat(ni, &ne);
311 }
312 mutex_unlock(&curseg->curseg_mutex);
313 if (i >= 0)
314 goto cache;
315
316 /* Fill node_info from nat page */
317 page = get_current_nat_page(sbi, start_nid);
318 nat_blk = (struct f2fs_nat_block *)page_address(page);
319 ne = nat_blk->entries[nid - start_nid];
320 node_info_from_raw_nat(ni, &ne);
321 f2fs_put_page(page, 1);
322cache:
323 /* cache nat entry */
324 cache_nat_entry(NM_I(sbi), nid, &ne);
325}
326
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900327/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900328 * The maximum depth is four.
329 * Offset[0] will have raw inode offset.
330 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900331static int get_node_path(struct f2fs_inode_info *fi, long block,
332 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900333{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900334 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900335 const long direct_blks = ADDRS_PER_BLOCK;
336 const long dptrs_per_blk = NIDS_PER_BLOCK;
337 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
338 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
339 int n = 0;
340 int level = 0;
341
342 noffset[0] = 0;
343
344 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900345 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900346 goto got;
347 }
348 block -= direct_index;
349 if (block < direct_blks) {
350 offset[n++] = NODE_DIR1_BLOCK;
351 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900352 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900353 level = 1;
354 goto got;
355 }
356 block -= direct_blks;
357 if (block < direct_blks) {
358 offset[n++] = NODE_DIR2_BLOCK;
359 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900360 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900361 level = 1;
362 goto got;
363 }
364 block -= direct_blks;
365 if (block < indirect_blks) {
366 offset[n++] = NODE_IND1_BLOCK;
367 noffset[n] = 3;
368 offset[n++] = block / direct_blks;
369 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900370 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900371 level = 2;
372 goto got;
373 }
374 block -= indirect_blks;
375 if (block < indirect_blks) {
376 offset[n++] = NODE_IND2_BLOCK;
377 noffset[n] = 4 + dptrs_per_blk;
378 offset[n++] = block / direct_blks;
379 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900380 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900381 level = 2;
382 goto got;
383 }
384 block -= indirect_blks;
385 if (block < dindirect_blks) {
386 offset[n++] = NODE_DIND_BLOCK;
387 noffset[n] = 5 + (dptrs_per_blk * 2);
388 offset[n++] = block / indirect_blks;
389 noffset[n] = 6 + (dptrs_per_blk * 2) +
390 offset[n - 1] * (dptrs_per_blk + 1);
391 offset[n++] = (block / direct_blks) % dptrs_per_blk;
392 noffset[n] = 7 + (dptrs_per_blk * 2) +
393 offset[n - 2] * (dptrs_per_blk + 1) +
394 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900395 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900396 level = 3;
397 goto got;
398 } else {
399 BUG();
400 }
401got:
402 return level;
403}
404
405/*
406 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800407 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
408 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900409 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900411int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700413 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900414 struct page *npage[4];
415 struct page *parent;
416 int offset[4];
417 unsigned int noffset[4];
418 nid_t nids[4];
419 int level, i;
420 int err = 0;
421
Jaegeuk Kimde936532013-08-12 21:08:03 +0900422 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900423
424 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900425 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900426
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900427 if (!npage[0]) {
428 npage[0] = get_node_page(sbi, nids[0]);
429 if (IS_ERR(npage[0]))
430 return PTR_ERR(npage[0]);
431 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900432 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900433 if (level != 0)
434 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900435 dn->inode_page = npage[0];
436 dn->inode_page_locked = true;
437
438 /* get indirect or direct nodes */
439 for (i = 1; i <= level; i++) {
440 bool done = false;
441
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900442 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900443 /* alloc new node */
444 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900445 err = -ENOSPC;
446 goto release_pages;
447 }
448
449 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900450 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900451 if (IS_ERR(npage[i])) {
452 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900453 err = PTR_ERR(npage[i]);
454 goto release_pages;
455 }
456
457 set_nid(parent, offset[i - 1], nids[i], i == 1);
458 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900459 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900460 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900461 npage[i] = get_node_page_ra(parent, offset[i - 1]);
462 if (IS_ERR(npage[i])) {
463 err = PTR_ERR(npage[i]);
464 goto release_pages;
465 }
466 done = true;
467 }
468 if (i == 1) {
469 dn->inode_page_locked = false;
470 unlock_page(parent);
471 } else {
472 f2fs_put_page(parent, 1);
473 }
474
475 if (!done) {
476 npage[i] = get_node_page(sbi, nids[i]);
477 if (IS_ERR(npage[i])) {
478 err = PTR_ERR(npage[i]);
479 f2fs_put_page(npage[0], 0);
480 goto release_out;
481 }
482 }
483 if (i < level) {
484 parent = npage[i];
485 nids[i + 1] = get_nid(parent, offset[i], false);
486 }
487 }
488 dn->nid = nids[level];
489 dn->ofs_in_node = offset[level];
490 dn->node_page = npage[level];
491 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
492 return 0;
493
494release_pages:
495 f2fs_put_page(parent, 1);
496 if (i > 1)
497 f2fs_put_page(npage[0], 0);
498release_out:
499 dn->inode_page = NULL;
500 dn->node_page = NULL;
501 return err;
502}
503
504static void truncate_node(struct dnode_of_data *dn)
505{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700506 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900507 struct node_info ni;
508
509 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900510 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700511 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900512 goto invalidate;
513 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700514 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900516 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900517 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800518 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900519 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900520
521 if (dn->nid == dn->inode->i_ino) {
522 remove_orphan_inode(sbi, dn->nid);
523 dec_valid_inode_count(sbi);
524 } else {
525 sync_inode_page(dn);
526 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900527invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900528 clear_node_page_dirty(dn->node_page);
529 F2FS_SET_SB_DIRT(sbi);
530
531 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900532
533 invalidate_mapping_pages(NODE_MAPPING(sbi),
534 dn->node_page->index, dn->node_page->index);
535
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900536 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900537 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538}
539
540static int truncate_dnode(struct dnode_of_data *dn)
541{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900542 struct page *page;
543
544 if (dn->nid == 0)
545 return 1;
546
547 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700548 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900549 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
550 return 1;
551 else if (IS_ERR(page))
552 return PTR_ERR(page);
553
554 /* Make dnode_of_data for parameter */
555 dn->node_page = page;
556 dn->ofs_in_node = 0;
557 truncate_data_blocks(dn);
558 truncate_node(dn);
559 return 1;
560}
561
562static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
563 int ofs, int depth)
564{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900565 struct dnode_of_data rdn = *dn;
566 struct page *page;
567 struct f2fs_node *rn;
568 nid_t child_nid;
569 unsigned int child_nofs;
570 int freed = 0;
571 int i, ret;
572
573 if (dn->nid == 0)
574 return NIDS_PER_BLOCK + 1;
575
Namjae Jeon51dd6242013-04-20 01:28:52 +0900576 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
577
Jaegeuk Kim40813632014-09-02 15:31:18 -0700578 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900579 if (IS_ERR(page)) {
580 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900581 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900582 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900583
Gu Zheng45590712013-07-15 17:57:38 +0800584 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900585 if (depth < 3) {
586 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
587 child_nid = le32_to_cpu(rn->in.nid[i]);
588 if (child_nid == 0)
589 continue;
590 rdn.nid = child_nid;
591 ret = truncate_dnode(&rdn);
592 if (ret < 0)
593 goto out_err;
594 set_nid(page, i, 0, false);
595 }
596 } else {
597 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
598 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
599 child_nid = le32_to_cpu(rn->in.nid[i]);
600 if (child_nid == 0) {
601 child_nofs += NIDS_PER_BLOCK + 1;
602 continue;
603 }
604 rdn.nid = child_nid;
605 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
606 if (ret == (NIDS_PER_BLOCK + 1)) {
607 set_nid(page, i, 0, false);
608 child_nofs += ret;
609 } else if (ret < 0 && ret != -ENOENT) {
610 goto out_err;
611 }
612 }
613 freed = child_nofs;
614 }
615
616 if (!ofs) {
617 /* remove current indirect node */
618 dn->node_page = page;
619 truncate_node(dn);
620 freed++;
621 } else {
622 f2fs_put_page(page, 1);
623 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900624 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900625 return freed;
626
627out_err:
628 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900629 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900630 return ret;
631}
632
633static int truncate_partial_nodes(struct dnode_of_data *dn,
634 struct f2fs_inode *ri, int *offset, int depth)
635{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900636 struct page *pages[2];
637 nid_t nid[3];
638 nid_t child_nid;
639 int err = 0;
640 int i;
641 int idx = depth - 2;
642
643 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
644 if (!nid[0])
645 return 0;
646
647 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800648 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900649 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700650 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900651 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900652 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800653 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900654 goto fail;
655 }
656 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
657 }
658
659 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800660 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900661 child_nid = get_nid(pages[idx], i, false);
662 if (!child_nid)
663 continue;
664 dn->nid = child_nid;
665 err = truncate_dnode(dn);
666 if (err < 0)
667 goto fail;
668 set_nid(pages[idx], i, 0, false);
669 }
670
shifei10.gea225dca2013-10-29 15:32:34 +0800671 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900672 dn->node_page = pages[idx];
673 dn->nid = nid[idx];
674 truncate_node(dn);
675 } else {
676 f2fs_put_page(pages[idx], 1);
677 }
678 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800679 offset[idx + 1] = 0;
680 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900681fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800682 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900683 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900684
685 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
686
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900687 return err;
688}
689
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900690/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900691 * All the block addresses of data and nodes should be nullified.
692 */
693int truncate_inode_blocks(struct inode *inode, pgoff_t from)
694{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700695 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900696 int err = 0, cont = 1;
697 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900698 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900699 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900700 struct dnode_of_data dn;
701 struct page *page;
702
Namjae Jeon51dd6242013-04-20 01:28:52 +0900703 trace_f2fs_truncate_inode_blocks_enter(inode, from);
704
Jaegeuk Kimde936532013-08-12 21:08:03 +0900705 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900706restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900707 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900708 if (IS_ERR(page)) {
709 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900710 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900711 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900712
713 set_new_dnode(&dn, inode, page, NULL, 0);
714 unlock_page(page);
715
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900716 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900717 switch (level) {
718 case 0:
719 case 1:
720 nofs = noffset[1];
721 break;
722 case 2:
723 nofs = noffset[1];
724 if (!offset[level - 1])
725 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900726 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900727 if (err < 0 && err != -ENOENT)
728 goto fail;
729 nofs += 1 + NIDS_PER_BLOCK;
730 break;
731 case 3:
732 nofs = 5 + 2 * NIDS_PER_BLOCK;
733 if (!offset[level - 1])
734 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900735 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900736 if (err < 0 && err != -ENOENT)
737 goto fail;
738 break;
739 default:
740 BUG();
741 }
742
743skip_partial:
744 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900745 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900746 switch (offset[0]) {
747 case NODE_DIR1_BLOCK:
748 case NODE_DIR2_BLOCK:
749 err = truncate_dnode(&dn);
750 break;
751
752 case NODE_IND1_BLOCK:
753 case NODE_IND2_BLOCK:
754 err = truncate_nodes(&dn, nofs, offset[1], 2);
755 break;
756
757 case NODE_DIND_BLOCK:
758 err = truncate_nodes(&dn, nofs, offset[1], 3);
759 cont = 0;
760 break;
761
762 default:
763 BUG();
764 }
765 if (err < 0 && err != -ENOENT)
766 goto fail;
767 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900768 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900769 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900770 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900771 f2fs_put_page(page, 1);
772 goto restart;
773 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900774 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900775 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900776 set_page_dirty(page);
777 unlock_page(page);
778 }
779 offset[1] = 0;
780 offset[0]++;
781 nofs += err;
782 }
783fail:
784 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900785 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900786 return err > 0 ? 0 : err;
787}
788
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900789int truncate_xattr_node(struct inode *inode, struct page *page)
790{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700791 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900792 nid_t nid = F2FS_I(inode)->i_xattr_nid;
793 struct dnode_of_data dn;
794 struct page *npage;
795
796 if (!nid)
797 return 0;
798
799 npage = get_node_page(sbi, nid);
800 if (IS_ERR(npage))
801 return PTR_ERR(npage);
802
803 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900804
805 /* need to do checkpoint during fsync */
806 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
807
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900808 set_new_dnode(&dn, inode, page, npage, nid);
809
810 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800811 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900812 truncate_node(&dn);
813 return 0;
814}
815
Jaegeuk Kim39936832012-11-22 16:21:29 +0900816/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800817 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
818 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900819 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800820void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900821{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900822 struct dnode_of_data dn;
823
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700824 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
825 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800826 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700828 if (truncate_xattr_node(inode, dn.inode_page)) {
829 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800830 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700832
833 /* remove potential inline_data blocks */
834 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
835 S_ISLNK(inode->i_mode))
836 truncate_data_blocks_range(&dn, 1);
837
arter97e1c42042014-08-06 23:22:50 +0900838 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700839 f2fs_bug_on(F2FS_I_SB(inode),
840 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700841
842 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900843 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844}
845
Jaegeuk Kima014e032014-06-20 21:44:02 -0700846struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900847{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848 struct dnode_of_data dn;
849
850 /* allocate inode page for new inode */
851 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900852
853 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900854 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855}
856
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900857struct page *new_node_page(struct dnode_of_data *dn,
858 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900859{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700860 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900861 struct node_info old_ni, new_ni;
862 struct page *page;
863 int err;
864
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900865 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900866 return ERR_PTR(-EPERM);
867
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900868 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900869 if (!page)
870 return ERR_PTR(-ENOMEM);
871
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900872 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900873 err = -ENOSPC;
874 goto fail;
875 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900876
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900877 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900878
879 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700880 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900881 new_ni = old_ni;
882 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900883 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900884
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900885 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900886 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900887 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900888 SetPageUptodate(page);
889 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900890
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800891 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900892 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
893
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900894 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900895 if (ipage)
896 update_inode(dn->inode, ipage);
897 else
898 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900899 if (ofs == 0)
900 inc_valid_inode_count(sbi);
901
902 return page;
903
904fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900905 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906 f2fs_put_page(page, 1);
907 return ERR_PTR(err);
908}
909
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900910/*
911 * Caller should do after getting the following values.
912 * 0: f2fs_put_page(page, 0)
913 * LOCKED_PAGE: f2fs_put_page(page, 1)
914 * error: nothing
915 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900916static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900917{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700918 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900919 struct node_info ni;
920
921 get_node_info(sbi, page->index, &ni);
922
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900923 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900924 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900925 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900926 }
927
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900928 if (PageUptodate(page))
929 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900930
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900931 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900932}
933
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900934/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900935 * Readahead a node page
936 */
937void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
938{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900939 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900940 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900941
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900942 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900943 if (apage && PageUptodate(apage)) {
944 f2fs_put_page(apage, 0);
945 return;
946 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900947 f2fs_put_page(apage, 0);
948
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900949 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900950 if (!apage)
951 return;
952
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900953 err = read_node_page(apage, READA);
954 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900955 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900956 else if (err == LOCKED_PAGE)
957 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958}
959
960struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
961{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900962 struct page *page;
963 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900964repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900965 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900966 if (!page)
967 return ERR_PTR(-ENOMEM);
968
969 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900970 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900971 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900972 else if (err == LOCKED_PAGE)
973 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900974
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900975 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +0900976 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900977 f2fs_put_page(page, 1);
978 return ERR_PTR(-EIO);
979 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900980 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900981 f2fs_put_page(page, 1);
982 goto repeat;
983 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900984got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985 return page;
986}
987
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900988/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900989 * Return a locked page for the desired node page.
990 * And, readahead MAX_RA_NODE number of node pages.
991 */
992struct page *get_node_page_ra(struct page *parent, int start)
993{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700994 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900995 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900996 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900997 int err, i, end;
998 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999
1000 /* First, try getting the desired direct node. */
1001 nid = get_nid(parent, start, false);
1002 if (!nid)
1003 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001004repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001005 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006 if (!page)
1007 return ERR_PTR(-ENOMEM);
1008
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001009 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001010 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001011 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001012 else if (err == LOCKED_PAGE)
1013 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001014
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001015 blk_start_plug(&plug);
1016
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017 /* Then, try readahead for siblings of the desired node */
1018 end = start + MAX_RA_NODE;
1019 end = min(end, NIDS_PER_BLOCK);
1020 for (i = start + 1; i < end; i++) {
1021 nid = get_nid(parent, i, false);
1022 if (!nid)
1023 continue;
1024 ra_node_page(sbi, nid);
1025 }
1026
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001027 blk_finish_plug(&plug);
1028
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001030 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001031 f2fs_put_page(page, 1);
1032 goto repeat;
1033 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001034page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001035 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001036 f2fs_put_page(page, 1);
1037 return ERR_PTR(-EIO);
1038 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001039 return page;
1040}
1041
1042void sync_inode_page(struct dnode_of_data *dn)
1043{
1044 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1045 update_inode(dn->inode, dn->node_page);
1046 } else if (dn->inode_page) {
1047 if (!dn->inode_page_locked)
1048 lock_page(dn->inode_page);
1049 update_inode(dn->inode, dn->inode_page);
1050 if (!dn->inode_page_locked)
1051 unlock_page(dn->inode_page);
1052 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001053 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001054 }
1055}
1056
1057int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1058 struct writeback_control *wbc)
1059{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001060 pgoff_t index, end;
1061 struct pagevec pvec;
1062 int step = ino ? 2 : 0;
1063 int nwritten = 0, wrote = 0;
1064
1065 pagevec_init(&pvec, 0);
1066
1067next_step:
1068 index = 0;
1069 end = LONG_MAX;
1070
1071 while (index <= end) {
1072 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001073 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074 PAGECACHE_TAG_DIRTY,
1075 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1076 if (nr_pages == 0)
1077 break;
1078
1079 for (i = 0; i < nr_pages; i++) {
1080 struct page *page = pvec.pages[i];
1081
1082 /*
1083 * flushing sequence with step:
1084 * 0. indirect nodes
1085 * 1. dentry dnodes
1086 * 2. file dnodes
1087 */
1088 if (step == 0 && IS_DNODE(page))
1089 continue;
1090 if (step == 1 && (!IS_DNODE(page) ||
1091 is_cold_node(page)))
1092 continue;
1093 if (step == 2 && (!IS_DNODE(page) ||
1094 !is_cold_node(page)))
1095 continue;
1096
1097 /*
1098 * If an fsync mode,
1099 * we should not skip writing node pages.
1100 */
1101 if (ino && ino_of_node(page) == ino)
1102 lock_page(page);
1103 else if (!trylock_page(page))
1104 continue;
1105
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001106 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001107continue_unlock:
1108 unlock_page(page);
1109 continue;
1110 }
1111 if (ino && ino_of_node(page) != ino)
1112 goto continue_unlock;
1113
1114 if (!PageDirty(page)) {
1115 /* someone wrote it for us */
1116 goto continue_unlock;
1117 }
1118
1119 if (!clear_page_dirty_for_io(page))
1120 goto continue_unlock;
1121
1122 /* called by fsync() */
1123 if (ino && IS_DNODE(page)) {
1124 int mark = !is_checkpointed_node(sbi, ino);
1125 set_fsync_mark(page, 1);
1126 if (IS_INODE(page))
1127 set_dentry_mark(page, mark);
1128 nwritten++;
1129 } else {
1130 set_fsync_mark(page, 0);
1131 set_dentry_mark(page, 0);
1132 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001133
1134 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1135 unlock_page(page);
1136 else
1137 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001138
1139 if (--wbc->nr_to_write == 0)
1140 break;
1141 }
1142 pagevec_release(&pvec);
1143 cond_resched();
1144
1145 if (wbc->nr_to_write == 0) {
1146 step = 2;
1147 break;
1148 }
1149 }
1150
1151 if (step < 2) {
1152 step++;
1153 goto next_step;
1154 }
1155
1156 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001157 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001158 return nwritten;
1159}
1160
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001161int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1162{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001163 pgoff_t index = 0, end = LONG_MAX;
1164 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001165 int ret2 = 0, ret = 0;
1166
1167 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001168
1169 while (index <= end) {
1170 int i, nr_pages;
1171 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1172 PAGECACHE_TAG_WRITEBACK,
1173 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1174 if (nr_pages == 0)
1175 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001176
1177 for (i = 0; i < nr_pages; i++) {
1178 struct page *page = pvec.pages[i];
1179
1180 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001181 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001182 continue;
1183
Chao Yu4bf08ff2013-11-04 10:28:33 +08001184 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001185 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001186 if (TestClearPageError(page))
1187 ret = -EIO;
1188 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001189 }
1190 pagevec_release(&pvec);
1191 cond_resched();
1192 }
1193
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001194 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001195 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001196 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001197 ret2 = -EIO;
1198 if (!ret)
1199 ret = ret2;
1200 return ret;
1201}
1202
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001203static int f2fs_write_node_page(struct page *page,
1204 struct writeback_control *wbc)
1205{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001206 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001207 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208 block_t new_addr;
1209 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001210 struct f2fs_io_info fio = {
1211 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001212 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001213 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001214
Chao Yuecda0de2014-05-06 16:48:26 +08001215 trace_f2fs_writepage(page, NODE);
1216
Chao Yucfb271d2013-12-05 17:15:22 +08001217 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001218 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001219 if (unlikely(f2fs_cp_error(sbi)))
1220 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001221
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001222 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001223
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001224 /* get old block addr of this node page */
1225 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001226 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001227
1228 get_node_info(sbi, nid, &ni);
1229
1230 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001231 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001232 dec_page_count(sbi, F2FS_DIRTY_NODES);
1233 unlock_page(page);
1234 return 0;
1235 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001237 if (wbc->for_reclaim)
1238 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001239
Chao Yub3582c62014-07-03 18:58:39 +08001240 down_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001241 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001242 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001243 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001244 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001245 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001246 unlock_page(page);
1247 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001248
1249redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001250 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001251 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001252}
1253
1254static int f2fs_write_node_pages(struct address_space *mapping,
1255 struct writeback_control *wbc)
1256{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001257 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001258 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001259
Chao Yue5748432014-05-06 16:51:24 +08001260 trace_f2fs_writepages(mapping->host, wbc, NODE);
1261
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001262 /* balancing f2fs's metadata in background */
1263 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001264
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001265 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001266 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001267 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001268
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001269 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001270 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001271 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001272 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001273 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001274
1275skip_write:
1276 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1277 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001278}
1279
1280static int f2fs_set_node_page_dirty(struct page *page)
1281{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001282 trace_f2fs_set_page_dirty(page, NODE);
1283
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001284 SetPageUptodate(page);
1285 if (!PageDirty(page)) {
1286 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001287 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001288 SetPagePrivate(page);
1289 return 1;
1290 }
1291 return 0;
1292}
1293
Lukas Czernerd47992f2013-05-21 23:17:23 -04001294static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1295 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001296{
1297 struct inode *inode = page->mapping->host;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001298 if (PageDirty(page))
Jaegeuk Kim40813632014-09-02 15:31:18 -07001299 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001300 ClearPagePrivate(page);
1301}
1302
1303static int f2fs_release_node_page(struct page *page, gfp_t wait)
1304{
1305 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001306 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001307}
1308
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001309/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310 * Structure of the f2fs node operations
1311 */
1312const struct address_space_operations f2fs_node_aops = {
1313 .writepage = f2fs_write_node_page,
1314 .writepages = f2fs_write_node_pages,
1315 .set_page_dirty = f2fs_set_node_page_dirty,
1316 .invalidatepage = f2fs_invalidate_node_page,
1317 .releasepage = f2fs_release_node_page,
1318};
1319
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001320static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1321 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001322{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001323 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001324}
1325
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001326static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1327 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001328{
1329 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001330 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001331}
1332
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001333static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001334{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001335 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001336 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001337 struct nat_entry *ne;
1338 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001339
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001340 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001341 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001342
1343 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001344 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001345 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001346
Gu Zheng7bd59382013-10-22 14:52:26 +08001347 if (build) {
1348 /* do not add allocated nids */
1349 read_lock(&nm_i->nat_tree_lock);
1350 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001351 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001352 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1353 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001354 allocated = true;
1355 read_unlock(&nm_i->nat_tree_lock);
1356 if (allocated)
1357 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001358 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001359
1360 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001361 i->nid = nid;
1362 i->state = NID_NEW;
1363
1364 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001365 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001366 spin_unlock(&nm_i->free_nid_list_lock);
1367 kmem_cache_free(free_nid_slab, i);
1368 return 0;
1369 }
1370 list_add_tail(&i->list, &nm_i->free_nid_list);
1371 nm_i->fcnt++;
1372 spin_unlock(&nm_i->free_nid_list_lock);
1373 return 1;
1374}
1375
1376static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1377{
1378 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001379 bool need_free = false;
1380
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001382 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001383 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001384 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001385 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001386 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001387 }
1388 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001389
1390 if (need_free)
1391 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392}
1393
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001394static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001395 struct page *nat_page, nid_t start_nid)
1396{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001397 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001398 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1399 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001400 int i;
1401
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001402 i = start_nid % NAT_ENTRY_PER_BLOCK;
1403
1404 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001405
Chao Yucfb271d2013-12-05 17:15:22 +08001406 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001407 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001408
1409 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001410 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001411 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001412 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001413 break;
1414 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001415 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001416}
1417
1418static void build_free_nids(struct f2fs_sb_info *sbi)
1419{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420 struct f2fs_nm_info *nm_i = NM_I(sbi);
1421 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1422 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001423 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001424 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001426 /* Enough entries */
1427 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1428 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001429
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001430 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001431 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001432
1433 while (1) {
1434 struct page *page = get_current_nat_page(sbi, nid);
1435
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001436 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001437 f2fs_put_page(page, 1);
1438
1439 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001440 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001441 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001442
1443 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444 break;
1445 }
1446
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001447 /* go to the next free nat pages to find free nids abundantly */
1448 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001449
1450 /* find free nids from current sum_pages */
1451 mutex_lock(&curseg->curseg_mutex);
1452 for (i = 0; i < nats_in_cursum(sum); i++) {
1453 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1454 nid = le32_to_cpu(nid_in_journal(sum, i));
1455 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001456 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001457 else
1458 remove_free_nid(nm_i, nid);
1459 }
1460 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001461}
1462
1463/*
1464 * If this function returns success, caller can obtain a new nid
1465 * from second parameter of this function.
1466 * The returned nid could be used ino as well as nid when inode is created.
1467 */
1468bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1469{
1470 struct f2fs_nm_info *nm_i = NM_I(sbi);
1471 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001472retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001473 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001474 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001475
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001476 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001477
1478 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001479 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001480 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001481 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001482 if (i->state == NID_NEW)
1483 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001484
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001485 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001486 *nid = i->nid;
1487 i->state = NID_ALLOC;
1488 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001489 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001490 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001491 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001492 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001493
1494 /* Let's scan nat pages and its caches to get free nids */
1495 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001496 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001497 mutex_unlock(&nm_i->build_lock);
1498 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001499}
1500
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001501/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001502 * alloc_nid() should be called prior to this function.
1503 */
1504void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1505{
1506 struct f2fs_nm_info *nm_i = NM_I(sbi);
1507 struct free_nid *i;
1508
1509 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001510 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001511 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001512 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001513 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001514
1515 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001516}
1517
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001518/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001519 * alloc_nid() should be called prior to this function.
1520 */
1521void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1522{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001523 struct f2fs_nm_info *nm_i = NM_I(sbi);
1524 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001525 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001526
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001527 if (!nid)
1528 return;
1529
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001530 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001531 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001532 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001533 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001534 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001535 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001536 } else {
1537 i->state = NID_NEW;
1538 nm_i->fcnt++;
1539 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001540 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001541
1542 if (need_free)
1543 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001544}
1545
Chao Yu70cfed82014-08-02 15:26:04 +08001546void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001547{
Chao Yu28cdce02014-03-11 13:37:38 +08001548 void *src_addr, *dst_addr;
1549 size_t inline_size;
1550 struct page *ipage;
1551 struct f2fs_inode *ri;
1552
Jaegeuk Kim40813632014-09-02 15:31:18 -07001553 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001554 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001555
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001556 ri = F2FS_INODE(page);
1557 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1558 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1559 goto update_inode;
1560 }
1561
Chao Yu28cdce02014-03-11 13:37:38 +08001562 dst_addr = inline_xattr_addr(ipage);
1563 src_addr = inline_xattr_addr(page);
1564 inline_size = inline_xattr_size(inode);
1565
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001566 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001567 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001568update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001569 update_inode(inode, ipage);
1570 f2fs_put_page(ipage, 1);
1571}
1572
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001573void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001574{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001575 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001576 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1577 nid_t new_xnid = nid_of_node(page);
1578 struct node_info ni;
1579
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001580 /* 1: invalidate the previous xattr nid */
1581 if (!prev_xnid)
1582 goto recover_xnid;
1583
1584 /* Deallocate node address */
1585 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001586 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001587 invalidate_blocks(sbi, ni.blk_addr);
1588 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001589 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001590
1591recover_xnid:
1592 /* 2: allocate new xattr nid */
1593 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001594 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001595
1596 remove_free_nid(NM_I(sbi), new_xnid);
1597 get_node_info(sbi, new_xnid, &ni);
1598 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001599 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001600 F2FS_I(inode)->i_xattr_nid = new_xnid;
1601
1602 /* 3: update xattr blkaddr */
1603 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001604 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001605
1606 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001607}
1608
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001609int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1610{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001611 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001612 nid_t ino = ino_of_node(page);
1613 struct node_info old_ni, new_ni;
1614 struct page *ipage;
1615
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001616 get_node_info(sbi, ino, &old_ni);
1617
1618 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1619 return -EINVAL;
1620
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001621 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001622 if (!ipage)
1623 return -ENOMEM;
1624
arter97e1c42042014-08-06 23:22:50 +09001625 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001626 remove_free_nid(NM_I(sbi), ino);
1627
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001628 SetPageUptodate(ipage);
1629 fill_node_footer(ipage, ino, ino, 0, true);
1630
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001631 src = F2FS_INODE(page);
1632 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001633
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001634 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1635 dst->i_size = 0;
1636 dst->i_blocks = cpu_to_le64(1);
1637 dst->i_links = cpu_to_le32(1);
1638 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001639 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001640
1641 new_ni = old_ni;
1642 new_ni.ino = ino;
1643
Chao Yucfb271d2013-12-05 17:15:22 +08001644 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001645 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001646 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001647 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001648 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001649 f2fs_put_page(ipage, 1);
1650 return 0;
1651}
1652
Chao Yu9af0ff12013-11-22 15:48:54 +08001653/*
1654 * ra_sum_pages() merge contiguous pages into one bio and submit.
arter97e1c42042014-08-06 23:22:50 +09001655 * these pre-read pages are allocated in bd_inode's mapping tree.
Chao Yu9af0ff12013-11-22 15:48:54 +08001656 */
Chao Yubac4eef2014-05-27 08:41:07 +08001657static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages,
Chao Yu9af0ff12013-11-22 15:48:54 +08001658 int start, int nrpages)
1659{
Chao Yubac4eef2014-05-27 08:41:07 +08001660 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1661 struct address_space *mapping = inode->i_mapping;
1662 int i, page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001663 struct f2fs_io_info fio = {
1664 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001665 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001666 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001667
Chao Yubac4eef2014-05-27 08:41:07 +08001668 for (i = 0; page_idx < start + nrpages; page_idx++, i++) {
1669 /* alloc page in bd_inode for reading node summary info */
1670 pages[i] = grab_cache_page(mapping, page_idx);
1671 if (!pages[i])
Gu Zhengd6537882014-03-07 18:43:36 +08001672 break;
Chao Yubac4eef2014-05-27 08:41:07 +08001673 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001674 }
1675
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001676 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yubac4eef2014-05-27 08:41:07 +08001677 return i;
Chao Yu9af0ff12013-11-22 15:48:54 +08001678}
1679
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001680int restore_node_summary(struct f2fs_sb_info *sbi,
1681 unsigned int segno, struct f2fs_summary_block *sum)
1682{
1683 struct f2fs_node *rn;
1684 struct f2fs_summary *sum_entry;
Chao Yubac4eef2014-05-27 08:41:07 +08001685 struct inode *inode = sbi->sb->s_bdev->bd_inode;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001686 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001687 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Chao Yubac4eef2014-05-27 08:41:07 +08001688 struct page *pages[bio_blocks];
1689 int i, idx, last_offset, nrpages, err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001690
1691 /* scan the node segment */
1692 last_offset = sbi->blocks_per_seg;
1693 addr = START_BLOCK(sbi, segno);
1694 sum_entry = &sum->entries[0];
1695
Gu Zhengd6537882014-03-07 18:43:36 +08001696 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001697 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001698
arter97e1c42042014-08-06 23:22:50 +09001699 /* readahead node pages */
Chao Yubac4eef2014-05-27 08:41:07 +08001700 nrpages = ra_sum_pages(sbi, pages, addr, nrpages);
Gu Zhengd6537882014-03-07 18:43:36 +08001701 if (!nrpages)
1702 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001703
Chao Yubac4eef2014-05-27 08:41:07 +08001704 for (idx = 0; idx < nrpages; idx++) {
Gu Zhengd6537882014-03-07 18:43:36 +08001705 if (err)
1706 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001707
Chao Yubac4eef2014-05-27 08:41:07 +08001708 lock_page(pages[idx]);
1709 if (unlikely(!PageUptodate(pages[idx]))) {
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001710 err = -EIO;
1711 } else {
Chao Yubac4eef2014-05-27 08:41:07 +08001712 rn = F2FS_NODE(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001713 sum_entry->nid = rn->footer.nid;
1714 sum_entry->version = 0;
1715 sum_entry->ofs_in_node = 0;
1716 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001717 }
Chao Yubac4eef2014-05-27 08:41:07 +08001718 unlock_page(pages[idx]);
Gu Zhengd6537882014-03-07 18:43:36 +08001719skip:
Chao Yubac4eef2014-05-27 08:41:07 +08001720 page_cache_release(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001721 }
Chao Yubac4eef2014-05-27 08:41:07 +08001722
1723 invalidate_mapping_pages(inode->i_mapping, addr,
1724 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001725 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001726 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001727}
1728
Chao Yuaec71382014-06-24 09:18:20 +08001729static struct nat_entry_set *grab_nat_entry_set(void)
1730{
1731 struct nat_entry_set *nes =
1732 f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
1733
1734 nes->entry_cnt = 0;
1735 INIT_LIST_HEAD(&nes->set_list);
1736 INIT_LIST_HEAD(&nes->entry_list);
1737 return nes;
1738}
1739
1740static void release_nat_entry_set(struct nat_entry_set *nes,
1741 struct f2fs_nm_info *nm_i)
1742{
Chao Yuaec71382014-06-24 09:18:20 +08001743 nm_i->dirty_nat_cnt -= nes->entry_cnt;
1744 list_del(&nes->set_list);
1745 kmem_cache_free(nat_entry_set_slab, nes);
1746}
1747
1748static void adjust_nat_entry_set(struct nat_entry_set *nes,
1749 struct list_head *head)
1750{
1751 struct nat_entry_set *next = nes;
1752
1753 if (list_is_last(&nes->set_list, head))
1754 return;
1755
1756 list_for_each_entry_continue(next, head, set_list)
1757 if (nes->entry_cnt <= next->entry_cnt)
1758 break;
1759
1760 list_move_tail(&nes->set_list, &next->set_list);
1761}
1762
1763static void add_nat_entry(struct nat_entry *ne, struct list_head *head)
1764{
1765 struct nat_entry_set *nes;
1766 nid_t start_nid = START_NID(ne->ni.nid);
1767
1768 list_for_each_entry(nes, head, set_list) {
1769 if (nes->start_nid == start_nid) {
1770 list_move_tail(&ne->list, &nes->entry_list);
1771 nes->entry_cnt++;
1772 adjust_nat_entry_set(nes, head);
1773 return;
1774 }
1775 }
1776
1777 nes = grab_nat_entry_set();
1778
1779 nes->start_nid = start_nid;
1780 list_move_tail(&ne->list, &nes->entry_list);
1781 nes->entry_cnt++;
1782 list_add(&nes->set_list, head);
1783}
1784
1785static void merge_nats_in_set(struct f2fs_sb_info *sbi)
1786{
1787 struct f2fs_nm_info *nm_i = NM_I(sbi);
1788 struct list_head *dirty_list = &nm_i->dirty_nat_entries;
1789 struct list_head *set_list = &nm_i->nat_entry_set;
1790 struct nat_entry *ne, *tmp;
1791
1792 write_lock(&nm_i->nat_tree_lock);
1793 list_for_each_entry_safe(ne, tmp, dirty_list, list) {
1794 if (nat_get_blkaddr(ne) == NEW_ADDR)
1795 continue;
1796 add_nat_entry(ne, set_list);
1797 nm_i->dirty_nat_cnt++;
1798 }
1799 write_unlock(&nm_i->nat_tree_lock);
1800}
1801
Chao Yuaec71382014-06-24 09:18:20 +08001802static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001803{
1804 struct f2fs_nm_info *nm_i = NM_I(sbi);
1805 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1806 struct f2fs_summary_block *sum = curseg->sum_blk;
1807 int i;
1808
1809 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001810 for (i = 0; i < nats_in_cursum(sum); i++) {
1811 struct nat_entry *ne;
1812 struct f2fs_nat_entry raw_ne;
1813 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1814
1815 raw_ne = nat_in_journal(sum, i);
1816retry:
1817 write_lock(&nm_i->nat_tree_lock);
1818 ne = __lookup_nat_cache(nm_i, nid);
Chao Yuaec71382014-06-24 09:18:20 +08001819 if (ne)
1820 goto found;
1821
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001822 ne = grab_nat_entry(nm_i, nid);
1823 if (!ne) {
1824 write_unlock(&nm_i->nat_tree_lock);
1825 goto retry;
1826 }
Chao Yu94dac222014-04-17 10:51:05 +08001827 node_info_from_raw_nat(&ne->ni, &raw_ne);
Chao Yuaec71382014-06-24 09:18:20 +08001828found:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001829 __set_nat_cache_dirty(nm_i, ne);
1830 write_unlock(&nm_i->nat_tree_lock);
1831 }
1832 update_nats_in_cursum(sum, -i);
1833 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001834}
1835
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001836/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001837 * This function is called during the checkpointing process.
1838 */
1839void flush_nat_entries(struct f2fs_sb_info *sbi)
1840{
1841 struct f2fs_nm_info *nm_i = NM_I(sbi);
1842 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1843 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yuaec71382014-06-24 09:18:20 +08001844 struct nat_entry_set *nes, *tmp;
1845 struct list_head *head = &nm_i->nat_entry_set;
1846 bool to_journal = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001847
Chao Yuaec71382014-06-24 09:18:20 +08001848 /* merge nat entries of dirty list to nat entry set temporarily */
1849 merge_nats_in_set(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001850
Chao Yuaec71382014-06-24 09:18:20 +08001851 /*
1852 * if there are no enough space in journal to store dirty nat
1853 * entries, remove all entries from journal and merge them
1854 * into nat entry set.
1855 */
Chao Yu184a5cd2014-09-04 18:13:01 +08001856 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL)) {
Chao Yuaec71382014-06-24 09:18:20 +08001857 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001858
Chao Yuaec71382014-06-24 09:18:20 +08001859 /*
1860 * merge nat entries of dirty list to nat entry set temporarily
1861 */
1862 merge_nats_in_set(sbi);
1863 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001864
Chao Yuaec71382014-06-24 09:18:20 +08001865 if (!nm_i->dirty_nat_cnt)
1866 return;
Chao Yu2d7b8222014-03-29 11:33:17 +08001867
Chao Yuaec71382014-06-24 09:18:20 +08001868 /*
1869 * there are two steps to flush nat entries:
1870 * #1, flush nat entries to journal in current hot data summary block.
1871 * #2, flush nat entries to nat page.
1872 */
1873 list_for_each_entry_safe(nes, tmp, head, set_list) {
1874 struct f2fs_nat_block *nat_blk;
1875 struct nat_entry *ne, *cur;
1876 struct page *page;
1877 nid_t start_nid = nes->start_nid;
Chao Yu2d7b8222014-03-29 11:33:17 +08001878
Chao Yu184a5cd2014-09-04 18:13:01 +08001879 if (to_journal &&
1880 !__has_cursum_space(sum, nes->entry_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001881 to_journal = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001882
Chao Yuaec71382014-06-24 09:18:20 +08001883 if (to_journal) {
1884 mutex_lock(&curseg->curseg_mutex);
1885 } else {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001886 page = get_next_nat_page(sbi, start_nid);
1887 nat_blk = page_address(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001888 f2fs_bug_on(sbi, !nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001889 }
1890
Chao Yuaec71382014-06-24 09:18:20 +08001891 /* flush dirty nats in nat entry set */
1892 list_for_each_entry_safe(ne, cur, &nes->entry_list, list) {
1893 struct f2fs_nat_entry *raw_ne;
1894 nid_t nid = nat_get_nid(ne);
1895 int offset;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001896
Chao Yuaec71382014-06-24 09:18:20 +08001897 if (to_journal) {
1898 offset = lookup_journal_in_cursum(sum,
1899 NAT_JOURNAL, nid, 1);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001900 f2fs_bug_on(sbi, offset < 0);
Chao Yuaec71382014-06-24 09:18:20 +08001901 raw_ne = &nat_in_journal(sum, offset);
1902 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1903 } else {
1904 raw_ne = &nat_blk->entries[nid - start_nid];
1905 }
1906 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001907
Chao Yuaec71382014-06-24 09:18:20 +08001908 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001909 add_free_nid(sbi, nid, false) <= 0) {
Chao Yuaec71382014-06-24 09:18:20 +08001910 write_lock(&nm_i->nat_tree_lock);
1911 __del_from_nat_cache(nm_i, ne);
1912 write_unlock(&nm_i->nat_tree_lock);
1913 } else {
1914 write_lock(&nm_i->nat_tree_lock);
1915 __clear_nat_cache_dirty(nm_i, ne);
1916 write_unlock(&nm_i->nat_tree_lock);
1917 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001918 }
Chao Yuaec71382014-06-24 09:18:20 +08001919
1920 if (to_journal)
1921 mutex_unlock(&curseg->curseg_mutex);
1922 else
1923 f2fs_put_page(page, 1);
1924
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001925 f2fs_bug_on(sbi, !list_empty(&nes->entry_list));
Chao Yuaec71382014-06-24 09:18:20 +08001926 release_nat_entry_set(nes, nm_i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001927 }
Chao Yuaec71382014-06-24 09:18:20 +08001928
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001929 f2fs_bug_on(sbi, !list_empty(head));
1930 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001931}
1932
1933static int init_node_manager(struct f2fs_sb_info *sbi)
1934{
1935 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1936 struct f2fs_nm_info *nm_i = NM_I(sbi);
1937 unsigned char *version_bitmap;
1938 unsigned int nat_segs, nat_blocks;
1939
1940 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1941
1942 /* segment_count_nat includes pair segment so divide to 2. */
1943 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1944 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001945
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001946 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1947
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001948 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001949 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001950 nm_i->fcnt = 0;
1951 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001952 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001953
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001954 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001955 INIT_LIST_HEAD(&nm_i->free_nid_list);
1956 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1957 INIT_LIST_HEAD(&nm_i->nat_entries);
1958 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
Chao Yuaec71382014-06-24 09:18:20 +08001959 INIT_LIST_HEAD(&nm_i->nat_entry_set);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001960
1961 mutex_init(&nm_i->build_lock);
1962 spin_lock_init(&nm_i->free_nid_list_lock);
1963 rwlock_init(&nm_i->nat_tree_lock);
1964
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001965 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001966 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001967 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1968 if (!version_bitmap)
1969 return -EFAULT;
1970
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001971 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1972 GFP_KERNEL);
1973 if (!nm_i->nat_bitmap)
1974 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001975 return 0;
1976}
1977
1978int build_node_manager(struct f2fs_sb_info *sbi)
1979{
1980 int err;
1981
1982 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1983 if (!sbi->nm_info)
1984 return -ENOMEM;
1985
1986 err = init_node_manager(sbi);
1987 if (err)
1988 return err;
1989
1990 build_free_nids(sbi);
1991 return 0;
1992}
1993
1994void destroy_node_manager(struct f2fs_sb_info *sbi)
1995{
1996 struct f2fs_nm_info *nm_i = NM_I(sbi);
1997 struct free_nid *i, *next_i;
1998 struct nat_entry *natvec[NATVEC_SIZE];
1999 nid_t nid = 0;
2000 unsigned int found;
2001
2002 if (!nm_i)
2003 return;
2004
2005 /* destroy free nid list */
2006 spin_lock(&nm_i->free_nid_list_lock);
2007 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002008 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002009 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002010 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002011 spin_unlock(&nm_i->free_nid_list_lock);
2012 kmem_cache_free(free_nid_slab, i);
2013 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002014 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002015 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002016 spin_unlock(&nm_i->free_nid_list_lock);
2017
2018 /* destroy nat cache */
2019 write_lock(&nm_i->nat_tree_lock);
2020 while ((found = __gang_lookup_nat_cache(nm_i,
2021 nid, NATVEC_SIZE, natvec))) {
2022 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08002023 nid = nat_get_nid(natvec[found - 1]) + 1;
2024 for (idx = 0; idx < found; idx++)
2025 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002026 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002027 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002028 write_unlock(&nm_i->nat_tree_lock);
2029
2030 kfree(nm_i->nat_bitmap);
2031 sbi->nm_info = NULL;
2032 kfree(nm_i);
2033}
2034
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002035int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002036{
2037 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002038 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002039 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002040 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002041
2042 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002043 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002044 if (!free_nid_slab)
2045 goto destory_nat_entry;
2046
2047 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2048 sizeof(struct nat_entry_set));
2049 if (!nat_entry_set_slab)
2050 goto destory_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002051 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002052
2053destory_free_nid:
2054 kmem_cache_destroy(free_nid_slab);
2055destory_nat_entry:
2056 kmem_cache_destroy(nat_entry_slab);
2057fail:
2058 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002059}
2060
2061void destroy_node_manager_caches(void)
2062{
Chao Yuaec71382014-06-24 09:18:20 +08002063 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002064 kmem_cache_destroy(free_nid_slab);
2065 kmem_cache_destroy(nat_entry_slab);
2066}