blob: 4d9bedfe101c9b843524fbbbe3edbc32f4d3eb8c [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"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080022#include "trace.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090023#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090024
Gu Zhengf978f5a2014-02-21 18:08:29 +080025#define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
26
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090027static struct kmem_cache *nat_entry_slab;
28static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080029static struct kmem_cache *nat_entry_set_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090030
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090031bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090032{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090033 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090034 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080035 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090036 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090037 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090038
39 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080040
41 /* only uses low memory */
42 avail_ram = val.totalram - val.totalhigh;
43
Chao Yu429511c2015-02-05 17:54:31 +080044 /*
45 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
46 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090047 if (type == FREE_NIDS) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080048 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
49 PAGE_CACHE_SHIFT;
50 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090051 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080052 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
53 PAGE_CACHE_SHIFT;
54 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090055 } else if (type == DIRTY_DENTS) {
Tejun Heoa88a3412015-05-22 17:13:28 -040056 if (sbi->sb->s_bdi->wb.dirty_exceeded)
Jaegeuk Kim2743f862014-06-28 01:00:41 +090057 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090058 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080059 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
60 } else if (type == INO_ENTRIES) {
61 int i;
62
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080063 for (i = 0; i <= UPDATE_INO; i++)
Chao Yu67298802014-11-18 11:18:36 +080064 mem_size += (sbi->im[i].ino_num *
65 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080066 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080067 } else if (type == EXTENT_CACHE) {
68 mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) +
69 atomic_read(&sbi->total_ext_node) *
70 sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
71 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080072 } else {
Tejun Heoa88a3412015-05-22 17:13:28 -040073 if (sbi->sb->s_bdi->wb.dirty_exceeded)
Jaegeuk Kim1e843712014-12-09 06:08:59 -080074 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090075 }
76 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090077}
78
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090079static void clear_node_page_dirty(struct page *page)
80{
81 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090082 unsigned int long flags;
83
84 if (PageDirty(page)) {
85 spin_lock_irqsave(&mapping->tree_lock, flags);
86 radix_tree_tag_clear(&mapping->page_tree,
87 page_index(page),
88 PAGECACHE_TAG_DIRTY);
89 spin_unlock_irqrestore(&mapping->tree_lock, flags);
90
91 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070092 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090093 }
94 ClearPageUptodate(page);
95}
96
97static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
98{
99 pgoff_t index = current_nat_addr(sbi, nid);
100 return get_meta_page(sbi, index);
101}
102
103static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
104{
105 struct page *src_page;
106 struct page *dst_page;
107 pgoff_t src_off;
108 pgoff_t dst_off;
109 void *src_addr;
110 void *dst_addr;
111 struct f2fs_nm_info *nm_i = NM_I(sbi);
112
113 src_off = current_nat_addr(sbi, nid);
114 dst_off = next_nat_addr(sbi, src_off);
115
116 /* get current nat block page with lock */
117 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900118 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700119 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900120
121 src_addr = page_address(src_page);
122 dst_addr = page_address(dst_page);
123 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
124 set_page_dirty(dst_page);
125 f2fs_put_page(src_page, 1);
126
127 set_to_next_nat(nm_i, nid);
128
129 return dst_page;
130}
131
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900132static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
133{
134 return radix_tree_lookup(&nm_i->nat_root, n);
135}
136
137static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
138 nid_t start, unsigned int nr, struct nat_entry **ep)
139{
140 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
141}
142
143static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
144{
145 list_del(&e->list);
146 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
147 nm_i->nat_cnt--;
148 kmem_cache_free(nat_entry_slab, e);
149}
150
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700151static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
152 struct nat_entry *ne)
153{
154 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
155 struct nat_entry_set *head;
156
157 if (get_nat_flag(ne, IS_DIRTY))
158 return;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800159
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700160 head = radix_tree_lookup(&nm_i->nat_set_root, set);
161 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700162 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700163
164 INIT_LIST_HEAD(&head->entry_list);
165 INIT_LIST_HEAD(&head->set_list);
166 head->set = set;
167 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800168 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700169 }
170 list_move_tail(&ne->list, &head->entry_list);
171 nm_i->dirty_nat_cnt++;
172 head->entry_cnt++;
173 set_nat_flag(ne, IS_DIRTY, true);
174}
175
176static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
177 struct nat_entry *ne)
178{
Changman Lee20d047c2014-11-25 12:44:23 +0900179 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700180 struct nat_entry_set *head;
181
182 head = radix_tree_lookup(&nm_i->nat_set_root, set);
183 if (head) {
184 list_move_tail(&ne->list, &nm_i->nat_entries);
185 set_nat_flag(ne, IS_DIRTY, false);
186 head->entry_cnt--;
187 nm_i->dirty_nat_cnt--;
188 }
189}
190
191static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
192 nid_t start, unsigned int nr, struct nat_entry_set **ep)
193{
194 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
195 start, nr);
196}
197
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700198int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
199{
200 struct f2fs_nm_info *nm_i = NM_I(sbi);
201 struct nat_entry *e;
202 bool need = false;
203
204 down_read(&nm_i->nat_tree_lock);
205 e = __lookup_nat_cache(nm_i, nid);
206 if (e) {
207 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
208 !get_nat_flag(e, HAS_FSYNCED_INODE))
209 need = true;
210 }
211 up_read(&nm_i->nat_tree_lock);
212 return need;
213}
214
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700215bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900216{
217 struct f2fs_nm_info *nm_i = NM_I(sbi);
218 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700219 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900220
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800221 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900222 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700223 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700224 is_cp = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800225 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900226 return is_cp;
227}
228
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700229bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900230{
231 struct f2fs_nm_info *nm_i = NM_I(sbi);
232 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700233 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900234
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800235 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700236 e = __lookup_nat_cache(nm_i, ino);
237 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
238 (get_nat_flag(e, IS_CHECKPOINTED) ||
239 get_nat_flag(e, HAS_FSYNCED_INODE)))
240 need_update = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800241 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700242 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900243}
244
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900245static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
246{
247 struct nat_entry *new;
248
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700249 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800250 f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900251 memset(new, 0, sizeof(struct nat_entry));
252 nat_set_nid(new, nid);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700253 nat_reset_flag(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900254 list_add_tail(&new->list, &nm_i->nat_entries);
255 nm_i->nat_cnt++;
256 return new;
257}
258
259static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
260 struct f2fs_nat_entry *ne)
261{
262 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800263
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800264 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900265 e = __lookup_nat_cache(nm_i, nid);
266 if (!e) {
267 e = grab_nat_entry(nm_i, nid);
Chao Yu94dac222014-04-17 10:51:05 +0800268 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900269 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800270 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900271}
272
273static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900274 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900275{
276 struct f2fs_nm_info *nm_i = NM_I(sbi);
277 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800278
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800279 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900280 e = __lookup_nat_cache(nm_i, ni->nid);
281 if (!e) {
282 e = grab_nat_entry(nm_i, ni->nid);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800283 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700284 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900285 } else if (new_blkaddr == NEW_ADDR) {
286 /*
287 * when nid is reallocated,
288 * previous nat entry can be remained in nat cache.
289 * So, reinitialize it with new information.
290 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800291 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700292 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900293 }
294
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900295 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700296 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
297 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900298 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700299 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900300 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700301 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900302 nat_get_blkaddr(e) != NULL_ADDR &&
303 new_blkaddr == NEW_ADDR);
304
arter97e1c42042014-08-06 23:22:50 +0900305 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900306 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
307 unsigned char version = nat_get_version(e);
308 nat_set_version(e, inc_node_version(version));
Jaegeuk Kim26834462015-08-14 17:57:29 -0700309
310 /* in order to reuse the nid */
311 if (nm_i->next_scan_nid > ni->nid)
312 nm_i->next_scan_nid = ni->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900313 }
314
315 /* change address */
316 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700317 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
318 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900319 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900320
321 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800322 if (ni->nid != ni->ino)
323 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700324 if (e) {
325 if (fsync_done && ni->nid == ni->ino)
326 set_nat_flag(e, HAS_FSYNCED_INODE, true);
327 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
328 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800329 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900330}
331
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900332int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900333{
334 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700335 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900336
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700337 if (!down_write_trylock(&nm_i->nat_tree_lock))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900338 return 0;
339
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900340 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
341 struct nat_entry *ne;
342 ne = list_first_entry(&nm_i->nat_entries,
343 struct nat_entry, list);
344 __del_from_nat_cache(nm_i, ne);
345 nr_shrink--;
346 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800347 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700348 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900349}
350
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900351/*
arter97e1c42042014-08-06 23:22:50 +0900352 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900353 */
354void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
355{
356 struct f2fs_nm_info *nm_i = NM_I(sbi);
357 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
358 struct f2fs_summary_block *sum = curseg->sum_blk;
359 nid_t start_nid = START_NID(nid);
360 struct f2fs_nat_block *nat_blk;
361 struct page *page = NULL;
362 struct f2fs_nat_entry ne;
363 struct nat_entry *e;
364 int i;
365
366 ni->nid = nid;
367
368 /* Check nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800369 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900370 e = __lookup_nat_cache(nm_i, nid);
371 if (e) {
372 ni->ino = nat_get_ino(e);
373 ni->blk_addr = nat_get_blkaddr(e);
374 ni->version = nat_get_version(e);
375 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800376 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900377 if (e)
378 return;
379
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800380 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
381
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900382 /* Check current segment summary */
383 mutex_lock(&curseg->curseg_mutex);
384 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
385 if (i >= 0) {
386 ne = nat_in_journal(sum, i);
387 node_info_from_raw_nat(ni, &ne);
388 }
389 mutex_unlock(&curseg->curseg_mutex);
390 if (i >= 0)
391 goto cache;
392
393 /* Fill node_info from nat page */
394 page = get_current_nat_page(sbi, start_nid);
395 nat_blk = (struct f2fs_nat_block *)page_address(page);
396 ne = nat_blk->entries[nid - start_nid];
397 node_info_from_raw_nat(ni, &ne);
398 f2fs_put_page(page, 1);
399cache:
400 /* cache nat entry */
401 cache_nat_entry(NM_I(sbi), nid, &ne);
402}
403
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900404/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900405 * The maximum depth is four.
406 * Offset[0] will have raw inode offset.
407 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900408static int get_node_path(struct f2fs_inode_info *fi, long block,
409 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900411 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 const long direct_blks = ADDRS_PER_BLOCK;
413 const long dptrs_per_blk = NIDS_PER_BLOCK;
414 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
415 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
416 int n = 0;
417 int level = 0;
418
419 noffset[0] = 0;
420
421 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900422 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900423 goto got;
424 }
425 block -= direct_index;
426 if (block < direct_blks) {
427 offset[n++] = NODE_DIR1_BLOCK;
428 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900429 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900430 level = 1;
431 goto got;
432 }
433 block -= direct_blks;
434 if (block < direct_blks) {
435 offset[n++] = NODE_DIR2_BLOCK;
436 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900437 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900438 level = 1;
439 goto got;
440 }
441 block -= direct_blks;
442 if (block < indirect_blks) {
443 offset[n++] = NODE_IND1_BLOCK;
444 noffset[n] = 3;
445 offset[n++] = block / direct_blks;
446 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900447 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900448 level = 2;
449 goto got;
450 }
451 block -= indirect_blks;
452 if (block < indirect_blks) {
453 offset[n++] = NODE_IND2_BLOCK;
454 noffset[n] = 4 + dptrs_per_blk;
455 offset[n++] = block / direct_blks;
456 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900457 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900458 level = 2;
459 goto got;
460 }
461 block -= indirect_blks;
462 if (block < dindirect_blks) {
463 offset[n++] = NODE_DIND_BLOCK;
464 noffset[n] = 5 + (dptrs_per_blk * 2);
465 offset[n++] = block / indirect_blks;
466 noffset[n] = 6 + (dptrs_per_blk * 2) +
467 offset[n - 1] * (dptrs_per_blk + 1);
468 offset[n++] = (block / direct_blks) % dptrs_per_blk;
469 noffset[n] = 7 + (dptrs_per_blk * 2) +
470 offset[n - 2] * (dptrs_per_blk + 1) +
471 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900472 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900473 level = 3;
474 goto got;
475 } else {
476 BUG();
477 }
478got:
479 return level;
480}
481
482/*
483 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800484 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
485 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900486 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900487 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900488int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900489{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700490 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900491 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800492 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900493 int offset[4];
494 unsigned int noffset[4];
495 nid_t nids[4];
496 int level, i;
497 int err = 0;
498
Jaegeuk Kimde936532013-08-12 21:08:03 +0900499 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900500
501 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900502 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900503
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900504 if (!npage[0]) {
505 npage[0] = get_node_page(sbi, nids[0]);
506 if (IS_ERR(npage[0]))
507 return PTR_ERR(npage[0]);
508 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800509
510 /* if inline_data is set, should not report any block indices */
511 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800512 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800513 f2fs_put_page(npage[0], 1);
514 goto release_out;
515 }
516
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900517 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900518 if (level != 0)
519 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900520 dn->inode_page = npage[0];
521 dn->inode_page_locked = true;
522
523 /* get indirect or direct nodes */
524 for (i = 1; i <= level; i++) {
525 bool done = false;
526
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900527 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900528 /* alloc new node */
529 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900530 err = -ENOSPC;
531 goto release_pages;
532 }
533
534 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900535 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900536 if (IS_ERR(npage[i])) {
537 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538 err = PTR_ERR(npage[i]);
539 goto release_pages;
540 }
541
542 set_nid(parent, offset[i - 1], nids[i], i == 1);
543 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900544 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900545 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900546 npage[i] = get_node_page_ra(parent, offset[i - 1]);
547 if (IS_ERR(npage[i])) {
548 err = PTR_ERR(npage[i]);
549 goto release_pages;
550 }
551 done = true;
552 }
553 if (i == 1) {
554 dn->inode_page_locked = false;
555 unlock_page(parent);
556 } else {
557 f2fs_put_page(parent, 1);
558 }
559
560 if (!done) {
561 npage[i] = get_node_page(sbi, nids[i]);
562 if (IS_ERR(npage[i])) {
563 err = PTR_ERR(npage[i]);
564 f2fs_put_page(npage[0], 0);
565 goto release_out;
566 }
567 }
568 if (i < level) {
569 parent = npage[i];
570 nids[i + 1] = get_nid(parent, offset[i], false);
571 }
572 }
573 dn->nid = nids[level];
574 dn->ofs_in_node = offset[level];
575 dn->node_page = npage[level];
576 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
577 return 0;
578
579release_pages:
580 f2fs_put_page(parent, 1);
581 if (i > 1)
582 f2fs_put_page(npage[0], 0);
583release_out:
584 dn->inode_page = NULL;
585 dn->node_page = NULL;
586 return err;
587}
588
589static void truncate_node(struct dnode_of_data *dn)
590{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700591 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900592 struct node_info ni;
593
594 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900595 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700596 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900597 goto invalidate;
598 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700599 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900600
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900601 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900602 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800603 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900604 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900605
606 if (dn->nid == dn->inode->i_ino) {
607 remove_orphan_inode(sbi, dn->nid);
608 dec_valid_inode_count(sbi);
609 } else {
610 sync_inode_page(dn);
611 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900612invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900613 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800614 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900615
616 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900617
618 invalidate_mapping_pages(NODE_MAPPING(sbi),
619 dn->node_page->index, dn->node_page->index);
620
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900621 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900622 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900623}
624
625static int truncate_dnode(struct dnode_of_data *dn)
626{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900627 struct page *page;
628
629 if (dn->nid == 0)
630 return 1;
631
632 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700633 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900634 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
635 return 1;
636 else if (IS_ERR(page))
637 return PTR_ERR(page);
638
639 /* Make dnode_of_data for parameter */
640 dn->node_page = page;
641 dn->ofs_in_node = 0;
642 truncate_data_blocks(dn);
643 truncate_node(dn);
644 return 1;
645}
646
647static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
648 int ofs, int depth)
649{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900650 struct dnode_of_data rdn = *dn;
651 struct page *page;
652 struct f2fs_node *rn;
653 nid_t child_nid;
654 unsigned int child_nofs;
655 int freed = 0;
656 int i, ret;
657
658 if (dn->nid == 0)
659 return NIDS_PER_BLOCK + 1;
660
Namjae Jeon51dd6242013-04-20 01:28:52 +0900661 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
662
Jaegeuk Kim40813632014-09-02 15:31:18 -0700663 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900664 if (IS_ERR(page)) {
665 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900666 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900667 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900668
Gu Zheng45590712013-07-15 17:57:38 +0800669 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900670 if (depth < 3) {
671 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
672 child_nid = le32_to_cpu(rn->in.nid[i]);
673 if (child_nid == 0)
674 continue;
675 rdn.nid = child_nid;
676 ret = truncate_dnode(&rdn);
677 if (ret < 0)
678 goto out_err;
679 set_nid(page, i, 0, false);
680 }
681 } else {
682 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
683 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
684 child_nid = le32_to_cpu(rn->in.nid[i]);
685 if (child_nid == 0) {
686 child_nofs += NIDS_PER_BLOCK + 1;
687 continue;
688 }
689 rdn.nid = child_nid;
690 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
691 if (ret == (NIDS_PER_BLOCK + 1)) {
692 set_nid(page, i, 0, false);
693 child_nofs += ret;
694 } else if (ret < 0 && ret != -ENOENT) {
695 goto out_err;
696 }
697 }
698 freed = child_nofs;
699 }
700
701 if (!ofs) {
702 /* remove current indirect node */
703 dn->node_page = page;
704 truncate_node(dn);
705 freed++;
706 } else {
707 f2fs_put_page(page, 1);
708 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900709 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900710 return freed;
711
712out_err:
713 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900714 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900715 return ret;
716}
717
718static int truncate_partial_nodes(struct dnode_of_data *dn,
719 struct f2fs_inode *ri, int *offset, int depth)
720{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900721 struct page *pages[2];
722 nid_t nid[3];
723 nid_t child_nid;
724 int err = 0;
725 int i;
726 int idx = depth - 2;
727
728 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
729 if (!nid[0])
730 return 0;
731
732 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800733 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900734 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700735 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900736 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900737 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800738 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900739 goto fail;
740 }
741 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
742 }
743
744 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800745 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900746 child_nid = get_nid(pages[idx], i, false);
747 if (!child_nid)
748 continue;
749 dn->nid = child_nid;
750 err = truncate_dnode(dn);
751 if (err < 0)
752 goto fail;
753 set_nid(pages[idx], i, 0, false);
754 }
755
shifei10.gea225dca2013-10-29 15:32:34 +0800756 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900757 dn->node_page = pages[idx];
758 dn->nid = nid[idx];
759 truncate_node(dn);
760 } else {
761 f2fs_put_page(pages[idx], 1);
762 }
763 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800764 offset[idx + 1] = 0;
765 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900766fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800767 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900768 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900769
770 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
771
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772 return err;
773}
774
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900775/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900776 * All the block addresses of data and nodes should be nullified.
777 */
778int truncate_inode_blocks(struct inode *inode, pgoff_t from)
779{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700780 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900781 int err = 0, cont = 1;
782 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900783 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900784 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900785 struct dnode_of_data dn;
786 struct page *page;
787
Namjae Jeon51dd6242013-04-20 01:28:52 +0900788 trace_f2fs_truncate_inode_blocks_enter(inode, from);
789
Jaegeuk Kimde936532013-08-12 21:08:03 +0900790 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900791restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900792 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900793 if (IS_ERR(page)) {
794 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900795 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900796 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900797
798 set_new_dnode(&dn, inode, page, NULL, 0);
799 unlock_page(page);
800
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900801 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900802 switch (level) {
803 case 0:
804 case 1:
805 nofs = noffset[1];
806 break;
807 case 2:
808 nofs = noffset[1];
809 if (!offset[level - 1])
810 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900811 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900812 if (err < 0 && err != -ENOENT)
813 goto fail;
814 nofs += 1 + NIDS_PER_BLOCK;
815 break;
816 case 3:
817 nofs = 5 + 2 * NIDS_PER_BLOCK;
818 if (!offset[level - 1])
819 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900820 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900821 if (err < 0 && err != -ENOENT)
822 goto fail;
823 break;
824 default:
825 BUG();
826 }
827
828skip_partial:
829 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900830 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831 switch (offset[0]) {
832 case NODE_DIR1_BLOCK:
833 case NODE_DIR2_BLOCK:
834 err = truncate_dnode(&dn);
835 break;
836
837 case NODE_IND1_BLOCK:
838 case NODE_IND2_BLOCK:
839 err = truncate_nodes(&dn, nofs, offset[1], 2);
840 break;
841
842 case NODE_DIND_BLOCK:
843 err = truncate_nodes(&dn, nofs, offset[1], 3);
844 cont = 0;
845 break;
846
847 default:
848 BUG();
849 }
850 if (err < 0 && err != -ENOENT)
851 goto fail;
852 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900853 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900854 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900855 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900856 f2fs_put_page(page, 1);
857 goto restart;
858 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900859 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900860 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900861 set_page_dirty(page);
862 unlock_page(page);
863 }
864 offset[1] = 0;
865 offset[0]++;
866 nofs += err;
867 }
868fail:
869 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900870 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900871 return err > 0 ? 0 : err;
872}
873
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900874int truncate_xattr_node(struct inode *inode, struct page *page)
875{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700876 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900877 nid_t nid = F2FS_I(inode)->i_xattr_nid;
878 struct dnode_of_data dn;
879 struct page *npage;
880
881 if (!nid)
882 return 0;
883
884 npage = get_node_page(sbi, nid);
885 if (IS_ERR(npage))
886 return PTR_ERR(npage);
887
888 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900889
890 /* need to do checkpoint during fsync */
891 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
892
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900893 set_new_dnode(&dn, inode, page, npage, nid);
894
895 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800896 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900897 truncate_node(&dn);
898 return 0;
899}
900
Jaegeuk Kim39936832012-11-22 16:21:29 +0900901/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800902 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
903 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900904 */
Chao Yu13ec7292015-08-24 17:40:45 +0800905int remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900907 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +0800908 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900909
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700910 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu13ec7292015-08-24 17:40:45 +0800911 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
912 if (err)
913 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900914
Chao Yu13ec7292015-08-24 17:40:45 +0800915 err = truncate_xattr_node(inode, dn.inode_page);
916 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700917 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +0800918 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900919 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700920
921 /* remove potential inline_data blocks */
922 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
923 S_ISLNK(inode->i_mode))
924 truncate_data_blocks_range(&dn, 1);
925
arter97e1c42042014-08-06 23:22:50 +0900926 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700927 f2fs_bug_on(F2FS_I_SB(inode),
928 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700929
930 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900931 truncate_node(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +0800932 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900933}
934
Jaegeuk Kima014e032014-06-20 21:44:02 -0700935struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900936{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900937 struct dnode_of_data dn;
938
939 /* allocate inode page for new inode */
940 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900941
942 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900943 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900944}
945
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900946struct page *new_node_page(struct dnode_of_data *dn,
947 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900948{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700949 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900950 struct node_info old_ni, new_ni;
951 struct page *page;
952 int err;
953
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900954 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900955 return ERR_PTR(-EPERM);
956
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900957 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958 if (!page)
959 return ERR_PTR(-ENOMEM);
960
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900961 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900962 err = -ENOSPC;
963 goto fail;
964 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900965
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900966 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900967
968 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700969 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900970 new_ni = old_ni;
971 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900972 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900973
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900974 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900975 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900976 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900977 SetPageUptodate(page);
978 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800980 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900981 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
982
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900983 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900984 if (ipage)
985 update_inode(dn->inode, ipage);
986 else
987 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988 if (ofs == 0)
989 inc_valid_inode_count(sbi);
990
991 return page;
992
993fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900994 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900995 f2fs_put_page(page, 1);
996 return ERR_PTR(err);
997}
998
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900999/*
1000 * Caller should do after getting the following values.
1001 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001002 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001003 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001004static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001005{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001006 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001007 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001008 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001009 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001010 .type = NODE,
1011 .rw = rw,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001012 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001013 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001014 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001015
1016 get_node_info(sbi, page->index, &ni);
1017
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001018 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001019 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001020 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001021 }
1022
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001023 if (PageUptodate(page))
1024 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001025
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001026 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001027 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001028}
1029
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001030/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001031 * Readahead a node page
1032 */
1033void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1034{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001035 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001036 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001037
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001038 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001039 if (apage && PageUptodate(apage)) {
1040 f2fs_put_page(apage, 0);
1041 return;
1042 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001043 f2fs_put_page(apage, 0);
1044
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001045 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001046 if (!apage)
1047 return;
1048
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001049 err = read_node_page(apage, READA);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001050 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001051}
1052
1053struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1054{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001055 struct page *page;
1056 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001057repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001058 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001059 if (!page)
1060 return ERR_PTR(-ENOMEM);
1061
1062 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001063 if (err < 0) {
1064 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001065 return ERR_PTR(err);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001066 } else if (err != LOCKED_PAGE) {
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001067 lock_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001068 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001069
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001070 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001071 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001072 f2fs_put_page(page, 1);
1073 return ERR_PTR(-EIO);
1074 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001075 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001076 f2fs_put_page(page, 1);
1077 goto repeat;
1078 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001079 return page;
1080}
1081
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001082/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001083 * Return a locked page for the desired node page.
1084 * And, readahead MAX_RA_NODE number of node pages.
1085 */
1086struct page *get_node_page_ra(struct page *parent, int start)
1087{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001088 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001089 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001090 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001091 int err, i, end;
1092 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001093
1094 /* First, try getting the desired direct node. */
1095 nid = get_nid(parent, start, false);
1096 if (!nid)
1097 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001098repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001099 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001100 if (!page)
1101 return ERR_PTR(-ENOMEM);
1102
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001103 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001104 if (err < 0) {
1105 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001106 return ERR_PTR(err);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001107 } else if (err == LOCKED_PAGE) {
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001108 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001109 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001110
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001111 blk_start_plug(&plug);
1112
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001113 /* Then, try readahead for siblings of the desired node */
1114 end = start + MAX_RA_NODE;
1115 end = min(end, NIDS_PER_BLOCK);
1116 for (i = start + 1; i < end; i++) {
1117 nid = get_nid(parent, i, false);
1118 if (!nid)
1119 continue;
1120 ra_node_page(sbi, nid);
1121 }
1122
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001123 blk_finish_plug(&plug);
1124
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001125 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001126 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001127 f2fs_put_page(page, 1);
1128 goto repeat;
1129 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001130page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001131 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001132 f2fs_put_page(page, 1);
1133 return ERR_PTR(-EIO);
1134 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001135 return page;
1136}
1137
1138void sync_inode_page(struct dnode_of_data *dn)
1139{
1140 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1141 update_inode(dn->inode, dn->node_page);
1142 } else if (dn->inode_page) {
1143 if (!dn->inode_page_locked)
1144 lock_page(dn->inode_page);
1145 update_inode(dn->inode, dn->inode_page);
1146 if (!dn->inode_page_locked)
1147 unlock_page(dn->inode_page);
1148 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001149 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001150 }
1151}
1152
1153int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1154 struct writeback_control *wbc)
1155{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001156 pgoff_t index, end;
1157 struct pagevec pvec;
1158 int step = ino ? 2 : 0;
1159 int nwritten = 0, wrote = 0;
1160
1161 pagevec_init(&pvec, 0);
1162
1163next_step:
1164 index = 0;
1165 end = LONG_MAX;
1166
1167 while (index <= end) {
1168 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001169 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001170 PAGECACHE_TAG_DIRTY,
1171 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1172 if (nr_pages == 0)
1173 break;
1174
1175 for (i = 0; i < nr_pages; i++) {
1176 struct page *page = pvec.pages[i];
1177
1178 /*
1179 * flushing sequence with step:
1180 * 0. indirect nodes
1181 * 1. dentry dnodes
1182 * 2. file dnodes
1183 */
1184 if (step == 0 && IS_DNODE(page))
1185 continue;
1186 if (step == 1 && (!IS_DNODE(page) ||
1187 is_cold_node(page)))
1188 continue;
1189 if (step == 2 && (!IS_DNODE(page) ||
1190 !is_cold_node(page)))
1191 continue;
1192
1193 /*
1194 * If an fsync mode,
1195 * we should not skip writing node pages.
1196 */
1197 if (ino && ino_of_node(page) == ino)
1198 lock_page(page);
1199 else if (!trylock_page(page))
1200 continue;
1201
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001202 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001203continue_unlock:
1204 unlock_page(page);
1205 continue;
1206 }
1207 if (ino && ino_of_node(page) != ino)
1208 goto continue_unlock;
1209
1210 if (!PageDirty(page)) {
1211 /* someone wrote it for us */
1212 goto continue_unlock;
1213 }
1214
1215 if (!clear_page_dirty_for_io(page))
1216 goto continue_unlock;
1217
1218 /* called by fsync() */
1219 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001220 set_fsync_mark(page, 1);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -07001221 if (IS_INODE(page))
1222 set_dentry_mark(page,
1223 need_dentry_mark(sbi, ino));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001224 nwritten++;
1225 } else {
1226 set_fsync_mark(page, 0);
1227 set_dentry_mark(page, 0);
1228 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001229
1230 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1231 unlock_page(page);
1232 else
1233 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001234
1235 if (--wbc->nr_to_write == 0)
1236 break;
1237 }
1238 pagevec_release(&pvec);
1239 cond_resched();
1240
1241 if (wbc->nr_to_write == 0) {
1242 step = 2;
1243 break;
1244 }
1245 }
1246
1247 if (step < 2) {
1248 step++;
1249 goto next_step;
1250 }
1251
1252 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001253 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001254 return nwritten;
1255}
1256
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001257int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1258{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001259 pgoff_t index = 0, end = LONG_MAX;
1260 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001261 int ret2 = 0, ret = 0;
1262
1263 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001264
1265 while (index <= end) {
1266 int i, nr_pages;
1267 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1268 PAGECACHE_TAG_WRITEBACK,
1269 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1270 if (nr_pages == 0)
1271 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001272
1273 for (i = 0; i < nr_pages; i++) {
1274 struct page *page = pvec.pages[i];
1275
1276 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001277 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001278 continue;
1279
Chao Yu4bf08ff2013-11-04 10:28:33 +08001280 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001281 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001282 if (TestClearPageError(page))
1283 ret = -EIO;
1284 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001285 }
1286 pagevec_release(&pvec);
1287 cond_resched();
1288 }
1289
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001290 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001291 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001292 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001293 ret2 = -EIO;
1294 if (!ret)
1295 ret = ret2;
1296 return ret;
1297}
1298
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001299static int f2fs_write_node_page(struct page *page,
1300 struct writeback_control *wbc)
1301{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001302 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001303 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001304 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001305 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001306 .sbi = sbi,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001307 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001308 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001309 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001310 .encrypted_page = NULL,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001311 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001312
Chao Yuecda0de2014-05-06 16:48:26 +08001313 trace_f2fs_writepage(page, NODE);
1314
Chao Yucaf00472015-01-28 17:48:42 +08001315 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001316 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001317 if (unlikely(f2fs_cp_error(sbi)))
1318 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001319
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001320 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001321
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001322 /* get old block addr of this node page */
1323 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001324 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001325
Jaegeuk Kim25b93342015-09-12 11:13:04 -07001326 if (wbc->for_reclaim) {
1327 if (!down_read_trylock(&sbi->node_write))
1328 goto redirty_out;
1329 } else {
1330 down_read(&sbi->node_write);
1331 }
1332
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333 get_node_info(sbi, nid, &ni);
1334
1335 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001336 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001337 ClearPageUptodate(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001338 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kim25b93342015-09-12 11:13:04 -07001339 up_read(&sbi->node_write);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001340 unlock_page(page);
1341 return 0;
1342 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001343
1344 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001345 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001346 write_node_page(nid, &fio);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001347 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001348 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001349 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001350 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001351
1352 if (wbc->for_reclaim)
1353 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1354
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001355 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001356
1357redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001358 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001359 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001360}
1361
1362static int f2fs_write_node_pages(struct address_space *mapping,
1363 struct writeback_control *wbc)
1364{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001365 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001366 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001367
Chao Yue5748432014-05-06 16:51:24 +08001368 trace_f2fs_writepages(mapping->host, wbc, NODE);
1369
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001370 /* balancing f2fs's metadata in background */
1371 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001372
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001373 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001374 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001375 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001376
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001377 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001378 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001379 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001380 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001382
1383skip_write:
1384 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1385 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386}
1387
1388static int f2fs_set_node_page_dirty(struct page *page)
1389{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001390 trace_f2fs_set_page_dirty(page, NODE);
1391
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392 SetPageUptodate(page);
1393 if (!PageDirty(page)) {
1394 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001395 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001396 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001397 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001398 return 1;
1399 }
1400 return 0;
1401}
1402
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001403/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001404 * Structure of the f2fs node operations
1405 */
1406const struct address_space_operations f2fs_node_aops = {
1407 .writepage = f2fs_write_node_page,
1408 .writepages = f2fs_write_node_pages,
1409 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001410 .invalidatepage = f2fs_invalidate_page,
1411 .releasepage = f2fs_release_page,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001412};
1413
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001414static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1415 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001416{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001417 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001418}
1419
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001420static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1421 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422{
1423 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001424 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425}
1426
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001427static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001428{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001429 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001430 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001431 struct nat_entry *ne;
1432 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001433
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001434 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001435 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001436
1437 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001438 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001439 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001440
Gu Zheng7bd59382013-10-22 14:52:26 +08001441 if (build) {
1442 /* do not add allocated nids */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001443 down_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001444 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001445 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001446 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1447 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001448 allocated = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001449 up_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001450 if (allocated)
1451 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001452 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001453
1454 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001455 i->nid = nid;
1456 i->state = NID_NEW;
1457
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001458 if (radix_tree_preload(GFP_NOFS)) {
1459 kmem_cache_free(free_nid_slab, i);
1460 return 0;
1461 }
1462
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001463 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001464 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001465 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001466 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001467 kmem_cache_free(free_nid_slab, i);
1468 return 0;
1469 }
1470 list_add_tail(&i->list, &nm_i->free_nid_list);
1471 nm_i->fcnt++;
1472 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001473 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001474 return 1;
1475}
1476
1477static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1478{
1479 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001480 bool need_free = false;
1481
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001482 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001483 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001484 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001485 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001486 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001487 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001488 }
1489 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001490
1491 if (need_free)
1492 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001493}
1494
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001495static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001496 struct page *nat_page, nid_t start_nid)
1497{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001498 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001499 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1500 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501 int i;
1502
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001503 i = start_nid % NAT_ENTRY_PER_BLOCK;
1504
1505 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001506
Chao Yucfb271d2013-12-05 17:15:22 +08001507 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001508 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001509
1510 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001511 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001512 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001513 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001514 break;
1515 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001516 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001517}
1518
1519static void build_free_nids(struct f2fs_sb_info *sbi)
1520{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001521 struct f2fs_nm_info *nm_i = NM_I(sbi);
1522 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1523 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001524 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001525 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001526
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001527 /* Enough entries */
1528 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1529 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001530
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001531 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001532 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001533
1534 while (1) {
1535 struct page *page = get_current_nat_page(sbi, nid);
1536
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001537 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001538 f2fs_put_page(page, 1);
1539
1540 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001541 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001542 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001543
Chao Yua6d494b2015-07-24 18:26:26 +08001544 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001545 break;
1546 }
1547
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001548 /* go to the next free nat pages to find free nids abundantly */
1549 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001550
1551 /* find free nids from current sum_pages */
1552 mutex_lock(&curseg->curseg_mutex);
1553 for (i = 0; i < nats_in_cursum(sum); i++) {
1554 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1555 nid = le32_to_cpu(nid_in_journal(sum, i));
1556 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001557 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001558 else
1559 remove_free_nid(nm_i, nid);
1560 }
1561 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001562}
1563
1564/*
1565 * If this function returns success, caller can obtain a new nid
1566 * from second parameter of this function.
1567 * The returned nid could be used ino as well as nid when inode is created.
1568 */
1569bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1570{
1571 struct f2fs_nm_info *nm_i = NM_I(sbi);
1572 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001573retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001574 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001575 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001576
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001578
1579 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001580 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim24928632015-08-16 13:04:50 -07001581 struct node_info ni;
1582
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001583 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001584 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001585 if (i->state == NID_NEW)
1586 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001587
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001588 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001589 *nid = i->nid;
1590 i->state = NID_ALLOC;
1591 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001592 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim24928632015-08-16 13:04:50 -07001593
1594 /* check nid is allocated already */
1595 get_node_info(sbi, *nid, &ni);
1596 if (ni.blk_addr != NULL_ADDR) {
1597 alloc_nid_done(sbi, *nid);
1598 goto retry;
1599 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001600 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001601 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001602 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001603
1604 /* Let's scan nat pages and its caches to get free nids */
1605 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001606 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001607 mutex_unlock(&nm_i->build_lock);
1608 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001609}
1610
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001611/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001612 * alloc_nid() should be called prior to this function.
1613 */
1614void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1615{
1616 struct f2fs_nm_info *nm_i = NM_I(sbi);
1617 struct free_nid *i;
1618
1619 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001620 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001621 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001622 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001623 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001624
1625 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001626}
1627
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001628/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001629 * alloc_nid() should be called prior to this function.
1630 */
1631void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1632{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001633 struct f2fs_nm_info *nm_i = NM_I(sbi);
1634 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001635 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001636
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001637 if (!nid)
1638 return;
1639
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001640 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001641 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001642 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001643 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001644 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001645 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001646 } else {
1647 i->state = NID_NEW;
1648 nm_i->fcnt++;
1649 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001650 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001651
1652 if (need_free)
1653 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001654}
1655
Chao Yu31696582015-07-28 18:33:46 +08001656int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
1657{
1658 struct f2fs_nm_info *nm_i = NM_I(sbi);
1659 struct free_nid *i, *next;
1660 int nr = nr_shrink;
1661
1662 if (!mutex_trylock(&nm_i->build_lock))
1663 return 0;
1664
1665 spin_lock(&nm_i->free_nid_list_lock);
1666 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
1667 if (nr_shrink <= 0 || nm_i->fcnt <= NAT_ENTRY_PER_BLOCK)
1668 break;
1669 if (i->state == NID_ALLOC)
1670 continue;
1671 __del_from_free_nid_list(nm_i, i);
Chao Yu31696582015-07-28 18:33:46 +08001672 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kimf7409d02015-08-21 23:37:18 -07001673 nm_i->fcnt--;
Chao Yu31696582015-07-28 18:33:46 +08001674 nr_shrink--;
Chao Yu31696582015-07-28 18:33:46 +08001675 }
1676 spin_unlock(&nm_i->free_nid_list_lock);
1677 mutex_unlock(&nm_i->build_lock);
1678
1679 return nr - nr_shrink;
1680}
1681
Chao Yu70cfed82014-08-02 15:26:04 +08001682void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001683{
Chao Yu28cdce02014-03-11 13:37:38 +08001684 void *src_addr, *dst_addr;
1685 size_t inline_size;
1686 struct page *ipage;
1687 struct f2fs_inode *ri;
1688
Jaegeuk Kim40813632014-09-02 15:31:18 -07001689 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001690 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001691
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001692 ri = F2FS_INODE(page);
1693 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1694 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1695 goto update_inode;
1696 }
1697
Chao Yu28cdce02014-03-11 13:37:38 +08001698 dst_addr = inline_xattr_addr(ipage);
1699 src_addr = inline_xattr_addr(page);
1700 inline_size = inline_xattr_size(inode);
1701
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001702 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001703 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001704update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001705 update_inode(inode, ipage);
1706 f2fs_put_page(ipage, 1);
1707}
1708
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001709void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001710{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001711 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001712 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1713 nid_t new_xnid = nid_of_node(page);
1714 struct node_info ni;
1715
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001716 /* 1: invalidate the previous xattr nid */
1717 if (!prev_xnid)
1718 goto recover_xnid;
1719
1720 /* Deallocate node address */
1721 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001722 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001723 invalidate_blocks(sbi, ni.blk_addr);
1724 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001725 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001726
1727recover_xnid:
1728 /* 2: allocate new xattr nid */
1729 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001730 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001731
1732 remove_free_nid(NM_I(sbi), new_xnid);
1733 get_node_info(sbi, new_xnid, &ni);
1734 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001735 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001736 F2FS_I(inode)->i_xattr_nid = new_xnid;
1737
1738 /* 3: update xattr blkaddr */
1739 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001740 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001741
1742 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001743}
1744
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001745int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1746{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001747 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001748 nid_t ino = ino_of_node(page);
1749 struct node_info old_ni, new_ni;
1750 struct page *ipage;
1751
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001752 get_node_info(sbi, ino, &old_ni);
1753
1754 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1755 return -EINVAL;
1756
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001757 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001758 if (!ipage)
1759 return -ENOMEM;
1760
arter97e1c42042014-08-06 23:22:50 +09001761 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001762 remove_free_nid(NM_I(sbi), ino);
1763
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001764 SetPageUptodate(ipage);
1765 fill_node_footer(ipage, ino, ino, 0, true);
1766
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001767 src = F2FS_INODE(page);
1768 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001769
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001770 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1771 dst->i_size = 0;
1772 dst->i_blocks = cpu_to_le64(1);
1773 dst->i_links = cpu_to_le32(1);
1774 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001775 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001776
1777 new_ni = old_ni;
1778 new_ni.ino = ino;
1779
Chao Yucfb271d2013-12-05 17:15:22 +08001780 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001781 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001782 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001783 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001784 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001785 f2fs_put_page(ipage, 1);
1786 return 0;
1787}
1788
1789int restore_node_summary(struct f2fs_sb_info *sbi,
1790 unsigned int segno, struct f2fs_summary_block *sum)
1791{
1792 struct f2fs_node *rn;
1793 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001794 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001795 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yu9ecf4b82014-12-18 18:29:05 +08001796 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001797
1798 /* scan the node segment */
1799 last_offset = sbi->blocks_per_seg;
1800 addr = START_BLOCK(sbi, segno);
1801 sum_entry = &sum->entries[0];
1802
Chao Yu9ecf4b82014-12-18 18:29:05 +08001803 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001804 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001805
arter97e1c42042014-08-06 23:22:50 +09001806 /* readahead node pages */
Chao Yu9ecf4b82014-12-18 18:29:05 +08001807 ra_meta_pages(sbi, addr, nrpages, META_POR);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001808
Chao Yu9ecf4b82014-12-18 18:29:05 +08001809 for (idx = addr; idx < addr + nrpages; idx++) {
1810 struct page *page = get_meta_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08001811
Chao Yu9ecf4b82014-12-18 18:29:05 +08001812 rn = F2FS_NODE(page);
1813 sum_entry->nid = rn->footer.nid;
1814 sum_entry->version = 0;
1815 sum_entry->ofs_in_node = 0;
1816 sum_entry++;
1817 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08001818 }
Chao Yubac4eef2014-05-27 08:41:07 +08001819
Chao Yu9ecf4b82014-12-18 18:29:05 +08001820 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08001821 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001822 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08001823 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001824}
1825
Chao Yuaec71382014-06-24 09:18:20 +08001826static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001827{
1828 struct f2fs_nm_info *nm_i = NM_I(sbi);
1829 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1830 struct f2fs_summary_block *sum = curseg->sum_blk;
1831 int i;
1832
1833 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001834 for (i = 0; i < nats_in_cursum(sum); i++) {
1835 struct nat_entry *ne;
1836 struct f2fs_nat_entry raw_ne;
1837 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1838
1839 raw_ne = nat_in_journal(sum, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001840
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001841 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001842 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001843 if (!ne) {
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001844 ne = grab_nat_entry(nm_i, nid);
1845 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001846 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001847 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001848 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001849 }
1850 update_nats_in_cursum(sum, -i);
1851 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001852}
1853
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001854static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1855 struct list_head *head, int max)
1856{
1857 struct nat_entry_set *cur;
1858
1859 if (nes->entry_cnt >= max)
1860 goto add_out;
1861
1862 list_for_each_entry(cur, head, set_list) {
1863 if (cur->entry_cnt >= nes->entry_cnt) {
1864 list_add(&nes->set_list, cur->set_list.prev);
1865 return;
1866 }
1867 }
1868add_out:
1869 list_add_tail(&nes->set_list, head);
1870}
1871
1872static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1873 struct nat_entry_set *set)
1874{
1875 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1876 struct f2fs_summary_block *sum = curseg->sum_blk;
1877 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1878 bool to_journal = true;
1879 struct f2fs_nat_block *nat_blk;
1880 struct nat_entry *ne, *cur;
1881 struct page *page = NULL;
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001882 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001883
1884 /*
1885 * there are two steps to flush nat entries:
1886 * #1, flush nat entries to journal in current hot data summary block.
1887 * #2, flush nat entries to nat page.
1888 */
1889 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1890 to_journal = false;
1891
1892 if (to_journal) {
1893 mutex_lock(&curseg->curseg_mutex);
1894 } else {
1895 page = get_next_nat_page(sbi, start_nid);
1896 nat_blk = page_address(page);
1897 f2fs_bug_on(sbi, !nat_blk);
1898 }
1899
1900 /* flush dirty nats in nat entry set */
1901 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1902 struct f2fs_nat_entry *raw_ne;
1903 nid_t nid = nat_get_nid(ne);
1904 int offset;
1905
1906 if (nat_get_blkaddr(ne) == NEW_ADDR)
1907 continue;
1908
1909 if (to_journal) {
1910 offset = lookup_journal_in_cursum(sum,
1911 NAT_JOURNAL, nid, 1);
1912 f2fs_bug_on(sbi, offset < 0);
1913 raw_ne = &nat_in_journal(sum, offset);
1914 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1915 } else {
1916 raw_ne = &nat_blk->entries[nid - start_nid];
1917 }
1918 raw_nat_from_node_info(raw_ne, &ne->ni);
1919
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001920 down_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001921 nat_reset_flag(ne);
1922 __clear_nat_cache_dirty(NM_I(sbi), ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001923 up_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001924
1925 if (nat_get_blkaddr(ne) == NULL_ADDR)
1926 add_free_nid(sbi, nid, false);
1927 }
1928
1929 if (to_journal)
1930 mutex_unlock(&curseg->curseg_mutex);
1931 else
1932 f2fs_put_page(page, 1);
1933
Changman Lee80ec2e92014-11-25 12:44:24 +09001934 f2fs_bug_on(sbi, set->entry_cnt);
1935
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001936 down_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001937 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001938 up_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001939 kmem_cache_free(nat_entry_set_slab, set);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001940}
1941
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001942/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001943 * This function is called during the checkpointing process.
1944 */
1945void flush_nat_entries(struct f2fs_sb_info *sbi)
1946{
1947 struct f2fs_nm_info *nm_i = NM_I(sbi);
1948 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1949 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001950 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001951 struct nat_entry_set *set, *tmp;
1952 unsigned int found;
1953 nid_t set_idx = 0;
1954 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001955
Changman Lee20d047c2014-11-25 12:44:23 +09001956 if (!nm_i->dirty_nat_cnt)
1957 return;
Chao Yuaec71382014-06-24 09:18:20 +08001958 /*
1959 * if there are no enough space in journal to store dirty nat
1960 * entries, remove all entries from journal and merge them
1961 * into nat entry set.
1962 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001963 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001964 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001965
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001966 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001967 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001968 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001969 unsigned idx;
1970 set_idx = setvec[found - 1]->set + 1;
1971 for (idx = 0; idx < found; idx++)
1972 __adjust_nat_entry_set(setvec[idx], &sets,
1973 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001974 }
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001975 up_write(&nm_i->nat_tree_lock);
Chao Yuaec71382014-06-24 09:18:20 +08001976
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001977 /* flush dirty nats in nat entry set */
1978 list_for_each_entry_safe(set, tmp, &sets, set_list)
1979 __flush_nat_entry_set(sbi, set);
1980
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001981 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001982}
1983
1984static int init_node_manager(struct f2fs_sb_info *sbi)
1985{
1986 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1987 struct f2fs_nm_info *nm_i = NM_I(sbi);
1988 unsigned char *version_bitmap;
1989 unsigned int nat_segs, nat_blocks;
1990
1991 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1992
1993 /* segment_count_nat includes pair segment so divide to 2. */
1994 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1995 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001996
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001997 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1998
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001999 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08002000 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002001 nm_i->fcnt = 0;
2002 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09002003 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002004
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002005 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002006 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002007 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2008 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002009 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002010
2011 mutex_init(&nm_i->build_lock);
2012 spin_lock_init(&nm_i->free_nid_list_lock);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002013 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002014
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002015 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002016 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002017 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2018 if (!version_bitmap)
2019 return -EFAULT;
2020
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002021 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2022 GFP_KERNEL);
2023 if (!nm_i->nat_bitmap)
2024 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002025 return 0;
2026}
2027
2028int build_node_manager(struct f2fs_sb_info *sbi)
2029{
2030 int err;
2031
2032 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2033 if (!sbi->nm_info)
2034 return -ENOMEM;
2035
2036 err = init_node_manager(sbi);
2037 if (err)
2038 return err;
2039
2040 build_free_nids(sbi);
2041 return 0;
2042}
2043
2044void destroy_node_manager(struct f2fs_sb_info *sbi)
2045{
2046 struct f2fs_nm_info *nm_i = NM_I(sbi);
2047 struct free_nid *i, *next_i;
2048 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002049 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002050 nid_t nid = 0;
2051 unsigned int found;
2052
2053 if (!nm_i)
2054 return;
2055
2056 /* destroy free nid list */
2057 spin_lock(&nm_i->free_nid_list_lock);
2058 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002059 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002060 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002061 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002062 spin_unlock(&nm_i->free_nid_list_lock);
2063 kmem_cache_free(free_nid_slab, i);
2064 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002065 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002066 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002067 spin_unlock(&nm_i->free_nid_list_lock);
2068
2069 /* destroy nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002070 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002071 while ((found = __gang_lookup_nat_cache(nm_i,
2072 nid, NATVEC_SIZE, natvec))) {
2073 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002074
Gu Zhengb6ce3912014-03-07 18:43:24 +08002075 nid = nat_get_nid(natvec[found - 1]) + 1;
2076 for (idx = 0; idx < found; idx++)
2077 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002078 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002079 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002080
2081 /* destroy nat set cache */
2082 nid = 0;
2083 while ((found = __gang_lookup_nat_set(nm_i,
2084 nid, SETVEC_SIZE, setvec))) {
2085 unsigned idx;
2086
2087 nid = setvec[found - 1]->set + 1;
2088 for (idx = 0; idx < found; idx++) {
2089 /* entry_cnt is not zero, when cp_error was occurred */
2090 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2091 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2092 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2093 }
2094 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002095 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002096
2097 kfree(nm_i->nat_bitmap);
2098 sbi->nm_info = NULL;
2099 kfree(nm_i);
2100}
2101
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002102int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002103{
2104 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002105 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002106 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002107 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002108
2109 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002110 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002111 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002112 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002113
2114 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2115 sizeof(struct nat_entry_set));
2116 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002117 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002118 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002119
Markus Elfringce3e6d22014-11-24 15:52:00 +01002120destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002121 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002122destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002123 kmem_cache_destroy(nat_entry_slab);
2124fail:
2125 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002126}
2127
2128void destroy_node_manager_caches(void)
2129{
Chao Yuaec71382014-06-24 09:18:20 +08002130 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002131 kmem_cache_destroy(free_nid_slab);
2132 kmem_cache_destroy(nat_entry_slab);
2133}