blob: 6c6ef772cf01dcd79c355d89949ca9bda8a50174 [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 Kim0a8165d2012-11-29 13:28:09 +090085/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090086 * Readahead NAT pages
87 */
88static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid)
89{
90 struct address_space *mapping = sbi->meta_inode->i_mapping;
91 struct f2fs_nm_info *nm_i = NM_I(sbi);
92 struct page *page;
93 pgoff_t index;
94 int i;
95
96 for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
Chao Yucfb271d2013-12-05 17:15:22 +080097 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090098 nid = 0;
99 index = current_nat_addr(sbi, nid);
100
101 page = grab_cache_page(mapping, index);
102 if (!page)
103 continue;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900104 if (PageUptodate(page)) {
Jaegeuk Kim7107e0a2013-11-21 13:54:23 +0900105 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900106 f2fs_put_page(page, 1);
107 continue;
108 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900109 f2fs_submit_page_mbio(sbi, page, index, META, READ);
Jaegeuk Kim7107e0a2013-11-21 13:54:23 +0900110 mark_page_accessed(page);
Jaegeuk Kim369a7082013-01-31 10:15:35 +0900111 f2fs_put_page(page, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900112 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900113 f2fs_submit_merged_bio(sbi, META, true, READ);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900114}
115
116static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
117{
118 return radix_tree_lookup(&nm_i->nat_root, n);
119}
120
121static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
122 nid_t start, unsigned int nr, struct nat_entry **ep)
123{
124 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
125}
126
127static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
128{
129 list_del(&e->list);
130 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
131 nm_i->nat_cnt--;
132 kmem_cache_free(nat_entry_slab, e);
133}
134
135int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
136{
137 struct f2fs_nm_info *nm_i = NM_I(sbi);
138 struct nat_entry *e;
139 int is_cp = 1;
140
141 read_lock(&nm_i->nat_tree_lock);
142 e = __lookup_nat_cache(nm_i, nid);
143 if (e && !e->checkpointed)
144 is_cp = 0;
145 read_unlock(&nm_i->nat_tree_lock);
146 return is_cp;
147}
148
149static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
150{
151 struct nat_entry *new;
152
153 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
154 if (!new)
155 return NULL;
156 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
157 kmem_cache_free(nat_entry_slab, new);
158 return NULL;
159 }
160 memset(new, 0, sizeof(struct nat_entry));
161 nat_set_nid(new, nid);
162 list_add_tail(&new->list, &nm_i->nat_entries);
163 nm_i->nat_cnt++;
164 return new;
165}
166
167static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
168 struct f2fs_nat_entry *ne)
169{
170 struct nat_entry *e;
171retry:
172 write_lock(&nm_i->nat_tree_lock);
173 e = __lookup_nat_cache(nm_i, nid);
174 if (!e) {
175 e = grab_nat_entry(nm_i, nid);
176 if (!e) {
177 write_unlock(&nm_i->nat_tree_lock);
178 goto retry;
179 }
180 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
181 nat_set_ino(e, le32_to_cpu(ne->ino));
182 nat_set_version(e, ne->version);
183 e->checkpointed = true;
184 }
185 write_unlock(&nm_i->nat_tree_lock);
186}
187
188static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
189 block_t new_blkaddr)
190{
191 struct f2fs_nm_info *nm_i = NM_I(sbi);
192 struct nat_entry *e;
193retry:
194 write_lock(&nm_i->nat_tree_lock);
195 e = __lookup_nat_cache(nm_i, ni->nid);
196 if (!e) {
197 e = grab_nat_entry(nm_i, ni->nid);
198 if (!e) {
199 write_unlock(&nm_i->nat_tree_lock);
200 goto retry;
201 }
202 e->ni = *ni;
203 e->checkpointed = true;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900204 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900205 } else if (new_blkaddr == NEW_ADDR) {
206 /*
207 * when nid is reallocated,
208 * previous nat entry can be remained in nat cache.
209 * So, reinitialize it with new information.
210 */
211 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900212 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900213 }
214
215 if (new_blkaddr == NEW_ADDR)
216 e->checkpointed = false;
217
218 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900219 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
220 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900221 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900222 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900223 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900224 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900225 nat_get_blkaddr(e) != NULL_ADDR &&
226 new_blkaddr == NEW_ADDR);
227
228 /* increament version no as node is removed */
229 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
230 unsigned char version = nat_get_version(e);
231 nat_set_version(e, inc_node_version(version));
232 }
233
234 /* change address */
235 nat_set_blkaddr(e, new_blkaddr);
236 __set_nat_cache_dirty(nm_i, e);
237 write_unlock(&nm_i->nat_tree_lock);
238}
239
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900240int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900241{
242 struct f2fs_nm_info *nm_i = NM_I(sbi);
243
Haicheng Li6cac3752013-04-28 19:16:06 +0800244 if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900245 return 0;
246
247 write_lock(&nm_i->nat_tree_lock);
248 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
249 struct nat_entry *ne;
250 ne = list_first_entry(&nm_i->nat_entries,
251 struct nat_entry, list);
252 __del_from_nat_cache(nm_i, ne);
253 nr_shrink--;
254 }
255 write_unlock(&nm_i->nat_tree_lock);
256 return nr_shrink;
257}
258
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900259/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900260 * This function returns always success
261 */
262void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
263{
264 struct f2fs_nm_info *nm_i = NM_I(sbi);
265 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
266 struct f2fs_summary_block *sum = curseg->sum_blk;
267 nid_t start_nid = START_NID(nid);
268 struct f2fs_nat_block *nat_blk;
269 struct page *page = NULL;
270 struct f2fs_nat_entry ne;
271 struct nat_entry *e;
272 int i;
273
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900274 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900275 ni->nid = nid;
276
277 /* Check nat cache */
278 read_lock(&nm_i->nat_tree_lock);
279 e = __lookup_nat_cache(nm_i, nid);
280 if (e) {
281 ni->ino = nat_get_ino(e);
282 ni->blk_addr = nat_get_blkaddr(e);
283 ni->version = nat_get_version(e);
284 }
285 read_unlock(&nm_i->nat_tree_lock);
286 if (e)
287 return;
288
289 /* Check current segment summary */
290 mutex_lock(&curseg->curseg_mutex);
291 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
292 if (i >= 0) {
293 ne = nat_in_journal(sum, i);
294 node_info_from_raw_nat(ni, &ne);
295 }
296 mutex_unlock(&curseg->curseg_mutex);
297 if (i >= 0)
298 goto cache;
299
300 /* Fill node_info from nat page */
301 page = get_current_nat_page(sbi, start_nid);
302 nat_blk = (struct f2fs_nat_block *)page_address(page);
303 ne = nat_blk->entries[nid - start_nid];
304 node_info_from_raw_nat(ni, &ne);
305 f2fs_put_page(page, 1);
306cache:
307 /* cache nat entry */
308 cache_nat_entry(NM_I(sbi), nid, &ne);
309}
310
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900311/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900312 * The maximum depth is four.
313 * Offset[0] will have raw inode offset.
314 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900315static int get_node_path(struct f2fs_inode_info *fi, long block,
316 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900317{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900318 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900319 const long direct_blks = ADDRS_PER_BLOCK;
320 const long dptrs_per_blk = NIDS_PER_BLOCK;
321 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
322 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
323 int n = 0;
324 int level = 0;
325
326 noffset[0] = 0;
327
328 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900329 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900330 goto got;
331 }
332 block -= direct_index;
333 if (block < direct_blks) {
334 offset[n++] = NODE_DIR1_BLOCK;
335 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900336 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900337 level = 1;
338 goto got;
339 }
340 block -= direct_blks;
341 if (block < direct_blks) {
342 offset[n++] = NODE_DIR2_BLOCK;
343 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900344 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900345 level = 1;
346 goto got;
347 }
348 block -= direct_blks;
349 if (block < indirect_blks) {
350 offset[n++] = NODE_IND1_BLOCK;
351 noffset[n] = 3;
352 offset[n++] = block / direct_blks;
353 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900354 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900355 level = 2;
356 goto got;
357 }
358 block -= indirect_blks;
359 if (block < indirect_blks) {
360 offset[n++] = NODE_IND2_BLOCK;
361 noffset[n] = 4 + dptrs_per_blk;
362 offset[n++] = block / direct_blks;
363 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900364 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900365 level = 2;
366 goto got;
367 }
368 block -= indirect_blks;
369 if (block < dindirect_blks) {
370 offset[n++] = NODE_DIND_BLOCK;
371 noffset[n] = 5 + (dptrs_per_blk * 2);
372 offset[n++] = block / indirect_blks;
373 noffset[n] = 6 + (dptrs_per_blk * 2) +
374 offset[n - 1] * (dptrs_per_blk + 1);
375 offset[n++] = (block / direct_blks) % dptrs_per_blk;
376 noffset[n] = 7 + (dptrs_per_blk * 2) +
377 offset[n - 2] * (dptrs_per_blk + 1) +
378 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900379 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900380 level = 3;
381 goto got;
382 } else {
383 BUG();
384 }
385got:
386 return level;
387}
388
389/*
390 * Caller should call f2fs_put_dnode(dn).
Jaegeuk Kim39936832012-11-22 16:21:29 +0900391 * Also, it should grab and release a mutex by calling mutex_lock_op() and
392 * mutex_unlock_op() only if ro is not set RDONLY_NODE.
393 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900394 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900395int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900396{
397 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
398 struct page *npage[4];
399 struct page *parent;
400 int offset[4];
401 unsigned int noffset[4];
402 nid_t nids[4];
403 int level, i;
404 int err = 0;
405
Jaegeuk Kimde936532013-08-12 21:08:03 +0900406 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900407
408 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900409 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900411 if (!npage[0]) {
412 npage[0] = get_node_page(sbi, nids[0]);
413 if (IS_ERR(npage[0]))
414 return PTR_ERR(npage[0]);
415 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900416 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900417 if (level != 0)
418 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900419 dn->inode_page = npage[0];
420 dn->inode_page_locked = true;
421
422 /* get indirect or direct nodes */
423 for (i = 1; i <= level; i++) {
424 bool done = false;
425
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900426 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900427 /* alloc new node */
428 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900429 err = -ENOSPC;
430 goto release_pages;
431 }
432
433 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900434 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900435 if (IS_ERR(npage[i])) {
436 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900437 err = PTR_ERR(npage[i]);
438 goto release_pages;
439 }
440
441 set_nid(parent, offset[i - 1], nids[i], i == 1);
442 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900443 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900444 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900445 npage[i] = get_node_page_ra(parent, offset[i - 1]);
446 if (IS_ERR(npage[i])) {
447 err = PTR_ERR(npage[i]);
448 goto release_pages;
449 }
450 done = true;
451 }
452 if (i == 1) {
453 dn->inode_page_locked = false;
454 unlock_page(parent);
455 } else {
456 f2fs_put_page(parent, 1);
457 }
458
459 if (!done) {
460 npage[i] = get_node_page(sbi, nids[i]);
461 if (IS_ERR(npage[i])) {
462 err = PTR_ERR(npage[i]);
463 f2fs_put_page(npage[0], 0);
464 goto release_out;
465 }
466 }
467 if (i < level) {
468 parent = npage[i];
469 nids[i + 1] = get_nid(parent, offset[i], false);
470 }
471 }
472 dn->nid = nids[level];
473 dn->ofs_in_node = offset[level];
474 dn->node_page = npage[level];
475 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
476 return 0;
477
478release_pages:
479 f2fs_put_page(parent, 1);
480 if (i > 1)
481 f2fs_put_page(npage[0], 0);
482release_out:
483 dn->inode_page = NULL;
484 dn->node_page = NULL;
485 return err;
486}
487
488static void truncate_node(struct dnode_of_data *dn)
489{
490 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
491 struct node_info ni;
492
493 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900494 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900495 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900496 goto invalidate;
497 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900498 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900499
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900500 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900501 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800502 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900503 set_node_addr(sbi, &ni, NULL_ADDR);
504
505 if (dn->nid == dn->inode->i_ino) {
506 remove_orphan_inode(sbi, dn->nid);
507 dec_valid_inode_count(sbi);
508 } else {
509 sync_inode_page(dn);
510 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900511invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512 clear_node_page_dirty(dn->node_page);
513 F2FS_SET_SB_DIRT(sbi);
514
515 f2fs_put_page(dn->node_page, 1);
516 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900517 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900518}
519
520static int truncate_dnode(struct dnode_of_data *dn)
521{
522 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
523 struct page *page;
524
525 if (dn->nid == 0)
526 return 1;
527
528 /* get direct node */
529 page = get_node_page(sbi, dn->nid);
530 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
531 return 1;
532 else if (IS_ERR(page))
533 return PTR_ERR(page);
534
535 /* Make dnode_of_data for parameter */
536 dn->node_page = page;
537 dn->ofs_in_node = 0;
538 truncate_data_blocks(dn);
539 truncate_node(dn);
540 return 1;
541}
542
543static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
544 int ofs, int depth)
545{
546 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
547 struct dnode_of_data rdn = *dn;
548 struct page *page;
549 struct f2fs_node *rn;
550 nid_t child_nid;
551 unsigned int child_nofs;
552 int freed = 0;
553 int i, ret;
554
555 if (dn->nid == 0)
556 return NIDS_PER_BLOCK + 1;
557
Namjae Jeon51dd6242013-04-20 01:28:52 +0900558 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
559
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900560 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900561 if (IS_ERR(page)) {
562 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900563 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900564 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900565
Gu Zheng45590712013-07-15 17:57:38 +0800566 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900567 if (depth < 3) {
568 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
569 child_nid = le32_to_cpu(rn->in.nid[i]);
570 if (child_nid == 0)
571 continue;
572 rdn.nid = child_nid;
573 ret = truncate_dnode(&rdn);
574 if (ret < 0)
575 goto out_err;
576 set_nid(page, i, 0, false);
577 }
578 } else {
579 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
580 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
581 child_nid = le32_to_cpu(rn->in.nid[i]);
582 if (child_nid == 0) {
583 child_nofs += NIDS_PER_BLOCK + 1;
584 continue;
585 }
586 rdn.nid = child_nid;
587 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
588 if (ret == (NIDS_PER_BLOCK + 1)) {
589 set_nid(page, i, 0, false);
590 child_nofs += ret;
591 } else if (ret < 0 && ret != -ENOENT) {
592 goto out_err;
593 }
594 }
595 freed = child_nofs;
596 }
597
598 if (!ofs) {
599 /* remove current indirect node */
600 dn->node_page = page;
601 truncate_node(dn);
602 freed++;
603 } else {
604 f2fs_put_page(page, 1);
605 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900606 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900607 return freed;
608
609out_err:
610 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900611 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900612 return ret;
613}
614
615static int truncate_partial_nodes(struct dnode_of_data *dn,
616 struct f2fs_inode *ri, int *offset, int depth)
617{
618 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
619 struct page *pages[2];
620 nid_t nid[3];
621 nid_t child_nid;
622 int err = 0;
623 int i;
624 int idx = depth - 2;
625
626 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
627 if (!nid[0])
628 return 0;
629
630 /* get indirect nodes in the path */
631 for (i = 0; i < depth - 1; i++) {
632 /* refernece count'll be increased */
633 pages[i] = get_node_page(sbi, nid[i]);
634 if (IS_ERR(pages[i])) {
635 depth = i + 1;
636 err = PTR_ERR(pages[i]);
637 goto fail;
638 }
639 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
640 }
641
642 /* free direct nodes linked to a partial indirect node */
643 for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) {
644 child_nid = get_nid(pages[idx], i, false);
645 if (!child_nid)
646 continue;
647 dn->nid = child_nid;
648 err = truncate_dnode(dn);
649 if (err < 0)
650 goto fail;
651 set_nid(pages[idx], i, 0, false);
652 }
653
654 if (offset[depth - 1] == 0) {
655 dn->node_page = pages[idx];
656 dn->nid = nid[idx];
657 truncate_node(dn);
658 } else {
659 f2fs_put_page(pages[idx], 1);
660 }
661 offset[idx]++;
662 offset[depth - 1] = 0;
663fail:
664 for (i = depth - 3; i >= 0; i--)
665 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900666
667 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
668
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900669 return err;
670}
671
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900672/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900673 * All the block addresses of data and nodes should be nullified.
674 */
675int truncate_inode_blocks(struct inode *inode, pgoff_t from)
676{
677 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900678 struct address_space *node_mapping = sbi->node_inode->i_mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900679 int err = 0, cont = 1;
680 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900681 unsigned int nofs = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900682 struct f2fs_node *rn;
683 struct dnode_of_data dn;
684 struct page *page;
685
Namjae Jeon51dd6242013-04-20 01:28:52 +0900686 trace_f2fs_truncate_inode_blocks_enter(inode, from);
687
Jaegeuk Kimde936532013-08-12 21:08:03 +0900688 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900689restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900690 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900691 if (IS_ERR(page)) {
692 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900693 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900694 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900695
696 set_new_dnode(&dn, inode, page, NULL, 0);
697 unlock_page(page);
698
Gu Zheng45590712013-07-15 17:57:38 +0800699 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900700 switch (level) {
701 case 0:
702 case 1:
703 nofs = noffset[1];
704 break;
705 case 2:
706 nofs = noffset[1];
707 if (!offset[level - 1])
708 goto skip_partial;
709 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
710 if (err < 0 && err != -ENOENT)
711 goto fail;
712 nofs += 1 + NIDS_PER_BLOCK;
713 break;
714 case 3:
715 nofs = 5 + 2 * NIDS_PER_BLOCK;
716 if (!offset[level - 1])
717 goto skip_partial;
718 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
719 if (err < 0 && err != -ENOENT)
720 goto fail;
721 break;
722 default:
723 BUG();
724 }
725
726skip_partial:
727 while (cont) {
728 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]);
729 switch (offset[0]) {
730 case NODE_DIR1_BLOCK:
731 case NODE_DIR2_BLOCK:
732 err = truncate_dnode(&dn);
733 break;
734
735 case NODE_IND1_BLOCK:
736 case NODE_IND2_BLOCK:
737 err = truncate_nodes(&dn, nofs, offset[1], 2);
738 break;
739
740 case NODE_DIND_BLOCK:
741 err = truncate_nodes(&dn, nofs, offset[1], 3);
742 cont = 0;
743 break;
744
745 default:
746 BUG();
747 }
748 if (err < 0 && err != -ENOENT)
749 goto fail;
750 if (offset[1] == 0 &&
751 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) {
752 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900753 if (unlikely(page->mapping != node_mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900754 f2fs_put_page(page, 1);
755 goto restart;
756 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900757 wait_on_page_writeback(page);
758 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
759 set_page_dirty(page);
760 unlock_page(page);
761 }
762 offset[1] = 0;
763 offset[0]++;
764 nofs += err;
765 }
766fail:
767 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900768 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900769 return err > 0 ? 0 : err;
770}
771
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900772int truncate_xattr_node(struct inode *inode, struct page *page)
773{
774 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
775 nid_t nid = F2FS_I(inode)->i_xattr_nid;
776 struct dnode_of_data dn;
777 struct page *npage;
778
779 if (!nid)
780 return 0;
781
782 npage = get_node_page(sbi, nid);
783 if (IS_ERR(npage))
784 return PTR_ERR(npage);
785
786 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900787
788 /* need to do checkpoint during fsync */
789 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
790
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900791 set_new_dnode(&dn, inode, page, npage, nid);
792
793 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800794 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900795 truncate_node(&dn);
796 return 0;
797}
798
Jaegeuk Kim39936832012-11-22 16:21:29 +0900799/*
800 * Caller should grab and release a mutex by calling mutex_lock_op() and
801 * mutex_unlock_op().
802 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800803void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900804{
805 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
806 struct page *page;
807 nid_t ino = inode->i_ino;
808 struct dnode_of_data dn;
809
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900810 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900811 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800812 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900813
Gu Zheng58e674d2013-11-19 18:03:18 +0800814 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900815 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800816 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900817 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900818 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900819 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900820 set_new_dnode(&dn, inode, page, page, ino);
821 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900822}
823
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900824struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900825{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900826 struct dnode_of_data dn;
827
828 /* allocate inode page for new inode */
829 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900830
831 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900832 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900833}
834
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900835struct page *new_node_page(struct dnode_of_data *dn,
836 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900837{
838 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
839 struct address_space *mapping = sbi->node_inode->i_mapping;
840 struct node_info old_ni, new_ni;
841 struct page *page;
842 int err;
843
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900844 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900845 return ERR_PTR(-EPERM);
846
847 page = grab_cache_page(mapping, dn->nid);
848 if (!page)
849 return ERR_PTR(-ENOMEM);
850
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900851 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900852 err = -ENOSPC;
853 goto fail;
854 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900856 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900857
858 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900859 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900860 new_ni = old_ni;
861 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900862 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900863
864 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900865 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900866 SetPageUptodate(page);
867 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900868
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900869 if (ofs == XATTR_NODE_OFFSET)
870 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
871
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900872 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900873 if (ipage)
874 update_inode(dn->inode, ipage);
875 else
876 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900877 if (ofs == 0)
878 inc_valid_inode_count(sbi);
879
880 return page;
881
882fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900883 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900884 f2fs_put_page(page, 1);
885 return ERR_PTR(err);
886}
887
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900888/*
889 * Caller should do after getting the following values.
890 * 0: f2fs_put_page(page, 0)
891 * LOCKED_PAGE: f2fs_put_page(page, 1)
892 * error: nothing
893 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900894static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900895{
896 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
897 struct node_info ni;
898
899 get_node_info(sbi, page->index, &ni);
900
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900901 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900902 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900903 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900904 }
905
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900906 if (PageUptodate(page))
907 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900908
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900909 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900910}
911
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900912/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900913 * Readahead a node page
914 */
915void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
916{
917 struct address_space *mapping = sbi->node_inode->i_mapping;
918 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900919 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900920
921 apage = find_get_page(mapping, nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900922 if (apage && PageUptodate(apage)) {
923 f2fs_put_page(apage, 0);
924 return;
925 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900926 f2fs_put_page(apage, 0);
927
928 apage = grab_cache_page(mapping, nid);
929 if (!apage)
930 return;
931
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900932 err = read_node_page(apage, READA);
933 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900934 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900935 else if (err == LOCKED_PAGE)
936 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900937}
938
939struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
940{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900941 struct address_space *mapping = sbi->node_inode->i_mapping;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900942 struct page *page;
943 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900944repeat:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900945 page = grab_cache_page(mapping, nid);
946 if (!page)
947 return ERR_PTR(-ENOMEM);
948
949 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900950 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900951 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900952 else if (err == LOCKED_PAGE)
953 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900954
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900955 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900956 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900957 f2fs_put_page(page, 1);
958 return ERR_PTR(-EIO);
959 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900960 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900961 f2fs_put_page(page, 1);
962 goto repeat;
963 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900964got_it:
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900965 f2fs_bug_on(nid != nid_of_node(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900966 mark_page_accessed(page);
967 return page;
968}
969
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900970/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900971 * Return a locked page for the desired node page.
972 * And, readahead MAX_RA_NODE number of node pages.
973 */
974struct page *get_node_page_ra(struct page *parent, int start)
975{
976 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
977 struct address_space *mapping = sbi->node_inode->i_mapping;
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900978 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900980 int err, i, end;
981 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900982
983 /* First, try getting the desired direct node. */
984 nid = get_nid(parent, start, false);
985 if (!nid)
986 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900987repeat:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988 page = grab_cache_page(mapping, nid);
989 if (!page)
990 return ERR_PTR(-ENOMEM);
991
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900992 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900993 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900994 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900995 else if (err == LOCKED_PAGE)
996 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900997
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900998 blk_start_plug(&plug);
999
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001000 /* Then, try readahead for siblings of the desired node */
1001 end = start + MAX_RA_NODE;
1002 end = min(end, NIDS_PER_BLOCK);
1003 for (i = start + 1; i < end; i++) {
1004 nid = get_nid(parent, i, false);
1005 if (!nid)
1006 continue;
1007 ra_node_page(sbi, nid);
1008 }
1009
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001010 blk_finish_plug(&plug);
1011
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001012 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001013 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001014 f2fs_put_page(page, 1);
1015 goto repeat;
1016 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001017page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001018 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001019 f2fs_put_page(page, 1);
1020 return ERR_PTR(-EIO);
1021 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001022 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001023 return page;
1024}
1025
1026void sync_inode_page(struct dnode_of_data *dn)
1027{
1028 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1029 update_inode(dn->inode, dn->node_page);
1030 } else if (dn->inode_page) {
1031 if (!dn->inode_page_locked)
1032 lock_page(dn->inode_page);
1033 update_inode(dn->inode, dn->inode_page);
1034 if (!dn->inode_page_locked)
1035 unlock_page(dn->inode_page);
1036 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001037 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001038 }
1039}
1040
1041int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1042 struct writeback_control *wbc)
1043{
1044 struct address_space *mapping = sbi->node_inode->i_mapping;
1045 pgoff_t index, end;
1046 struct pagevec pvec;
1047 int step = ino ? 2 : 0;
1048 int nwritten = 0, wrote = 0;
1049
1050 pagevec_init(&pvec, 0);
1051
1052next_step:
1053 index = 0;
1054 end = LONG_MAX;
1055
1056 while (index <= end) {
1057 int i, nr_pages;
1058 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1059 PAGECACHE_TAG_DIRTY,
1060 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1061 if (nr_pages == 0)
1062 break;
1063
1064 for (i = 0; i < nr_pages; i++) {
1065 struct page *page = pvec.pages[i];
1066
1067 /*
1068 * flushing sequence with step:
1069 * 0. indirect nodes
1070 * 1. dentry dnodes
1071 * 2. file dnodes
1072 */
1073 if (step == 0 && IS_DNODE(page))
1074 continue;
1075 if (step == 1 && (!IS_DNODE(page) ||
1076 is_cold_node(page)))
1077 continue;
1078 if (step == 2 && (!IS_DNODE(page) ||
1079 !is_cold_node(page)))
1080 continue;
1081
1082 /*
1083 * If an fsync mode,
1084 * we should not skip writing node pages.
1085 */
1086 if (ino && ino_of_node(page) == ino)
1087 lock_page(page);
1088 else if (!trylock_page(page))
1089 continue;
1090
1091 if (unlikely(page->mapping != mapping)) {
1092continue_unlock:
1093 unlock_page(page);
1094 continue;
1095 }
1096 if (ino && ino_of_node(page) != ino)
1097 goto continue_unlock;
1098
1099 if (!PageDirty(page)) {
1100 /* someone wrote it for us */
1101 goto continue_unlock;
1102 }
1103
1104 if (!clear_page_dirty_for_io(page))
1105 goto continue_unlock;
1106
1107 /* called by fsync() */
1108 if (ino && IS_DNODE(page)) {
1109 int mark = !is_checkpointed_node(sbi, ino);
1110 set_fsync_mark(page, 1);
1111 if (IS_INODE(page))
1112 set_dentry_mark(page, mark);
1113 nwritten++;
1114 } else {
1115 set_fsync_mark(page, 0);
1116 set_dentry_mark(page, 0);
1117 }
1118 mapping->a_ops->writepage(page, wbc);
1119 wrote++;
1120
1121 if (--wbc->nr_to_write == 0)
1122 break;
1123 }
1124 pagevec_release(&pvec);
1125 cond_resched();
1126
1127 if (wbc->nr_to_write == 0) {
1128 step = 2;
1129 break;
1130 }
1131 }
1132
1133 if (step < 2) {
1134 step++;
1135 goto next_step;
1136 }
1137
1138 if (wrote)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001139 f2fs_submit_merged_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL,
1140 WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001141 return nwritten;
1142}
1143
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001144int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1145{
1146 struct address_space *mapping = sbi->node_inode->i_mapping;
1147 pgoff_t index = 0, end = LONG_MAX;
1148 struct pagevec pvec;
1149 int nr_pages;
1150 int ret2 = 0, ret = 0;
1151
1152 pagevec_init(&pvec, 0);
1153 while ((index <= end) &&
1154 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1155 PAGECACHE_TAG_WRITEBACK,
1156 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
1157 unsigned i;
1158
1159 for (i = 0; i < nr_pages; i++) {
1160 struct page *page = pvec.pages[i];
1161
1162 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001163 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001164 continue;
1165
Chao Yu4bf08ff2013-11-04 10:28:33 +08001166 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001167 wait_on_page_writeback(page);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001168 if (TestClearPageError(page))
1169 ret = -EIO;
1170 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001171 }
1172 pagevec_release(&pvec);
1173 cond_resched();
1174 }
1175
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001176 if (unlikely(test_and_clear_bit(AS_ENOSPC, &mapping->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001177 ret2 = -ENOSPC;
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001178 if (unlikely(test_and_clear_bit(AS_EIO, &mapping->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001179 ret2 = -EIO;
1180 if (!ret)
1181 ret = ret2;
1182 return ret;
1183}
1184
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001185static int f2fs_write_node_page(struct page *page,
1186 struct writeback_control *wbc)
1187{
1188 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1189 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001190 block_t new_addr;
1191 struct node_info ni;
1192
Chao Yucfb271d2013-12-05 17:15:22 +08001193 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001194 goto redirty_out;
1195
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001196 wait_on_page_writeback(page);
1197
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001198 /* get old block addr of this node page */
1199 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001200 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001201
1202 get_node_info(sbi, nid, &ni);
1203
1204 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001205 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001206 dec_page_count(sbi, F2FS_DIRTY_NODES);
1207 unlock_page(page);
1208 return 0;
1209 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001210
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001211 if (wbc->for_reclaim)
1212 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001213
Jaegeuk Kim39936832012-11-22 16:21:29 +09001214 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001215 set_page_writeback(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001216 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1217 set_node_addr(sbi, &ni, new_addr);
1218 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001219 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001220 unlock_page(page);
1221 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001222
1223redirty_out:
1224 dec_page_count(sbi, F2FS_DIRTY_NODES);
1225 wbc->pages_skipped++;
1226 set_page_dirty(page);
1227 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001228}
1229
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001230/*
1231 * It is very important to gather dirty pages and write at once, so that we can
1232 * submit a big bio without interfering other data writes.
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001233 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001234 */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001235#define COLLECT_DIRTY_NODES 1536
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236static int f2fs_write_node_pages(struct address_space *mapping,
1237 struct writeback_control *wbc)
1238{
1239 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001240 long nr_to_write = wbc->nr_to_write;
1241
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001242 /* balancing f2fs's metadata in background */
1243 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001244
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001245 /* collect a number of dirty node pages and write together */
1246 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1247 return 0;
1248
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001249 /* if mounting is failed, skip writing node pages */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001250 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001251 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001252 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1253 wbc->nr_to_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001254 return 0;
1255}
1256
1257static int f2fs_set_node_page_dirty(struct page *page)
1258{
1259 struct address_space *mapping = page->mapping;
1260 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1261
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001262 trace_f2fs_set_page_dirty(page, NODE);
1263
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001264 SetPageUptodate(page);
1265 if (!PageDirty(page)) {
1266 __set_page_dirty_nobuffers(page);
1267 inc_page_count(sbi, F2FS_DIRTY_NODES);
1268 SetPagePrivate(page);
1269 return 1;
1270 }
1271 return 0;
1272}
1273
Lukas Czernerd47992f2013-05-21 23:17:23 -04001274static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1275 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001276{
1277 struct inode *inode = page->mapping->host;
1278 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1279 if (PageDirty(page))
1280 dec_page_count(sbi, F2FS_DIRTY_NODES);
1281 ClearPagePrivate(page);
1282}
1283
1284static int f2fs_release_node_page(struct page *page, gfp_t wait)
1285{
1286 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001287 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001288}
1289
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001290/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291 * Structure of the f2fs node operations
1292 */
1293const struct address_space_operations f2fs_node_aops = {
1294 .writepage = f2fs_write_node_page,
1295 .writepages = f2fs_write_node_pages,
1296 .set_page_dirty = f2fs_set_node_page_dirty,
1297 .invalidatepage = f2fs_invalidate_node_page,
1298 .releasepage = f2fs_release_node_page,
1299};
1300
1301static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1302{
1303 struct list_head *this;
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001304 struct free_nid *i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001305 list_for_each(this, head) {
1306 i = list_entry(this, struct free_nid, list);
1307 if (i->nid == n)
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001308 return i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001309 }
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001310 return NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001311}
1312
1313static void __del_from_free_nid_list(struct free_nid *i)
1314{
1315 list_del(&i->list);
1316 kmem_cache_free(free_nid_slab, i);
1317}
1318
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001319static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001320{
1321 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001322 struct nat_entry *ne;
1323 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001324
1325 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
Haicheng Li23d38842013-05-06 23:15:43 +08001326 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001327
1328 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001329 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001330 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001331
Gu Zheng7bd59382013-10-22 14:52:26 +08001332 if (build) {
1333 /* do not add allocated nids */
1334 read_lock(&nm_i->nat_tree_lock);
1335 ne = __lookup_nat_cache(nm_i, nid);
1336 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1337 allocated = true;
1338 read_unlock(&nm_i->nat_tree_lock);
1339 if (allocated)
1340 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001341 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001342
1343 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344 i->nid = nid;
1345 i->state = NID_NEW;
1346
1347 spin_lock(&nm_i->free_nid_list_lock);
1348 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1349 spin_unlock(&nm_i->free_nid_list_lock);
1350 kmem_cache_free(free_nid_slab, i);
1351 return 0;
1352 }
1353 list_add_tail(&i->list, &nm_i->free_nid_list);
1354 nm_i->fcnt++;
1355 spin_unlock(&nm_i->free_nid_list_lock);
1356 return 1;
1357}
1358
1359static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1360{
1361 struct free_nid *i;
1362 spin_lock(&nm_i->free_nid_list_lock);
1363 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1364 if (i && i->state == NID_NEW) {
1365 __del_from_free_nid_list(i);
1366 nm_i->fcnt--;
1367 }
1368 spin_unlock(&nm_i->free_nid_list_lock);
1369}
1370
Haicheng Li87609522013-05-06 23:15:42 +08001371static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001372 struct page *nat_page, nid_t start_nid)
1373{
1374 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1375 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001376 int i;
1377
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001378 i = start_nid % NAT_ENTRY_PER_BLOCK;
1379
1380 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001381
Chao Yucfb271d2013-12-05 17:15:22 +08001382 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001383 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001384
1385 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001386 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001387 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001388 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001389 break;
1390 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001391 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392}
1393
1394static void build_free_nids(struct f2fs_sb_info *sbi)
1395{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001396 struct f2fs_nm_info *nm_i = NM_I(sbi);
1397 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1398 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001399 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001400 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001401
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001402 /* Enough entries */
1403 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1404 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001405
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001406 /* readahead nat pages to be scanned */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001407 ra_nat_pages(sbi, nid);
1408
1409 while (1) {
1410 struct page *page = get_current_nat_page(sbi, nid);
1411
Haicheng Li87609522013-05-06 23:15:42 +08001412 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001413 f2fs_put_page(page, 1);
1414
1415 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001416 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001417 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001418
1419 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420 break;
1421 }
1422
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001423 /* go to the next free nat pages to find free nids abundantly */
1424 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425
1426 /* find free nids from current sum_pages */
1427 mutex_lock(&curseg->curseg_mutex);
1428 for (i = 0; i < nats_in_cursum(sum); i++) {
1429 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1430 nid = le32_to_cpu(nid_in_journal(sum, i));
1431 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001432 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001433 else
1434 remove_free_nid(nm_i, nid);
1435 }
1436 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001437}
1438
1439/*
1440 * If this function returns success, caller can obtain a new nid
1441 * from second parameter of this function.
1442 * The returned nid could be used ino as well as nid when inode is created.
1443 */
1444bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1445{
1446 struct f2fs_nm_info *nm_i = NM_I(sbi);
1447 struct free_nid *i = NULL;
1448 struct list_head *this;
1449retry:
Chao Yucfb271d2013-12-05 17:15:22 +08001450 if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001451 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001452
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001453 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001454
1455 /* We should not use stale free nids created by build_free_nids */
1456 if (nm_i->fcnt && !sbi->on_build_free_nids) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001457 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001458 list_for_each(this, &nm_i->free_nid_list) {
1459 i = list_entry(this, struct free_nid, list);
1460 if (i->state == NID_NEW)
1461 break;
1462 }
1463
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001464 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001465 *nid = i->nid;
1466 i->state = NID_ALLOC;
1467 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001468 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001469 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001470 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001471 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001472
1473 /* Let's scan nat pages and its caches to get free nids */
1474 mutex_lock(&nm_i->build_lock);
Haicheng Liaabe5132013-10-23 12:39:32 +08001475 sbi->on_build_free_nids = true;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001476 build_free_nids(sbi);
Haicheng Liaabe5132013-10-23 12:39:32 +08001477 sbi->on_build_free_nids = false;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001478 mutex_unlock(&nm_i->build_lock);
1479 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001480}
1481
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001482/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001483 * alloc_nid() should be called prior to this function.
1484 */
1485void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1486{
1487 struct f2fs_nm_info *nm_i = NM_I(sbi);
1488 struct free_nid *i;
1489
1490 spin_lock(&nm_i->free_nid_list_lock);
1491 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001492 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001493 __del_from_free_nid_list(i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001494 spin_unlock(&nm_i->free_nid_list_lock);
1495}
1496
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001497/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001498 * alloc_nid() should be called prior to this function.
1499 */
1500void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1501{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001502 struct f2fs_nm_info *nm_i = NM_I(sbi);
1503 struct free_nid *i;
1504
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001505 if (!nid)
1506 return;
1507
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001508 spin_lock(&nm_i->free_nid_list_lock);
1509 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001510 f2fs_bug_on(!i || i->state != NID_ALLOC);
Haicheng Li95630cb2013-05-06 23:15:41 +08001511 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1512 __del_from_free_nid_list(i);
1513 } else {
1514 i->state = NID_NEW;
1515 nm_i->fcnt++;
1516 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001517 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001518}
1519
1520void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1521 struct f2fs_summary *sum, struct node_info *ni,
1522 block_t new_blkaddr)
1523{
1524 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1525 set_node_addr(sbi, ni, new_blkaddr);
1526 clear_node_page_dirty(page);
1527}
1528
1529int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1530{
1531 struct address_space *mapping = sbi->node_inode->i_mapping;
1532 struct f2fs_node *src, *dst;
1533 nid_t ino = ino_of_node(page);
1534 struct node_info old_ni, new_ni;
1535 struct page *ipage;
1536
1537 ipage = grab_cache_page(mapping, ino);
1538 if (!ipage)
1539 return -ENOMEM;
1540
1541 /* Should not use this inode from free nid list */
1542 remove_free_nid(NM_I(sbi), ino);
1543
1544 get_node_info(sbi, ino, &old_ni);
1545 SetPageUptodate(ipage);
1546 fill_node_footer(ipage, ino, ino, 0, true);
1547
Gu Zheng45590712013-07-15 17:57:38 +08001548 src = F2FS_NODE(page);
1549 dst = F2FS_NODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001550
1551 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1552 dst->i.i_size = 0;
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001553 dst->i.i_blocks = cpu_to_le64(1);
1554 dst->i.i_links = cpu_to_le32(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001555 dst->i.i_xattr_nid = 0;
1556
1557 new_ni = old_ni;
1558 new_ni.ino = ino;
1559
Chao Yucfb271d2013-12-05 17:15:22 +08001560 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001561 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001562 set_node_addr(sbi, &new_ni, NEW_ADDR);
1563 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001564 f2fs_put_page(ipage, 1);
1565 return 0;
1566}
1567
Chao Yu9af0ff12013-11-22 15:48:54 +08001568/*
1569 * ra_sum_pages() merge contiguous pages into one bio and submit.
1570 * these pre-readed pages are linked in pages list.
1571 */
1572static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1573 int start, int nrpages)
1574{
1575 struct page *page;
1576 int page_idx = start;
1577
1578 for (; page_idx < start + nrpages; page_idx++) {
1579 /* alloc temporal page for read node summary info*/
Chao Yua0acdfe2013-12-05 09:54:00 +08001580 page = alloc_page(GFP_F2FS_ZERO);
Chao Yu9af0ff12013-11-22 15:48:54 +08001581 if (!page) {
1582 struct page *tmp;
1583 list_for_each_entry_safe(page, tmp, pages, lru) {
1584 list_del(&page->lru);
1585 unlock_page(page);
1586 __free_pages(page, 0);
1587 }
1588 return -ENOMEM;
1589 }
1590
1591 lock_page(page);
1592 page->index = page_idx;
1593 list_add_tail(&page->lru, pages);
1594 }
1595
1596 list_for_each_entry(page, pages, lru)
1597 f2fs_submit_page_mbio(sbi, page, page->index, META, READ);
1598
1599 f2fs_submit_merged_bio(sbi, META, true, READ);
1600 return 0;
1601}
1602
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001603int restore_node_summary(struct f2fs_sb_info *sbi,
1604 unsigned int segno, struct f2fs_summary_block *sum)
1605{
1606 struct f2fs_node *rn;
1607 struct f2fs_summary *sum_entry;
Chao Yu9af0ff12013-11-22 15:48:54 +08001608 struct page *page, *tmp;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001609 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001610 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1611 int i, last_offset, nrpages, err = 0;
1612 LIST_HEAD(page_list);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001613
1614 /* scan the node segment */
1615 last_offset = sbi->blocks_per_seg;
1616 addr = START_BLOCK(sbi, segno);
1617 sum_entry = &sum->entries[0];
1618
Chao Yu9af0ff12013-11-22 15:48:54 +08001619 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
1620 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001621
Chao Yu9af0ff12013-11-22 15:48:54 +08001622 /* read ahead node pages */
1623 err = ra_sum_pages(sbi, &page_list, addr, nrpages);
1624 if (err)
1625 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001626
Chao Yu9af0ff12013-11-22 15:48:54 +08001627 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1628
1629 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001630 if (unlikely(!PageUptodate(page))) {
1631 err = -EIO;
1632 } else {
Chao Yu9af0ff12013-11-22 15:48:54 +08001633 rn = F2FS_NODE(page);
1634 sum_entry->nid = rn->footer.nid;
1635 sum_entry->version = 0;
1636 sum_entry->ofs_in_node = 0;
1637 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001638 }
1639
1640 list_del(&page->lru);
1641 unlock_page(page);
1642 __free_pages(page, 0);
1643 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001644 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001645 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001646}
1647
1648static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1649{
1650 struct f2fs_nm_info *nm_i = NM_I(sbi);
1651 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1652 struct f2fs_summary_block *sum = curseg->sum_blk;
1653 int i;
1654
1655 mutex_lock(&curseg->curseg_mutex);
1656
1657 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1658 mutex_unlock(&curseg->curseg_mutex);
1659 return false;
1660 }
1661
1662 for (i = 0; i < nats_in_cursum(sum); i++) {
1663 struct nat_entry *ne;
1664 struct f2fs_nat_entry raw_ne;
1665 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1666
1667 raw_ne = nat_in_journal(sum, i);
1668retry:
1669 write_lock(&nm_i->nat_tree_lock);
1670 ne = __lookup_nat_cache(nm_i, nid);
1671 if (ne) {
1672 __set_nat_cache_dirty(nm_i, ne);
1673 write_unlock(&nm_i->nat_tree_lock);
1674 continue;
1675 }
1676 ne = grab_nat_entry(nm_i, nid);
1677 if (!ne) {
1678 write_unlock(&nm_i->nat_tree_lock);
1679 goto retry;
1680 }
1681 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1682 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1683 nat_set_version(ne, raw_ne.version);
1684 __set_nat_cache_dirty(nm_i, ne);
1685 write_unlock(&nm_i->nat_tree_lock);
1686 }
1687 update_nats_in_cursum(sum, -i);
1688 mutex_unlock(&curseg->curseg_mutex);
1689 return true;
1690}
1691
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001692/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001693 * This function is called during the checkpointing process.
1694 */
1695void flush_nat_entries(struct f2fs_sb_info *sbi)
1696{
1697 struct f2fs_nm_info *nm_i = NM_I(sbi);
1698 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1699 struct f2fs_summary_block *sum = curseg->sum_blk;
1700 struct list_head *cur, *n;
1701 struct page *page = NULL;
1702 struct f2fs_nat_block *nat_blk = NULL;
1703 nid_t start_nid = 0, end_nid = 0;
1704 bool flushed;
1705
1706 flushed = flush_nats_in_journal(sbi);
1707
1708 if (!flushed)
1709 mutex_lock(&curseg->curseg_mutex);
1710
1711 /* 1) flush dirty nat caches */
1712 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1713 struct nat_entry *ne;
1714 nid_t nid;
1715 struct f2fs_nat_entry raw_ne;
1716 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001717 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001718
1719 ne = list_entry(cur, struct nat_entry, list);
1720 nid = nat_get_nid(ne);
1721
1722 if (nat_get_blkaddr(ne) == NEW_ADDR)
1723 continue;
1724 if (flushed)
1725 goto to_nat_page;
1726
1727 /* if there is room for nat enries in curseg->sumpage */
1728 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1729 if (offset >= 0) {
1730 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001731 goto flush_now;
1732 }
1733to_nat_page:
1734 if (!page || (start_nid > nid || nid > end_nid)) {
1735 if (page) {
1736 f2fs_put_page(page, 1);
1737 page = NULL;
1738 }
1739 start_nid = START_NID(nid);
1740 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1741
1742 /*
1743 * get nat block with dirty flag, increased reference
1744 * count, mapped and lock
1745 */
1746 page = get_next_nat_page(sbi, start_nid);
1747 nat_blk = page_address(page);
1748 }
1749
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001750 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001751 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001752flush_now:
1753 new_blkaddr = nat_get_blkaddr(ne);
1754
1755 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1756 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1757 raw_ne.version = nat_get_version(ne);
1758
1759 if (offset < 0) {
1760 nat_blk->entries[nid - start_nid] = raw_ne;
1761 } else {
1762 nat_in_journal(sum, offset) = raw_ne;
1763 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1764 }
1765
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001766 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001767 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001768 write_lock(&nm_i->nat_tree_lock);
1769 __del_from_nat_cache(nm_i, ne);
1770 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001771 } else {
1772 write_lock(&nm_i->nat_tree_lock);
1773 __clear_nat_cache_dirty(nm_i, ne);
1774 ne->checkpointed = true;
1775 write_unlock(&nm_i->nat_tree_lock);
1776 }
1777 }
1778 if (!flushed)
1779 mutex_unlock(&curseg->curseg_mutex);
1780 f2fs_put_page(page, 1);
1781
1782 /* 2) shrink nat caches if necessary */
1783 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1784}
1785
1786static int init_node_manager(struct f2fs_sb_info *sbi)
1787{
1788 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1789 struct f2fs_nm_info *nm_i = NM_I(sbi);
1790 unsigned char *version_bitmap;
1791 unsigned int nat_segs, nat_blocks;
1792
1793 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1794
1795 /* segment_count_nat includes pair segment so divide to 2. */
1796 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1797 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1798 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1799 nm_i->fcnt = 0;
1800 nm_i->nat_cnt = 0;
1801
1802 INIT_LIST_HEAD(&nm_i->free_nid_list);
1803 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1804 INIT_LIST_HEAD(&nm_i->nat_entries);
1805 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1806
1807 mutex_init(&nm_i->build_lock);
1808 spin_lock_init(&nm_i->free_nid_list_lock);
1809 rwlock_init(&nm_i->nat_tree_lock);
1810
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001811 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001812 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001813 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1814 if (!version_bitmap)
1815 return -EFAULT;
1816
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001817 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1818 GFP_KERNEL);
1819 if (!nm_i->nat_bitmap)
1820 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001821 return 0;
1822}
1823
1824int build_node_manager(struct f2fs_sb_info *sbi)
1825{
1826 int err;
1827
1828 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1829 if (!sbi->nm_info)
1830 return -ENOMEM;
1831
1832 err = init_node_manager(sbi);
1833 if (err)
1834 return err;
1835
1836 build_free_nids(sbi);
1837 return 0;
1838}
1839
1840void destroy_node_manager(struct f2fs_sb_info *sbi)
1841{
1842 struct f2fs_nm_info *nm_i = NM_I(sbi);
1843 struct free_nid *i, *next_i;
1844 struct nat_entry *natvec[NATVEC_SIZE];
1845 nid_t nid = 0;
1846 unsigned int found;
1847
1848 if (!nm_i)
1849 return;
1850
1851 /* destroy free nid list */
1852 spin_lock(&nm_i->free_nid_list_lock);
1853 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001854 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001855 __del_from_free_nid_list(i);
1856 nm_i->fcnt--;
1857 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001858 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001859 spin_unlock(&nm_i->free_nid_list_lock);
1860
1861 /* destroy nat cache */
1862 write_lock(&nm_i->nat_tree_lock);
1863 while ((found = __gang_lookup_nat_cache(nm_i,
1864 nid, NATVEC_SIZE, natvec))) {
1865 unsigned idx;
1866 for (idx = 0; idx < found; idx++) {
1867 struct nat_entry *e = natvec[idx];
1868 nid = nat_get_nid(e) + 1;
1869 __del_from_nat_cache(nm_i, e);
1870 }
1871 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001872 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001873 write_unlock(&nm_i->nat_tree_lock);
1874
1875 kfree(nm_i->nat_bitmap);
1876 sbi->nm_info = NULL;
1877 kfree(nm_i);
1878}
1879
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001880int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001881{
1882 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1883 sizeof(struct nat_entry), NULL);
1884 if (!nat_entry_slab)
1885 return -ENOMEM;
1886
1887 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1888 sizeof(struct free_nid), NULL);
1889 if (!free_nid_slab) {
1890 kmem_cache_destroy(nat_entry_slab);
1891 return -ENOMEM;
1892 }
1893 return 0;
1894}
1895
1896void destroy_node_manager_caches(void)
1897{
1898 kmem_cache_destroy(free_nid_slab);
1899 kmem_cache_destroy(nat_entry_slab);
1900}