blob: d452185c5eaadb32aff5454d2aba99f397fe4361 [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
24static struct kmem_cache *nat_entry_slab;
25static struct kmem_cache *free_nid_slab;
26
27static void clear_node_page_dirty(struct page *page)
28{
29 struct address_space *mapping = page->mapping;
30 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
31 unsigned int long flags;
32
33 if (PageDirty(page)) {
34 spin_lock_irqsave(&mapping->tree_lock, flags);
35 radix_tree_tag_clear(&mapping->page_tree,
36 page_index(page),
37 PAGECACHE_TAG_DIRTY);
38 spin_unlock_irqrestore(&mapping->tree_lock, flags);
39
40 clear_page_dirty_for_io(page);
41 dec_page_count(sbi, F2FS_DIRTY_NODES);
42 }
43 ClearPageUptodate(page);
44}
45
46static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
47{
48 pgoff_t index = current_nat_addr(sbi, nid);
49 return get_meta_page(sbi, index);
50}
51
52static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
53{
54 struct page *src_page;
55 struct page *dst_page;
56 pgoff_t src_off;
57 pgoff_t dst_off;
58 void *src_addr;
59 void *dst_addr;
60 struct f2fs_nm_info *nm_i = NM_I(sbi);
61
62 src_off = current_nat_addr(sbi, nid);
63 dst_off = next_nat_addr(sbi, src_off);
64
65 /* get current nat block page with lock */
66 src_page = get_meta_page(sbi, src_off);
67
68 /* Dirty src_page means that it is already the new target NAT page. */
69 if (PageDirty(src_page))
70 return src_page;
71
72 dst_page = grab_meta_page(sbi, dst_off);
73
74 src_addr = page_address(src_page);
75 dst_addr = page_address(dst_page);
76 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
77 set_page_dirty(dst_page);
78 f2fs_put_page(src_page, 1);
79
80 set_to_next_nat(nm_i, nid);
81
82 return dst_page;
83}
84
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090085static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
86{
87 return radix_tree_lookup(&nm_i->nat_root, n);
88}
89
90static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
91 nid_t start, unsigned int nr, struct nat_entry **ep)
92{
93 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
94}
95
96static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
97{
98 list_del(&e->list);
99 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
100 nm_i->nat_cnt--;
101 kmem_cache_free(nat_entry_slab, e);
102}
103
104int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
105{
106 struct f2fs_nm_info *nm_i = NM_I(sbi);
107 struct nat_entry *e;
108 int is_cp = 1;
109
110 read_lock(&nm_i->nat_tree_lock);
111 e = __lookup_nat_cache(nm_i, nid);
112 if (e && !e->checkpointed)
113 is_cp = 0;
114 read_unlock(&nm_i->nat_tree_lock);
115 return is_cp;
116}
117
118static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
119{
120 struct nat_entry *new;
121
122 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
123 if (!new)
124 return NULL;
125 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
126 kmem_cache_free(nat_entry_slab, new);
127 return NULL;
128 }
129 memset(new, 0, sizeof(struct nat_entry));
130 nat_set_nid(new, nid);
131 list_add_tail(&new->list, &nm_i->nat_entries);
132 nm_i->nat_cnt++;
133 return new;
134}
135
136static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
137 struct f2fs_nat_entry *ne)
138{
139 struct nat_entry *e;
140retry:
141 write_lock(&nm_i->nat_tree_lock);
142 e = __lookup_nat_cache(nm_i, nid);
143 if (!e) {
144 e = grab_nat_entry(nm_i, nid);
145 if (!e) {
146 write_unlock(&nm_i->nat_tree_lock);
147 goto retry;
148 }
149 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
150 nat_set_ino(e, le32_to_cpu(ne->ino));
151 nat_set_version(e, ne->version);
152 e->checkpointed = true;
153 }
154 write_unlock(&nm_i->nat_tree_lock);
155}
156
157static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
158 block_t new_blkaddr)
159{
160 struct f2fs_nm_info *nm_i = NM_I(sbi);
161 struct nat_entry *e;
162retry:
163 write_lock(&nm_i->nat_tree_lock);
164 e = __lookup_nat_cache(nm_i, ni->nid);
165 if (!e) {
166 e = grab_nat_entry(nm_i, ni->nid);
167 if (!e) {
168 write_unlock(&nm_i->nat_tree_lock);
169 goto retry;
170 }
171 e->ni = *ni;
172 e->checkpointed = true;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900173 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900174 } else if (new_blkaddr == NEW_ADDR) {
175 /*
176 * when nid is reallocated,
177 * previous nat entry can be remained in nat cache.
178 * So, reinitialize it with new information.
179 */
180 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900181 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900182 }
183
184 if (new_blkaddr == NEW_ADDR)
185 e->checkpointed = false;
186
187 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900188 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
189 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900190 new_blkaddr == NULL_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 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900193 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900194 nat_get_blkaddr(e) != NULL_ADDR &&
195 new_blkaddr == NEW_ADDR);
196
197 /* increament version no as node is removed */
198 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
199 unsigned char version = nat_get_version(e);
200 nat_set_version(e, inc_node_version(version));
201 }
202
203 /* change address */
204 nat_set_blkaddr(e, new_blkaddr);
205 __set_nat_cache_dirty(nm_i, e);
206 write_unlock(&nm_i->nat_tree_lock);
207}
208
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900209int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900210{
211 struct f2fs_nm_info *nm_i = NM_I(sbi);
212
Haicheng Li6cac3752013-04-28 19:16:06 +0800213 if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900214 return 0;
215
216 write_lock(&nm_i->nat_tree_lock);
217 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
218 struct nat_entry *ne;
219 ne = list_first_entry(&nm_i->nat_entries,
220 struct nat_entry, list);
221 __del_from_nat_cache(nm_i, ne);
222 nr_shrink--;
223 }
224 write_unlock(&nm_i->nat_tree_lock);
225 return nr_shrink;
226}
227
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900228/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900229 * This function returns always success
230 */
231void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
232{
233 struct f2fs_nm_info *nm_i = NM_I(sbi);
234 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
235 struct f2fs_summary_block *sum = curseg->sum_blk;
236 nid_t start_nid = START_NID(nid);
237 struct f2fs_nat_block *nat_blk;
238 struct page *page = NULL;
239 struct f2fs_nat_entry ne;
240 struct nat_entry *e;
241 int i;
242
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900243 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900244 ni->nid = nid;
245
246 /* Check nat cache */
247 read_lock(&nm_i->nat_tree_lock);
248 e = __lookup_nat_cache(nm_i, nid);
249 if (e) {
250 ni->ino = nat_get_ino(e);
251 ni->blk_addr = nat_get_blkaddr(e);
252 ni->version = nat_get_version(e);
253 }
254 read_unlock(&nm_i->nat_tree_lock);
255 if (e)
256 return;
257
258 /* Check current segment summary */
259 mutex_lock(&curseg->curseg_mutex);
260 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
261 if (i >= 0) {
262 ne = nat_in_journal(sum, i);
263 node_info_from_raw_nat(ni, &ne);
264 }
265 mutex_unlock(&curseg->curseg_mutex);
266 if (i >= 0)
267 goto cache;
268
269 /* Fill node_info from nat page */
270 page = get_current_nat_page(sbi, start_nid);
271 nat_blk = (struct f2fs_nat_block *)page_address(page);
272 ne = nat_blk->entries[nid - start_nid];
273 node_info_from_raw_nat(ni, &ne);
274 f2fs_put_page(page, 1);
275cache:
276 /* cache nat entry */
277 cache_nat_entry(NM_I(sbi), nid, &ne);
278}
279
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900280/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900281 * The maximum depth is four.
282 * Offset[0] will have raw inode offset.
283 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900284static int get_node_path(struct f2fs_inode_info *fi, long block,
285 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900286{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900287 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900288 const long direct_blks = ADDRS_PER_BLOCK;
289 const long dptrs_per_blk = NIDS_PER_BLOCK;
290 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
291 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
292 int n = 0;
293 int level = 0;
294
295 noffset[0] = 0;
296
297 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900298 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900299 goto got;
300 }
301 block -= direct_index;
302 if (block < direct_blks) {
303 offset[n++] = NODE_DIR1_BLOCK;
304 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900305 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900306 level = 1;
307 goto got;
308 }
309 block -= direct_blks;
310 if (block < direct_blks) {
311 offset[n++] = NODE_DIR2_BLOCK;
312 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900313 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900314 level = 1;
315 goto got;
316 }
317 block -= direct_blks;
318 if (block < indirect_blks) {
319 offset[n++] = NODE_IND1_BLOCK;
320 noffset[n] = 3;
321 offset[n++] = block / direct_blks;
322 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900323 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900324 level = 2;
325 goto got;
326 }
327 block -= indirect_blks;
328 if (block < indirect_blks) {
329 offset[n++] = NODE_IND2_BLOCK;
330 noffset[n] = 4 + dptrs_per_blk;
331 offset[n++] = block / direct_blks;
332 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900333 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900334 level = 2;
335 goto got;
336 }
337 block -= indirect_blks;
338 if (block < dindirect_blks) {
339 offset[n++] = NODE_DIND_BLOCK;
340 noffset[n] = 5 + (dptrs_per_blk * 2);
341 offset[n++] = block / indirect_blks;
342 noffset[n] = 6 + (dptrs_per_blk * 2) +
343 offset[n - 1] * (dptrs_per_blk + 1);
344 offset[n++] = (block / direct_blks) % dptrs_per_blk;
345 noffset[n] = 7 + (dptrs_per_blk * 2) +
346 offset[n - 2] * (dptrs_per_blk + 1) +
347 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900348 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900349 level = 3;
350 goto got;
351 } else {
352 BUG();
353 }
354got:
355 return level;
356}
357
358/*
359 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800360 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
361 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900362 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900363 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900364int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900365{
366 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
367 struct page *npage[4];
368 struct page *parent;
369 int offset[4];
370 unsigned int noffset[4];
371 nid_t nids[4];
372 int level, i;
373 int err = 0;
374
Jaegeuk Kimde936532013-08-12 21:08:03 +0900375 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900376
377 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900378 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900379
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900380 if (!npage[0]) {
381 npage[0] = get_node_page(sbi, nids[0]);
382 if (IS_ERR(npage[0]))
383 return PTR_ERR(npage[0]);
384 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900385 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900386 if (level != 0)
387 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900388 dn->inode_page = npage[0];
389 dn->inode_page_locked = true;
390
391 /* get indirect or direct nodes */
392 for (i = 1; i <= level; i++) {
393 bool done = false;
394
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900395 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900396 /* alloc new node */
397 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900398 err = -ENOSPC;
399 goto release_pages;
400 }
401
402 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900403 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900404 if (IS_ERR(npage[i])) {
405 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900406 err = PTR_ERR(npage[i]);
407 goto release_pages;
408 }
409
410 set_nid(parent, offset[i - 1], nids[i], i == 1);
411 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900413 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900414 npage[i] = get_node_page_ra(parent, offset[i - 1]);
415 if (IS_ERR(npage[i])) {
416 err = PTR_ERR(npage[i]);
417 goto release_pages;
418 }
419 done = true;
420 }
421 if (i == 1) {
422 dn->inode_page_locked = false;
423 unlock_page(parent);
424 } else {
425 f2fs_put_page(parent, 1);
426 }
427
428 if (!done) {
429 npage[i] = get_node_page(sbi, nids[i]);
430 if (IS_ERR(npage[i])) {
431 err = PTR_ERR(npage[i]);
432 f2fs_put_page(npage[0], 0);
433 goto release_out;
434 }
435 }
436 if (i < level) {
437 parent = npage[i];
438 nids[i + 1] = get_nid(parent, offset[i], false);
439 }
440 }
441 dn->nid = nids[level];
442 dn->ofs_in_node = offset[level];
443 dn->node_page = npage[level];
444 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
445 return 0;
446
447release_pages:
448 f2fs_put_page(parent, 1);
449 if (i > 1)
450 f2fs_put_page(npage[0], 0);
451release_out:
452 dn->inode_page = NULL;
453 dn->node_page = NULL;
454 return err;
455}
456
457static void truncate_node(struct dnode_of_data *dn)
458{
459 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
460 struct node_info ni;
461
462 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900463 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900464 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900465 goto invalidate;
466 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900467 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900468
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900469 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900470 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800471 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900472 set_node_addr(sbi, &ni, NULL_ADDR);
473
474 if (dn->nid == dn->inode->i_ino) {
475 remove_orphan_inode(sbi, dn->nid);
476 dec_valid_inode_count(sbi);
477 } else {
478 sync_inode_page(dn);
479 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900480invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900481 clear_node_page_dirty(dn->node_page);
482 F2FS_SET_SB_DIRT(sbi);
483
484 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900485
486 invalidate_mapping_pages(NODE_MAPPING(sbi),
487 dn->node_page->index, dn->node_page->index);
488
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900489 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900490 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900491}
492
493static int truncate_dnode(struct dnode_of_data *dn)
494{
495 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
496 struct page *page;
497
498 if (dn->nid == 0)
499 return 1;
500
501 /* get direct node */
502 page = get_node_page(sbi, dn->nid);
503 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
504 return 1;
505 else if (IS_ERR(page))
506 return PTR_ERR(page);
507
508 /* Make dnode_of_data for parameter */
509 dn->node_page = page;
510 dn->ofs_in_node = 0;
511 truncate_data_blocks(dn);
512 truncate_node(dn);
513 return 1;
514}
515
516static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
517 int ofs, int depth)
518{
519 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
520 struct dnode_of_data rdn = *dn;
521 struct page *page;
522 struct f2fs_node *rn;
523 nid_t child_nid;
524 unsigned int child_nofs;
525 int freed = 0;
526 int i, ret;
527
528 if (dn->nid == 0)
529 return NIDS_PER_BLOCK + 1;
530
Namjae Jeon51dd6242013-04-20 01:28:52 +0900531 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
532
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900533 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900534 if (IS_ERR(page)) {
535 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900536 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900537 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538
Gu Zheng45590712013-07-15 17:57:38 +0800539 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900540 if (depth < 3) {
541 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
542 child_nid = le32_to_cpu(rn->in.nid[i]);
543 if (child_nid == 0)
544 continue;
545 rdn.nid = child_nid;
546 ret = truncate_dnode(&rdn);
547 if (ret < 0)
548 goto out_err;
549 set_nid(page, i, 0, false);
550 }
551 } else {
552 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
553 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
554 child_nid = le32_to_cpu(rn->in.nid[i]);
555 if (child_nid == 0) {
556 child_nofs += NIDS_PER_BLOCK + 1;
557 continue;
558 }
559 rdn.nid = child_nid;
560 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
561 if (ret == (NIDS_PER_BLOCK + 1)) {
562 set_nid(page, i, 0, false);
563 child_nofs += ret;
564 } else if (ret < 0 && ret != -ENOENT) {
565 goto out_err;
566 }
567 }
568 freed = child_nofs;
569 }
570
571 if (!ofs) {
572 /* remove current indirect node */
573 dn->node_page = page;
574 truncate_node(dn);
575 freed++;
576 } else {
577 f2fs_put_page(page, 1);
578 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900579 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900580 return freed;
581
582out_err:
583 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900584 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900585 return ret;
586}
587
588static int truncate_partial_nodes(struct dnode_of_data *dn,
589 struct f2fs_inode *ri, int *offset, int depth)
590{
591 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
592 struct page *pages[2];
593 nid_t nid[3];
594 nid_t child_nid;
595 int err = 0;
596 int i;
597 int idx = depth - 2;
598
599 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
600 if (!nid[0])
601 return 0;
602
603 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800604 for (i = 0; i < idx + 1; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900605 /* refernece count'll be increased */
606 pages[i] = get_node_page(sbi, nid[i]);
607 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900608 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800609 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900610 goto fail;
611 }
612 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
613 }
614
615 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800616 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900617 child_nid = get_nid(pages[idx], i, false);
618 if (!child_nid)
619 continue;
620 dn->nid = child_nid;
621 err = truncate_dnode(dn);
622 if (err < 0)
623 goto fail;
624 set_nid(pages[idx], i, 0, false);
625 }
626
shifei10.gea225dca2013-10-29 15:32:34 +0800627 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900628 dn->node_page = pages[idx];
629 dn->nid = nid[idx];
630 truncate_node(dn);
631 } else {
632 f2fs_put_page(pages[idx], 1);
633 }
634 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800635 offset[idx + 1] = 0;
636 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900637fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800638 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900639 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900640
641 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
642
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900643 return err;
644}
645
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900646/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900647 * All the block addresses of data and nodes should be nullified.
648 */
649int truncate_inode_blocks(struct inode *inode, pgoff_t from)
650{
651 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
652 int err = 0, cont = 1;
653 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900654 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900655 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900656 struct dnode_of_data dn;
657 struct page *page;
658
Namjae Jeon51dd6242013-04-20 01:28:52 +0900659 trace_f2fs_truncate_inode_blocks_enter(inode, from);
660
Jaegeuk Kimde936532013-08-12 21:08:03 +0900661 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900662restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900663 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900664 if (IS_ERR(page)) {
665 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900666 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900667 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900668
669 set_new_dnode(&dn, inode, page, NULL, 0);
670 unlock_page(page);
671
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900672 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900673 switch (level) {
674 case 0:
675 case 1:
676 nofs = noffset[1];
677 break;
678 case 2:
679 nofs = noffset[1];
680 if (!offset[level - 1])
681 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900682 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900683 if (err < 0 && err != -ENOENT)
684 goto fail;
685 nofs += 1 + NIDS_PER_BLOCK;
686 break;
687 case 3:
688 nofs = 5 + 2 * NIDS_PER_BLOCK;
689 if (!offset[level - 1])
690 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900691 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900692 if (err < 0 && err != -ENOENT)
693 goto fail;
694 break;
695 default:
696 BUG();
697 }
698
699skip_partial:
700 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900701 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900702 switch (offset[0]) {
703 case NODE_DIR1_BLOCK:
704 case NODE_DIR2_BLOCK:
705 err = truncate_dnode(&dn);
706 break;
707
708 case NODE_IND1_BLOCK:
709 case NODE_IND2_BLOCK:
710 err = truncate_nodes(&dn, nofs, offset[1], 2);
711 break;
712
713 case NODE_DIND_BLOCK:
714 err = truncate_nodes(&dn, nofs, offset[1], 3);
715 cont = 0;
716 break;
717
718 default:
719 BUG();
720 }
721 if (err < 0 && err != -ENOENT)
722 goto fail;
723 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900724 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900725 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900726 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900727 f2fs_put_page(page, 1);
728 goto restart;
729 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900730 wait_on_page_writeback(page);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900731 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732 set_page_dirty(page);
733 unlock_page(page);
734 }
735 offset[1] = 0;
736 offset[0]++;
737 nofs += err;
738 }
739fail:
740 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900741 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900742 return err > 0 ? 0 : err;
743}
744
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900745int truncate_xattr_node(struct inode *inode, struct page *page)
746{
747 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
748 nid_t nid = F2FS_I(inode)->i_xattr_nid;
749 struct dnode_of_data dn;
750 struct page *npage;
751
752 if (!nid)
753 return 0;
754
755 npage = get_node_page(sbi, nid);
756 if (IS_ERR(npage))
757 return PTR_ERR(npage);
758
759 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900760
761 /* need to do checkpoint during fsync */
762 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
763
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900764 set_new_dnode(&dn, inode, page, npage, nid);
765
766 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800767 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900768 truncate_node(&dn);
769 return 0;
770}
771
Jaegeuk Kim39936832012-11-22 16:21:29 +0900772/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800773 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
774 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900775 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800776void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900777{
778 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
779 struct page *page;
780 nid_t ino = inode->i_ino;
781 struct dnode_of_data dn;
782
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900783 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900784 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800785 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900786
Gu Zheng58e674d2013-11-19 18:03:18 +0800787 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900788 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800789 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900790 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900791 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900792 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900793 set_new_dnode(&dn, inode, page, page, ino);
794 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900795}
796
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900797struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900798{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900799 struct dnode_of_data dn;
800
801 /* allocate inode page for new inode */
802 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900803
804 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900805 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900806}
807
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900808struct page *new_node_page(struct dnode_of_data *dn,
809 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900810{
811 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900812 struct node_info old_ni, new_ni;
813 struct page *page;
814 int err;
815
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900816 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900817 return ERR_PTR(-EPERM);
818
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900819 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900820 if (!page)
821 return ERR_PTR(-ENOMEM);
822
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900823 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900824 err = -ENOSPC;
825 goto fail;
826 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900828 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900829
830 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900831 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832 new_ni = old_ni;
833 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900834 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900835
836 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900837 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900838 SetPageUptodate(page);
839 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900840
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900841 if (ofs == XATTR_NODE_OFFSET)
842 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
843
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900845 if (ipage)
846 update_inode(dn->inode, ipage);
847 else
848 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900849 if (ofs == 0)
850 inc_valid_inode_count(sbi);
851
852 return page;
853
854fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900855 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900856 f2fs_put_page(page, 1);
857 return ERR_PTR(err);
858}
859
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900860/*
861 * Caller should do after getting the following values.
862 * 0: f2fs_put_page(page, 0)
863 * LOCKED_PAGE: f2fs_put_page(page, 1)
864 * error: nothing
865 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900866static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900867{
868 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
869 struct node_info ni;
870
871 get_node_info(sbi, page->index, &ni);
872
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900873 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900874 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900875 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900876 }
877
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900878 if (PageUptodate(page))
879 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900880
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900881 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900882}
883
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900884/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900885 * Readahead a node page
886 */
887void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
888{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900889 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900890 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900891
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900892 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900893 if (apage && PageUptodate(apage)) {
894 f2fs_put_page(apage, 0);
895 return;
896 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900897 f2fs_put_page(apage, 0);
898
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900899 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900900 if (!apage)
901 return;
902
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900903 err = read_node_page(apage, READA);
904 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900905 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900906 else if (err == LOCKED_PAGE)
907 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900908}
909
910struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
911{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900912 struct page *page;
913 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900914repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900915 page = grab_cache_page(NODE_MAPPING(sbi), nid);
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 Kimcfe58f92013-10-31 14:57:01 +09001135 wait_on_page_writeback(page);
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 Kime05df3b2012-11-02 17:08:50 +09001168 wait_on_page_writeback(page);
1169
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++;
1198 set_page_dirty(page);
1199 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001200}
1201
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001202/*
1203 * It is very important to gather dirty pages and write at once, so that we can
1204 * submit a big bio without interfering other data writes.
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001205 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001206 */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001207#define COLLECT_DIRTY_NODES 1536
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208static int f2fs_write_node_pages(struct address_space *mapping,
1209 struct writeback_control *wbc)
1210{
1211 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001212 long nr_to_write = wbc->nr_to_write;
1213
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001214 /* balancing f2fs's metadata in background */
1215 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001216
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001217 /* collect a number of dirty node pages and write together */
1218 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1219 return 0;
1220
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001221 /* if mounting is failed, skip writing node pages */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001222 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001223 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001224 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001225 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1226 wbc->nr_to_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001227 return 0;
1228}
1229
1230static int f2fs_set_node_page_dirty(struct page *page)
1231{
1232 struct address_space *mapping = page->mapping;
1233 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1234
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001235 trace_f2fs_set_page_dirty(page, NODE);
1236
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001237 SetPageUptodate(page);
1238 if (!PageDirty(page)) {
1239 __set_page_dirty_nobuffers(page);
1240 inc_page_count(sbi, F2FS_DIRTY_NODES);
1241 SetPagePrivate(page);
1242 return 1;
1243 }
1244 return 0;
1245}
1246
Lukas Czernerd47992f2013-05-21 23:17:23 -04001247static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1248 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001249{
1250 struct inode *inode = page->mapping->host;
1251 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1252 if (PageDirty(page))
1253 dec_page_count(sbi, F2FS_DIRTY_NODES);
1254 ClearPagePrivate(page);
1255}
1256
1257static int f2fs_release_node_page(struct page *page, gfp_t wait)
1258{
1259 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001260 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001261}
1262
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001263/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001264 * Structure of the f2fs node operations
1265 */
1266const struct address_space_operations f2fs_node_aops = {
1267 .writepage = f2fs_write_node_page,
1268 .writepages = f2fs_write_node_pages,
1269 .set_page_dirty = f2fs_set_node_page_dirty,
1270 .invalidatepage = f2fs_invalidate_node_page,
1271 .releasepage = f2fs_release_node_page,
1272};
1273
1274static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1275{
1276 struct list_head *this;
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001277 struct free_nid *i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001278 list_for_each(this, head) {
1279 i = list_entry(this, struct free_nid, list);
1280 if (i->nid == n)
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001281 return i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001282 }
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001283 return NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001284}
1285
1286static void __del_from_free_nid_list(struct free_nid *i)
1287{
1288 list_del(&i->list);
1289 kmem_cache_free(free_nid_slab, i);
1290}
1291
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001292static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001293{
1294 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001295 struct nat_entry *ne;
1296 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001297
1298 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
Haicheng Li23d38842013-05-06 23:15:43 +08001299 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001300
1301 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001302 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001303 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001304
Gu Zheng7bd59382013-10-22 14:52:26 +08001305 if (build) {
1306 /* do not add allocated nids */
1307 read_lock(&nm_i->nat_tree_lock);
1308 ne = __lookup_nat_cache(nm_i, nid);
1309 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1310 allocated = true;
1311 read_unlock(&nm_i->nat_tree_lock);
1312 if (allocated)
1313 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001314 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001315
1316 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001317 i->nid = nid;
1318 i->state = NID_NEW;
1319
1320 spin_lock(&nm_i->free_nid_list_lock);
1321 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1322 spin_unlock(&nm_i->free_nid_list_lock);
1323 kmem_cache_free(free_nid_slab, i);
1324 return 0;
1325 }
1326 list_add_tail(&i->list, &nm_i->free_nid_list);
1327 nm_i->fcnt++;
1328 spin_unlock(&nm_i->free_nid_list_lock);
1329 return 1;
1330}
1331
1332static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1333{
1334 struct free_nid *i;
1335 spin_lock(&nm_i->free_nid_list_lock);
1336 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1337 if (i && i->state == NID_NEW) {
1338 __del_from_free_nid_list(i);
1339 nm_i->fcnt--;
1340 }
1341 spin_unlock(&nm_i->free_nid_list_lock);
1342}
1343
Haicheng Li87609522013-05-06 23:15:42 +08001344static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001345 struct page *nat_page, nid_t start_nid)
1346{
1347 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1348 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349 int i;
1350
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001351 i = start_nid % NAT_ENTRY_PER_BLOCK;
1352
1353 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001354
Chao Yucfb271d2013-12-05 17:15:22 +08001355 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001356 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001357
1358 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001359 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001360 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001361 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001362 break;
1363 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001364 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001365}
1366
1367static void build_free_nids(struct f2fs_sb_info *sbi)
1368{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001369 struct f2fs_nm_info *nm_i = NM_I(sbi);
1370 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1371 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001372 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001373 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001374
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001375 /* Enough entries */
1376 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1377 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001378
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001379 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001380 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381
1382 while (1) {
1383 struct page *page = get_current_nat_page(sbi, nid);
1384
Haicheng Li87609522013-05-06 23:15:42 +08001385 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386 f2fs_put_page(page, 1);
1387
1388 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001389 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001390 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001391
1392 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001393 break;
1394 }
1395
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001396 /* go to the next free nat pages to find free nids abundantly */
1397 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001398
1399 /* find free nids from current sum_pages */
1400 mutex_lock(&curseg->curseg_mutex);
1401 for (i = 0; i < nats_in_cursum(sum); i++) {
1402 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1403 nid = le32_to_cpu(nid_in_journal(sum, i));
1404 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001405 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001406 else
1407 remove_free_nid(nm_i, nid);
1408 }
1409 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001410}
1411
1412/*
1413 * If this function returns success, caller can obtain a new nid
1414 * from second parameter of this function.
1415 * The returned nid could be used ino as well as nid when inode is created.
1416 */
1417bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1418{
1419 struct f2fs_nm_info *nm_i = NM_I(sbi);
1420 struct free_nid *i = NULL;
1421 struct list_head *this;
1422retry:
Chao Yucfb271d2013-12-05 17:15:22 +08001423 if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001424 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001426 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001427
1428 /* We should not use stale free nids created by build_free_nids */
1429 if (nm_i->fcnt && !sbi->on_build_free_nids) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001430 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001431 list_for_each(this, &nm_i->free_nid_list) {
1432 i = list_entry(this, struct free_nid, list);
1433 if (i->state == NID_NEW)
1434 break;
1435 }
1436
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001437 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001438 *nid = i->nid;
1439 i->state = NID_ALLOC;
1440 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001441 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001442 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001443 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001445
1446 /* Let's scan nat pages and its caches to get free nids */
1447 mutex_lock(&nm_i->build_lock);
Haicheng Liaabe5132013-10-23 12:39:32 +08001448 sbi->on_build_free_nids = true;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001449 build_free_nids(sbi);
Haicheng Liaabe5132013-10-23 12:39:32 +08001450 sbi->on_build_free_nids = false;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001451 mutex_unlock(&nm_i->build_lock);
1452 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001453}
1454
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001455/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001456 * alloc_nid() should be called prior to this function.
1457 */
1458void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1459{
1460 struct f2fs_nm_info *nm_i = NM_I(sbi);
1461 struct free_nid *i;
1462
1463 spin_lock(&nm_i->free_nid_list_lock);
1464 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001465 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001466 __del_from_free_nid_list(i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001467 spin_unlock(&nm_i->free_nid_list_lock);
1468}
1469
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001470/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001471 * alloc_nid() should be called prior to this function.
1472 */
1473void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1474{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001475 struct f2fs_nm_info *nm_i = NM_I(sbi);
1476 struct free_nid *i;
1477
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001478 if (!nid)
1479 return;
1480
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001481 spin_lock(&nm_i->free_nid_list_lock);
1482 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001483 f2fs_bug_on(!i || i->state != NID_ALLOC);
Haicheng Li95630cb2013-05-06 23:15:41 +08001484 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1485 __del_from_free_nid_list(i);
1486 } else {
1487 i->state = NID_NEW;
1488 nm_i->fcnt++;
1489 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001490 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001491}
1492
1493void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1494 struct f2fs_summary *sum, struct node_info *ni,
1495 block_t new_blkaddr)
1496{
1497 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1498 set_node_addr(sbi, ni, new_blkaddr);
1499 clear_node_page_dirty(page);
1500}
1501
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001502bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1503{
1504 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1505 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1506 nid_t new_xnid = nid_of_node(page);
1507 struct node_info ni;
1508
1509 if (ofs_of_node(page) != XATTR_NODE_OFFSET)
1510 return false;
1511
1512 /* 1: invalidate the previous xattr nid */
1513 if (!prev_xnid)
1514 goto recover_xnid;
1515
1516 /* Deallocate node address */
1517 get_node_info(sbi, prev_xnid, &ni);
1518 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1519 invalidate_blocks(sbi, ni.blk_addr);
1520 dec_valid_node_count(sbi, inode);
1521 set_node_addr(sbi, &ni, NULL_ADDR);
1522
1523recover_xnid:
1524 /* 2: allocate new xattr nid */
1525 if (unlikely(!inc_valid_node_count(sbi, inode)))
1526 f2fs_bug_on(1);
1527
1528 remove_free_nid(NM_I(sbi), new_xnid);
1529 get_node_info(sbi, new_xnid, &ni);
1530 ni.ino = inode->i_ino;
1531 set_node_addr(sbi, &ni, NEW_ADDR);
1532 F2FS_I(inode)->i_xattr_nid = new_xnid;
1533
1534 /* 3: update xattr blkaddr */
1535 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1536 set_node_addr(sbi, &ni, blkaddr);
1537
1538 update_inode_page(inode);
1539 return true;
1540}
1541
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001542int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1543{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001544 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001545 nid_t ino = ino_of_node(page);
1546 struct node_info old_ni, new_ni;
1547 struct page *ipage;
1548
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001549 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001550 if (!ipage)
1551 return -ENOMEM;
1552
1553 /* Should not use this inode from free nid list */
1554 remove_free_nid(NM_I(sbi), ino);
1555
1556 get_node_info(sbi, ino, &old_ni);
1557 SetPageUptodate(ipage);
1558 fill_node_footer(ipage, ino, ino, 0, true);
1559
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001560 src = F2FS_INODE(page);
1561 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001562
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001563 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1564 dst->i_size = 0;
1565 dst->i_blocks = cpu_to_le64(1);
1566 dst->i_links = cpu_to_le32(1);
1567 dst->i_xattr_nid = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001568
1569 new_ni = old_ni;
1570 new_ni.ino = ino;
1571
Chao Yucfb271d2013-12-05 17:15:22 +08001572 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001573 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001574 set_node_addr(sbi, &new_ni, NEW_ADDR);
1575 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001576 f2fs_put_page(ipage, 1);
1577 return 0;
1578}
1579
Chao Yu9af0ff12013-11-22 15:48:54 +08001580/*
1581 * ra_sum_pages() merge contiguous pages into one bio and submit.
1582 * these pre-readed pages are linked in pages list.
1583 */
1584static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1585 int start, int nrpages)
1586{
1587 struct page *page;
1588 int page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001589 struct f2fs_io_info fio = {
1590 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001591 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001592 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001593
1594 for (; page_idx < start + nrpages; page_idx++) {
1595 /* alloc temporal page for read node summary info*/
Chao Yua0acdfe2013-12-05 09:54:00 +08001596 page = alloc_page(GFP_F2FS_ZERO);
Chao Yu9af0ff12013-11-22 15:48:54 +08001597 if (!page) {
1598 struct page *tmp;
1599 list_for_each_entry_safe(page, tmp, pages, lru) {
1600 list_del(&page->lru);
1601 unlock_page(page);
1602 __free_pages(page, 0);
1603 }
1604 return -ENOMEM;
1605 }
1606
1607 lock_page(page);
1608 page->index = page_idx;
1609 list_add_tail(&page->lru, pages);
1610 }
1611
1612 list_for_each_entry(page, pages, lru)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001613 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001614
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001615 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yu9af0ff12013-11-22 15:48:54 +08001616 return 0;
1617}
1618
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001619int restore_node_summary(struct f2fs_sb_info *sbi,
1620 unsigned int segno, struct f2fs_summary_block *sum)
1621{
1622 struct f2fs_node *rn;
1623 struct f2fs_summary *sum_entry;
Chao Yu9af0ff12013-11-22 15:48:54 +08001624 struct page *page, *tmp;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001625 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001626 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1627 int i, last_offset, nrpages, err = 0;
1628 LIST_HEAD(page_list);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001629
1630 /* scan the node segment */
1631 last_offset = sbi->blocks_per_seg;
1632 addr = START_BLOCK(sbi, segno);
1633 sum_entry = &sum->entries[0];
1634
Chao Yu9af0ff12013-11-22 15:48:54 +08001635 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
1636 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001637
Chao Yu9af0ff12013-11-22 15:48:54 +08001638 /* read ahead node pages */
1639 err = ra_sum_pages(sbi, &page_list, addr, nrpages);
1640 if (err)
1641 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001642
Chao Yu9af0ff12013-11-22 15:48:54 +08001643 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1644
1645 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001646 if (unlikely(!PageUptodate(page))) {
1647 err = -EIO;
1648 } else {
Chao Yu9af0ff12013-11-22 15:48:54 +08001649 rn = F2FS_NODE(page);
1650 sum_entry->nid = rn->footer.nid;
1651 sum_entry->version = 0;
1652 sum_entry->ofs_in_node = 0;
1653 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001654 }
1655
1656 list_del(&page->lru);
1657 unlock_page(page);
1658 __free_pages(page, 0);
1659 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001660 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001661 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001662}
1663
1664static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1665{
1666 struct f2fs_nm_info *nm_i = NM_I(sbi);
1667 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1668 struct f2fs_summary_block *sum = curseg->sum_blk;
1669 int i;
1670
1671 mutex_lock(&curseg->curseg_mutex);
1672
1673 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1674 mutex_unlock(&curseg->curseg_mutex);
1675 return false;
1676 }
1677
1678 for (i = 0; i < nats_in_cursum(sum); i++) {
1679 struct nat_entry *ne;
1680 struct f2fs_nat_entry raw_ne;
1681 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1682
1683 raw_ne = nat_in_journal(sum, i);
1684retry:
1685 write_lock(&nm_i->nat_tree_lock);
1686 ne = __lookup_nat_cache(nm_i, nid);
1687 if (ne) {
1688 __set_nat_cache_dirty(nm_i, ne);
1689 write_unlock(&nm_i->nat_tree_lock);
1690 continue;
1691 }
1692 ne = grab_nat_entry(nm_i, nid);
1693 if (!ne) {
1694 write_unlock(&nm_i->nat_tree_lock);
1695 goto retry;
1696 }
1697 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1698 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1699 nat_set_version(ne, raw_ne.version);
1700 __set_nat_cache_dirty(nm_i, ne);
1701 write_unlock(&nm_i->nat_tree_lock);
1702 }
1703 update_nats_in_cursum(sum, -i);
1704 mutex_unlock(&curseg->curseg_mutex);
1705 return true;
1706}
1707
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001708/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001709 * This function is called during the checkpointing process.
1710 */
1711void flush_nat_entries(struct f2fs_sb_info *sbi)
1712{
1713 struct f2fs_nm_info *nm_i = NM_I(sbi);
1714 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1715 struct f2fs_summary_block *sum = curseg->sum_blk;
1716 struct list_head *cur, *n;
1717 struct page *page = NULL;
1718 struct f2fs_nat_block *nat_blk = NULL;
1719 nid_t start_nid = 0, end_nid = 0;
1720 bool flushed;
1721
1722 flushed = flush_nats_in_journal(sbi);
1723
1724 if (!flushed)
1725 mutex_lock(&curseg->curseg_mutex);
1726
1727 /* 1) flush dirty nat caches */
1728 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1729 struct nat_entry *ne;
1730 nid_t nid;
1731 struct f2fs_nat_entry raw_ne;
1732 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001733 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001734
1735 ne = list_entry(cur, struct nat_entry, list);
1736 nid = nat_get_nid(ne);
1737
1738 if (nat_get_blkaddr(ne) == NEW_ADDR)
1739 continue;
1740 if (flushed)
1741 goto to_nat_page;
1742
1743 /* if there is room for nat enries in curseg->sumpage */
1744 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1745 if (offset >= 0) {
1746 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001747 goto flush_now;
1748 }
1749to_nat_page:
1750 if (!page || (start_nid > nid || nid > end_nid)) {
1751 if (page) {
1752 f2fs_put_page(page, 1);
1753 page = NULL;
1754 }
1755 start_nid = START_NID(nid);
1756 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1757
1758 /*
1759 * get nat block with dirty flag, increased reference
1760 * count, mapped and lock
1761 */
1762 page = get_next_nat_page(sbi, start_nid);
1763 nat_blk = page_address(page);
1764 }
1765
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001766 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001767 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001768flush_now:
1769 new_blkaddr = nat_get_blkaddr(ne);
1770
1771 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1772 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1773 raw_ne.version = nat_get_version(ne);
1774
1775 if (offset < 0) {
1776 nat_blk->entries[nid - start_nid] = raw_ne;
1777 } else {
1778 nat_in_journal(sum, offset) = raw_ne;
1779 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1780 }
1781
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001782 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001783 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001784 write_lock(&nm_i->nat_tree_lock);
1785 __del_from_nat_cache(nm_i, ne);
1786 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001787 } else {
1788 write_lock(&nm_i->nat_tree_lock);
1789 __clear_nat_cache_dirty(nm_i, ne);
1790 ne->checkpointed = true;
1791 write_unlock(&nm_i->nat_tree_lock);
1792 }
1793 }
1794 if (!flushed)
1795 mutex_unlock(&curseg->curseg_mutex);
1796 f2fs_put_page(page, 1);
1797
1798 /* 2) shrink nat caches if necessary */
1799 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1800}
1801
1802static int init_node_manager(struct f2fs_sb_info *sbi)
1803{
1804 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1805 struct f2fs_nm_info *nm_i = NM_I(sbi);
1806 unsigned char *version_bitmap;
1807 unsigned int nat_segs, nat_blocks;
1808
1809 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1810
1811 /* segment_count_nat includes pair segment so divide to 2. */
1812 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1813 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001814
1815 /* not used nids: 0, node, meta, (and root counted as valid node) */
1816 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001817 nm_i->fcnt = 0;
1818 nm_i->nat_cnt = 0;
1819
1820 INIT_LIST_HEAD(&nm_i->free_nid_list);
1821 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1822 INIT_LIST_HEAD(&nm_i->nat_entries);
1823 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1824
1825 mutex_init(&nm_i->build_lock);
1826 spin_lock_init(&nm_i->free_nid_list_lock);
1827 rwlock_init(&nm_i->nat_tree_lock);
1828
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001829 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001830 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001831 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1832 if (!version_bitmap)
1833 return -EFAULT;
1834
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001835 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1836 GFP_KERNEL);
1837 if (!nm_i->nat_bitmap)
1838 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001839 return 0;
1840}
1841
1842int build_node_manager(struct f2fs_sb_info *sbi)
1843{
1844 int err;
1845
1846 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1847 if (!sbi->nm_info)
1848 return -ENOMEM;
1849
1850 err = init_node_manager(sbi);
1851 if (err)
1852 return err;
1853
1854 build_free_nids(sbi);
1855 return 0;
1856}
1857
1858void destroy_node_manager(struct f2fs_sb_info *sbi)
1859{
1860 struct f2fs_nm_info *nm_i = NM_I(sbi);
1861 struct free_nid *i, *next_i;
1862 struct nat_entry *natvec[NATVEC_SIZE];
1863 nid_t nid = 0;
1864 unsigned int found;
1865
1866 if (!nm_i)
1867 return;
1868
1869 /* destroy free nid list */
1870 spin_lock(&nm_i->free_nid_list_lock);
1871 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001872 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001873 __del_from_free_nid_list(i);
1874 nm_i->fcnt--;
1875 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001876 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001877 spin_unlock(&nm_i->free_nid_list_lock);
1878
1879 /* destroy nat cache */
1880 write_lock(&nm_i->nat_tree_lock);
1881 while ((found = __gang_lookup_nat_cache(nm_i,
1882 nid, NATVEC_SIZE, natvec))) {
1883 unsigned idx;
1884 for (idx = 0; idx < found; idx++) {
1885 struct nat_entry *e = natvec[idx];
1886 nid = nat_get_nid(e) + 1;
1887 __del_from_nat_cache(nm_i, e);
1888 }
1889 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001890 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001891 write_unlock(&nm_i->nat_tree_lock);
1892
1893 kfree(nm_i->nat_bitmap);
1894 sbi->nm_info = NULL;
1895 kfree(nm_i);
1896}
1897
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001898int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001899{
1900 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1901 sizeof(struct nat_entry), NULL);
1902 if (!nat_entry_slab)
1903 return -ENOMEM;
1904
1905 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1906 sizeof(struct free_nid), NULL);
1907 if (!free_nid_slab) {
1908 kmem_cache_destroy(nat_entry_slab);
1909 return -ENOMEM;
1910 }
1911 return 0;
1912}
1913
1914void destroy_node_manager_caches(void)
1915{
1916 kmem_cache_destroy(free_nid_slab);
1917 kmem_cache_destroy(nat_entry_slab);
1918}