blob: ef80f791b02b1d3f8fed5c5d2b382d78f5d4c7b4 [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;
207 BUG_ON(ni->blk_addr == NEW_ADDR);
208 } 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;
215 BUG_ON(ni->blk_addr != NULL_ADDR);
216 }
217
218 if (new_blkaddr == NEW_ADDR)
219 e->checkpointed = false;
220
221 /* sanity check */
222 BUG_ON(nat_get_blkaddr(e) != ni->blk_addr);
223 BUG_ON(nat_get_blkaddr(e) == NULL_ADDR &&
224 new_blkaddr == NULL_ADDR);
225 BUG_ON(nat_get_blkaddr(e) == NEW_ADDR &&
226 new_blkaddr == NEW_ADDR);
227 BUG_ON(nat_get_blkaddr(e) != NEW_ADDR &&
228 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
243static int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
244{
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) {
498 BUG_ON(ni.blk_addr != NULL_ADDR);
499 goto invalidate;
500 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900501 BUG_ON(ni.blk_addr == NULL_ADDR);
502
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);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505 dec_valid_node_count(sbi, dn->inode, 1);
506 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 */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900806int remove_inode_page(struct inode *inode)
807{
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;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900812 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900813
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900814 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900815 if (IS_ERR(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900816 return PTR_ERR(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900817
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900818 err = truncate_xattr_node(inode, page);
819 if (err) {
820 f2fs_put_page(page, 1);
821 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900822 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900823
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900824 /* 0 is possible, after f2fs_new_inode() is failed */
825 BUG_ON(inode->i_blocks != 0 && inode->i_blocks != 1);
826 set_new_dnode(&dn, inode, page, page, ino);
827 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900828 return 0;
829}
830
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900831struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900833 struct dnode_of_data dn;
834
835 /* allocate inode page for new inode */
836 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900837
838 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900839 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900840}
841
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900842struct page *new_node_page(struct dnode_of_data *dn,
843 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844{
845 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
846 struct address_space *mapping = sbi->node_inode->i_mapping;
847 struct node_info old_ni, new_ni;
848 struct page *page;
849 int err;
850
851 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
852 return ERR_PTR(-EPERM);
853
854 page = grab_cache_page(mapping, dn->nid);
855 if (!page)
856 return ERR_PTR(-ENOMEM);
857
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900858 if (!inc_valid_node_count(sbi, dn->inode, 1)) {
859 err = -ENOSPC;
860 goto fail;
861 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900862
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900863 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900864
865 /* Reinitialize old_ni with new node page */
866 BUG_ON(old_ni.blk_addr != NULL_ADDR);
867 new_ni = old_ni;
868 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900869 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900870
871 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900872 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900873 SetPageUptodate(page);
874 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900875
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900876 if (ofs == XATTR_NODE_OFFSET)
877 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
878
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900879 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900880 if (ipage)
881 update_inode(dn->inode, ipage);
882 else
883 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900884 if (ofs == 0)
885 inc_valid_inode_count(sbi);
886
887 return page;
888
889fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900890 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900891 f2fs_put_page(page, 1);
892 return ERR_PTR(err);
893}
894
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900895/*
896 * Caller should do after getting the following values.
897 * 0: f2fs_put_page(page, 0)
898 * LOCKED_PAGE: f2fs_put_page(page, 1)
899 * error: nothing
900 */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900901static int read_node_page(struct page *page, int type)
902{
903 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
904 struct node_info ni;
905
906 get_node_info(sbi, page->index, &ni);
907
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900908 if (ni.blk_addr == NULL_ADDR) {
909 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900910 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900911 }
912
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900913 if (PageUptodate(page))
914 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900915
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900916 return f2fs_readpage(sbi, page, ni.blk_addr, type);
917}
918
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900919/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900920 * Readahead a node page
921 */
922void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
923{
924 struct address_space *mapping = sbi->node_inode->i_mapping;
925 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900926 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900927
928 apage = find_get_page(mapping, nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900929 if (apage && PageUptodate(apage)) {
930 f2fs_put_page(apage, 0);
931 return;
932 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900933 f2fs_put_page(apage, 0);
934
935 apage = grab_cache_page(mapping, nid);
936 if (!apage)
937 return;
938
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900939 err = read_node_page(apage, READA);
940 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900941 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900942 else if (err == LOCKED_PAGE)
943 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900944}
945
946struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
947{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900948 struct address_space *mapping = sbi->node_inode->i_mapping;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900949 struct page *page;
950 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900951repeat:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900952 page = grab_cache_page(mapping, nid);
953 if (!page)
954 return ERR_PTR(-ENOMEM);
955
956 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900957 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900959 else if (err == LOCKED_PAGE)
960 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900961
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900962 lock_page(page);
963 if (!PageUptodate(page)) {
964 f2fs_put_page(page, 1);
965 return ERR_PTR(-EIO);
966 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900967 if (page->mapping != mapping) {
968 f2fs_put_page(page, 1);
969 goto repeat;
970 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900971got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900972 BUG_ON(nid != nid_of_node(page));
973 mark_page_accessed(page);
974 return page;
975}
976
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900977/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900978 * Return a locked page for the desired node page.
979 * And, readahead MAX_RA_NODE number of node pages.
980 */
981struct page *get_node_page_ra(struct page *parent, int start)
982{
983 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
984 struct address_space *mapping = sbi->node_inode->i_mapping;
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900985 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900986 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900987 int err, i, end;
988 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900989
990 /* First, try getting the desired direct node. */
991 nid = get_nid(parent, start, false);
992 if (!nid)
993 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900994repeat:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900995 page = grab_cache_page(mapping, nid);
996 if (!page)
997 return ERR_PTR(-ENOMEM);
998
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900999 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001000 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001001 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001002 else if (err == LOCKED_PAGE)
1003 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001004
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001005 blk_start_plug(&plug);
1006
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001007 /* Then, try readahead for siblings of the desired node */
1008 end = start + MAX_RA_NODE;
1009 end = min(end, NIDS_PER_BLOCK);
1010 for (i = start + 1; i < end; i++) {
1011 nid = get_nid(parent, i, false);
1012 if (!nid)
1013 continue;
1014 ra_node_page(sbi, nid);
1015 }
1016
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001017 blk_finish_plug(&plug);
1018
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001019 lock_page(page);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001020 if (page->mapping != mapping) {
1021 f2fs_put_page(page, 1);
1022 goto repeat;
1023 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001024page_hit:
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001025 if (!PageUptodate(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001026 f2fs_put_page(page, 1);
1027 return ERR_PTR(-EIO);
1028 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001029 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001030 return page;
1031}
1032
1033void sync_inode_page(struct dnode_of_data *dn)
1034{
1035 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1036 update_inode(dn->inode, dn->node_page);
1037 } else if (dn->inode_page) {
1038 if (!dn->inode_page_locked)
1039 lock_page(dn->inode_page);
1040 update_inode(dn->inode, dn->inode_page);
1041 if (!dn->inode_page_locked)
1042 unlock_page(dn->inode_page);
1043 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001044 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001045 }
1046}
1047
1048int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1049 struct writeback_control *wbc)
1050{
1051 struct address_space *mapping = sbi->node_inode->i_mapping;
1052 pgoff_t index, end;
1053 struct pagevec pvec;
1054 int step = ino ? 2 : 0;
1055 int nwritten = 0, wrote = 0;
1056
1057 pagevec_init(&pvec, 0);
1058
1059next_step:
1060 index = 0;
1061 end = LONG_MAX;
1062
1063 while (index <= end) {
1064 int i, nr_pages;
1065 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1066 PAGECACHE_TAG_DIRTY,
1067 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1068 if (nr_pages == 0)
1069 break;
1070
1071 for (i = 0; i < nr_pages; i++) {
1072 struct page *page = pvec.pages[i];
1073
1074 /*
1075 * flushing sequence with step:
1076 * 0. indirect nodes
1077 * 1. dentry dnodes
1078 * 2. file dnodes
1079 */
1080 if (step == 0 && IS_DNODE(page))
1081 continue;
1082 if (step == 1 && (!IS_DNODE(page) ||
1083 is_cold_node(page)))
1084 continue;
1085 if (step == 2 && (!IS_DNODE(page) ||
1086 !is_cold_node(page)))
1087 continue;
1088
1089 /*
1090 * If an fsync mode,
1091 * we should not skip writing node pages.
1092 */
1093 if (ino && ino_of_node(page) == ino)
1094 lock_page(page);
1095 else if (!trylock_page(page))
1096 continue;
1097
1098 if (unlikely(page->mapping != mapping)) {
1099continue_unlock:
1100 unlock_page(page);
1101 continue;
1102 }
1103 if (ino && ino_of_node(page) != ino)
1104 goto continue_unlock;
1105
1106 if (!PageDirty(page)) {
1107 /* someone wrote it for us */
1108 goto continue_unlock;
1109 }
1110
1111 if (!clear_page_dirty_for_io(page))
1112 goto continue_unlock;
1113
1114 /* called by fsync() */
1115 if (ino && IS_DNODE(page)) {
1116 int mark = !is_checkpointed_node(sbi, ino);
1117 set_fsync_mark(page, 1);
1118 if (IS_INODE(page))
1119 set_dentry_mark(page, mark);
1120 nwritten++;
1121 } else {
1122 set_fsync_mark(page, 0);
1123 set_dentry_mark(page, 0);
1124 }
1125 mapping->a_ops->writepage(page, wbc);
1126 wrote++;
1127
1128 if (--wbc->nr_to_write == 0)
1129 break;
1130 }
1131 pagevec_release(&pvec);
1132 cond_resched();
1133
1134 if (wbc->nr_to_write == 0) {
1135 step = 2;
1136 break;
1137 }
1138 }
1139
1140 if (step < 2) {
1141 step++;
1142 goto next_step;
1143 }
1144
1145 if (wrote)
1146 f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL);
1147
1148 return nwritten;
1149}
1150
1151static int f2fs_write_node_page(struct page *page,
1152 struct writeback_control *wbc)
1153{
1154 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1155 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001156 block_t new_addr;
1157 struct node_info ni;
1158
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001159 if (sbi->por_doing)
1160 goto redirty_out;
1161
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001162 wait_on_page_writeback(page);
1163
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001164 /* get old block addr of this node page */
1165 nid = nid_of_node(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001166 BUG_ON(page->index != nid);
1167
1168 get_node_info(sbi, nid, &ni);
1169
1170 /* This page is already truncated */
Jaegeuk Kim39936832012-11-22 16:21:29 +09001171 if (ni.blk_addr == NULL_ADDR) {
1172 dec_page_count(sbi, F2FS_DIRTY_NODES);
1173 unlock_page(page);
1174 return 0;
1175 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001176
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001177 if (wbc->for_reclaim)
1178 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001179
Jaegeuk Kim39936832012-11-22 16:21:29 +09001180 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001181 set_page_writeback(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001182 write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1183 set_node_addr(sbi, &ni, new_addr);
1184 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001185 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001186 unlock_page(page);
1187 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001188
1189redirty_out:
1190 dec_page_count(sbi, F2FS_DIRTY_NODES);
1191 wbc->pages_skipped++;
1192 set_page_dirty(page);
1193 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001194}
1195
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001196/*
1197 * It is very important to gather dirty pages and write at once, so that we can
1198 * submit a big bio without interfering other data writes.
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001199 * Be default, 512 pages (2MB) * 3 node types, is more reasonable.
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001200 */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001201#define COLLECT_DIRTY_NODES 1536
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001202static int f2fs_write_node_pages(struct address_space *mapping,
1203 struct writeback_control *wbc)
1204{
1205 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001206 long nr_to_write = wbc->nr_to_write;
1207
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001208 /* First check balancing cached NAT entries */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001209 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK)) {
Jaegeuk Kimb7473752013-04-01 08:32:21 +09001210 f2fs_sync_fs(sbi->sb, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001211 return 0;
1212 }
1213
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001214 /* collect a number of dirty node pages and write together */
1215 if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1216 return 0;
1217
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001218 /* if mounting is failed, skip writing node pages */
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001219 wbc->nr_to_write = 3 * max_hw_blocks(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001220 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim423e95c2013-09-05 10:07:15 +09001221 wbc->nr_to_write = nr_to_write - (3 * max_hw_blocks(sbi) -
1222 wbc->nr_to_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001223 return 0;
1224}
1225
1226static int f2fs_set_node_page_dirty(struct page *page)
1227{
1228 struct address_space *mapping = page->mapping;
1229 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1230
1231 SetPageUptodate(page);
1232 if (!PageDirty(page)) {
1233 __set_page_dirty_nobuffers(page);
1234 inc_page_count(sbi, F2FS_DIRTY_NODES);
1235 SetPagePrivate(page);
1236 return 1;
1237 }
1238 return 0;
1239}
1240
Lukas Czernerd47992f2013-05-21 23:17:23 -04001241static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1242 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001243{
1244 struct inode *inode = page->mapping->host;
1245 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1246 if (PageDirty(page))
1247 dec_page_count(sbi, F2FS_DIRTY_NODES);
1248 ClearPagePrivate(page);
1249}
1250
1251static int f2fs_release_node_page(struct page *page, gfp_t wait)
1252{
1253 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001254 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001255}
1256
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001257/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001258 * Structure of the f2fs node operations
1259 */
1260const struct address_space_operations f2fs_node_aops = {
1261 .writepage = f2fs_write_node_page,
1262 .writepages = f2fs_write_node_pages,
1263 .set_page_dirty = f2fs_set_node_page_dirty,
1264 .invalidatepage = f2fs_invalidate_node_page,
1265 .releasepage = f2fs_release_node_page,
1266};
1267
1268static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1269{
1270 struct list_head *this;
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001271 struct free_nid *i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001272 list_for_each(this, head) {
1273 i = list_entry(this, struct free_nid, list);
1274 if (i->nid == n)
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001275 return i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001276 }
Namjae Jeon3aa770a2013-03-02 12:40:50 +09001277 return NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001278}
1279
1280static void __del_from_free_nid_list(struct free_nid *i)
1281{
1282 list_del(&i->list);
1283 kmem_cache_free(free_nid_slab, i);
1284}
1285
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001286static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001287{
1288 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001289 struct nat_entry *ne;
1290 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291
1292 if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
Haicheng Li23d38842013-05-06 23:15:43 +08001293 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001294
1295 /* 0 nid should not be used */
1296 if (nid == 0)
1297 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001298
1299 if (!build)
1300 goto retry;
1301
1302 /* do not add allocated nids */
1303 read_lock(&nm_i->nat_tree_lock);
1304 ne = __lookup_nat_cache(nm_i, nid);
1305 if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1306 allocated = true;
1307 read_unlock(&nm_i->nat_tree_lock);
1308 if (allocated)
1309 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310retry:
1311 i = kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1312 if (!i) {
1313 cond_resched();
1314 goto retry;
1315 }
1316 i->nid = nid;
1317 i->state = NID_NEW;
1318
1319 spin_lock(&nm_i->free_nid_list_lock);
1320 if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1321 spin_unlock(&nm_i->free_nid_list_lock);
1322 kmem_cache_free(free_nid_slab, i);
1323 return 0;
1324 }
1325 list_add_tail(&i->list, &nm_i->free_nid_list);
1326 nm_i->fcnt++;
1327 spin_unlock(&nm_i->free_nid_list_lock);
1328 return 1;
1329}
1330
1331static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1332{
1333 struct free_nid *i;
1334 spin_lock(&nm_i->free_nid_list_lock);
1335 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1336 if (i && i->state == NID_NEW) {
1337 __del_from_free_nid_list(i);
1338 nm_i->fcnt--;
1339 }
1340 spin_unlock(&nm_i->free_nid_list_lock);
1341}
1342
Haicheng Li87609522013-05-06 23:15:42 +08001343static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344 struct page *nat_page, nid_t start_nid)
1345{
1346 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1347 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001348 int i;
1349
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001350 i = start_nid % NAT_ENTRY_PER_BLOCK;
1351
1352 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001353
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001354 if (start_nid >= nm_i->max_nid)
1355 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001356
1357 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001358 BUG_ON(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001359 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001360 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001361 break;
1362 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001363 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001364}
1365
1366static void build_free_nids(struct f2fs_sb_info *sbi)
1367{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001368 struct f2fs_nm_info *nm_i = NM_I(sbi);
1369 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1370 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001371 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001372 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001373
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001374 /* Enough entries */
1375 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1376 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001377
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001378 /* readahead nat pages to be scanned */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001379 ra_nat_pages(sbi, nid);
1380
1381 while (1) {
1382 struct page *page = get_current_nat_page(sbi, nid);
1383
Haicheng Li87609522013-05-06 23:15:42 +08001384 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001385 f2fs_put_page(page, 1);
1386
1387 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001388 if (nid >= nm_i->max_nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001389 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001390
1391 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392 break;
1393 }
1394
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001395 /* go to the next free nat pages to find free nids abundantly */
1396 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001397
1398 /* find free nids from current sum_pages */
1399 mutex_lock(&curseg->curseg_mutex);
1400 for (i = 0; i < nats_in_cursum(sum); i++) {
1401 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1402 nid = le32_to_cpu(nid_in_journal(sum, i));
1403 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001404 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001405 else
1406 remove_free_nid(nm_i, nid);
1407 }
1408 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001409}
1410
1411/*
1412 * If this function returns success, caller can obtain a new nid
1413 * from second parameter of this function.
1414 * The returned nid could be used ino as well as nid when inode is created.
1415 */
1416bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1417{
1418 struct f2fs_nm_info *nm_i = NM_I(sbi);
1419 struct free_nid *i = NULL;
1420 struct list_head *this;
1421retry:
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001422 if (sbi->total_valid_node_count + 1 >= nm_i->max_nid)
1423 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001424
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001426
1427 /* We should not use stale free nids created by build_free_nids */
1428 if (nm_i->fcnt && !sbi->on_build_free_nids) {
1429 BUG_ON(list_empty(&nm_i->free_nid_list));
1430 list_for_each(this, &nm_i->free_nid_list) {
1431 i = list_entry(this, struct free_nid, list);
1432 if (i->state == NID_NEW)
1433 break;
1434 }
1435
1436 BUG_ON(i->state != NID_NEW);
1437 *nid = i->nid;
1438 i->state = NID_ALLOC;
1439 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001440 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001441 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001442 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001443 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001444
1445 /* Let's scan nat pages and its caches to get free nids */
1446 mutex_lock(&nm_i->build_lock);
1447 sbi->on_build_free_nids = 1;
1448 build_free_nids(sbi);
1449 sbi->on_build_free_nids = 0;
1450 mutex_unlock(&nm_i->build_lock);
1451 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001452}
1453
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001454/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001455 * alloc_nid() should be called prior to this function.
1456 */
1457void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1458{
1459 struct f2fs_nm_info *nm_i = NM_I(sbi);
1460 struct free_nid *i;
1461
1462 spin_lock(&nm_i->free_nid_list_lock);
1463 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001464 BUG_ON(!i || i->state != NID_ALLOC);
1465 __del_from_free_nid_list(i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001466 spin_unlock(&nm_i->free_nid_list_lock);
1467}
1468
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001469/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001470 * alloc_nid() should be called prior to this function.
1471 */
1472void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1473{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001474 struct f2fs_nm_info *nm_i = NM_I(sbi);
1475 struct free_nid *i;
1476
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001477 if (!nid)
1478 return;
1479
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001480 spin_lock(&nm_i->free_nid_list_lock);
1481 i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1482 BUG_ON(!i || i->state != NID_ALLOC);
Haicheng Li95630cb2013-05-06 23:15:41 +08001483 if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1484 __del_from_free_nid_list(i);
1485 } else {
1486 i->state = NID_NEW;
1487 nm_i->fcnt++;
1488 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001489 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001490}
1491
1492void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1493 struct f2fs_summary *sum, struct node_info *ni,
1494 block_t new_blkaddr)
1495{
1496 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1497 set_node_addr(sbi, ni, new_blkaddr);
1498 clear_node_page_dirty(page);
1499}
1500
1501int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1502{
1503 struct address_space *mapping = sbi->node_inode->i_mapping;
1504 struct f2fs_node *src, *dst;
1505 nid_t ino = ino_of_node(page);
1506 struct node_info old_ni, new_ni;
1507 struct page *ipage;
1508
1509 ipage = grab_cache_page(mapping, ino);
1510 if (!ipage)
1511 return -ENOMEM;
1512
1513 /* Should not use this inode from free nid list */
1514 remove_free_nid(NM_I(sbi), ino);
1515
1516 get_node_info(sbi, ino, &old_ni);
1517 SetPageUptodate(ipage);
1518 fill_node_footer(ipage, ino, ino, 0, true);
1519
Gu Zheng45590712013-07-15 17:57:38 +08001520 src = F2FS_NODE(page);
1521 dst = F2FS_NODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001522
1523 memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1524 dst->i.i_size = 0;
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001525 dst->i.i_blocks = cpu_to_le64(1);
1526 dst->i.i_links = cpu_to_le32(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001527 dst->i.i_xattr_nid = 0;
1528
1529 new_ni = old_ni;
1530 new_ni.ino = ino;
1531
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001532 if (!inc_valid_node_count(sbi, NULL, 1))
1533 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001534 set_node_addr(sbi, &new_ni, NEW_ADDR);
1535 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001536 f2fs_put_page(ipage, 1);
1537 return 0;
1538}
1539
1540int restore_node_summary(struct f2fs_sb_info *sbi,
1541 unsigned int segno, struct f2fs_summary_block *sum)
1542{
1543 struct f2fs_node *rn;
1544 struct f2fs_summary *sum_entry;
1545 struct page *page;
1546 block_t addr;
1547 int i, last_offset;
1548
1549 /* alloc temporal page for read node */
1550 page = alloc_page(GFP_NOFS | __GFP_ZERO);
Dan Carpentere27dae42013-08-15 08:54:56 +03001551 if (!page)
1552 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001553 lock_page(page);
1554
1555 /* scan the node segment */
1556 last_offset = sbi->blocks_per_seg;
1557 addr = START_BLOCK(sbi, segno);
1558 sum_entry = &sum->entries[0];
1559
1560 for (i = 0; i < last_offset; i++, sum_entry++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001561 /*
1562 * In order to read next node page,
1563 * we must clear PageUptodate flag.
1564 */
1565 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001566
1567 if (f2fs_readpage(sbi, page, addr, READ_SYNC))
1568 goto out;
1569
1570 lock_page(page);
Gu Zheng45590712013-07-15 17:57:38 +08001571 rn = F2FS_NODE(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001572 sum_entry->nid = rn->footer.nid;
1573 sum_entry->version = 0;
1574 sum_entry->ofs_in_node = 0;
1575 addr++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001576 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577 unlock_page(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001578out:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001579 __free_pages(page, 0);
1580 return 0;
1581}
1582
1583static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1584{
1585 struct f2fs_nm_info *nm_i = NM_I(sbi);
1586 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1587 struct f2fs_summary_block *sum = curseg->sum_blk;
1588 int i;
1589
1590 mutex_lock(&curseg->curseg_mutex);
1591
1592 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1593 mutex_unlock(&curseg->curseg_mutex);
1594 return false;
1595 }
1596
1597 for (i = 0; i < nats_in_cursum(sum); i++) {
1598 struct nat_entry *ne;
1599 struct f2fs_nat_entry raw_ne;
1600 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1601
1602 raw_ne = nat_in_journal(sum, i);
1603retry:
1604 write_lock(&nm_i->nat_tree_lock);
1605 ne = __lookup_nat_cache(nm_i, nid);
1606 if (ne) {
1607 __set_nat_cache_dirty(nm_i, ne);
1608 write_unlock(&nm_i->nat_tree_lock);
1609 continue;
1610 }
1611 ne = grab_nat_entry(nm_i, nid);
1612 if (!ne) {
1613 write_unlock(&nm_i->nat_tree_lock);
1614 goto retry;
1615 }
1616 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1617 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1618 nat_set_version(ne, raw_ne.version);
1619 __set_nat_cache_dirty(nm_i, ne);
1620 write_unlock(&nm_i->nat_tree_lock);
1621 }
1622 update_nats_in_cursum(sum, -i);
1623 mutex_unlock(&curseg->curseg_mutex);
1624 return true;
1625}
1626
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001627/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001628 * This function is called during the checkpointing process.
1629 */
1630void flush_nat_entries(struct f2fs_sb_info *sbi)
1631{
1632 struct f2fs_nm_info *nm_i = NM_I(sbi);
1633 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1634 struct f2fs_summary_block *sum = curseg->sum_blk;
1635 struct list_head *cur, *n;
1636 struct page *page = NULL;
1637 struct f2fs_nat_block *nat_blk = NULL;
1638 nid_t start_nid = 0, end_nid = 0;
1639 bool flushed;
1640
1641 flushed = flush_nats_in_journal(sbi);
1642
1643 if (!flushed)
1644 mutex_lock(&curseg->curseg_mutex);
1645
1646 /* 1) flush dirty nat caches */
1647 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1648 struct nat_entry *ne;
1649 nid_t nid;
1650 struct f2fs_nat_entry raw_ne;
1651 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001652 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001653
1654 ne = list_entry(cur, struct nat_entry, list);
1655 nid = nat_get_nid(ne);
1656
1657 if (nat_get_blkaddr(ne) == NEW_ADDR)
1658 continue;
1659 if (flushed)
1660 goto to_nat_page;
1661
1662 /* if there is room for nat enries in curseg->sumpage */
1663 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1664 if (offset >= 0) {
1665 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001666 goto flush_now;
1667 }
1668to_nat_page:
1669 if (!page || (start_nid > nid || nid > end_nid)) {
1670 if (page) {
1671 f2fs_put_page(page, 1);
1672 page = NULL;
1673 }
1674 start_nid = START_NID(nid);
1675 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1676
1677 /*
1678 * get nat block with dirty flag, increased reference
1679 * count, mapped and lock
1680 */
1681 page = get_next_nat_page(sbi, start_nid);
1682 nat_blk = page_address(page);
1683 }
1684
1685 BUG_ON(!nat_blk);
1686 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001687flush_now:
1688 new_blkaddr = nat_get_blkaddr(ne);
1689
1690 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1691 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1692 raw_ne.version = nat_get_version(ne);
1693
1694 if (offset < 0) {
1695 nat_blk->entries[nid - start_nid] = raw_ne;
1696 } else {
1697 nat_in_journal(sum, offset) = raw_ne;
1698 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1699 }
1700
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001701 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001702 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001703 write_lock(&nm_i->nat_tree_lock);
1704 __del_from_nat_cache(nm_i, ne);
1705 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001706 } else {
1707 write_lock(&nm_i->nat_tree_lock);
1708 __clear_nat_cache_dirty(nm_i, ne);
1709 ne->checkpointed = true;
1710 write_unlock(&nm_i->nat_tree_lock);
1711 }
1712 }
1713 if (!flushed)
1714 mutex_unlock(&curseg->curseg_mutex);
1715 f2fs_put_page(page, 1);
1716
1717 /* 2) shrink nat caches if necessary */
1718 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1719}
1720
1721static int init_node_manager(struct f2fs_sb_info *sbi)
1722{
1723 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1724 struct f2fs_nm_info *nm_i = NM_I(sbi);
1725 unsigned char *version_bitmap;
1726 unsigned int nat_segs, nat_blocks;
1727
1728 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1729
1730 /* segment_count_nat includes pair segment so divide to 2. */
1731 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1732 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1733 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1734 nm_i->fcnt = 0;
1735 nm_i->nat_cnt = 0;
1736
1737 INIT_LIST_HEAD(&nm_i->free_nid_list);
1738 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1739 INIT_LIST_HEAD(&nm_i->nat_entries);
1740 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1741
1742 mutex_init(&nm_i->build_lock);
1743 spin_lock_init(&nm_i->free_nid_list_lock);
1744 rwlock_init(&nm_i->nat_tree_lock);
1745
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001746 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001747 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001748 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1749 if (!version_bitmap)
1750 return -EFAULT;
1751
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001752 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1753 GFP_KERNEL);
1754 if (!nm_i->nat_bitmap)
1755 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001756 return 0;
1757}
1758
1759int build_node_manager(struct f2fs_sb_info *sbi)
1760{
1761 int err;
1762
1763 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1764 if (!sbi->nm_info)
1765 return -ENOMEM;
1766
1767 err = init_node_manager(sbi);
1768 if (err)
1769 return err;
1770
1771 build_free_nids(sbi);
1772 return 0;
1773}
1774
1775void destroy_node_manager(struct f2fs_sb_info *sbi)
1776{
1777 struct f2fs_nm_info *nm_i = NM_I(sbi);
1778 struct free_nid *i, *next_i;
1779 struct nat_entry *natvec[NATVEC_SIZE];
1780 nid_t nid = 0;
1781 unsigned int found;
1782
1783 if (!nm_i)
1784 return;
1785
1786 /* destroy free nid list */
1787 spin_lock(&nm_i->free_nid_list_lock);
1788 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1789 BUG_ON(i->state == NID_ALLOC);
1790 __del_from_free_nid_list(i);
1791 nm_i->fcnt--;
1792 }
1793 BUG_ON(nm_i->fcnt);
1794 spin_unlock(&nm_i->free_nid_list_lock);
1795
1796 /* destroy nat cache */
1797 write_lock(&nm_i->nat_tree_lock);
1798 while ((found = __gang_lookup_nat_cache(nm_i,
1799 nid, NATVEC_SIZE, natvec))) {
1800 unsigned idx;
1801 for (idx = 0; idx < found; idx++) {
1802 struct nat_entry *e = natvec[idx];
1803 nid = nat_get_nid(e) + 1;
1804 __del_from_nat_cache(nm_i, e);
1805 }
1806 }
1807 BUG_ON(nm_i->nat_cnt);
1808 write_unlock(&nm_i->nat_tree_lock);
1809
1810 kfree(nm_i->nat_bitmap);
1811 sbi->nm_info = NULL;
1812 kfree(nm_i);
1813}
1814
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001815int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001816{
1817 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1818 sizeof(struct nat_entry), NULL);
1819 if (!nat_entry_slab)
1820 return -ENOMEM;
1821
1822 free_nid_slab = f2fs_kmem_cache_create("free_nid",
1823 sizeof(struct free_nid), NULL);
1824 if (!free_nid_slab) {
1825 kmem_cache_destroy(nat_entry_slab);
1826 return -ENOMEM;
1827 }
1828 return 0;
1829}
1830
1831void destroy_node_manager_caches(void)
1832{
1833 kmem_cache_destroy(free_nid_slab);
1834 kmem_cache_destroy(nat_entry_slab);
1835}