blob: 44b8afef43d926873b250e802dd2ed3245796e69 [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;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090057 unsigned int long flags;
58
59 if (PageDirty(page)) {
60 spin_lock_irqsave(&mapping->tree_lock, flags);
61 radix_tree_tag_clear(&mapping->page_tree,
62 page_index(page),
63 PAGECACHE_TAG_DIRTY);
64 spin_unlock_irqrestore(&mapping->tree_lock, flags);
65
66 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070067 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090068 }
69 ClearPageUptodate(page);
70}
71
72static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
73{
74 pgoff_t index = current_nat_addr(sbi, nid);
75 return get_meta_page(sbi, index);
76}
77
78static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
79{
80 struct page *src_page;
81 struct page *dst_page;
82 pgoff_t src_off;
83 pgoff_t dst_off;
84 void *src_addr;
85 void *dst_addr;
86 struct f2fs_nm_info *nm_i = NM_I(sbi);
87
88 src_off = current_nat_addr(sbi, nid);
89 dst_off = next_nat_addr(sbi, src_off);
90
91 /* get current nat block page with lock */
92 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090093 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -070094 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090095
96 src_addr = page_address(src_page);
97 dst_addr = page_address(dst_page);
98 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
99 set_page_dirty(dst_page);
100 f2fs_put_page(src_page, 1);
101
102 set_to_next_nat(nm_i, nid);
103
104 return dst_page;
105}
106
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900107static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
108{
109 return radix_tree_lookup(&nm_i->nat_root, n);
110}
111
112static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
113 nid_t start, unsigned int nr, struct nat_entry **ep)
114{
115 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
116}
117
118static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
119{
120 list_del(&e->list);
121 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
122 nm_i->nat_cnt--;
123 kmem_cache_free(nat_entry_slab, e);
124}
125
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700126static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
127 struct nat_entry *ne)
128{
129 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
130 struct nat_entry_set *head;
131
132 if (get_nat_flag(ne, IS_DIRTY))
133 return;
134retry:
135 head = radix_tree_lookup(&nm_i->nat_set_root, set);
136 if (!head) {
137 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
138
139 INIT_LIST_HEAD(&head->entry_list);
140 INIT_LIST_HEAD(&head->set_list);
141 head->set = set;
142 head->entry_cnt = 0;
143
144 if (radix_tree_insert(&nm_i->nat_set_root, set, head)) {
145 cond_resched();
146 goto retry;
147 }
148 }
149 list_move_tail(&ne->list, &head->entry_list);
150 nm_i->dirty_nat_cnt++;
151 head->entry_cnt++;
152 set_nat_flag(ne, IS_DIRTY, true);
153}
154
155static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
156 struct nat_entry *ne)
157{
158 nid_t set = ne->ni.nid / NAT_ENTRY_PER_BLOCK;
159 struct nat_entry_set *head;
160
161 head = radix_tree_lookup(&nm_i->nat_set_root, set);
162 if (head) {
163 list_move_tail(&ne->list, &nm_i->nat_entries);
164 set_nat_flag(ne, IS_DIRTY, false);
165 head->entry_cnt--;
166 nm_i->dirty_nat_cnt--;
167 }
168}
169
170static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
171 nid_t start, unsigned int nr, struct nat_entry_set **ep)
172{
173 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
174 start, nr);
175}
176
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700177bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900178{
179 struct f2fs_nm_info *nm_i = NM_I(sbi);
180 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700181 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900182
183 read_lock(&nm_i->nat_tree_lock);
184 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700185 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700186 is_cp = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900187 read_unlock(&nm_i->nat_tree_lock);
188 return is_cp;
189}
190
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700191bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900192{
193 struct f2fs_nm_info *nm_i = NM_I(sbi);
194 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700195 bool fsynced = false;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900196
197 read_lock(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700198 e = __lookup_nat_cache(nm_i, ino);
199 if (e && get_nat_flag(e, HAS_FSYNCED_INODE))
200 fsynced = true;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900201 read_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700202 return fsynced;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900203}
204
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700205bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900206{
207 struct f2fs_nm_info *nm_i = NM_I(sbi);
208 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700209 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900210
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700211 read_lock(&nm_i->nat_tree_lock);
212 e = __lookup_nat_cache(nm_i, ino);
213 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
214 (get_nat_flag(e, IS_CHECKPOINTED) ||
215 get_nat_flag(e, HAS_FSYNCED_INODE)))
216 need_update = false;
217 read_unlock(&nm_i->nat_tree_lock);
218 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900219}
220
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900221static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
222{
223 struct nat_entry *new;
224
225 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
226 if (!new)
227 return NULL;
228 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
229 kmem_cache_free(nat_entry_slab, new);
230 return NULL;
231 }
232 memset(new, 0, sizeof(struct nat_entry));
233 nat_set_nid(new, nid);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700234 nat_reset_flag(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900235 list_add_tail(&new->list, &nm_i->nat_entries);
236 nm_i->nat_cnt++;
237 return new;
238}
239
240static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
241 struct f2fs_nat_entry *ne)
242{
243 struct nat_entry *e;
244retry:
245 write_lock(&nm_i->nat_tree_lock);
246 e = __lookup_nat_cache(nm_i, nid);
247 if (!e) {
248 e = grab_nat_entry(nm_i, nid);
249 if (!e) {
250 write_unlock(&nm_i->nat_tree_lock);
251 goto retry;
252 }
Chao Yu94dac222014-04-17 10:51:05 +0800253 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900254 }
255 write_unlock(&nm_i->nat_tree_lock);
256}
257
258static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900259 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900260{
261 struct f2fs_nm_info *nm_i = NM_I(sbi);
262 struct nat_entry *e;
263retry:
264 write_lock(&nm_i->nat_tree_lock);
265 e = __lookup_nat_cache(nm_i, ni->nid);
266 if (!e) {
267 e = grab_nat_entry(nm_i, ni->nid);
268 if (!e) {
269 write_unlock(&nm_i->nat_tree_lock);
270 goto retry;
271 }
272 e->ni = *ni;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700273 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900274 } else if (new_blkaddr == NEW_ADDR) {
275 /*
276 * when nid is reallocated,
277 * previous nat entry can be remained in nat cache.
278 * So, reinitialize it with new information.
279 */
280 e->ni = *ni;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700281 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900282 }
283
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900284 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700285 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
286 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900287 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700288 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900289 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700290 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900291 nat_get_blkaddr(e) != NULL_ADDR &&
292 new_blkaddr == NEW_ADDR);
293
arter97e1c42042014-08-06 23:22:50 +0900294 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900295 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
296 unsigned char version = nat_get_version(e);
297 nat_set_version(e, inc_node_version(version));
298 }
299
300 /* change address */
301 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700302 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
303 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900304 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900305
306 /* update fsync_mark if its inode nat entry is still alive */
307 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700308 if (e) {
309 if (fsync_done && ni->nid == ni->ino)
310 set_nat_flag(e, HAS_FSYNCED_INODE, true);
311 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
312 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900313 write_unlock(&nm_i->nat_tree_lock);
314}
315
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900316int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900317{
318 struct f2fs_nm_info *nm_i = NM_I(sbi);
319
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900320 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900321 return 0;
322
323 write_lock(&nm_i->nat_tree_lock);
324 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
325 struct nat_entry *ne;
326 ne = list_first_entry(&nm_i->nat_entries,
327 struct nat_entry, list);
328 __del_from_nat_cache(nm_i, ne);
329 nr_shrink--;
330 }
331 write_unlock(&nm_i->nat_tree_lock);
332 return nr_shrink;
333}
334
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900335/*
arter97e1c42042014-08-06 23:22:50 +0900336 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900337 */
338void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
339{
340 struct f2fs_nm_info *nm_i = NM_I(sbi);
341 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
342 struct f2fs_summary_block *sum = curseg->sum_blk;
343 nid_t start_nid = START_NID(nid);
344 struct f2fs_nat_block *nat_blk;
345 struct page *page = NULL;
346 struct f2fs_nat_entry ne;
347 struct nat_entry *e;
348 int i;
349
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900350 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900351 ni->nid = nid;
352
353 /* Check nat cache */
354 read_lock(&nm_i->nat_tree_lock);
355 e = __lookup_nat_cache(nm_i, nid);
356 if (e) {
357 ni->ino = nat_get_ino(e);
358 ni->blk_addr = nat_get_blkaddr(e);
359 ni->version = nat_get_version(e);
360 }
361 read_unlock(&nm_i->nat_tree_lock);
362 if (e)
363 return;
364
365 /* Check current segment summary */
366 mutex_lock(&curseg->curseg_mutex);
367 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
368 if (i >= 0) {
369 ne = nat_in_journal(sum, i);
370 node_info_from_raw_nat(ni, &ne);
371 }
372 mutex_unlock(&curseg->curseg_mutex);
373 if (i >= 0)
374 goto cache;
375
376 /* Fill node_info from nat page */
377 page = get_current_nat_page(sbi, start_nid);
378 nat_blk = (struct f2fs_nat_block *)page_address(page);
379 ne = nat_blk->entries[nid - start_nid];
380 node_info_from_raw_nat(ni, &ne);
381 f2fs_put_page(page, 1);
382cache:
383 /* cache nat entry */
384 cache_nat_entry(NM_I(sbi), nid, &ne);
385}
386
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900387/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900388 * The maximum depth is four.
389 * Offset[0] will have raw inode offset.
390 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900391static int get_node_path(struct f2fs_inode_info *fi, long block,
392 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900393{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900394 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900395 const long direct_blks = ADDRS_PER_BLOCK;
396 const long dptrs_per_blk = NIDS_PER_BLOCK;
397 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
398 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
399 int n = 0;
400 int level = 0;
401
402 noffset[0] = 0;
403
404 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900405 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900406 goto got;
407 }
408 block -= direct_index;
409 if (block < direct_blks) {
410 offset[n++] = NODE_DIR1_BLOCK;
411 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900412 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900413 level = 1;
414 goto got;
415 }
416 block -= direct_blks;
417 if (block < direct_blks) {
418 offset[n++] = NODE_DIR2_BLOCK;
419 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900420 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900421 level = 1;
422 goto got;
423 }
424 block -= direct_blks;
425 if (block < indirect_blks) {
426 offset[n++] = NODE_IND1_BLOCK;
427 noffset[n] = 3;
428 offset[n++] = block / direct_blks;
429 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900430 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900431 level = 2;
432 goto got;
433 }
434 block -= indirect_blks;
435 if (block < indirect_blks) {
436 offset[n++] = NODE_IND2_BLOCK;
437 noffset[n] = 4 + dptrs_per_blk;
438 offset[n++] = block / direct_blks;
439 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900440 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900441 level = 2;
442 goto got;
443 }
444 block -= indirect_blks;
445 if (block < dindirect_blks) {
446 offset[n++] = NODE_DIND_BLOCK;
447 noffset[n] = 5 + (dptrs_per_blk * 2);
448 offset[n++] = block / indirect_blks;
449 noffset[n] = 6 + (dptrs_per_blk * 2) +
450 offset[n - 1] * (dptrs_per_blk + 1);
451 offset[n++] = (block / direct_blks) % dptrs_per_blk;
452 noffset[n] = 7 + (dptrs_per_blk * 2) +
453 offset[n - 2] * (dptrs_per_blk + 1) +
454 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900455 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900456 level = 3;
457 goto got;
458 } else {
459 BUG();
460 }
461got:
462 return level;
463}
464
465/*
466 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800467 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
468 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900469 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900470 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900471int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900472{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700473 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900474 struct page *npage[4];
475 struct page *parent;
476 int offset[4];
477 unsigned int noffset[4];
478 nid_t nids[4];
479 int level, i;
480 int err = 0;
481
Jaegeuk Kimde936532013-08-12 21:08:03 +0900482 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900483
484 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900485 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900486
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900487 if (!npage[0]) {
488 npage[0] = get_node_page(sbi, nids[0]);
489 if (IS_ERR(npage[0]))
490 return PTR_ERR(npage[0]);
491 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900492 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900493 if (level != 0)
494 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900495 dn->inode_page = npage[0];
496 dn->inode_page_locked = true;
497
498 /* get indirect or direct nodes */
499 for (i = 1; i <= level; i++) {
500 bool done = false;
501
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900502 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900503 /* alloc new node */
504 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505 err = -ENOSPC;
506 goto release_pages;
507 }
508
509 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900510 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900511 if (IS_ERR(npage[i])) {
512 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900513 err = PTR_ERR(npage[i]);
514 goto release_pages;
515 }
516
517 set_nid(parent, offset[i - 1], nids[i], i == 1);
518 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900519 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900520 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521 npage[i] = get_node_page_ra(parent, offset[i - 1]);
522 if (IS_ERR(npage[i])) {
523 err = PTR_ERR(npage[i]);
524 goto release_pages;
525 }
526 done = true;
527 }
528 if (i == 1) {
529 dn->inode_page_locked = false;
530 unlock_page(parent);
531 } else {
532 f2fs_put_page(parent, 1);
533 }
534
535 if (!done) {
536 npage[i] = get_node_page(sbi, nids[i]);
537 if (IS_ERR(npage[i])) {
538 err = PTR_ERR(npage[i]);
539 f2fs_put_page(npage[0], 0);
540 goto release_out;
541 }
542 }
543 if (i < level) {
544 parent = npage[i];
545 nids[i + 1] = get_nid(parent, offset[i], false);
546 }
547 }
548 dn->nid = nids[level];
549 dn->ofs_in_node = offset[level];
550 dn->node_page = npage[level];
551 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
552 return 0;
553
554release_pages:
555 f2fs_put_page(parent, 1);
556 if (i > 1)
557 f2fs_put_page(npage[0], 0);
558release_out:
559 dn->inode_page = NULL;
560 dn->node_page = NULL;
561 return err;
562}
563
564static void truncate_node(struct dnode_of_data *dn)
565{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700566 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900567 struct node_info ni;
568
569 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900570 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700571 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900572 goto invalidate;
573 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700574 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900575
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900576 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900577 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800578 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900579 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900580
581 if (dn->nid == dn->inode->i_ino) {
582 remove_orphan_inode(sbi, dn->nid);
583 dec_valid_inode_count(sbi);
584 } else {
585 sync_inode_page(dn);
586 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900587invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900588 clear_node_page_dirty(dn->node_page);
589 F2FS_SET_SB_DIRT(sbi);
590
591 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900592
593 invalidate_mapping_pages(NODE_MAPPING(sbi),
594 dn->node_page->index, dn->node_page->index);
595
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900596 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900597 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900598}
599
600static int truncate_dnode(struct dnode_of_data *dn)
601{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900602 struct page *page;
603
604 if (dn->nid == 0)
605 return 1;
606
607 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700608 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900609 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
610 return 1;
611 else if (IS_ERR(page))
612 return PTR_ERR(page);
613
614 /* Make dnode_of_data for parameter */
615 dn->node_page = page;
616 dn->ofs_in_node = 0;
617 truncate_data_blocks(dn);
618 truncate_node(dn);
619 return 1;
620}
621
622static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
623 int ofs, int depth)
624{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900625 struct dnode_of_data rdn = *dn;
626 struct page *page;
627 struct f2fs_node *rn;
628 nid_t child_nid;
629 unsigned int child_nofs;
630 int freed = 0;
631 int i, ret;
632
633 if (dn->nid == 0)
634 return NIDS_PER_BLOCK + 1;
635
Namjae Jeon51dd6242013-04-20 01:28:52 +0900636 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
637
Jaegeuk Kim40813632014-09-02 15:31:18 -0700638 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900639 if (IS_ERR(page)) {
640 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900641 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900642 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900643
Gu Zheng45590712013-07-15 17:57:38 +0800644 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900645 if (depth < 3) {
646 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
647 child_nid = le32_to_cpu(rn->in.nid[i]);
648 if (child_nid == 0)
649 continue;
650 rdn.nid = child_nid;
651 ret = truncate_dnode(&rdn);
652 if (ret < 0)
653 goto out_err;
654 set_nid(page, i, 0, false);
655 }
656 } else {
657 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
658 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
659 child_nid = le32_to_cpu(rn->in.nid[i]);
660 if (child_nid == 0) {
661 child_nofs += NIDS_PER_BLOCK + 1;
662 continue;
663 }
664 rdn.nid = child_nid;
665 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
666 if (ret == (NIDS_PER_BLOCK + 1)) {
667 set_nid(page, i, 0, false);
668 child_nofs += ret;
669 } else if (ret < 0 && ret != -ENOENT) {
670 goto out_err;
671 }
672 }
673 freed = child_nofs;
674 }
675
676 if (!ofs) {
677 /* remove current indirect node */
678 dn->node_page = page;
679 truncate_node(dn);
680 freed++;
681 } else {
682 f2fs_put_page(page, 1);
683 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900684 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900685 return freed;
686
687out_err:
688 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900689 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900690 return ret;
691}
692
693static int truncate_partial_nodes(struct dnode_of_data *dn,
694 struct f2fs_inode *ri, int *offset, int depth)
695{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900696 struct page *pages[2];
697 nid_t nid[3];
698 nid_t child_nid;
699 int err = 0;
700 int i;
701 int idx = depth - 2;
702
703 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
704 if (!nid[0])
705 return 0;
706
707 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800708 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900709 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700710 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900711 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900712 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800713 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900714 goto fail;
715 }
716 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
717 }
718
719 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800720 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900721 child_nid = get_nid(pages[idx], i, false);
722 if (!child_nid)
723 continue;
724 dn->nid = child_nid;
725 err = truncate_dnode(dn);
726 if (err < 0)
727 goto fail;
728 set_nid(pages[idx], i, 0, false);
729 }
730
shifei10.gea225dca2013-10-29 15:32:34 +0800731 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732 dn->node_page = pages[idx];
733 dn->nid = nid[idx];
734 truncate_node(dn);
735 } else {
736 f2fs_put_page(pages[idx], 1);
737 }
738 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800739 offset[idx + 1] = 0;
740 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900741fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800742 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900743 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900744
745 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
746
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900747 return err;
748}
749
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900750/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900751 * All the block addresses of data and nodes should be nullified.
752 */
753int truncate_inode_blocks(struct inode *inode, pgoff_t from)
754{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700755 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900756 int err = 0, cont = 1;
757 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900758 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900759 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900760 struct dnode_of_data dn;
761 struct page *page;
762
Namjae Jeon51dd6242013-04-20 01:28:52 +0900763 trace_f2fs_truncate_inode_blocks_enter(inode, from);
764
Jaegeuk Kimde936532013-08-12 21:08:03 +0900765 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900766restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900767 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900768 if (IS_ERR(page)) {
769 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900770 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900771 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772
773 set_new_dnode(&dn, inode, page, NULL, 0);
774 unlock_page(page);
775
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900776 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900777 switch (level) {
778 case 0:
779 case 1:
780 nofs = noffset[1];
781 break;
782 case 2:
783 nofs = noffset[1];
784 if (!offset[level - 1])
785 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900786 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900787 if (err < 0 && err != -ENOENT)
788 goto fail;
789 nofs += 1 + NIDS_PER_BLOCK;
790 break;
791 case 3:
792 nofs = 5 + 2 * NIDS_PER_BLOCK;
793 if (!offset[level - 1])
794 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900795 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900796 if (err < 0 && err != -ENOENT)
797 goto fail;
798 break;
799 default:
800 BUG();
801 }
802
803skip_partial:
804 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900805 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900806 switch (offset[0]) {
807 case NODE_DIR1_BLOCK:
808 case NODE_DIR2_BLOCK:
809 err = truncate_dnode(&dn);
810 break;
811
812 case NODE_IND1_BLOCK:
813 case NODE_IND2_BLOCK:
814 err = truncate_nodes(&dn, nofs, offset[1], 2);
815 break;
816
817 case NODE_DIND_BLOCK:
818 err = truncate_nodes(&dn, nofs, offset[1], 3);
819 cont = 0;
820 break;
821
822 default:
823 BUG();
824 }
825 if (err < 0 && err != -ENOENT)
826 goto fail;
827 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900828 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900829 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900830 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900831 f2fs_put_page(page, 1);
832 goto restart;
833 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900834 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900835 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900836 set_page_dirty(page);
837 unlock_page(page);
838 }
839 offset[1] = 0;
840 offset[0]++;
841 nofs += err;
842 }
843fail:
844 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900845 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900846 return err > 0 ? 0 : err;
847}
848
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900849int truncate_xattr_node(struct inode *inode, struct page *page)
850{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700851 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900852 nid_t nid = F2FS_I(inode)->i_xattr_nid;
853 struct dnode_of_data dn;
854 struct page *npage;
855
856 if (!nid)
857 return 0;
858
859 npage = get_node_page(sbi, nid);
860 if (IS_ERR(npage))
861 return PTR_ERR(npage);
862
863 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900864
865 /* need to do checkpoint during fsync */
866 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
867
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900868 set_new_dnode(&dn, inode, page, npage, nid);
869
870 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800871 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900872 truncate_node(&dn);
873 return 0;
874}
875
Jaegeuk Kim39936832012-11-22 16:21:29 +0900876/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800877 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
878 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900879 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800880void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900881{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900882 struct dnode_of_data dn;
883
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700884 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
885 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800886 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900887
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700888 if (truncate_xattr_node(inode, dn.inode_page)) {
889 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800890 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900891 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700892
893 /* remove potential inline_data blocks */
894 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
895 S_ISLNK(inode->i_mode))
896 truncate_data_blocks_range(&dn, 1);
897
arter97e1c42042014-08-06 23:22:50 +0900898 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700899 f2fs_bug_on(F2FS_I_SB(inode),
900 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700901
902 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900903 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900904}
905
Jaegeuk Kima014e032014-06-20 21:44:02 -0700906struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900907{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900908 struct dnode_of_data dn;
909
910 /* allocate inode page for new inode */
911 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900912
913 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900914 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900915}
916
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900917struct page *new_node_page(struct dnode_of_data *dn,
918 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900919{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700920 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900921 struct node_info old_ni, new_ni;
922 struct page *page;
923 int err;
924
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900925 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900926 return ERR_PTR(-EPERM);
927
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900928 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900929 if (!page)
930 return ERR_PTR(-ENOMEM);
931
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900932 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900933 err = -ENOSPC;
934 goto fail;
935 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900936
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900937 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900938
939 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700940 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900941 new_ni = old_ni;
942 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900943 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900944
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900945 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900946 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900947 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900948 SetPageUptodate(page);
949 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900950
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800951 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900952 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
953
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900954 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900955 if (ipage)
956 update_inode(dn->inode, ipage);
957 else
958 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900959 if (ofs == 0)
960 inc_valid_inode_count(sbi);
961
962 return page;
963
964fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900965 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900966 f2fs_put_page(page, 1);
967 return ERR_PTR(err);
968}
969
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900970/*
971 * Caller should do after getting the following values.
972 * 0: f2fs_put_page(page, 0)
973 * LOCKED_PAGE: f2fs_put_page(page, 1)
974 * error: nothing
975 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900976static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900977{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700978 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979 struct node_info ni;
980
981 get_node_info(sbi, page->index, &ni);
982
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900983 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900984 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900986 }
987
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900988 if (PageUptodate(page))
989 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900990
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900991 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900992}
993
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900994/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900995 * Readahead a node page
996 */
997void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
998{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001000 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001001
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001002 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001003 if (apage && PageUptodate(apage)) {
1004 f2fs_put_page(apage, 0);
1005 return;
1006 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001007 f2fs_put_page(apage, 0);
1008
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001009 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001010 if (!apage)
1011 return;
1012
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001013 err = read_node_page(apage, READA);
1014 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001015 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001016 else if (err == LOCKED_PAGE)
1017 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001018}
1019
1020struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1021{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001022 struct page *page;
1023 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001024repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001025 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001026 if (!page)
1027 return ERR_PTR(-ENOMEM);
1028
1029 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001030 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001031 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001032 else if (err == LOCKED_PAGE)
1033 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001034
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001035 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001036 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001037 f2fs_put_page(page, 1);
1038 return ERR_PTR(-EIO);
1039 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001040 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001041 f2fs_put_page(page, 1);
1042 goto repeat;
1043 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001044got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001045 return page;
1046}
1047
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001048/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001049 * Return a locked page for the desired node page.
1050 * And, readahead MAX_RA_NODE number of node pages.
1051 */
1052struct page *get_node_page_ra(struct page *parent, int start)
1053{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001054 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001055 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001056 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001057 int err, i, end;
1058 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001059
1060 /* First, try getting the desired direct node. */
1061 nid = get_nid(parent, start, false);
1062 if (!nid)
1063 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001064repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001065 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001066 if (!page)
1067 return ERR_PTR(-ENOMEM);
1068
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001069 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001070 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001071 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001072 else if (err == LOCKED_PAGE)
1073 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001075 blk_start_plug(&plug);
1076
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001077 /* Then, try readahead for siblings of the desired node */
1078 end = start + MAX_RA_NODE;
1079 end = min(end, NIDS_PER_BLOCK);
1080 for (i = start + 1; i < end; i++) {
1081 nid = get_nid(parent, i, false);
1082 if (!nid)
1083 continue;
1084 ra_node_page(sbi, nid);
1085 }
1086
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001087 blk_finish_plug(&plug);
1088
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001089 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001090 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001091 f2fs_put_page(page, 1);
1092 goto repeat;
1093 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001094page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001095 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001096 f2fs_put_page(page, 1);
1097 return ERR_PTR(-EIO);
1098 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001099 return page;
1100}
1101
1102void sync_inode_page(struct dnode_of_data *dn)
1103{
1104 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1105 update_inode(dn->inode, dn->node_page);
1106 } else if (dn->inode_page) {
1107 if (!dn->inode_page_locked)
1108 lock_page(dn->inode_page);
1109 update_inode(dn->inode, dn->inode_page);
1110 if (!dn->inode_page_locked)
1111 unlock_page(dn->inode_page);
1112 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001113 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001114 }
1115}
1116
1117int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1118 struct writeback_control *wbc)
1119{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001120 pgoff_t index, end;
1121 struct pagevec pvec;
1122 int step = ino ? 2 : 0;
1123 int nwritten = 0, wrote = 0;
1124
1125 pagevec_init(&pvec, 0);
1126
1127next_step:
1128 index = 0;
1129 end = LONG_MAX;
1130
1131 while (index <= end) {
1132 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001133 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001134 PAGECACHE_TAG_DIRTY,
1135 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1136 if (nr_pages == 0)
1137 break;
1138
1139 for (i = 0; i < nr_pages; i++) {
1140 struct page *page = pvec.pages[i];
1141
1142 /*
1143 * flushing sequence with step:
1144 * 0. indirect nodes
1145 * 1. dentry dnodes
1146 * 2. file dnodes
1147 */
1148 if (step == 0 && IS_DNODE(page))
1149 continue;
1150 if (step == 1 && (!IS_DNODE(page) ||
1151 is_cold_node(page)))
1152 continue;
1153 if (step == 2 && (!IS_DNODE(page) ||
1154 !is_cold_node(page)))
1155 continue;
1156
1157 /*
1158 * If an fsync mode,
1159 * we should not skip writing node pages.
1160 */
1161 if (ino && ino_of_node(page) == ino)
1162 lock_page(page);
1163 else if (!trylock_page(page))
1164 continue;
1165
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001166 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001167continue_unlock:
1168 unlock_page(page);
1169 continue;
1170 }
1171 if (ino && ino_of_node(page) != ino)
1172 goto continue_unlock;
1173
1174 if (!PageDirty(page)) {
1175 /* someone wrote it for us */
1176 goto continue_unlock;
1177 }
1178
1179 if (!clear_page_dirty_for_io(page))
1180 goto continue_unlock;
1181
1182 /* called by fsync() */
1183 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001184 set_fsync_mark(page, 1);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -07001185 if (IS_INODE(page)) {
1186 if (!is_checkpointed_node(sbi, ino) &&
1187 !has_fsynced_inode(sbi, ino))
1188 set_dentry_mark(page, 1);
1189 else
1190 set_dentry_mark(page, 0);
1191 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001192 nwritten++;
1193 } else {
1194 set_fsync_mark(page, 0);
1195 set_dentry_mark(page, 0);
1196 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001197
1198 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1199 unlock_page(page);
1200 else
1201 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001202
1203 if (--wbc->nr_to_write == 0)
1204 break;
1205 }
1206 pagevec_release(&pvec);
1207 cond_resched();
1208
1209 if (wbc->nr_to_write == 0) {
1210 step = 2;
1211 break;
1212 }
1213 }
1214
1215 if (step < 2) {
1216 step++;
1217 goto next_step;
1218 }
1219
1220 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001221 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001222 return nwritten;
1223}
1224
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001225int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1226{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001227 pgoff_t index = 0, end = LONG_MAX;
1228 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001229 int ret2 = 0, ret = 0;
1230
1231 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001232
1233 while (index <= end) {
1234 int i, nr_pages;
1235 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1236 PAGECACHE_TAG_WRITEBACK,
1237 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1238 if (nr_pages == 0)
1239 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001240
1241 for (i = 0; i < nr_pages; i++) {
1242 struct page *page = pvec.pages[i];
1243
1244 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001245 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001246 continue;
1247
Chao Yu4bf08ff2013-11-04 10:28:33 +08001248 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001249 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001250 if (TestClearPageError(page))
1251 ret = -EIO;
1252 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001253 }
1254 pagevec_release(&pvec);
1255 cond_resched();
1256 }
1257
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001258 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001259 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001260 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001261 ret2 = -EIO;
1262 if (!ret)
1263 ret = ret2;
1264 return ret;
1265}
1266
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001267static int f2fs_write_node_page(struct page *page,
1268 struct writeback_control *wbc)
1269{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001270 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001271 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001272 block_t new_addr;
1273 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001274 struct f2fs_io_info fio = {
1275 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001276 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001277 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001278
Chao Yuecda0de2014-05-06 16:48:26 +08001279 trace_f2fs_writepage(page, NODE);
1280
Chao Yucfb271d2013-12-05 17:15:22 +08001281 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001282 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001283 if (unlikely(f2fs_cp_error(sbi)))
1284 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001285
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001286 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001287
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001288 /* get old block addr of this node page */
1289 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001290 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291
1292 get_node_info(sbi, nid, &ni);
1293
1294 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001295 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001296 dec_page_count(sbi, F2FS_DIRTY_NODES);
1297 unlock_page(page);
1298 return 0;
1299 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001300
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001301 if (wbc->for_reclaim)
1302 goto redirty_out;
Jaegeuk Kim08d80582013-03-13 17:49:22 +09001303
Chao Yub3582c62014-07-03 18:58:39 +08001304 down_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001305 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001306 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001307 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001308 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001309 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310 unlock_page(page);
1311 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001312
1313redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001314 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001315 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001316}
1317
1318static int f2fs_write_node_pages(struct address_space *mapping,
1319 struct writeback_control *wbc)
1320{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001321 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001322 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323
Chao Yue5748432014-05-06 16:51:24 +08001324 trace_f2fs_writepages(mapping->host, wbc, NODE);
1325
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001326 /* balancing f2fs's metadata in background */
1327 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001328
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001329 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001330 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001331 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001332
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001333 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001334 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001335 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001336 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001337 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001338
1339skip_write:
1340 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1341 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001342}
1343
1344static int f2fs_set_node_page_dirty(struct page *page)
1345{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001346 trace_f2fs_set_page_dirty(page, NODE);
1347
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001348 SetPageUptodate(page);
1349 if (!PageDirty(page)) {
1350 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001351 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001352 SetPagePrivate(page);
1353 return 1;
1354 }
1355 return 0;
1356}
1357
Lukas Czernerd47992f2013-05-21 23:17:23 -04001358static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1359 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001360{
1361 struct inode *inode = page->mapping->host;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001362 if (PageDirty(page))
Jaegeuk Kim40813632014-09-02 15:31:18 -07001363 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001364 ClearPagePrivate(page);
1365}
1366
1367static int f2fs_release_node_page(struct page *page, gfp_t wait)
1368{
1369 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001370 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001371}
1372
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001373/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001374 * Structure of the f2fs node operations
1375 */
1376const struct address_space_operations f2fs_node_aops = {
1377 .writepage = f2fs_write_node_page,
1378 .writepages = f2fs_write_node_pages,
1379 .set_page_dirty = f2fs_set_node_page_dirty,
1380 .invalidatepage = f2fs_invalidate_node_page,
1381 .releasepage = f2fs_release_node_page,
1382};
1383
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001384static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1385 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001387 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001388}
1389
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001390static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1391 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392{
1393 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001394 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001395}
1396
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001397static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001398{
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 free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001401 struct nat_entry *ne;
1402 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001403
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001404 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001405 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001406
1407 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001408 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001409 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001410
Gu Zheng7bd59382013-10-22 14:52:26 +08001411 if (build) {
1412 /* do not add allocated nids */
1413 read_lock(&nm_i->nat_tree_lock);
1414 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001415 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001416 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1417 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001418 allocated = true;
1419 read_unlock(&nm_i->nat_tree_lock);
1420 if (allocated)
1421 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001423
1424 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425 i->nid = nid;
1426 i->state = NID_NEW;
1427
1428 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001429 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001430 spin_unlock(&nm_i->free_nid_list_lock);
1431 kmem_cache_free(free_nid_slab, i);
1432 return 0;
1433 }
1434 list_add_tail(&i->list, &nm_i->free_nid_list);
1435 nm_i->fcnt++;
1436 spin_unlock(&nm_i->free_nid_list_lock);
1437 return 1;
1438}
1439
1440static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1441{
1442 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001443 bool need_free = false;
1444
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001445 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001446 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001447 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001448 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001449 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001450 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001451 }
1452 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001453
1454 if (need_free)
1455 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001456}
1457
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001458static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459 struct page *nat_page, nid_t start_nid)
1460{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001461 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001462 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1463 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001464 int i;
1465
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001466 i = start_nid % NAT_ENTRY_PER_BLOCK;
1467
1468 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001469
Chao Yucfb271d2013-12-05 17:15:22 +08001470 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001471 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001472
1473 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001474 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001475 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001476 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001477 break;
1478 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001479 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001480}
1481
1482static void build_free_nids(struct f2fs_sb_info *sbi)
1483{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001484 struct f2fs_nm_info *nm_i = NM_I(sbi);
1485 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1486 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001487 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001488 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001489
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001490 /* Enough entries */
1491 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1492 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001493
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001494 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001495 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001496
1497 while (1) {
1498 struct page *page = get_current_nat_page(sbi, nid);
1499
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001500 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501 f2fs_put_page(page, 1);
1502
1503 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001504 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001505 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001506
1507 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001508 break;
1509 }
1510
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001511 /* go to the next free nat pages to find free nids abundantly */
1512 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001513
1514 /* find free nids from current sum_pages */
1515 mutex_lock(&curseg->curseg_mutex);
1516 for (i = 0; i < nats_in_cursum(sum); i++) {
1517 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1518 nid = le32_to_cpu(nid_in_journal(sum, i));
1519 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001520 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001521 else
1522 remove_free_nid(nm_i, nid);
1523 }
1524 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001525}
1526
1527/*
1528 * If this function returns success, caller can obtain a new nid
1529 * from second parameter of this function.
1530 * The returned nid could be used ino as well as nid when inode is created.
1531 */
1532bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1533{
1534 struct f2fs_nm_info *nm_i = NM_I(sbi);
1535 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001536retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001537 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001538 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001539
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001540 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001541
1542 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001543 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001544 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001545 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001546 if (i->state == NID_NEW)
1547 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001548
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001549 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001550 *nid = i->nid;
1551 i->state = NID_ALLOC;
1552 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001553 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001554 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001555 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001556 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001557
1558 /* Let's scan nat pages and its caches to get free nids */
1559 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001560 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001561 mutex_unlock(&nm_i->build_lock);
1562 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001563}
1564
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001565/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001566 * alloc_nid() should be called prior to this function.
1567 */
1568void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1569{
1570 struct f2fs_nm_info *nm_i = NM_I(sbi);
1571 struct free_nid *i;
1572
1573 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001574 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001575 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001576 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001578
1579 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001580}
1581
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001582/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001583 * alloc_nid() should be called prior to this function.
1584 */
1585void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1586{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001587 struct f2fs_nm_info *nm_i = NM_I(sbi);
1588 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001589 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001590
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001591 if (!nid)
1592 return;
1593
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001594 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001595 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001596 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001597 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001598 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001599 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001600 } else {
1601 i->state = NID_NEW;
1602 nm_i->fcnt++;
1603 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001604 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001605
1606 if (need_free)
1607 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001608}
1609
Chao Yu70cfed82014-08-02 15:26:04 +08001610void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001611{
Chao Yu28cdce02014-03-11 13:37:38 +08001612 void *src_addr, *dst_addr;
1613 size_t inline_size;
1614 struct page *ipage;
1615 struct f2fs_inode *ri;
1616
Jaegeuk Kim40813632014-09-02 15:31:18 -07001617 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001618 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001619
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001620 ri = F2FS_INODE(page);
1621 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1622 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1623 goto update_inode;
1624 }
1625
Chao Yu28cdce02014-03-11 13:37:38 +08001626 dst_addr = inline_xattr_addr(ipage);
1627 src_addr = inline_xattr_addr(page);
1628 inline_size = inline_xattr_size(inode);
1629
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001630 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001631 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001632update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001633 update_inode(inode, ipage);
1634 f2fs_put_page(ipage, 1);
1635}
1636
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001637void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001638{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001639 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001640 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1641 nid_t new_xnid = nid_of_node(page);
1642 struct node_info ni;
1643
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001644 /* 1: invalidate the previous xattr nid */
1645 if (!prev_xnid)
1646 goto recover_xnid;
1647
1648 /* Deallocate node address */
1649 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001650 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001651 invalidate_blocks(sbi, ni.blk_addr);
1652 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001653 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001654
1655recover_xnid:
1656 /* 2: allocate new xattr nid */
1657 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001658 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001659
1660 remove_free_nid(NM_I(sbi), new_xnid);
1661 get_node_info(sbi, new_xnid, &ni);
1662 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001663 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001664 F2FS_I(inode)->i_xattr_nid = new_xnid;
1665
1666 /* 3: update xattr blkaddr */
1667 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001668 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001669
1670 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001671}
1672
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001673int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1674{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001675 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001676 nid_t ino = ino_of_node(page);
1677 struct node_info old_ni, new_ni;
1678 struct page *ipage;
1679
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001680 get_node_info(sbi, ino, &old_ni);
1681
1682 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1683 return -EINVAL;
1684
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001685 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001686 if (!ipage)
1687 return -ENOMEM;
1688
arter97e1c42042014-08-06 23:22:50 +09001689 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001690 remove_free_nid(NM_I(sbi), ino);
1691
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001692 SetPageUptodate(ipage);
1693 fill_node_footer(ipage, ino, ino, 0, true);
1694
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001695 src = F2FS_INODE(page);
1696 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001697
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001698 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1699 dst->i_size = 0;
1700 dst->i_blocks = cpu_to_le64(1);
1701 dst->i_links = cpu_to_le32(1);
1702 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001703 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001704
1705 new_ni = old_ni;
1706 new_ni.ino = ino;
1707
Chao Yucfb271d2013-12-05 17:15:22 +08001708 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001709 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001710 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001711 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001712 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001713 f2fs_put_page(ipage, 1);
1714 return 0;
1715}
1716
Chao Yu9af0ff12013-11-22 15:48:54 +08001717/*
1718 * ra_sum_pages() merge contiguous pages into one bio and submit.
arter97e1c42042014-08-06 23:22:50 +09001719 * these pre-read pages are allocated in bd_inode's mapping tree.
Chao Yu9af0ff12013-11-22 15:48:54 +08001720 */
Chao Yubac4eef2014-05-27 08:41:07 +08001721static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages,
Chao Yu9af0ff12013-11-22 15:48:54 +08001722 int start, int nrpages)
1723{
Chao Yubac4eef2014-05-27 08:41:07 +08001724 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1725 struct address_space *mapping = inode->i_mapping;
1726 int i, page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001727 struct f2fs_io_info fio = {
1728 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001729 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001730 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001731
Chao Yubac4eef2014-05-27 08:41:07 +08001732 for (i = 0; page_idx < start + nrpages; page_idx++, i++) {
1733 /* alloc page in bd_inode for reading node summary info */
1734 pages[i] = grab_cache_page(mapping, page_idx);
1735 if (!pages[i])
Gu Zhengd6537882014-03-07 18:43:36 +08001736 break;
Chao Yubac4eef2014-05-27 08:41:07 +08001737 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001738 }
1739
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001740 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yubac4eef2014-05-27 08:41:07 +08001741 return i;
Chao Yu9af0ff12013-11-22 15:48:54 +08001742}
1743
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001744int restore_node_summary(struct f2fs_sb_info *sbi,
1745 unsigned int segno, struct f2fs_summary_block *sum)
1746{
1747 struct f2fs_node *rn;
1748 struct f2fs_summary *sum_entry;
Chao Yubac4eef2014-05-27 08:41:07 +08001749 struct inode *inode = sbi->sb->s_bdev->bd_inode;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001750 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001751 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yubac4eef2014-05-27 08:41:07 +08001752 struct page *pages[bio_blocks];
1753 int i, idx, last_offset, nrpages, err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001754
1755 /* scan the node segment */
1756 last_offset = sbi->blocks_per_seg;
1757 addr = START_BLOCK(sbi, segno);
1758 sum_entry = &sum->entries[0];
1759
Gu Zhengd6537882014-03-07 18:43:36 +08001760 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001761 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001762
arter97e1c42042014-08-06 23:22:50 +09001763 /* readahead node pages */
Chao Yubac4eef2014-05-27 08:41:07 +08001764 nrpages = ra_sum_pages(sbi, pages, addr, nrpages);
Gu Zhengd6537882014-03-07 18:43:36 +08001765 if (!nrpages)
1766 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001767
Chao Yubac4eef2014-05-27 08:41:07 +08001768 for (idx = 0; idx < nrpages; idx++) {
Gu Zhengd6537882014-03-07 18:43:36 +08001769 if (err)
1770 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001771
Chao Yubac4eef2014-05-27 08:41:07 +08001772 lock_page(pages[idx]);
1773 if (unlikely(!PageUptodate(pages[idx]))) {
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001774 err = -EIO;
1775 } else {
Chao Yubac4eef2014-05-27 08:41:07 +08001776 rn = F2FS_NODE(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001777 sum_entry->nid = rn->footer.nid;
1778 sum_entry->version = 0;
1779 sum_entry->ofs_in_node = 0;
1780 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001781 }
Chao Yubac4eef2014-05-27 08:41:07 +08001782 unlock_page(pages[idx]);
Gu Zhengd6537882014-03-07 18:43:36 +08001783skip:
Chao Yubac4eef2014-05-27 08:41:07 +08001784 page_cache_release(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001785 }
Chao Yubac4eef2014-05-27 08:41:07 +08001786
1787 invalidate_mapping_pages(inode->i_mapping, addr,
1788 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001789 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001790 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001791}
1792
Chao Yuaec71382014-06-24 09:18:20 +08001793static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001794{
1795 struct f2fs_nm_info *nm_i = NM_I(sbi);
1796 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1797 struct f2fs_summary_block *sum = curseg->sum_blk;
1798 int i;
1799
1800 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001801 for (i = 0; i < nats_in_cursum(sum); i++) {
1802 struct nat_entry *ne;
1803 struct f2fs_nat_entry raw_ne;
1804 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1805
1806 raw_ne = nat_in_journal(sum, i);
1807retry:
1808 write_lock(&nm_i->nat_tree_lock);
1809 ne = __lookup_nat_cache(nm_i, nid);
Chao Yuaec71382014-06-24 09:18:20 +08001810 if (ne)
1811 goto found;
1812
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001813 ne = grab_nat_entry(nm_i, nid);
1814 if (!ne) {
1815 write_unlock(&nm_i->nat_tree_lock);
1816 goto retry;
1817 }
Chao Yu94dac222014-04-17 10:51:05 +08001818 node_info_from_raw_nat(&ne->ni, &raw_ne);
Chao Yuaec71382014-06-24 09:18:20 +08001819found:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001820 __set_nat_cache_dirty(nm_i, ne);
1821 write_unlock(&nm_i->nat_tree_lock);
1822 }
1823 update_nats_in_cursum(sum, -i);
1824 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001825}
1826
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001827static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1828 struct list_head *head, int max)
1829{
1830 struct nat_entry_set *cur;
1831
1832 if (nes->entry_cnt >= max)
1833 goto add_out;
1834
1835 list_for_each_entry(cur, head, set_list) {
1836 if (cur->entry_cnt >= nes->entry_cnt) {
1837 list_add(&nes->set_list, cur->set_list.prev);
1838 return;
1839 }
1840 }
1841add_out:
1842 list_add_tail(&nes->set_list, head);
1843}
1844
1845static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1846 struct nat_entry_set *set)
1847{
1848 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1849 struct f2fs_summary_block *sum = curseg->sum_blk;
1850 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1851 bool to_journal = true;
1852 struct f2fs_nat_block *nat_blk;
1853 struct nat_entry *ne, *cur;
1854 struct page *page = NULL;
1855
1856 /*
1857 * there are two steps to flush nat entries:
1858 * #1, flush nat entries to journal in current hot data summary block.
1859 * #2, flush nat entries to nat page.
1860 */
1861 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1862 to_journal = false;
1863
1864 if (to_journal) {
1865 mutex_lock(&curseg->curseg_mutex);
1866 } else {
1867 page = get_next_nat_page(sbi, start_nid);
1868 nat_blk = page_address(page);
1869 f2fs_bug_on(sbi, !nat_blk);
1870 }
1871
1872 /* flush dirty nats in nat entry set */
1873 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1874 struct f2fs_nat_entry *raw_ne;
1875 nid_t nid = nat_get_nid(ne);
1876 int offset;
1877
1878 if (nat_get_blkaddr(ne) == NEW_ADDR)
1879 continue;
1880
1881 if (to_journal) {
1882 offset = lookup_journal_in_cursum(sum,
1883 NAT_JOURNAL, nid, 1);
1884 f2fs_bug_on(sbi, offset < 0);
1885 raw_ne = &nat_in_journal(sum, offset);
1886 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1887 } else {
1888 raw_ne = &nat_blk->entries[nid - start_nid];
1889 }
1890 raw_nat_from_node_info(raw_ne, &ne->ni);
1891
1892 write_lock(&NM_I(sbi)->nat_tree_lock);
1893 nat_reset_flag(ne);
1894 __clear_nat_cache_dirty(NM_I(sbi), ne);
1895 write_unlock(&NM_I(sbi)->nat_tree_lock);
1896
1897 if (nat_get_blkaddr(ne) == NULL_ADDR)
1898 add_free_nid(sbi, nid, false);
1899 }
1900
1901 if (to_journal)
1902 mutex_unlock(&curseg->curseg_mutex);
1903 else
1904 f2fs_put_page(page, 1);
1905
1906 if (!set->entry_cnt) {
1907 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1908 kmem_cache_free(nat_entry_set_slab, set);
1909 }
1910}
1911
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001912/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001913 * This function is called during the checkpointing process.
1914 */
1915void flush_nat_entries(struct f2fs_sb_info *sbi)
1916{
1917 struct f2fs_nm_info *nm_i = NM_I(sbi);
1918 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1919 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001920 struct nat_entry_set *setvec[NATVEC_SIZE];
1921 struct nat_entry_set *set, *tmp;
1922 unsigned int found;
1923 nid_t set_idx = 0;
1924 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001925
Chao Yuaec71382014-06-24 09:18:20 +08001926 /*
1927 * if there are no enough space in journal to store dirty nat
1928 * entries, remove all entries from journal and merge them
1929 * into nat entry set.
1930 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001931 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001932 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001933
Chao Yuaec71382014-06-24 09:18:20 +08001934 if (!nm_i->dirty_nat_cnt)
1935 return;
Chao Yu2d7b8222014-03-29 11:33:17 +08001936
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001937 while ((found = __gang_lookup_nat_set(nm_i,
1938 set_idx, NATVEC_SIZE, setvec))) {
1939 unsigned idx;
1940 set_idx = setvec[found - 1]->set + 1;
1941 for (idx = 0; idx < found; idx++)
1942 __adjust_nat_entry_set(setvec[idx], &sets,
1943 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001944 }
Chao Yuaec71382014-06-24 09:18:20 +08001945
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001946 /* flush dirty nats in nat entry set */
1947 list_for_each_entry_safe(set, tmp, &sets, set_list)
1948 __flush_nat_entry_set(sbi, set);
1949
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001950 f2fs_bug_on(sbi, 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) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001969 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
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);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001977 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001978 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001979
1980 mutex_init(&nm_i->build_lock);
1981 spin_lock_init(&nm_i->free_nid_list_lock);
1982 rwlock_init(&nm_i->nat_tree_lock);
1983
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001984 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001985 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001986 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1987 if (!version_bitmap)
1988 return -EFAULT;
1989
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001990 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1991 GFP_KERNEL);
1992 if (!nm_i->nat_bitmap)
1993 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001994 return 0;
1995}
1996
1997int build_node_manager(struct f2fs_sb_info *sbi)
1998{
1999 int err;
2000
2001 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2002 if (!sbi->nm_info)
2003 return -ENOMEM;
2004
2005 err = init_node_manager(sbi);
2006 if (err)
2007 return err;
2008
2009 build_free_nids(sbi);
2010 return 0;
2011}
2012
2013void destroy_node_manager(struct f2fs_sb_info *sbi)
2014{
2015 struct f2fs_nm_info *nm_i = NM_I(sbi);
2016 struct free_nid *i, *next_i;
2017 struct nat_entry *natvec[NATVEC_SIZE];
2018 nid_t nid = 0;
2019 unsigned int found;
2020
2021 if (!nm_i)
2022 return;
2023
2024 /* destroy free nid list */
2025 spin_lock(&nm_i->free_nid_list_lock);
2026 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002027 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002028 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002029 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002030 spin_unlock(&nm_i->free_nid_list_lock);
2031 kmem_cache_free(free_nid_slab, i);
2032 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002033 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002034 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002035 spin_unlock(&nm_i->free_nid_list_lock);
2036
2037 /* destroy nat cache */
2038 write_lock(&nm_i->nat_tree_lock);
2039 while ((found = __gang_lookup_nat_cache(nm_i,
2040 nid, NATVEC_SIZE, natvec))) {
2041 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08002042 nid = nat_get_nid(natvec[found - 1]) + 1;
2043 for (idx = 0; idx < found; idx++)
2044 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002045 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002046 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002047 write_unlock(&nm_i->nat_tree_lock);
2048
2049 kfree(nm_i->nat_bitmap);
2050 sbi->nm_info = NULL;
2051 kfree(nm_i);
2052}
2053
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002054int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002055{
2056 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002057 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002058 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002059 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002060
2061 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002062 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002063 if (!free_nid_slab)
2064 goto destory_nat_entry;
2065
2066 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2067 sizeof(struct nat_entry_set));
2068 if (!nat_entry_set_slab)
2069 goto destory_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002070 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002071
2072destory_free_nid:
2073 kmem_cache_destroy(free_nid_slab);
2074destory_nat_entry:
2075 kmem_cache_destroy(nat_entry_slab);
2076fail:
2077 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002078}
2079
2080void destroy_node_manager_caches(void)
2081{
Chao Yuaec71382014-06-24 09:18:20 +08002082 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002083 kmem_cache_destroy(free_nid_slab);
2084 kmem_cache_destroy(nat_entry_slab);
2085}