blob: d2f78428342503111d1b9e5f0d2d86507fac7d8d [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;
Chao Yuaec71382014-06-24 09:18:20 +080028static struct kmem_cache *nat_entry_set_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090029
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090030bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090031{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090032 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090033 struct sysinfo val;
34 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090035 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090036
37 si_meminfo(&val);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090038 /* give 25%, 25%, 50% memory for each components respectively */
39 if (type == FREE_NIDS) {
40 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12;
41 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
42 } else if (type == NAT_ENTRIES) {
43 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12;
44 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
45 } else if (type == DIRTY_DENTS) {
Jaegeuk Kim2743f862014-06-28 01:00:41 +090046 if (sbi->sb->s_bdi->dirty_exceeded)
47 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090048 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
49 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1);
50 }
51 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090052}
53
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090054static void clear_node_page_dirty(struct page *page)
55{
56 struct address_space *mapping = page->mapping;
57 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
58 unsigned int long flags;
59
60 if (PageDirty(page)) {
61 spin_lock_irqsave(&mapping->tree_lock, flags);
62 radix_tree_tag_clear(&mapping->page_tree,
63 page_index(page),
64 PAGECACHE_TAG_DIRTY);
65 spin_unlock_irqrestore(&mapping->tree_lock, flags);
66
67 clear_page_dirty_for_io(page);
68 dec_page_count(sbi, F2FS_DIRTY_NODES);
69 }
70 ClearPageUptodate(page);
71}
72
73static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
74{
75 pgoff_t index = current_nat_addr(sbi, nid);
76 return get_meta_page(sbi, index);
77}
78
79static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
80{
81 struct page *src_page;
82 struct page *dst_page;
83 pgoff_t src_off;
84 pgoff_t dst_off;
85 void *src_addr;
86 void *dst_addr;
87 struct f2fs_nm_info *nm_i = NM_I(sbi);
88
89 src_off = current_nat_addr(sbi, nid);
90 dst_off = next_nat_addr(sbi, src_off);
91
92 /* get current nat block page with lock */
93 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090094 dst_page = grab_meta_page(sbi, dst_off);
Chao Yuaec71382014-06-24 09:18:20 +080095 f2fs_bug_on(PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090096
97 src_addr = page_address(src_page);
98 dst_addr = page_address(dst_page);
99 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
100 set_page_dirty(dst_page);
101 f2fs_put_page(src_page, 1);
102
103 set_to_next_nat(nm_i, nid);
104
105 return dst_page;
106}
107
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900108static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
109{
110 return radix_tree_lookup(&nm_i->nat_root, n);
111}
112
113static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
114 nid_t start, unsigned int nr, struct nat_entry **ep)
115{
116 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
117}
118
119static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
120{
121 list_del(&e->list);
122 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
123 nm_i->nat_cnt--;
124 kmem_cache_free(nat_entry_slab, e);
125}
126
127int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
128{
129 struct f2fs_nm_info *nm_i = NM_I(sbi);
130 struct nat_entry *e;
131 int is_cp = 1;
132
133 read_lock(&nm_i->nat_tree_lock);
134 e = __lookup_nat_cache(nm_i, nid);
135 if (e && !e->checkpointed)
136 is_cp = 0;
137 read_unlock(&nm_i->nat_tree_lock);
138 return is_cp;
139}
140
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900141bool fsync_mark_done(struct f2fs_sb_info *sbi, nid_t nid)
142{
143 struct f2fs_nm_info *nm_i = NM_I(sbi);
144 struct nat_entry *e;
145 bool fsync_done = false;
146
147 read_lock(&nm_i->nat_tree_lock);
148 e = __lookup_nat_cache(nm_i, nid);
149 if (e)
150 fsync_done = e->fsync_done;
151 read_unlock(&nm_i->nat_tree_lock);
152 return fsync_done;
153}
154
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900155void fsync_mark_clear(struct f2fs_sb_info *sbi, nid_t nid)
156{
157 struct f2fs_nm_info *nm_i = NM_I(sbi);
158 struct nat_entry *e;
159
160 write_lock(&nm_i->nat_tree_lock);
161 e = __lookup_nat_cache(nm_i, nid);
162 if (e)
163 e->fsync_done = false;
164 write_unlock(&nm_i->nat_tree_lock);
165}
166
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900167static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
168{
169 struct nat_entry *new;
170
171 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
172 if (!new)
173 return NULL;
174 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
175 kmem_cache_free(nat_entry_slab, new);
176 return NULL;
177 }
178 memset(new, 0, sizeof(struct nat_entry));
179 nat_set_nid(new, nid);
Jaegeuk Kimfffc2a02014-02-21 13:17:22 +0900180 new->checkpointed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900181 list_add_tail(&new->list, &nm_i->nat_entries);
182 nm_i->nat_cnt++;
183 return new;
184}
185
186static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
187 struct f2fs_nat_entry *ne)
188{
189 struct nat_entry *e;
190retry:
191 write_lock(&nm_i->nat_tree_lock);
192 e = __lookup_nat_cache(nm_i, nid);
193 if (!e) {
194 e = grab_nat_entry(nm_i, nid);
195 if (!e) {
196 write_unlock(&nm_i->nat_tree_lock);
197 goto retry;
198 }
Chao Yu94dac222014-04-17 10:51:05 +0800199 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900200 }
201 write_unlock(&nm_i->nat_tree_lock);
202}
203
204static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900205 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900206{
207 struct f2fs_nm_info *nm_i = NM_I(sbi);
208 struct nat_entry *e;
209retry:
210 write_lock(&nm_i->nat_tree_lock);
211 e = __lookup_nat_cache(nm_i, ni->nid);
212 if (!e) {
213 e = grab_nat_entry(nm_i, ni->nid);
214 if (!e) {
215 write_unlock(&nm_i->nat_tree_lock);
216 goto retry;
217 }
218 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900219 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900220 } else if (new_blkaddr == NEW_ADDR) {
221 /*
222 * when nid is reallocated,
223 * previous nat entry can be remained in nat cache.
224 * So, reinitialize it with new information.
225 */
226 e->ni = *ni;
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900227 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900228 }
229
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900230 /* sanity check */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900231 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
232 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900233 new_blkaddr == NULL_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900234 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900235 new_blkaddr == NEW_ADDR);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900236 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900237 nat_get_blkaddr(e) != NULL_ADDR &&
238 new_blkaddr == NEW_ADDR);
239
arter97e1c42042014-08-06 23:22:50 +0900240 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900241 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
242 unsigned char version = nat_get_version(e);
243 nat_set_version(e, inc_node_version(version));
244 }
245
246 /* change address */
247 nat_set_blkaddr(e, new_blkaddr);
248 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900249
250 /* update fsync_mark if its inode nat entry is still alive */
251 e = __lookup_nat_cache(nm_i, ni->ino);
252 if (e)
253 e->fsync_done = fsync_done;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900254 write_unlock(&nm_i->nat_tree_lock);
255}
256
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900257int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900258{
259 struct f2fs_nm_info *nm_i = NM_I(sbi);
260
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900261 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900262 return 0;
263
264 write_lock(&nm_i->nat_tree_lock);
265 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
266 struct nat_entry *ne;
267 ne = list_first_entry(&nm_i->nat_entries,
268 struct nat_entry, list);
269 __del_from_nat_cache(nm_i, ne);
270 nr_shrink--;
271 }
272 write_unlock(&nm_i->nat_tree_lock);
273 return nr_shrink;
274}
275
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900276/*
arter97e1c42042014-08-06 23:22:50 +0900277 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900278 */
279void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
280{
281 struct f2fs_nm_info *nm_i = NM_I(sbi);
282 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
283 struct f2fs_summary_block *sum = curseg->sum_blk;
284 nid_t start_nid = START_NID(nid);
285 struct f2fs_nat_block *nat_blk;
286 struct page *page = NULL;
287 struct f2fs_nat_entry ne;
288 struct nat_entry *e;
289 int i;
290
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900291 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900292 ni->nid = nid;
293
294 /* Check nat cache */
295 read_lock(&nm_i->nat_tree_lock);
296 e = __lookup_nat_cache(nm_i, nid);
297 if (e) {
298 ni->ino = nat_get_ino(e);
299 ni->blk_addr = nat_get_blkaddr(e);
300 ni->version = nat_get_version(e);
301 }
302 read_unlock(&nm_i->nat_tree_lock);
303 if (e)
304 return;
305
306 /* Check current segment summary */
307 mutex_lock(&curseg->curseg_mutex);
308 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
309 if (i >= 0) {
310 ne = nat_in_journal(sum, i);
311 node_info_from_raw_nat(ni, &ne);
312 }
313 mutex_unlock(&curseg->curseg_mutex);
314 if (i >= 0)
315 goto cache;
316
317 /* Fill node_info from nat page */
318 page = get_current_nat_page(sbi, start_nid);
319 nat_blk = (struct f2fs_nat_block *)page_address(page);
320 ne = nat_blk->entries[nid - start_nid];
321 node_info_from_raw_nat(ni, &ne);
322 f2fs_put_page(page, 1);
323cache:
324 /* cache nat entry */
325 cache_nat_entry(NM_I(sbi), nid, &ne);
326}
327
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900328/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900329 * The maximum depth is four.
330 * Offset[0] will have raw inode offset.
331 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900332static int get_node_path(struct f2fs_inode_info *fi, long block,
333 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900334{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900335 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900336 const long direct_blks = ADDRS_PER_BLOCK;
337 const long dptrs_per_blk = NIDS_PER_BLOCK;
338 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
339 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
340 int n = 0;
341 int level = 0;
342
343 noffset[0] = 0;
344
345 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900346 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900347 goto got;
348 }
349 block -= direct_index;
350 if (block < direct_blks) {
351 offset[n++] = NODE_DIR1_BLOCK;
352 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900353 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900354 level = 1;
355 goto got;
356 }
357 block -= direct_blks;
358 if (block < direct_blks) {
359 offset[n++] = NODE_DIR2_BLOCK;
360 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900361 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900362 level = 1;
363 goto got;
364 }
365 block -= direct_blks;
366 if (block < indirect_blks) {
367 offset[n++] = NODE_IND1_BLOCK;
368 noffset[n] = 3;
369 offset[n++] = block / direct_blks;
370 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900371 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900372 level = 2;
373 goto got;
374 }
375 block -= indirect_blks;
376 if (block < indirect_blks) {
377 offset[n++] = NODE_IND2_BLOCK;
378 noffset[n] = 4 + dptrs_per_blk;
379 offset[n++] = block / direct_blks;
380 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900381 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900382 level = 2;
383 goto got;
384 }
385 block -= indirect_blks;
386 if (block < dindirect_blks) {
387 offset[n++] = NODE_DIND_BLOCK;
388 noffset[n] = 5 + (dptrs_per_blk * 2);
389 offset[n++] = block / indirect_blks;
390 noffset[n] = 6 + (dptrs_per_blk * 2) +
391 offset[n - 1] * (dptrs_per_blk + 1);
392 offset[n++] = (block / direct_blks) % dptrs_per_blk;
393 noffset[n] = 7 + (dptrs_per_blk * 2) +
394 offset[n - 2] * (dptrs_per_blk + 1) +
395 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900396 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900397 level = 3;
398 goto got;
399 } else {
400 BUG();
401 }
402got:
403 return level;
404}
405
406/*
407 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800408 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
409 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900410 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900411 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900412int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900413{
414 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
415 struct page *npage[4];
416 struct page *parent;
417 int offset[4];
418 unsigned int noffset[4];
419 nid_t nids[4];
420 int level, i;
421 int err = 0;
422
Jaegeuk Kimde936532013-08-12 21:08:03 +0900423 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900424
425 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900426 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900427
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900428 if (!npage[0]) {
429 npage[0] = get_node_page(sbi, nids[0]);
430 if (IS_ERR(npage[0]))
431 return PTR_ERR(npage[0]);
432 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900433 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900434 if (level != 0)
435 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900436 dn->inode_page = npage[0];
437 dn->inode_page_locked = true;
438
439 /* get indirect or direct nodes */
440 for (i = 1; i <= level; i++) {
441 bool done = false;
442
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900443 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900444 /* alloc new node */
445 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900446 err = -ENOSPC;
447 goto release_pages;
448 }
449
450 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900451 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900452 if (IS_ERR(npage[i])) {
453 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900454 err = PTR_ERR(npage[i]);
455 goto release_pages;
456 }
457
458 set_nid(parent, offset[i - 1], nids[i], i == 1);
459 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900460 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900461 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900462 npage[i] = get_node_page_ra(parent, offset[i - 1]);
463 if (IS_ERR(npage[i])) {
464 err = PTR_ERR(npage[i]);
465 goto release_pages;
466 }
467 done = true;
468 }
469 if (i == 1) {
470 dn->inode_page_locked = false;
471 unlock_page(parent);
472 } else {
473 f2fs_put_page(parent, 1);
474 }
475
476 if (!done) {
477 npage[i] = get_node_page(sbi, nids[i]);
478 if (IS_ERR(npage[i])) {
479 err = PTR_ERR(npage[i]);
480 f2fs_put_page(npage[0], 0);
481 goto release_out;
482 }
483 }
484 if (i < level) {
485 parent = npage[i];
486 nids[i + 1] = get_nid(parent, offset[i], false);
487 }
488 }
489 dn->nid = nids[level];
490 dn->ofs_in_node = offset[level];
491 dn->node_page = npage[level];
492 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
493 return 0;
494
495release_pages:
496 f2fs_put_page(parent, 1);
497 if (i > 1)
498 f2fs_put_page(npage[0], 0);
499release_out:
500 dn->inode_page = NULL;
501 dn->node_page = NULL;
502 return err;
503}
504
505static void truncate_node(struct dnode_of_data *dn)
506{
507 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
508 struct node_info ni;
509
510 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900511 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900512 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900513 goto invalidate;
514 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900515 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900516
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900517 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900518 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800519 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900520 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521
522 if (dn->nid == dn->inode->i_ino) {
523 remove_orphan_inode(sbi, dn->nid);
524 dec_valid_inode_count(sbi);
525 } else {
526 sync_inode_page(dn);
527 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900528invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900529 clear_node_page_dirty(dn->node_page);
530 F2FS_SET_SB_DIRT(sbi);
531
532 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900533
534 invalidate_mapping_pages(NODE_MAPPING(sbi),
535 dn->node_page->index, dn->node_page->index);
536
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900537 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900538 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900539}
540
541static int truncate_dnode(struct dnode_of_data *dn)
542{
543 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
544 struct page *page;
545
546 if (dn->nid == 0)
547 return 1;
548
549 /* get direct node */
550 page = get_node_page(sbi, dn->nid);
551 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
552 return 1;
553 else if (IS_ERR(page))
554 return PTR_ERR(page);
555
556 /* Make dnode_of_data for parameter */
557 dn->node_page = page;
558 dn->ofs_in_node = 0;
559 truncate_data_blocks(dn);
560 truncate_node(dn);
561 return 1;
562}
563
564static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
565 int ofs, int depth)
566{
567 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
568 struct dnode_of_data rdn = *dn;
569 struct page *page;
570 struct f2fs_node *rn;
571 nid_t child_nid;
572 unsigned int child_nofs;
573 int freed = 0;
574 int i, ret;
575
576 if (dn->nid == 0)
577 return NIDS_PER_BLOCK + 1;
578
Namjae Jeon51dd6242013-04-20 01:28:52 +0900579 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
580
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900581 page = get_node_page(sbi, dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900582 if (IS_ERR(page)) {
583 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900584 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900585 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900586
Gu Zheng45590712013-07-15 17:57:38 +0800587 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900588 if (depth < 3) {
589 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
590 child_nid = le32_to_cpu(rn->in.nid[i]);
591 if (child_nid == 0)
592 continue;
593 rdn.nid = child_nid;
594 ret = truncate_dnode(&rdn);
595 if (ret < 0)
596 goto out_err;
597 set_nid(page, i, 0, false);
598 }
599 } else {
600 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
601 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
602 child_nid = le32_to_cpu(rn->in.nid[i]);
603 if (child_nid == 0) {
604 child_nofs += NIDS_PER_BLOCK + 1;
605 continue;
606 }
607 rdn.nid = child_nid;
608 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
609 if (ret == (NIDS_PER_BLOCK + 1)) {
610 set_nid(page, i, 0, false);
611 child_nofs += ret;
612 } else if (ret < 0 && ret != -ENOENT) {
613 goto out_err;
614 }
615 }
616 freed = child_nofs;
617 }
618
619 if (!ofs) {
620 /* remove current indirect node */
621 dn->node_page = page;
622 truncate_node(dn);
623 freed++;
624 } else {
625 f2fs_put_page(page, 1);
626 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900627 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900628 return freed;
629
630out_err:
631 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900632 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900633 return ret;
634}
635
636static int truncate_partial_nodes(struct dnode_of_data *dn,
637 struct f2fs_inode *ri, int *offset, int depth)
638{
639 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
640 struct page *pages[2];
641 nid_t nid[3];
642 nid_t child_nid;
643 int err = 0;
644 int i;
645 int idx = depth - 2;
646
647 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
648 if (!nid[0])
649 return 0;
650
651 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800652 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900653 /* reference count'll be increased */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900654 pages[i] = get_node_page(sbi, nid[i]);
655 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900656 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800657 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900658 goto fail;
659 }
660 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
661 }
662
663 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800664 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900665 child_nid = get_nid(pages[idx], i, false);
666 if (!child_nid)
667 continue;
668 dn->nid = child_nid;
669 err = truncate_dnode(dn);
670 if (err < 0)
671 goto fail;
672 set_nid(pages[idx], i, 0, false);
673 }
674
shifei10.gea225dca2013-10-29 15:32:34 +0800675 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900676 dn->node_page = pages[idx];
677 dn->nid = nid[idx];
678 truncate_node(dn);
679 } else {
680 f2fs_put_page(pages[idx], 1);
681 }
682 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800683 offset[idx + 1] = 0;
684 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900685fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800686 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900687 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900688
689 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
690
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900691 return err;
692}
693
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900694/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900695 * All the block addresses of data and nodes should be nullified.
696 */
697int truncate_inode_blocks(struct inode *inode, pgoff_t from)
698{
699 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
700 int err = 0, cont = 1;
701 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900702 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900703 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900704 struct dnode_of_data dn;
705 struct page *page;
706
Namjae Jeon51dd6242013-04-20 01:28:52 +0900707 trace_f2fs_truncate_inode_blocks_enter(inode, from);
708
Jaegeuk Kimde936532013-08-12 21:08:03 +0900709 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900710restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900711 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900712 if (IS_ERR(page)) {
713 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900714 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900715 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716
717 set_new_dnode(&dn, inode, page, NULL, 0);
718 unlock_page(page);
719
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900720 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900721 switch (level) {
722 case 0:
723 case 1:
724 nofs = noffset[1];
725 break;
726 case 2:
727 nofs = noffset[1];
728 if (!offset[level - 1])
729 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900730 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900731 if (err < 0 && err != -ENOENT)
732 goto fail;
733 nofs += 1 + NIDS_PER_BLOCK;
734 break;
735 case 3:
736 nofs = 5 + 2 * NIDS_PER_BLOCK;
737 if (!offset[level - 1])
738 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900739 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900740 if (err < 0 && err != -ENOENT)
741 goto fail;
742 break;
743 default:
744 BUG();
745 }
746
747skip_partial:
748 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900749 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900750 switch (offset[0]) {
751 case NODE_DIR1_BLOCK:
752 case NODE_DIR2_BLOCK:
753 err = truncate_dnode(&dn);
754 break;
755
756 case NODE_IND1_BLOCK:
757 case NODE_IND2_BLOCK:
758 err = truncate_nodes(&dn, nofs, offset[1], 2);
759 break;
760
761 case NODE_DIND_BLOCK:
762 err = truncate_nodes(&dn, nofs, offset[1], 3);
763 cont = 0;
764 break;
765
766 default:
767 BUG();
768 }
769 if (err < 0 && err != -ENOENT)
770 goto fail;
771 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900772 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900773 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900774 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900775 f2fs_put_page(page, 1);
776 goto restart;
777 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900778 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900779 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900780 set_page_dirty(page);
781 unlock_page(page);
782 }
783 offset[1] = 0;
784 offset[0]++;
785 nofs += err;
786 }
787fail:
788 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900789 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900790 return err > 0 ? 0 : err;
791}
792
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900793int truncate_xattr_node(struct inode *inode, struct page *page)
794{
795 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
796 nid_t nid = F2FS_I(inode)->i_xattr_nid;
797 struct dnode_of_data dn;
798 struct page *npage;
799
800 if (!nid)
801 return 0;
802
803 npage = get_node_page(sbi, nid);
804 if (IS_ERR(npage))
805 return PTR_ERR(npage);
806
807 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900808
809 /* need to do checkpoint during fsync */
810 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
811
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900812 set_new_dnode(&dn, inode, page, npage, nid);
813
814 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800815 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900816 truncate_node(&dn);
817 return 0;
818}
819
Jaegeuk Kim39936832012-11-22 16:21:29 +0900820/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800821 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
822 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900823 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800824void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900825{
826 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
827 struct page *page;
828 nid_t ino = inode->i_ino;
829 struct dnode_of_data dn;
830
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831 page = get_node_page(sbi, ino);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900832 if (IS_ERR(page))
Gu Zheng58e674d2013-11-19 18:03:18 +0800833 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900834
Gu Zheng58e674d2013-11-19 18:03:18 +0800835 if (truncate_xattr_node(inode, page)) {
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900836 f2fs_put_page(page, 1);
Gu Zheng58e674d2013-11-19 18:03:18 +0800837 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900838 }
arter97e1c42042014-08-06 23:22:50 +0900839 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900840 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900841 set_new_dnode(&dn, inode, page, page, ino);
842 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900843}
844
Jaegeuk Kima014e032014-06-20 21:44:02 -0700845struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900846{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900847 struct dnode_of_data dn;
848
849 /* allocate inode page for new inode */
850 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900851
852 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900853 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900854}
855
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900856struct page *new_node_page(struct dnode_of_data *dn,
857 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900858{
859 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900860 struct node_info old_ni, new_ni;
861 struct page *page;
862 int err;
863
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900864 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900865 return ERR_PTR(-EPERM);
866
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900867 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900868 if (!page)
869 return ERR_PTR(-ENOMEM);
870
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900871 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900872 err = -ENOSPC;
873 goto fail;
874 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900875
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900876 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900877
878 /* Reinitialize old_ni with new node page */
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900879 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900880 new_ni = old_ni;
881 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900882 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900883
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900884 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900885 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900886 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900887 SetPageUptodate(page);
888 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900889
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800890 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900891 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
892
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900893 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900894 if (ipage)
895 update_inode(dn->inode, ipage);
896 else
897 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900898 if (ofs == 0)
899 inc_valid_inode_count(sbi);
900
901 return page;
902
903fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900904 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900905 f2fs_put_page(page, 1);
906 return ERR_PTR(err);
907}
908
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900909/*
910 * Caller should do after getting the following values.
911 * 0: f2fs_put_page(page, 0)
912 * LOCKED_PAGE: f2fs_put_page(page, 1)
913 * error: nothing
914 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900915static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900916{
917 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
918 struct node_info ni;
919
920 get_node_info(sbi, page->index, &ni);
921
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900922 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900923 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900924 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900925 }
926
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900927 if (PageUptodate(page))
928 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900929
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900930 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900931}
932
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900933/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900934 * Readahead a node page
935 */
936void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
937{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900938 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900939 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900940
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900941 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900942 if (apage && PageUptodate(apage)) {
943 f2fs_put_page(apage, 0);
944 return;
945 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900946 f2fs_put_page(apage, 0);
947
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900948 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900949 if (!apage)
950 return;
951
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900952 err = read_node_page(apage, READA);
953 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900954 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900955 else if (err == LOCKED_PAGE)
956 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900957}
958
959struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
960{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900961 struct page *page;
962 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900963repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900964 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900965 if (!page)
966 return ERR_PTR(-ENOMEM);
967
968 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900969 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900970 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900971 else if (err == LOCKED_PAGE)
972 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900973
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900974 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +0900975 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900976 f2fs_put_page(page, 1);
977 return ERR_PTR(-EIO);
978 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900979 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900980 f2fs_put_page(page, 1);
981 goto repeat;
982 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900983got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900984 return page;
985}
986
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900987/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988 * Return a locked page for the desired node page.
989 * And, readahead MAX_RA_NODE number of node pages.
990 */
991struct page *get_node_page_ra(struct page *parent, int start)
992{
993 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900994 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900995 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900996 int err, i, end;
997 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900998
999 /* First, try getting the desired direct node. */
1000 nid = get_nid(parent, start, false);
1001 if (!nid)
1002 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001003repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001004 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001005 if (!page)
1006 return ERR_PTR(-ENOMEM);
1007
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001008 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001009 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001010 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001011 else if (err == LOCKED_PAGE)
1012 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001013
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001014 blk_start_plug(&plug);
1015
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001016 /* Then, try readahead for siblings of the desired node */
1017 end = start + MAX_RA_NODE;
1018 end = min(end, NIDS_PER_BLOCK);
1019 for (i = start + 1; i < end; i++) {
1020 nid = get_nid(parent, i, false);
1021 if (!nid)
1022 continue;
1023 ra_node_page(sbi, nid);
1024 }
1025
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001026 blk_finish_plug(&plug);
1027
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001028 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001029 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001030 f2fs_put_page(page, 1);
1031 goto repeat;
1032 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001033page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001034 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001035 f2fs_put_page(page, 1);
1036 return ERR_PTR(-EIO);
1037 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001038 return page;
1039}
1040
1041void sync_inode_page(struct dnode_of_data *dn)
1042{
1043 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1044 update_inode(dn->inode, dn->node_page);
1045 } else if (dn->inode_page) {
1046 if (!dn->inode_page_locked)
1047 lock_page(dn->inode_page);
1048 update_inode(dn->inode, dn->inode_page);
1049 if (!dn->inode_page_locked)
1050 unlock_page(dn->inode_page);
1051 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001052 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001053 }
1054}
1055
1056int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1057 struct writeback_control *wbc)
1058{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001059 pgoff_t index, end;
1060 struct pagevec pvec;
1061 int step = ino ? 2 : 0;
1062 int nwritten = 0, wrote = 0;
1063
1064 pagevec_init(&pvec, 0);
1065
1066next_step:
1067 index = 0;
1068 end = LONG_MAX;
1069
1070 while (index <= end) {
1071 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001072 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001073 PAGECACHE_TAG_DIRTY,
1074 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1075 if (nr_pages == 0)
1076 break;
1077
1078 for (i = 0; i < nr_pages; i++) {
1079 struct page *page = pvec.pages[i];
1080
1081 /*
1082 * flushing sequence with step:
1083 * 0. indirect nodes
1084 * 1. dentry dnodes
1085 * 2. file dnodes
1086 */
1087 if (step == 0 && IS_DNODE(page))
1088 continue;
1089 if (step == 1 && (!IS_DNODE(page) ||
1090 is_cold_node(page)))
1091 continue;
1092 if (step == 2 && (!IS_DNODE(page) ||
1093 !is_cold_node(page)))
1094 continue;
1095
1096 /*
1097 * If an fsync mode,
1098 * we should not skip writing node pages.
1099 */
1100 if (ino && ino_of_node(page) == ino)
1101 lock_page(page);
1102 else if (!trylock_page(page))
1103 continue;
1104
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001105 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001106continue_unlock:
1107 unlock_page(page);
1108 continue;
1109 }
1110 if (ino && ino_of_node(page) != ino)
1111 goto continue_unlock;
1112
1113 if (!PageDirty(page)) {
1114 /* someone wrote it for us */
1115 goto continue_unlock;
1116 }
1117
1118 if (!clear_page_dirty_for_io(page))
1119 goto continue_unlock;
1120
1121 /* called by fsync() */
1122 if (ino && IS_DNODE(page)) {
1123 int mark = !is_checkpointed_node(sbi, ino);
1124 set_fsync_mark(page, 1);
1125 if (IS_INODE(page))
1126 set_dentry_mark(page, mark);
1127 nwritten++;
1128 } else {
1129 set_fsync_mark(page, 0);
1130 set_dentry_mark(page, 0);
1131 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001132
1133 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1134 unlock_page(page);
1135 else
1136 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001137
1138 if (--wbc->nr_to_write == 0)
1139 break;
1140 }
1141 pagevec_release(&pvec);
1142 cond_resched();
1143
1144 if (wbc->nr_to_write == 0) {
1145 step = 2;
1146 break;
1147 }
1148 }
1149
1150 if (step < 2) {
1151 step++;
1152 goto next_step;
1153 }
1154
1155 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001156 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001157 return nwritten;
1158}
1159
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001160int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1161{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001162 pgoff_t index = 0, end = LONG_MAX;
1163 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001164 int ret2 = 0, ret = 0;
1165
1166 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001167
1168 while (index <= end) {
1169 int i, nr_pages;
1170 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1171 PAGECACHE_TAG_WRITEBACK,
1172 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1173 if (nr_pages == 0)
1174 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001175
1176 for (i = 0; i < nr_pages; i++) {
1177 struct page *page = pvec.pages[i];
1178
1179 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001180 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001181 continue;
1182
Chao Yu4bf08ff2013-11-04 10:28:33 +08001183 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001184 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001185 if (TestClearPageError(page))
1186 ret = -EIO;
1187 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001188 }
1189 pagevec_release(&pvec);
1190 cond_resched();
1191 }
1192
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001193 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001194 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001195 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001196 ret2 = -EIO;
1197 if (!ret)
1198 ret = ret2;
1199 return ret;
1200}
1201
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001202static int f2fs_write_node_page(struct page *page,
1203 struct writeback_control *wbc)
1204{
1205 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1206 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001207 block_t new_addr;
1208 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001209 struct f2fs_io_info fio = {
1210 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001211 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001212 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001213
Chao Yuecda0de2014-05-06 16:48:26 +08001214 trace_f2fs_writepage(page, NODE);
1215
Chao Yucfb271d2013-12-05 17:15:22 +08001216 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001217 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001218 if (unlikely(f2fs_cp_error(sbi)))
1219 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001220
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001221 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001222
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001223 /* get old block addr of this node page */
1224 nid = nid_of_node(page);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001225 f2fs_bug_on(page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001226
1227 get_node_info(sbi, nid, &ni);
1228
1229 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001230 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001231 dec_page_count(sbi, F2FS_DIRTY_NODES);
1232 unlock_page(page);
1233 return 0;
1234 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001235
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001236 if (wbc->for_reclaim)
1237 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001238
Chao Yub3582c62014-07-03 18:58:39 +08001239 down_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001240 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001241 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001242 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001243 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001244 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001245 unlock_page(page);
1246 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001247
1248redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001249 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001250 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001251}
1252
1253static int f2fs_write_node_pages(struct address_space *mapping,
1254 struct writeback_control *wbc)
1255{
1256 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001257 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001258
Chao Yue5748432014-05-06 16:51:24 +08001259 trace_f2fs_writepages(mapping->host, wbc, NODE);
1260
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001261 /* balancing f2fs's metadata in background */
1262 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001263
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001264 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001265 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001266 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001267
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001268 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001269 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001270 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001271 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001272 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001273
1274skip_write:
1275 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1276 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001277}
1278
1279static int f2fs_set_node_page_dirty(struct page *page)
1280{
1281 struct address_space *mapping = page->mapping;
1282 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1283
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001284 trace_f2fs_set_page_dirty(page, NODE);
1285
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001286 SetPageUptodate(page);
1287 if (!PageDirty(page)) {
1288 __set_page_dirty_nobuffers(page);
1289 inc_page_count(sbi, F2FS_DIRTY_NODES);
1290 SetPagePrivate(page);
1291 return 1;
1292 }
1293 return 0;
1294}
1295
Lukas Czernerd47992f2013-05-21 23:17:23 -04001296static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1297 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001298{
1299 struct inode *inode = page->mapping->host;
1300 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1301 if (PageDirty(page))
1302 dec_page_count(sbi, F2FS_DIRTY_NODES);
1303 ClearPagePrivate(page);
1304}
1305
1306static int f2fs_release_node_page(struct page *page, gfp_t wait)
1307{
1308 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001309 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310}
1311
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001312/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001313 * Structure of the f2fs node operations
1314 */
1315const struct address_space_operations f2fs_node_aops = {
1316 .writepage = f2fs_write_node_page,
1317 .writepages = f2fs_write_node_pages,
1318 .set_page_dirty = f2fs_set_node_page_dirty,
1319 .invalidatepage = f2fs_invalidate_node_page,
1320 .releasepage = f2fs_release_node_page,
1321};
1322
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001323static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1324 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001325{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001326 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327}
1328
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001329static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1330 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001331{
1332 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001333 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001334}
1335
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001336static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001337{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001338 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001339 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001340 struct nat_entry *ne;
1341 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001342
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001343 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001344 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001345
1346 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001347 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001348 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001349
Gu Zheng7bd59382013-10-22 14:52:26 +08001350 if (build) {
1351 /* do not add allocated nids */
1352 read_lock(&nm_i->nat_tree_lock);
1353 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001354 if (ne &&
1355 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001356 allocated = true;
1357 read_unlock(&nm_i->nat_tree_lock);
1358 if (allocated)
1359 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001360 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001361
1362 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001363 i->nid = nid;
1364 i->state = NID_NEW;
1365
1366 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001367 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001368 spin_unlock(&nm_i->free_nid_list_lock);
1369 kmem_cache_free(free_nid_slab, i);
1370 return 0;
1371 }
1372 list_add_tail(&i->list, &nm_i->free_nid_list);
1373 nm_i->fcnt++;
1374 spin_unlock(&nm_i->free_nid_list_lock);
1375 return 1;
1376}
1377
1378static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1379{
1380 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001381 bool need_free = false;
1382
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001383 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001384 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001385 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001386 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001387 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001388 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001389 }
1390 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001391
1392 if (need_free)
1393 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001394}
1395
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001396static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001397 struct page *nat_page, nid_t start_nid)
1398{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001399 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001400 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1401 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001402 int i;
1403
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001404 i = start_nid % NAT_ENTRY_PER_BLOCK;
1405
1406 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001407
Chao Yucfb271d2013-12-05 17:15:22 +08001408 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001409 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001410
1411 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001412 f2fs_bug_on(blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001413 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001414 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001415 break;
1416 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001417 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001418}
1419
1420static void build_free_nids(struct f2fs_sb_info *sbi)
1421{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422 struct f2fs_nm_info *nm_i = NM_I(sbi);
1423 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1424 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001425 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001426 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001427
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001428 /* Enough entries */
1429 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1430 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001431
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001432 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001433 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001434
1435 while (1) {
1436 struct page *page = get_current_nat_page(sbi, nid);
1437
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001438 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001439 f2fs_put_page(page, 1);
1440
1441 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001442 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001443 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001444
1445 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001446 break;
1447 }
1448
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001449 /* go to the next free nat pages to find free nids abundantly */
1450 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001451
1452 /* find free nids from current sum_pages */
1453 mutex_lock(&curseg->curseg_mutex);
1454 for (i = 0; i < nats_in_cursum(sum); i++) {
1455 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1456 nid = le32_to_cpu(nid_in_journal(sum, i));
1457 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001458 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459 else
1460 remove_free_nid(nm_i, nid);
1461 }
1462 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001463}
1464
1465/*
1466 * If this function returns success, caller can obtain a new nid
1467 * from second parameter of this function.
1468 * The returned nid could be used ino as well as nid when inode is created.
1469 */
1470bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1471{
1472 struct f2fs_nm_info *nm_i = NM_I(sbi);
1473 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001474retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001475 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001476 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001477
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001478 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001479
1480 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001481 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001482 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001483 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001484 if (i->state == NID_NEW)
1485 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001486
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001487 f2fs_bug_on(i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001488 *nid = i->nid;
1489 i->state = NID_ALLOC;
1490 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001491 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001492 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001493 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001494 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001495
1496 /* Let's scan nat pages and its caches to get free nids */
1497 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001498 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001499 mutex_unlock(&nm_i->build_lock);
1500 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501}
1502
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001503/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001504 * alloc_nid() should be called prior to this function.
1505 */
1506void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1507{
1508 struct f2fs_nm_info *nm_i = NM_I(sbi);
1509 struct free_nid *i;
1510
1511 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001512 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001513 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001514 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001515 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001516
1517 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001518}
1519
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001520/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001521 * alloc_nid() should be called prior to this function.
1522 */
1523void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1524{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001525 struct f2fs_nm_info *nm_i = NM_I(sbi);
1526 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001527 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001528
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001529 if (!nid)
1530 return;
1531
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001532 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001533 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09001534 f2fs_bug_on(!i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001535 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001536 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001537 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001538 } else {
1539 i->state = NID_NEW;
1540 nm_i->fcnt++;
1541 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001542 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001543
1544 if (need_free)
1545 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001546}
1547
1548void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1549 struct f2fs_summary *sum, struct node_info *ni,
1550 block_t new_blkaddr)
1551{
1552 rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001553 set_node_addr(sbi, ni, new_blkaddr, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001554 clear_node_page_dirty(page);
1555}
1556
Chao Yu70cfed82014-08-02 15:26:04 +08001557void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001558{
1559 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1560 void *src_addr, *dst_addr;
1561 size_t inline_size;
1562 struct page *ipage;
1563 struct f2fs_inode *ri;
1564
Chao Yu28cdce02014-03-11 13:37:38 +08001565 ipage = get_node_page(sbi, inode->i_ino);
1566 f2fs_bug_on(IS_ERR(ipage));
1567
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001568 ri = F2FS_INODE(page);
1569 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1570 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1571 goto update_inode;
1572 }
1573
Chao Yu28cdce02014-03-11 13:37:38 +08001574 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);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001580update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001581 update_inode(inode, ipage);
1582 f2fs_put_page(ipage, 1);
1583}
1584
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001585void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001586{
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
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001592 /* 1: invalidate the previous xattr nid */
1593 if (!prev_xnid)
1594 goto recover_xnid;
1595
1596 /* Deallocate node address */
1597 get_node_info(sbi, prev_xnid, &ni);
1598 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1599 invalidate_blocks(sbi, ni.blk_addr);
1600 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001601 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001602
1603recover_xnid:
1604 /* 2: allocate new xattr nid */
1605 if (unlikely(!inc_valid_node_count(sbi, inode)))
1606 f2fs_bug_on(1);
1607
1608 remove_free_nid(NM_I(sbi), new_xnid);
1609 get_node_info(sbi, new_xnid, &ni);
1610 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001611 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001612 F2FS_I(inode)->i_xattr_nid = new_xnid;
1613
1614 /* 3: update xattr blkaddr */
1615 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001616 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001617
1618 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001619}
1620
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001621int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1622{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001623 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001624 nid_t ino = ino_of_node(page);
1625 struct node_info old_ni, new_ni;
1626 struct page *ipage;
1627
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001628 get_node_info(sbi, ino, &old_ni);
1629
1630 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1631 return -EINVAL;
1632
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001633 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001634 if (!ipage)
1635 return -ENOMEM;
1636
arter97e1c42042014-08-06 23:22:50 +09001637 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001638 remove_free_nid(NM_I(sbi), ino);
1639
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001640 SetPageUptodate(ipage);
1641 fill_node_footer(ipage, ino, ino, 0, true);
1642
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001643 src = F2FS_INODE(page);
1644 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001645
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001646 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1647 dst->i_size = 0;
1648 dst->i_blocks = cpu_to_le64(1);
1649 dst->i_links = cpu_to_le32(1);
1650 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001651 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001652
1653 new_ni = old_ni;
1654 new_ni.ino = ino;
1655
Chao Yucfb271d2013-12-05 17:15:22 +08001656 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001657 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001658 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001659 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001660 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001661 f2fs_put_page(ipage, 1);
1662 return 0;
1663}
1664
Chao Yu9af0ff12013-11-22 15:48:54 +08001665/*
1666 * ra_sum_pages() merge contiguous pages into one bio and submit.
arter97e1c42042014-08-06 23:22:50 +09001667 * these pre-read pages are allocated in bd_inode's mapping tree.
Chao Yu9af0ff12013-11-22 15:48:54 +08001668 */
Chao Yubac4eef2014-05-27 08:41:07 +08001669static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages,
Chao Yu9af0ff12013-11-22 15:48:54 +08001670 int start, int nrpages)
1671{
Chao Yubac4eef2014-05-27 08:41:07 +08001672 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1673 struct address_space *mapping = inode->i_mapping;
1674 int i, page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001675 struct f2fs_io_info fio = {
1676 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001677 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001678 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001679
Chao Yubac4eef2014-05-27 08:41:07 +08001680 for (i = 0; page_idx < start + nrpages; page_idx++, i++) {
1681 /* alloc page in bd_inode for reading node summary info */
1682 pages[i] = grab_cache_page(mapping, page_idx);
1683 if (!pages[i])
Gu Zhengd6537882014-03-07 18:43:36 +08001684 break;
Chao Yubac4eef2014-05-27 08:41:07 +08001685 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001686 }
1687
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001688 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yubac4eef2014-05-27 08:41:07 +08001689 return i;
Chao Yu9af0ff12013-11-22 15:48:54 +08001690}
1691
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001692int restore_node_summary(struct f2fs_sb_info *sbi,
1693 unsigned int segno, struct f2fs_summary_block *sum)
1694{
1695 struct f2fs_node *rn;
1696 struct f2fs_summary *sum_entry;
Chao Yubac4eef2014-05-27 08:41:07 +08001697 struct inode *inode = sbi->sb->s_bdev->bd_inode;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001698 block_t addr;
Chao Yu9af0ff12013-11-22 15:48:54 +08001699 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
Chao Yubac4eef2014-05-27 08:41:07 +08001700 struct page *pages[bio_blocks];
1701 int i, idx, last_offset, nrpages, err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001702
1703 /* scan the node segment */
1704 last_offset = sbi->blocks_per_seg;
1705 addr = START_BLOCK(sbi, segno);
1706 sum_entry = &sum->entries[0];
1707
Gu Zhengd6537882014-03-07 18:43:36 +08001708 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001709 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001710
arter97e1c42042014-08-06 23:22:50 +09001711 /* readahead node pages */
Chao Yubac4eef2014-05-27 08:41:07 +08001712 nrpages = ra_sum_pages(sbi, pages, addr, nrpages);
Gu Zhengd6537882014-03-07 18:43:36 +08001713 if (!nrpages)
1714 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001715
Chao Yubac4eef2014-05-27 08:41:07 +08001716 for (idx = 0; idx < nrpages; idx++) {
Gu Zhengd6537882014-03-07 18:43:36 +08001717 if (err)
1718 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001719
Chao Yubac4eef2014-05-27 08:41:07 +08001720 lock_page(pages[idx]);
1721 if (unlikely(!PageUptodate(pages[idx]))) {
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001722 err = -EIO;
1723 } else {
Chao Yubac4eef2014-05-27 08:41:07 +08001724 rn = F2FS_NODE(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001725 sum_entry->nid = rn->footer.nid;
1726 sum_entry->version = 0;
1727 sum_entry->ofs_in_node = 0;
1728 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001729 }
Chao Yubac4eef2014-05-27 08:41:07 +08001730 unlock_page(pages[idx]);
Gu Zhengd6537882014-03-07 18:43:36 +08001731skip:
Chao Yubac4eef2014-05-27 08:41:07 +08001732 page_cache_release(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001733 }
Chao Yubac4eef2014-05-27 08:41:07 +08001734
1735 invalidate_mapping_pages(inode->i_mapping, addr,
1736 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001737 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001738 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001739}
1740
Chao Yuaec71382014-06-24 09:18:20 +08001741static struct nat_entry_set *grab_nat_entry_set(void)
1742{
1743 struct nat_entry_set *nes =
1744 f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
1745
1746 nes->entry_cnt = 0;
1747 INIT_LIST_HEAD(&nes->set_list);
1748 INIT_LIST_HEAD(&nes->entry_list);
1749 return nes;
1750}
1751
1752static void release_nat_entry_set(struct nat_entry_set *nes,
1753 struct f2fs_nm_info *nm_i)
1754{
1755 f2fs_bug_on(!list_empty(&nes->entry_list));
1756
1757 nm_i->dirty_nat_cnt -= nes->entry_cnt;
1758 list_del(&nes->set_list);
1759 kmem_cache_free(nat_entry_set_slab, nes);
1760}
1761
1762static void adjust_nat_entry_set(struct nat_entry_set *nes,
1763 struct list_head *head)
1764{
1765 struct nat_entry_set *next = nes;
1766
1767 if (list_is_last(&nes->set_list, head))
1768 return;
1769
1770 list_for_each_entry_continue(next, head, set_list)
1771 if (nes->entry_cnt <= next->entry_cnt)
1772 break;
1773
1774 list_move_tail(&nes->set_list, &next->set_list);
1775}
1776
1777static void add_nat_entry(struct nat_entry *ne, struct list_head *head)
1778{
1779 struct nat_entry_set *nes;
1780 nid_t start_nid = START_NID(ne->ni.nid);
1781
1782 list_for_each_entry(nes, head, set_list) {
1783 if (nes->start_nid == start_nid) {
1784 list_move_tail(&ne->list, &nes->entry_list);
1785 nes->entry_cnt++;
1786 adjust_nat_entry_set(nes, head);
1787 return;
1788 }
1789 }
1790
1791 nes = grab_nat_entry_set();
1792
1793 nes->start_nid = start_nid;
1794 list_move_tail(&ne->list, &nes->entry_list);
1795 nes->entry_cnt++;
1796 list_add(&nes->set_list, head);
1797}
1798
1799static void merge_nats_in_set(struct f2fs_sb_info *sbi)
1800{
1801 struct f2fs_nm_info *nm_i = NM_I(sbi);
1802 struct list_head *dirty_list = &nm_i->dirty_nat_entries;
1803 struct list_head *set_list = &nm_i->nat_entry_set;
1804 struct nat_entry *ne, *tmp;
1805
1806 write_lock(&nm_i->nat_tree_lock);
1807 list_for_each_entry_safe(ne, tmp, dirty_list, list) {
1808 if (nat_get_blkaddr(ne) == NEW_ADDR)
1809 continue;
1810 add_nat_entry(ne, set_list);
1811 nm_i->dirty_nat_cnt++;
1812 }
1813 write_unlock(&nm_i->nat_tree_lock);
1814}
1815
1816static bool __has_cursum_space(struct f2fs_summary_block *sum, int size)
1817{
1818 if (nats_in_cursum(sum) + size <= NAT_JOURNAL_ENTRIES)
1819 return true;
1820 else
1821 return false;
1822}
1823
1824static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001825{
1826 struct f2fs_nm_info *nm_i = NM_I(sbi);
1827 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1828 struct f2fs_summary_block *sum = curseg->sum_blk;
1829 int i;
1830
1831 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001832 for (i = 0; i < nats_in_cursum(sum); i++) {
1833 struct nat_entry *ne;
1834 struct f2fs_nat_entry raw_ne;
1835 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1836
1837 raw_ne = nat_in_journal(sum, i);
1838retry:
1839 write_lock(&nm_i->nat_tree_lock);
1840 ne = __lookup_nat_cache(nm_i, nid);
Chao Yuaec71382014-06-24 09:18:20 +08001841 if (ne)
1842 goto found;
1843
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001844 ne = grab_nat_entry(nm_i, nid);
1845 if (!ne) {
1846 write_unlock(&nm_i->nat_tree_lock);
1847 goto retry;
1848 }
Chao Yu94dac222014-04-17 10:51:05 +08001849 node_info_from_raw_nat(&ne->ni, &raw_ne);
Chao Yuaec71382014-06-24 09:18:20 +08001850found:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001851 __set_nat_cache_dirty(nm_i, ne);
1852 write_unlock(&nm_i->nat_tree_lock);
1853 }
1854 update_nats_in_cursum(sum, -i);
1855 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001856}
1857
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001858/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001859 * This function is called during the checkpointing process.
1860 */
1861void flush_nat_entries(struct f2fs_sb_info *sbi)
1862{
1863 struct f2fs_nm_info *nm_i = NM_I(sbi);
1864 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1865 struct f2fs_summary_block *sum = curseg->sum_blk;
Chao Yuaec71382014-06-24 09:18:20 +08001866 struct nat_entry_set *nes, *tmp;
1867 struct list_head *head = &nm_i->nat_entry_set;
1868 bool to_journal = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001869
Chao Yuaec71382014-06-24 09:18:20 +08001870 /* merge nat entries of dirty list to nat entry set temporarily */
1871 merge_nats_in_set(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001872
Chao Yuaec71382014-06-24 09:18:20 +08001873 /*
1874 * if there are no enough space in journal to store dirty nat
1875 * entries, remove all entries from journal and merge them
1876 * into nat entry set.
1877 */
1878 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt)) {
1879 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001880
Chao Yuaec71382014-06-24 09:18:20 +08001881 /*
1882 * merge nat entries of dirty list to nat entry set temporarily
1883 */
1884 merge_nats_in_set(sbi);
1885 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001886
Chao Yuaec71382014-06-24 09:18:20 +08001887 if (!nm_i->dirty_nat_cnt)
1888 return;
Chao Yu2d7b8222014-03-29 11:33:17 +08001889
Chao Yuaec71382014-06-24 09:18:20 +08001890 /*
1891 * there are two steps to flush nat entries:
1892 * #1, flush nat entries to journal in current hot data summary block.
1893 * #2, flush nat entries to nat page.
1894 */
1895 list_for_each_entry_safe(nes, tmp, head, set_list) {
1896 struct f2fs_nat_block *nat_blk;
1897 struct nat_entry *ne, *cur;
1898 struct page *page;
1899 nid_t start_nid = nes->start_nid;
Chao Yu2d7b8222014-03-29 11:33:17 +08001900
Chao Yuaec71382014-06-24 09:18:20 +08001901 if (to_journal && !__has_cursum_space(sum, nes->entry_cnt))
1902 to_journal = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001903
Chao Yuaec71382014-06-24 09:18:20 +08001904 if (to_journal) {
1905 mutex_lock(&curseg->curseg_mutex);
1906 } else {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001907 page = get_next_nat_page(sbi, start_nid);
1908 nat_blk = page_address(page);
Chao Yuaec71382014-06-24 09:18:20 +08001909 f2fs_bug_on(!nat_blk);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001910 }
1911
Chao Yuaec71382014-06-24 09:18:20 +08001912 /* flush dirty nats in nat entry set */
1913 list_for_each_entry_safe(ne, cur, &nes->entry_list, list) {
1914 struct f2fs_nat_entry *raw_ne;
1915 nid_t nid = nat_get_nid(ne);
1916 int offset;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001917
Chao Yuaec71382014-06-24 09:18:20 +08001918 if (to_journal) {
1919 offset = lookup_journal_in_cursum(sum,
1920 NAT_JOURNAL, nid, 1);
1921 f2fs_bug_on(offset < 0);
1922 raw_ne = &nat_in_journal(sum, offset);
1923 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1924 } else {
1925 raw_ne = &nat_blk->entries[nid - start_nid];
1926 }
1927 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001928
Chao Yuaec71382014-06-24 09:18:20 +08001929 if (nat_get_blkaddr(ne) == NULL_ADDR &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001930 add_free_nid(sbi, nid, false) <= 0) {
Chao Yuaec71382014-06-24 09:18:20 +08001931 write_lock(&nm_i->nat_tree_lock);
1932 __del_from_nat_cache(nm_i, ne);
1933 write_unlock(&nm_i->nat_tree_lock);
1934 } else {
1935 write_lock(&nm_i->nat_tree_lock);
1936 __clear_nat_cache_dirty(nm_i, ne);
1937 write_unlock(&nm_i->nat_tree_lock);
1938 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001939 }
Chao Yuaec71382014-06-24 09:18:20 +08001940
1941 if (to_journal)
1942 mutex_unlock(&curseg->curseg_mutex);
1943 else
1944 f2fs_put_page(page, 1);
1945
1946 release_nat_entry_set(nes, nm_i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001947 }
Chao Yuaec71382014-06-24 09:18:20 +08001948
1949 f2fs_bug_on(!list_empty(head));
1950 f2fs_bug_on(nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951}
1952
1953static int init_node_manager(struct f2fs_sb_info *sbi)
1954{
1955 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1956 struct f2fs_nm_info *nm_i = NM_I(sbi);
1957 unsigned char *version_bitmap;
1958 unsigned int nat_segs, nat_blocks;
1959
1960 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1961
1962 /* segment_count_nat includes pair segment so divide to 2. */
1963 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1964 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001965
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001966 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1967
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001968 /* not used nids: 0, node, meta, (and root counted as valid node) */
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001969 nm_i->available_nids = nm_i->max_nid - 3;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001970 nm_i->fcnt = 0;
1971 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001972 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001973
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001974 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001975 INIT_LIST_HEAD(&nm_i->free_nid_list);
1976 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1977 INIT_LIST_HEAD(&nm_i->nat_entries);
1978 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
Chao Yuaec71382014-06-24 09:18:20 +08001979 INIT_LIST_HEAD(&nm_i->nat_entry_set);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001980
1981 mutex_init(&nm_i->build_lock);
1982 spin_lock_init(&nm_i->free_nid_list_lock);
1983 rwlock_init(&nm_i->nat_tree_lock);
1984
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001985 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001986 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001987 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1988 if (!version_bitmap)
1989 return -EFAULT;
1990
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001991 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1992 GFP_KERNEL);
1993 if (!nm_i->nat_bitmap)
1994 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001995 return 0;
1996}
1997
1998int build_node_manager(struct f2fs_sb_info *sbi)
1999{
2000 int err;
2001
2002 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2003 if (!sbi->nm_info)
2004 return -ENOMEM;
2005
2006 err = init_node_manager(sbi);
2007 if (err)
2008 return err;
2009
2010 build_free_nids(sbi);
2011 return 0;
2012}
2013
2014void destroy_node_manager(struct f2fs_sb_info *sbi)
2015{
2016 struct f2fs_nm_info *nm_i = NM_I(sbi);
2017 struct free_nid *i, *next_i;
2018 struct nat_entry *natvec[NATVEC_SIZE];
2019 nid_t nid = 0;
2020 unsigned int found;
2021
2022 if (!nm_i)
2023 return;
2024
2025 /* destroy free nid list */
2026 spin_lock(&nm_i->free_nid_list_lock);
2027 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09002028 f2fs_bug_on(i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002029 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002030 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002031 spin_unlock(&nm_i->free_nid_list_lock);
2032 kmem_cache_free(free_nid_slab, i);
2033 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002034 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09002035 f2fs_bug_on(nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002036 spin_unlock(&nm_i->free_nid_list_lock);
2037
2038 /* destroy nat cache */
2039 write_lock(&nm_i->nat_tree_lock);
2040 while ((found = __gang_lookup_nat_cache(nm_i,
2041 nid, NATVEC_SIZE, natvec))) {
2042 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08002043 nid = nat_get_nid(natvec[found - 1]) + 1;
2044 for (idx = 0; idx < found; idx++)
2045 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002046 }
Jaegeuk Kim5d56b672013-10-29 15:14:54 +09002047 f2fs_bug_on(nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002048 write_unlock(&nm_i->nat_tree_lock);
2049
2050 kfree(nm_i->nat_bitmap);
2051 sbi->nm_info = NULL;
2052 kfree(nm_i);
2053}
2054
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002055int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002056{
2057 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002058 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002059 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002060 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002061
2062 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002063 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002064 if (!free_nid_slab)
2065 goto destory_nat_entry;
2066
2067 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2068 sizeof(struct nat_entry_set));
2069 if (!nat_entry_set_slab)
2070 goto destory_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002071 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002072
2073destory_free_nid:
2074 kmem_cache_destroy(free_nid_slab);
2075destory_nat_entry:
2076 kmem_cache_destroy(nat_entry_slab);
2077fail:
2078 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002079}
2080
2081void destroy_node_manager_caches(void)
2082{
Chao Yuaec71382014-06-24 09:18:20 +08002083 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002084 kmem_cache_destroy(free_nid_slab);
2085 kmem_cache_destroy(nat_entry_slab);
2086}