blob: 3d60d3d34ed292e7df0434b9b37c24b40af02c6c [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;
28
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090029bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090030{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090031 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090032 struct sysinfo val;
33 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090034 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090035
36 si_meminfo(&val);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090037 /* give 25%, 25%, 50% memory for each components respectively */
38 if (type == FREE_NIDS) {
39 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12;
40 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
41 } else if (type == NAT_ENTRIES) {
42 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12;
43 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
44 } else if (type == DIRTY_DENTS) {
45 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
46 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1);
47 }
48 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090049}
50
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090051static void clear_node_page_dirty(struct page *page)
52{
53 struct address_space *mapping = page->mapping;
54 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
55 unsigned int long flags;
56
57 if (PageDirty(page)) {
58 spin_lock_irqsave(&mapping->tree_lock, flags);
59 radix_tree_tag_clear(&mapping->page_tree,
60 page_index(page),
61 PAGECACHE_TAG_DIRTY);
62 spin_unlock_irqrestore(&mapping->tree_lock, flags);
63
64 clear_page_dirty_for_io(page);
65 dec_page_count(sbi, F2FS_DIRTY_NODES);
66 }
67 ClearPageUptodate(page);
68}
69
70static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
71{
72 pgoff_t index = current_nat_addr(sbi, nid);
73 return get_meta_page(sbi, index);
74}
75
76static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
77{
78 struct page *src_page;
79 struct page *dst_page;
80 pgoff_t src_off;
81 pgoff_t dst_off;
82 void *src_addr;
83 void *dst_addr;
84 struct f2fs_nm_info *nm_i = NM_I(sbi);
85
86 src_off = current_nat_addr(sbi, nid);
87 dst_off = next_nat_addr(sbi, src_off);
88
89 /* get current nat block page with lock */
90 src_page = get_meta_page(sbi, src_off);
91
92 /* Dirty src_page means that it is already the new target NAT page. */
93 if (PageDirty(src_page))
94 return src_page;
95
96 dst_page = grab_meta_page(sbi, dst_off);
97
98 src_addr = page_address(src_page);
99 dst_addr = page_address(dst_page);
100 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
101 set_page_dirty(dst_page);
102 f2fs_put_page(src_page, 1);
103
104 set_to_next_nat(nm_i, nid);
105
106 return dst_page;
107}
108
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900109static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
110{
111 return radix_tree_lookup(&nm_i->nat_root, n);
112}
113
114static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
115 nid_t start, unsigned int nr, struct nat_entry **ep)
116{
117 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
118}
119
120static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
121{
122 list_del(&e->list);
123 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
124 nm_i->nat_cnt--;
125 kmem_cache_free(nat_entry_slab, e);
126}
127
128int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
129{
130 struct f2fs_nm_info *nm_i = NM_I(sbi);
131 struct nat_entry *e;
132 int is_cp = 1;
133
134 read_lock(&nm_i->nat_tree_lock);
135 e = __lookup_nat_cache(nm_i, nid);
136 if (e && !e->checkpointed)
137 is_cp = 0;
138 read_unlock(&nm_i->nat_tree_lock);
139 return is_cp;
140}
141
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900142bool fsync_mark_done(struct f2fs_sb_info *sbi, nid_t nid)
143{
144 struct f2fs_nm_info *nm_i = NM_I(sbi);
145 struct nat_entry *e;
146 bool fsync_done = false;
147
148 read_lock(&nm_i->nat_tree_lock);
149 e = __lookup_nat_cache(nm_i, nid);
150 if (e)
151 fsync_done = e->fsync_done;
152 read_unlock(&nm_i->nat_tree_lock);
153 return fsync_done;
154}
155
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900156static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
157{
158 struct nat_entry *new;
159
160 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
161 if (!new)
162 return NULL;
163 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
164 kmem_cache_free(nat_entry_slab, new);
165 return NULL;
166 }
167 memset(new, 0, sizeof(struct nat_entry));
168 nat_set_nid(new, nid);
Jaegeuk Kimfffc2a02014-02-21 13:17:22 +0900169 new->checkpointed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900170 list_add_tail(&new->list, &nm_i->nat_entries);
171 nm_i->nat_cnt++;
172 return new;
173}
174
175static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
176 struct f2fs_nat_entry *ne)
177{
178 struct nat_entry *e;
179retry:
180 write_lock(&nm_i->nat_tree_lock);
181 e = __lookup_nat_cache(nm_i, nid);
182 if (!e) {
183 e = grab_nat_entry(nm_i, nid);
184 if (!e) {
185 write_unlock(&nm_i->nat_tree_lock);
186 goto retry;
187 }
Chao Yu94dac222014-04-17 10:51:05 +0800188 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900189 }
190 write_unlock(&nm_i->nat_tree_lock);
191}
192
193static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900194 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900195{
196 struct f2fs_nm_info *nm_i = NM_I(sbi);
197 struct nat_entry *e;
198retry:
199 write_lock(&nm_i->nat_tree_lock);
200 e = __lookup_nat_cache(nm_i, ni->nid);
201 if (!e) {
202 e = grab_nat_entry(nm_i, ni->nid);
203 if (!e) {
204 write_unlock(&nm_i->nat_tree_lock);
205 goto retry;
206 }
207 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900208 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900209 } else if (new_blkaddr == NEW_ADDR) {
210 /*
211 * when nid is reallocated,
212 * previous nat entry can be remained in nat cache.
213 * So, reinitialize it with new information.
214 */
215 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900216 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900217 }
218
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900219 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900220 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
221 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900222 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900223 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900224 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900225 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900226 nat_get_blkaddr(e) != NULL_ADDR &&
227 new_blkaddr == NEW_ADDR);
228
229 /* increament version no as node is removed */
230 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
231 unsigned char version = nat_get_version(e);
232 nat_set_version(e, inc_node_version(version));
233 }
234
235 /* change address */
236 nat_set_blkaddr(e, new_blkaddr);
237 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900238
239 /* update fsync_mark if its inode nat entry is still alive */
240 e = __lookup_nat_cache(nm_i, ni->ino);
241 if (e)
242 e->fsync_done = fsync_done;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900243 write_unlock(&nm_i->nat_tree_lock);
244}
245
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900246int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900247{
248 struct f2fs_nm_info *nm_i = NM_I(sbi);
249
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900250 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900251 return 0;
252
253 write_lock(&nm_i->nat_tree_lock);
254 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
255 struct nat_entry *ne;
256 ne = list_first_entry(&nm_i->nat_entries,
257 struct nat_entry, list);
258 __del_from_nat_cache(nm_i, ne);
259 nr_shrink--;
260 }
261 write_unlock(&nm_i->nat_tree_lock);
262 return nr_shrink;
263}
264
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900265/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900266 * This function returns always success
267 */
268void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
269{
270 struct f2fs_nm_info *nm_i = NM_I(sbi);
271 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
272 struct f2fs_summary_block *sum = curseg->sum_blk;
273 nid_t start_nid = START_NID(nid);
274 struct f2fs_nat_block *nat_blk;
275 struct page *page = NULL;
276 struct f2fs_nat_entry ne;
277 struct nat_entry *e;
278 int i;
279
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900280 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900281 ni->nid = nid;
282
283 /* Check nat cache */
284 read_lock(&nm_i->nat_tree_lock);
285 e = __lookup_nat_cache(nm_i, nid);
286 if (e) {
287 ni->ino = nat_get_ino(e);
288 ni->blk_addr = nat_get_blkaddr(e);
289 ni->version = nat_get_version(e);
290 }
291 read_unlock(&nm_i->nat_tree_lock);
292 if (e)
293 return;
294
295 /* Check current segment summary */
296 mutex_lock(&curseg->curseg_mutex);
297 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
298 if (i >= 0) {
299 ne = nat_in_journal(sum, i);
300 node_info_from_raw_nat(ni, &ne);
301 }
302 mutex_unlock(&curseg->curseg_mutex);
303 if (i >= 0)
304 goto cache;
305
306 /* Fill node_info from nat page */
307 page = get_current_nat_page(sbi, start_nid);
308 nat_blk = (struct f2fs_nat_block *)page_address(page);
309 ne = nat_blk->entries[nid - start_nid];
310 node_info_from_raw_nat(ni, &ne);
311 f2fs_put_page(page, 1);
312cache:
313 /* cache nat entry */
314 cache_nat_entry(NM_I(sbi), nid, &ne);
315}
316
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900317/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900318 * The maximum depth is four.
319 * Offset[0] will have raw inode offset.
320 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900321static int get_node_path(struct f2fs_inode_info *fi, long block,
322 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900323{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900324 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900325 const long direct_blks = ADDRS_PER_BLOCK;
326 const long dptrs_per_blk = NIDS_PER_BLOCK;
327 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
328 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
329 int n = 0;
330 int level = 0;
331
332 noffset[0] = 0;
333
334 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900335 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900336 goto got;
337 }
338 block -= direct_index;
339 if (block < direct_blks) {
340 offset[n++] = NODE_DIR1_BLOCK;
341 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900342 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900343 level = 1;
344 goto got;
345 }
346 block -= direct_blks;
347 if (block < direct_blks) {
348 offset[n++] = NODE_DIR2_BLOCK;
349 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900350 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900351 level = 1;
352 goto got;
353 }
354 block -= direct_blks;
355 if (block < indirect_blks) {
356 offset[n++] = NODE_IND1_BLOCK;
357 noffset[n] = 3;
358 offset[n++] = block / direct_blks;
359 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900360 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900361 level = 2;
362 goto got;
363 }
364 block -= indirect_blks;
365 if (block < indirect_blks) {
366 offset[n++] = NODE_IND2_BLOCK;
367 noffset[n] = 4 + dptrs_per_blk;
368 offset[n++] = block / direct_blks;
369 noffset[n] = 5 + dptrs_per_blk + 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 < dindirect_blks) {
376 offset[n++] = NODE_DIND_BLOCK;
377 noffset[n] = 5 + (dptrs_per_blk * 2);
378 offset[n++] = block / indirect_blks;
379 noffset[n] = 6 + (dptrs_per_blk * 2) +
380 offset[n - 1] * (dptrs_per_blk + 1);
381 offset[n++] = (block / direct_blks) % dptrs_per_blk;
382 noffset[n] = 7 + (dptrs_per_blk * 2) +
383 offset[n - 2] * (dptrs_per_blk + 1) +
384 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900385 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900386 level = 3;
387 goto got;
388 } else {
389 BUG();
390 }
391got:
392 return level;
393}
394
395/*
396 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800397 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
398 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900399 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900400 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900401int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900402{
403 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
404 struct page *npage[4];
405 struct page *parent;
406 int offset[4];
407 unsigned int noffset[4];
408 nid_t nids[4];
409 int level, i;
410 int err = 0;
411
Jaegeuk Kimde936532013-08-12 21:08:03 +0900412 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900413
414 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900415 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900416
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900417 if (!npage[0]) {
418 npage[0] = get_node_page(sbi, nids[0]);
419 if (IS_ERR(npage[0]))
420 return PTR_ERR(npage[0]);
421 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900422 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900423 if (level != 0)
424 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900425 dn->inode_page = npage[0];
426 dn->inode_page_locked = true;
427
428 /* get indirect or direct nodes */
429 for (i = 1; i <= level; i++) {
430 bool done = false;
431
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900432 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900433 /* alloc new node */
434 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900435 err = -ENOSPC;
436 goto release_pages;
437 }
438
439 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900440 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900441 if (IS_ERR(npage[i])) {
442 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900443 err = PTR_ERR(npage[i]);
444 goto release_pages;
445 }
446
447 set_nid(parent, offset[i - 1], nids[i], i == 1);
448 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900449 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900450 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900451 npage[i] = get_node_page_ra(parent, offset[i - 1]);
452 if (IS_ERR(npage[i])) {
453 err = PTR_ERR(npage[i]);
454 goto release_pages;
455 }
456 done = true;
457 }
458 if (i == 1) {
459 dn->inode_page_locked = false;
460 unlock_page(parent);
461 } else {
462 f2fs_put_page(parent, 1);
463 }
464
465 if (!done) {
466 npage[i] = get_node_page(sbi, nids[i]);
467 if (IS_ERR(npage[i])) {
468 err = PTR_ERR(npage[i]);
469 f2fs_put_page(npage[0], 0);
470 goto release_out;
471 }
472 }
473 if (i < level) {
474 parent = npage[i];
475 nids[i + 1] = get_nid(parent, offset[i], false);
476 }
477 }
478 dn->nid = nids[level];
479 dn->ofs_in_node = offset[level];
480 dn->node_page = npage[level];
481 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
482 return 0;
483
484release_pages:
485 f2fs_put_page(parent, 1);
486 if (i > 1)
487 f2fs_put_page(npage[0], 0);
488release_out:
489 dn->inode_page = NULL;
490 dn->node_page = NULL;
491 return err;
492}
493
494static void truncate_node(struct dnode_of_data *dn)
495{
496 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
497 struct node_info ni;
498
499 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900500 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900501 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900502 goto invalidate;
503 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900504 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900506 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900507 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800508 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900509 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900510
511 if (dn->nid == dn->inode->i_ino) {
512 remove_orphan_inode(sbi, dn->nid);
513 dec_valid_inode_count(sbi);
514 } else {
515 sync_inode_page(dn);
516 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900517invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900518 clear_node_page_dirty(dn->node_page);
519 F2FS_SET_SB_DIRT(sbi);
520
521 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900522
523 invalidate_mapping_pages(NODE_MAPPING(sbi),
524 dn->node_page->index, dn->node_page->index);
525
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900526 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900527 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900528}
529
530static int truncate_dnode(struct dnode_of_data *dn)
531{
532 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
533 struct page *page;
534
535 if (dn->nid == 0)
536 return 1;
537
538 /* get direct node */
539 page = get_node_page(sbi, dn->nid);
540 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
541 return 1;
542 else if (IS_ERR(page))
543 return PTR_ERR(page);
544
545 /* Make dnode_of_data for parameter */
546 dn->node_page = page;
547 dn->ofs_in_node = 0;
548 truncate_data_blocks(dn);
549 truncate_node(dn);
550 return 1;
551}
552
553static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
554 int ofs, int depth)
555{
556 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
557 struct dnode_of_data rdn = *dn;
558 struct page *page;
559 struct f2fs_node *rn;
560 nid_t child_nid;
561 unsigned int child_nofs;
562 int freed = 0;
563 int i, ret;
564
565 if (dn->nid == 0)
566 return NIDS_PER_BLOCK + 1;
567
Namjae Jeon51dd6242013-04-20 01:28:52 +0900568 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
569
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900570 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900571 if (IS_ERR(page)) {
572 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900573 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900574 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900575
Gu Zheng45590712013-07-15 17:57:38 +0800576 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900577 if (depth < 3) {
578 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
579 child_nid = le32_to_cpu(rn->in.nid[i]);
580 if (child_nid == 0)
581 continue;
582 rdn.nid = child_nid;
583 ret = truncate_dnode(&rdn);
584 if (ret < 0)
585 goto out_err;
586 set_nid(page, i, 0, false);
587 }
588 } else {
589 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
590 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
591 child_nid = le32_to_cpu(rn->in.nid[i]);
592 if (child_nid == 0) {
593 child_nofs += NIDS_PER_BLOCK + 1;
594 continue;
595 }
596 rdn.nid = child_nid;
597 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
598 if (ret == (NIDS_PER_BLOCK + 1)) {
599 set_nid(page, i, 0, false);
600 child_nofs += ret;
601 } else if (ret < 0 && ret != -ENOENT) {
602 goto out_err;
603 }
604 }
605 freed = child_nofs;
606 }
607
608 if (!ofs) {
609 /* remove current indirect node */
610 dn->node_page = page;
611 truncate_node(dn);
612 freed++;
613 } else {
614 f2fs_put_page(page, 1);
615 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900616 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900617 return freed;
618
619out_err:
620 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900621 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900622 return ret;
623}
624
625static int truncate_partial_nodes(struct dnode_of_data *dn,
626 struct f2fs_inode *ri, int *offset, int depth)
627{
628 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
629 struct page *pages[2];
630 nid_t nid[3];
631 nid_t child_nid;
632 int err = 0;
633 int i;
634 int idx = depth - 2;
635
636 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
637 if (!nid[0])
638 return 0;
639
640 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800641 for (i = 0; i < idx + 1; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900642 /* refernece count'll be increased */
643 pages[i] = get_node_page(sbi, nid[i]);
644 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900645 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800646 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900647 goto fail;
648 }
649 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
650 }
651
652 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800653 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900654 child_nid = get_nid(pages[idx], i, false);
655 if (!child_nid)
656 continue;
657 dn->nid = child_nid;
658 err = truncate_dnode(dn);
659 if (err < 0)
660 goto fail;
661 set_nid(pages[idx], i, 0, false);
662 }
663
shifei10.gea225dca2013-10-29 15:32:34 +0800664 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900665 dn->node_page = pages[idx];
666 dn->nid = nid[idx];
667 truncate_node(dn);
668 } else {
669 f2fs_put_page(pages[idx], 1);
670 }
671 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800672 offset[idx + 1] = 0;
673 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900674fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800675 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900676 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900677
678 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
679
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900680 return err;
681}
682
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900683/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900684 * All the block addresses of data and nodes should be nullified.
685 */
686int truncate_inode_blocks(struct inode *inode, pgoff_t from)
687{
688 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
689 int err = 0, cont = 1;
690 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900691 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900692 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900693 struct dnode_of_data dn;
694 struct page *page;
695
Namjae Jeon51dd6242013-04-20 01:28:52 +0900696 trace_f2fs_truncate_inode_blocks_enter(inode, from);
697
Jaegeuk Kimde936532013-08-12 21:08:03 +0900698 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900699restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900700 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900701 if (IS_ERR(page)) {
702 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900703 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900704 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900705
706 set_new_dnode(&dn, inode, page, NULL, 0);
707 unlock_page(page);
708
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900709 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900710 switch (level) {
711 case 0:
712 case 1:
713 nofs = noffset[1];
714 break;
715 case 2:
716 nofs = noffset[1];
717 if (!offset[level - 1])
718 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900719 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900720 if (err < 0 && err != -ENOENT)
721 goto fail;
722 nofs += 1 + NIDS_PER_BLOCK;
723 break;
724 case 3:
725 nofs = 5 + 2 * NIDS_PER_BLOCK;
726 if (!offset[level - 1])
727 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900728 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900729 if (err < 0 && err != -ENOENT)
730 goto fail;
731 break;
732 default:
733 BUG();
734 }
735
736skip_partial:
737 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900738 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900739 switch (offset[0]) {
740 case NODE_DIR1_BLOCK:
741 case NODE_DIR2_BLOCK:
742 err = truncate_dnode(&dn);
743 break;
744
745 case NODE_IND1_BLOCK:
746 case NODE_IND2_BLOCK:
747 err = truncate_nodes(&dn, nofs, offset[1], 2);
748 break;
749
750 case NODE_DIND_BLOCK:
751 err = truncate_nodes(&dn, nofs, offset[1], 3);
752 cont = 0;
753 break;
754
755 default:
756 BUG();
757 }
758 if (err < 0 && err != -ENOENT)
759 goto fail;
760 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900761 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900762 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900763 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900764 f2fs_put_page(page, 1);
765 goto restart;
766 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900767 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900768 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900769 set_page_dirty(page);
770 unlock_page(page);
771 }
772 offset[1] = 0;
773 offset[0]++;
774 nofs += err;
775 }
776fail:
777 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900778 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900779 return err > 0 ? 0 : err;
780}
781
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900782int truncate_xattr_node(struct inode *inode, struct page *page)
783{
784 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
785 nid_t nid = F2FS_I(inode)->i_xattr_nid;
786 struct dnode_of_data dn;
787 struct page *npage;
788
789 if (!nid)
790 return 0;
791
792 npage = get_node_page(sbi, nid);
793 if (IS_ERR(npage))
794 return PTR_ERR(npage);
795
796 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900797
798 /* need to do checkpoint during fsync */
799 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
800
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900801 set_new_dnode(&dn, inode, page, npage, nid);
802
803 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800804 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900805 truncate_node(&dn);
806 return 0;
807}
808
Jaegeuk Kim39936832012-11-22 16:21:29 +0900809/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800810 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
811 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900812 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800813void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900814{
815 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
816 struct page *page;
817 nid_t ino = inode->i_ino;
818 struct dnode_of_data dn;
819
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900820 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900821 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800822 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900823
Gu Zheng58e674d2013-11-19 18:03:18 +0800824 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900825 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800826 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900828 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900829 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900830 set_new_dnode(&dn, inode, page, page, ino);
831 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832}
833
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900834struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900835{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900836 struct dnode_of_data dn;
837
838 /* allocate inode page for new inode */
839 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900840
841 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900842 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900843}
844
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900845struct page *new_node_page(struct dnode_of_data *dn,
846 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900847{
848 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900849 struct node_info old_ni, new_ni;
850 struct page *page;
851 int err;
852
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900853 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900854 return ERR_PTR(-EPERM);
855
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900856 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900857 if (!page)
858 return ERR_PTR(-ENOMEM);
859
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900860 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900861 err = -ENOSPC;
862 goto fail;
863 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900864
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900865 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900866
867 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900868 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900869 new_ni = old_ni;
870 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900871 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900872
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900873 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900874 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900875 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900876 SetPageUptodate(page);
877 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900878
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800879 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900880 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
881
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900882 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900883 if (ipage)
884 update_inode(dn->inode, ipage);
885 else
886 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900887 if (ofs == 0)
888 inc_valid_inode_count(sbi);
889
890 return page;
891
892fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900893 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900894 f2fs_put_page(page, 1);
895 return ERR_PTR(err);
896}
897
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900898/*
899 * Caller should do after getting the following values.
900 * 0: f2fs_put_page(page, 0)
901 * LOCKED_PAGE: f2fs_put_page(page, 1)
902 * error: nothing
903 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900904static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900905{
906 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
907 struct node_info ni;
908
909 get_node_info(sbi, page->index, &ni);
910
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900911 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900912 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900913 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900914 }
915
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900916 if (PageUptodate(page))
917 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900918
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900919 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900920}
921
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900922/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923 * Readahead a node page
924 */
925void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
926{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900927 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900928 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900929
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900930 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900931 if (apage && PageUptodate(apage)) {
932 f2fs_put_page(apage, 0);
933 return;
934 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900935 f2fs_put_page(apage, 0);
936
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900937 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900938 if (!apage)
939 return;
940
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900941 err = read_node_page(apage, READA);
942 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900943 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900944 else if (err == LOCKED_PAGE)
945 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900946}
947
948struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
949{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900950 struct page *page;
951 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900952repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900953 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900954 if (!page)
955 return ERR_PTR(-ENOMEM);
956
957 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900958 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900959 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900960 else if (err == LOCKED_PAGE)
961 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900962
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900963 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +0900964 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900965 f2fs_put_page(page, 1);
966 return ERR_PTR(-EIO);
967 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900968 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900969 f2fs_put_page(page, 1);
970 goto repeat;
971 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900972got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900973 mark_page_accessed(page);
974 return page;
975}
976
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900977/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900978 * Return a locked page for the desired node page.
979 * And, readahead MAX_RA_NODE number of node pages.
980 */
981struct page *get_node_page_ra(struct page *parent, int start)
982{
983 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900984 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900986 int err, i, end;
987 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988
989 /* First, try getting the desired direct node. */
990 nid = get_nid(parent, start, false);
991 if (!nid)
992 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900993repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900994 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900995 if (!page)
996 return ERR_PTR(-ENOMEM);
997
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900998 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900999 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001000 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001001 else if (err == LOCKED_PAGE)
1002 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001003
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001004 blk_start_plug(&plug);
1005
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006 /* Then, try readahead for siblings of the desired node */
1007 end = start + MAX_RA_NODE;
1008 end = min(end, NIDS_PER_BLOCK);
1009 for (i = start + 1; i < end; i++) {
1010 nid = get_nid(parent, i, false);
1011 if (!nid)
1012 continue;
1013 ra_node_page(sbi, nid);
1014 }
1015
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001016 blk_finish_plug(&plug);
1017
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001018 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001019 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001020 f2fs_put_page(page, 1);
1021 goto repeat;
1022 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001023page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001024 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025 f2fs_put_page(page, 1);
1026 return ERR_PTR(-EIO);
1027 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001028 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029 return page;
1030}
1031
1032void sync_inode_page(struct dnode_of_data *dn)
1033{
1034 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1035 update_inode(dn->inode, dn->node_page);
1036 } else if (dn->inode_page) {
1037 if (!dn->inode_page_locked)
1038 lock_page(dn->inode_page);
1039 update_inode(dn->inode, dn->inode_page);
1040 if (!dn->inode_page_locked)
1041 unlock_page(dn->inode_page);
1042 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001043 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001044 }
1045}
1046
1047int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1048 struct writeback_control *wbc)
1049{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001050 pgoff_t index, end;
1051 struct pagevec pvec;
1052 int step = ino ? 2 : 0;
1053 int nwritten = 0, wrote = 0;
1054
1055 pagevec_init(&pvec, 0);
1056
1057next_step:
1058 index = 0;
1059 end = LONG_MAX;
1060
1061 while (index <= end) {
1062 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001063 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001064 PAGECACHE_TAG_DIRTY,
1065 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1066 if (nr_pages == 0)
1067 break;
1068
1069 for (i = 0; i < nr_pages; i++) {
1070 struct page *page = pvec.pages[i];
1071
1072 /*
1073 * flushing sequence with step:
1074 * 0. indirect nodes
1075 * 1. dentry dnodes
1076 * 2. file dnodes
1077 */
1078 if (step == 0 && IS_DNODE(page))
1079 continue;
1080 if (step == 1 && (!IS_DNODE(page) ||
1081 is_cold_node(page)))
1082 continue;
1083 if (step == 2 && (!IS_DNODE(page) ||
1084 !is_cold_node(page)))
1085 continue;
1086
1087 /*
1088 * If an fsync mode,
1089 * we should not skip writing node pages.
1090 */
1091 if (ino && ino_of_node(page) == ino)
1092 lock_page(page);
1093 else if (!trylock_page(page))
1094 continue;
1095
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001096 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001097continue_unlock:
1098 unlock_page(page);
1099 continue;
1100 }
1101 if (ino && ino_of_node(page) != ino)
1102 goto continue_unlock;
1103
1104 if (!PageDirty(page)) {
1105 /* someone wrote it for us */
1106 goto continue_unlock;
1107 }
1108
1109 if (!clear_page_dirty_for_io(page))
1110 goto continue_unlock;
1111
1112 /* called by fsync() */
1113 if (ino && IS_DNODE(page)) {
1114 int mark = !is_checkpointed_node(sbi, ino);
1115 set_fsync_mark(page, 1);
1116 if (IS_INODE(page))
1117 set_dentry_mark(page, mark);
1118 nwritten++;
1119 } else {
1120 set_fsync_mark(page, 0);
1121 set_dentry_mark(page, 0);
1122 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001123 NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001124 wrote++;
1125
1126 if (--wbc->nr_to_write == 0)
1127 break;
1128 }
1129 pagevec_release(&pvec);
1130 cond_resched();
1131
1132 if (wbc->nr_to_write == 0) {
1133 step = 2;
1134 break;
1135 }
1136 }
1137
1138 if (step < 2) {
1139 step++;
1140 goto next_step;
1141 }
1142
1143 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001144 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001145 return nwritten;
1146}
1147
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001148int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1149{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001150 pgoff_t index = 0, end = LONG_MAX;
1151 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001152 int ret2 = 0, ret = 0;
1153
1154 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001155
1156 while (index <= end) {
1157 int i, nr_pages;
1158 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1159 PAGECACHE_TAG_WRITEBACK,
1160 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1161 if (nr_pages == 0)
1162 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001163
1164 for (i = 0; i < nr_pages; i++) {
1165 struct page *page = pvec.pages[i];
1166
1167 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001168 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001169 continue;
1170
Chao Yu4bf08ff2013-11-04 10:28:33 +08001171 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001172 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001173 if (TestClearPageError(page))
1174 ret = -EIO;
1175 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001176 }
1177 pagevec_release(&pvec);
1178 cond_resched();
1179 }
1180
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001181 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001182 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001183 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001184 ret2 = -EIO;
1185 if (!ret)
1186 ret = ret2;
1187 return ret;
1188}
1189
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001190static int f2fs_write_node_page(struct page *page,
1191 struct writeback_control *wbc)
1192{
1193 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1194 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001195 block_t new_addr;
1196 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001197 struct f2fs_io_info fio = {
1198 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001199 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001200 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001201
Chao Yuecda0de2014-05-06 16:48:26 +08001202 trace_f2fs_writepage(page, NODE);
1203
Chao Yucfb271d2013-12-05 17:15:22 +08001204 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001205 goto redirty_out;
1206
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001207 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001209 /* get old block addr of this node page */
1210 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001211 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001212
1213 get_node_info(sbi, nid, &ni);
1214
1215 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001216 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001217 dec_page_count(sbi, F2FS_DIRTY_NODES);
1218 unlock_page(page);
1219 return 0;
1220 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001221
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001222 if (wbc->for_reclaim)
1223 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001224
Jaegeuk Kim39936832012-11-22 16:21:29 +09001225 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001226 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001227 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001228 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001229 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001230 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001231 unlock_page(page);
1232 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001233
1234redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001235 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001236 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001237}
1238
1239static int f2fs_write_node_pages(struct address_space *mapping,
1240 struct writeback_control *wbc)
1241{
1242 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001243 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001244
Chao Yue5748432014-05-06 16:51:24 +08001245 trace_f2fs_writepages(mapping->host, wbc, NODE);
1246
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001247 /* balancing f2fs's metadata in background */
1248 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001249
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001250 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001251 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001252 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001253
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001254 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001255 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001256 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001257 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001258 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001259
1260skip_write:
1261 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1262 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001263}
1264
1265static int f2fs_set_node_page_dirty(struct page *page)
1266{
1267 struct address_space *mapping = page->mapping;
1268 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1269
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001270 trace_f2fs_set_page_dirty(page, NODE);
1271
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001272 SetPageUptodate(page);
1273 if (!PageDirty(page)) {
1274 __set_page_dirty_nobuffers(page);
1275 inc_page_count(sbi, F2FS_DIRTY_NODES);
1276 SetPagePrivate(page);
1277 return 1;
1278 }
1279 return 0;
1280}
1281
Lukas Czernerd47992f2013-05-21 23:17:23 -04001282static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1283 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001284{
1285 struct inode *inode = page->mapping->host;
1286 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1287 if (PageDirty(page))
1288 dec_page_count(sbi, F2FS_DIRTY_NODES);
1289 ClearPagePrivate(page);
1290}
1291
1292static int f2fs_release_node_page(struct page *page, gfp_t wait)
1293{
1294 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001295 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001296}
1297
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001298/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001299 * Structure of the f2fs node operations
1300 */
1301const struct address_space_operations f2fs_node_aops = {
1302 .writepage = f2fs_write_node_page,
1303 .writepages = f2fs_write_node_pages,
1304 .set_page_dirty = f2fs_set_node_page_dirty,
1305 .invalidatepage = f2fs_invalidate_node_page,
1306 .releasepage = f2fs_release_node_page,
1307};
1308
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001309static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1310 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001311{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001312 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001313}
1314
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001315static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1316 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001317{
1318 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001319 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001320}
1321
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001322static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001324 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001325 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001326 struct nat_entry *ne;
1327 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001328
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001329 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001330 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001331
1332 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001333 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001334 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001335
Gu Zheng7bd59382013-10-22 14:52:26 +08001336 if (build) {
1337 /* do not add allocated nids */
1338 read_lock(&nm_i->nat_tree_lock);
1339 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001340 if (ne &&
1341 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001342 allocated = true;
1343 read_unlock(&nm_i->nat_tree_lock);
1344 if (allocated)
1345 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001346 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001347
1348 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349 i->nid = nid;
1350 i->state = NID_NEW;
1351
1352 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001353 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001354 spin_unlock(&nm_i->free_nid_list_lock);
1355 kmem_cache_free(free_nid_slab, i);
1356 return 0;
1357 }
1358 list_add_tail(&i->list, &nm_i->free_nid_list);
1359 nm_i->fcnt++;
1360 spin_unlock(&nm_i->free_nid_list_lock);
1361 return 1;
1362}
1363
1364static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1365{
1366 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001367 bool need_free = false;
1368
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001369 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001370 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001371 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001372 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001373 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001374 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375 }
1376 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001377
1378 if (need_free)
1379 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001380}
1381
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001382static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001383 struct page *nat_page, nid_t start_nid)
1384{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001385 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1387 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001388 int i;
1389
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001390 i = start_nid % NAT_ENTRY_PER_BLOCK;
1391
1392 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001393
Chao Yucfb271d2013-12-05 17:15:22 +08001394 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001395 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001396
1397 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001398 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001399 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001400 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001401 break;
1402 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001403 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001404}
1405
1406static void build_free_nids(struct f2fs_sb_info *sbi)
1407{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001408 struct f2fs_nm_info *nm_i = NM_I(sbi);
1409 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1410 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001411 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001412 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001413
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001414 /* Enough entries */
1415 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1416 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001417
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001418 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001419 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420
1421 while (1) {
1422 struct page *page = get_current_nat_page(sbi, nid);
1423
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001424 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425 f2fs_put_page(page, 1);
1426
1427 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001428 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001429 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001430
1431 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001432 break;
1433 }
1434
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001435 /* go to the next free nat pages to find free nids abundantly */
1436 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001437
1438 /* find free nids from current sum_pages */
1439 mutex_lock(&curseg->curseg_mutex);
1440 for (i = 0; i < nats_in_cursum(sum); i++) {
1441 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1442 nid = le32_to_cpu(nid_in_journal(sum, i));
1443 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001444 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001445 else
1446 remove_free_nid(nm_i, nid);
1447 }
1448 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001449}
1450
1451/*
1452 * If this function returns success, caller can obtain a new nid
1453 * from second parameter of this function.
1454 * The returned nid could be used ino as well as nid when inode is created.
1455 */
1456bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1457{
1458 struct f2fs_nm_info *nm_i = NM_I(sbi);
1459 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001460retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001461 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001462 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001463
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001464 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001465
1466 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001467 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001468 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001469 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001470 if (i->state == NID_NEW)
1471 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001472
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001473 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001474 *nid = i->nid;
1475 i->state = NID_ALLOC;
1476 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001477 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001478 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001479 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001480 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001481
1482 /* Let's scan nat pages and its caches to get free nids */
1483 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001484 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001485 mutex_unlock(&nm_i->build_lock);
1486 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001487}
1488
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001489/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001490 * alloc_nid() should be called prior to this function.
1491 */
1492void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1493{
1494 struct f2fs_nm_info *nm_i = NM_I(sbi);
1495 struct free_nid *i;
1496
1497 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001498 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001499 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001500 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001502
1503 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001504}
1505
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001506/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001507 * alloc_nid() should be called prior to this function.
1508 */
1509void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1510{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001511 struct f2fs_nm_info *nm_i = NM_I(sbi);
1512 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001513 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001514
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001515 if (!nid)
1516 return;
1517
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001518 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001519 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001520 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001521 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001522 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001523 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001524 } else {
1525 i->state = NID_NEW;
1526 nm_i->fcnt++;
1527 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001528 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001529
1530 if (need_free)
1531 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001532}
1533
1534void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1535 struct f2fs_summary *sum, struct node_info *ni,
1536 block_t new_blkaddr)
1537{
1538 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001539 set_node_addr(sbi, ni, new_blkaddr, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001540 clear_node_page_dirty(page);
1541}
1542
Jingoo Hanb156d542014-04-15 17:51:05 +09001543static void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001544{
1545 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1546 void *src_addr, *dst_addr;
1547 size_t inline_size;
1548 struct page *ipage;
1549 struct f2fs_inode *ri;
1550
Chao Yu987c7c32014-03-12 15:59:03 +08001551 if (!f2fs_has_inline_xattr(inode))
Chao Yu28cdce02014-03-11 13:37:38 +08001552 return;
1553
1554 if (!IS_INODE(page))
1555 return;
1556
1557 ri = F2FS_INODE(page);
1558 if (!(ri->i_inline & F2FS_INLINE_XATTR))
1559 return;
1560
1561 ipage = get_node_page(sbi, inode->i_ino);
1562 f2fs_bug_on(IS_ERR(ipage));
1563
1564 dst_addr = inline_xattr_addr(ipage);
1565 src_addr = inline_xattr_addr(page);
1566 inline_size = inline_xattr_size(inode);
1567
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001568 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001569 memcpy(dst_addr, src_addr, inline_size);
1570
1571 update_inode(inode, ipage);
1572 f2fs_put_page(ipage, 1);
1573}
1574
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001575bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1576{
1577 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1578 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1579 nid_t new_xnid = nid_of_node(page);
1580 struct node_info ni;
1581
Chao Yu28cdce02014-03-11 13:37:38 +08001582 recover_inline_xattr(inode, page);
1583
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001584 if (!f2fs_has_xattr_block(ofs_of_node(page)))
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001585 return false;
1586
1587 /* 1: invalidate the previous xattr nid */
1588 if (!prev_xnid)
1589 goto recover_xnid;
1590
1591 /* Deallocate node address */
1592 get_node_info(sbi, prev_xnid, &ni);
1593 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1594 invalidate_blocks(sbi, ni.blk_addr);
1595 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001596 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001597
1598recover_xnid:
1599 /* 2: allocate new xattr nid */
1600 if (unlikely(!inc_valid_node_count(sbi, inode)))
1601 f2fs_bug_on(1);
1602
1603 remove_free_nid(NM_I(sbi), new_xnid);
1604 get_node_info(sbi, new_xnid, &ni);
1605 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001606 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001607 F2FS_I(inode)->i_xattr_nid = new_xnid;
1608
1609 /* 3: update xattr blkaddr */
1610 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001611 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001612
1613 update_inode_page(inode);
1614 return true;
1615}
1616
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001617int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1618{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001619 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001620 nid_t ino = ino_of_node(page);
1621 struct node_info old_ni, new_ni;
1622 struct page *ipage;
1623
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001624 get_node_info(sbi, ino, &old_ni);
1625
1626 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1627 return -EINVAL;
1628
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001629 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001630 if (!ipage)
1631 return -ENOMEM;
1632
1633 /* Should not use this inode from free nid list */
1634 remove_free_nid(NM_I(sbi), ino);
1635
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001636 SetPageUptodate(ipage);
1637 fill_node_footer(ipage, ino, ino, 0, true);
1638
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001639 src = F2FS_INODE(page);
1640 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001641
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001642 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1643 dst->i_size = 0;
1644 dst->i_blocks = cpu_to_le64(1);
1645 dst->i_links = cpu_to_le32(1);
1646 dst->i_xattr_nid = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001647
1648 new_ni = old_ni;
1649 new_ni.ino = ino;
1650
Chao Yucfb271d2013-12-05 17:15:22 +08001651 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001652 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001653 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001654 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001655 f2fs_put_page(ipage, 1);
1656 return 0;
1657}
1658
Chao Yu9af0ff12013-11-22 15:48:54 +08001659/*
1660 * ra_sum_pages() merge contiguous pages into one bio and submit.
1661 * these pre-readed pages are linked in pages list.
1662 */
1663static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1664 int start, int nrpages)
1665{
1666 struct page *page;
1667 int page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001668 struct f2fs_io_info fio = {
1669 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001670 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001671 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001672
1673 for (; page_idx < start + nrpages; page_idx++) {
1674 /* alloc temporal page for read node summary info*/
Chao Yua0acdfe2013-12-05 09:54:00 +08001675 page = alloc_page(GFP_F2FS_ZERO);
Gu Zhengd6537882014-03-07 18:43:36 +08001676 if (!page)
1677 break;
Chao Yu9af0ff12013-11-22 15:48:54 +08001678
1679 lock_page(page);
1680 page->index = page_idx;
1681 list_add_tail(&page->lru, pages);
1682 }
1683
1684 list_for_each_entry(page, pages, lru)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001685 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001686
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001687 f2fs_submit_merged_bio(sbi, META, READ);
Gu Zhengd6537882014-03-07 18:43:36 +08001688
1689 return page_idx - start;
Chao Yu9af0ff12013-11-22 15:48:54 +08001690}
1691
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001692int restore_node_summary(struct f2fs_sb_info *sbi,
1693 unsigned int segno, struct f2fs_summary_block *sum)
1694{
1695 struct f2fs_node *rn;
1696 struct f2fs_summary *sum_entry;
Chao Yu9af0ff12013-11-22 15:48:54 +08001697 struct page *page, *tmp;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001698 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001699 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1700 int i, last_offset, nrpages, err = 0;
1701 LIST_HEAD(page_list);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001702
1703 /* scan the node segment */
1704 last_offset = sbi->blocks_per_seg;
1705 addr = START_BLOCK(sbi, segno);
1706 sum_entry = &sum->entries[0];
1707
Gu Zhengd6537882014-03-07 18:43:36 +08001708 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001709 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001710
Chao Yu9af0ff12013-11-22 15:48:54 +08001711 /* read ahead node pages */
Gu Zhengd6537882014-03-07 18:43:36 +08001712 nrpages = ra_sum_pages(sbi, &page_list, addr, nrpages);
1713 if (!nrpages)
1714 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001715
Chao Yu9af0ff12013-11-22 15:48:54 +08001716 list_for_each_entry_safe(page, tmp, &page_list, lru) {
Gu Zhengd6537882014-03-07 18:43:36 +08001717 if (err)
1718 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001719
1720 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001721 if (unlikely(!PageUptodate(page))) {
1722 err = -EIO;
1723 } else {
Chao Yu9af0ff12013-11-22 15:48:54 +08001724 rn = F2FS_NODE(page);
1725 sum_entry->nid = rn->footer.nid;
1726 sum_entry->version = 0;
1727 sum_entry->ofs_in_node = 0;
1728 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001729 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001730 unlock_page(page);
Gu Zhengd6537882014-03-07 18:43:36 +08001731skip:
1732 list_del(&page->lru);
Chao Yu9af0ff12013-11-22 15:48:54 +08001733 __free_pages(page, 0);
1734 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001735 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001736 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001737}
1738
1739static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1740{
1741 struct f2fs_nm_info *nm_i = NM_I(sbi);
1742 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1743 struct f2fs_summary_block *sum = curseg->sum_blk;
1744 int i;
1745
1746 mutex_lock(&curseg->curseg_mutex);
1747
1748 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1749 mutex_unlock(&curseg->curseg_mutex);
1750 return false;
1751 }
1752
1753 for (i = 0; i < nats_in_cursum(sum); i++) {
1754 struct nat_entry *ne;
1755 struct f2fs_nat_entry raw_ne;
1756 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1757
1758 raw_ne = nat_in_journal(sum, i);
1759retry:
1760 write_lock(&nm_i->nat_tree_lock);
1761 ne = __lookup_nat_cache(nm_i, nid);
1762 if (ne) {
1763 __set_nat_cache_dirty(nm_i, ne);
1764 write_unlock(&nm_i->nat_tree_lock);
1765 continue;
1766 }
1767 ne = grab_nat_entry(nm_i, nid);
1768 if (!ne) {
1769 write_unlock(&nm_i->nat_tree_lock);
1770 goto retry;
1771 }
Chao Yu94dac222014-04-17 10:51:05 +08001772 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001773 __set_nat_cache_dirty(nm_i, ne);
1774 write_unlock(&nm_i->nat_tree_lock);
1775 }
1776 update_nats_in_cursum(sum, -i);
1777 mutex_unlock(&curseg->curseg_mutex);
1778 return true;
1779}
1780
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001781/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001782 * This function is called during the checkpointing process.
1783 */
1784void flush_nat_entries(struct f2fs_sb_info *sbi)
1785{
1786 struct f2fs_nm_info *nm_i = NM_I(sbi);
1787 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1788 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu2d7b8222014-03-29 11:33:17 +08001789 struct nat_entry *ne, *cur;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001790 struct page *page = NULL;
1791 struct f2fs_nat_block *nat_blk = NULL;
1792 nid_t start_nid = 0, end_nid = 0;
1793 bool flushed;
1794
1795 flushed = flush_nats_in_journal(sbi);
1796
1797 if (!flushed)
1798 mutex_lock(&curseg->curseg_mutex);
1799
1800 /* 1) flush dirty nat caches */
Chao Yu2d7b8222014-03-29 11:33:17 +08001801 list_for_each_entry_safe(ne, cur, &nm_i->dirty_nat_entries, list) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001802 nid_t nid;
1803 struct f2fs_nat_entry raw_ne;
1804 int offset = -1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001805
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001806 if (nat_get_blkaddr(ne) == NEW_ADDR)
1807 continue;
Chao Yu2d7b8222014-03-29 11:33:17 +08001808
1809 nid = nat_get_nid(ne);
1810
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001811 if (flushed)
1812 goto to_nat_page;
1813
1814 /* if there is room for nat enries in curseg->sumpage */
1815 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1816 if (offset >= 0) {
1817 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001818 goto flush_now;
1819 }
1820to_nat_page:
1821 if (!page || (start_nid > nid || nid > end_nid)) {
1822 if (page) {
1823 f2fs_put_page(page, 1);
1824 page = NULL;
1825 }
1826 start_nid = START_NID(nid);
1827 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1828
1829 /*
1830 * get nat block with dirty flag, increased reference
1831 * count, mapped and lock
1832 */
1833 page = get_next_nat_page(sbi, start_nid);
1834 nat_blk = page_address(page);
1835 }
1836
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001837 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001838 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001839flush_now:
Chao Yu94dac222014-04-17 10:51:05 +08001840 raw_nat_from_node_info(&raw_ne, &ne->ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001841
1842 if (offset < 0) {
1843 nat_blk->entries[nid - start_nid] = raw_ne;
1844 } else {
1845 nat_in_journal(sum, offset) = raw_ne;
1846 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1847 }
1848
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001849 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001850 add_free_nid(sbi, nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001851 write_lock(&nm_i->nat_tree_lock);
1852 __del_from_nat_cache(nm_i, ne);
1853 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001854 } else {
1855 write_lock(&nm_i->nat_tree_lock);
1856 __clear_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001857 write_unlock(&nm_i->nat_tree_lock);
1858 }
1859 }
1860 if (!flushed)
1861 mutex_unlock(&curseg->curseg_mutex);
1862 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001863}
1864
1865static int init_node_manager(struct f2fs_sb_info *sbi)
1866{
1867 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1868 struct f2fs_nm_info *nm_i = NM_I(sbi);
1869 unsigned char *version_bitmap;
1870 unsigned int nat_segs, nat_blocks;
1871
1872 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1873
1874 /* segment_count_nat includes pair segment so divide to 2. */
1875 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1876 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001877
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001878 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1879
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001880 /* not used nids: 0, node, meta, (and root counted as valid node) */
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001881 nm_i->available_nids = nm_i->max_nid - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001882 nm_i->fcnt = 0;
1883 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001884 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001885
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001886 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001887 INIT_LIST_HEAD(&nm_i->free_nid_list);
1888 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1889 INIT_LIST_HEAD(&nm_i->nat_entries);
1890 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1891
1892 mutex_init(&nm_i->build_lock);
1893 spin_lock_init(&nm_i->free_nid_list_lock);
1894 rwlock_init(&nm_i->nat_tree_lock);
1895
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001896 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001897 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001898 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1899 if (!version_bitmap)
1900 return -EFAULT;
1901
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001902 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1903 GFP_KERNEL);
1904 if (!nm_i->nat_bitmap)
1905 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001906 return 0;
1907}
1908
1909int build_node_manager(struct f2fs_sb_info *sbi)
1910{
1911 int err;
1912
1913 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1914 if (!sbi->nm_info)
1915 return -ENOMEM;
1916
1917 err = init_node_manager(sbi);
1918 if (err)
1919 return err;
1920
1921 build_free_nids(sbi);
1922 return 0;
1923}
1924
1925void destroy_node_manager(struct f2fs_sb_info *sbi)
1926{
1927 struct f2fs_nm_info *nm_i = NM_I(sbi);
1928 struct free_nid *i, *next_i;
1929 struct nat_entry *natvec[NATVEC_SIZE];
1930 nid_t nid = 0;
1931 unsigned int found;
1932
1933 if (!nm_i)
1934 return;
1935
1936 /* destroy free nid list */
1937 spin_lock(&nm_i->free_nid_list_lock);
1938 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001939 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001940 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001941 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001942 spin_unlock(&nm_i->free_nid_list_lock);
1943 kmem_cache_free(free_nid_slab, i);
1944 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001945 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001946 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001947 spin_unlock(&nm_i->free_nid_list_lock);
1948
1949 /* destroy nat cache */
1950 write_lock(&nm_i->nat_tree_lock);
1951 while ((found = __gang_lookup_nat_cache(nm_i,
1952 nid, NATVEC_SIZE, natvec))) {
1953 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08001954 nid = nat_get_nid(natvec[found - 1]) + 1;
1955 for (idx = 0; idx < found; idx++)
1956 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001957 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001958 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001959 write_unlock(&nm_i->nat_tree_lock);
1960
1961 kfree(nm_i->nat_bitmap);
1962 sbi->nm_info = NULL;
1963 kfree(nm_i);
1964}
1965
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001966int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001967{
1968 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001969 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001970 if (!nat_entry_slab)
1971 return -ENOMEM;
1972
1973 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08001974 sizeof(struct free_nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001975 if (!free_nid_slab) {
1976 kmem_cache_destroy(nat_entry_slab);
1977 return -ENOMEM;
1978 }
1979 return 0;
1980}
1981
1982void destroy_node_manager_caches(void)
1983{
1984 kmem_cache_destroy(free_nid_slab);
1985 kmem_cache_destroy(nat_entry_slab);
1986}