blob: 36656add1c9b11bc2ea29461040dd6c5ebcebf45 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/mpage.h>
14#include <linux/backing-dev.h>
15#include <linux/blkdev.h>
16#include <linux/pagevec.h>
17#include <linux/swap.h>
18
19#include "f2fs.h"
20#include "node.h"
21#include "segment.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090022#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090023
Gu Zhengf978f5a2014-02-21 18:08:29 +080024#define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
25
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090026static struct kmem_cache *nat_entry_slab;
27static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080028static struct kmem_cache *nat_entry_set_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090029
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090030bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090031{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090032 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090033 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080034 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090035 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090036 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090037
38 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080039
40 /* only uses low memory */
41 avail_ram = val.totalram - val.totalhigh;
42
43 /* give 25%, 25%, 50%, 50% memory for each components respectively */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090044 if (type == FREE_NIDS) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080045 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
46 PAGE_CACHE_SHIFT;
47 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090048 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080049 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
50 PAGE_CACHE_SHIFT;
51 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090052 } else if (type == DIRTY_DENTS) {
Jaegeuk Kim2743f862014-06-28 01:00:41 +090053 if (sbi->sb->s_bdi->dirty_exceeded)
54 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090055 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080056 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
57 } else if (type == INO_ENTRIES) {
58 int i;
59
60 if (sbi->sb->s_bdi->dirty_exceeded)
61 return false;
62 for (i = 0; i <= UPDATE_INO; i++)
Chao Yu67298802014-11-18 11:18:36 +080063 mem_size += (sbi->im[i].ino_num *
64 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080065 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090066 }
67 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090068}
69
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090070static void clear_node_page_dirty(struct page *page)
71{
72 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090073 unsigned int long flags;
74
75 if (PageDirty(page)) {
76 spin_lock_irqsave(&mapping->tree_lock, flags);
77 radix_tree_tag_clear(&mapping->page_tree,
78 page_index(page),
79 PAGECACHE_TAG_DIRTY);
80 spin_unlock_irqrestore(&mapping->tree_lock, flags);
81
82 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070083 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090084 }
85 ClearPageUptodate(page);
86}
87
88static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
89{
90 pgoff_t index = current_nat_addr(sbi, nid);
91 return get_meta_page(sbi, index);
92}
93
94static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
95{
96 struct page *src_page;
97 struct page *dst_page;
98 pgoff_t src_off;
99 pgoff_t dst_off;
100 void *src_addr;
101 void *dst_addr;
102 struct f2fs_nm_info *nm_i = NM_I(sbi);
103
104 src_off = current_nat_addr(sbi, nid);
105 dst_off = next_nat_addr(sbi, src_off);
106
107 /* get current nat block page with lock */
108 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900109 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700110 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900111
112 src_addr = page_address(src_page);
113 dst_addr = page_address(dst_page);
114 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
115 set_page_dirty(dst_page);
116 f2fs_put_page(src_page, 1);
117
118 set_to_next_nat(nm_i, nid);
119
120 return dst_page;
121}
122
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900123static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
124{
125 return radix_tree_lookup(&nm_i->nat_root, n);
126}
127
128static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
129 nid_t start, unsigned int nr, struct nat_entry **ep)
130{
131 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
132}
133
134static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
135{
136 list_del(&e->list);
137 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
138 nm_i->nat_cnt--;
139 kmem_cache_free(nat_entry_slab, e);
140}
141
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700142static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
143 struct nat_entry *ne)
144{
145 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
146 struct nat_entry_set *head;
147
148 if (get_nat_flag(ne, IS_DIRTY))
149 return;
150retry:
151 head = radix_tree_lookup(&nm_i->nat_set_root, set);
152 if (!head) {
153 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
154
155 INIT_LIST_HEAD(&head->entry_list);
156 INIT_LIST_HEAD(&head->set_list);
157 head->set = set;
158 head->entry_cnt = 0;
159
160 if (radix_tree_insert(&nm_i->nat_set_root, set, head)) {
161 cond_resched();
162 goto retry;
163 }
164 }
165 list_move_tail(&ne->list, &head->entry_list);
166 nm_i->dirty_nat_cnt++;
167 head->entry_cnt++;
168 set_nat_flag(ne, IS_DIRTY, true);
169}
170
171static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
172 struct nat_entry *ne)
173{
174 nid_t set = ne->ni.nid / NAT_ENTRY_PER_BLOCK;
175 struct nat_entry_set *head;
176
177 head = radix_tree_lookup(&nm_i->nat_set_root, set);
178 if (head) {
179 list_move_tail(&ne->list, &nm_i->nat_entries);
180 set_nat_flag(ne, IS_DIRTY, false);
181 head->entry_cnt--;
182 nm_i->dirty_nat_cnt--;
183 }
184}
185
186static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
187 nid_t start, unsigned int nr, struct nat_entry_set **ep)
188{
189 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
190 start, nr);
191}
192
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700193bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900194{
195 struct f2fs_nm_info *nm_i = NM_I(sbi);
196 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700197 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900198
199 read_lock(&nm_i->nat_tree_lock);
200 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700201 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700202 is_cp = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900203 read_unlock(&nm_i->nat_tree_lock);
204 return is_cp;
205}
206
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700207bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900208{
209 struct f2fs_nm_info *nm_i = NM_I(sbi);
210 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700211 bool fsynced = false;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900212
213 read_lock(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700214 e = __lookup_nat_cache(nm_i, ino);
215 if (e && get_nat_flag(e, HAS_FSYNCED_INODE))
216 fsynced = true;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900217 read_unlock(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700218 return fsynced;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900219}
220
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700221bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900222{
223 struct f2fs_nm_info *nm_i = NM_I(sbi);
224 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700225 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900226
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700227 read_lock(&nm_i->nat_tree_lock);
228 e = __lookup_nat_cache(nm_i, ino);
229 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
230 (get_nat_flag(e, IS_CHECKPOINTED) ||
231 get_nat_flag(e, HAS_FSYNCED_INODE)))
232 need_update = false;
233 read_unlock(&nm_i->nat_tree_lock);
234 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900235}
236
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900237static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
238{
239 struct nat_entry *new;
240
241 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
242 if (!new)
243 return NULL;
244 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
245 kmem_cache_free(nat_entry_slab, new);
246 return NULL;
247 }
248 memset(new, 0, sizeof(struct nat_entry));
249 nat_set_nid(new, nid);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700250 nat_reset_flag(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900251 list_add_tail(&new->list, &nm_i->nat_entries);
252 nm_i->nat_cnt++;
253 return new;
254}
255
256static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
257 struct f2fs_nat_entry *ne)
258{
259 struct nat_entry *e;
260retry:
261 write_lock(&nm_i->nat_tree_lock);
262 e = __lookup_nat_cache(nm_i, nid);
263 if (!e) {
264 e = grab_nat_entry(nm_i, nid);
265 if (!e) {
266 write_unlock(&nm_i->nat_tree_lock);
267 goto retry;
268 }
Chao Yu94dac222014-04-17 10:51:05 +0800269 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900270 }
271 write_unlock(&nm_i->nat_tree_lock);
272}
273
274static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900275 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900276{
277 struct f2fs_nm_info *nm_i = NM_I(sbi);
278 struct nat_entry *e;
279retry:
280 write_lock(&nm_i->nat_tree_lock);
281 e = __lookup_nat_cache(nm_i, ni->nid);
282 if (!e) {
283 e = grab_nat_entry(nm_i, ni->nid);
284 if (!e) {
285 write_unlock(&nm_i->nat_tree_lock);
286 goto retry;
287 }
288 e->ni = *ni;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700289 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900290 } else if (new_blkaddr == NEW_ADDR) {
291 /*
292 * when nid is reallocated,
293 * previous nat entry can be remained in nat cache.
294 * So, reinitialize it with new information.
295 */
296 e->ni = *ni;
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700297 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900298 }
299
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900300 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700301 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
302 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900303 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700304 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900305 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700306 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900307 nat_get_blkaddr(e) != NULL_ADDR &&
308 new_blkaddr == NEW_ADDR);
309
arter97e1c42042014-08-06 23:22:50 +0900310 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900311 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
312 unsigned char version = nat_get_version(e);
313 nat_set_version(e, inc_node_version(version));
314 }
315
316 /* change address */
317 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700318 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
319 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900320 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900321
322 /* update fsync_mark if its inode nat entry is still alive */
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 Kime05df3b2012-11-02 17:08:50 +0900329 write_unlock(&nm_i->nat_tree_lock);
330}
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);
335
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900336 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900337 return 0;
338
339 write_lock(&nm_i->nat_tree_lock);
340 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 }
347 write_unlock(&nm_i->nat_tree_lock);
348 return nr_shrink;
349}
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
Namjae Jeonbe4124f2012-12-01 10:55:12 +0900366 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900367 ni->nid = nid;
368
369 /* Check nat cache */
370 read_lock(&nm_i->nat_tree_lock);
371 e = __lookup_nat_cache(nm_i, nid);
372 if (e) {
373 ni->ino = nat_get_ino(e);
374 ni->blk_addr = nat_get_blkaddr(e);
375 ni->version = nat_get_version(e);
376 }
377 read_unlock(&nm_i->nat_tree_lock);
378 if (e)
379 return;
380
381 /* Check current segment summary */
382 mutex_lock(&curseg->curseg_mutex);
383 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
384 if (i >= 0) {
385 ne = nat_in_journal(sum, i);
386 node_info_from_raw_nat(ni, &ne);
387 }
388 mutex_unlock(&curseg->curseg_mutex);
389 if (i >= 0)
390 goto cache;
391
392 /* Fill node_info from nat page */
393 page = get_current_nat_page(sbi, start_nid);
394 nat_blk = (struct f2fs_nat_block *)page_address(page);
395 ne = nat_blk->entries[nid - start_nid];
396 node_info_from_raw_nat(ni, &ne);
397 f2fs_put_page(page, 1);
398cache:
399 /* cache nat entry */
400 cache_nat_entry(NM_I(sbi), nid, &ne);
401}
402
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900403/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900404 * The maximum depth is four.
405 * Offset[0] will have raw inode offset.
406 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900407static int get_node_path(struct f2fs_inode_info *fi, long block,
408 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900409{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900410 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900411 const long direct_blks = ADDRS_PER_BLOCK;
412 const long dptrs_per_blk = NIDS_PER_BLOCK;
413 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
414 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
415 int n = 0;
416 int level = 0;
417
418 noffset[0] = 0;
419
420 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900421 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900422 goto got;
423 }
424 block -= direct_index;
425 if (block < direct_blks) {
426 offset[n++] = NODE_DIR1_BLOCK;
427 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900428 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900429 level = 1;
430 goto got;
431 }
432 block -= direct_blks;
433 if (block < direct_blks) {
434 offset[n++] = NODE_DIR2_BLOCK;
435 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900436 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900437 level = 1;
438 goto got;
439 }
440 block -= direct_blks;
441 if (block < indirect_blks) {
442 offset[n++] = NODE_IND1_BLOCK;
443 noffset[n] = 3;
444 offset[n++] = block / direct_blks;
445 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900446 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900447 level = 2;
448 goto got;
449 }
450 block -= indirect_blks;
451 if (block < indirect_blks) {
452 offset[n++] = NODE_IND2_BLOCK;
453 noffset[n] = 4 + dptrs_per_blk;
454 offset[n++] = block / direct_blks;
455 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900456 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900457 level = 2;
458 goto got;
459 }
460 block -= indirect_blks;
461 if (block < dindirect_blks) {
462 offset[n++] = NODE_DIND_BLOCK;
463 noffset[n] = 5 + (dptrs_per_blk * 2);
464 offset[n++] = block / indirect_blks;
465 noffset[n] = 6 + (dptrs_per_blk * 2) +
466 offset[n - 1] * (dptrs_per_blk + 1);
467 offset[n++] = (block / direct_blks) % dptrs_per_blk;
468 noffset[n] = 7 + (dptrs_per_blk * 2) +
469 offset[n - 2] * (dptrs_per_blk + 1) +
470 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900471 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900472 level = 3;
473 goto got;
474 } else {
475 BUG();
476 }
477got:
478 return level;
479}
480
481/*
482 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800483 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
484 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900485 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900486 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900487int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900488{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700489 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900490 struct page *npage[4];
491 struct page *parent;
492 int offset[4];
493 unsigned int noffset[4];
494 nid_t nids[4];
495 int level, i;
496 int err = 0;
497
Jaegeuk Kimde936532013-08-12 21:08:03 +0900498 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900499
500 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900501 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900502
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900503 if (!npage[0]) {
504 npage[0] = get_node_page(sbi, nids[0]);
505 if (IS_ERR(npage[0]))
506 return PTR_ERR(npage[0]);
507 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900508 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900509 if (level != 0)
510 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900511 dn->inode_page = npage[0];
512 dn->inode_page_locked = true;
513
514 /* get indirect or direct nodes */
515 for (i = 1; i <= level; i++) {
516 bool done = false;
517
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900518 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900519 /* alloc new node */
520 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521 err = -ENOSPC;
522 goto release_pages;
523 }
524
525 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900526 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900527 if (IS_ERR(npage[i])) {
528 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900529 err = PTR_ERR(npage[i]);
530 goto release_pages;
531 }
532
533 set_nid(parent, offset[i - 1], nids[i], i == 1);
534 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900535 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900536 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900537 npage[i] = get_node_page_ra(parent, offset[i - 1]);
538 if (IS_ERR(npage[i])) {
539 err = PTR_ERR(npage[i]);
540 goto release_pages;
541 }
542 done = true;
543 }
544 if (i == 1) {
545 dn->inode_page_locked = false;
546 unlock_page(parent);
547 } else {
548 f2fs_put_page(parent, 1);
549 }
550
551 if (!done) {
552 npage[i] = get_node_page(sbi, nids[i]);
553 if (IS_ERR(npage[i])) {
554 err = PTR_ERR(npage[i]);
555 f2fs_put_page(npage[0], 0);
556 goto release_out;
557 }
558 }
559 if (i < level) {
560 parent = npage[i];
561 nids[i + 1] = get_nid(parent, offset[i], false);
562 }
563 }
564 dn->nid = nids[level];
565 dn->ofs_in_node = offset[level];
566 dn->node_page = npage[level];
567 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
568 return 0;
569
570release_pages:
571 f2fs_put_page(parent, 1);
572 if (i > 1)
573 f2fs_put_page(npage[0], 0);
574release_out:
575 dn->inode_page = NULL;
576 dn->node_page = NULL;
577 return err;
578}
579
580static void truncate_node(struct dnode_of_data *dn)
581{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700582 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900583 struct node_info ni;
584
585 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900586 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700587 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900588 goto invalidate;
589 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700590 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900591
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900592 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900593 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800594 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900595 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900596
597 if (dn->nid == dn->inode->i_ino) {
598 remove_orphan_inode(sbi, dn->nid);
599 dec_valid_inode_count(sbi);
600 } else {
601 sync_inode_page(dn);
602 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900603invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900604 clear_node_page_dirty(dn->node_page);
605 F2FS_SET_SB_DIRT(sbi);
606
607 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900608
609 invalidate_mapping_pages(NODE_MAPPING(sbi),
610 dn->node_page->index, dn->node_page->index);
611
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900612 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900613 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900614}
615
616static int truncate_dnode(struct dnode_of_data *dn)
617{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900618 struct page *page;
619
620 if (dn->nid == 0)
621 return 1;
622
623 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700624 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900625 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
626 return 1;
627 else if (IS_ERR(page))
628 return PTR_ERR(page);
629
630 /* Make dnode_of_data for parameter */
631 dn->node_page = page;
632 dn->ofs_in_node = 0;
633 truncate_data_blocks(dn);
634 truncate_node(dn);
635 return 1;
636}
637
638static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
639 int ofs, int depth)
640{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900641 struct dnode_of_data rdn = *dn;
642 struct page *page;
643 struct f2fs_node *rn;
644 nid_t child_nid;
645 unsigned int child_nofs;
646 int freed = 0;
647 int i, ret;
648
649 if (dn->nid == 0)
650 return NIDS_PER_BLOCK + 1;
651
Namjae Jeon51dd6242013-04-20 01:28:52 +0900652 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
653
Jaegeuk Kim40813632014-09-02 15:31:18 -0700654 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900655 if (IS_ERR(page)) {
656 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900657 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900658 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900659
Gu Zheng45590712013-07-15 17:57:38 +0800660 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900661 if (depth < 3) {
662 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
663 child_nid = le32_to_cpu(rn->in.nid[i]);
664 if (child_nid == 0)
665 continue;
666 rdn.nid = child_nid;
667 ret = truncate_dnode(&rdn);
668 if (ret < 0)
669 goto out_err;
670 set_nid(page, i, 0, false);
671 }
672 } else {
673 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
674 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
675 child_nid = le32_to_cpu(rn->in.nid[i]);
676 if (child_nid == 0) {
677 child_nofs += NIDS_PER_BLOCK + 1;
678 continue;
679 }
680 rdn.nid = child_nid;
681 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
682 if (ret == (NIDS_PER_BLOCK + 1)) {
683 set_nid(page, i, 0, false);
684 child_nofs += ret;
685 } else if (ret < 0 && ret != -ENOENT) {
686 goto out_err;
687 }
688 }
689 freed = child_nofs;
690 }
691
692 if (!ofs) {
693 /* remove current indirect node */
694 dn->node_page = page;
695 truncate_node(dn);
696 freed++;
697 } else {
698 f2fs_put_page(page, 1);
699 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900700 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900701 return freed;
702
703out_err:
704 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900705 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900706 return ret;
707}
708
709static int truncate_partial_nodes(struct dnode_of_data *dn,
710 struct f2fs_inode *ri, int *offset, int depth)
711{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900712 struct page *pages[2];
713 nid_t nid[3];
714 nid_t child_nid;
715 int err = 0;
716 int i;
717 int idx = depth - 2;
718
719 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
720 if (!nid[0])
721 return 0;
722
723 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800724 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900725 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700726 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900727 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900728 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800729 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900730 goto fail;
731 }
732 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
733 }
734
735 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800736 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900737 child_nid = get_nid(pages[idx], i, false);
738 if (!child_nid)
739 continue;
740 dn->nid = child_nid;
741 err = truncate_dnode(dn);
742 if (err < 0)
743 goto fail;
744 set_nid(pages[idx], i, 0, false);
745 }
746
shifei10.gea225dca2013-10-29 15:32:34 +0800747 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900748 dn->node_page = pages[idx];
749 dn->nid = nid[idx];
750 truncate_node(dn);
751 } else {
752 f2fs_put_page(pages[idx], 1);
753 }
754 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800755 offset[idx + 1] = 0;
756 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900757fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800758 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900759 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900760
761 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
762
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900763 return err;
764}
765
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900766/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900767 * All the block addresses of data and nodes should be nullified.
768 */
769int truncate_inode_blocks(struct inode *inode, pgoff_t from)
770{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700771 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772 int err = 0, cont = 1;
773 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900774 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900775 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900776 struct dnode_of_data dn;
777 struct page *page;
778
Namjae Jeon51dd6242013-04-20 01:28:52 +0900779 trace_f2fs_truncate_inode_blocks_enter(inode, from);
780
Jaegeuk Kimde936532013-08-12 21:08:03 +0900781 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900782restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900783 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900784 if (IS_ERR(page)) {
785 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900786 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900787 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900788
789 set_new_dnode(&dn, inode, page, NULL, 0);
790 unlock_page(page);
791
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900792 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900793 switch (level) {
794 case 0:
795 case 1:
796 nofs = noffset[1];
797 break;
798 case 2:
799 nofs = noffset[1];
800 if (!offset[level - 1])
801 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900802 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900803 if (err < 0 && err != -ENOENT)
804 goto fail;
805 nofs += 1 + NIDS_PER_BLOCK;
806 break;
807 case 3:
808 nofs = 5 + 2 * NIDS_PER_BLOCK;
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 break;
815 default:
816 BUG();
817 }
818
819skip_partial:
820 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900821 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900822 switch (offset[0]) {
823 case NODE_DIR1_BLOCK:
824 case NODE_DIR2_BLOCK:
825 err = truncate_dnode(&dn);
826 break;
827
828 case NODE_IND1_BLOCK:
829 case NODE_IND2_BLOCK:
830 err = truncate_nodes(&dn, nofs, offset[1], 2);
831 break;
832
833 case NODE_DIND_BLOCK:
834 err = truncate_nodes(&dn, nofs, offset[1], 3);
835 cont = 0;
836 break;
837
838 default:
839 BUG();
840 }
841 if (err < 0 && err != -ENOENT)
842 goto fail;
843 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900844 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900845 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900846 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900847 f2fs_put_page(page, 1);
848 goto restart;
849 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900850 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900851 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900852 set_page_dirty(page);
853 unlock_page(page);
854 }
855 offset[1] = 0;
856 offset[0]++;
857 nofs += err;
858 }
859fail:
860 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900861 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900862 return err > 0 ? 0 : err;
863}
864
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900865int truncate_xattr_node(struct inode *inode, struct page *page)
866{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700867 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900868 nid_t nid = F2FS_I(inode)->i_xattr_nid;
869 struct dnode_of_data dn;
870 struct page *npage;
871
872 if (!nid)
873 return 0;
874
875 npage = get_node_page(sbi, nid);
876 if (IS_ERR(npage))
877 return PTR_ERR(npage);
878
879 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900880
881 /* need to do checkpoint during fsync */
882 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
883
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900884 set_new_dnode(&dn, inode, page, npage, nid);
885
886 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800887 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900888 truncate_node(&dn);
889 return 0;
890}
891
Jaegeuk Kim39936832012-11-22 16:21:29 +0900892/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800893 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
894 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900895 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800896void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900897{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900898 struct dnode_of_data dn;
899
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700900 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
901 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800902 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900903
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700904 if (truncate_xattr_node(inode, dn.inode_page)) {
905 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800906 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900907 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700908
909 /* remove potential inline_data blocks */
910 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
911 S_ISLNK(inode->i_mode))
912 truncate_data_blocks_range(&dn, 1);
913
arter97e1c42042014-08-06 23:22:50 +0900914 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700915 f2fs_bug_on(F2FS_I_SB(inode),
916 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700917
918 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900919 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900920}
921
Jaegeuk Kima014e032014-06-20 21:44:02 -0700922struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900924 struct dnode_of_data dn;
925
926 /* allocate inode page for new inode */
927 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900928
929 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900930 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900931}
932
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900933struct page *new_node_page(struct dnode_of_data *dn,
934 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900935{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700936 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900937 struct node_info old_ni, new_ni;
938 struct page *page;
939 int err;
940
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900941 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900942 return ERR_PTR(-EPERM);
943
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900944 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900945 if (!page)
946 return ERR_PTR(-ENOMEM);
947
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900948 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900949 err = -ENOSPC;
950 goto fail;
951 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900952
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900953 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900954
955 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700956 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900957 new_ni = old_ni;
958 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900959 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900960
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900961 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900962 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900963 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900964 SetPageUptodate(page);
965 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900966
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800967 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900968 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
969
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900970 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900971 if (ipage)
972 update_inode(dn->inode, ipage);
973 else
974 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900975 if (ofs == 0)
976 inc_valid_inode_count(sbi);
977
978 return page;
979
980fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900981 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900982 f2fs_put_page(page, 1);
983 return ERR_PTR(err);
984}
985
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900986/*
987 * Caller should do after getting the following values.
988 * 0: f2fs_put_page(page, 0)
989 * LOCKED_PAGE: f2fs_put_page(page, 1)
990 * error: nothing
991 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900992static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900993{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700994 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900995 struct node_info ni;
996
997 get_node_info(sbi, page->index, &ni);
998
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900999 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001000 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001001 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001002 }
1003
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001004 if (PageUptodate(page))
1005 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001006
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001007 return f2fs_submit_page_bio(sbi, page, ni.blk_addr, rw);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001008}
1009
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001010/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001011 * Readahead a node page
1012 */
1013void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1014{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001015 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001016 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001018 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001019 if (apage && PageUptodate(apage)) {
1020 f2fs_put_page(apage, 0);
1021 return;
1022 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001023 f2fs_put_page(apage, 0);
1024
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001025 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001026 if (!apage)
1027 return;
1028
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001029 err = read_node_page(apage, READA);
1030 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001031 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001032 else if (err == LOCKED_PAGE)
1033 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001034}
1035
1036struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1037{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001038 struct page *page;
1039 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001040repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001041 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001042 if (!page)
1043 return ERR_PTR(-ENOMEM);
1044
1045 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001046 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001047 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001048 else if (err == LOCKED_PAGE)
1049 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001050
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001051 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001052 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001053 f2fs_put_page(page, 1);
1054 return ERR_PTR(-EIO);
1055 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001056 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001057 f2fs_put_page(page, 1);
1058 goto repeat;
1059 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001060got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001061 return page;
1062}
1063
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001064/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001065 * Return a locked page for the desired node page.
1066 * And, readahead MAX_RA_NODE number of node pages.
1067 */
1068struct page *get_node_page_ra(struct page *parent, int start)
1069{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001070 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001071 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001072 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001073 int err, i, end;
1074 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001075
1076 /* First, try getting the desired direct node. */
1077 nid = get_nid(parent, start, false);
1078 if (!nid)
1079 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001080repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001081 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001082 if (!page)
1083 return ERR_PTR(-ENOMEM);
1084
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001085 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001086 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001087 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001088 else if (err == LOCKED_PAGE)
1089 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001090
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001091 blk_start_plug(&plug);
1092
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001093 /* Then, try readahead for siblings of the desired node */
1094 end = start + MAX_RA_NODE;
1095 end = min(end, NIDS_PER_BLOCK);
1096 for (i = start + 1; i < end; i++) {
1097 nid = get_nid(parent, i, false);
1098 if (!nid)
1099 continue;
1100 ra_node_page(sbi, nid);
1101 }
1102
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001103 blk_finish_plug(&plug);
1104
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001105 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001106 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001107 f2fs_put_page(page, 1);
1108 goto repeat;
1109 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001110page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001111 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001112 f2fs_put_page(page, 1);
1113 return ERR_PTR(-EIO);
1114 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001115 return page;
1116}
1117
1118void sync_inode_page(struct dnode_of_data *dn)
1119{
1120 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1121 update_inode(dn->inode, dn->node_page);
1122 } else if (dn->inode_page) {
1123 if (!dn->inode_page_locked)
1124 lock_page(dn->inode_page);
1125 update_inode(dn->inode, dn->inode_page);
1126 if (!dn->inode_page_locked)
1127 unlock_page(dn->inode_page);
1128 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001129 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001130 }
1131}
1132
1133int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1134 struct writeback_control *wbc)
1135{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001136 pgoff_t index, end;
1137 struct pagevec pvec;
1138 int step = ino ? 2 : 0;
1139 int nwritten = 0, wrote = 0;
1140
1141 pagevec_init(&pvec, 0);
1142
1143next_step:
1144 index = 0;
1145 end = LONG_MAX;
1146
1147 while (index <= end) {
1148 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001149 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001150 PAGECACHE_TAG_DIRTY,
1151 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1152 if (nr_pages == 0)
1153 break;
1154
1155 for (i = 0; i < nr_pages; i++) {
1156 struct page *page = pvec.pages[i];
1157
1158 /*
1159 * flushing sequence with step:
1160 * 0. indirect nodes
1161 * 1. dentry dnodes
1162 * 2. file dnodes
1163 */
1164 if (step == 0 && IS_DNODE(page))
1165 continue;
1166 if (step == 1 && (!IS_DNODE(page) ||
1167 is_cold_node(page)))
1168 continue;
1169 if (step == 2 && (!IS_DNODE(page) ||
1170 !is_cold_node(page)))
1171 continue;
1172
1173 /*
1174 * If an fsync mode,
1175 * we should not skip writing node pages.
1176 */
1177 if (ino && ino_of_node(page) == ino)
1178 lock_page(page);
1179 else if (!trylock_page(page))
1180 continue;
1181
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001182 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001183continue_unlock:
1184 unlock_page(page);
1185 continue;
1186 }
1187 if (ino && ino_of_node(page) != ino)
1188 goto continue_unlock;
1189
1190 if (!PageDirty(page)) {
1191 /* someone wrote it for us */
1192 goto continue_unlock;
1193 }
1194
1195 if (!clear_page_dirty_for_io(page))
1196 goto continue_unlock;
1197
1198 /* called by fsync() */
1199 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001200 set_fsync_mark(page, 1);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -07001201 if (IS_INODE(page)) {
1202 if (!is_checkpointed_node(sbi, ino) &&
1203 !has_fsynced_inode(sbi, ino))
1204 set_dentry_mark(page, 1);
1205 else
1206 set_dentry_mark(page, 0);
1207 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208 nwritten++;
1209 } else {
1210 set_fsync_mark(page, 0);
1211 set_dentry_mark(page, 0);
1212 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001213
1214 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1215 unlock_page(page);
1216 else
1217 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001218
1219 if (--wbc->nr_to_write == 0)
1220 break;
1221 }
1222 pagevec_release(&pvec);
1223 cond_resched();
1224
1225 if (wbc->nr_to_write == 0) {
1226 step = 2;
1227 break;
1228 }
1229 }
1230
1231 if (step < 2) {
1232 step++;
1233 goto next_step;
1234 }
1235
1236 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001237 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001238 return nwritten;
1239}
1240
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001241int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1242{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001243 pgoff_t index = 0, end = LONG_MAX;
1244 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001245 int ret2 = 0, ret = 0;
1246
1247 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001248
1249 while (index <= end) {
1250 int i, nr_pages;
1251 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1252 PAGECACHE_TAG_WRITEBACK,
1253 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1254 if (nr_pages == 0)
1255 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001256
1257 for (i = 0; i < nr_pages; i++) {
1258 struct page *page = pvec.pages[i];
1259
1260 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001261 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001262 continue;
1263
Chao Yu4bf08ff2013-11-04 10:28:33 +08001264 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001265 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001266 if (TestClearPageError(page))
1267 ret = -EIO;
1268 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001269 }
1270 pagevec_release(&pvec);
1271 cond_resched();
1272 }
1273
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001274 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001275 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001276 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001277 ret2 = -EIO;
1278 if (!ret)
1279 ret = ret2;
1280 return ret;
1281}
1282
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001283static int f2fs_write_node_page(struct page *page,
1284 struct writeback_control *wbc)
1285{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001286 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001287 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001288 block_t new_addr;
1289 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001290 struct f2fs_io_info fio = {
1291 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001292 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001293 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294
Chao Yuecda0de2014-05-06 16:48:26 +08001295 trace_f2fs_writepage(page, NODE);
1296
Chao Yucfb271d2013-12-05 17:15:22 +08001297 if (unlikely(sbi->por_doing))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001298 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001299 if (unlikely(f2fs_cp_error(sbi)))
1300 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001301
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001302 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001303
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001304 /* get old block addr of this node page */
1305 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001306 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001307
1308 get_node_info(sbi, nid, &ni);
1309
1310 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001311 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001312 dec_page_count(sbi, F2FS_DIRTY_NODES);
1313 unlock_page(page);
1314 return 0;
1315 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001316
Jaegeuk Kim2f97c322014-11-06 17:21:24 -08001317 if (wbc->for_reclaim) {
1318 if (!down_read_trylock(&sbi->node_write))
1319 goto redirty_out;
1320 } else {
1321 down_read(&sbi->node_write);
1322 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323 set_page_writeback(page);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001324 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001325 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001326 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001327 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001328 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001329
1330 if (wbc->for_reclaim)
1331 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1332
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001334
1335redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001336 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001337 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001338}
1339
1340static int f2fs_write_node_pages(struct address_space *mapping,
1341 struct writeback_control *wbc)
1342{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001343 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001344 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001345
Chao Yue5748432014-05-06 16:51:24 +08001346 trace_f2fs_writepages(mapping->host, wbc, NODE);
1347
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001348 /* balancing f2fs's metadata in background */
1349 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001350
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001351 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001352 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001353 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001354
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001355 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001356 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001357 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001358 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001359 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001360
1361skip_write:
1362 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1363 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001364}
1365
1366static int f2fs_set_node_page_dirty(struct page *page)
1367{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001368 trace_f2fs_set_page_dirty(page, NODE);
1369
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001370 SetPageUptodate(page);
1371 if (!PageDirty(page)) {
1372 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001373 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001374 SetPagePrivate(page);
1375 return 1;
1376 }
1377 return 0;
1378}
1379
Lukas Czernerd47992f2013-05-21 23:17:23 -04001380static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1381 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001382{
1383 struct inode *inode = page->mapping->host;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001384 if (PageDirty(page))
Jaegeuk Kim40813632014-09-02 15:31:18 -07001385 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386 ClearPagePrivate(page);
1387}
1388
1389static int f2fs_release_node_page(struct page *page, gfp_t wait)
1390{
1391 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001392 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001393}
1394
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001395/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001396 * Structure of the f2fs node operations
1397 */
1398const struct address_space_operations f2fs_node_aops = {
1399 .writepage = f2fs_write_node_page,
1400 .writepages = f2fs_write_node_pages,
1401 .set_page_dirty = f2fs_set_node_page_dirty,
1402 .invalidatepage = f2fs_invalidate_node_page,
1403 .releasepage = f2fs_release_node_page,
1404};
1405
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001406static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1407 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001408{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001409 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001410}
1411
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001412static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1413 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001414{
1415 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001416 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001417}
1418
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001419static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001421 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001423 struct nat_entry *ne;
1424 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001426 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001427 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001428
1429 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001430 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001431 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001432
Gu Zheng7bd59382013-10-22 14:52:26 +08001433 if (build) {
1434 /* do not add allocated nids */
1435 read_lock(&nm_i->nat_tree_lock);
1436 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001437 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001438 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1439 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001440 allocated = true;
1441 read_unlock(&nm_i->nat_tree_lock);
1442 if (allocated)
1443 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001445
1446 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001447 i->nid = nid;
1448 i->state = NID_NEW;
1449
1450 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001451 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001452 spin_unlock(&nm_i->free_nid_list_lock);
1453 kmem_cache_free(free_nid_slab, i);
1454 return 0;
1455 }
1456 list_add_tail(&i->list, &nm_i->free_nid_list);
1457 nm_i->fcnt++;
1458 spin_unlock(&nm_i->free_nid_list_lock);
1459 return 1;
1460}
1461
1462static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1463{
1464 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001465 bool need_free = false;
1466
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001467 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001468 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001469 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001470 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001471 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001472 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001473 }
1474 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001475
1476 if (need_free)
1477 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001478}
1479
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001480static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001481 struct page *nat_page, nid_t start_nid)
1482{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001483 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001484 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1485 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001486 int i;
1487
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001488 i = start_nid % NAT_ENTRY_PER_BLOCK;
1489
1490 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001491
Chao Yucfb271d2013-12-05 17:15:22 +08001492 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001493 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001494
1495 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001496 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001497 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001498 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001499 break;
1500 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001502}
1503
1504static void build_free_nids(struct f2fs_sb_info *sbi)
1505{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001506 struct f2fs_nm_info *nm_i = NM_I(sbi);
1507 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1508 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001509 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001510 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001511
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001512 /* Enough entries */
1513 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1514 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001515
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001516 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001517 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001518
1519 while (1) {
1520 struct page *page = get_current_nat_page(sbi, nid);
1521
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001522 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001523 f2fs_put_page(page, 1);
1524
1525 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001526 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001527 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001528
1529 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001530 break;
1531 }
1532
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001533 /* go to the next free nat pages to find free nids abundantly */
1534 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001535
1536 /* find free nids from current sum_pages */
1537 mutex_lock(&curseg->curseg_mutex);
1538 for (i = 0; i < nats_in_cursum(sum); i++) {
1539 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1540 nid = le32_to_cpu(nid_in_journal(sum, i));
1541 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001542 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001543 else
1544 remove_free_nid(nm_i, nid);
1545 }
1546 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001547}
1548
1549/*
1550 * If this function returns success, caller can obtain a new nid
1551 * from second parameter of this function.
1552 * The returned nid could be used ino as well as nid when inode is created.
1553 */
1554bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1555{
1556 struct f2fs_nm_info *nm_i = NM_I(sbi);
1557 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001558retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001559 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001560 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001561
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001562 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001563
1564 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001565 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001566 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001567 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001568 if (i->state == NID_NEW)
1569 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001570
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001571 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001572 *nid = i->nid;
1573 i->state = NID_ALLOC;
1574 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001575 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001576 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001578 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001579
1580 /* Let's scan nat pages and its caches to get free nids */
1581 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001582 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001583 mutex_unlock(&nm_i->build_lock);
1584 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001585}
1586
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001587/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001588 * alloc_nid() should be called prior to this function.
1589 */
1590void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1591{
1592 struct f2fs_nm_info *nm_i = NM_I(sbi);
1593 struct free_nid *i;
1594
1595 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001596 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001597 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001598 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001599 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001600
1601 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001602}
1603
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001604/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001605 * alloc_nid() should be called prior to this function.
1606 */
1607void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1608{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001609 struct f2fs_nm_info *nm_i = NM_I(sbi);
1610 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001611 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001612
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001613 if (!nid)
1614 return;
1615
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001616 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001617 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001618 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001619 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001620 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001621 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001622 } else {
1623 i->state = NID_NEW;
1624 nm_i->fcnt++;
1625 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001626 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001627
1628 if (need_free)
1629 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001630}
1631
Chao Yu70cfed82014-08-02 15:26:04 +08001632void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001633{
Chao Yu28cdce02014-03-11 13:37:38 +08001634 void *src_addr, *dst_addr;
1635 size_t inline_size;
1636 struct page *ipage;
1637 struct f2fs_inode *ri;
1638
Jaegeuk Kim40813632014-09-02 15:31:18 -07001639 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001640 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001641
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001642 ri = F2FS_INODE(page);
1643 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1644 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1645 goto update_inode;
1646 }
1647
Chao Yu28cdce02014-03-11 13:37:38 +08001648 dst_addr = inline_xattr_addr(ipage);
1649 src_addr = inline_xattr_addr(page);
1650 inline_size = inline_xattr_size(inode);
1651
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001652 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001653 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001654update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001655 update_inode(inode, ipage);
1656 f2fs_put_page(ipage, 1);
1657}
1658
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001659void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001660{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001661 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001662 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1663 nid_t new_xnid = nid_of_node(page);
1664 struct node_info ni;
1665
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001666 /* 1: invalidate the previous xattr nid */
1667 if (!prev_xnid)
1668 goto recover_xnid;
1669
1670 /* Deallocate node address */
1671 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001672 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001673 invalidate_blocks(sbi, ni.blk_addr);
1674 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001675 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001676
1677recover_xnid:
1678 /* 2: allocate new xattr nid */
1679 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001680 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001681
1682 remove_free_nid(NM_I(sbi), new_xnid);
1683 get_node_info(sbi, new_xnid, &ni);
1684 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001685 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001686 F2FS_I(inode)->i_xattr_nid = new_xnid;
1687
1688 /* 3: update xattr blkaddr */
1689 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001690 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001691
1692 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001693}
1694
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001695int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1696{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001697 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001698 nid_t ino = ino_of_node(page);
1699 struct node_info old_ni, new_ni;
1700 struct page *ipage;
1701
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001702 get_node_info(sbi, ino, &old_ni);
1703
1704 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1705 return -EINVAL;
1706
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001707 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001708 if (!ipage)
1709 return -ENOMEM;
1710
arter97e1c42042014-08-06 23:22:50 +09001711 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001712 remove_free_nid(NM_I(sbi), ino);
1713
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001714 SetPageUptodate(ipage);
1715 fill_node_footer(ipage, ino, ino, 0, true);
1716
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001717 src = F2FS_INODE(page);
1718 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001719
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001720 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1721 dst->i_size = 0;
1722 dst->i_blocks = cpu_to_le64(1);
1723 dst->i_links = cpu_to_le32(1);
1724 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001725 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001726
1727 new_ni = old_ni;
1728 new_ni.ino = ino;
1729
Chao Yucfb271d2013-12-05 17:15:22 +08001730 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001731 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001732 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001733 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001734 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001735 f2fs_put_page(ipage, 1);
1736 return 0;
1737}
1738
Chao Yu9af0ff12013-11-22 15:48:54 +08001739/*
1740 * ra_sum_pages() merge contiguous pages into one bio and submit.
arter97e1c42042014-08-06 23:22:50 +09001741 * these pre-read pages are allocated in bd_inode's mapping tree.
Chao Yu9af0ff12013-11-22 15:48:54 +08001742 */
Chao Yubac4eef2014-05-27 08:41:07 +08001743static int ra_sum_pages(struct f2fs_sb_info *sbi, struct page **pages,
Chao Yu9af0ff12013-11-22 15:48:54 +08001744 int start, int nrpages)
1745{
Chao Yubac4eef2014-05-27 08:41:07 +08001746 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1747 struct address_space *mapping = inode->i_mapping;
1748 int i, page_idx = start;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001749 struct f2fs_io_info fio = {
1750 .type = META,
Gu Zheng7e8f2302013-12-20 18:17:49 +08001751 .rw = READ_SYNC | REQ_META | REQ_PRIO
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001752 };
Chao Yu9af0ff12013-11-22 15:48:54 +08001753
Chao Yubac4eef2014-05-27 08:41:07 +08001754 for (i = 0; page_idx < start + nrpages; page_idx++, i++) {
1755 /* alloc page in bd_inode for reading node summary info */
1756 pages[i] = grab_cache_page(mapping, page_idx);
1757 if (!pages[i])
Gu Zhengd6537882014-03-07 18:43:36 +08001758 break;
Chao Yubac4eef2014-05-27 08:41:07 +08001759 f2fs_submit_page_mbio(sbi, pages[i], page_idx, &fio);
Chao Yu9af0ff12013-11-22 15:48:54 +08001760 }
1761
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001762 f2fs_submit_merged_bio(sbi, META, READ);
Chao Yubac4eef2014-05-27 08:41:07 +08001763 return i;
Chao Yu9af0ff12013-11-22 15:48:54 +08001764}
1765
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001766int restore_node_summary(struct f2fs_sb_info *sbi,
1767 unsigned int segno, struct f2fs_summary_block *sum)
1768{
1769 struct f2fs_node *rn;
1770 struct f2fs_summary *sum_entry;
Chao Yubac4eef2014-05-27 08:41:07 +08001771 struct inode *inode = sbi->sb->s_bdev->bd_inode;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001772 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001773 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yubac4eef2014-05-27 08:41:07 +08001774 struct page *pages[bio_blocks];
1775 int i, idx, last_offset, nrpages, err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001776
1777 /* scan the node segment */
1778 last_offset = sbi->blocks_per_seg;
1779 addr = START_BLOCK(sbi, segno);
1780 sum_entry = &sum->entries[0];
1781
Gu Zhengd6537882014-03-07 18:43:36 +08001782 for (i = 0; !err && i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001783 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001784
arter97e1c42042014-08-06 23:22:50 +09001785 /* readahead node pages */
Chao Yubac4eef2014-05-27 08:41:07 +08001786 nrpages = ra_sum_pages(sbi, pages, addr, nrpages);
Gu Zhengd6537882014-03-07 18:43:36 +08001787 if (!nrpages)
1788 return -ENOMEM;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001789
Chao Yubac4eef2014-05-27 08:41:07 +08001790 for (idx = 0; idx < nrpages; idx++) {
Gu Zhengd6537882014-03-07 18:43:36 +08001791 if (err)
1792 goto skip;
Chao Yu9af0ff12013-11-22 15:48:54 +08001793
Chao Yubac4eef2014-05-27 08:41:07 +08001794 lock_page(pages[idx]);
1795 if (unlikely(!PageUptodate(pages[idx]))) {
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001796 err = -EIO;
1797 } else {
Chao Yubac4eef2014-05-27 08:41:07 +08001798 rn = F2FS_NODE(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001799 sum_entry->nid = rn->footer.nid;
1800 sum_entry->version = 0;
1801 sum_entry->ofs_in_node = 0;
1802 sum_entry++;
Chao Yu9af0ff12013-11-22 15:48:54 +08001803 }
Chao Yubac4eef2014-05-27 08:41:07 +08001804 unlock_page(pages[idx]);
Gu Zhengd6537882014-03-07 18:43:36 +08001805skip:
Chao Yubac4eef2014-05-27 08:41:07 +08001806 page_cache_release(pages[idx]);
Chao Yu9af0ff12013-11-22 15:48:54 +08001807 }
Chao Yubac4eef2014-05-27 08:41:07 +08001808
1809 invalidate_mapping_pages(inode->i_mapping, addr,
1810 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001811 }
Chao Yu9af0ff12013-11-22 15:48:54 +08001812 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001813}
1814
Chao Yuaec71382014-06-24 09:18:20 +08001815static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001816{
1817 struct f2fs_nm_info *nm_i = NM_I(sbi);
1818 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1819 struct f2fs_summary_block *sum = curseg->sum_blk;
1820 int i;
1821
1822 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001823 for (i = 0; i < nats_in_cursum(sum); i++) {
1824 struct nat_entry *ne;
1825 struct f2fs_nat_entry raw_ne;
1826 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1827
1828 raw_ne = nat_in_journal(sum, i);
1829retry:
1830 write_lock(&nm_i->nat_tree_lock);
1831 ne = __lookup_nat_cache(nm_i, nid);
Chao Yuaec71382014-06-24 09:18:20 +08001832 if (ne)
1833 goto found;
1834
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001835 ne = grab_nat_entry(nm_i, nid);
1836 if (!ne) {
1837 write_unlock(&nm_i->nat_tree_lock);
1838 goto retry;
1839 }
Chao Yu94dac222014-04-17 10:51:05 +08001840 node_info_from_raw_nat(&ne->ni, &raw_ne);
Chao Yuaec71382014-06-24 09:18:20 +08001841found:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001842 __set_nat_cache_dirty(nm_i, ne);
1843 write_unlock(&nm_i->nat_tree_lock);
1844 }
1845 update_nats_in_cursum(sum, -i);
1846 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001847}
1848
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001849static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1850 struct list_head *head, int max)
1851{
1852 struct nat_entry_set *cur;
1853
1854 if (nes->entry_cnt >= max)
1855 goto add_out;
1856
1857 list_for_each_entry(cur, head, set_list) {
1858 if (cur->entry_cnt >= nes->entry_cnt) {
1859 list_add(&nes->set_list, cur->set_list.prev);
1860 return;
1861 }
1862 }
1863add_out:
1864 list_add_tail(&nes->set_list, head);
1865}
1866
1867static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1868 struct nat_entry_set *set)
1869{
1870 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1871 struct f2fs_summary_block *sum = curseg->sum_blk;
1872 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1873 bool to_journal = true;
1874 struct f2fs_nat_block *nat_blk;
1875 struct nat_entry *ne, *cur;
1876 struct page *page = NULL;
1877
1878 /*
1879 * there are two steps to flush nat entries:
1880 * #1, flush nat entries to journal in current hot data summary block.
1881 * #2, flush nat entries to nat page.
1882 */
1883 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1884 to_journal = false;
1885
1886 if (to_journal) {
1887 mutex_lock(&curseg->curseg_mutex);
1888 } else {
1889 page = get_next_nat_page(sbi, start_nid);
1890 nat_blk = page_address(page);
1891 f2fs_bug_on(sbi, !nat_blk);
1892 }
1893
1894 /* flush dirty nats in nat entry set */
1895 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1896 struct f2fs_nat_entry *raw_ne;
1897 nid_t nid = nat_get_nid(ne);
1898 int offset;
1899
1900 if (nat_get_blkaddr(ne) == NEW_ADDR)
1901 continue;
1902
1903 if (to_journal) {
1904 offset = lookup_journal_in_cursum(sum,
1905 NAT_JOURNAL, nid, 1);
1906 f2fs_bug_on(sbi, offset < 0);
1907 raw_ne = &nat_in_journal(sum, offset);
1908 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1909 } else {
1910 raw_ne = &nat_blk->entries[nid - start_nid];
1911 }
1912 raw_nat_from_node_info(raw_ne, &ne->ni);
1913
1914 write_lock(&NM_I(sbi)->nat_tree_lock);
1915 nat_reset_flag(ne);
1916 __clear_nat_cache_dirty(NM_I(sbi), ne);
1917 write_unlock(&NM_I(sbi)->nat_tree_lock);
1918
1919 if (nat_get_blkaddr(ne) == NULL_ADDR)
1920 add_free_nid(sbi, nid, false);
1921 }
1922
1923 if (to_journal)
1924 mutex_unlock(&curseg->curseg_mutex);
1925 else
1926 f2fs_put_page(page, 1);
1927
1928 if (!set->entry_cnt) {
1929 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1930 kmem_cache_free(nat_entry_set_slab, set);
1931 }
1932}
1933
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001934/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001935 * This function is called during the checkpointing process.
1936 */
1937void flush_nat_entries(struct f2fs_sb_info *sbi)
1938{
1939 struct f2fs_nm_info *nm_i = NM_I(sbi);
1940 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1941 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001942 struct nat_entry_set *setvec[NATVEC_SIZE];
1943 struct nat_entry_set *set, *tmp;
1944 unsigned int found;
1945 nid_t set_idx = 0;
1946 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001947
Chao Yuaec71382014-06-24 09:18:20 +08001948 /*
1949 * if there are no enough space in journal to store dirty nat
1950 * entries, remove all entries from journal and merge them
1951 * into nat entry set.
1952 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001953 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001954 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001955
Chao Yuaec71382014-06-24 09:18:20 +08001956 if (!nm_i->dirty_nat_cnt)
1957 return;
Chao Yu2d7b8222014-03-29 11:33:17 +08001958
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001959 while ((found = __gang_lookup_nat_set(nm_i,
1960 set_idx, NATVEC_SIZE, setvec))) {
1961 unsigned idx;
1962 set_idx = setvec[found - 1]->set + 1;
1963 for (idx = 0; idx < found; idx++)
1964 __adjust_nat_entry_set(setvec[idx], &sets,
1965 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001966 }
Chao Yuaec71382014-06-24 09:18:20 +08001967
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001968 /* flush dirty nats in nat entry set */
1969 list_for_each_entry_safe(set, tmp, &sets, set_list)
1970 __flush_nat_entry_set(sbi, set);
1971
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001972 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001973}
1974
1975static int init_node_manager(struct f2fs_sb_info *sbi)
1976{
1977 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1978 struct f2fs_nm_info *nm_i = NM_I(sbi);
1979 unsigned char *version_bitmap;
1980 unsigned int nat_segs, nat_blocks;
1981
1982 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1983
1984 /* segment_count_nat includes pair segment so divide to 2. */
1985 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1986 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001987
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001988 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1989
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001990 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001991 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001992 nm_i->fcnt = 0;
1993 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001994 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001995
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001996 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001997 INIT_LIST_HEAD(&nm_i->free_nid_list);
1998 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001999 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002000 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002001
2002 mutex_init(&nm_i->build_lock);
2003 spin_lock_init(&nm_i->free_nid_list_lock);
2004 rwlock_init(&nm_i->nat_tree_lock);
2005
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002006 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002007 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002008 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2009 if (!version_bitmap)
2010 return -EFAULT;
2011
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002012 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2013 GFP_KERNEL);
2014 if (!nm_i->nat_bitmap)
2015 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002016 return 0;
2017}
2018
2019int build_node_manager(struct f2fs_sb_info *sbi)
2020{
2021 int err;
2022
2023 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2024 if (!sbi->nm_info)
2025 return -ENOMEM;
2026
2027 err = init_node_manager(sbi);
2028 if (err)
2029 return err;
2030
2031 build_free_nids(sbi);
2032 return 0;
2033}
2034
2035void destroy_node_manager(struct f2fs_sb_info *sbi)
2036{
2037 struct f2fs_nm_info *nm_i = NM_I(sbi);
2038 struct free_nid *i, *next_i;
2039 struct nat_entry *natvec[NATVEC_SIZE];
2040 nid_t nid = 0;
2041 unsigned int found;
2042
2043 if (!nm_i)
2044 return;
2045
2046 /* destroy free nid list */
2047 spin_lock(&nm_i->free_nid_list_lock);
2048 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002049 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002050 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002051 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002052 spin_unlock(&nm_i->free_nid_list_lock);
2053 kmem_cache_free(free_nid_slab, i);
2054 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002055 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002056 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002057 spin_unlock(&nm_i->free_nid_list_lock);
2058
2059 /* destroy nat cache */
2060 write_lock(&nm_i->nat_tree_lock);
2061 while ((found = __gang_lookup_nat_cache(nm_i,
2062 nid, NATVEC_SIZE, natvec))) {
2063 unsigned idx;
Gu Zhengb6ce3912014-03-07 18:43:24 +08002064 nid = nat_get_nid(natvec[found - 1]) + 1;
2065 for (idx = 0; idx < found; idx++)
2066 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002067 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002068 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002069 write_unlock(&nm_i->nat_tree_lock);
2070
2071 kfree(nm_i->nat_bitmap);
2072 sbi->nm_info = NULL;
2073 kfree(nm_i);
2074}
2075
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002076int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002077{
2078 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002079 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002080 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002081 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002082
2083 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002084 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002085 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002086 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002087
2088 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2089 sizeof(struct nat_entry_set));
2090 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002091 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002092 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002093
Markus Elfringce3e6d22014-11-24 15:52:00 +01002094destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002095 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002096destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002097 kmem_cache_destroy(nat_entry_slab);
2098fail:
2099 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002100}
2101
2102void destroy_node_manager_caches(void)
2103{
Chao Yuaec71382014-06-24 09:18:20 +08002104 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002105 kmem_cache_destroy(free_nid_slab);
2106 kmem_cache_destroy(nat_entry_slab);
2107}