blob: 9dfb9a042fd295bf20d31cf1bb4d8f280a631368 [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 Kim6fb03f32014-04-16 10:47:06 +090029bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090030{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090031 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090032 struct sysinfo val;
33 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090034 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090035
36 si_meminfo(&val);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090037 /* give 25%, 25%, 50% memory for each components respectively */
38 if (type == FREE_NIDS) {
39 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12;
40 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
41 } else if (type == NAT_ENTRIES) {
42 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12;
43 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
44 } else if (type == DIRTY_DENTS) {
45 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
46 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1);
47 }
48 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090049}
50
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090051static void clear_node_page_dirty(struct page *page)
52{
53 struct address_space *mapping = page->mapping;
54 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
55 unsigned int long flags;
56
57 if (PageDirty(page)) {
58 spin_lock_irqsave(&mapping->tree_lock, flags);
59 radix_tree_tag_clear(&mapping->page_tree,
60 page_index(page),
61 PAGECACHE_TAG_DIRTY);
62 spin_unlock_irqrestore(&mapping->tree_lock, flags);
63
64 clear_page_dirty_for_io(page);
65 dec_page_count(sbi, F2FS_DIRTY_NODES);
66 }
67 ClearPageUptodate(page);
68}
69
70static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
71{
72 pgoff_t index = current_nat_addr(sbi, nid);
73 return get_meta_page(sbi, index);
74}
75
76static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
77{
78 struct page *src_page;
79 struct page *dst_page;
80 pgoff_t src_off;
81 pgoff_t dst_off;
82 void *src_addr;
83 void *dst_addr;
84 struct f2fs_nm_info *nm_i = NM_I(sbi);
85
86 src_off = current_nat_addr(sbi, nid);
87 dst_off = next_nat_addr(sbi, src_off);
88
89 /* get current nat block page with lock */
90 src_page = get_meta_page(sbi, src_off);
91
92 /* Dirty src_page means that it is already the new target NAT page. */
93 if (PageDirty(src_page))
94 return src_page;
95
96 dst_page = grab_meta_page(sbi, dst_off);
97
98 src_addr = page_address(src_page);
99 dst_addr = page_address(dst_page);
100 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
101 set_page_dirty(dst_page);
102 f2fs_put_page(src_page, 1);
103
104 set_to_next_nat(nm_i, nid);
105
106 return dst_page;
107}
108
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900109static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
110{
111 return radix_tree_lookup(&nm_i->nat_root, n);
112}
113
114static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
115 nid_t start, unsigned int nr, struct nat_entry **ep)
116{
117 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
118}
119
120static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
121{
122 list_del(&e->list);
123 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
124 nm_i->nat_cnt--;
125 kmem_cache_free(nat_entry_slab, e);
126}
127
128int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
129{
130 struct f2fs_nm_info *nm_i = NM_I(sbi);
131 struct nat_entry *e;
132 int is_cp = 1;
133
134 read_lock(&nm_i->nat_tree_lock);
135 e = __lookup_nat_cache(nm_i, nid);
136 if (e && !e->checkpointed)
137 is_cp = 0;
138 read_unlock(&nm_i->nat_tree_lock);
139 return is_cp;
140}
141
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900142bool fsync_mark_done(struct f2fs_sb_info *sbi, nid_t nid)
143{
144 struct f2fs_nm_info *nm_i = NM_I(sbi);
145 struct nat_entry *e;
146 bool fsync_done = false;
147
148 read_lock(&nm_i->nat_tree_lock);
149 e = __lookup_nat_cache(nm_i, nid);
150 if (e)
151 fsync_done = e->fsync_done;
152 read_unlock(&nm_i->nat_tree_lock);
153 return fsync_done;
154}
155
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900156void fsync_mark_clear(struct f2fs_sb_info *sbi, nid_t nid)
157{
158 struct f2fs_nm_info *nm_i = NM_I(sbi);
159 struct nat_entry *e;
160
161 write_lock(&nm_i->nat_tree_lock);
162 e = __lookup_nat_cache(nm_i, nid);
163 if (e)
164 e->fsync_done = false;
165 write_unlock(&nm_i->nat_tree_lock);
166}
167
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900168static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
169{
170 struct nat_entry *new;
171
172 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
173 if (!new)
174 return NULL;
175 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
176 kmem_cache_free(nat_entry_slab, new);
177 return NULL;
178 }
179 memset(new, 0, sizeof(struct nat_entry));
180 nat_set_nid(new, nid);
Jaegeuk Kimfffc2a02014-02-21 13:17:22 +0900181 new->checkpointed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900182 list_add_tail(&new->list, &nm_i->nat_entries);
183 nm_i->nat_cnt++;
184 return new;
185}
186
187static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
188 struct f2fs_nat_entry *ne)
189{
190 struct nat_entry *e;
191retry:
192 write_lock(&nm_i->nat_tree_lock);
193 e = __lookup_nat_cache(nm_i, nid);
194 if (!e) {
195 e = grab_nat_entry(nm_i, nid);
196 if (!e) {
197 write_unlock(&nm_i->nat_tree_lock);
198 goto retry;
199 }
Chao Yu94dac222014-04-17 10:51:05 +0800200 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900201 }
202 write_unlock(&nm_i->nat_tree_lock);
203}
204
205static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900206 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900207{
208 struct f2fs_nm_info *nm_i = NM_I(sbi);
209 struct nat_entry *e;
210retry:
211 write_lock(&nm_i->nat_tree_lock);
212 e = __lookup_nat_cache(nm_i, ni->nid);
213 if (!e) {
214 e = grab_nat_entry(nm_i, ni->nid);
215 if (!e) {
216 write_unlock(&nm_i->nat_tree_lock);
217 goto retry;
218 }
219 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900220 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900221 } else if (new_blkaddr == NEW_ADDR) {
222 /*
223 * when nid is reallocated,
224 * previous nat entry can be remained in nat cache.
225 * So, reinitialize it with new information.
226 */
227 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900228 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900229 }
230
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900231 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900232 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
233 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900234 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900235 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900236 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900237 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900238 nat_get_blkaddr(e) != NULL_ADDR &&
239 new_blkaddr == NEW_ADDR);
240
241 /* increament version no as node is removed */
242 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
243 unsigned char version = nat_get_version(e);
244 nat_set_version(e, inc_node_version(version));
245 }
246
247 /* change address */
248 nat_set_blkaddr(e, new_blkaddr);
249 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900250
251 /* update fsync_mark if its inode nat entry is still alive */
252 e = __lookup_nat_cache(nm_i, ni->ino);
253 if (e)
254 e->fsync_done = fsync_done;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900255 write_unlock(&nm_i->nat_tree_lock);
256}
257
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900258int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900259{
260 struct f2fs_nm_info *nm_i = NM_I(sbi);
261
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900262 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900263 return 0;
264
265 write_lock(&nm_i->nat_tree_lock);
266 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
267 struct nat_entry *ne;
268 ne = list_first_entry(&nm_i->nat_entries,
269 struct nat_entry, list);
270 __del_from_nat_cache(nm_i, ne);
271 nr_shrink--;
272 }
273 write_unlock(&nm_i->nat_tree_lock);
274 return nr_shrink;
275}
276
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900277/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900278 * This function returns always success
279 */
280void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
281{
282 struct f2fs_nm_info *nm_i = NM_I(sbi);
283 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
284 struct f2fs_summary_block *sum = curseg->sum_blk;
285 nid_t start_nid = START_NID(nid);
286 struct f2fs_nat_block *nat_blk;
287 struct page *page = NULL;
288 struct f2fs_nat_entry ne;
289 struct nat_entry *e;
290 int i;
291
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900292 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900293 ni->nid = nid;
294
295 /* Check nat cache */
296 read_lock(&nm_i->nat_tree_lock);
297 e = __lookup_nat_cache(nm_i, nid);
298 if (e) {
299 ni->ino = nat_get_ino(e);
300 ni->blk_addr = nat_get_blkaddr(e);
301 ni->version = nat_get_version(e);
302 }
303 read_unlock(&nm_i->nat_tree_lock);
304 if (e)
305 return;
306
307 /* Check current segment summary */
308 mutex_lock(&curseg->curseg_mutex);
309 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
310 if (i >= 0) {
311 ne = nat_in_journal(sum, i);
312 node_info_from_raw_nat(ni, &ne);
313 }
314 mutex_unlock(&curseg->curseg_mutex);
315 if (i >= 0)
316 goto cache;
317
318 /* Fill node_info from nat page */
319 page = get_current_nat_page(sbi, start_nid);
320 nat_blk = (struct f2fs_nat_block *)page_address(page);
321 ne = nat_blk->entries[nid - start_nid];
322 node_info_from_raw_nat(ni, &ne);
323 f2fs_put_page(page, 1);
324cache:
325 /* cache nat entry */
326 cache_nat_entry(NM_I(sbi), nid, &ne);
327}
328
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900329/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900330 * The maximum depth is four.
331 * Offset[0] will have raw inode offset.
332 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900333static int get_node_path(struct f2fs_inode_info *fi, long block,
334 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900335{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900336 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900337 const long direct_blks = ADDRS_PER_BLOCK;
338 const long dptrs_per_blk = NIDS_PER_BLOCK;
339 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
340 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
341 int n = 0;
342 int level = 0;
343
344 noffset[0] = 0;
345
346 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900347 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900348 goto got;
349 }
350 block -= direct_index;
351 if (block < direct_blks) {
352 offset[n++] = NODE_DIR1_BLOCK;
353 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900354 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900355 level = 1;
356 goto got;
357 }
358 block -= direct_blks;
359 if (block < direct_blks) {
360 offset[n++] = NODE_DIR2_BLOCK;
361 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900362 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900363 level = 1;
364 goto got;
365 }
366 block -= direct_blks;
367 if (block < indirect_blks) {
368 offset[n++] = NODE_IND1_BLOCK;
369 noffset[n] = 3;
370 offset[n++] = block / direct_blks;
371 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900372 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900373 level = 2;
374 goto got;
375 }
376 block -= indirect_blks;
377 if (block < indirect_blks) {
378 offset[n++] = NODE_IND2_BLOCK;
379 noffset[n] = 4 + dptrs_per_blk;
380 offset[n++] = block / direct_blks;
381 noffset[n] = 5 + dptrs_per_blk + 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 = 2;
384 goto got;
385 }
386 block -= indirect_blks;
387 if (block < dindirect_blks) {
388 offset[n++] = NODE_DIND_BLOCK;
389 noffset[n] = 5 + (dptrs_per_blk * 2);
390 offset[n++] = block / indirect_blks;
391 noffset[n] = 6 + (dptrs_per_blk * 2) +
392 offset[n - 1] * (dptrs_per_blk + 1);
393 offset[n++] = (block / direct_blks) % dptrs_per_blk;
394 noffset[n] = 7 + (dptrs_per_blk * 2) +
395 offset[n - 2] * (dptrs_per_blk + 1) +
396 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900397 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900398 level = 3;
399 goto got;
400 } else {
401 BUG();
402 }
403got:
404 return level;
405}
406
407/*
408 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800409 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
410 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900411 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900413int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900414{
415 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
416 struct page *npage[4];
417 struct page *parent;
418 int offset[4];
419 unsigned int noffset[4];
420 nid_t nids[4];
421 int level, i;
422 int err = 0;
423
Jaegeuk Kimde936532013-08-12 21:08:03 +0900424 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900425
426 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900427 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900428
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900429 if (!npage[0]) {
430 npage[0] = get_node_page(sbi, nids[0]);
431 if (IS_ERR(npage[0]))
432 return PTR_ERR(npage[0]);
433 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900434 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900435 if (level != 0)
436 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900437 dn->inode_page = npage[0];
438 dn->inode_page_locked = true;
439
440 /* get indirect or direct nodes */
441 for (i = 1; i <= level; i++) {
442 bool done = false;
443
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900444 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900445 /* alloc new node */
446 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900447 err = -ENOSPC;
448 goto release_pages;
449 }
450
451 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900452 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900453 if (IS_ERR(npage[i])) {
454 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900455 err = PTR_ERR(npage[i]);
456 goto release_pages;
457 }
458
459 set_nid(parent, offset[i - 1], nids[i], i == 1);
460 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900461 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900462 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900463 npage[i] = get_node_page_ra(parent, offset[i - 1]);
464 if (IS_ERR(npage[i])) {
465 err = PTR_ERR(npage[i]);
466 goto release_pages;
467 }
468 done = true;
469 }
470 if (i == 1) {
471 dn->inode_page_locked = false;
472 unlock_page(parent);
473 } else {
474 f2fs_put_page(parent, 1);
475 }
476
477 if (!done) {
478 npage[i] = get_node_page(sbi, nids[i]);
479 if (IS_ERR(npage[i])) {
480 err = PTR_ERR(npage[i]);
481 f2fs_put_page(npage[0], 0);
482 goto release_out;
483 }
484 }
485 if (i < level) {
486 parent = npage[i];
487 nids[i + 1] = get_nid(parent, offset[i], false);
488 }
489 }
490 dn->nid = nids[level];
491 dn->ofs_in_node = offset[level];
492 dn->node_page = npage[level];
493 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
494 return 0;
495
496release_pages:
497 f2fs_put_page(parent, 1);
498 if (i > 1)
499 f2fs_put_page(npage[0], 0);
500release_out:
501 dn->inode_page = NULL;
502 dn->node_page = NULL;
503 return err;
504}
505
506static void truncate_node(struct dnode_of_data *dn)
507{
508 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
509 struct node_info ni;
510
511 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900512 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900513 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900514 goto invalidate;
515 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900516 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900517
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900518 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900519 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800520 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900521 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900522
523 if (dn->nid == dn->inode->i_ino) {
524 remove_orphan_inode(sbi, dn->nid);
525 dec_valid_inode_count(sbi);
526 } else {
527 sync_inode_page(dn);
528 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900529invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900530 clear_node_page_dirty(dn->node_page);
531 F2FS_SET_SB_DIRT(sbi);
532
533 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900534
535 invalidate_mapping_pages(NODE_MAPPING(sbi),
536 dn->node_page->index, dn->node_page->index);
537
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900539 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900540}
541
542static int truncate_dnode(struct dnode_of_data *dn)
543{
544 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
545 struct page *page;
546
547 if (dn->nid == 0)
548 return 1;
549
550 /* get direct node */
551 page = get_node_page(sbi, dn->nid);
552 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
553 return 1;
554 else if (IS_ERR(page))
555 return PTR_ERR(page);
556
557 /* Make dnode_of_data for parameter */
558 dn->node_page = page;
559 dn->ofs_in_node = 0;
560 truncate_data_blocks(dn);
561 truncate_node(dn);
562 return 1;
563}
564
565static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
566 int ofs, int depth)
567{
568 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
569 struct dnode_of_data rdn = *dn;
570 struct page *page;
571 struct f2fs_node *rn;
572 nid_t child_nid;
573 unsigned int child_nofs;
574 int freed = 0;
575 int i, ret;
576
577 if (dn->nid == 0)
578 return NIDS_PER_BLOCK + 1;
579
Namjae Jeon51dd6242013-04-20 01:28:52 +0900580 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
581
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900582 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900583 if (IS_ERR(page)) {
584 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900585 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900586 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900587
Gu Zheng45590712013-07-15 17:57:38 +0800588 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900589 if (depth < 3) {
590 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
591 child_nid = le32_to_cpu(rn->in.nid[i]);
592 if (child_nid == 0)
593 continue;
594 rdn.nid = child_nid;
595 ret = truncate_dnode(&rdn);
596 if (ret < 0)
597 goto out_err;
598 set_nid(page, i, 0, false);
599 }
600 } else {
601 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
602 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
603 child_nid = le32_to_cpu(rn->in.nid[i]);
604 if (child_nid == 0) {
605 child_nofs += NIDS_PER_BLOCK + 1;
606 continue;
607 }
608 rdn.nid = child_nid;
609 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
610 if (ret == (NIDS_PER_BLOCK + 1)) {
611 set_nid(page, i, 0, false);
612 child_nofs += ret;
613 } else if (ret < 0 && ret != -ENOENT) {
614 goto out_err;
615 }
616 }
617 freed = child_nofs;
618 }
619
620 if (!ofs) {
621 /* remove current indirect node */
622 dn->node_page = page;
623 truncate_node(dn);
624 freed++;
625 } else {
626 f2fs_put_page(page, 1);
627 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900628 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900629 return freed;
630
631out_err:
632 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900633 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900634 return ret;
635}
636
637static int truncate_partial_nodes(struct dnode_of_data *dn,
638 struct f2fs_inode *ri, int *offset, int depth)
639{
640 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
641 struct page *pages[2];
642 nid_t nid[3];
643 nid_t child_nid;
644 int err = 0;
645 int i;
646 int idx = depth - 2;
647
648 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
649 if (!nid[0])
650 return 0;
651
652 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800653 for (i = 0; i < idx + 1; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900654 /* refernece count'll be increased */
655 pages[i] = get_node_page(sbi, nid[i]);
656 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900657 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800658 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900659 goto fail;
660 }
661 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
662 }
663
664 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800665 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900666 child_nid = get_nid(pages[idx], i, false);
667 if (!child_nid)
668 continue;
669 dn->nid = child_nid;
670 err = truncate_dnode(dn);
671 if (err < 0)
672 goto fail;
673 set_nid(pages[idx], i, 0, false);
674 }
675
shifei10.gea225dca2013-10-29 15:32:34 +0800676 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900677 dn->node_page = pages[idx];
678 dn->nid = nid[idx];
679 truncate_node(dn);
680 } else {
681 f2fs_put_page(pages[idx], 1);
682 }
683 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800684 offset[idx + 1] = 0;
685 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900686fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800687 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900688 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900689
690 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
691
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900692 return err;
693}
694
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900695/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900696 * All the block addresses of data and nodes should be nullified.
697 */
698int truncate_inode_blocks(struct inode *inode, pgoff_t from)
699{
700 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
701 int err = 0, cont = 1;
702 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900703 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900704 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900705 struct dnode_of_data dn;
706 struct page *page;
707
Namjae Jeon51dd6242013-04-20 01:28:52 +0900708 trace_f2fs_truncate_inode_blocks_enter(inode, from);
709
Jaegeuk Kimde936532013-08-12 21:08:03 +0900710 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900711restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900712 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900713 if (IS_ERR(page)) {
714 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900715 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900716 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900717
718 set_new_dnode(&dn, inode, page, NULL, 0);
719 unlock_page(page);
720
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900721 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900722 switch (level) {
723 case 0:
724 case 1:
725 nofs = noffset[1];
726 break;
727 case 2:
728 nofs = noffset[1];
729 if (!offset[level - 1])
730 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900731 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732 if (err < 0 && err != -ENOENT)
733 goto fail;
734 nofs += 1 + NIDS_PER_BLOCK;
735 break;
736 case 3:
737 nofs = 5 + 2 * NIDS_PER_BLOCK;
738 if (!offset[level - 1])
739 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900740 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900741 if (err < 0 && err != -ENOENT)
742 goto fail;
743 break;
744 default:
745 BUG();
746 }
747
748skip_partial:
749 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900750 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900751 switch (offset[0]) {
752 case NODE_DIR1_BLOCK:
753 case NODE_DIR2_BLOCK:
754 err = truncate_dnode(&dn);
755 break;
756
757 case NODE_IND1_BLOCK:
758 case NODE_IND2_BLOCK:
759 err = truncate_nodes(&dn, nofs, offset[1], 2);
760 break;
761
762 case NODE_DIND_BLOCK:
763 err = truncate_nodes(&dn, nofs, offset[1], 3);
764 cont = 0;
765 break;
766
767 default:
768 BUG();
769 }
770 if (err < 0 && err != -ENOENT)
771 goto fail;
772 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900773 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900774 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900775 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900776 f2fs_put_page(page, 1);
777 goto restart;
778 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900779 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900780 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900781 set_page_dirty(page);
782 unlock_page(page);
783 }
784 offset[1] = 0;
785 offset[0]++;
786 nofs += err;
787 }
788fail:
789 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900790 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900791 return err > 0 ? 0 : err;
792}
793
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900794int truncate_xattr_node(struct inode *inode, struct page *page)
795{
796 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
797 nid_t nid = F2FS_I(inode)->i_xattr_nid;
798 struct dnode_of_data dn;
799 struct page *npage;
800
801 if (!nid)
802 return 0;
803
804 npage = get_node_page(sbi, nid);
805 if (IS_ERR(npage))
806 return PTR_ERR(npage);
807
808 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900809
810 /* need to do checkpoint during fsync */
811 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
812
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900813 set_new_dnode(&dn, inode, page, npage, nid);
814
815 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800816 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900817 truncate_node(&dn);
818 return 0;
819}
820
Jaegeuk Kim39936832012-11-22 16:21:29 +0900821/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800822 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
823 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900824 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800825void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900826{
827 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
828 struct page *page;
829 nid_t ino = inode->i_ino;
830 struct dnode_of_data dn;
831
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900833 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800834 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900835
Gu Zheng58e674d2013-11-19 18:03:18 +0800836 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900837 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800838 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900839 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900840 /* 0 is possible, after f2fs_new_inode() is failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900841 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900842 set_new_dnode(&dn, inode, page, page, ino);
843 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844}
845
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900846struct page *new_inode_page(struct inode *inode, const struct qstr *name)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900847{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848 struct dnode_of_data dn;
849
850 /* allocate inode page for new inode */
851 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900852
853 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900854 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855}
856
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900857struct page *new_node_page(struct dnode_of_data *dn,
858 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900859{
860 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900861 struct node_info old_ni, new_ni;
862 struct page *page;
863 int err;
864
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900865 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900866 return ERR_PTR(-EPERM);
867
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900868 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900869 if (!page)
870 return ERR_PTR(-ENOMEM);
871
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900872 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900873 err = -ENOSPC;
874 goto fail;
875 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900876
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900877 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900878
879 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900880 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900881 new_ni = old_ni;
882 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900883 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900884
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900885 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900886 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900887 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900888 SetPageUptodate(page);
889 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900890
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800891 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900892 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
893
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900894 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900895 if (ipage)
896 update_inode(dn->inode, ipage);
897 else
898 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900899 if (ofs == 0)
900 inc_valid_inode_count(sbi);
901
902 return page;
903
904fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900905 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906 f2fs_put_page(page, 1);
907 return ERR_PTR(err);
908}
909
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900910/*
911 * Caller should do after getting the following values.
912 * 0: f2fs_put_page(page, 0)
913 * LOCKED_PAGE: f2fs_put_page(page, 1)
914 * error: nothing
915 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900916static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900917{
918 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
919 struct node_info ni;
920
921 get_node_info(sbi, page->index, &ni);
922
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900923 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900924 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900925 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900926 }
927
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900928 if (PageUptodate(page))
929 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900930
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900931 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900932}
933
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900934/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900935 * Readahead a node page
936 */
937void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
938{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900939 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900940 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900941
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900942 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900943 if (apage && PageUptodate(apage)) {
944 f2fs_put_page(apage, 0);
945 return;
946 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900947 f2fs_put_page(apage, 0);
948
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900949 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900950 if (!apage)
951 return;
952
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900953 err = read_node_page(apage, READA);
954 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900955 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900956 else if (err == LOCKED_PAGE)
957 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958}
959
960struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
961{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900962 struct page *page;
963 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900964repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900965 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900966 if (!page)
967 return ERR_PTR(-ENOMEM);
968
969 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900970 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900971 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900972 else if (err == LOCKED_PAGE)
973 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900974
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900975 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +0900976 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900977 f2fs_put_page(page, 1);
978 return ERR_PTR(-EIO);
979 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900980 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900981 f2fs_put_page(page, 1);
982 goto repeat;
983 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900984got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985 return page;
986}
987
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900988/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900989 * Return a locked page for the desired node page.
990 * And, readahead MAX_RA_NODE number of node pages.
991 */
992struct page *get_node_page_ra(struct page *parent, int start)
993{
994 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900995 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900996 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900997 int err, i, end;
998 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999
1000 /* First, try getting the desired direct node. */
1001 nid = get_nid(parent, start, false);
1002 if (!nid)
1003 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001004repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001005 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006 if (!page)
1007 return ERR_PTR(-ENOMEM);
1008
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001009 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001010 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001011 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001012 else if (err == LOCKED_PAGE)
1013 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001014
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001015 blk_start_plug(&plug);
1016
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017 /* Then, try readahead for siblings of the desired node */
1018 end = start + MAX_RA_NODE;
1019 end = min(end, NIDS_PER_BLOCK);
1020 for (i = start + 1; i < end; i++) {
1021 nid = get_nid(parent, i, false);
1022 if (!nid)
1023 continue;
1024 ra_node_page(sbi, nid);
1025 }
1026
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001027 blk_finish_plug(&plug);
1028
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001030 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001031 f2fs_put_page(page, 1);
1032 goto repeat;
1033 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001034page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001035 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001036 f2fs_put_page(page, 1);
1037 return ERR_PTR(-EIO);
1038 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001039 return page;
1040}
1041
1042void sync_inode_page(struct dnode_of_data *dn)
1043{
1044 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1045 update_inode(dn->inode, dn->node_page);
1046 } else if (dn->inode_page) {
1047 if (!dn->inode_page_locked)
1048 lock_page(dn->inode_page);
1049 update_inode(dn->inode, dn->inode_page);
1050 if (!dn->inode_page_locked)
1051 unlock_page(dn->inode_page);
1052 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001053 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001054 }
1055}
1056
1057int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1058 struct writeback_control *wbc)
1059{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001060 pgoff_t index, end;
1061 struct pagevec pvec;
1062 int step = ino ? 2 : 0;
1063 int nwritten = 0, wrote = 0;
1064
1065 pagevec_init(&pvec, 0);
1066
1067next_step:
1068 index = 0;
1069 end = LONG_MAX;
1070
1071 while (index <= end) {
1072 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001073 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074 PAGECACHE_TAG_DIRTY,
1075 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1076 if (nr_pages == 0)
1077 break;
1078
1079 for (i = 0; i < nr_pages; i++) {
1080 struct page *page = pvec.pages[i];
1081
1082 /*
1083 * flushing sequence with step:
1084 * 0. indirect nodes
1085 * 1. dentry dnodes
1086 * 2. file dnodes
1087 */
1088 if (step == 0 && IS_DNODE(page))
1089 continue;
1090 if (step == 1 && (!IS_DNODE(page) ||
1091 is_cold_node(page)))
1092 continue;
1093 if (step == 2 && (!IS_DNODE(page) ||
1094 !is_cold_node(page)))
1095 continue;
1096
1097 /*
1098 * If an fsync mode,
1099 * we should not skip writing node pages.
1100 */
1101 if (ino && ino_of_node(page) == ino)
1102 lock_page(page);
1103 else if (!trylock_page(page))
1104 continue;
1105
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001106 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001107continue_unlock:
1108 unlock_page(page);
1109 continue;
1110 }
1111 if (ino && ino_of_node(page) != ino)
1112 goto continue_unlock;
1113
1114 if (!PageDirty(page)) {
1115 /* someone wrote it for us */
1116 goto continue_unlock;
1117 }
1118
1119 if (!clear_page_dirty_for_io(page))
1120 goto continue_unlock;
1121
1122 /* called by fsync() */
1123 if (ino && IS_DNODE(page)) {
1124 int mark = !is_checkpointed_node(sbi, ino);
1125 set_fsync_mark(page, 1);
1126 if (IS_INODE(page))
1127 set_dentry_mark(page, mark);
1128 nwritten++;
1129 } else {
1130 set_fsync_mark(page, 0);
1131 set_dentry_mark(page, 0);
1132 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001133 NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001134 wrote++;
1135
1136 if (--wbc->nr_to_write == 0)
1137 break;
1138 }
1139 pagevec_release(&pvec);
1140 cond_resched();
1141
1142 if (wbc->nr_to_write == 0) {
1143 step = 2;
1144 break;
1145 }
1146 }
1147
1148 if (step < 2) {
1149 step++;
1150 goto next_step;
1151 }
1152
1153 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001154 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001155 return nwritten;
1156}
1157
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001158int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1159{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001160 pgoff_t index = 0, end = LONG_MAX;
1161 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001162 int ret2 = 0, ret = 0;
1163
1164 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001165
1166 while (index <= end) {
1167 int i, nr_pages;
1168 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1169 PAGECACHE_TAG_WRITEBACK,
1170 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1171 if (nr_pages == 0)
1172 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001173
1174 for (i = 0; i < nr_pages; i++) {
1175 struct page *page = pvec.pages[i];
1176
1177 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001178 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001179 continue;
1180
Chao Yu4bf08ff2013-11-04 10:28:33 +08001181 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001182 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001183 if (TestClearPageError(page))
1184 ret = -EIO;
1185 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001186 }
1187 pagevec_release(&pvec);
1188 cond_resched();
1189 }
1190
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001191 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001192 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001193 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001194 ret2 = -EIO;
1195 if (!ret)
1196 ret = ret2;
1197 return ret;
1198}
1199
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001200static int f2fs_write_node_page(struct page *page,
1201 struct writeback_control *wbc)
1202{
1203 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1204 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001205 block_t new_addr;
1206 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001207 struct f2fs_io_info fio = {
1208 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001209 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001210 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001211
Chao Yuecda0de2014-05-06 16:48:26 +08001212 trace_f2fs_writepage(page, NODE);
1213
Chao Yucfb271d2013-12-05 17:15:22 +08001214 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001215 goto redirty_out;
1216
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001217 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001218
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001219 /* get old block addr of this node page */
1220 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001221 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001222
1223 get_node_info(sbi, nid, &ni);
1224
1225 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001226 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001227 dec_page_count(sbi, F2FS_DIRTY_NODES);
1228 unlock_page(page);
1229 return 0;
1230 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001231
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001232 if (wbc->for_reclaim)
1233 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001234
Jaegeuk Kim39936832012-11-22 16:21:29 +09001235 mutex_lock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001237 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001238 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001239 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001240 mutex_unlock(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001241 unlock_page(page);
1242 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001243
1244redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001245 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001246 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001247}
1248
1249static int f2fs_write_node_pages(struct address_space *mapping,
1250 struct writeback_control *wbc)
1251{
1252 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001253 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001254
Chao Yue5748432014-05-06 16:51:24 +08001255 trace_f2fs_writepages(mapping->host, wbc, NODE);
1256
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001257 /* balancing f2fs's metadata in background */
1258 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001259
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001260 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001261 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001262 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001263
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001264 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001265 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001266 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001267 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001268 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001269
1270skip_write:
1271 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1272 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001273}
1274
1275static int f2fs_set_node_page_dirty(struct page *page)
1276{
1277 struct address_space *mapping = page->mapping;
1278 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1279
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001280 trace_f2fs_set_page_dirty(page, NODE);
1281
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001282 SetPageUptodate(page);
1283 if (!PageDirty(page)) {
1284 __set_page_dirty_nobuffers(page);
1285 inc_page_count(sbi, F2FS_DIRTY_NODES);
1286 SetPagePrivate(page);
1287 return 1;
1288 }
1289 return 0;
1290}
1291
Lukas Czernerd47992f2013-05-21 23:17:23 -04001292static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1293 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294{
1295 struct inode *inode = page->mapping->host;
1296 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1297 if (PageDirty(page))
1298 dec_page_count(sbi, F2FS_DIRTY_NODES);
1299 ClearPagePrivate(page);
1300}
1301
1302static int f2fs_release_node_page(struct page *page, gfp_t wait)
1303{
1304 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001305 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001306}
1307
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001308/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001309 * Structure of the f2fs node operations
1310 */
1311const struct address_space_operations f2fs_node_aops = {
1312 .writepage = f2fs_write_node_page,
1313 .writepages = f2fs_write_node_pages,
1314 .set_page_dirty = f2fs_set_node_page_dirty,
1315 .invalidatepage = f2fs_invalidate_node_page,
1316 .releasepage = f2fs_release_node_page,
1317};
1318
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001319static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1320 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001321{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001322 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323}
1324
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001325static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1326 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327{
1328 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001329 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001330}
1331
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001332static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001334 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001335 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001336 struct nat_entry *ne;
1337 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001338
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001339 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001340 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001341
1342 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001343 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001344 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001345
Gu Zheng7bd59382013-10-22 14:52:26 +08001346 if (build) {
1347 /* do not add allocated nids */
1348 read_lock(&nm_i->nat_tree_lock);
1349 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001350 if (ne &&
1351 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001352 allocated = true;
1353 read_unlock(&nm_i->nat_tree_lock);
1354 if (allocated)
1355 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001356 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001357
1358 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001359 i->nid = nid;
1360 i->state = NID_NEW;
1361
1362 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001363 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001364 spin_unlock(&nm_i->free_nid_list_lock);
1365 kmem_cache_free(free_nid_slab, i);
1366 return 0;
1367 }
1368 list_add_tail(&i->list, &nm_i->free_nid_list);
1369 nm_i->fcnt++;
1370 spin_unlock(&nm_i->free_nid_list_lock);
1371 return 1;
1372}
1373
1374static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1375{
1376 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001377 bool need_free = false;
1378
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001379 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001380 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001382 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001383 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001384 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001385 }
1386 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001387
1388 if (need_free)
1389 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001390}
1391
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001392static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001393 struct page *nat_page, nid_t start_nid)
1394{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001395 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001396 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1397 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001398 int i;
1399
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001400 i = start_nid % NAT_ENTRY_PER_BLOCK;
1401
1402 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001403
Chao Yucfb271d2013-12-05 17:15:22 +08001404 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001405 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001406
1407 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001408 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001409 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001410 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001411 break;
1412 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001413 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001414}
1415
1416static void build_free_nids(struct f2fs_sb_info *sbi)
1417{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001418 struct f2fs_nm_info *nm_i = NM_I(sbi);
1419 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1420 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001421 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001422 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001423
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001424 /* Enough entries */
1425 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1426 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001427
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001428 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001429 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001430
1431 while (1) {
1432 struct page *page = get_current_nat_page(sbi, nid);
1433
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001434 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001435 f2fs_put_page(page, 1);
1436
1437 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001438 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001439 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001440
1441 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001442 break;
1443 }
1444
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001445 /* go to the next free nat pages to find free nids abundantly */
1446 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001447
1448 /* find free nids from current sum_pages */
1449 mutex_lock(&curseg->curseg_mutex);
1450 for (i = 0; i < nats_in_cursum(sum); i++) {
1451 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1452 nid = le32_to_cpu(nid_in_journal(sum, i));
1453 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001454 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001455 else
1456 remove_free_nid(nm_i, nid);
1457 }
1458 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459}
1460
1461/*
1462 * If this function returns success, caller can obtain a new nid
1463 * from second parameter of this function.
1464 * The returned nid could be used ino as well as nid when inode is created.
1465 */
1466bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1467{
1468 struct f2fs_nm_info *nm_i = NM_I(sbi);
1469 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001470retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001471 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001472 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001473
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001474 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001475
1476 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001477 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001478 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001479 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001480 if (i->state == NID_NEW)
1481 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001482
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001483 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001484 *nid = i->nid;
1485 i->state = NID_ALLOC;
1486 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001487 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001488 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001489 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001490 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001491
1492 /* Let's scan nat pages and its caches to get free nids */
1493 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001494 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001495 mutex_unlock(&nm_i->build_lock);
1496 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001497}
1498
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001499/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001500 * alloc_nid() should be called prior to this function.
1501 */
1502void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1503{
1504 struct f2fs_nm_info *nm_i = NM_I(sbi);
1505 struct free_nid *i;
1506
1507 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001508 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001509 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001510 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001511 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001512
1513 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001514}
1515
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001516/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001517 * alloc_nid() should be called prior to this function.
1518 */
1519void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1520{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001521 struct f2fs_nm_info *nm_i = NM_I(sbi);
1522 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001523 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001524
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001525 if (!nid)
1526 return;
1527
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001528 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001529 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001530 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001531 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001532 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001533 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001534 } else {
1535 i->state = NID_NEW;
1536 nm_i->fcnt++;
1537 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001538 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001539
1540 if (need_free)
1541 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001542}
1543
1544void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1545 struct f2fs_summary *sum, struct node_info *ni,
1546 block_t new_blkaddr)
1547{
1548 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001549 set_node_addr(sbi, ni, new_blkaddr, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001550 clear_node_page_dirty(page);
1551}
1552
Jingoo Hanb156d542014-04-15 17:51:05 +09001553static void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001554{
1555 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1556 void *src_addr, *dst_addr;
1557 size_t inline_size;
1558 struct page *ipage;
1559 struct f2fs_inode *ri;
1560
Chao Yu987c7c32014-03-12 15:59:03 +08001561 if (!f2fs_has_inline_xattr(inode))
Chao Yu28cdce02014-03-11 13:37:38 +08001562 return;
1563
1564 if (!IS_INODE(page))
1565 return;
1566
1567 ri = F2FS_INODE(page);
1568 if (!(ri->i_inline & F2FS_INLINE_XATTR))
1569 return;
1570
1571 ipage = get_node_page(sbi, inode->i_ino);
1572 f2fs_bug_on(IS_ERR(ipage));
1573
1574 dst_addr = inline_xattr_addr(ipage);
1575 src_addr = inline_xattr_addr(page);
1576 inline_size = inline_xattr_size(inode);
1577
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001578 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001579 memcpy(dst_addr, src_addr, inline_size);
1580
1581 update_inode(inode, ipage);
1582 f2fs_put_page(ipage, 1);
1583}
1584
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001585bool recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1586{
1587 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1588 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1589 nid_t new_xnid = nid_of_node(page);
1590 struct node_info ni;
1591
Chao Yu28cdce02014-03-11 13:37:38 +08001592 recover_inline_xattr(inode, page);
1593
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001594 if (!f2fs_has_xattr_block(ofs_of_node(page)))
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001595 return false;
1596
1597 /* 1: invalidate the previous xattr nid */
1598 if (!prev_xnid)
1599 goto recover_xnid;
1600
1601 /* Deallocate node address */
1602 get_node_info(sbi, prev_xnid, &ni);
1603 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1604 invalidate_blocks(sbi, ni.blk_addr);
1605 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001606 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001607
1608recover_xnid:
1609 /* 2: allocate new xattr nid */
1610 if (unlikely(!inc_valid_node_count(sbi, inode)))
1611 f2fs_bug_on(1);
1612
1613 remove_free_nid(NM_I(sbi), new_xnid);
1614 get_node_info(sbi, new_xnid, &ni);
1615 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001616 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001617 F2FS_I(inode)->i_xattr_nid = new_xnid;
1618
1619 /* 3: update xattr blkaddr */
1620 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001621 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001622
1623 update_inode_page(inode);
1624 return true;
1625}
1626
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001627int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1628{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001629 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001630 nid_t ino = ino_of_node(page);
1631 struct node_info old_ni, new_ni;
1632 struct page *ipage;
1633
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001634 get_node_info(sbi, ino, &old_ni);
1635
1636 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1637 return -EINVAL;
1638
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001639 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001640 if (!ipage)
1641 return -ENOMEM;
1642
1643 /* Should not use this inode from free nid list */
1644 remove_free_nid(NM_I(sbi), ino);
1645
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001646 SetPageUptodate(ipage);
1647 fill_node_footer(ipage, ino, ino, 0, true);
1648
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001649 src = F2FS_INODE(page);
1650 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001651
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001652 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1653 dst->i_size = 0;
1654 dst->i_blocks = cpu_to_le64(1);
1655 dst->i_links = cpu_to_le32(1);
1656 dst->i_xattr_nid = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001657
1658 new_ni = old_ni;
1659 new_ni.ino = ino;
1660
Chao Yucfb271d2013-12-05 17:15:22 +08001661 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001662 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001663 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001664 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001665 f2fs_put_page(ipage, 1);
1666 return 0;
1667}
1668
Chao Yu9af0ff12013-11-22 15:48:54 +08001669/*
1670 * ra_sum_pages() merge contiguous pages into one bio and submit.
Chao Yubac4eef2014-05-27 08:41:07 +08001671 * these pre-readed pages are alloced in bd_inode's mapping tree.
Chao Yu9af0ff12013-11-22 15:48:54 +08001672 */
Chao Yubac4eef2014-05-27 08:41:07 +08001673static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages,
Chao Yu9af0ff12013-11-22 15:48:54 +08001674 int start, int nrpages)
1675{
Chao Yubac4eef2014-05-27 08:41:07 +08001676 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1677 struct address_space *mapping = inode->i_mapping;
1678 int i, page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001679 struct f2fs_io_info fio = {
1680 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001681 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001682 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001683
Chao Yubac4eef2014-05-27 08:41:07 +08001684 for (i = 0; page_idx < start + nrpages; page_idx++, i++) {
1685 /* alloc page in bd_inode for reading node summary info */
1686 pages[i] = grab_cache_page(mapping, page_idx);
1687 if (!pages[i])
Gu Zhengd6537882014-03-07 18:43:36 +08001688 break;
Chao Yubac4eef2014-05-27 08:41:07 +08001689 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001690 }
1691
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001692 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yubac4eef2014-05-27 08:41:07 +08001693 return i;
Chao Yu9af0ff12013-11-22 15:48:54 +08001694}
1695
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001696int restore_node_summary(struct f2fs_sb_info *sbi,
1697 unsigned int segno, struct f2fs_summary_block *sum)
1698{
1699 struct f2fs_node *rn;
1700 struct f2fs_summary *sum_entry;
Chao Yubac4eef2014-05-27 08:41:07 +08001701 struct inode *inode = sbi->sb->s_bdev->bd_inode;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001702 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001703 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Chao Yubac4eef2014-05-27 08:41:07 +08001704 struct page *pages[bio_blocks];
1705 int i, idx, last_offset, nrpages, err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001706
1707 /* scan the node segment */
1708 last_offset = sbi->blocks_per_seg;
1709 addr = START_BLOCK(sbi, segno);
1710 sum_entry = &sum->entries[0];
1711
Gu Zhengd6537882014-03-07 18:43:36 +08001712 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001713 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001714
Chao Yu9af0ff12013-11-22 15:48:54 +08001715 /* read ahead node pages */
Chao Yubac4eef2014-05-27 08:41:07 +08001716 nrpages = ra_sum_pages(sbi, pages, addr, nrpages);
Gu Zhengd6537882014-03-07 18:43:36 +08001717 if (!nrpages)
1718 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001719
Chao Yubac4eef2014-05-27 08:41:07 +08001720 for (idx = 0; idx < nrpages; idx++) {
Gu Zhengd6537882014-03-07 18:43:36 +08001721 if (err)
1722 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001723
Chao Yubac4eef2014-05-27 08:41:07 +08001724 lock_page(pages[idx]);
1725 if (unlikely(!PageUptodate(pages[idx]))) {
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001726 err = -EIO;
1727 } else {
Chao Yubac4eef2014-05-27 08:41:07 +08001728 rn = F2FS_NODE(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001729 sum_entry->nid = rn->footer.nid;
1730 sum_entry->version = 0;
1731 sum_entry->ofs_in_node = 0;
1732 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001733 }
Chao Yubac4eef2014-05-27 08:41:07 +08001734 unlock_page(pages[idx]);
Gu Zhengd6537882014-03-07 18:43:36 +08001735skip:
Chao Yubac4eef2014-05-27 08:41:07 +08001736 page_cache_release(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001737 }
Chao Yubac4eef2014-05-27 08:41:07 +08001738
1739 invalidate_mapping_pages(inode->i_mapping, addr,
1740 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001741 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001742 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001743}
1744
1745static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1746{
1747 struct f2fs_nm_info *nm_i = NM_I(sbi);
1748 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1749 struct f2fs_summary_block *sum = curseg->sum_blk;
1750 int i;
1751
1752 mutex_lock(&curseg->curseg_mutex);
1753
1754 if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1755 mutex_unlock(&curseg->curseg_mutex);
1756 return false;
1757 }
1758
1759 for (i = 0; i < nats_in_cursum(sum); i++) {
1760 struct nat_entry *ne;
1761 struct f2fs_nat_entry raw_ne;
1762 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1763
1764 raw_ne = nat_in_journal(sum, i);
1765retry:
1766 write_lock(&nm_i->nat_tree_lock);
1767 ne = __lookup_nat_cache(nm_i, nid);
1768 if (ne) {
1769 __set_nat_cache_dirty(nm_i, ne);
1770 write_unlock(&nm_i->nat_tree_lock);
1771 continue;
1772 }
1773 ne = grab_nat_entry(nm_i, nid);
1774 if (!ne) {
1775 write_unlock(&nm_i->nat_tree_lock);
1776 goto retry;
1777 }
Chao Yu94dac222014-04-17 10:51:05 +08001778 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001779 __set_nat_cache_dirty(nm_i, ne);
1780 write_unlock(&nm_i->nat_tree_lock);
1781 }
1782 update_nats_in_cursum(sum, -i);
1783 mutex_unlock(&curseg->curseg_mutex);
1784 return true;
1785}
1786
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001787/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001788 * This function is called during the checkpointing process.
1789 */
1790void flush_nat_entries(struct f2fs_sb_info *sbi)
1791{
1792 struct f2fs_nm_info *nm_i = NM_I(sbi);
1793 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1794 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yu2d7b8222014-03-29 11:33:17 +08001795 struct nat_entry *ne, *cur;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001796 struct page *page = NULL;
1797 struct f2fs_nat_block *nat_blk = NULL;
1798 nid_t start_nid = 0, end_nid = 0;
1799 bool flushed;
1800
1801 flushed = flush_nats_in_journal(sbi);
1802
1803 if (!flushed)
1804 mutex_lock(&curseg->curseg_mutex);
1805
1806 /* 1) flush dirty nat caches */
Chao Yu2d7b8222014-03-29 11:33:17 +08001807 list_for_each_entry_safe(ne, cur, &nm_i->dirty_nat_entries, list) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001808 nid_t nid;
1809 struct f2fs_nat_entry raw_ne;
1810 int offset = -1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001811
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001812 if (nat_get_blkaddr(ne) == NEW_ADDR)
1813 continue;
Chao Yu2d7b8222014-03-29 11:33:17 +08001814
1815 nid = nat_get_nid(ne);
1816
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001817 if (flushed)
1818 goto to_nat_page;
1819
1820 /* if there is room for nat enries in curseg->sumpage */
1821 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1822 if (offset >= 0) {
1823 raw_ne = nat_in_journal(sum, offset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001824 goto flush_now;
1825 }
1826to_nat_page:
1827 if (!page || (start_nid > nid || nid > end_nid)) {
1828 if (page) {
1829 f2fs_put_page(page, 1);
1830 page = NULL;
1831 }
1832 start_nid = START_NID(nid);
1833 end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1834
1835 /*
1836 * get nat block with dirty flag, increased reference
1837 * count, mapped and lock
1838 */
1839 page = get_next_nat_page(sbi, start_nid);
1840 nat_blk = page_address(page);
1841 }
1842
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001843 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001844 raw_ne = nat_blk->entries[nid - start_nid];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001845flush_now:
Chao Yu94dac222014-04-17 10:51:05 +08001846 raw_nat_from_node_info(&raw_ne, &ne->ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001847
1848 if (offset < 0) {
1849 nat_blk->entries[nid - start_nid] = raw_ne;
1850 } else {
1851 nat_in_journal(sum, offset) = raw_ne;
1852 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1853 }
1854
Jaegeuk Kimfa372412013-03-21 12:53:19 +09001855 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001856 add_free_nid(sbi, nid, false) <= 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001857 write_lock(&nm_i->nat_tree_lock);
1858 __del_from_nat_cache(nm_i, ne);
1859 write_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001860 } else {
1861 write_lock(&nm_i->nat_tree_lock);
1862 __clear_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001863 write_unlock(&nm_i->nat_tree_lock);
1864 }
1865 }
1866 if (!flushed)
1867 mutex_unlock(&curseg->curseg_mutex);
1868 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001869}
1870
1871static int init_node_manager(struct f2fs_sb_info *sbi)
1872{
1873 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1874 struct f2fs_nm_info *nm_i = NM_I(sbi);
1875 unsigned char *version_bitmap;
1876 unsigned int nat_segs, nat_blocks;
1877
1878 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1879
1880 /* segment_count_nat includes pair segment so divide to 2. */
1881 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1882 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001883
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001884 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1885
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001886 /* not used nids: 0, node, meta, (and root counted as valid node) */
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001887 nm_i->available_nids = nm_i->max_nid - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001888 nm_i->fcnt = 0;
1889 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001890 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001891
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001892 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001893 INIT_LIST_HEAD(&nm_i->free_nid_list);
1894 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1895 INIT_LIST_HEAD(&nm_i->nat_entries);
1896 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1897
1898 mutex_init(&nm_i->build_lock);
1899 spin_lock_init(&nm_i->free_nid_list_lock);
1900 rwlock_init(&nm_i->nat_tree_lock);
1901
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001902 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001903 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001904 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1905 if (!version_bitmap)
1906 return -EFAULT;
1907
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001908 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1909 GFP_KERNEL);
1910 if (!nm_i->nat_bitmap)
1911 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912 return 0;
1913}
1914
1915int build_node_manager(struct f2fs_sb_info *sbi)
1916{
1917 int err;
1918
1919 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1920 if (!sbi->nm_info)
1921 return -ENOMEM;
1922
1923 err = init_node_manager(sbi);
1924 if (err)
1925 return err;
1926
1927 build_free_nids(sbi);
1928 return 0;
1929}
1930
1931void destroy_node_manager(struct f2fs_sb_info *sbi)
1932{
1933 struct f2fs_nm_info *nm_i = NM_I(sbi);
1934 struct free_nid *i, *next_i;
1935 struct nat_entry *natvec[NATVEC_SIZE];
1936 nid_t nid = 0;
1937 unsigned int found;
1938
1939 if (!nm_i)
1940 return;
1941
1942 /* destroy free nid list */
1943 spin_lock(&nm_i->free_nid_list_lock);
1944 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001945 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001946 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001947 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001948 spin_unlock(&nm_i->free_nid_list_lock);
1949 kmem_cache_free(free_nid_slab, i);
1950 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001952 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001953 spin_unlock(&nm_i->free_nid_list_lock);
1954
1955 /* destroy nat cache */
1956 write_lock(&nm_i->nat_tree_lock);
1957 while ((found = __gang_lookup_nat_cache(nm_i,
1958 nid, NATVEC_SIZE, natvec))) {
1959 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08001960 nid = nat_get_nid(natvec[found - 1]) + 1;
1961 for (idx = 0; idx < found; idx++)
1962 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001963 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001964 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001965 write_unlock(&nm_i->nat_tree_lock);
1966
1967 kfree(nm_i->nat_bitmap);
1968 sbi->nm_info = NULL;
1969 kfree(nm_i);
1970}
1971
Namjae Jeon6e6093a2013-01-17 00:08:30 +09001972int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001973{
1974 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08001975 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001976 if (!nat_entry_slab)
1977 return -ENOMEM;
1978
1979 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08001980 sizeof(struct free_nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001981 if (!free_nid_slab) {
1982 kmem_cache_destroy(nat_entry_slab);
1983 return -ENOMEM;
1984 }
1985 return 0;
1986}
1987
1988void destroy_node_manager_caches(void)
1989{
1990 kmem_cache_destroy(free_nid_slab);
1991 kmem_cache_destroy(nat_entry_slab);
1992}