blob: 9a6d8bbf0bd73281ed10bf99b4812ae1492e482f [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 Kim3cb5ad12014-03-18 13:29:07 +0900728 f2fs_wait_on_page_writeback(page, NODE);
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 Kim3cb5ad12014-03-18 13:29:07 +0900817 page = grab_cache_page_write_begin(NODE_MAPPING(sbi),
818 dn->nid, AOP_FLAG_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900819 if (!page)
820 return ERR_PTR(-ENOMEM);
821
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900822 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900823 err = -ENOSPC;
824 goto fail;
825 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900826
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900827 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900828
829 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900830 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831 new_ni = old_ni;
832 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900833 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900834
835 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900836 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900837 SetPageUptodate(page);
838 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900839
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800840 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900841 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
842
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900843 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900844 if (ipage)
845 update_inode(dn->inode, ipage);
846 else
847 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848 if (ofs == 0)
849 inc_valid_inode_count(sbi);
850
851 return page;
852
853fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900854 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855 f2fs_put_page(page, 1);
856 return ERR_PTR(err);
857}
858
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900859/*
860 * Caller should do after getting the following values.
861 * 0: f2fs_put_page(page, 0)
862 * LOCKED_PAGE: f2fs_put_page(page, 1)
863 * error: nothing
864 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900865static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900866{
867 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
868 struct node_info ni;
869
870 get_node_info(sbi, page->index, &ni);
871
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900872 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900873 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900874 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900875 }
876
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900877 if (PageUptodate(page))
878 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900879
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900880 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900881}
882
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900883/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900884 * Readahead a node page
885 */
886void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
887{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900888 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900889 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900890
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900891 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900892 if (apage && PageUptodate(apage)) {
893 f2fs_put_page(apage, 0);
894 return;
895 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900896 f2fs_put_page(apage, 0);
897
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900898 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900899 if (!apage)
900 return;
901
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900902 err = read_node_page(apage, READA);
903 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900904 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900905 else if (err == LOCKED_PAGE)
906 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900907}
908
909struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
910{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900911 struct page *page;
912 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900913repeat:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900914 page = grab_cache_page_write_begin(NODE_MAPPING(sbi),
915 nid, AOP_FLAG_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900916 if (!page)
917 return ERR_PTR(-ENOMEM);
918
919 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900920 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900921 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900922 else if (err == LOCKED_PAGE)
923 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900924
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900925 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900926 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900927 f2fs_put_page(page, 1);
928 return ERR_PTR(-EIO);
929 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900930 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900931 f2fs_put_page(page, 1);
932 goto repeat;
933 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900934got_it:
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900935 f2fs_bug_on(nid != nid_of_node(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900936 mark_page_accessed(page);
937 return page;
938}
939
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900940/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900941 * Return a locked page for the desired node page.
942 * And, readahead MAX_RA_NODE number of node pages.
943 */
944struct page *get_node_page_ra(struct page *parent, int start)
945{
946 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900947 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900948 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900949 int err, i, end;
950 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900951
952 /* First, try getting the desired direct node. */
953 nid = get_nid(parent, start, false);
954 if (!nid)
955 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900956repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900957 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958 if (!page)
959 return ERR_PTR(-ENOMEM);
960
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900961 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900962 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900963 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900964 else if (err == LOCKED_PAGE)
965 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900966
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900967 blk_start_plug(&plug);
968
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900969 /* Then, try readahead for siblings of the desired node */
970 end = start + MAX_RA_NODE;
971 end = min(end, NIDS_PER_BLOCK);
972 for (i = start + 1; i < end; i++) {
973 nid = get_nid(parent, i, false);
974 if (!nid)
975 continue;
976 ra_node_page(sbi, nid);
977 }
978
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900979 blk_finish_plug(&plug);
980
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900981 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900982 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900983 f2fs_put_page(page, 1);
984 goto repeat;
985 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +0900986page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900987 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988 f2fs_put_page(page, 1);
989 return ERR_PTR(-EIO);
990 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900991 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900992 return page;
993}
994
995void sync_inode_page(struct dnode_of_data *dn)
996{
997 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
998 update_inode(dn->inode, dn->node_page);
999 } else if (dn->inode_page) {
1000 if (!dn->inode_page_locked)
1001 lock_page(dn->inode_page);
1002 update_inode(dn->inode, dn->inode_page);
1003 if (!dn->inode_page_locked)
1004 unlock_page(dn->inode_page);
1005 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001006 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001007 }
1008}
1009
1010int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1011 struct writeback_control *wbc)
1012{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001013 pgoff_t index, end;
1014 struct pagevec pvec;
1015 int step = ino ? 2 : 0;
1016 int nwritten = 0, wrote = 0;
1017
1018 pagevec_init(&pvec, 0);
1019
1020next_step:
1021 index = 0;
1022 end = LONG_MAX;
1023
1024 while (index <= end) {
1025 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001026 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001027 PAGECACHE_TAG_DIRTY,
1028 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1029 if (nr_pages == 0)
1030 break;
1031
1032 for (i = 0; i < nr_pages; i++) {
1033 struct page *page = pvec.pages[i];
1034
1035 /*
1036 * flushing sequence with step:
1037 * 0. indirect nodes
1038 * 1. dentry dnodes
1039 * 2. file dnodes
1040 */
1041 if (step == 0 && IS_DNODE(page))
1042 continue;
1043 if (step == 1 && (!IS_DNODE(page) ||
1044 is_cold_node(page)))
1045 continue;
1046 if (step == 2 && (!IS_DNODE(page) ||
1047 !is_cold_node(page)))
1048 continue;
1049
1050 /*
1051 * If an fsync mode,
1052 * we should not skip writing node pages.
1053 */
1054 if (ino && ino_of_node(page) == ino)
1055 lock_page(page);
1056 else if (!trylock_page(page))
1057 continue;
1058
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001059 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001060continue_unlock:
1061 unlock_page(page);
1062 continue;
1063 }
1064 if (ino && ino_of_node(page) != ino)
1065 goto continue_unlock;
1066
1067 if (!PageDirty(page)) {
1068 /* someone wrote it for us */
1069 goto continue_unlock;
1070 }
1071
1072 if (!clear_page_dirty_for_io(page))
1073 goto continue_unlock;
1074
1075 /* called by fsync() */
1076 if (ino && IS_DNODE(page)) {
1077 int mark = !is_checkpointed_node(sbi, ino);
1078 set_fsync_mark(page, 1);
1079 if (IS_INODE(page))
1080 set_dentry_mark(page, mark);
1081 nwritten++;
1082 } else {
1083 set_fsync_mark(page, 0);
1084 set_dentry_mark(page, 0);
1085 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001086 NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001087 wrote++;
1088
1089 if (--wbc->nr_to_write == 0)
1090 break;
1091 }
1092 pagevec_release(&pvec);
1093 cond_resched();
1094
1095 if (wbc->nr_to_write == 0) {
1096 step = 2;
1097 break;
1098 }
1099 }
1100
1101 if (step < 2) {
1102 step++;
1103 goto next_step;
1104 }
1105
1106 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001107 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001108 return nwritten;
1109}
1110
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001111int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1112{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001113 pgoff_t index = 0, end = LONG_MAX;
1114 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001115 int ret2 = 0, ret = 0;
1116
1117 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001118
1119 while (index <= end) {
1120 int i, nr_pages;
1121 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1122 PAGECACHE_TAG_WRITEBACK,
1123 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1124 if (nr_pages == 0)
1125 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001126
1127 for (i = 0; i < nr_pages; i++) {
1128 struct page *page = pvec.pages[i];
1129
1130 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001131 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001132 continue;
1133
Chao Yu4bf08ff2013-11-04 10:28:33 +08001134 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001135 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001136 if (TestClearPageError(page))
1137 ret = -EIO;
1138 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001139 }
1140 pagevec_release(&pvec);
1141 cond_resched();
1142 }
1143
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001144 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001145 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001146 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001147 ret2 = -EIO;
1148 if (!ret)
1149 ret = ret2;
1150 return ret;
1151}
1152
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001153static int f2fs_write_node_page(struct page *page,
1154 struct writeback_control *wbc)
1155{
1156 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1157 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001158 block_t new_addr;
1159 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001160 struct f2fs_io_info fio = {
1161 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001162 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001163 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001164
Chao Yucfb271d2013-12-05 17:15:22 +08001165 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001166 goto redirty_out;
1167
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001168 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001169
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001170 /* get old block addr of this node page */
1171 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001172 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001173
1174 get_node_info(sbi, nid, &ni);
1175
1176 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001177 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001178 dec_page_count(sbi, F2FS_DIRTY_NODES);
1179 unlock_page(page);
1180 return 0;
1181 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001182
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001183 if (wbc->for_reclaim)
1184 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001185
Jaegeuk Kim39936832012-11-22 16:21:29 +09001186 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001187 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001188 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001189 set_node_addr(sbi, &ni, new_addr);
1190 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001191 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001192 unlock_page(page);
1193 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001194
1195redirty_out:
1196 dec_page_count(sbi, F2FS_DIRTY_NODES);
1197 wbc->pages_skipped++;
Chao Yu9cf3c382014-02-28 10:12:05 +08001198 account_page_redirty(page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001199 set_page_dirty(page);
1200 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001201}
1202
1203static int f2fs_write_node_pages(struct address_space *mapping,
1204 struct writeback_control *wbc)
1205{
1206 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001207 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001209 /* balancing f2fs's metadata in background */
1210 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001211
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001212 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001213 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001214 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001215
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001216 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001217 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001218 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001219 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001220 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001221
1222skip_write:
1223 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1224 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001225}
1226
1227static int f2fs_set_node_page_dirty(struct page *page)
1228{
1229 struct address_space *mapping = page->mapping;
1230 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1231
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001232 trace_f2fs_set_page_dirty(page, NODE);
1233
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001234 SetPageUptodate(page);
1235 if (!PageDirty(page)) {
1236 __set_page_dirty_nobuffers(page);
1237 inc_page_count(sbi, F2FS_DIRTY_NODES);
1238 SetPagePrivate(page);
1239 return 1;
1240 }
1241 return 0;
1242}
1243
Lukas Czernerd47992f2013-05-21 23:17:23 -04001244static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1245 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001246{
1247 struct inode *inode = page->mapping->host;
1248 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1249 if (PageDirty(page))
1250 dec_page_count(sbi, F2FS_DIRTY_NODES);
1251 ClearPagePrivate(page);
1252}
1253
1254static int f2fs_release_node_page(struct page *page, gfp_t wait)
1255{
1256 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001257 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001258}
1259
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001260/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001261 * Structure of the f2fs node operations
1262 */
1263const struct address_space_operations f2fs_node_aops = {
1264 .writepage = f2fs_write_node_page,
1265 .writepages = f2fs_write_node_pages,
1266 .set_page_dirty = f2fs_set_node_page_dirty,
1267 .invalidatepage = f2fs_invalidate_node_page,
1268 .releasepage = f2fs_release_node_page,
1269};
1270
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001271static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1272 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001273{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001274 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001275}
1276
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001277static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1278 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001279{
1280 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001281 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001282 kmem_cache_free(free_nid_slab, i);
1283}
1284
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001285static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001286{
1287 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001288 struct nat_entry *ne;
1289 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001290
1291 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
Haicheng Li23d38842013-05-06 23:15:43 +08001292 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001293
1294 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001295 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001296 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001297
Gu Zheng7bd59382013-10-22 14:52:26 +08001298 if (build) {
1299 /* do not add allocated nids */
1300 read_lock(&nm_i->nat_tree_lock);
1301 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001302 if (ne &&
1303 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001304 allocated = true;
1305 read_unlock(&nm_i->nat_tree_lock);
1306 if (allocated)
1307 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001308 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001309
1310 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001311 i->nid = nid;
1312 i->state = NID_NEW;
1313
1314 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001315 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001316 spin_unlock(&nm_i->free_nid_list_lock);
1317 kmem_cache_free(free_nid_slab, i);
1318 return 0;
1319 }
1320 list_add_tail(&i->list, &nm_i->free_nid_list);
1321 nm_i->fcnt++;
1322 spin_unlock(&nm_i->free_nid_list_lock);
1323 return 1;
1324}
1325
1326static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1327{
1328 struct free_nid *i;
1329 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001330 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001331 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001332 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333 nm_i->fcnt--;
1334 }
1335 spin_unlock(&nm_i->free_nid_list_lock);
1336}
1337
Haicheng Li87609522013-05-06 23:15:42 +08001338static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001339 struct page *nat_page, nid_t start_nid)
1340{
1341 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1342 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001343 int i;
1344
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001345 i = start_nid % NAT_ENTRY_PER_BLOCK;
1346
1347 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001348
Chao Yucfb271d2013-12-05 17:15:22 +08001349 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001350 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001351
1352 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001353 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001354 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001355 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001356 break;
1357 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001358 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001359}
1360
1361static void build_free_nids(struct f2fs_sb_info *sbi)
1362{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001363 struct f2fs_nm_info *nm_i = NM_I(sbi);
1364 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1365 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001366 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001367 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001368
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001369 /* Enough entries */
1370 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1371 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001372
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001373 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001374 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375
1376 while (1) {
1377 struct page *page = get_current_nat_page(sbi, nid);
1378
Haicheng Li87609522013-05-06 23:15:42 +08001379 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001380 f2fs_put_page(page, 1);
1381
1382 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001383 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001384 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001385
1386 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001387 break;
1388 }
1389
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001390 /* go to the next free nat pages to find free nids abundantly */
1391 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392
1393 /* find free nids from current sum_pages */
1394 mutex_lock(&curseg->curseg_mutex);
1395 for (i = 0; i < nats_in_cursum(sum); i++) {
1396 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1397 nid = le32_to_cpu(nid_in_journal(sum, i));
1398 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001399 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001400 else
1401 remove_free_nid(nm_i, nid);
1402 }
1403 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001404}
1405
1406/*
1407 * If this function returns success, caller can obtain a new nid
1408 * from second parameter of this function.
1409 * The returned nid could be used ino as well as nid when inode is created.
1410 */
1411bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1412{
1413 struct f2fs_nm_info *nm_i = NM_I(sbi);
1414 struct free_nid *i = NULL;
1415 struct list_head *this;
1416retry:
Chao Yucfb271d2013-12-05 17:15:22 +08001417 if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001418 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001419
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001421
1422 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001423 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001424 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001425 list_for_each(this, &nm_i->free_nid_list) {
1426 i = list_entry(this, struct free_nid, list);
1427 if (i->state == NID_NEW)
1428 break;
1429 }
1430
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001431 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001432 *nid = i->nid;
1433 i->state = NID_ALLOC;
1434 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001435 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001436 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001437 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001438 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001439
1440 /* Let's scan nat pages and its caches to get free nids */
1441 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001442 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001443 mutex_unlock(&nm_i->build_lock);
1444 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001445}
1446
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001447/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001448 * alloc_nid() should be called prior to this function.
1449 */
1450void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1451{
1452 struct f2fs_nm_info *nm_i = NM_I(sbi);
1453 struct free_nid *i;
1454
1455 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001456 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001457 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001458 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459 spin_unlock(&nm_i->free_nid_list_lock);
1460}
1461
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001462/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001463 * alloc_nid() should be called prior to this function.
1464 */
1465void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1466{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001467 struct f2fs_nm_info *nm_i = NM_I(sbi);
1468 struct free_nid *i;
1469
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001470 if (!nid)
1471 return;
1472
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001473 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001474 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001475 f2fs_bug_on(!i || i->state != NID_ALLOC);
Haicheng Li95630cb2013-05-06 23:15:41 +08001476 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001477 __del_from_free_nid_list(nm_i, i);
Haicheng Li95630cb2013-05-06 23:15:41 +08001478 } else {
1479 i->state = NID_NEW;
1480 nm_i->fcnt++;
1481 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001482 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001483}
1484
1485void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1486 struct f2fs_summary *sum, struct node_info *ni,
1487 block_t new_blkaddr)
1488{
1489 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1490 set_node_addr(sbi, ni, new_blkaddr);
1491 clear_node_page_dirty(page);
1492}
1493
Chao Yu28cdce02014-03-11 13:37:38 +08001494void recover_inline_xattr(struct inode *inode, struct page *page)
1495{
1496 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1497 void *src_addr, *dst_addr;
1498 size_t inline_size;
1499 struct page *ipage;
1500 struct f2fs_inode *ri;
1501
Chao Yu987c7c32014-03-12 15:59:03 +08001502 if (!f2fs_has_inline_xattr(inode))
Chao Yu28cdce02014-03-11 13:37:38 +08001503 return;
1504
1505 if (!IS_INODE(page))
1506 return;
1507
1508 ri = F2FS_INODE(page);
1509 if (!(ri->i_inline & F2FS_INLINE_XATTR))
1510 return;
1511
1512 ipage = get_node_page(sbi, inode->i_ino);
1513 f2fs_bug_on(IS_ERR(ipage));
1514
1515 dst_addr = inline_xattr_addr(ipage);
1516 src_addr = inline_xattr_addr(page);
1517 inline_size = inline_xattr_size(inode);
1518
1519 memcpy(dst_addr, src_addr, inline_size);
1520
1521 update_inode(inode, ipage);
1522 f2fs_put_page(ipage, 1);
1523}
1524
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001525bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1526{
1527 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1528 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1529 nid_t new_xnid = nid_of_node(page);
1530 struct node_info ni;
1531
Chao Yu28cdce02014-03-11 13:37:38 +08001532 recover_inline_xattr(inode, page);
1533
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001534 if (!f2fs_has_xattr_block(ofs_of_node(page)))
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001535 return false;
1536
1537 /* 1: invalidate the previous xattr nid */
1538 if (!prev_xnid)
1539 goto recover_xnid;
1540
1541 /* Deallocate node address */
1542 get_node_info(sbi, prev_xnid, &ni);
1543 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1544 invalidate_blocks(sbi, ni.blk_addr);
1545 dec_valid_node_count(sbi, inode);
1546 set_node_addr(sbi, &ni, NULL_ADDR);
1547
1548recover_xnid:
1549 /* 2: allocate new xattr nid */
1550 if (unlikely(!inc_valid_node_count(sbi, inode)))
1551 f2fs_bug_on(1);
1552
1553 remove_free_nid(NM_I(sbi), new_xnid);
1554 get_node_info(sbi, new_xnid, &ni);
1555 ni.ino = inode->i_ino;
1556 set_node_addr(sbi, &ni, NEW_ADDR);
1557 F2FS_I(inode)->i_xattr_nid = new_xnid;
1558
1559 /* 3: update xattr blkaddr */
1560 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1561 set_node_addr(sbi, &ni, blkaddr);
1562
1563 update_inode_page(inode);
1564 return true;
1565}
1566
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001567int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1568{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001569 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001570 nid_t ino = ino_of_node(page);
1571 struct node_info old_ni, new_ni;
1572 struct page *ipage;
1573
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001574 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001575 if (!ipage)
1576 return -ENOMEM;
1577
1578 /* Should not use this inode from free nid list */
1579 remove_free_nid(NM_I(sbi), ino);
1580
1581 get_node_info(sbi, ino, &old_ni);
1582 SetPageUptodate(ipage);
1583 fill_node_footer(ipage, ino, ino, 0, true);
1584
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001585 src = F2FS_INODE(page);
1586 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001587
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001588 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1589 dst->i_size = 0;
1590 dst->i_blocks = cpu_to_le64(1);
1591 dst->i_links = cpu_to_le32(1);
1592 dst->i_xattr_nid = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001593
1594 new_ni = old_ni;
1595 new_ni.ino = ino;
1596
Chao Yucfb271d2013-12-05 17:15:22 +08001597 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001598 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001599 set_node_addr(sbi, &new_ni, NEW_ADDR);
1600 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001601 f2fs_put_page(ipage, 1);
1602 return 0;
1603}
1604
Chao Yu9af0ff12013-11-22 15:48:54 +08001605/*
1606 * ra_sum_pages() merge contiguous pages into one bio and submit.
1607 * these pre-readed pages are linked in pages list.
1608 */
1609static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1610 int start, int nrpages)
1611{
1612 struct page *page;
1613 int page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001614 struct f2fs_io_info fio = {
1615 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001616 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001617 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001618
1619 for (; page_idx < start + nrpages; page_idx++) {
1620 /* alloc temporal page for read node summary info*/
Chao Yua0acdfe2013-12-05 09:54:00 +08001621 page = alloc_page(GFP_F2FS_ZERO);
Gu Zhengd6537882014-03-07 18:43:36 +08001622 if (!page)
1623 break;
Chao Yu9af0ff12013-11-22 15:48:54 +08001624
1625 lock_page(page);
1626 page->index = page_idx;
1627 list_add_tail(&page->lru, pages);
1628 }
1629
1630 list_for_each_entry(page, pages, lru)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001631 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001632
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001633 f2fs_submit_merged_bio(sbi, META, READ);
Gu Zhengd6537882014-03-07 18:43:36 +08001634
1635 return page_idx - start;
Chao Yu9af0ff12013-11-22 15:48:54 +08001636}
1637
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001638int restore_node_summary(struct f2fs_sb_info *sbi,
1639 unsigned int segno, struct f2fs_summary_block *sum)
1640{
1641 struct f2fs_node *rn;
1642 struct f2fs_summary *sum_entry;
Chao Yu9af0ff12013-11-22 15:48:54 +08001643 struct page *page, *tmp;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001644 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001645 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1646 int i, last_offset, nrpages, err = 0;
1647 LIST_HEAD(page_list);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001648
1649 /* scan the node segment */
1650 last_offset = sbi->blocks_per_seg;
1651 addr = START_BLOCK(sbi, segno);
1652 sum_entry = &sum->entries[0];
1653
Gu Zhengd6537882014-03-07 18:43:36 +08001654 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001655 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001656
Chao Yu9af0ff12013-11-22 15:48:54 +08001657 /* read ahead node pages */
Gu Zhengd6537882014-03-07 18:43:36 +08001658 nrpages = ra_sum_pages(sbi, &page_list, addr, nrpages);
1659 if (!nrpages)
1660 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001661
Chao Yu9af0ff12013-11-22 15:48:54 +08001662 list_for_each_entry_safe(page, tmp, &page_list, lru) {
Gu Zhengd6537882014-03-07 18:43:36 +08001663 if (err)
1664 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001665
1666 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001667 if (unlikely(!PageUptodate(page))) {
1668 err = -EIO;
1669 } else {
Chao Yu9af0ff12013-11-22 15:48:54 +08001670 rn = F2FS_NODE(page);
1671 sum_entry->nid = rn->footer.nid;
1672 sum_entry->version = 0;
1673 sum_entry->ofs_in_node = 0;
1674 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001675 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001676 unlock_page(page);
Gu Zhengd6537882014-03-07 18:43:36 +08001677skip:
1678 list_del(&page->lru);
Chao Yu9af0ff12013-11-22 15:48:54 +08001679 __free_pages(page, 0);
1680 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001681 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001682 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001683}
1684
1685static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1686{
1687 struct f2fs_nm_info *nm_i = NM_I(sbi);
1688 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1689 struct f2fs_summary_block *sum = curseg->sum_blk;
1690 int i;
1691
1692 mutex_lock(&curseg->curseg_mutex);
1693
1694 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1695 mutex_unlock(&curseg->curseg_mutex);
1696 return false;
1697 }
1698
1699 for (i = 0; i < nats_in_cursum(sum); i++) {
1700 struct nat_entry *ne;
1701 struct f2fs_nat_entry raw_ne;
1702 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1703
1704 raw_ne = nat_in_journal(sum, i);
1705retry:
1706 write_lock(&nm_i->nat_tree_lock);
1707 ne = __lookup_nat_cache(nm_i, nid);
1708 if (ne) {
1709 __set_nat_cache_dirty(nm_i, ne);
1710 write_unlock(&nm_i->nat_tree_lock);
1711 continue;
1712 }
1713 ne = grab_nat_entry(nm_i, nid);
1714 if (!ne) {
1715 write_unlock(&nm_i->nat_tree_lock);
1716 goto retry;
1717 }
1718 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1719 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1720 nat_set_version(ne, raw_ne.version);
1721 __set_nat_cache_dirty(nm_i, ne);
1722 write_unlock(&nm_i->nat_tree_lock);
1723 }
1724 update_nats_in_cursum(sum, -i);
1725 mutex_unlock(&curseg->curseg_mutex);
1726 return true;
1727}
1728
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001729/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001730 * This function is called during the checkpointing process.
1731 */
1732void flush_nat_entries(struct f2fs_sb_info *sbi)
1733{
1734 struct f2fs_nm_info *nm_i = NM_I(sbi);
1735 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1736 struct f2fs_summary_block *sum = curseg->sum_blk;
1737 struct list_head *cur, *n;
1738 struct page *page = NULL;
1739 struct f2fs_nat_block *nat_blk = NULL;
1740 nid_t start_nid = 0, end_nid = 0;
1741 bool flushed;
1742
1743 flushed = flush_nats_in_journal(sbi);
1744
1745 if (!flushed)
1746 mutex_lock(&curseg->curseg_mutex);
1747
1748 /* 1) flush dirty nat caches */
1749 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1750 struct nat_entry *ne;
1751 nid_t nid;
1752 struct f2fs_nat_entry raw_ne;
1753 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001754 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001755
1756 ne = list_entry(cur, struct nat_entry, list);
1757 nid = nat_get_nid(ne);
1758
1759 if (nat_get_blkaddr(ne) == NEW_ADDR)
1760 continue;
1761 if (flushed)
1762 goto to_nat_page;
1763
1764 /* if there is room for nat enries in curseg->sumpage */
1765 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1766 if (offset >= 0) {
1767 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001768 goto flush_now;
1769 }
1770to_nat_page:
1771 if (!page || (start_nid > nid || nid > end_nid)) {
1772 if (page) {
1773 f2fs_put_page(page, 1);
1774 page = NULL;
1775 }
1776 start_nid = START_NID(nid);
1777 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1778
1779 /*
1780 * get nat block with dirty flag, increased reference
1781 * count, mapped and lock
1782 */
1783 page = get_next_nat_page(sbi, start_nid);
1784 nat_blk = page_address(page);
1785 }
1786
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001787 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001788 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001789flush_now:
1790 new_blkaddr = nat_get_blkaddr(ne);
1791
1792 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1793 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1794 raw_ne.version = nat_get_version(ne);
1795
1796 if (offset < 0) {
1797 nat_blk->entries[nid - start_nid] = raw_ne;
1798 } else {
1799 nat_in_journal(sum, offset) = raw_ne;
1800 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1801 }
1802
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001803 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001804 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001805 write_lock(&nm_i->nat_tree_lock);
1806 __del_from_nat_cache(nm_i, ne);
1807 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001808 } else {
1809 write_lock(&nm_i->nat_tree_lock);
1810 __clear_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001811 write_unlock(&nm_i->nat_tree_lock);
1812 }
1813 }
1814 if (!flushed)
1815 mutex_unlock(&curseg->curseg_mutex);
1816 f2fs_put_page(page, 1);
1817
1818 /* 2) shrink nat caches if necessary */
1819 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1820}
1821
1822static int init_node_manager(struct f2fs_sb_info *sbi)
1823{
1824 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1825 struct f2fs_nm_info *nm_i = NM_I(sbi);
1826 unsigned char *version_bitmap;
1827 unsigned int nat_segs, nat_blocks;
1828
1829 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1830
1831 /* segment_count_nat includes pair segment so divide to 2. */
1832 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1833 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001834
1835 /* not used nids: 0, node, meta, (and root counted as valid node) */
1836 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001837 nm_i->fcnt = 0;
1838 nm_i->nat_cnt = 0;
1839
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001840 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001841 INIT_LIST_HEAD(&nm_i->free_nid_list);
1842 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1843 INIT_LIST_HEAD(&nm_i->nat_entries);
1844 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1845
1846 mutex_init(&nm_i->build_lock);
1847 spin_lock_init(&nm_i->free_nid_list_lock);
1848 rwlock_init(&nm_i->nat_tree_lock);
1849
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001850 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001851 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001852 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1853 if (!version_bitmap)
1854 return -EFAULT;
1855
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001856 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1857 GFP_KERNEL);
1858 if (!nm_i->nat_bitmap)
1859 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001860 return 0;
1861}
1862
1863int build_node_manager(struct f2fs_sb_info *sbi)
1864{
1865 int err;
1866
1867 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1868 if (!sbi->nm_info)
1869 return -ENOMEM;
1870
1871 err = init_node_manager(sbi);
1872 if (err)
1873 return err;
1874
1875 build_free_nids(sbi);
1876 return 0;
1877}
1878
1879void destroy_node_manager(struct f2fs_sb_info *sbi)
1880{
1881 struct f2fs_nm_info *nm_i = NM_I(sbi);
1882 struct free_nid *i, *next_i;
1883 struct nat_entry *natvec[NATVEC_SIZE];
1884 nid_t nid = 0;
1885 unsigned int found;
1886
1887 if (!nm_i)
1888 return;
1889
1890 /* destroy free nid list */
1891 spin_lock(&nm_i->free_nid_list_lock);
1892 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001893 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001894 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001895 nm_i->fcnt--;
1896 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001897 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001898 spin_unlock(&nm_i->free_nid_list_lock);
1899
1900 /* destroy nat cache */
1901 write_lock(&nm_i->nat_tree_lock);
1902 while ((found = __gang_lookup_nat_cache(nm_i,
1903 nid, NATVEC_SIZE, natvec))) {
1904 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08001905 nid = nat_get_nid(natvec[found - 1]) + 1;
1906 for (idx = 0; idx < found; idx++)
1907 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001908 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001909 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001910 write_unlock(&nm_i->nat_tree_lock);
1911
1912 kfree(nm_i->nat_bitmap);
1913 sbi->nm_info = NULL;
1914 kfree(nm_i);
1915}
1916
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001917int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001918{
1919 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001920 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001921 if (!nat_entry_slab)
1922 return -ENOMEM;
1923
1924 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08001925 sizeof(struct free_nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001926 if (!free_nid_slab) {
1927 kmem_cache_destroy(nat_entry_slab);
1928 return -ENOMEM;
1929 }
1930 return 0;
1931}
1932
1933void destroy_node_manager_caches(void)
1934{
1935 kmem_cache_destroy(free_nid_slab);
1936 kmem_cache_destroy(nat_entry_slab);
1937}