blob: b843a5b3d5eec01e2c44a9c6f6dcdd950fd11683 [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);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +090092 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090093 struct page *page;
94 pgoff_t index;
95 int i;
96
Jaegeuk Kimc718379b2013-04-24 13:19:56 +090097 blk_start_plug(&plug);
98
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090099 for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
100 if (nid >= nm_i->max_nid)
101 nid = 0;
102 index = current_nat_addr(sbi, nid);
103
104 page = grab_cache_page(mapping, index);
105 if (!page)
106 continue;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900107 if (PageUptodate(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900108 f2fs_put_page(page, 1);
109 continue;
110 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900111 if (f2fs_readpage(sbi, page, index, READ))
112 continue;
113
Jaegeuk Kim369a7082013-01-31 10:15:35 +0900114 f2fs_put_page(page, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900115 }
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900116 blk_finish_plug(&plug);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900117}
118
119static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
120{
121 return radix_tree_lookup(&nm_i->nat_root, n);
122}
123
124static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
125 nid_t start, unsigned int nr, struct nat_entry **ep)
126{
127 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
128}
129
130static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
131{
132 list_del(&e->list);
133 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
134 nm_i->nat_cnt--;
135 kmem_cache_free(nat_entry_slab, e);
136}
137
138int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
139{
140 struct f2fs_nm_info *nm_i = NM_I(sbi);
141 struct nat_entry *e;
142 int is_cp = 1;
143
144 read_lock(&nm_i->nat_tree_lock);
145 e = __lookup_nat_cache(nm_i, nid);
146 if (e && !e->checkpointed)
147 is_cp = 0;
148 read_unlock(&nm_i->nat_tree_lock);
149 return is_cp;
150}
151
152static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
153{
154 struct nat_entry *new;
155
156 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
157 if (!new)
158 return NULL;
159 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
160 kmem_cache_free(nat_entry_slab, new);
161 return NULL;
162 }
163 memset(new, 0, sizeof(struct nat_entry));
164 nat_set_nid(new, nid);
165 list_add_tail(&new->list, &nm_i->nat_entries);
166 nm_i->nat_cnt++;
167 return new;
168}
169
170static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
171 struct f2fs_nat_entry *ne)
172{
173 struct nat_entry *e;
174retry:
175 write_lock(&nm_i->nat_tree_lock);
176 e = __lookup_nat_cache(nm_i, nid);
177 if (!e) {
178 e = grab_nat_entry(nm_i, nid);
179 if (!e) {
180 write_unlock(&nm_i->nat_tree_lock);
181 goto retry;
182 }
183 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
184 nat_set_ino(e, le32_to_cpu(ne->ino));
185 nat_set_version(e, ne->version);
186 e->checkpointed = true;
187 }
188 write_unlock(&nm_i->nat_tree_lock);
189}
190
191static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
192 block_t new_blkaddr)
193{
194 struct f2fs_nm_info *nm_i = NM_I(sbi);
195 struct nat_entry *e;
196retry:
197 write_lock(&nm_i->nat_tree_lock);
198 e = __lookup_nat_cache(nm_i, ni->nid);
199 if (!e) {
200 e = grab_nat_entry(nm_i, ni->nid);
201 if (!e) {
202 write_unlock(&nm_i->nat_tree_lock);
203 goto retry;
204 }
205 e->ni = *ni;
206 e->checkpointed = true;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900207 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900208 } else if (new_blkaddr == NEW_ADDR) {
209 /*
210 * when nid is reallocated,
211 * previous nat entry can be remained in nat cache.
212 * So, reinitialize it with new information.
213 */
214 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900215 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900216 }
217
218 if (new_blkaddr == NEW_ADDR)
219 e->checkpointed = false;
220
221 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900222 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
223 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900224 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900225 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900226 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900227 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900228 nat_get_blkaddr(e) != NULL_ADDR &&
229 new_blkaddr == NEW_ADDR);
230
231 /* increament version no as node is removed */
232 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
233 unsigned char version = nat_get_version(e);
234 nat_set_version(e, inc_node_version(version));
235 }
236
237 /* change address */
238 nat_set_blkaddr(e, new_blkaddr);
239 __set_nat_cache_dirty(nm_i, e);
240 write_unlock(&nm_i->nat_tree_lock);
241}
242
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900243int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900244{
245 struct f2fs_nm_info *nm_i = NM_I(sbi);
246
Haicheng Li6cac3752013-04-28 19:16:06 +0800247 if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900248 return 0;
249
250 write_lock(&nm_i->nat_tree_lock);
251 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
252 struct nat_entry *ne;
253 ne = list_first_entry(&nm_i->nat_entries,
254 struct nat_entry, list);
255 __del_from_nat_cache(nm_i, ne);
256 nr_shrink--;
257 }
258 write_unlock(&nm_i->nat_tree_lock);
259 return nr_shrink;
260}
261
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900262/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900263 * This function returns always success
264 */
265void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
266{
267 struct f2fs_nm_info *nm_i = NM_I(sbi);
268 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
269 struct f2fs_summary_block *sum = curseg->sum_blk;
270 nid_t start_nid = START_NID(nid);
271 struct f2fs_nat_block *nat_blk;
272 struct page *page = NULL;
273 struct f2fs_nat_entry ne;
274 struct nat_entry *e;
275 int i;
276
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900277 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900278 ni->nid = nid;
279
280 /* Check nat cache */
281 read_lock(&nm_i->nat_tree_lock);
282 e = __lookup_nat_cache(nm_i, nid);
283 if (e) {
284 ni->ino = nat_get_ino(e);
285 ni->blk_addr = nat_get_blkaddr(e);
286 ni->version = nat_get_version(e);
287 }
288 read_unlock(&nm_i->nat_tree_lock);
289 if (e)
290 return;
291
292 /* Check current segment summary */
293 mutex_lock(&curseg->curseg_mutex);
294 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
295 if (i >= 0) {
296 ne = nat_in_journal(sum, i);
297 node_info_from_raw_nat(ni, &ne);
298 }
299 mutex_unlock(&curseg->curseg_mutex);
300 if (i >= 0)
301 goto cache;
302
303 /* Fill node_info from nat page */
304 page = get_current_nat_page(sbi, start_nid);
305 nat_blk = (struct f2fs_nat_block *)page_address(page);
306 ne = nat_blk->entries[nid - start_nid];
307 node_info_from_raw_nat(ni, &ne);
308 f2fs_put_page(page, 1);
309cache:
310 /* cache nat entry */
311 cache_nat_entry(NM_I(sbi), nid, &ne);
312}
313
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900314/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900315 * The maximum depth is four.
316 * Offset[0] will have raw inode offset.
317 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900318static int get_node_path(struct f2fs_inode_info *fi, long block,
319 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900320{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900321 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900322 const long direct_blks = ADDRS_PER_BLOCK;
323 const long dptrs_per_blk = NIDS_PER_BLOCK;
324 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
325 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
326 int n = 0;
327 int level = 0;
328
329 noffset[0] = 0;
330
331 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900332 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900333 goto got;
334 }
335 block -= direct_index;
336 if (block < direct_blks) {
337 offset[n++] = NODE_DIR1_BLOCK;
338 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900339 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900340 level = 1;
341 goto got;
342 }
343 block -= direct_blks;
344 if (block < direct_blks) {
345 offset[n++] = NODE_DIR2_BLOCK;
346 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900347 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900348 level = 1;
349 goto got;
350 }
351 block -= direct_blks;
352 if (block < indirect_blks) {
353 offset[n++] = NODE_IND1_BLOCK;
354 noffset[n] = 3;
355 offset[n++] = block / direct_blks;
356 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900357 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900358 level = 2;
359 goto got;
360 }
361 block -= indirect_blks;
362 if (block < indirect_blks) {
363 offset[n++] = NODE_IND2_BLOCK;
364 noffset[n] = 4 + dptrs_per_blk;
365 offset[n++] = block / direct_blks;
366 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900367 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900368 level = 2;
369 goto got;
370 }
371 block -= indirect_blks;
372 if (block < dindirect_blks) {
373 offset[n++] = NODE_DIND_BLOCK;
374 noffset[n] = 5 + (dptrs_per_blk * 2);
375 offset[n++] = block / indirect_blks;
376 noffset[n] = 6 + (dptrs_per_blk * 2) +
377 offset[n - 1] * (dptrs_per_blk + 1);
378 offset[n++] = (block / direct_blks) % dptrs_per_blk;
379 noffset[n] = 7 + (dptrs_per_blk * 2) +
380 offset[n - 2] * (dptrs_per_blk + 1) +
381 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900382 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900383 level = 3;
384 goto got;
385 } else {
386 BUG();
387 }
388got:
389 return level;
390}
391
392/*
393 * Caller should call f2fs_put_dnode(dn).
Jaegeuk Kim39936832012-11-22 16:21:29 +0900394 * Also, it should grab and release a mutex by calling mutex_lock_op() and
395 * mutex_unlock_op() only if ro is not set RDONLY_NODE.
396 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900397 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900398int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900399{
400 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
401 struct page *npage[4];
402 struct page *parent;
403 int offset[4];
404 unsigned int noffset[4];
405 nid_t nids[4];
406 int level, i;
407 int err = 0;
408
Jaegeuk Kimde936532013-08-12 21:08:03 +0900409 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410
411 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900412 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900413
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900414 if (!npage[0]) {
415 npage[0] = get_node_page(sbi, nids[0]);
416 if (IS_ERR(npage[0]))
417 return PTR_ERR(npage[0]);
418 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900419 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900420 if (level != 0)
421 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900422 dn->inode_page = npage[0];
423 dn->inode_page_locked = true;
424
425 /* get indirect or direct nodes */
426 for (i = 1; i <= level; i++) {
427 bool done = false;
428
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900429 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900430 /* alloc new node */
431 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900432 err = -ENOSPC;
433 goto release_pages;
434 }
435
436 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900437 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900438 if (IS_ERR(npage[i])) {
439 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900440 err = PTR_ERR(npage[i]);
441 goto release_pages;
442 }
443
444 set_nid(parent, offset[i - 1], nids[i], i == 1);
445 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900446 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900447 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900448 npage[i] = get_node_page_ra(parent, offset[i - 1]);
449 if (IS_ERR(npage[i])) {
450 err = PTR_ERR(npage[i]);
451 goto release_pages;
452 }
453 done = true;
454 }
455 if (i == 1) {
456 dn->inode_page_locked = false;
457 unlock_page(parent);
458 } else {
459 f2fs_put_page(parent, 1);
460 }
461
462 if (!done) {
463 npage[i] = get_node_page(sbi, nids[i]);
464 if (IS_ERR(npage[i])) {
465 err = PTR_ERR(npage[i]);
466 f2fs_put_page(npage[0], 0);
467 goto release_out;
468 }
469 }
470 if (i < level) {
471 parent = npage[i];
472 nids[i + 1] = get_nid(parent, offset[i], false);
473 }
474 }
475 dn->nid = nids[level];
476 dn->ofs_in_node = offset[level];
477 dn->node_page = npage[level];
478 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
479 return 0;
480
481release_pages:
482 f2fs_put_page(parent, 1);
483 if (i > 1)
484 f2fs_put_page(npage[0], 0);
485release_out:
486 dn->inode_page = NULL;
487 dn->node_page = NULL;
488 return err;
489}
490
491static void truncate_node(struct dnode_of_data *dn)
492{
493 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
494 struct node_info ni;
495
496 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900497 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900498 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900499 goto invalidate;
500 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900501 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900502
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900503 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900504 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800505 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900506 set_node_addr(sbi, &ni, NULL_ADDR);
507
508 if (dn->nid == dn->inode->i_ino) {
509 remove_orphan_inode(sbi, dn->nid);
510 dec_valid_inode_count(sbi);
511 } else {
512 sync_inode_page(dn);
513 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900514invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515 clear_node_page_dirty(dn->node_page);
516 F2FS_SET_SB_DIRT(sbi);
517
518 f2fs_put_page(dn->node_page, 1);
519 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900520 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521}
522
523static int truncate_dnode(struct dnode_of_data *dn)
524{
525 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
526 struct page *page;
527
528 if (dn->nid == 0)
529 return 1;
530
531 /* get direct node */
532 page = get_node_page(sbi, dn->nid);
533 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
534 return 1;
535 else if (IS_ERR(page))
536 return PTR_ERR(page);
537
538 /* Make dnode_of_data for parameter */
539 dn->node_page = page;
540 dn->ofs_in_node = 0;
541 truncate_data_blocks(dn);
542 truncate_node(dn);
543 return 1;
544}
545
546static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
547 int ofs, int depth)
548{
549 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
550 struct dnode_of_data rdn = *dn;
551 struct page *page;
552 struct f2fs_node *rn;
553 nid_t child_nid;
554 unsigned int child_nofs;
555 int freed = 0;
556 int i, ret;
557
558 if (dn->nid == 0)
559 return NIDS_PER_BLOCK + 1;
560
Namjae Jeon51dd6242013-04-20 01:28:52 +0900561 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
562
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900563 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900564 if (IS_ERR(page)) {
565 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900566 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900567 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900568
Gu Zheng45590712013-07-15 17:57:38 +0800569 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900570 if (depth < 3) {
571 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
572 child_nid = le32_to_cpu(rn->in.nid[i]);
573 if (child_nid == 0)
574 continue;
575 rdn.nid = child_nid;
576 ret = truncate_dnode(&rdn);
577 if (ret < 0)
578 goto out_err;
579 set_nid(page, i, 0, false);
580 }
581 } else {
582 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
583 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
584 child_nid = le32_to_cpu(rn->in.nid[i]);
585 if (child_nid == 0) {
586 child_nofs += NIDS_PER_BLOCK + 1;
587 continue;
588 }
589 rdn.nid = child_nid;
590 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
591 if (ret == (NIDS_PER_BLOCK + 1)) {
592 set_nid(page, i, 0, false);
593 child_nofs += ret;
594 } else if (ret < 0 && ret != -ENOENT) {
595 goto out_err;
596 }
597 }
598 freed = child_nofs;
599 }
600
601 if (!ofs) {
602 /* remove current indirect node */
603 dn->node_page = page;
604 truncate_node(dn);
605 freed++;
606 } else {
607 f2fs_put_page(page, 1);
608 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900609 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900610 return freed;
611
612out_err:
613 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900614 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900615 return ret;
616}
617
618static int truncate_partial_nodes(struct dnode_of_data *dn,
619 struct f2fs_inode *ri, int *offset, int depth)
620{
621 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
622 struct page *pages[2];
623 nid_t nid[3];
624 nid_t child_nid;
625 int err = 0;
626 int i;
627 int idx = depth - 2;
628
629 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
630 if (!nid[0])
631 return 0;
632
633 /* get indirect nodes in the path */
634 for (i = 0; i < depth - 1; i++) {
635 /* refernece count'll be increased */
636 pages[i] = get_node_page(sbi, nid[i]);
637 if (IS_ERR(pages[i])) {
638 depth = i + 1;
639 err = PTR_ERR(pages[i]);
640 goto fail;
641 }
642 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
643 }
644
645 /* free direct nodes linked to a partial indirect node */
646 for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) {
647 child_nid = get_nid(pages[idx], i, false);
648 if (!child_nid)
649 continue;
650 dn->nid = child_nid;
651 err = truncate_dnode(dn);
652 if (err < 0)
653 goto fail;
654 set_nid(pages[idx], i, 0, false);
655 }
656
657 if (offset[depth - 1] == 0) {
658 dn->node_page = pages[idx];
659 dn->nid = nid[idx];
660 truncate_node(dn);
661 } else {
662 f2fs_put_page(pages[idx], 1);
663 }
664 offset[idx]++;
665 offset[depth - 1] = 0;
666fail:
667 for (i = depth - 3; i >= 0; i--)
668 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900669
670 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
671
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900672 return err;
673}
674
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900675/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900676 * All the block addresses of data and nodes should be nullified.
677 */
678int truncate_inode_blocks(struct inode *inode, pgoff_t from)
679{
680 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900681 struct address_space *node_mapping = sbi->node_inode->i_mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900682 int err = 0, cont = 1;
683 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900684 unsigned int nofs = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900685 struct f2fs_node *rn;
686 struct dnode_of_data dn;
687 struct page *page;
688
Namjae Jeon51dd6242013-04-20 01:28:52 +0900689 trace_f2fs_truncate_inode_blocks_enter(inode, from);
690
Jaegeuk Kimde936532013-08-12 21:08:03 +0900691 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900692restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900693 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900694 if (IS_ERR(page)) {
695 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900696 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900697 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900698
699 set_new_dnode(&dn, inode, page, NULL, 0);
700 unlock_page(page);
701
Gu Zheng45590712013-07-15 17:57:38 +0800702 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900703 switch (level) {
704 case 0:
705 case 1:
706 nofs = noffset[1];
707 break;
708 case 2:
709 nofs = noffset[1];
710 if (!offset[level - 1])
711 goto skip_partial;
712 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
713 if (err < 0 && err != -ENOENT)
714 goto fail;
715 nofs += 1 + NIDS_PER_BLOCK;
716 break;
717 case 3:
718 nofs = 5 + 2 * NIDS_PER_BLOCK;
719 if (!offset[level - 1])
720 goto skip_partial;
721 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
722 if (err < 0 && err != -ENOENT)
723 goto fail;
724 break;
725 default:
726 BUG();
727 }
728
729skip_partial:
730 while (cont) {
731 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]);
732 switch (offset[0]) {
733 case NODE_DIR1_BLOCK:
734 case NODE_DIR2_BLOCK:
735 err = truncate_dnode(&dn);
736 break;
737
738 case NODE_IND1_BLOCK:
739 case NODE_IND2_BLOCK:
740 err = truncate_nodes(&dn, nofs, offset[1], 2);
741 break;
742
743 case NODE_DIND_BLOCK:
744 err = truncate_nodes(&dn, nofs, offset[1], 3);
745 cont = 0;
746 break;
747
748 default:
749 BUG();
750 }
751 if (err < 0 && err != -ENOENT)
752 goto fail;
753 if (offset[1] == 0 &&
754 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) {
755 lock_page(page);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900756 if (page->mapping != node_mapping) {
757 f2fs_put_page(page, 1);
758 goto restart;
759 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900760 wait_on_page_writeback(page);
761 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
762 set_page_dirty(page);
763 unlock_page(page);
764 }
765 offset[1] = 0;
766 offset[0]++;
767 nofs += err;
768 }
769fail:
770 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900771 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772 return err > 0 ? 0 : err;
773}
774
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900775int truncate_xattr_node(struct inode *inode, struct page *page)
776{
777 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
778 nid_t nid = F2FS_I(inode)->i_xattr_nid;
779 struct dnode_of_data dn;
780 struct page *npage;
781
782 if (!nid)
783 return 0;
784
785 npage = get_node_page(sbi, nid);
786 if (IS_ERR(npage))
787 return PTR_ERR(npage);
788
789 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900790
791 /* need to do checkpoint during fsync */
792 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
793
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900794 set_new_dnode(&dn, inode, page, npage, nid);
795
796 if (page)
797 dn.inode_page_locked = 1;
798 truncate_node(&dn);
799 return 0;
800}
801
Jaegeuk Kim39936832012-11-22 16:21:29 +0900802/*
803 * Caller should grab and release a mutex by calling mutex_lock_op() and
804 * mutex_unlock_op().
805 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800806void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900807{
808 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
809 struct page *page;
810 nid_t ino = inode->i_ino;
811 struct dnode_of_data dn;
812
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900813 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900814 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800815 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900816
Gu Zheng58e674d2013-11-19 18:03:18 +0800817 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900818 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800819 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900820 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900821 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900822 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900823 set_new_dnode(&dn, inode, page, page, ino);
824 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900825}
826
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900827struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900828{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900829 struct dnode_of_data dn;
830
831 /* allocate inode page for new inode */
832 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900833
834 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900835 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900836}
837
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900838struct page *new_node_page(struct dnode_of_data *dn,
839 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900840{
841 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
842 struct address_space *mapping = sbi->node_inode->i_mapping;
843 struct node_info old_ni, new_ni;
844 struct page *page;
845 int err;
846
847 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
848 return ERR_PTR(-EPERM);
849
850 page = grab_cache_page(mapping, dn->nid);
851 if (!page)
852 return ERR_PTR(-ENOMEM);
853
Gu Zhengef86d702013-11-19 18:03:38 +0800854 if (!inc_valid_node_count(sbi, dn->inode)) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900855 err = -ENOSPC;
856 goto fail;
857 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900858
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900859 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900860
861 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900862 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900863 new_ni = old_ni;
864 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900865 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900866
867 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900868 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900869 SetPageUptodate(page);
870 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900871
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900872 if (ofs == XATTR_NODE_OFFSET)
873 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
874
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900875 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900876 if (ipage)
877 update_inode(dn->inode, ipage);
878 else
879 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900880 if (ofs == 0)
881 inc_valid_inode_count(sbi);
882
883 return page;
884
885fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900886 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900887 f2fs_put_page(page, 1);
888 return ERR_PTR(err);
889}
890
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900891/*
892 * Caller should do after getting the following values.
893 * 0: f2fs_put_page(page, 0)
894 * LOCKED_PAGE: f2fs_put_page(page, 1)
895 * error: nothing
896 */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900897static int read_node_page(struct page *page, int type)
898{
899 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
900 struct node_info ni;
901
902 get_node_info(sbi, page->index, &ni);
903
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900904 if (ni.blk_addr == NULL_ADDR) {
905 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900907 }
908
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900909 if (PageUptodate(page))
910 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900911
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900912 return f2fs_readpage(sbi, page, ni.blk_addr, type);
913}
914
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900915/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900916 * Readahead a node page
917 */
918void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
919{
920 struct address_space *mapping = sbi->node_inode->i_mapping;
921 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900922 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923
924 apage = find_get_page(mapping, nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900925 if (apage && PageUptodate(apage)) {
926 f2fs_put_page(apage, 0);
927 return;
928 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900929 f2fs_put_page(apage, 0);
930
931 apage = grab_cache_page(mapping, nid);
932 if (!apage)
933 return;
934
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900935 err = read_node_page(apage, READA);
936 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900937 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900938 else if (err == LOCKED_PAGE)
939 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900940}
941
942struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
943{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900944 struct address_space *mapping = sbi->node_inode->i_mapping;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900945 struct page *page;
946 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900947repeat:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900948 page = grab_cache_page(mapping, nid);
949 if (!page)
950 return ERR_PTR(-ENOMEM);
951
952 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900953 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900954 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900955 else if (err == LOCKED_PAGE)
956 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900957
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900958 lock_page(page);
959 if (!PageUptodate(page)) {
960 f2fs_put_page(page, 1);
961 return ERR_PTR(-EIO);
962 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900963 if (page->mapping != mapping) {
964 f2fs_put_page(page, 1);
965 goto repeat;
966 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900967got_it:
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900968 f2fs_bug_on(nid != nid_of_node(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900969 mark_page_accessed(page);
970 return page;
971}
972
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900973/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900974 * Return a locked page for the desired node page.
975 * And, readahead MAX_RA_NODE number of node pages.
976 */
977struct page *get_node_page_ra(struct page *parent, int start)
978{
979 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
980 struct address_space *mapping = sbi->node_inode->i_mapping;
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900981 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900982 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900983 int err, i, end;
984 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985
986 /* First, try getting the desired direct node. */
987 nid = get_nid(parent, start, false);
988 if (!nid)
989 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900990repeat:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900991 page = grab_cache_page(mapping, nid);
992 if (!page)
993 return ERR_PTR(-ENOMEM);
994
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900995 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900996 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900997 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900998 else if (err == LOCKED_PAGE)
999 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001000
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001001 blk_start_plug(&plug);
1002
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001003 /* Then, try readahead for siblings of the desired node */
1004 end = start + MAX_RA_NODE;
1005 end = min(end, NIDS_PER_BLOCK);
1006 for (i = start + 1; i < end; i++) {
1007 nid = get_nid(parent, i, false);
1008 if (!nid)
1009 continue;
1010 ra_node_page(sbi, nid);
1011 }
1012
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001013 blk_finish_plug(&plug);
1014
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001015 lock_page(page);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001016 if (page->mapping != mapping) {
1017 f2fs_put_page(page, 1);
1018 goto repeat;
1019 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001020page_hit:
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001021 if (!PageUptodate(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001022 f2fs_put_page(page, 1);
1023 return ERR_PTR(-EIO);
1024 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001025 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001026 return page;
1027}
1028
1029void sync_inode_page(struct dnode_of_data *dn)
1030{
1031 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1032 update_inode(dn->inode, dn->node_page);
1033 } else if (dn->inode_page) {
1034 if (!dn->inode_page_locked)
1035 lock_page(dn->inode_page);
1036 update_inode(dn->inode, dn->inode_page);
1037 if (!dn->inode_page_locked)
1038 unlock_page(dn->inode_page);
1039 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001040 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001041 }
1042}
1043
1044int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1045 struct writeback_control *wbc)
1046{
1047 struct address_space *mapping = sbi->node_inode->i_mapping;
1048 pgoff_t index, end;
1049 struct pagevec pvec;
1050 int step = ino ? 2 : 0;
1051 int nwritten = 0, wrote = 0;
1052
1053 pagevec_init(&pvec, 0);
1054
1055next_step:
1056 index = 0;
1057 end = LONG_MAX;
1058
1059 while (index <= end) {
1060 int i, nr_pages;
1061 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1062 PAGECACHE_TAG_DIRTY,
1063 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1064 if (nr_pages == 0)
1065 break;
1066
1067 for (i = 0; i < nr_pages; i++) {
1068 struct page *page = pvec.pages[i];
1069
1070 /*
1071 * flushing sequence with step:
1072 * 0. indirect nodes
1073 * 1. dentry dnodes
1074 * 2. file dnodes
1075 */
1076 if (step == 0 && IS_DNODE(page))
1077 continue;
1078 if (step == 1 && (!IS_DNODE(page) ||
1079 is_cold_node(page)))
1080 continue;
1081 if (step == 2 && (!IS_DNODE(page) ||
1082 !is_cold_node(page)))
1083 continue;
1084
1085 /*
1086 * If an fsync mode,
1087 * we should not skip writing node pages.
1088 */
1089 if (ino && ino_of_node(page) == ino)
1090 lock_page(page);
1091 else if (!trylock_page(page))
1092 continue;
1093
1094 if (unlikely(page->mapping != mapping)) {
1095continue_unlock:
1096 unlock_page(page);
1097 continue;
1098 }
1099 if (ino && ino_of_node(page) != ino)
1100 goto continue_unlock;
1101
1102 if (!PageDirty(page)) {
1103 /* someone wrote it for us */
1104 goto continue_unlock;
1105 }
1106
1107 if (!clear_page_dirty_for_io(page))
1108 goto continue_unlock;
1109
1110 /* called by fsync() */
1111 if (ino && IS_DNODE(page)) {
1112 int mark = !is_checkpointed_node(sbi, ino);
1113 set_fsync_mark(page, 1);
1114 if (IS_INODE(page))
1115 set_dentry_mark(page, mark);
1116 nwritten++;
1117 } else {
1118 set_fsync_mark(page, 0);
1119 set_dentry_mark(page, 0);
1120 }
1121 mapping->a_ops->writepage(page, wbc);
1122 wrote++;
1123
1124 if (--wbc->nr_to_write == 0)
1125 break;
1126 }
1127 pagevec_release(&pvec);
1128 cond_resched();
1129
1130 if (wbc->nr_to_write == 0) {
1131 step = 2;
1132 break;
1133 }
1134 }
1135
1136 if (step < 2) {
1137 step++;
1138 goto next_step;
1139 }
1140
1141 if (wrote)
1142 f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL);
1143
1144 return nwritten;
1145}
1146
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001147int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1148{
1149 struct address_space *mapping = sbi->node_inode->i_mapping;
1150 pgoff_t index = 0, end = LONG_MAX;
1151 struct pagevec pvec;
1152 int nr_pages;
1153 int ret2 = 0, ret = 0;
1154
1155 pagevec_init(&pvec, 0);
1156 while ((index <= end) &&
1157 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1158 PAGECACHE_TAG_WRITEBACK,
1159 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1)) != 0) {
1160 unsigned i;
1161
1162 for (i = 0; i < nr_pages; i++) {
1163 struct page *page = pvec.pages[i];
1164
1165 /* until radix tree lookup accepts end_index */
1166 if (page->index > end)
1167 continue;
1168
Chao Yu4bf08ff2013-11-04 10:28:33 +08001169 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001170 wait_on_page_writeback(page);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001171 if (TestClearPageError(page))
1172 ret = -EIO;
1173 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001174 }
1175 pagevec_release(&pvec);
1176 cond_resched();
1177 }
1178
1179 if (test_and_clear_bit(AS_ENOSPC, &mapping->flags))
1180 ret2 = -ENOSPC;
1181 if (test_and_clear_bit(AS_EIO, &mapping->flags))
1182 ret2 = -EIO;
1183 if (!ret)
1184 ret = ret2;
1185 return ret;
1186}
1187
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001188static int f2fs_write_node_page(struct page *page,
1189 struct writeback_control *wbc)
1190{
1191 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1192 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001193 block_t new_addr;
1194 struct node_info ni;
1195
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001196 if (sbi->por_doing)
1197 goto redirty_out;
1198
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001199 wait_on_page_writeback(page);
1200
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001201 /* get old block addr of this node page */
1202 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001203 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001204
1205 get_node_info(sbi, nid, &ni);
1206
1207 /* This page is already truncated */
Jaegeuk Kim39936832012-11-22 16:21:29 +09001208 if (ni.blk_addr == NULL_ADDR) {
1209 dec_page_count(sbi, F2FS_DIRTY_NODES);
1210 unlock_page(page);
1211 return 0;
1212 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001213
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001214 if (wbc->for_reclaim)
1215 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001216
Jaegeuk Kim39936832012-11-22 16:21:29 +09001217 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001218 set_page_writeback(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001219 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1220 set_node_addr(sbi, &ni, new_addr);
1221 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001222 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001223 unlock_page(page);
1224 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001225
1226redirty_out:
1227 dec_page_count(sbi, F2FS_DIRTY_NODES);
1228 wbc->pages_skipped++;
1229 set_page_dirty(page);
1230 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001231}
1232
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001233/*
1234 * It is very important to gather dirty pages and write at once, so that we can
1235 * submit a big bio without interfering other data writes.
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001236 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001237 */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001238#define COLLECT_DIRTY_NODES 1536
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001239static int f2fs_write_node_pages(struct address_space *mapping,
1240 struct writeback_control *wbc)
1241{
1242 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001243 long nr_to_write = wbc->nr_to_write;
1244
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001245 /* balancing f2fs's metadata in background */
1246 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001247
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001248 /* collect a number of dirty node pages and write together */
1249 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1250 return 0;
1251
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001252 /* if mounting is failed, skip writing node pages */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001253 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001254 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001255 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1256 wbc->nr_to_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001257 return 0;
1258}
1259
1260static int f2fs_set_node_page_dirty(struct page *page)
1261{
1262 struct address_space *mapping = page->mapping;
1263 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1264
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001265 trace_f2fs_set_page_dirty(page, NODE);
1266
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001267 SetPageUptodate(page);
1268 if (!PageDirty(page)) {
1269 __set_page_dirty_nobuffers(page);
1270 inc_page_count(sbi, F2FS_DIRTY_NODES);
1271 SetPagePrivate(page);
1272 return 1;
1273 }
1274 return 0;
1275}
1276
Lukas Czernerd47992f2013-05-21 23:17:23 -04001277static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1278 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001279{
1280 struct inode *inode = page->mapping->host;
1281 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1282 if (PageDirty(page))
1283 dec_page_count(sbi, F2FS_DIRTY_NODES);
1284 ClearPagePrivate(page);
1285}
1286
1287static int f2fs_release_node_page(struct page *page, gfp_t wait)
1288{
1289 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001290 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291}
1292
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001293/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294 * Structure of the f2fs node operations
1295 */
1296const struct address_space_operations f2fs_node_aops = {
1297 .writepage = f2fs_write_node_page,
1298 .writepages = f2fs_write_node_pages,
1299 .set_page_dirty = f2fs_set_node_page_dirty,
1300 .invalidatepage = f2fs_invalidate_node_page,
1301 .releasepage = f2fs_release_node_page,
1302};
1303
1304static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1305{
1306 struct list_head *this;
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001307 struct free_nid *i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001308 list_for_each(this, head) {
1309 i = list_entry(this, struct free_nid, list);
1310 if (i->nid == n)
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001311 return i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001312 }
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001313 return NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001314}
1315
1316static void __del_from_free_nid_list(struct free_nid *i)
1317{
1318 list_del(&i->list);
1319 kmem_cache_free(free_nid_slab, i);
1320}
1321
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001322static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323{
1324 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001325 struct nat_entry *ne;
1326 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327
1328 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
Haicheng Li23d38842013-05-06 23:15:43 +08001329 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001330
1331 /* 0 nid should not be used */
1332 if (nid == 0)
1333 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001334
Gu Zheng7bd59382013-10-22 14:52:26 +08001335 if (build) {
1336 /* do not add allocated nids */
1337 read_lock(&nm_i->nat_tree_lock);
1338 ne = __lookup_nat_cache(nm_i, nid);
1339 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1340 allocated = true;
1341 read_unlock(&nm_i->nat_tree_lock);
1342 if (allocated)
1343 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001345
1346 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001347 i->nid = nid;
1348 i->state = NID_NEW;
1349
1350 spin_lock(&nm_i->free_nid_list_lock);
1351 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1352 spin_unlock(&nm_i->free_nid_list_lock);
1353 kmem_cache_free(free_nid_slab, i);
1354 return 0;
1355 }
1356 list_add_tail(&i->list, &nm_i->free_nid_list);
1357 nm_i->fcnt++;
1358 spin_unlock(&nm_i->free_nid_list_lock);
1359 return 1;
1360}
1361
1362static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1363{
1364 struct free_nid *i;
1365 spin_lock(&nm_i->free_nid_list_lock);
1366 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1367 if (i && i->state == NID_NEW) {
1368 __del_from_free_nid_list(i);
1369 nm_i->fcnt--;
1370 }
1371 spin_unlock(&nm_i->free_nid_list_lock);
1372}
1373
Haicheng Li87609522013-05-06 23:15:42 +08001374static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375 struct page *nat_page, nid_t start_nid)
1376{
1377 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1378 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001379 int i;
1380
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 i = start_nid % NAT_ENTRY_PER_BLOCK;
1382
1383 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001384
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001385 if (start_nid >= nm_i->max_nid)
1386 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001387
1388 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001389 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001390 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001391 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001392 break;
1393 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001394 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001395}
1396
1397static void build_free_nids(struct f2fs_sb_info *sbi)
1398{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001399 struct f2fs_nm_info *nm_i = NM_I(sbi);
1400 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1401 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001402 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001403 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001404
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001405 /* Enough entries */
1406 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1407 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001408
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001409 /* readahead nat pages to be scanned */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001410 ra_nat_pages(sbi, nid);
1411
1412 while (1) {
1413 struct page *page = get_current_nat_page(sbi, nid);
1414
Haicheng Li87609522013-05-06 23:15:42 +08001415 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001416 f2fs_put_page(page, 1);
1417
1418 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001419 if (nid >= nm_i->max_nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001421
1422 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001423 break;
1424 }
1425
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001426 /* go to the next free nat pages to find free nids abundantly */
1427 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001428
1429 /* find free nids from current sum_pages */
1430 mutex_lock(&curseg->curseg_mutex);
1431 for (i = 0; i < nats_in_cursum(sum); i++) {
1432 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1433 nid = le32_to_cpu(nid_in_journal(sum, i));
1434 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001435 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001436 else
1437 remove_free_nid(nm_i, nid);
1438 }
1439 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001440}
1441
1442/*
1443 * If this function returns success, caller can obtain a new nid
1444 * from second parameter of this function.
1445 * The returned nid could be used ino as well as nid when inode is created.
1446 */
1447bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1448{
1449 struct f2fs_nm_info *nm_i = NM_I(sbi);
1450 struct free_nid *i = NULL;
1451 struct list_head *this;
1452retry:
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001453 if (sbi->total_valid_node_count + 1 >= nm_i->max_nid)
1454 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001455
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001456 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001457
1458 /* We should not use stale free nids created by build_free_nids */
1459 if (nm_i->fcnt && !sbi->on_build_free_nids) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001460 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001461 list_for_each(this, &nm_i->free_nid_list) {
1462 i = list_entry(this, struct free_nid, list);
1463 if (i->state == NID_NEW)
1464 break;
1465 }
1466
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001467 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001468 *nid = i->nid;
1469 i->state = NID_ALLOC;
1470 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001471 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001472 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001473 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001474 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001475
1476 /* Let's scan nat pages and its caches to get free nids */
1477 mutex_lock(&nm_i->build_lock);
Haicheng Liaabe5132013-10-23 12:39:32 +08001478 sbi->on_build_free_nids = true;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001479 build_free_nids(sbi);
Haicheng Liaabe5132013-10-23 12:39:32 +08001480 sbi->on_build_free_nids = false;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001481 mutex_unlock(&nm_i->build_lock);
1482 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001483}
1484
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001485/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001486 * alloc_nid() should be called prior to this function.
1487 */
1488void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1489{
1490 struct f2fs_nm_info *nm_i = NM_I(sbi);
1491 struct free_nid *i;
1492
1493 spin_lock(&nm_i->free_nid_list_lock);
1494 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001495 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001496 __del_from_free_nid_list(i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001497 spin_unlock(&nm_i->free_nid_list_lock);
1498}
1499
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001500/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501 * alloc_nid() should be called prior to this function.
1502 */
1503void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1504{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001505 struct f2fs_nm_info *nm_i = NM_I(sbi);
1506 struct free_nid *i;
1507
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001508 if (!nid)
1509 return;
1510
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001511 spin_lock(&nm_i->free_nid_list_lock);
1512 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001513 f2fs_bug_on(!i || i->state != NID_ALLOC);
Haicheng Li95630cb2013-05-06 23:15:41 +08001514 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1515 __del_from_free_nid_list(i);
1516 } else {
1517 i->state = NID_NEW;
1518 nm_i->fcnt++;
1519 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001520 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001521}
1522
1523void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1524 struct f2fs_summary *sum, struct node_info *ni,
1525 block_t new_blkaddr)
1526{
1527 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1528 set_node_addr(sbi, ni, new_blkaddr);
1529 clear_node_page_dirty(page);
1530}
1531
1532int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1533{
1534 struct address_space *mapping = sbi->node_inode->i_mapping;
1535 struct f2fs_node *src, *dst;
1536 nid_t ino = ino_of_node(page);
1537 struct node_info old_ni, new_ni;
1538 struct page *ipage;
1539
1540 ipage = grab_cache_page(mapping, ino);
1541 if (!ipage)
1542 return -ENOMEM;
1543
1544 /* Should not use this inode from free nid list */
1545 remove_free_nid(NM_I(sbi), ino);
1546
1547 get_node_info(sbi, ino, &old_ni);
1548 SetPageUptodate(ipage);
1549 fill_node_footer(ipage, ino, ino, 0, true);
1550
Gu Zheng45590712013-07-15 17:57:38 +08001551 src = F2FS_NODE(page);
1552 dst = F2FS_NODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001553
1554 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1555 dst->i.i_size = 0;
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001556 dst->i.i_blocks = cpu_to_le64(1);
1557 dst->i.i_links = cpu_to_le32(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001558 dst->i.i_xattr_nid = 0;
1559
1560 new_ni = old_ni;
1561 new_ni.ino = ino;
1562
Gu Zhengef86d702013-11-19 18:03:38 +08001563 if (!inc_valid_node_count(sbi, NULL))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001564 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001565 set_node_addr(sbi, &new_ni, NEW_ADDR);
1566 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001567 f2fs_put_page(ipage, 1);
1568 return 0;
1569}
1570
1571int restore_node_summary(struct f2fs_sb_info *sbi,
1572 unsigned int segno, struct f2fs_summary_block *sum)
1573{
1574 struct f2fs_node *rn;
1575 struct f2fs_summary *sum_entry;
1576 struct page *page;
1577 block_t addr;
1578 int i, last_offset;
1579
1580 /* alloc temporal page for read node */
1581 page = alloc_page(GFP_NOFS | __GFP_ZERO);
Dan Carpentere27dae42013-08-15 08:54:56 +03001582 if (!page)
1583 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001584 lock_page(page);
1585
1586 /* scan the node segment */
1587 last_offset = sbi->blocks_per_seg;
1588 addr = START_BLOCK(sbi, segno);
1589 sum_entry = &sum->entries[0];
1590
1591 for (i = 0; i < last_offset; i++, sum_entry++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001592 /*
1593 * In order to read next node page,
1594 * we must clear PageUptodate flag.
1595 */
1596 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001597
1598 if (f2fs_readpage(sbi, page, addr, READ_SYNC))
1599 goto out;
1600
1601 lock_page(page);
Gu Zheng45590712013-07-15 17:57:38 +08001602 rn = F2FS_NODE(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001603 sum_entry->nid = rn->footer.nid;
1604 sum_entry->version = 0;
1605 sum_entry->ofs_in_node = 0;
1606 addr++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001607 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001608 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001609out:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001610 __free_pages(page, 0);
1611 return 0;
1612}
1613
1614static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1615{
1616 struct f2fs_nm_info *nm_i = NM_I(sbi);
1617 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1618 struct f2fs_summary_block *sum = curseg->sum_blk;
1619 int i;
1620
1621 mutex_lock(&curseg->curseg_mutex);
1622
1623 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1624 mutex_unlock(&curseg->curseg_mutex);
1625 return false;
1626 }
1627
1628 for (i = 0; i < nats_in_cursum(sum); i++) {
1629 struct nat_entry *ne;
1630 struct f2fs_nat_entry raw_ne;
1631 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1632
1633 raw_ne = nat_in_journal(sum, i);
1634retry:
1635 write_lock(&nm_i->nat_tree_lock);
1636 ne = __lookup_nat_cache(nm_i, nid);
1637 if (ne) {
1638 __set_nat_cache_dirty(nm_i, ne);
1639 write_unlock(&nm_i->nat_tree_lock);
1640 continue;
1641 }
1642 ne = grab_nat_entry(nm_i, nid);
1643 if (!ne) {
1644 write_unlock(&nm_i->nat_tree_lock);
1645 goto retry;
1646 }
1647 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1648 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1649 nat_set_version(ne, raw_ne.version);
1650 __set_nat_cache_dirty(nm_i, ne);
1651 write_unlock(&nm_i->nat_tree_lock);
1652 }
1653 update_nats_in_cursum(sum, -i);
1654 mutex_unlock(&curseg->curseg_mutex);
1655 return true;
1656}
1657
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001658/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001659 * This function is called during the checkpointing process.
1660 */
1661void flush_nat_entries(struct f2fs_sb_info *sbi)
1662{
1663 struct f2fs_nm_info *nm_i = NM_I(sbi);
1664 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1665 struct f2fs_summary_block *sum = curseg->sum_blk;
1666 struct list_head *cur, *n;
1667 struct page *page = NULL;
1668 struct f2fs_nat_block *nat_blk = NULL;
1669 nid_t start_nid = 0, end_nid = 0;
1670 bool flushed;
1671
1672 flushed = flush_nats_in_journal(sbi);
1673
1674 if (!flushed)
1675 mutex_lock(&curseg->curseg_mutex);
1676
1677 /* 1) flush dirty nat caches */
1678 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1679 struct nat_entry *ne;
1680 nid_t nid;
1681 struct f2fs_nat_entry raw_ne;
1682 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001683 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001684
1685 ne = list_entry(cur, struct nat_entry, list);
1686 nid = nat_get_nid(ne);
1687
1688 if (nat_get_blkaddr(ne) == NEW_ADDR)
1689 continue;
1690 if (flushed)
1691 goto to_nat_page;
1692
1693 /* if there is room for nat enries in curseg->sumpage */
1694 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1695 if (offset >= 0) {
1696 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001697 goto flush_now;
1698 }
1699to_nat_page:
1700 if (!page || (start_nid > nid || nid > end_nid)) {
1701 if (page) {
1702 f2fs_put_page(page, 1);
1703 page = NULL;
1704 }
1705 start_nid = START_NID(nid);
1706 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1707
1708 /*
1709 * get nat block with dirty flag, increased reference
1710 * count, mapped and lock
1711 */
1712 page = get_next_nat_page(sbi, start_nid);
1713 nat_blk = page_address(page);
1714 }
1715
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001716 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001717 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001718flush_now:
1719 new_blkaddr = nat_get_blkaddr(ne);
1720
1721 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1722 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1723 raw_ne.version = nat_get_version(ne);
1724
1725 if (offset < 0) {
1726 nat_blk->entries[nid - start_nid] = raw_ne;
1727 } else {
1728 nat_in_journal(sum, offset) = raw_ne;
1729 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1730 }
1731
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001732 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001733 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001734 write_lock(&nm_i->nat_tree_lock);
1735 __del_from_nat_cache(nm_i, ne);
1736 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001737 } else {
1738 write_lock(&nm_i->nat_tree_lock);
1739 __clear_nat_cache_dirty(nm_i, ne);
1740 ne->checkpointed = true;
1741 write_unlock(&nm_i->nat_tree_lock);
1742 }
1743 }
1744 if (!flushed)
1745 mutex_unlock(&curseg->curseg_mutex);
1746 f2fs_put_page(page, 1);
1747
1748 /* 2) shrink nat caches if necessary */
1749 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1750}
1751
1752static int init_node_manager(struct f2fs_sb_info *sbi)
1753{
1754 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1755 struct f2fs_nm_info *nm_i = NM_I(sbi);
1756 unsigned char *version_bitmap;
1757 unsigned int nat_segs, nat_blocks;
1758
1759 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1760
1761 /* segment_count_nat includes pair segment so divide to 2. */
1762 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1763 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1764 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1765 nm_i->fcnt = 0;
1766 nm_i->nat_cnt = 0;
1767
1768 INIT_LIST_HEAD(&nm_i->free_nid_list);
1769 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1770 INIT_LIST_HEAD(&nm_i->nat_entries);
1771 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1772
1773 mutex_init(&nm_i->build_lock);
1774 spin_lock_init(&nm_i->free_nid_list_lock);
1775 rwlock_init(&nm_i->nat_tree_lock);
1776
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001777 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001778 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001779 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1780 if (!version_bitmap)
1781 return -EFAULT;
1782
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001783 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1784 GFP_KERNEL);
1785 if (!nm_i->nat_bitmap)
1786 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001787 return 0;
1788}
1789
1790int build_node_manager(struct f2fs_sb_info *sbi)
1791{
1792 int err;
1793
1794 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1795 if (!sbi->nm_info)
1796 return -ENOMEM;
1797
1798 err = init_node_manager(sbi);
1799 if (err)
1800 return err;
1801
1802 build_free_nids(sbi);
1803 return 0;
1804}
1805
1806void destroy_node_manager(struct f2fs_sb_info *sbi)
1807{
1808 struct f2fs_nm_info *nm_i = NM_I(sbi);
1809 struct free_nid *i, *next_i;
1810 struct nat_entry *natvec[NATVEC_SIZE];
1811 nid_t nid = 0;
1812 unsigned int found;
1813
1814 if (!nm_i)
1815 return;
1816
1817 /* destroy free nid list */
1818 spin_lock(&nm_i->free_nid_list_lock);
1819 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001820 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001821 __del_from_free_nid_list(i);
1822 nm_i->fcnt--;
1823 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001824 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001825 spin_unlock(&nm_i->free_nid_list_lock);
1826
1827 /* destroy nat cache */
1828 write_lock(&nm_i->nat_tree_lock);
1829 while ((found = __gang_lookup_nat_cache(nm_i,
1830 nid, NATVEC_SIZE, natvec))) {
1831 unsigned idx;
1832 for (idx = 0; idx < found; idx++) {
1833 struct nat_entry *e = natvec[idx];
1834 nid = nat_get_nid(e) + 1;
1835 __del_from_nat_cache(nm_i, e);
1836 }
1837 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001838 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001839 write_unlock(&nm_i->nat_tree_lock);
1840
1841 kfree(nm_i->nat_bitmap);
1842 sbi->nm_info = NULL;
1843 kfree(nm_i);
1844}
1845
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001846int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001847{
1848 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1849 sizeof(struct nat_entry), NULL);
1850 if (!nat_entry_slab)
1851 return -ENOMEM;
1852
1853 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1854 sizeof(struct free_nid), NULL);
1855 if (!free_nid_slab) {
1856 kmem_cache_destroy(nat_entry_slab);
1857 return -ENOMEM;
1858 }
1859 return 0;
1860}
1861
1862void destroy_node_manager_caches(void)
1863{
1864 kmem_cache_destroy(free_nid_slab);
1865 kmem_cache_destroy(nat_entry_slab);
1866}