blob: e72b2585de687a1ce9033d347ee61a8cf252c090 [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
29static void clear_node_page_dirty(struct page *page)
30{
31 struct address_space *mapping = page->mapping;
32 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
33 unsigned int long flags;
34
35 if (PageDirty(page)) {
36 spin_lock_irqsave(&mapping->tree_lock, flags);
37 radix_tree_tag_clear(&mapping->page_tree,
38 page_index(page),
39 PAGECACHE_TAG_DIRTY);
40 spin_unlock_irqrestore(&mapping->tree_lock, flags);
41
42 clear_page_dirty_for_io(page);
43 dec_page_count(sbi, F2FS_DIRTY_NODES);
44 }
45 ClearPageUptodate(page);
46}
47
48static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
49{
50 pgoff_t index = current_nat_addr(sbi, nid);
51 return get_meta_page(sbi, index);
52}
53
54static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
55{
56 struct page *src_page;
57 struct page *dst_page;
58 pgoff_t src_off;
59 pgoff_t dst_off;
60 void *src_addr;
61 void *dst_addr;
62 struct f2fs_nm_info *nm_i = NM_I(sbi);
63
64 src_off = current_nat_addr(sbi, nid);
65 dst_off = next_nat_addr(sbi, src_off);
66
67 /* get current nat block page with lock */
68 src_page = get_meta_page(sbi, src_off);
69
70 /* Dirty src_page means that it is already the new target NAT page. */
71 if (PageDirty(src_page))
72 return src_page;
73
74 dst_page = grab_meta_page(sbi, dst_off);
75
76 src_addr = page_address(src_page);
77 dst_addr = page_address(dst_page);
78 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
79 set_page_dirty(dst_page);
80 f2fs_put_page(src_page, 1);
81
82 set_to_next_nat(nm_i, nid);
83
84 return dst_page;
85}
86
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090087static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
88{
89 return radix_tree_lookup(&nm_i->nat_root, n);
90}
91
92static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
93 nid_t start, unsigned int nr, struct nat_entry **ep)
94{
95 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
96}
97
98static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
99{
100 list_del(&e->list);
101 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
102 nm_i->nat_cnt--;
103 kmem_cache_free(nat_entry_slab, e);
104}
105
106int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
107{
108 struct f2fs_nm_info *nm_i = NM_I(sbi);
109 struct nat_entry *e;
110 int is_cp = 1;
111
112 read_lock(&nm_i->nat_tree_lock);
113 e = __lookup_nat_cache(nm_i, nid);
114 if (e && !e->checkpointed)
115 is_cp = 0;
116 read_unlock(&nm_i->nat_tree_lock);
117 return is_cp;
118}
119
120static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
121{
122 struct nat_entry *new;
123
124 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
125 if (!new)
126 return NULL;
127 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
128 kmem_cache_free(nat_entry_slab, new);
129 return NULL;
130 }
131 memset(new, 0, sizeof(struct nat_entry));
132 nat_set_nid(new, nid);
Jaegeuk Kimfffc2a02014-02-21 13:17:22 +0900133 new->checkpointed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900134 list_add_tail(&new->list, &nm_i->nat_entries);
135 nm_i->nat_cnt++;
136 return new;
137}
138
139static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
140 struct f2fs_nat_entry *ne)
141{
142 struct nat_entry *e;
143retry:
144 write_lock(&nm_i->nat_tree_lock);
145 e = __lookup_nat_cache(nm_i, nid);
146 if (!e) {
147 e = grab_nat_entry(nm_i, nid);
148 if (!e) {
149 write_unlock(&nm_i->nat_tree_lock);
150 goto retry;
151 }
152 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
153 nat_set_ino(e, le32_to_cpu(ne->ino));
154 nat_set_version(e, ne->version);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900155 }
156 write_unlock(&nm_i->nat_tree_lock);
157}
158
159static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
160 block_t new_blkaddr)
161{
162 struct f2fs_nm_info *nm_i = NM_I(sbi);
163 struct nat_entry *e;
164retry:
165 write_lock(&nm_i->nat_tree_lock);
166 e = __lookup_nat_cache(nm_i, ni->nid);
167 if (!e) {
168 e = grab_nat_entry(nm_i, ni->nid);
169 if (!e) {
170 write_unlock(&nm_i->nat_tree_lock);
171 goto retry;
172 }
173 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900174 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900175 } else if (new_blkaddr == NEW_ADDR) {
176 /*
177 * when nid is reallocated,
178 * previous nat entry can be remained in nat cache.
179 * So, reinitialize it with new information.
180 */
181 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900182 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900183 }
184
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900185 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900186 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
187 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900188 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900189 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900190 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900191 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900192 nat_get_blkaddr(e) != NULL_ADDR &&
193 new_blkaddr == NEW_ADDR);
194
195 /* increament version no as node is removed */
196 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
197 unsigned char version = nat_get_version(e);
198 nat_set_version(e, inc_node_version(version));
199 }
200
201 /* change address */
202 nat_set_blkaddr(e, new_blkaddr);
203 __set_nat_cache_dirty(nm_i, e);
204 write_unlock(&nm_i->nat_tree_lock);
205}
206
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900207int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900208{
209 struct f2fs_nm_info *nm_i = NM_I(sbi);
210
Haicheng Li6cac3752013-04-28 19:16:06 +0800211 if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900212 return 0;
213
214 write_lock(&nm_i->nat_tree_lock);
215 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
216 struct nat_entry *ne;
217 ne = list_first_entry(&nm_i->nat_entries,
218 struct nat_entry, list);
219 __del_from_nat_cache(nm_i, ne);
220 nr_shrink--;
221 }
222 write_unlock(&nm_i->nat_tree_lock);
223 return nr_shrink;
224}
225
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900226/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900227 * This function returns always success
228 */
229void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
230{
231 struct f2fs_nm_info *nm_i = NM_I(sbi);
232 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
233 struct f2fs_summary_block *sum = curseg->sum_blk;
234 nid_t start_nid = START_NID(nid);
235 struct f2fs_nat_block *nat_blk;
236 struct page *page = NULL;
237 struct f2fs_nat_entry ne;
238 struct nat_entry *e;
239 int i;
240
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900241 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900242 ni->nid = nid;
243
244 /* Check nat cache */
245 read_lock(&nm_i->nat_tree_lock);
246 e = __lookup_nat_cache(nm_i, nid);
247 if (e) {
248 ni->ino = nat_get_ino(e);
249 ni->blk_addr = nat_get_blkaddr(e);
250 ni->version = nat_get_version(e);
251 }
252 read_unlock(&nm_i->nat_tree_lock);
253 if (e)
254 return;
255
256 /* Check current segment summary */
257 mutex_lock(&curseg->curseg_mutex);
258 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
259 if (i >= 0) {
260 ne = nat_in_journal(sum, i);
261 node_info_from_raw_nat(ni, &ne);
262 }
263 mutex_unlock(&curseg->curseg_mutex);
264 if (i >= 0)
265 goto cache;
266
267 /* Fill node_info from nat page */
268 page = get_current_nat_page(sbi, start_nid);
269 nat_blk = (struct f2fs_nat_block *)page_address(page);
270 ne = nat_blk->entries[nid - start_nid];
271 node_info_from_raw_nat(ni, &ne);
272 f2fs_put_page(page, 1);
273cache:
274 /* cache nat entry */
275 cache_nat_entry(NM_I(sbi), nid, &ne);
276}
277
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900278/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900279 * The maximum depth is four.
280 * Offset[0] will have raw inode offset.
281 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900282static int get_node_path(struct f2fs_inode_info *fi, long block,
283 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900284{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900285 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900286 const long direct_blks = ADDRS_PER_BLOCK;
287 const long dptrs_per_blk = NIDS_PER_BLOCK;
288 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
289 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
290 int n = 0;
291 int level = 0;
292
293 noffset[0] = 0;
294
295 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900296 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900297 goto got;
298 }
299 block -= direct_index;
300 if (block < direct_blks) {
301 offset[n++] = NODE_DIR1_BLOCK;
302 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900303 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900304 level = 1;
305 goto got;
306 }
307 block -= direct_blks;
308 if (block < direct_blks) {
309 offset[n++] = NODE_DIR2_BLOCK;
310 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900311 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900312 level = 1;
313 goto got;
314 }
315 block -= direct_blks;
316 if (block < indirect_blks) {
317 offset[n++] = NODE_IND1_BLOCK;
318 noffset[n] = 3;
319 offset[n++] = block / direct_blks;
320 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900321 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900322 level = 2;
323 goto got;
324 }
325 block -= indirect_blks;
326 if (block < indirect_blks) {
327 offset[n++] = NODE_IND2_BLOCK;
328 noffset[n] = 4 + dptrs_per_blk;
329 offset[n++] = block / direct_blks;
330 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900331 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900332 level = 2;
333 goto got;
334 }
335 block -= indirect_blks;
336 if (block < dindirect_blks) {
337 offset[n++] = NODE_DIND_BLOCK;
338 noffset[n] = 5 + (dptrs_per_blk * 2);
339 offset[n++] = block / indirect_blks;
340 noffset[n] = 6 + (dptrs_per_blk * 2) +
341 offset[n - 1] * (dptrs_per_blk + 1);
342 offset[n++] = (block / direct_blks) % dptrs_per_blk;
343 noffset[n] = 7 + (dptrs_per_blk * 2) +
344 offset[n - 2] * (dptrs_per_blk + 1) +
345 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900346 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900347 level = 3;
348 goto got;
349 } else {
350 BUG();
351 }
352got:
353 return level;
354}
355
356/*
357 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800358 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
359 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900360 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900361 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900362int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900363{
364 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
365 struct page *npage[4];
366 struct page *parent;
367 int offset[4];
368 unsigned int noffset[4];
369 nid_t nids[4];
370 int level, i;
371 int err = 0;
372
Jaegeuk Kimde936532013-08-12 21:08:03 +0900373 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900374
375 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900376 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900377
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900378 if (!npage[0]) {
379 npage[0] = get_node_page(sbi, nids[0]);
380 if (IS_ERR(npage[0]))
381 return PTR_ERR(npage[0]);
382 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900383 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900384 if (level != 0)
385 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900386 dn->inode_page = npage[0];
387 dn->inode_page_locked = true;
388
389 /* get indirect or direct nodes */
390 for (i = 1; i <= level; i++) {
391 bool done = false;
392
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900393 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900394 /* alloc new node */
395 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900396 err = -ENOSPC;
397 goto release_pages;
398 }
399
400 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900401 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900402 if (IS_ERR(npage[i])) {
403 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900404 err = PTR_ERR(npage[i]);
405 goto release_pages;
406 }
407
408 set_nid(parent, offset[i - 1], nids[i], i == 1);
409 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900411 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 npage[i] = get_node_page_ra(parent, offset[i - 1]);
413 if (IS_ERR(npage[i])) {
414 err = PTR_ERR(npage[i]);
415 goto release_pages;
416 }
417 done = true;
418 }
419 if (i == 1) {
420 dn->inode_page_locked = false;
421 unlock_page(parent);
422 } else {
423 f2fs_put_page(parent, 1);
424 }
425
426 if (!done) {
427 npage[i] = get_node_page(sbi, nids[i]);
428 if (IS_ERR(npage[i])) {
429 err = PTR_ERR(npage[i]);
430 f2fs_put_page(npage[0], 0);
431 goto release_out;
432 }
433 }
434 if (i < level) {
435 parent = npage[i];
436 nids[i + 1] = get_nid(parent, offset[i], false);
437 }
438 }
439 dn->nid = nids[level];
440 dn->ofs_in_node = offset[level];
441 dn->node_page = npage[level];
442 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
443 return 0;
444
445release_pages:
446 f2fs_put_page(parent, 1);
447 if (i > 1)
448 f2fs_put_page(npage[0], 0);
449release_out:
450 dn->inode_page = NULL;
451 dn->node_page = NULL;
452 return err;
453}
454
455static void truncate_node(struct dnode_of_data *dn)
456{
457 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
458 struct node_info ni;
459
460 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900461 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900462 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900463 goto invalidate;
464 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900465 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900466
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900467 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900468 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800469 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900470 set_node_addr(sbi, &ni, NULL_ADDR);
471
472 if (dn->nid == dn->inode->i_ino) {
473 remove_orphan_inode(sbi, dn->nid);
474 dec_valid_inode_count(sbi);
475 } else {
476 sync_inode_page(dn);
477 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900478invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900479 clear_node_page_dirty(dn->node_page);
480 F2FS_SET_SB_DIRT(sbi);
481
482 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900483
484 invalidate_mapping_pages(NODE_MAPPING(sbi),
485 dn->node_page->index, dn->node_page->index);
486
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900487 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900488 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900489}
490
491static int truncate_dnode(struct dnode_of_data *dn)
492{
493 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
494 struct page *page;
495
496 if (dn->nid == 0)
497 return 1;
498
499 /* get direct node */
500 page = get_node_page(sbi, dn->nid);
501 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
502 return 1;
503 else if (IS_ERR(page))
504 return PTR_ERR(page);
505
506 /* Make dnode_of_data for parameter */
507 dn->node_page = page;
508 dn->ofs_in_node = 0;
509 truncate_data_blocks(dn);
510 truncate_node(dn);
511 return 1;
512}
513
514static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
515 int ofs, int depth)
516{
517 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
518 struct dnode_of_data rdn = *dn;
519 struct page *page;
520 struct f2fs_node *rn;
521 nid_t child_nid;
522 unsigned int child_nofs;
523 int freed = 0;
524 int i, ret;
525
526 if (dn->nid == 0)
527 return NIDS_PER_BLOCK + 1;
528
Namjae Jeon51dd6242013-04-20 01:28:52 +0900529 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
530
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900531 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900532 if (IS_ERR(page)) {
533 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900534 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900535 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900536
Gu Zheng45590712013-07-15 17:57:38 +0800537 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538 if (depth < 3) {
539 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
540 child_nid = le32_to_cpu(rn->in.nid[i]);
541 if (child_nid == 0)
542 continue;
543 rdn.nid = child_nid;
544 ret = truncate_dnode(&rdn);
545 if (ret < 0)
546 goto out_err;
547 set_nid(page, i, 0, false);
548 }
549 } else {
550 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
551 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
552 child_nid = le32_to_cpu(rn->in.nid[i]);
553 if (child_nid == 0) {
554 child_nofs += NIDS_PER_BLOCK + 1;
555 continue;
556 }
557 rdn.nid = child_nid;
558 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
559 if (ret == (NIDS_PER_BLOCK + 1)) {
560 set_nid(page, i, 0, false);
561 child_nofs += ret;
562 } else if (ret < 0 && ret != -ENOENT) {
563 goto out_err;
564 }
565 }
566 freed = child_nofs;
567 }
568
569 if (!ofs) {
570 /* remove current indirect node */
571 dn->node_page = page;
572 truncate_node(dn);
573 freed++;
574 } else {
575 f2fs_put_page(page, 1);
576 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900577 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900578 return freed;
579
580out_err:
581 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900582 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900583 return ret;
584}
585
586static int truncate_partial_nodes(struct dnode_of_data *dn,
587 struct f2fs_inode *ri, int *offset, int depth)
588{
589 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
590 struct page *pages[2];
591 nid_t nid[3];
592 nid_t child_nid;
593 int err = 0;
594 int i;
595 int idx = depth - 2;
596
597 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
598 if (!nid[0])
599 return 0;
600
601 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800602 for (i = 0; i < idx + 1; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900603 /* refernece count'll be increased */
604 pages[i] = get_node_page(sbi, nid[i]);
605 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900606 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800607 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900608 goto fail;
609 }
610 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
611 }
612
613 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800614 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900615 child_nid = get_nid(pages[idx], i, false);
616 if (!child_nid)
617 continue;
618 dn->nid = child_nid;
619 err = truncate_dnode(dn);
620 if (err < 0)
621 goto fail;
622 set_nid(pages[idx], i, 0, false);
623 }
624
shifei10.gea225dca2013-10-29 15:32:34 +0800625 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900626 dn->node_page = pages[idx];
627 dn->nid = nid[idx];
628 truncate_node(dn);
629 } else {
630 f2fs_put_page(pages[idx], 1);
631 }
632 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800633 offset[idx + 1] = 0;
634 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900635fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800636 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900637 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900638
639 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
640
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900641 return err;
642}
643
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900644/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900645 * All the block addresses of data and nodes should be nullified.
646 */
647int truncate_inode_blocks(struct inode *inode, pgoff_t from)
648{
649 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
650 int err = 0, cont = 1;
651 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900652 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900653 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900654 struct dnode_of_data dn;
655 struct page *page;
656
Namjae Jeon51dd6242013-04-20 01:28:52 +0900657 trace_f2fs_truncate_inode_blocks_enter(inode, from);
658
Jaegeuk Kimde936532013-08-12 21:08:03 +0900659 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900660restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900661 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900662 if (IS_ERR(page)) {
663 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900664 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900665 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900666
667 set_new_dnode(&dn, inode, page, NULL, 0);
668 unlock_page(page);
669
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900670 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900671 switch (level) {
672 case 0:
673 case 1:
674 nofs = noffset[1];
675 break;
676 case 2:
677 nofs = noffset[1];
678 if (!offset[level - 1])
679 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900680 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900681 if (err < 0 && err != -ENOENT)
682 goto fail;
683 nofs += 1 + NIDS_PER_BLOCK;
684 break;
685 case 3:
686 nofs = 5 + 2 * NIDS_PER_BLOCK;
687 if (!offset[level - 1])
688 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900689 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900690 if (err < 0 && err != -ENOENT)
691 goto fail;
692 break;
693 default:
694 BUG();
695 }
696
697skip_partial:
698 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900699 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900700 switch (offset[0]) {
701 case NODE_DIR1_BLOCK:
702 case NODE_DIR2_BLOCK:
703 err = truncate_dnode(&dn);
704 break;
705
706 case NODE_IND1_BLOCK:
707 case NODE_IND2_BLOCK:
708 err = truncate_nodes(&dn, nofs, offset[1], 2);
709 break;
710
711 case NODE_DIND_BLOCK:
712 err = truncate_nodes(&dn, nofs, offset[1], 3);
713 cont = 0;
714 break;
715
716 default:
717 BUG();
718 }
719 if (err < 0 && err != -ENOENT)
720 goto fail;
721 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900722 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900723 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900724 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900725 f2fs_put_page(page, 1);
726 goto restart;
727 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900728 wait_on_page_writeback(page);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900729 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900730 set_page_dirty(page);
731 unlock_page(page);
732 }
733 offset[1] = 0;
734 offset[0]++;
735 nofs += err;
736 }
737fail:
738 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900739 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900740 return err > 0 ? 0 : err;
741}
742
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900743int truncate_xattr_node(struct inode *inode, struct page *page)
744{
745 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
746 nid_t nid = F2FS_I(inode)->i_xattr_nid;
747 struct dnode_of_data dn;
748 struct page *npage;
749
750 if (!nid)
751 return 0;
752
753 npage = get_node_page(sbi, nid);
754 if (IS_ERR(npage))
755 return PTR_ERR(npage);
756
757 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900758
759 /* need to do checkpoint during fsync */
760 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
761
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900762 set_new_dnode(&dn, inode, page, npage, nid);
763
764 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800765 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900766 truncate_node(&dn);
767 return 0;
768}
769
Jaegeuk Kim39936832012-11-22 16:21:29 +0900770/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800771 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
772 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900773 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800774void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900775{
776 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
777 struct page *page;
778 nid_t ino = inode->i_ino;
779 struct dnode_of_data dn;
780
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900781 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900782 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800783 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900784
Gu Zheng58e674d2013-11-19 18:03:18 +0800785 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900786 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800787 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900788 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900789 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900790 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900791 set_new_dnode(&dn, inode, page, page, ino);
792 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900793}
794
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900795struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900796{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900797 struct dnode_of_data dn;
798
799 /* allocate inode page for new inode */
800 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900801
802 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900803 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900804}
805
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900806struct page *new_node_page(struct dnode_of_data *dn,
807 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900808{
809 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900810 struct node_info old_ni, new_ni;
811 struct page *page;
812 int err;
813
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900814 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900815 return ERR_PTR(-EPERM);
816
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900817 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900818 if (!page)
819 return ERR_PTR(-ENOMEM);
820
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900821 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900822 err = -ENOSPC;
823 goto fail;
824 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900825
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900826 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827
828 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900829 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900830 new_ni = old_ni;
831 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900833
834 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900835 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900836 SetPageUptodate(page);
837 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900838
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900839 if (ofs == XATTR_NODE_OFFSET)
840 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
841
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900842 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900843 if (ipage)
844 update_inode(dn->inode, ipage);
845 else
846 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900847 if (ofs == 0)
848 inc_valid_inode_count(sbi);
849
850 return page;
851
852fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900853 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900854 f2fs_put_page(page, 1);
855 return ERR_PTR(err);
856}
857
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900858/*
859 * Caller should do after getting the following values.
860 * 0: f2fs_put_page(page, 0)
861 * LOCKED_PAGE: f2fs_put_page(page, 1)
862 * error: nothing
863 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900864static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900865{
866 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
867 struct node_info ni;
868
869 get_node_info(sbi, page->index, &ni);
870
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900871 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900872 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900873 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900874 }
875
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900876 if (PageUptodate(page))
877 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900878
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900879 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900880}
881
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900882/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900883 * Readahead a node page
884 */
885void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
886{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900887 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900888 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900889
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900890 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900891 if (apage && PageUptodate(apage)) {
892 f2fs_put_page(apage, 0);
893 return;
894 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900895 f2fs_put_page(apage, 0);
896
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900897 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900898 if (!apage)
899 return;
900
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900901 err = read_node_page(apage, READA);
902 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900903 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900904 else if (err == LOCKED_PAGE)
905 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906}
907
908struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
909{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900910 struct page *page;
911 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900912repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900913 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900914 if (!page)
915 return ERR_PTR(-ENOMEM);
916
917 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900918 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900919 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900920 else if (err == LOCKED_PAGE)
921 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900922
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900923 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900924 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900925 f2fs_put_page(page, 1);
926 return ERR_PTR(-EIO);
927 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900928 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900929 f2fs_put_page(page, 1);
930 goto repeat;
931 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900932got_it:
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900933 f2fs_bug_on(nid != nid_of_node(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900934 mark_page_accessed(page);
935 return page;
936}
937
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900938/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900939 * Return a locked page for the desired node page.
940 * And, readahead MAX_RA_NODE number of node pages.
941 */
942struct page *get_node_page_ra(struct page *parent, int start)
943{
944 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900945 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900946 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900947 int err, i, end;
948 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900949
950 /* First, try getting the desired direct node. */
951 nid = get_nid(parent, start, false);
952 if (!nid)
953 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900954repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900955 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900956 if (!page)
957 return ERR_PTR(-ENOMEM);
958
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900959 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900960 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900961 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900962 else if (err == LOCKED_PAGE)
963 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900964
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900965 blk_start_plug(&plug);
966
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900967 /* Then, try readahead for siblings of the desired node */
968 end = start + MAX_RA_NODE;
969 end = min(end, NIDS_PER_BLOCK);
970 for (i = start + 1; i < end; i++) {
971 nid = get_nid(parent, i, false);
972 if (!nid)
973 continue;
974 ra_node_page(sbi, nid);
975 }
976
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900977 blk_finish_plug(&plug);
978
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979 lock_page(page);
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 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +0900984page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900985 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900986 f2fs_put_page(page, 1);
987 return ERR_PTR(-EIO);
988 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900989 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900990 return page;
991}
992
993void sync_inode_page(struct dnode_of_data *dn)
994{
995 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
996 update_inode(dn->inode, dn->node_page);
997 } else if (dn->inode_page) {
998 if (!dn->inode_page_locked)
999 lock_page(dn->inode_page);
1000 update_inode(dn->inode, dn->inode_page);
1001 if (!dn->inode_page_locked)
1002 unlock_page(dn->inode_page);
1003 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001004 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001005 }
1006}
1007
1008int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1009 struct writeback_control *wbc)
1010{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001011 pgoff_t index, end;
1012 struct pagevec pvec;
1013 int step = ino ? 2 : 0;
1014 int nwritten = 0, wrote = 0;
1015
1016 pagevec_init(&pvec, 0);
1017
1018next_step:
1019 index = 0;
1020 end = LONG_MAX;
1021
1022 while (index <= end) {
1023 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001024 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025 PAGECACHE_TAG_DIRTY,
1026 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1027 if (nr_pages == 0)
1028 break;
1029
1030 for (i = 0; i < nr_pages; i++) {
1031 struct page *page = pvec.pages[i];
1032
1033 /*
1034 * flushing sequence with step:
1035 * 0. indirect nodes
1036 * 1. dentry dnodes
1037 * 2. file dnodes
1038 */
1039 if (step == 0 && IS_DNODE(page))
1040 continue;
1041 if (step == 1 && (!IS_DNODE(page) ||
1042 is_cold_node(page)))
1043 continue;
1044 if (step == 2 && (!IS_DNODE(page) ||
1045 !is_cold_node(page)))
1046 continue;
1047
1048 /*
1049 * If an fsync mode,
1050 * we should not skip writing node pages.
1051 */
1052 if (ino && ino_of_node(page) == ino)
1053 lock_page(page);
1054 else if (!trylock_page(page))
1055 continue;
1056
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001057 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001058continue_unlock:
1059 unlock_page(page);
1060 continue;
1061 }
1062 if (ino && ino_of_node(page) != ino)
1063 goto continue_unlock;
1064
1065 if (!PageDirty(page)) {
1066 /* someone wrote it for us */
1067 goto continue_unlock;
1068 }
1069
1070 if (!clear_page_dirty_for_io(page))
1071 goto continue_unlock;
1072
1073 /* called by fsync() */
1074 if (ino && IS_DNODE(page)) {
1075 int mark = !is_checkpointed_node(sbi, ino);
1076 set_fsync_mark(page, 1);
1077 if (IS_INODE(page))
1078 set_dentry_mark(page, mark);
1079 nwritten++;
1080 } else {
1081 set_fsync_mark(page, 0);
1082 set_dentry_mark(page, 0);
1083 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001084 NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001085 wrote++;
1086
1087 if (--wbc->nr_to_write == 0)
1088 break;
1089 }
1090 pagevec_release(&pvec);
1091 cond_resched();
1092
1093 if (wbc->nr_to_write == 0) {
1094 step = 2;
1095 break;
1096 }
1097 }
1098
1099 if (step < 2) {
1100 step++;
1101 goto next_step;
1102 }
1103
1104 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001105 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001106 return nwritten;
1107}
1108
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001109int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1110{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001111 pgoff_t index = 0, end = LONG_MAX;
1112 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001113 int ret2 = 0, ret = 0;
1114
1115 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001116
1117 while (index <= end) {
1118 int i, nr_pages;
1119 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1120 PAGECACHE_TAG_WRITEBACK,
1121 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1122 if (nr_pages == 0)
1123 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001124
1125 for (i = 0; i < nr_pages; i++) {
1126 struct page *page = pvec.pages[i];
1127
1128 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001129 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001130 continue;
1131
Chao Yu4bf08ff2013-11-04 10:28:33 +08001132 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001133 wait_on_page_writeback(page);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001134 if (TestClearPageError(page))
1135 ret = -EIO;
1136 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001137 }
1138 pagevec_release(&pvec);
1139 cond_resched();
1140 }
1141
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001142 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001143 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001144 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001145 ret2 = -EIO;
1146 if (!ret)
1147 ret = ret2;
1148 return ret;
1149}
1150
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001151static int f2fs_write_node_page(struct page *page,
1152 struct writeback_control *wbc)
1153{
1154 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1155 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001156 block_t new_addr;
1157 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001158 struct f2fs_io_info fio = {
1159 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001160 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001161 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001162
Chao Yucfb271d2013-12-05 17:15:22 +08001163 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001164 goto redirty_out;
1165
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001166 wait_on_page_writeback(page);
1167
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001168 /* get old block addr of this node page */
1169 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001170 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001171
1172 get_node_info(sbi, nid, &ni);
1173
1174 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001175 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001176 dec_page_count(sbi, F2FS_DIRTY_NODES);
1177 unlock_page(page);
1178 return 0;
1179 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001180
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001181 if (wbc->for_reclaim)
1182 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001183
Jaegeuk Kim39936832012-11-22 16:21:29 +09001184 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001185 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001186 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001187 set_node_addr(sbi, &ni, new_addr);
1188 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001189 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001190 unlock_page(page);
1191 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001192
1193redirty_out:
1194 dec_page_count(sbi, F2FS_DIRTY_NODES);
1195 wbc->pages_skipped++;
Chao Yu9cf3c382014-02-28 10:12:05 +08001196 account_page_redirty(page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001197 set_page_dirty(page);
1198 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001199}
1200
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001201/*
1202 * It is very important to gather dirty pages and write at once, so that we can
1203 * submit a big bio without interfering other data writes.
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001204 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001205 */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001206#define COLLECT_DIRTY_NODES 1536
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001207static int f2fs_write_node_pages(struct address_space *mapping,
1208 struct writeback_control *wbc)
1209{
1210 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001211 long nr_to_write = wbc->nr_to_write;
1212
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001213 /* balancing f2fs's metadata in background */
1214 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001215
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001216 /* collect a number of dirty node pages and write together */
1217 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1218 return 0;
1219
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001220 /* if mounting is failed, skip writing node pages */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001221 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001222 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001223 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001224 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1225 wbc->nr_to_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001226 return 0;
1227}
1228
1229static int f2fs_set_node_page_dirty(struct page *page)
1230{
1231 struct address_space *mapping = page->mapping;
1232 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1233
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001234 trace_f2fs_set_page_dirty(page, NODE);
1235
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236 SetPageUptodate(page);
1237 if (!PageDirty(page)) {
1238 __set_page_dirty_nobuffers(page);
1239 inc_page_count(sbi, F2FS_DIRTY_NODES);
1240 SetPagePrivate(page);
1241 return 1;
1242 }
1243 return 0;
1244}
1245
Lukas Czernerd47992f2013-05-21 23:17:23 -04001246static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1247 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001248{
1249 struct inode *inode = page->mapping->host;
1250 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1251 if (PageDirty(page))
1252 dec_page_count(sbi, F2FS_DIRTY_NODES);
1253 ClearPagePrivate(page);
1254}
1255
1256static int f2fs_release_node_page(struct page *page, gfp_t wait)
1257{
1258 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001259 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001260}
1261
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001262/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001263 * Structure of the f2fs node operations
1264 */
1265const struct address_space_operations f2fs_node_aops = {
1266 .writepage = f2fs_write_node_page,
1267 .writepages = f2fs_write_node_pages,
1268 .set_page_dirty = f2fs_set_node_page_dirty,
1269 .invalidatepage = f2fs_invalidate_node_page,
1270 .releasepage = f2fs_release_node_page,
1271};
1272
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001273static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1274 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001275{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001276 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001277}
1278
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001279static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1280 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001281{
1282 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001283 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001284 kmem_cache_free(free_nid_slab, i);
1285}
1286
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001287static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001288{
1289 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001290 struct nat_entry *ne;
1291 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001292
1293 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
Haicheng Li23d38842013-05-06 23:15:43 +08001294 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001295
1296 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001297 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001298 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001299
Gu Zheng7bd59382013-10-22 14:52:26 +08001300 if (build) {
1301 /* do not add allocated nids */
1302 read_lock(&nm_i->nat_tree_lock);
1303 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001304 if (ne &&
1305 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001306 allocated = true;
1307 read_unlock(&nm_i->nat_tree_lock);
1308 if (allocated)
1309 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001311
1312 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001313 i->nid = nid;
1314 i->state = NID_NEW;
1315
1316 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001317 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001318 spin_unlock(&nm_i->free_nid_list_lock);
1319 kmem_cache_free(free_nid_slab, i);
1320 return 0;
1321 }
1322 list_add_tail(&i->list, &nm_i->free_nid_list);
1323 nm_i->fcnt++;
1324 spin_unlock(&nm_i->free_nid_list_lock);
1325 return 1;
1326}
1327
1328static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1329{
1330 struct free_nid *i;
1331 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001332 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001334 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001335 nm_i->fcnt--;
1336 }
1337 spin_unlock(&nm_i->free_nid_list_lock);
1338}
1339
Haicheng Li87609522013-05-06 23:15:42 +08001340static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001341 struct page *nat_page, nid_t start_nid)
1342{
1343 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1344 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001345 int i;
1346
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001347 i = start_nid % NAT_ENTRY_PER_BLOCK;
1348
1349 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001350
Chao Yucfb271d2013-12-05 17:15:22 +08001351 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001352 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001353
1354 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001355 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001356 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001357 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001358 break;
1359 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001360 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001361}
1362
1363static void build_free_nids(struct f2fs_sb_info *sbi)
1364{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001365 struct f2fs_nm_info *nm_i = NM_I(sbi);
1366 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1367 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001368 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001369 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001370
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001371 /* Enough entries */
1372 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1373 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001374
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001375 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001376 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001377
1378 while (1) {
1379 struct page *page = get_current_nat_page(sbi, nid);
1380
Haicheng Li87609522013-05-06 23:15:42 +08001381 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001382 f2fs_put_page(page, 1);
1383
1384 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001385 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001387
1388 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001389 break;
1390 }
1391
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001392 /* go to the next free nat pages to find free nids abundantly */
1393 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001394
1395 /* find free nids from current sum_pages */
1396 mutex_lock(&curseg->curseg_mutex);
1397 for (i = 0; i < nats_in_cursum(sum); i++) {
1398 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1399 nid = le32_to_cpu(nid_in_journal(sum, i));
1400 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001401 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001402 else
1403 remove_free_nid(nm_i, nid);
1404 }
1405 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001406}
1407
1408/*
1409 * If this function returns success, caller can obtain a new nid
1410 * from second parameter of this function.
1411 * The returned nid could be used ino as well as nid when inode is created.
1412 */
1413bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1414{
1415 struct f2fs_nm_info *nm_i = NM_I(sbi);
1416 struct free_nid *i = NULL;
1417 struct list_head *this;
1418retry:
Chao Yucfb271d2013-12-05 17:15:22 +08001419 if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001420 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001421
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001423
1424 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001425 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001426 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001427 list_for_each(this, &nm_i->free_nid_list) {
1428 i = list_entry(this, struct free_nid, list);
1429 if (i->state == NID_NEW)
1430 break;
1431 }
1432
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001433 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001434 *nid = i->nid;
1435 i->state = NID_ALLOC;
1436 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001437 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001438 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001439 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001440 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001441
1442 /* Let's scan nat pages and its caches to get free nids */
1443 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001444 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001445 mutex_unlock(&nm_i->build_lock);
1446 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001447}
1448
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001449/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001450 * alloc_nid() should be called prior to this function.
1451 */
1452void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1453{
1454 struct f2fs_nm_info *nm_i = NM_I(sbi);
1455 struct free_nid *i;
1456
1457 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001458 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001459 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001460 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001461 spin_unlock(&nm_i->free_nid_list_lock);
1462}
1463
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001464/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001465 * alloc_nid() should be called prior to this function.
1466 */
1467void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1468{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001469 struct f2fs_nm_info *nm_i = NM_I(sbi);
1470 struct free_nid *i;
1471
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001472 if (!nid)
1473 return;
1474
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001475 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001476 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001477 f2fs_bug_on(!i || i->state != NID_ALLOC);
Haicheng Li95630cb2013-05-06 23:15:41 +08001478 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001479 __del_from_free_nid_list(nm_i, i);
Haicheng Li95630cb2013-05-06 23:15:41 +08001480 } else {
1481 i->state = NID_NEW;
1482 nm_i->fcnt++;
1483 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001484 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001485}
1486
1487void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1488 struct f2fs_summary *sum, struct node_info *ni,
1489 block_t new_blkaddr)
1490{
1491 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1492 set_node_addr(sbi, ni, new_blkaddr);
1493 clear_node_page_dirty(page);
1494}
1495
Chao Yu28cdce02014-03-11 13:37:38 +08001496void recover_inline_xattr(struct inode *inode, struct page *page)
1497{
1498 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1499 void *src_addr, *dst_addr;
1500 size_t inline_size;
1501 struct page *ipage;
1502 struct f2fs_inode *ri;
1503
1504 if (!is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR))
1505 return;
1506
1507 if (!IS_INODE(page))
1508 return;
1509
1510 ri = F2FS_INODE(page);
1511 if (!(ri->i_inline & F2FS_INLINE_XATTR))
1512 return;
1513
1514 ipage = get_node_page(sbi, inode->i_ino);
1515 f2fs_bug_on(IS_ERR(ipage));
1516
1517 dst_addr = inline_xattr_addr(ipage);
1518 src_addr = inline_xattr_addr(page);
1519 inline_size = inline_xattr_size(inode);
1520
1521 memcpy(dst_addr, src_addr, inline_size);
1522
1523 update_inode(inode, ipage);
1524 f2fs_put_page(ipage, 1);
1525}
1526
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001527bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1528{
1529 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1530 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1531 nid_t new_xnid = nid_of_node(page);
1532 struct node_info ni;
1533
Chao Yu28cdce02014-03-11 13:37:38 +08001534 recover_inline_xattr(inode, page);
1535
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001536 if (ofs_of_node(page) != XATTR_NODE_OFFSET)
1537 return false;
1538
1539 /* 1: invalidate the previous xattr nid */
1540 if (!prev_xnid)
1541 goto recover_xnid;
1542
1543 /* Deallocate node address */
1544 get_node_info(sbi, prev_xnid, &ni);
1545 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1546 invalidate_blocks(sbi, ni.blk_addr);
1547 dec_valid_node_count(sbi, inode);
1548 set_node_addr(sbi, &ni, NULL_ADDR);
1549
1550recover_xnid:
1551 /* 2: allocate new xattr nid */
1552 if (unlikely(!inc_valid_node_count(sbi, inode)))
1553 f2fs_bug_on(1);
1554
1555 remove_free_nid(NM_I(sbi), new_xnid);
1556 get_node_info(sbi, new_xnid, &ni);
1557 ni.ino = inode->i_ino;
1558 set_node_addr(sbi, &ni, NEW_ADDR);
1559 F2FS_I(inode)->i_xattr_nid = new_xnid;
1560
1561 /* 3: update xattr blkaddr */
1562 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1563 set_node_addr(sbi, &ni, blkaddr);
1564
1565 update_inode_page(inode);
1566 return true;
1567}
1568
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001569int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1570{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001571 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001572 nid_t ino = ino_of_node(page);
1573 struct node_info old_ni, new_ni;
1574 struct page *ipage;
1575
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001576 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577 if (!ipage)
1578 return -ENOMEM;
1579
1580 /* Should not use this inode from free nid list */
1581 remove_free_nid(NM_I(sbi), ino);
1582
1583 get_node_info(sbi, ino, &old_ni);
1584 SetPageUptodate(ipage);
1585 fill_node_footer(ipage, ino, ino, 0, true);
1586
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001587 src = F2FS_INODE(page);
1588 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001589
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001590 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1591 dst->i_size = 0;
1592 dst->i_blocks = cpu_to_le64(1);
1593 dst->i_links = cpu_to_le32(1);
1594 dst->i_xattr_nid = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001595
1596 new_ni = old_ni;
1597 new_ni.ino = ino;
1598
Chao Yucfb271d2013-12-05 17:15:22 +08001599 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001600 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001601 set_node_addr(sbi, &new_ni, NEW_ADDR);
1602 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001603 f2fs_put_page(ipage, 1);
1604 return 0;
1605}
1606
Chao Yu9af0ff12013-11-22 15:48:54 +08001607/*
1608 * ra_sum_pages() merge contiguous pages into one bio and submit.
1609 * these pre-readed pages are linked in pages list.
1610 */
1611static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1612 int start, int nrpages)
1613{
1614 struct page *page;
1615 int page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001616 struct f2fs_io_info fio = {
1617 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001618 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001619 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001620
1621 for (; page_idx < start + nrpages; page_idx++) {
1622 /* alloc temporal page for read node summary info*/
Chao Yua0acdfe2013-12-05 09:54:00 +08001623 page = alloc_page(GFP_F2FS_ZERO);
Gu Zhengd6537882014-03-07 18:43:36 +08001624 if (!page)
1625 break;
Chao Yu9af0ff12013-11-22 15:48:54 +08001626
1627 lock_page(page);
1628 page->index = page_idx;
1629 list_add_tail(&page->lru, pages);
1630 }
1631
1632 list_for_each_entry(page, pages, lru)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001633 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001634
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001635 f2fs_submit_merged_bio(sbi, META, READ);
Gu Zhengd6537882014-03-07 18:43:36 +08001636
1637 return page_idx - start;
Chao Yu9af0ff12013-11-22 15:48:54 +08001638}
1639
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001640int restore_node_summary(struct f2fs_sb_info *sbi,
1641 unsigned int segno, struct f2fs_summary_block *sum)
1642{
1643 struct f2fs_node *rn;
1644 struct f2fs_summary *sum_entry;
Chao Yu9af0ff12013-11-22 15:48:54 +08001645 struct page *page, *tmp;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001646 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001647 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1648 int i, last_offset, nrpages, err = 0;
1649 LIST_HEAD(page_list);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001650
1651 /* scan the node segment */
1652 last_offset = sbi->blocks_per_seg;
1653 addr = START_BLOCK(sbi, segno);
1654 sum_entry = &sum->entries[0];
1655
Gu Zhengd6537882014-03-07 18:43:36 +08001656 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001657 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001658
Chao Yu9af0ff12013-11-22 15:48:54 +08001659 /* read ahead node pages */
Gu Zhengd6537882014-03-07 18:43:36 +08001660 nrpages = ra_sum_pages(sbi, &page_list, addr, nrpages);
1661 if (!nrpages)
1662 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001663
Chao Yu9af0ff12013-11-22 15:48:54 +08001664 list_for_each_entry_safe(page, tmp, &page_list, lru) {
Gu Zhengd6537882014-03-07 18:43:36 +08001665 if (err)
1666 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001667
1668 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001669 if (unlikely(!PageUptodate(page))) {
1670 err = -EIO;
1671 } else {
Chao Yu9af0ff12013-11-22 15:48:54 +08001672 rn = F2FS_NODE(page);
1673 sum_entry->nid = rn->footer.nid;
1674 sum_entry->version = 0;
1675 sum_entry->ofs_in_node = 0;
1676 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001677 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001678 unlock_page(page);
Gu Zhengd6537882014-03-07 18:43:36 +08001679skip:
1680 list_del(&page->lru);
Chao Yu9af0ff12013-11-22 15:48:54 +08001681 __free_pages(page, 0);
1682 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001683 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001684 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001685}
1686
1687static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1688{
1689 struct f2fs_nm_info *nm_i = NM_I(sbi);
1690 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1691 struct f2fs_summary_block *sum = curseg->sum_blk;
1692 int i;
1693
1694 mutex_lock(&curseg->curseg_mutex);
1695
1696 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1697 mutex_unlock(&curseg->curseg_mutex);
1698 return false;
1699 }
1700
1701 for (i = 0; i < nats_in_cursum(sum); i++) {
1702 struct nat_entry *ne;
1703 struct f2fs_nat_entry raw_ne;
1704 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1705
1706 raw_ne = nat_in_journal(sum, i);
1707retry:
1708 write_lock(&nm_i->nat_tree_lock);
1709 ne = __lookup_nat_cache(nm_i, nid);
1710 if (ne) {
1711 __set_nat_cache_dirty(nm_i, ne);
1712 write_unlock(&nm_i->nat_tree_lock);
1713 continue;
1714 }
1715 ne = grab_nat_entry(nm_i, nid);
1716 if (!ne) {
1717 write_unlock(&nm_i->nat_tree_lock);
1718 goto retry;
1719 }
1720 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1721 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1722 nat_set_version(ne, raw_ne.version);
1723 __set_nat_cache_dirty(nm_i, ne);
1724 write_unlock(&nm_i->nat_tree_lock);
1725 }
1726 update_nats_in_cursum(sum, -i);
1727 mutex_unlock(&curseg->curseg_mutex);
1728 return true;
1729}
1730
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001731/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001732 * This function is called during the checkpointing process.
1733 */
1734void flush_nat_entries(struct f2fs_sb_info *sbi)
1735{
1736 struct f2fs_nm_info *nm_i = NM_I(sbi);
1737 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1738 struct f2fs_summary_block *sum = curseg->sum_blk;
1739 struct list_head *cur, *n;
1740 struct page *page = NULL;
1741 struct f2fs_nat_block *nat_blk = NULL;
1742 nid_t start_nid = 0, end_nid = 0;
1743 bool flushed;
1744
1745 flushed = flush_nats_in_journal(sbi);
1746
1747 if (!flushed)
1748 mutex_lock(&curseg->curseg_mutex);
1749
1750 /* 1) flush dirty nat caches */
1751 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1752 struct nat_entry *ne;
1753 nid_t nid;
1754 struct f2fs_nat_entry raw_ne;
1755 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001756 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001757
1758 ne = list_entry(cur, struct nat_entry, list);
1759 nid = nat_get_nid(ne);
1760
1761 if (nat_get_blkaddr(ne) == NEW_ADDR)
1762 continue;
1763 if (flushed)
1764 goto to_nat_page;
1765
1766 /* if there is room for nat enries in curseg->sumpage */
1767 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1768 if (offset >= 0) {
1769 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001770 goto flush_now;
1771 }
1772to_nat_page:
1773 if (!page || (start_nid > nid || nid > end_nid)) {
1774 if (page) {
1775 f2fs_put_page(page, 1);
1776 page = NULL;
1777 }
1778 start_nid = START_NID(nid);
1779 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1780
1781 /*
1782 * get nat block with dirty flag, increased reference
1783 * count, mapped and lock
1784 */
1785 page = get_next_nat_page(sbi, start_nid);
1786 nat_blk = page_address(page);
1787 }
1788
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001789 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001790 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001791flush_now:
1792 new_blkaddr = nat_get_blkaddr(ne);
1793
1794 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1795 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1796 raw_ne.version = nat_get_version(ne);
1797
1798 if (offset < 0) {
1799 nat_blk->entries[nid - start_nid] = raw_ne;
1800 } else {
1801 nat_in_journal(sum, offset) = raw_ne;
1802 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1803 }
1804
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001805 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001806 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001807 write_lock(&nm_i->nat_tree_lock);
1808 __del_from_nat_cache(nm_i, ne);
1809 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001810 } else {
1811 write_lock(&nm_i->nat_tree_lock);
1812 __clear_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001813 write_unlock(&nm_i->nat_tree_lock);
1814 }
1815 }
1816 if (!flushed)
1817 mutex_unlock(&curseg->curseg_mutex);
1818 f2fs_put_page(page, 1);
1819
1820 /* 2) shrink nat caches if necessary */
1821 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1822}
1823
1824static int init_node_manager(struct f2fs_sb_info *sbi)
1825{
1826 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1827 struct f2fs_nm_info *nm_i = NM_I(sbi);
1828 unsigned char *version_bitmap;
1829 unsigned int nat_segs, nat_blocks;
1830
1831 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1832
1833 /* segment_count_nat includes pair segment so divide to 2. */
1834 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1835 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001836
1837 /* not used nids: 0, node, meta, (and root counted as valid node) */
1838 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001839 nm_i->fcnt = 0;
1840 nm_i->nat_cnt = 0;
1841
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001842 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001843 INIT_LIST_HEAD(&nm_i->free_nid_list);
1844 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1845 INIT_LIST_HEAD(&nm_i->nat_entries);
1846 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1847
1848 mutex_init(&nm_i->build_lock);
1849 spin_lock_init(&nm_i->free_nid_list_lock);
1850 rwlock_init(&nm_i->nat_tree_lock);
1851
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001852 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001853 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001854 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1855 if (!version_bitmap)
1856 return -EFAULT;
1857
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001858 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1859 GFP_KERNEL);
1860 if (!nm_i->nat_bitmap)
1861 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001862 return 0;
1863}
1864
1865int build_node_manager(struct f2fs_sb_info *sbi)
1866{
1867 int err;
1868
1869 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1870 if (!sbi->nm_info)
1871 return -ENOMEM;
1872
1873 err = init_node_manager(sbi);
1874 if (err)
1875 return err;
1876
1877 build_free_nids(sbi);
1878 return 0;
1879}
1880
1881void destroy_node_manager(struct f2fs_sb_info *sbi)
1882{
1883 struct f2fs_nm_info *nm_i = NM_I(sbi);
1884 struct free_nid *i, *next_i;
1885 struct nat_entry *natvec[NATVEC_SIZE];
1886 nid_t nid = 0;
1887 unsigned int found;
1888
1889 if (!nm_i)
1890 return;
1891
1892 /* destroy free nid list */
1893 spin_lock(&nm_i->free_nid_list_lock);
1894 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001895 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001896 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001897 nm_i->fcnt--;
1898 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001899 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001900 spin_unlock(&nm_i->free_nid_list_lock);
1901
1902 /* destroy nat cache */
1903 write_lock(&nm_i->nat_tree_lock);
1904 while ((found = __gang_lookup_nat_cache(nm_i,
1905 nid, NATVEC_SIZE, natvec))) {
1906 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08001907 nid = nat_get_nid(natvec[found - 1]) + 1;
1908 for (idx = 0; idx < found; idx++)
1909 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001910 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001911 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912 write_unlock(&nm_i->nat_tree_lock);
1913
1914 kfree(nm_i->nat_bitmap);
1915 sbi->nm_info = NULL;
1916 kfree(nm_i);
1917}
1918
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001919int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001920{
1921 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001922 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001923 if (!nat_entry_slab)
1924 return -ENOMEM;
1925
1926 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08001927 sizeof(struct free_nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001928 if (!free_nid_slab) {
1929 kmem_cache_destroy(nat_entry_slab);
1930 return -ENOMEM;
1931 }
1932 return 0;
1933}
1934
1935void destroy_node_manager_caches(void)
1936{
1937 kmem_cache_destroy(free_nid_slab);
1938 kmem_cache_destroy(nat_entry_slab);
1939}