blob: fec4967fb8d280d03a666f9c900376691338420e [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/mpage.h>
14#include <linux/backing-dev.h>
15#include <linux/blkdev.h>
16#include <linux/pagevec.h>
17#include <linux/swap.h>
18
19#include "f2fs.h"
20#include "node.h"
21#include "segment.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090022#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090023
Gu Zhengf978f5a2014-02-21 18:08:29 +080024#define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
25
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090026static struct kmem_cache *nat_entry_slab;
27static struct kmem_cache *free_nid_slab;
28
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090029static inline bool available_free_memory(struct f2fs_nm_info *nm_i, int type)
30{
31 struct sysinfo val;
32 unsigned long mem_size = 0;
33
34 si_meminfo(&val);
35 if (type == FREE_NIDS)
36 mem_size = nm_i->fcnt * sizeof(struct free_nid);
37 else if (type == NAT_ENTRIES)
38 mem_size += nm_i->nat_cnt * sizeof(struct nat_entry);
39 mem_size >>= 12;
40
41 /* give 50:50 memory for free nids and nat caches respectively */
42 return (mem_size < ((val.totalram * nm_i->ram_thresh) >> 11));
43}
44
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090045static void clear_node_page_dirty(struct page *page)
46{
47 struct address_space *mapping = page->mapping;
48 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
49 unsigned int long flags;
50
51 if (PageDirty(page)) {
52 spin_lock_irqsave(&mapping->tree_lock, flags);
53 radix_tree_tag_clear(&mapping->page_tree,
54 page_index(page),
55 PAGECACHE_TAG_DIRTY);
56 spin_unlock_irqrestore(&mapping->tree_lock, flags);
57
58 clear_page_dirty_for_io(page);
59 dec_page_count(sbi, F2FS_DIRTY_NODES);
60 }
61 ClearPageUptodate(page);
62}
63
64static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
65{
66 pgoff_t index = current_nat_addr(sbi, nid);
67 return get_meta_page(sbi, index);
68}
69
70static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
71{
72 struct page *src_page;
73 struct page *dst_page;
74 pgoff_t src_off;
75 pgoff_t dst_off;
76 void *src_addr;
77 void *dst_addr;
78 struct f2fs_nm_info *nm_i = NM_I(sbi);
79
80 src_off = current_nat_addr(sbi, nid);
81 dst_off = next_nat_addr(sbi, src_off);
82
83 /* get current nat block page with lock */
84 src_page = get_meta_page(sbi, src_off);
85
86 /* Dirty src_page means that it is already the new target NAT page. */
87 if (PageDirty(src_page))
88 return src_page;
89
90 dst_page = grab_meta_page(sbi, dst_off);
91
92 src_addr = page_address(src_page);
93 dst_addr = page_address(dst_page);
94 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
95 set_page_dirty(dst_page);
96 f2fs_put_page(src_page, 1);
97
98 set_to_next_nat(nm_i, nid);
99
100 return dst_page;
101}
102
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900103static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
104{
105 return radix_tree_lookup(&nm_i->nat_root, n);
106}
107
108static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
109 nid_t start, unsigned int nr, struct nat_entry **ep)
110{
111 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
112}
113
114static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
115{
116 list_del(&e->list);
117 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
118 nm_i->nat_cnt--;
119 kmem_cache_free(nat_entry_slab, e);
120}
121
122int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
123{
124 struct f2fs_nm_info *nm_i = NM_I(sbi);
125 struct nat_entry *e;
126 int is_cp = 1;
127
128 read_lock(&nm_i->nat_tree_lock);
129 e = __lookup_nat_cache(nm_i, nid);
130 if (e && !e->checkpointed)
131 is_cp = 0;
132 read_unlock(&nm_i->nat_tree_lock);
133 return is_cp;
134}
135
136static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
137{
138 struct nat_entry *new;
139
140 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
141 if (!new)
142 return NULL;
143 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
144 kmem_cache_free(nat_entry_slab, new);
145 return NULL;
146 }
147 memset(new, 0, sizeof(struct nat_entry));
148 nat_set_nid(new, nid);
Jaegeuk Kimfffc2a02014-02-21 13:17:22 +0900149 new->checkpointed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900150 list_add_tail(&new->list, &nm_i->nat_entries);
151 nm_i->nat_cnt++;
152 return new;
153}
154
155static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
156 struct f2fs_nat_entry *ne)
157{
158 struct nat_entry *e;
159retry:
160 write_lock(&nm_i->nat_tree_lock);
161 e = __lookup_nat_cache(nm_i, nid);
162 if (!e) {
163 e = grab_nat_entry(nm_i, nid);
164 if (!e) {
165 write_unlock(&nm_i->nat_tree_lock);
166 goto retry;
167 }
168 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
169 nat_set_ino(e, le32_to_cpu(ne->ino));
170 nat_set_version(e, ne->version);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900171 }
172 write_unlock(&nm_i->nat_tree_lock);
173}
174
175static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
176 block_t new_blkaddr)
177{
178 struct f2fs_nm_info *nm_i = NM_I(sbi);
179 struct nat_entry *e;
180retry:
181 write_lock(&nm_i->nat_tree_lock);
182 e = __lookup_nat_cache(nm_i, ni->nid);
183 if (!e) {
184 e = grab_nat_entry(nm_i, ni->nid);
185 if (!e) {
186 write_unlock(&nm_i->nat_tree_lock);
187 goto retry;
188 }
189 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900190 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900191 } else if (new_blkaddr == NEW_ADDR) {
192 /*
193 * when nid is reallocated,
194 * previous nat entry can be remained in nat cache.
195 * So, reinitialize it with new information.
196 */
197 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900198 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900199 }
200
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900201 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900202 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
203 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900204 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900205 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900206 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900207 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900208 nat_get_blkaddr(e) != NULL_ADDR &&
209 new_blkaddr == NEW_ADDR);
210
211 /* increament version no as node is removed */
212 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
213 unsigned char version = nat_get_version(e);
214 nat_set_version(e, inc_node_version(version));
215 }
216
217 /* change address */
218 nat_set_blkaddr(e, new_blkaddr);
219 __set_nat_cache_dirty(nm_i, e);
220 write_unlock(&nm_i->nat_tree_lock);
221}
222
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900223int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900224{
225 struct f2fs_nm_info *nm_i = NM_I(sbi);
226
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900227 if (available_free_memory(nm_i, NAT_ENTRIES) || nr_shrink <= 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900228 return 0;
229
230 write_lock(&nm_i->nat_tree_lock);
231 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
232 struct nat_entry *ne;
233 ne = list_first_entry(&nm_i->nat_entries,
234 struct nat_entry, list);
235 __del_from_nat_cache(nm_i, ne);
236 nr_shrink--;
237 }
238 write_unlock(&nm_i->nat_tree_lock);
239 return nr_shrink;
240}
241
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900242/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900243 * This function returns always success
244 */
245void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
246{
247 struct f2fs_nm_info *nm_i = NM_I(sbi);
248 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
249 struct f2fs_summary_block *sum = curseg->sum_blk;
250 nid_t start_nid = START_NID(nid);
251 struct f2fs_nat_block *nat_blk;
252 struct page *page = NULL;
253 struct f2fs_nat_entry ne;
254 struct nat_entry *e;
255 int i;
256
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900257 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900258 ni->nid = nid;
259
260 /* Check nat cache */
261 read_lock(&nm_i->nat_tree_lock);
262 e = __lookup_nat_cache(nm_i, nid);
263 if (e) {
264 ni->ino = nat_get_ino(e);
265 ni->blk_addr = nat_get_blkaddr(e);
266 ni->version = nat_get_version(e);
267 }
268 read_unlock(&nm_i->nat_tree_lock);
269 if (e)
270 return;
271
272 /* Check current segment summary */
273 mutex_lock(&curseg->curseg_mutex);
274 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
275 if (i >= 0) {
276 ne = nat_in_journal(sum, i);
277 node_info_from_raw_nat(ni, &ne);
278 }
279 mutex_unlock(&curseg->curseg_mutex);
280 if (i >= 0)
281 goto cache;
282
283 /* Fill node_info from nat page */
284 page = get_current_nat_page(sbi, start_nid);
285 nat_blk = (struct f2fs_nat_block *)page_address(page);
286 ne = nat_blk->entries[nid - start_nid];
287 node_info_from_raw_nat(ni, &ne);
288 f2fs_put_page(page, 1);
289cache:
290 /* cache nat entry */
291 cache_nat_entry(NM_I(sbi), nid, &ne);
292}
293
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900294/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900295 * The maximum depth is four.
296 * Offset[0] will have raw inode offset.
297 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900298static int get_node_path(struct f2fs_inode_info *fi, long block,
299 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900300{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900301 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900302 const long direct_blks = ADDRS_PER_BLOCK;
303 const long dptrs_per_blk = NIDS_PER_BLOCK;
304 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
305 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
306 int n = 0;
307 int level = 0;
308
309 noffset[0] = 0;
310
311 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900312 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900313 goto got;
314 }
315 block -= direct_index;
316 if (block < direct_blks) {
317 offset[n++] = NODE_DIR1_BLOCK;
318 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900319 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900320 level = 1;
321 goto got;
322 }
323 block -= direct_blks;
324 if (block < direct_blks) {
325 offset[n++] = NODE_DIR2_BLOCK;
326 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900327 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900328 level = 1;
329 goto got;
330 }
331 block -= direct_blks;
332 if (block < indirect_blks) {
333 offset[n++] = NODE_IND1_BLOCK;
334 noffset[n] = 3;
335 offset[n++] = block / direct_blks;
336 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900337 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900338 level = 2;
339 goto got;
340 }
341 block -= indirect_blks;
342 if (block < indirect_blks) {
343 offset[n++] = NODE_IND2_BLOCK;
344 noffset[n] = 4 + dptrs_per_blk;
345 offset[n++] = block / direct_blks;
346 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900347 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900348 level = 2;
349 goto got;
350 }
351 block -= indirect_blks;
352 if (block < dindirect_blks) {
353 offset[n++] = NODE_DIND_BLOCK;
354 noffset[n] = 5 + (dptrs_per_blk * 2);
355 offset[n++] = block / indirect_blks;
356 noffset[n] = 6 + (dptrs_per_blk * 2) +
357 offset[n - 1] * (dptrs_per_blk + 1);
358 offset[n++] = (block / direct_blks) % dptrs_per_blk;
359 noffset[n] = 7 + (dptrs_per_blk * 2) +
360 offset[n - 2] * (dptrs_per_blk + 1) +
361 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900362 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900363 level = 3;
364 goto got;
365 } else {
366 BUG();
367 }
368got:
369 return level;
370}
371
372/*
373 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800374 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
375 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900376 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900377 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900378int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900379{
380 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
381 struct page *npage[4];
382 struct page *parent;
383 int offset[4];
384 unsigned int noffset[4];
385 nid_t nids[4];
386 int level, i;
387 int err = 0;
388
Jaegeuk Kimde936532013-08-12 21:08:03 +0900389 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900390
391 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900392 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900393
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900394 if (!npage[0]) {
395 npage[0] = get_node_page(sbi, nids[0]);
396 if (IS_ERR(npage[0]))
397 return PTR_ERR(npage[0]);
398 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900399 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900400 if (level != 0)
401 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900402 dn->inode_page = npage[0];
403 dn->inode_page_locked = true;
404
405 /* get indirect or direct nodes */
406 for (i = 1; i <= level; i++) {
407 bool done = false;
408
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900409 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410 /* alloc new node */
411 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 err = -ENOSPC;
413 goto release_pages;
414 }
415
416 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900417 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900418 if (IS_ERR(npage[i])) {
419 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900420 err = PTR_ERR(npage[i]);
421 goto release_pages;
422 }
423
424 set_nid(parent, offset[i - 1], nids[i], i == 1);
425 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900426 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900427 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900428 npage[i] = get_node_page_ra(parent, offset[i - 1]);
429 if (IS_ERR(npage[i])) {
430 err = PTR_ERR(npage[i]);
431 goto release_pages;
432 }
433 done = true;
434 }
435 if (i == 1) {
436 dn->inode_page_locked = false;
437 unlock_page(parent);
438 } else {
439 f2fs_put_page(parent, 1);
440 }
441
442 if (!done) {
443 npage[i] = get_node_page(sbi, nids[i]);
444 if (IS_ERR(npage[i])) {
445 err = PTR_ERR(npage[i]);
446 f2fs_put_page(npage[0], 0);
447 goto release_out;
448 }
449 }
450 if (i < level) {
451 parent = npage[i];
452 nids[i + 1] = get_nid(parent, offset[i], false);
453 }
454 }
455 dn->nid = nids[level];
456 dn->ofs_in_node = offset[level];
457 dn->node_page = npage[level];
458 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
459 return 0;
460
461release_pages:
462 f2fs_put_page(parent, 1);
463 if (i > 1)
464 f2fs_put_page(npage[0], 0);
465release_out:
466 dn->inode_page = NULL;
467 dn->node_page = NULL;
468 return err;
469}
470
471static void truncate_node(struct dnode_of_data *dn)
472{
473 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
474 struct node_info ni;
475
476 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900477 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900478 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900479 goto invalidate;
480 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900481 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900482
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900483 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900484 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800485 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900486 set_node_addr(sbi, &ni, NULL_ADDR);
487
488 if (dn->nid == dn->inode->i_ino) {
489 remove_orphan_inode(sbi, dn->nid);
490 dec_valid_inode_count(sbi);
491 } else {
492 sync_inode_page(dn);
493 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900494invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900495 clear_node_page_dirty(dn->node_page);
496 F2FS_SET_SB_DIRT(sbi);
497
498 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900499
500 invalidate_mapping_pages(NODE_MAPPING(sbi),
501 dn->node_page->index, dn->node_page->index);
502
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900503 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900504 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505}
506
507static int truncate_dnode(struct dnode_of_data *dn)
508{
509 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
510 struct page *page;
511
512 if (dn->nid == 0)
513 return 1;
514
515 /* get direct node */
516 page = get_node_page(sbi, dn->nid);
517 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
518 return 1;
519 else if (IS_ERR(page))
520 return PTR_ERR(page);
521
522 /* Make dnode_of_data for parameter */
523 dn->node_page = page;
524 dn->ofs_in_node = 0;
525 truncate_data_blocks(dn);
526 truncate_node(dn);
527 return 1;
528}
529
530static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
531 int ofs, int depth)
532{
533 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
534 struct dnode_of_data rdn = *dn;
535 struct page *page;
536 struct f2fs_node *rn;
537 nid_t child_nid;
538 unsigned int child_nofs;
539 int freed = 0;
540 int i, ret;
541
542 if (dn->nid == 0)
543 return NIDS_PER_BLOCK + 1;
544
Namjae Jeon51dd6242013-04-20 01:28:52 +0900545 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
546
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900547 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900548 if (IS_ERR(page)) {
549 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900550 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900551 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900552
Gu Zheng45590712013-07-15 17:57:38 +0800553 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900554 if (depth < 3) {
555 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
556 child_nid = le32_to_cpu(rn->in.nid[i]);
557 if (child_nid == 0)
558 continue;
559 rdn.nid = child_nid;
560 ret = truncate_dnode(&rdn);
561 if (ret < 0)
562 goto out_err;
563 set_nid(page, i, 0, false);
564 }
565 } else {
566 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
567 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
568 child_nid = le32_to_cpu(rn->in.nid[i]);
569 if (child_nid == 0) {
570 child_nofs += NIDS_PER_BLOCK + 1;
571 continue;
572 }
573 rdn.nid = child_nid;
574 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
575 if (ret == (NIDS_PER_BLOCK + 1)) {
576 set_nid(page, i, 0, false);
577 child_nofs += ret;
578 } else if (ret < 0 && ret != -ENOENT) {
579 goto out_err;
580 }
581 }
582 freed = child_nofs;
583 }
584
585 if (!ofs) {
586 /* remove current indirect node */
587 dn->node_page = page;
588 truncate_node(dn);
589 freed++;
590 } else {
591 f2fs_put_page(page, 1);
592 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900593 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900594 return freed;
595
596out_err:
597 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900598 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900599 return ret;
600}
601
602static int truncate_partial_nodes(struct dnode_of_data *dn,
603 struct f2fs_inode *ri, int *offset, int depth)
604{
605 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
606 struct page *pages[2];
607 nid_t nid[3];
608 nid_t child_nid;
609 int err = 0;
610 int i;
611 int idx = depth - 2;
612
613 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
614 if (!nid[0])
615 return 0;
616
617 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800618 for (i = 0; i < idx + 1; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900619 /* refernece count'll be increased */
620 pages[i] = get_node_page(sbi, nid[i]);
621 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900622 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800623 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900624 goto fail;
625 }
626 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
627 }
628
629 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800630 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900631 child_nid = get_nid(pages[idx], i, false);
632 if (!child_nid)
633 continue;
634 dn->nid = child_nid;
635 err = truncate_dnode(dn);
636 if (err < 0)
637 goto fail;
638 set_nid(pages[idx], i, 0, false);
639 }
640
shifei10.gea225dca2013-10-29 15:32:34 +0800641 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900642 dn->node_page = pages[idx];
643 dn->nid = nid[idx];
644 truncate_node(dn);
645 } else {
646 f2fs_put_page(pages[idx], 1);
647 }
648 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800649 offset[idx + 1] = 0;
650 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900651fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800652 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900653 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900654
655 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
656
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900657 return err;
658}
659
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900660/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900661 * All the block addresses of data and nodes should be nullified.
662 */
663int truncate_inode_blocks(struct inode *inode, pgoff_t from)
664{
665 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
666 int err = 0, cont = 1;
667 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900668 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900669 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900670 struct dnode_of_data dn;
671 struct page *page;
672
Namjae Jeon51dd6242013-04-20 01:28:52 +0900673 trace_f2fs_truncate_inode_blocks_enter(inode, from);
674
Jaegeuk Kimde936532013-08-12 21:08:03 +0900675 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900676restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900677 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900678 if (IS_ERR(page)) {
679 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900680 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900681 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900682
683 set_new_dnode(&dn, inode, page, NULL, 0);
684 unlock_page(page);
685
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900686 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900687 switch (level) {
688 case 0:
689 case 1:
690 nofs = noffset[1];
691 break;
692 case 2:
693 nofs = noffset[1];
694 if (!offset[level - 1])
695 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900696 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900697 if (err < 0 && err != -ENOENT)
698 goto fail;
699 nofs += 1 + NIDS_PER_BLOCK;
700 break;
701 case 3:
702 nofs = 5 + 2 * NIDS_PER_BLOCK;
703 if (!offset[level - 1])
704 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900705 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900706 if (err < 0 && err != -ENOENT)
707 goto fail;
708 break;
709 default:
710 BUG();
711 }
712
713skip_partial:
714 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900715 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716 switch (offset[0]) {
717 case NODE_DIR1_BLOCK:
718 case NODE_DIR2_BLOCK:
719 err = truncate_dnode(&dn);
720 break;
721
722 case NODE_IND1_BLOCK:
723 case NODE_IND2_BLOCK:
724 err = truncate_nodes(&dn, nofs, offset[1], 2);
725 break;
726
727 case NODE_DIND_BLOCK:
728 err = truncate_nodes(&dn, nofs, offset[1], 3);
729 cont = 0;
730 break;
731
732 default:
733 BUG();
734 }
735 if (err < 0 && err != -ENOENT)
736 goto fail;
737 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900738 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900739 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900740 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900741 f2fs_put_page(page, 1);
742 goto restart;
743 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900744 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900745 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900746 set_page_dirty(page);
747 unlock_page(page);
748 }
749 offset[1] = 0;
750 offset[0]++;
751 nofs += err;
752 }
753fail:
754 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900755 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900756 return err > 0 ? 0 : err;
757}
758
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900759int truncate_xattr_node(struct inode *inode, struct page *page)
760{
761 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
762 nid_t nid = F2FS_I(inode)->i_xattr_nid;
763 struct dnode_of_data dn;
764 struct page *npage;
765
766 if (!nid)
767 return 0;
768
769 npage = get_node_page(sbi, nid);
770 if (IS_ERR(npage))
771 return PTR_ERR(npage);
772
773 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900774
775 /* need to do checkpoint during fsync */
776 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
777
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900778 set_new_dnode(&dn, inode, page, npage, nid);
779
780 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800781 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900782 truncate_node(&dn);
783 return 0;
784}
785
Jaegeuk Kim39936832012-11-22 16:21:29 +0900786/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800787 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
788 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900789 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800790void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900791{
792 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
793 struct page *page;
794 nid_t ino = inode->i_ino;
795 struct dnode_of_data dn;
796
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900797 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900798 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800799 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900800
Gu Zheng58e674d2013-11-19 18:03:18 +0800801 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900802 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800803 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900804 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900805 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900806 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900807 set_new_dnode(&dn, inode, page, page, ino);
808 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900809}
810
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900811struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900812{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900813 struct dnode_of_data dn;
814
815 /* allocate inode page for new inode */
816 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900817
818 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900819 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900820}
821
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900822struct page *new_node_page(struct dnode_of_data *dn,
823 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900824{
825 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900826 struct node_info old_ni, new_ni;
827 struct page *page;
828 int err;
829
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900830 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831 return ERR_PTR(-EPERM);
832
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900833 page = grab_cache_page_write_begin(NODE_MAPPING(sbi),
834 dn->nid, AOP_FLAG_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900835 if (!page)
836 return ERR_PTR(-ENOMEM);
837
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900838 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900839 err = -ENOSPC;
840 goto fail;
841 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900842
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900843 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844
845 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900846 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900847 new_ni = old_ni;
848 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900849 set_node_addr(sbi, &new_ni, NEW_ADDR);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900850
851 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900852 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900853 SetPageUptodate(page);
854 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800856 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900857 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
858
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900859 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900860 if (ipage)
861 update_inode(dn->inode, ipage);
862 else
863 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900864 if (ofs == 0)
865 inc_valid_inode_count(sbi);
866
867 return page;
868
869fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900870 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900871 f2fs_put_page(page, 1);
872 return ERR_PTR(err);
873}
874
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900875/*
876 * Caller should do after getting the following values.
877 * 0: f2fs_put_page(page, 0)
878 * LOCKED_PAGE: f2fs_put_page(page, 1)
879 * error: nothing
880 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900881static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900882{
883 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
884 struct node_info ni;
885
886 get_node_info(sbi, page->index, &ni);
887
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900888 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900889 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900890 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900891 }
892
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900893 if (PageUptodate(page))
894 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900895
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900896 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900897}
898
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900899/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900900 * Readahead a node page
901 */
902void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
903{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900904 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900905 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900907 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900908 if (apage && PageUptodate(apage)) {
909 f2fs_put_page(apage, 0);
910 return;
911 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900912 f2fs_put_page(apage, 0);
913
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900914 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900915 if (!apage)
916 return;
917
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900918 err = read_node_page(apage, READA);
919 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900920 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900921 else if (err == LOCKED_PAGE)
922 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923}
924
925struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
926{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900927 struct page *page;
928 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900929repeat:
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900930 page = grab_cache_page_write_begin(NODE_MAPPING(sbi),
931 nid, AOP_FLAG_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900932 if (!page)
933 return ERR_PTR(-ENOMEM);
934
935 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900936 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900937 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900938 else if (err == LOCKED_PAGE)
939 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900940
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900941 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900942 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900943 f2fs_put_page(page, 1);
944 return ERR_PTR(-EIO);
945 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900946 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900947 f2fs_put_page(page, 1);
948 goto repeat;
949 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900950got_it:
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900951 f2fs_bug_on(nid != nid_of_node(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900952 mark_page_accessed(page);
953 return page;
954}
955
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900956/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900957 * Return a locked page for the desired node page.
958 * And, readahead MAX_RA_NODE number of node pages.
959 */
960struct page *get_node_page_ra(struct page *parent, int start)
961{
962 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900963 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900964 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900965 int err, i, end;
966 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900967
968 /* First, try getting the desired direct node. */
969 nid = get_nid(parent, start, false);
970 if (!nid)
971 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900972repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900973 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900974 if (!page)
975 return ERR_PTR(-ENOMEM);
976
Jaegeuk Kim66d36a22013-02-26 12:43:46 +0900977 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900978 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900980 else if (err == LOCKED_PAGE)
981 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900982
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900983 blk_start_plug(&plug);
984
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985 /* Then, try readahead for siblings of the desired node */
986 end = start + MAX_RA_NODE;
987 end = min(end, NIDS_PER_BLOCK);
988 for (i = start + 1; i < end; i++) {
989 nid = get_nid(parent, i, false);
990 if (!nid)
991 continue;
992 ra_node_page(sbi, nid);
993 }
994
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900995 blk_finish_plug(&plug);
996
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900997 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900998 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900999 f2fs_put_page(page, 1);
1000 goto repeat;
1001 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001002page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001003 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001004 f2fs_put_page(page, 1);
1005 return ERR_PTR(-EIO);
1006 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001007 mark_page_accessed(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001008 return page;
1009}
1010
1011void sync_inode_page(struct dnode_of_data *dn)
1012{
1013 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1014 update_inode(dn->inode, dn->node_page);
1015 } else if (dn->inode_page) {
1016 if (!dn->inode_page_locked)
1017 lock_page(dn->inode_page);
1018 update_inode(dn->inode, dn->inode_page);
1019 if (!dn->inode_page_locked)
1020 unlock_page(dn->inode_page);
1021 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001022 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001023 }
1024}
1025
1026int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1027 struct writeback_control *wbc)
1028{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029 pgoff_t index, end;
1030 struct pagevec pvec;
1031 int step = ino ? 2 : 0;
1032 int nwritten = 0, wrote = 0;
1033
1034 pagevec_init(&pvec, 0);
1035
1036next_step:
1037 index = 0;
1038 end = LONG_MAX;
1039
1040 while (index <= end) {
1041 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001042 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001043 PAGECACHE_TAG_DIRTY,
1044 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1045 if (nr_pages == 0)
1046 break;
1047
1048 for (i = 0; i < nr_pages; i++) {
1049 struct page *page = pvec.pages[i];
1050
1051 /*
1052 * flushing sequence with step:
1053 * 0. indirect nodes
1054 * 1. dentry dnodes
1055 * 2. file dnodes
1056 */
1057 if (step == 0 && IS_DNODE(page))
1058 continue;
1059 if (step == 1 && (!IS_DNODE(page) ||
1060 is_cold_node(page)))
1061 continue;
1062 if (step == 2 && (!IS_DNODE(page) ||
1063 !is_cold_node(page)))
1064 continue;
1065
1066 /*
1067 * If an fsync mode,
1068 * we should not skip writing node pages.
1069 */
1070 if (ino && ino_of_node(page) == ino)
1071 lock_page(page);
1072 else if (!trylock_page(page))
1073 continue;
1074
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001075 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001076continue_unlock:
1077 unlock_page(page);
1078 continue;
1079 }
1080 if (ino && ino_of_node(page) != ino)
1081 goto continue_unlock;
1082
1083 if (!PageDirty(page)) {
1084 /* someone wrote it for us */
1085 goto continue_unlock;
1086 }
1087
1088 if (!clear_page_dirty_for_io(page))
1089 goto continue_unlock;
1090
1091 /* called by fsync() */
1092 if (ino && IS_DNODE(page)) {
1093 int mark = !is_checkpointed_node(sbi, ino);
1094 set_fsync_mark(page, 1);
1095 if (IS_INODE(page))
1096 set_dentry_mark(page, mark);
1097 nwritten++;
1098 } else {
1099 set_fsync_mark(page, 0);
1100 set_dentry_mark(page, 0);
1101 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001102 NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001103 wrote++;
1104
1105 if (--wbc->nr_to_write == 0)
1106 break;
1107 }
1108 pagevec_release(&pvec);
1109 cond_resched();
1110
1111 if (wbc->nr_to_write == 0) {
1112 step = 2;
1113 break;
1114 }
1115 }
1116
1117 if (step < 2) {
1118 step++;
1119 goto next_step;
1120 }
1121
1122 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001123 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001124 return nwritten;
1125}
1126
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001127int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1128{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001129 pgoff_t index = 0, end = LONG_MAX;
1130 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001131 int ret2 = 0, ret = 0;
1132
1133 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001134
1135 while (index <= end) {
1136 int i, nr_pages;
1137 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1138 PAGECACHE_TAG_WRITEBACK,
1139 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1140 if (nr_pages == 0)
1141 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001142
1143 for (i = 0; i < nr_pages; i++) {
1144 struct page *page = pvec.pages[i];
1145
1146 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001147 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001148 continue;
1149
Chao Yu4bf08ff2013-11-04 10:28:33 +08001150 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001151 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001152 if (TestClearPageError(page))
1153 ret = -EIO;
1154 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001155 }
1156 pagevec_release(&pvec);
1157 cond_resched();
1158 }
1159
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001160 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001161 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001162 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001163 ret2 = -EIO;
1164 if (!ret)
1165 ret = ret2;
1166 return ret;
1167}
1168
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001169static int f2fs_write_node_page(struct page *page,
1170 struct writeback_control *wbc)
1171{
1172 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1173 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001174 block_t new_addr;
1175 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001176 struct f2fs_io_info fio = {
1177 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001178 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001179 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001180
Chao Yucfb271d2013-12-05 17:15:22 +08001181 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001182 goto redirty_out;
1183
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001184 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001185
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001186 /* get old block addr of this node page */
1187 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001188 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001189
1190 get_node_info(sbi, nid, &ni);
1191
1192 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001193 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001194 dec_page_count(sbi, F2FS_DIRTY_NODES);
1195 unlock_page(page);
1196 return 0;
1197 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001198
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001199 if (wbc->for_reclaim)
1200 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001201
Jaegeuk Kim39936832012-11-22 16:21:29 +09001202 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001203 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001204 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001205 set_node_addr(sbi, &ni, new_addr);
1206 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001207 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208 unlock_page(page);
1209 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001210
1211redirty_out:
1212 dec_page_count(sbi, F2FS_DIRTY_NODES);
1213 wbc->pages_skipped++;
Chao Yu9cf3c382014-02-28 10:12:05 +08001214 account_page_redirty(page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001215 set_page_dirty(page);
1216 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001217}
1218
1219static int f2fs_write_node_pages(struct address_space *mapping,
1220 struct writeback_control *wbc)
1221{
1222 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001223 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001224
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001225 /* balancing f2fs's metadata in background */
1226 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001227
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001228 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001229 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001230 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001231
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001232 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001233 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001234 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001235 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001237
1238skip_write:
1239 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1240 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001241}
1242
1243static int f2fs_set_node_page_dirty(struct page *page)
1244{
1245 struct address_space *mapping = page->mapping;
1246 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1247
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001248 trace_f2fs_set_page_dirty(page, NODE);
1249
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001250 SetPageUptodate(page);
1251 if (!PageDirty(page)) {
1252 __set_page_dirty_nobuffers(page);
1253 inc_page_count(sbi, F2FS_DIRTY_NODES);
1254 SetPagePrivate(page);
1255 return 1;
1256 }
1257 return 0;
1258}
1259
Lukas Czernerd47992f2013-05-21 23:17:23 -04001260static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1261 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001262{
1263 struct inode *inode = page->mapping->host;
1264 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1265 if (PageDirty(page))
1266 dec_page_count(sbi, F2FS_DIRTY_NODES);
1267 ClearPagePrivate(page);
1268}
1269
1270static int f2fs_release_node_page(struct page *page, gfp_t wait)
1271{
1272 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001273 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001274}
1275
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001276/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001277 * Structure of the f2fs node operations
1278 */
1279const struct address_space_operations f2fs_node_aops = {
1280 .writepage = f2fs_write_node_page,
1281 .writepages = f2fs_write_node_pages,
1282 .set_page_dirty = f2fs_set_node_page_dirty,
1283 .invalidatepage = f2fs_invalidate_node_page,
1284 .releasepage = f2fs_release_node_page,
1285};
1286
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001287static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1288 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001289{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001290 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291}
1292
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001293static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1294 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001295{
1296 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001297 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001298 kmem_cache_free(free_nid_slab, i);
1299}
1300
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001301static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001302{
1303 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001304 struct nat_entry *ne;
1305 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001306
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001307 if (!available_free_memory(nm_i, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001308 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001309
1310 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001311 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001312 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001313
Gu Zheng7bd59382013-10-22 14:52:26 +08001314 if (build) {
1315 /* do not add allocated nids */
1316 read_lock(&nm_i->nat_tree_lock);
1317 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001318 if (ne &&
1319 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001320 allocated = true;
1321 read_unlock(&nm_i->nat_tree_lock);
1322 if (allocated)
1323 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001324 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001325
1326 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327 i->nid = nid;
1328 i->state = NID_NEW;
1329
1330 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001331 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001332 spin_unlock(&nm_i->free_nid_list_lock);
1333 kmem_cache_free(free_nid_slab, i);
1334 return 0;
1335 }
1336 list_add_tail(&i->list, &nm_i->free_nid_list);
1337 nm_i->fcnt++;
1338 spin_unlock(&nm_i->free_nid_list_lock);
1339 return 1;
1340}
1341
1342static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1343{
1344 struct free_nid *i;
1345 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001346 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001347 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001348 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349 nm_i->fcnt--;
1350 }
1351 spin_unlock(&nm_i->free_nid_list_lock);
1352}
1353
Haicheng Li87609522013-05-06 23:15:42 +08001354static void scan_nat_page(struct f2fs_nm_info *nm_i,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001355 struct page *nat_page, nid_t start_nid)
1356{
1357 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1358 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001359 int i;
1360
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001361 i = start_nid % NAT_ENTRY_PER_BLOCK;
1362
1363 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001364
Chao Yucfb271d2013-12-05 17:15:22 +08001365 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001366 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001367
1368 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001369 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001370 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001371 if (add_free_nid(nm_i, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001372 break;
1373 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001374 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375}
1376
1377static void build_free_nids(struct f2fs_sb_info *sbi)
1378{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001379 struct f2fs_nm_info *nm_i = NM_I(sbi);
1380 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1381 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001382 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001383 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001384
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001385 /* Enough entries */
1386 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1387 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001388
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001389 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001390 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001391
1392 while (1) {
1393 struct page *page = get_current_nat_page(sbi, nid);
1394
Haicheng Li87609522013-05-06 23:15:42 +08001395 scan_nat_page(nm_i, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001396 f2fs_put_page(page, 1);
1397
1398 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001399 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001400 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001401
1402 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001403 break;
1404 }
1405
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001406 /* go to the next free nat pages to find free nids abundantly */
1407 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001408
1409 /* find free nids from current sum_pages */
1410 mutex_lock(&curseg->curseg_mutex);
1411 for (i = 0; i < nats_in_cursum(sum); i++) {
1412 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1413 nid = le32_to_cpu(nid_in_journal(sum, i));
1414 if (addr == NULL_ADDR)
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001415 add_free_nid(nm_i, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001416 else
1417 remove_free_nid(nm_i, nid);
1418 }
1419 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420}
1421
1422/*
1423 * If this function returns success, caller can obtain a new nid
1424 * from second parameter of this function.
1425 * The returned nid could be used ino as well as nid when inode is created.
1426 */
1427bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1428{
1429 struct f2fs_nm_info *nm_i = NM_I(sbi);
1430 struct free_nid *i = NULL;
1431 struct list_head *this;
1432retry:
Chao Yucfb271d2013-12-05 17:15:22 +08001433 if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001434 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001435
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001436 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001437
1438 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001439 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001440 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001441 list_for_each(this, &nm_i->free_nid_list) {
1442 i = list_entry(this, struct free_nid, list);
1443 if (i->state == NID_NEW)
1444 break;
1445 }
1446
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001447 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001448 *nid = i->nid;
1449 i->state = NID_ALLOC;
1450 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001451 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001452 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001453 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001454 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001455
1456 /* Let's scan nat pages and its caches to get free nids */
1457 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001458 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001459 mutex_unlock(&nm_i->build_lock);
1460 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001461}
1462
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001463/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001464 * alloc_nid() should be called prior to this function.
1465 */
1466void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1467{
1468 struct f2fs_nm_info *nm_i = NM_I(sbi);
1469 struct free_nid *i;
1470
1471 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001472 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001473 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001474 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001475 spin_unlock(&nm_i->free_nid_list_lock);
1476}
1477
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001478/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001479 * alloc_nid() should be called prior to this function.
1480 */
1481void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1482{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001483 struct f2fs_nm_info *nm_i = NM_I(sbi);
1484 struct free_nid *i;
1485
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001486 if (!nid)
1487 return;
1488
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001489 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001490 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001491 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001492 if (!available_free_memory(nm_i, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001493 __del_from_free_nid_list(nm_i, i);
Haicheng Li95630cb2013-05-06 23:15:41 +08001494 } else {
1495 i->state = NID_NEW;
1496 nm_i->fcnt++;
1497 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001498 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001499}
1500
1501void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1502 struct f2fs_summary *sum, struct node_info *ni,
1503 block_t new_blkaddr)
1504{
1505 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1506 set_node_addr(sbi, ni, new_blkaddr);
1507 clear_node_page_dirty(page);
1508}
1509
Chao Yu28cdce02014-03-11 13:37:38 +08001510void recover_inline_xattr(struct inode *inode, struct page *page)
1511{
1512 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1513 void *src_addr, *dst_addr;
1514 size_t inline_size;
1515 struct page *ipage;
1516 struct f2fs_inode *ri;
1517
Chao Yu987c7c32014-03-12 15:59:03 +08001518 if (!f2fs_has_inline_xattr(inode))
Chao Yu28cdce02014-03-11 13:37:38 +08001519 return;
1520
1521 if (!IS_INODE(page))
1522 return;
1523
1524 ri = F2FS_INODE(page);
1525 if (!(ri->i_inline & F2FS_INLINE_XATTR))
1526 return;
1527
1528 ipage = get_node_page(sbi, inode->i_ino);
1529 f2fs_bug_on(IS_ERR(ipage));
1530
1531 dst_addr = inline_xattr_addr(ipage);
1532 src_addr = inline_xattr_addr(page);
1533 inline_size = inline_xattr_size(inode);
1534
1535 memcpy(dst_addr, src_addr, inline_size);
1536
1537 update_inode(inode, ipage);
1538 f2fs_put_page(ipage, 1);
1539}
1540
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001541bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1542{
1543 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1544 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1545 nid_t new_xnid = nid_of_node(page);
1546 struct node_info ni;
1547
Chao Yu28cdce02014-03-11 13:37:38 +08001548 recover_inline_xattr(inode, page);
1549
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001550 if (!f2fs_has_xattr_block(ofs_of_node(page)))
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001551 return false;
1552
1553 /* 1: invalidate the previous xattr nid */
1554 if (!prev_xnid)
1555 goto recover_xnid;
1556
1557 /* Deallocate node address */
1558 get_node_info(sbi, prev_xnid, &ni);
1559 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1560 invalidate_blocks(sbi, ni.blk_addr);
1561 dec_valid_node_count(sbi, inode);
1562 set_node_addr(sbi, &ni, NULL_ADDR);
1563
1564recover_xnid:
1565 /* 2: allocate new xattr nid */
1566 if (unlikely(!inc_valid_node_count(sbi, inode)))
1567 f2fs_bug_on(1);
1568
1569 remove_free_nid(NM_I(sbi), new_xnid);
1570 get_node_info(sbi, new_xnid, &ni);
1571 ni.ino = inode->i_ino;
1572 set_node_addr(sbi, &ni, NEW_ADDR);
1573 F2FS_I(inode)->i_xattr_nid = new_xnid;
1574
1575 /* 3: update xattr blkaddr */
1576 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1577 set_node_addr(sbi, &ni, blkaddr);
1578
1579 update_inode_page(inode);
1580 return true;
1581}
1582
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001583int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1584{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001585 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001586 nid_t ino = ino_of_node(page);
1587 struct node_info old_ni, new_ni;
1588 struct page *ipage;
1589
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001590 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001591 if (!ipage)
1592 return -ENOMEM;
1593
1594 /* Should not use this inode from free nid list */
1595 remove_free_nid(NM_I(sbi), ino);
1596
1597 get_node_info(sbi, ino, &old_ni);
1598 SetPageUptodate(ipage);
1599 fill_node_footer(ipage, ino, ino, 0, true);
1600
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001601 src = F2FS_INODE(page);
1602 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001603
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001604 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1605 dst->i_size = 0;
1606 dst->i_blocks = cpu_to_le64(1);
1607 dst->i_links = cpu_to_le32(1);
1608 dst->i_xattr_nid = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001609
1610 new_ni = old_ni;
1611 new_ni.ino = ino;
1612
Chao Yucfb271d2013-12-05 17:15:22 +08001613 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001614 WARN_ON(1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001615 set_node_addr(sbi, &new_ni, NEW_ADDR);
1616 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001617 f2fs_put_page(ipage, 1);
1618 return 0;
1619}
1620
Chao Yu9af0ff12013-11-22 15:48:54 +08001621/*
1622 * ra_sum_pages() merge contiguous pages into one bio and submit.
1623 * these pre-readed pages are linked in pages list.
1624 */
1625static int ra_sum_pages(struct f2fs_sb_info *sbi, struct list_head *pages,
1626 int start, int nrpages)
1627{
1628 struct page *page;
1629 int page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001630 struct f2fs_io_info fio = {
1631 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001632 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001633 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001634
1635 for (; page_idx < start + nrpages; page_idx++) {
1636 /* alloc temporal page for read node summary info*/
Chao Yua0acdfe2013-12-05 09:54:00 +08001637 page = alloc_page(GFP_F2FS_ZERO);
Gu Zhengd6537882014-03-07 18:43:36 +08001638 if (!page)
1639 break;
Chao Yu9af0ff12013-11-22 15:48:54 +08001640
1641 lock_page(page);
1642 page->index = page_idx;
1643 list_add_tail(&page->lru, pages);
1644 }
1645
1646 list_for_each_entry(page, pages, lru)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001647 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001648
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001649 f2fs_submit_merged_bio(sbi, META, READ);
Gu Zhengd6537882014-03-07 18:43:36 +08001650
1651 return page_idx - start;
Chao Yu9af0ff12013-11-22 15:48:54 +08001652}
1653
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001654int restore_node_summary(struct f2fs_sb_info *sbi,
1655 unsigned int segno, struct f2fs_summary_block *sum)
1656{
1657 struct f2fs_node *rn;
1658 struct f2fs_summary *sum_entry;
Chao Yu9af0ff12013-11-22 15:48:54 +08001659 struct page *page, *tmp;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001660 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001661 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1662 int i, last_offset, nrpages, err = 0;
1663 LIST_HEAD(page_list);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001664
1665 /* scan the node segment */
1666 last_offset = sbi->blocks_per_seg;
1667 addr = START_BLOCK(sbi, segno);
1668 sum_entry = &sum->entries[0];
1669
Gu Zhengd6537882014-03-07 18:43:36 +08001670 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001671 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001672
Chao Yu9af0ff12013-11-22 15:48:54 +08001673 /* read ahead node pages */
Gu Zhengd6537882014-03-07 18:43:36 +08001674 nrpages = ra_sum_pages(sbi, &page_list, addr, nrpages);
1675 if (!nrpages)
1676 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001677
Chao Yu9af0ff12013-11-22 15:48:54 +08001678 list_for_each_entry_safe(page, tmp, &page_list, lru) {
Gu Zhengd6537882014-03-07 18:43:36 +08001679 if (err)
1680 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001681
1682 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001683 if (unlikely(!PageUptodate(page))) {
1684 err = -EIO;
1685 } else {
Chao Yu9af0ff12013-11-22 15:48:54 +08001686 rn = F2FS_NODE(page);
1687 sum_entry->nid = rn->footer.nid;
1688 sum_entry->version = 0;
1689 sum_entry->ofs_in_node = 0;
1690 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001691 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001692 unlock_page(page);
Gu Zhengd6537882014-03-07 18:43:36 +08001693skip:
1694 list_del(&page->lru);
Chao Yu9af0ff12013-11-22 15:48:54 +08001695 __free_pages(page, 0);
1696 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001697 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001698 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001699}
1700
1701static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1702{
1703 struct f2fs_nm_info *nm_i = NM_I(sbi);
1704 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1705 struct f2fs_summary_block *sum = curseg->sum_blk;
1706 int i;
1707
1708 mutex_lock(&curseg->curseg_mutex);
1709
1710 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1711 mutex_unlock(&curseg->curseg_mutex);
1712 return false;
1713 }
1714
1715 for (i = 0; i < nats_in_cursum(sum); i++) {
1716 struct nat_entry *ne;
1717 struct f2fs_nat_entry raw_ne;
1718 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1719
1720 raw_ne = nat_in_journal(sum, i);
1721retry:
1722 write_lock(&nm_i->nat_tree_lock);
1723 ne = __lookup_nat_cache(nm_i, nid);
1724 if (ne) {
1725 __set_nat_cache_dirty(nm_i, ne);
1726 write_unlock(&nm_i->nat_tree_lock);
1727 continue;
1728 }
1729 ne = grab_nat_entry(nm_i, nid);
1730 if (!ne) {
1731 write_unlock(&nm_i->nat_tree_lock);
1732 goto retry;
1733 }
1734 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1735 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1736 nat_set_version(ne, raw_ne.version);
1737 __set_nat_cache_dirty(nm_i, ne);
1738 write_unlock(&nm_i->nat_tree_lock);
1739 }
1740 update_nats_in_cursum(sum, -i);
1741 mutex_unlock(&curseg->curseg_mutex);
1742 return true;
1743}
1744
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001745/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001746 * This function is called during the checkpointing process.
1747 */
1748void flush_nat_entries(struct f2fs_sb_info *sbi)
1749{
1750 struct f2fs_nm_info *nm_i = NM_I(sbi);
1751 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1752 struct f2fs_summary_block *sum = curseg->sum_blk;
1753 struct list_head *cur, *n;
1754 struct page *page = NULL;
1755 struct f2fs_nat_block *nat_blk = NULL;
1756 nid_t start_nid = 0, end_nid = 0;
1757 bool flushed;
1758
1759 flushed = flush_nats_in_journal(sbi);
1760
1761 if (!flushed)
1762 mutex_lock(&curseg->curseg_mutex);
1763
1764 /* 1) flush dirty nat caches */
1765 list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1766 struct nat_entry *ne;
1767 nid_t nid;
1768 struct f2fs_nat_entry raw_ne;
1769 int offset = -1;
Jaegeuk Kim2b506382012-12-26 14:39:50 +09001770 block_t new_blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001771
1772 ne = list_entry(cur, struct nat_entry, list);
1773 nid = nat_get_nid(ne);
1774
1775 if (nat_get_blkaddr(ne) == NEW_ADDR)
1776 continue;
1777 if (flushed)
1778 goto to_nat_page;
1779
1780 /* if there is room for nat enries in curseg->sumpage */
1781 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1782 if (offset >= 0) {
1783 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001784 goto flush_now;
1785 }
1786to_nat_page:
1787 if (!page || (start_nid > nid || nid > end_nid)) {
1788 if (page) {
1789 f2fs_put_page(page, 1);
1790 page = NULL;
1791 }
1792 start_nid = START_NID(nid);
1793 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1794
1795 /*
1796 * get nat block with dirty flag, increased reference
1797 * count, mapped and lock
1798 */
1799 page = get_next_nat_page(sbi, start_nid);
1800 nat_blk = page_address(page);
1801 }
1802
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001803 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001804 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001805flush_now:
1806 new_blkaddr = nat_get_blkaddr(ne);
1807
1808 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1809 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1810 raw_ne.version = nat_get_version(ne);
1811
1812 if (offset < 0) {
1813 nat_blk->entries[nid - start_nid] = raw_ne;
1814 } else {
1815 nat_in_journal(sum, offset) = raw_ne;
1816 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1817 }
1818
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001819 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001820 add_free_nid(NM_I(sbi), nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001821 write_lock(&nm_i->nat_tree_lock);
1822 __del_from_nat_cache(nm_i, ne);
1823 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001824 } else {
1825 write_lock(&nm_i->nat_tree_lock);
1826 __clear_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001827 write_unlock(&nm_i->nat_tree_lock);
1828 }
1829 }
1830 if (!flushed)
1831 mutex_unlock(&curseg->curseg_mutex);
1832 f2fs_put_page(page, 1);
1833
1834 /* 2) shrink nat caches if necessary */
1835 try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1836}
1837
1838static int init_node_manager(struct f2fs_sb_info *sbi)
1839{
1840 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1841 struct f2fs_nm_info *nm_i = NM_I(sbi);
1842 unsigned char *version_bitmap;
1843 unsigned int nat_segs, nat_blocks;
1844
1845 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1846
1847 /* segment_count_nat includes pair segment so divide to 2. */
1848 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1849 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001850
1851 /* not used nids: 0, node, meta, (and root counted as valid node) */
1852 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001853 nm_i->fcnt = 0;
1854 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001855 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001856
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001857 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001858 INIT_LIST_HEAD(&nm_i->free_nid_list);
1859 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1860 INIT_LIST_HEAD(&nm_i->nat_entries);
1861 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1862
1863 mutex_init(&nm_i->build_lock);
1864 spin_lock_init(&nm_i->free_nid_list_lock);
1865 rwlock_init(&nm_i->nat_tree_lock);
1866
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001867 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001868 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001869 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1870 if (!version_bitmap)
1871 return -EFAULT;
1872
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001873 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1874 GFP_KERNEL);
1875 if (!nm_i->nat_bitmap)
1876 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001877 return 0;
1878}
1879
1880int build_node_manager(struct f2fs_sb_info *sbi)
1881{
1882 int err;
1883
1884 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1885 if (!sbi->nm_info)
1886 return -ENOMEM;
1887
1888 err = init_node_manager(sbi);
1889 if (err)
1890 return err;
1891
1892 build_free_nids(sbi);
1893 return 0;
1894}
1895
1896void destroy_node_manager(struct f2fs_sb_info *sbi)
1897{
1898 struct f2fs_nm_info *nm_i = NM_I(sbi);
1899 struct free_nid *i, *next_i;
1900 struct nat_entry *natvec[NATVEC_SIZE];
1901 nid_t nid = 0;
1902 unsigned int found;
1903
1904 if (!nm_i)
1905 return;
1906
1907 /* destroy free nid list */
1908 spin_lock(&nm_i->free_nid_list_lock);
1909 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001910 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001911 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912 nm_i->fcnt--;
1913 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001914 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001915 spin_unlock(&nm_i->free_nid_list_lock);
1916
1917 /* destroy nat cache */
1918 write_lock(&nm_i->nat_tree_lock);
1919 while ((found = __gang_lookup_nat_cache(nm_i,
1920 nid, NATVEC_SIZE, natvec))) {
1921 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08001922 nid = nat_get_nid(natvec[found - 1]) + 1;
1923 for (idx = 0; idx < found; idx++)
1924 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001925 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001926 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001927 write_unlock(&nm_i->nat_tree_lock);
1928
1929 kfree(nm_i->nat_bitmap);
1930 sbi->nm_info = NULL;
1931 kfree(nm_i);
1932}
1933
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001934int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001935{
1936 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001937 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001938 if (!nat_entry_slab)
1939 return -ENOMEM;
1940
1941 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08001942 sizeof(struct free_nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001943 if (!free_nid_slab) {
1944 kmem_cache_destroy(nat_entry_slab);
1945 return -ENOMEM;
1946 }
1947 return 0;
1948}
1949
1950void destroy_node_manager_caches(void)
1951{
1952 kmem_cache_destroy(free_nid_slab);
1953 kmem_cache_destroy(nat_entry_slab);
1954}