node.c (b10778a00d40b3d9fdaaf5891e802794781ff71c) node.c (67298804f34452a53a9ec9e609d95aa35084132b)
1/*
2 * 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

--- 17 unchanged lines hidden (view full) ---

26static struct kmem_cache *nat_entry_slab;
27static struct kmem_cache *free_nid_slab;
28static struct kmem_cache *nat_entry_set_slab;
29
30bool available_free_memory(struct f2fs_sb_info *sbi, int type)
31{
32 struct f2fs_nm_info *nm_i = NM_I(sbi);
33 struct sysinfo val;
1/*
2 * 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

--- 17 unchanged lines hidden (view full) ---

26static struct kmem_cache *nat_entry_slab;
27static struct kmem_cache *free_nid_slab;
28static struct kmem_cache *nat_entry_set_slab;
29
30bool available_free_memory(struct f2fs_sb_info *sbi, int type)
31{
32 struct f2fs_nm_info *nm_i = NM_I(sbi);
33 struct sysinfo val;
34 unsigned long avail_ram;
34 unsigned long mem_size = 0;
35 bool res = false;
36
37 si_meminfo(&val);
35 unsigned long mem_size = 0;
36 bool res = false;
37
38 si_meminfo(&val);
38 /* give 25%, 25%, 50% memory for each components respectively */
39
40 /* only uses low memory */
41 avail_ram = val.totalram - val.totalhigh;
42
43 /* give 25%, 25%, 50%, 50% memory for each components respectively */
39 if (type == FREE_NIDS) {
44 if (type == FREE_NIDS) {
40 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >> 12;
41 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
45 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);
42 } else if (type == NAT_ENTRIES) {
48 } else if (type == NAT_ENTRIES) {
43 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >> 12;
44 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 2);
49 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);
45 } else if (type == DIRTY_DENTS) {
46 if (sbi->sb->s_bdi->dirty_exceeded)
47 return false;
48 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
52 } else if (type == DIRTY_DENTS) {
53 if (sbi->sb->s_bdi->dirty_exceeded)
54 return false;
55 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
49 res = mem_size < ((val.totalram * nm_i->ram_thresh / 100) >> 1);
56 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++)
63 mem_size += (sbi->im[i].ino_num *
64 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
65 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
50 }
51 return res;
52}
53
54static void clear_node_page_dirty(struct page *page)
55{
56 struct address_space *mapping = page->mapping;
66 }
67 return res;
68}
69
70static void clear_node_page_dirty(struct page *page)
71{
72 struct address_space *mapping = page->mapping;
57 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
58 unsigned int long flags;
59
60 if (PageDirty(page)) {
61 spin_lock_irqsave(&mapping->tree_lock, flags);
62 radix_tree_tag_clear(&mapping->page_tree,
63 page_index(page),
64 PAGECACHE_TAG_DIRTY);
65 spin_unlock_irqrestore(&mapping->tree_lock, flags);
66
67 clear_page_dirty_for_io(page);
73 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);
68 dec_page_count(sbi, F2FS_DIRTY_NODES);
83 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
69 }
70 ClearPageUptodate(page);
71}
72
73static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
74{
75 pgoff_t index = current_nat_addr(sbi, nid);
76 return get_meta_page(sbi, index);

--- 10 unchanged lines hidden (view full) ---

87 struct f2fs_nm_info *nm_i = NM_I(sbi);
88
89 src_off = current_nat_addr(sbi, nid);
90 dst_off = next_nat_addr(sbi, src_off);
91
92 /* get current nat block page with lock */
93 src_page = get_meta_page(sbi, src_off);
94 dst_page = grab_meta_page(sbi, dst_off);
84 }
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);

--- 10 unchanged lines hidden (view full) ---

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);
109 dst_page = grab_meta_page(sbi, dst_off);
95 f2fs_bug_on(PageDirty(src_page));
110 f2fs_bug_on(sbi, PageDirty(src_page));
96
97 src_addr = page_address(src_page);
98 dst_addr = page_address(dst_page);
99 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
100 set_page_dirty(dst_page);
101 f2fs_put_page(src_page, 1);
102
103 set_to_next_nat(nm_i, nid);

--- 15 unchanged lines hidden (view full) ---

119static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
120{
121 list_del(&e->list);
122 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
123 nm_i->nat_cnt--;
124 kmem_cache_free(nat_entry_slab, e);
125}
126
111
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);

--- 15 unchanged lines hidden (view full) ---

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
127int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
142static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
143 struct nat_entry *ne)
128{
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
193bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
194{
129 struct f2fs_nm_info *nm_i = NM_I(sbi);
130 struct nat_entry *e;
195 struct f2fs_nm_info *nm_i = NM_I(sbi);
196 struct nat_entry *e;
131 int is_cp = 1;
197 bool is_cp = true;
132
133 read_lock(&nm_i->nat_tree_lock);
134 e = __lookup_nat_cache(nm_i, nid);
198
199 read_lock(&nm_i->nat_tree_lock);
200 e = __lookup_nat_cache(nm_i, nid);
135 if (e && !e->checkpointed)
136 is_cp = 0;
201 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
202 is_cp = false;
137 read_unlock(&nm_i->nat_tree_lock);
138 return is_cp;
139}
140
203 read_unlock(&nm_i->nat_tree_lock);
204 return is_cp;
205}
206
141bool fsync_mark_done(struct f2fs_sb_info *sbi, nid_t nid)
207bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino)
142{
143 struct f2fs_nm_info *nm_i = NM_I(sbi);
144 struct nat_entry *e;
208{
209 struct f2fs_nm_info *nm_i = NM_I(sbi);
210 struct nat_entry *e;
145 bool fsync_done = false;
211 bool fsynced = false;
146
147 read_lock(&nm_i->nat_tree_lock);
212
213 read_lock(&nm_i->nat_tree_lock);
148 e = __lookup_nat_cache(nm_i, nid);
149 if (e)
150 fsync_done = e->fsync_done;
214 e = __lookup_nat_cache(nm_i, ino);
215 if (e && get_nat_flag(e, HAS_FSYNCED_INODE))
216 fsynced = true;
151 read_unlock(&nm_i->nat_tree_lock);
217 read_unlock(&nm_i->nat_tree_lock);
152 return fsync_done;
218 return fsynced;
153}
154
219}
220
155void fsync_mark_clear(struct f2fs_sb_info *sbi, nid_t nid)
221bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
156{
157 struct f2fs_nm_info *nm_i = NM_I(sbi);
158 struct nat_entry *e;
222{
223 struct f2fs_nm_info *nm_i = NM_I(sbi);
224 struct nat_entry *e;
225 bool need_update = true;
159
226
160 write_lock(&nm_i->nat_tree_lock);
161 e = __lookup_nat_cache(nm_i, nid);
162 if (e)
163 e->fsync_done = false;
164 write_unlock(&nm_i->nat_tree_lock);
227 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;
165}
166
167static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
168{
169 struct nat_entry *new;
170
171 new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
172 if (!new)
173 return NULL;
174 if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
175 kmem_cache_free(nat_entry_slab, new);
176 return NULL;
177 }
178 memset(new, 0, sizeof(struct nat_entry));
179 nat_set_nid(new, nid);
235}
236
237static 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);
180 new->checkpointed = true;
250 nat_reset_flag(new);
181 list_add_tail(&new->list, &nm_i->nat_entries);
182 nm_i->nat_cnt++;
183 return new;
184}
185
186static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
187 struct f2fs_nat_entry *ne)
188{

--- 22 unchanged lines hidden (view full) ---

211 e = __lookup_nat_cache(nm_i, ni->nid);
212 if (!e) {
213 e = grab_nat_entry(nm_i, ni->nid);
214 if (!e) {
215 write_unlock(&nm_i->nat_tree_lock);
216 goto retry;
217 }
218 e->ni = *ni;
251 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{

--- 22 unchanged lines hidden (view full) ---

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;
219 f2fs_bug_on(ni->blk_addr == NEW_ADDR);
289 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
220 } else if (new_blkaddr == NEW_ADDR) {
221 /*
222 * when nid is reallocated,
223 * previous nat entry can be remained in nat cache.
224 * So, reinitialize it with new information.
225 */
226 e->ni = *ni;
290 } 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;
227 f2fs_bug_on(ni->blk_addr != NULL_ADDR);
297 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
228 }
229
230 /* sanity check */
298 }
299
300 /* sanity check */
231 f2fs_bug_on(nat_get_blkaddr(e) != ni->blk_addr);
232 f2fs_bug_on(nat_get_blkaddr(e) == NULL_ADDR &&
301 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
302 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
233 new_blkaddr == NULL_ADDR);
303 new_blkaddr == NULL_ADDR);
234 f2fs_bug_on(nat_get_blkaddr(e) == NEW_ADDR &&
304 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
235 new_blkaddr == NEW_ADDR);
305 new_blkaddr == NEW_ADDR);
236 f2fs_bug_on(nat_get_blkaddr(e) != NEW_ADDR &&
306 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
237 nat_get_blkaddr(e) != NULL_ADDR &&
238 new_blkaddr == NEW_ADDR);
239
240 /* increment version no as node is removed */
241 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
242 unsigned char version = nat_get_version(e);
243 nat_set_version(e, inc_node_version(version));
244 }
245
246 /* change address */
247 nat_set_blkaddr(e, new_blkaddr);
307 nat_get_blkaddr(e) != NULL_ADDR &&
308 new_blkaddr == NEW_ADDR);
309
310 /* increment version no as node is removed */
311 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);
318 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
319 set_nat_flag(e, IS_CHECKPOINTED, false);
248 __set_nat_cache_dirty(nm_i, e);
249
250 /* update fsync_mark if its inode nat entry is still alive */
251 e = __lookup_nat_cache(nm_i, ni->ino);
320 __set_nat_cache_dirty(nm_i, e);
321
322 /* update fsync_mark if its inode nat entry is still alive */
323 e = __lookup_nat_cache(nm_i, ni->ino);
252 if (e)
253 e->fsync_done = fsync_done;
324 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 }
254 write_unlock(&nm_i->nat_tree_lock);
255}
256
257int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
258{
259 struct f2fs_nm_info *nm_i = NM_I(sbi);
260
261 if (available_free_memory(sbi, NAT_ENTRIES))

--- 144 unchanged lines hidden (view full) ---

406/*
407 * Caller should call f2fs_put_dnode(dn).
408 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
409 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
410 * In the case of RDONLY_NODE, we don't need to care about mutex.
411 */
412int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
413{
329 write_unlock(&nm_i->nat_tree_lock);
330}
331
332int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
333{
334 struct f2fs_nm_info *nm_i = NM_I(sbi);
335
336 if (available_free_memory(sbi, NAT_ENTRIES))

--- 144 unchanged lines hidden (view full) ---

481/*
482 * Caller should call f2fs_put_dnode(dn).
483 * 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.
485 * In the case of RDONLY_NODE, we don't need to care about mutex.
486 */
487int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
488{
414 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
489 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
415 struct page *npage[4];
416 struct page *parent;
417 int offset[4];
418 unsigned int noffset[4];
419 nid_t nids[4];
420 int level, i;
421 int err = 0;
422

--- 76 unchanged lines hidden (view full) ---

499release_out:
500 dn->inode_page = NULL;
501 dn->node_page = NULL;
502 return err;
503}
504
505static void truncate_node(struct dnode_of_data *dn)
506{
490 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

--- 76 unchanged lines hidden (view full) ---

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{
507 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
582 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
508 struct node_info ni;
509
510 get_node_info(sbi, dn->nid, &ni);
511 if (dn->inode->i_blocks == 0) {
583 struct node_info ni;
584
585 get_node_info(sbi, dn->nid, &ni);
586 if (dn->inode->i_blocks == 0) {
512 f2fs_bug_on(ni.blk_addr != NULL_ADDR);
587 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
513 goto invalidate;
514 }
588 goto invalidate;
589 }
515 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
590 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
516
517 /* Deallocate node address */
518 invalidate_blocks(sbi, ni.blk_addr);
519 dec_valid_node_count(sbi, dn->inode);
520 set_node_addr(sbi, &ni, NULL_ADDR, false);
521
522 if (dn->nid == dn->inode->i_ino) {
523 remove_orphan_inode(sbi, dn->nid);

--- 11 unchanged lines hidden (view full) ---

535 dn->node_page->index, dn->node_page->index);
536
537 dn->node_page = NULL;
538 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
539}
540
541static int truncate_dnode(struct dnode_of_data *dn)
542{
591
592 /* Deallocate node address */
593 invalidate_blocks(sbi, ni.blk_addr);
594 dec_valid_node_count(sbi, dn->inode);
595 set_node_addr(sbi, &ni, NULL_ADDR, false);
596
597 if (dn->nid == dn->inode->i_ino) {
598 remove_orphan_inode(sbi, dn->nid);

--- 11 unchanged lines hidden (view full) ---

610 dn->node_page->index, dn->node_page->index);
611
612 dn->node_page = NULL;
613 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
614}
615
616static int truncate_dnode(struct dnode_of_data *dn)
617{
543 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
544 struct page *page;
545
546 if (dn->nid == 0)
547 return 1;
548
549 /* get direct node */
618 struct page *page;
619
620 if (dn->nid == 0)
621 return 1;
622
623 /* get direct node */
550 page = get_node_page(sbi, dn->nid);
624 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
551 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
552 return 1;
553 else if (IS_ERR(page))
554 return PTR_ERR(page);
555
556 /* Make dnode_of_data for parameter */
557 dn->node_page = page;
558 dn->ofs_in_node = 0;
559 truncate_data_blocks(dn);
560 truncate_node(dn);
561 return 1;
562}
563
564static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
565 int ofs, int depth)
566{
625 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{
567 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
568 struct dnode_of_data rdn = *dn;
569 struct page *page;
570 struct f2fs_node *rn;
571 nid_t child_nid;
572 unsigned int child_nofs;
573 int freed = 0;
574 int i, ret;
575
576 if (dn->nid == 0)
577 return NIDS_PER_BLOCK + 1;
578
579 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
580
641 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
652 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
653
581 page = get_node_page(sbi, dn->nid);
654 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
582 if (IS_ERR(page)) {
583 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
584 return PTR_ERR(page);
585 }
586
587 rn = F2FS_NODE(page);
588 if (depth < 3) {
589 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {

--- 41 unchanged lines hidden (view full) ---

631 f2fs_put_page(page, 1);
632 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
633 return ret;
634}
635
636static int truncate_partial_nodes(struct dnode_of_data *dn,
637 struct f2fs_inode *ri, int *offset, int depth)
638{
655 if (IS_ERR(page)) {
656 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
657 return PTR_ERR(page);
658 }
659
660 rn = F2FS_NODE(page);
661 if (depth < 3) {
662 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {

--- 41 unchanged lines hidden (view full) ---

704 f2fs_put_page(page, 1);
705 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
706 return ret;
707}
708
709static int truncate_partial_nodes(struct dnode_of_data *dn,
710 struct f2fs_inode *ri, int *offset, int depth)
711{
639 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
640 struct page *pages[2];
641 nid_t nid[3];
642 nid_t child_nid;
643 int err = 0;
644 int i;
645 int idx = depth - 2;
646
647 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
648 if (!nid[0])
649 return 0;
650
651 /* get indirect nodes in the path */
652 for (i = 0; i < idx + 1; i++) {
653 /* reference count'll be increased */
712 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 */
724 for (i = 0; i < idx + 1; i++) {
725 /* reference count'll be increased */
654 pages[i] = get_node_page(sbi, nid[i]);
726 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
655 if (IS_ERR(pages[i])) {
656 err = PTR_ERR(pages[i]);
657 idx = i - 1;
658 goto fail;
659 }
660 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
661 }
662

--- 28 unchanged lines hidden (view full) ---

691 return err;
692}
693
694/*
695 * All the block addresses of data and nodes should be nullified.
696 */
697int truncate_inode_blocks(struct inode *inode, pgoff_t from)
698{
727 if (IS_ERR(pages[i])) {
728 err = PTR_ERR(pages[i]);
729 idx = i - 1;
730 goto fail;
731 }
732 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
733 }
734

--- 28 unchanged lines hidden (view full) ---

763 return err;
764}
765
766/*
767 * All the block addresses of data and nodes should be nullified.
768 */
769int truncate_inode_blocks(struct inode *inode, pgoff_t from)
770{
699 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
771 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
700 int err = 0, cont = 1;
701 int level, offset[4], noffset[4];
702 unsigned int nofs = 0;
703 struct f2fs_inode *ri;
704 struct dnode_of_data dn;
705 struct page *page;
706
707 trace_f2fs_truncate_inode_blocks_enter(inode, from);

--- 79 unchanged lines hidden (view full) ---

787fail:
788 f2fs_put_page(page, 0);
789 trace_f2fs_truncate_inode_blocks_exit(inode, err);
790 return err > 0 ? 0 : err;
791}
792
793int truncate_xattr_node(struct inode *inode, struct page *page)
794{
772 int err = 0, cont = 1;
773 int level, offset[4], noffset[4];
774 unsigned int nofs = 0;
775 struct f2fs_inode *ri;
776 struct dnode_of_data dn;
777 struct page *page;
778
779 trace_f2fs_truncate_inode_blocks_enter(inode, from);

--- 79 unchanged lines hidden (view full) ---

859fail:
860 f2fs_put_page(page, 0);
861 trace_f2fs_truncate_inode_blocks_exit(inode, err);
862 return err > 0 ? 0 : err;
863}
864
865int truncate_xattr_node(struct inode *inode, struct page *page)
866{
795 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
867 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
796 nid_t nid = F2FS_I(inode)->i_xattr_nid;
797 struct dnode_of_data dn;
798 struct page *npage;
799
800 if (!nid)
801 return 0;
802
803 npage = get_node_page(sbi, nid);

--- 31 unchanged lines hidden (view full) ---

835 }
836
837 /* remove potential inline_data blocks */
838 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
839 S_ISLNK(inode->i_mode))
840 truncate_data_blocks_range(&dn, 1);
841
842 /* 0 is possible, after f2fs_new_inode() has failed */
868 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);

--- 31 unchanged lines hidden (view full) ---

907 }
908
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
914 /* 0 is possible, after f2fs_new_inode() has failed */
843 f2fs_bug_on(inode->i_blocks != 0 && inode->i_blocks != 1);
915 f2fs_bug_on(F2FS_I_SB(inode),
916 inode->i_blocks != 0 && inode->i_blocks != 1);
844
845 /* will put inode & node pages */
846 truncate_node(&dn);
847}
848
849struct page *new_inode_page(struct inode *inode)
850{
851 struct dnode_of_data dn;
852
853 /* allocate inode page for new inode */
854 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
855
856 /* caller should f2fs_put_page(page, 1); */
857 return new_node_page(&dn, 0, NULL);
858}
859
860struct page *new_node_page(struct dnode_of_data *dn,
861 unsigned int ofs, struct page *ipage)
862{
917
918 /* will put inode & node pages */
919 truncate_node(&dn);
920}
921
922struct page *new_inode_page(struct inode *inode)
923{
924 struct dnode_of_data dn;
925
926 /* allocate inode page for new inode */
927 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
928
929 /* caller should f2fs_put_page(page, 1); */
930 return new_node_page(&dn, 0, NULL);
931}
932
933struct page *new_node_page(struct dnode_of_data *dn,
934 unsigned int ofs, struct page *ipage)
935{
863 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
936 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
864 struct node_info old_ni, new_ni;
865 struct page *page;
866 int err;
867
868 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
869 return ERR_PTR(-EPERM);
870
871 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
872 if (!page)
873 return ERR_PTR(-ENOMEM);
874
875 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
876 err = -ENOSPC;
877 goto fail;
878 }
879
880 get_node_info(sbi, dn->nid, &old_ni);
881
882 /* Reinitialize old_ni with new node page */
937 struct node_info old_ni, new_ni;
938 struct page *page;
939 int err;
940
941 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
942 return ERR_PTR(-EPERM);
943
944 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
945 if (!page)
946 return ERR_PTR(-ENOMEM);
947
948 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
949 err = -ENOSPC;
950 goto fail;
951 }
952
953 get_node_info(sbi, dn->nid, &old_ni);
954
955 /* Reinitialize old_ni with new node page */
883 f2fs_bug_on(old_ni.blk_addr != NULL_ADDR);
956 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
884 new_ni = old_ni;
885 new_ni.ino = dn->inode->i_ino;
886 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
887
888 f2fs_wait_on_page_writeback(page, NODE);
889 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
890 set_cold_node(dn->inode, page);
891 SetPageUptodate(page);

--- 21 unchanged lines hidden (view full) ---

913/*
914 * Caller should do after getting the following values.
915 * 0: f2fs_put_page(page, 0)
916 * LOCKED_PAGE: f2fs_put_page(page, 1)
917 * error: nothing
918 */
919static int read_node_page(struct page *page, int rw)
920{
957 new_ni = old_ni;
958 new_ni.ino = dn->inode->i_ino;
959 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
960
961 f2fs_wait_on_page_writeback(page, NODE);
962 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
963 set_cold_node(dn->inode, page);
964 SetPageUptodate(page);

--- 21 unchanged lines hidden (view full) ---

986/*
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 */
992static int read_node_page(struct page *page, int rw)
993{
921 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
994 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
922 struct node_info ni;
923
924 get_node_info(sbi, page->index, &ni);
925
926 if (unlikely(ni.blk_addr == NULL_ADDR)) {
927 f2fs_put_page(page, 1);
928 return -ENOENT;
929 }

--- 59 unchanged lines hidden (view full) ---

989}
990
991/*
992 * Return a locked page for the desired node page.
993 * And, readahead MAX_RA_NODE number of node pages.
994 */
995struct page *get_node_page_ra(struct page *parent, int start)
996{
995 struct node_info ni;
996
997 get_node_info(sbi, page->index, &ni);
998
999 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1000 f2fs_put_page(page, 1);
1001 return -ENOENT;
1002 }

--- 59 unchanged lines hidden (view full) ---

1062}
1063
1064/*
1065 * 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{
997 struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
1070 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
998 struct blk_plug plug;
999 struct page *page;
1000 int err, i, end;
1001 nid_t nid;
1002
1003 /* First, try getting the desired direct node. */
1004 nid = get_nid(parent, start, false);
1005 if (!nid)

--- 113 unchanged lines hidden (view full) ---

1119 goto continue_unlock;
1120 }
1121
1122 if (!clear_page_dirty_for_io(page))
1123 goto continue_unlock;
1124
1125 /* called by fsync() */
1126 if (ino && IS_DNODE(page)) {
1071 struct blk_plug plug;
1072 struct page *page;
1073 int err, i, end;
1074 nid_t nid;
1075
1076 /* First, try getting the desired direct node. */
1077 nid = get_nid(parent, start, false);
1078 if (!nid)

--- 113 unchanged lines hidden (view full) ---

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)) {
1127 int mark = !is_checkpointed_node(sbi, ino);
1128 set_fsync_mark(page, 1);
1200 set_fsync_mark(page, 1);
1129 if (IS_INODE(page))
1130 set_dentry_mark(page, mark);
1201 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 }
1131 nwritten++;
1132 } else {
1133 set_fsync_mark(page, 0);
1134 set_dentry_mark(page, 0);
1135 }
1136
1137 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1138 unlock_page(page);

--- 62 unchanged lines hidden (view full) ---

1201 if (!ret)
1202 ret = ret2;
1203 return ret;
1204}
1205
1206static int f2fs_write_node_page(struct page *page,
1207 struct writeback_control *wbc)
1208{
1208 nwritten++;
1209 } else {
1210 set_fsync_mark(page, 0);
1211 set_dentry_mark(page, 0);
1212 }
1213
1214 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1215 unlock_page(page);

--- 62 unchanged lines hidden (view full) ---

1278 if (!ret)
1279 ret = ret2;
1280 return ret;
1281}
1282
1283static int f2fs_write_node_page(struct page *page,
1284 struct writeback_control *wbc)
1285{
1209 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1286 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1210 nid_t nid;
1211 block_t new_addr;
1212 struct node_info ni;
1213 struct f2fs_io_info fio = {
1214 .type = NODE,
1215 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
1216 };
1217
1218 trace_f2fs_writepage(page, NODE);
1219
1220 if (unlikely(sbi->por_doing))
1221 goto redirty_out;
1222 if (unlikely(f2fs_cp_error(sbi)))
1223 goto redirty_out;
1224
1225 f2fs_wait_on_page_writeback(page, NODE);
1226
1227 /* get old block addr of this node page */
1228 nid = nid_of_node(page);
1287 nid_t nid;
1288 block_t new_addr;
1289 struct node_info ni;
1290 struct f2fs_io_info fio = {
1291 .type = NODE,
1292 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
1293 };
1294
1295 trace_f2fs_writepage(page, NODE);
1296
1297 if (unlikely(sbi->por_doing))
1298 goto redirty_out;
1299 if (unlikely(f2fs_cp_error(sbi)))
1300 goto redirty_out;
1301
1302 f2fs_wait_on_page_writeback(page, NODE);
1303
1304 /* get old block addr of this node page */
1305 nid = nid_of_node(page);
1229 f2fs_bug_on(page->index != nid);
1306 f2fs_bug_on(sbi, page->index != nid);
1230
1231 get_node_info(sbi, nid, &ni);
1232
1233 /* This page is already truncated */
1234 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1235 dec_page_count(sbi, F2FS_DIRTY_NODES);
1236 unlock_page(page);
1237 return 0;
1238 }
1239
1307
1308 get_node_info(sbi, nid, &ni);
1309
1310 /* This page is already truncated */
1311 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1312 dec_page_count(sbi, F2FS_DIRTY_NODES);
1313 unlock_page(page);
1314 return 0;
1315 }
1316
1240 if (wbc->for_reclaim)
1241 goto redirty_out;
1242
1243 down_read(&sbi->node_write);
1317 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 }
1244 set_page_writeback(page);
1245 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
1246 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
1247 dec_page_count(sbi, F2FS_DIRTY_NODES);
1248 up_read(&sbi->node_write);
1249 unlock_page(page);
1250 return 0;
1251
1252redirty_out:
1253 redirty_page_for_writepage(wbc, page);
1254 return AOP_WRITEPAGE_ACTIVATE;
1255}
1256
1257static int f2fs_write_node_pages(struct address_space *mapping,
1258 struct writeback_control *wbc)
1259{
1323 set_page_writeback(page);
1324 write_node_page(sbi, page, &fio, nid, ni.blk_addr, &new_addr);
1325 set_node_addr(sbi, &ni, new_addr, is_fsync_dnode(page));
1326 dec_page_count(sbi, F2FS_DIRTY_NODES);
1327 up_read(&sbi->node_write);
1328 unlock_page(page);
1329 return 0;
1330
1331redirty_out:
1332 redirty_page_for_writepage(wbc, page);
1333 return AOP_WRITEPAGE_ACTIVATE;
1334}
1335
1336static int f2fs_write_node_pages(struct address_space *mapping,
1337 struct writeback_control *wbc)
1338{
1260 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1339 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1261 long diff;
1262
1263 trace_f2fs_writepages(mapping->host, wbc, NODE);
1264
1265 /* balancing f2fs's metadata in background */
1266 f2fs_balance_fs_bg(sbi);
1267
1268 /* collect a number of dirty node pages and write together */

--- 8 unchanged lines hidden (view full) ---

1277
1278skip_write:
1279 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1280 return 0;
1281}
1282
1283static int f2fs_set_node_page_dirty(struct page *page)
1284{
1340 long diff;
1341
1342 trace_f2fs_writepages(mapping->host, wbc, NODE);
1343
1344 /* balancing f2fs's metadata in background */
1345 f2fs_balance_fs_bg(sbi);
1346
1347 /* collect a number of dirty node pages and write together */

--- 8 unchanged lines hidden (view full) ---

1356
1357skip_write:
1358 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1359 return 0;
1360}
1361
1362static int f2fs_set_node_page_dirty(struct page *page)
1363{
1285 struct address_space *mapping = page->mapping;
1286 struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1287
1288 trace_f2fs_set_page_dirty(page, NODE);
1289
1290 SetPageUptodate(page);
1291 if (!PageDirty(page)) {
1292 __set_page_dirty_nobuffers(page);
1364 trace_f2fs_set_page_dirty(page, NODE);
1365
1366 SetPageUptodate(page);
1367 if (!PageDirty(page)) {
1368 __set_page_dirty_nobuffers(page);
1293 inc_page_count(sbi, F2FS_DIRTY_NODES);
1369 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
1294 SetPagePrivate(page);
1295 return 1;
1296 }
1297 return 0;
1298}
1299
1300static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1301 unsigned int length)
1302{
1303 struct inode *inode = page->mapping->host;
1370 SetPagePrivate(page);
1371 return 1;
1372 }
1373 return 0;
1374}
1375
1376static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1377 unsigned int length)
1378{
1379 struct inode *inode = page->mapping->host;
1304 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1305 if (PageDirty(page))
1380 if (PageDirty(page))
1306 dec_page_count(sbi, F2FS_DIRTY_NODES);
1381 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_NODES);
1307 ClearPagePrivate(page);
1308}
1309
1310static int f2fs_release_node_page(struct page *page, gfp_t wait)
1311{
1312 ClearPagePrivate(page);
1313 return 1;
1314}

--- 36 unchanged lines hidden (view full) ---

1351 if (unlikely(nid == 0))
1352 return 0;
1353
1354 if (build) {
1355 /* do not add allocated nids */
1356 read_lock(&nm_i->nat_tree_lock);
1357 ne = __lookup_nat_cache(nm_i, nid);
1358 if (ne &&
1382 ClearPagePrivate(page);
1383}
1384
1385static int f2fs_release_node_page(struct page *page, gfp_t wait)
1386{
1387 ClearPagePrivate(page);
1388 return 1;
1389}

--- 36 unchanged lines hidden (view full) ---

1426 if (unlikely(nid == 0))
1427 return 0;
1428
1429 if (build) {
1430 /* do not add allocated nids */
1431 read_lock(&nm_i->nat_tree_lock);
1432 ne = __lookup_nat_cache(nm_i, nid);
1433 if (ne &&
1359 (!ne->checkpointed || nat_get_blkaddr(ne) != NULL_ADDR))
1434 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1435 nat_get_blkaddr(ne) != NULL_ADDR))
1360 allocated = true;
1361 read_unlock(&nm_i->nat_tree_lock);
1362 if (allocated)
1363 return 0;
1364 }
1365
1366 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1367 i->nid = nid;

--- 40 unchanged lines hidden (view full) ---

1408 i = start_nid % NAT_ENTRY_PER_BLOCK;
1409
1410 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1411
1412 if (unlikely(start_nid >= nm_i->max_nid))
1413 break;
1414
1415 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1436 allocated = true;
1437 read_unlock(&nm_i->nat_tree_lock);
1438 if (allocated)
1439 return 0;
1440 }
1441
1442 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1443 i->nid = nid;

--- 40 unchanged lines hidden (view full) ---

1484 i = start_nid % NAT_ENTRY_PER_BLOCK;
1485
1486 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1487
1488 if (unlikely(start_nid >= nm_i->max_nid))
1489 break;
1490
1491 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1416 f2fs_bug_on(blk_addr == NEW_ADDR);
1492 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
1417 if (blk_addr == NULL_ADDR) {
1418 if (add_free_nid(sbi, start_nid, true) < 0)
1419 break;
1420 }
1421 }
1422}
1423
1424static void build_free_nids(struct f2fs_sb_info *sbi)

--- 53 unchanged lines hidden (view full) ---

1478retry:
1479 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
1480 return false;
1481
1482 spin_lock(&nm_i->free_nid_list_lock);
1483
1484 /* We should not use stale free nids created by build_free_nids */
1485 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
1493 if (blk_addr == NULL_ADDR) {
1494 if (add_free_nid(sbi, start_nid, true) < 0)
1495 break;
1496 }
1497 }
1498}
1499
1500static void build_free_nids(struct f2fs_sb_info *sbi)

--- 53 unchanged lines hidden (view full) ---

1554retry:
1555 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
1556 return false;
1557
1558 spin_lock(&nm_i->free_nid_list_lock);
1559
1560 /* We should not use stale free nids created by build_free_nids */
1561 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
1486 f2fs_bug_on(list_empty(&nm_i->free_nid_list));
1562 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
1487 list_for_each_entry(i, &nm_i->free_nid_list, list)
1488 if (i->state == NID_NEW)
1489 break;
1490
1563 list_for_each_entry(i, &nm_i->free_nid_list, list)
1564 if (i->state == NID_NEW)
1565 break;
1566
1491 f2fs_bug_on(i->state != NID_NEW);
1567 f2fs_bug_on(sbi, i->state != NID_NEW);
1492 *nid = i->nid;
1493 i->state = NID_ALLOC;
1494 nm_i->fcnt--;
1495 spin_unlock(&nm_i->free_nid_list_lock);
1496 return true;
1497 }
1498 spin_unlock(&nm_i->free_nid_list_lock);
1499

--- 9 unchanged lines hidden (view full) ---

1509 */
1510void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1511{
1512 struct f2fs_nm_info *nm_i = NM_I(sbi);
1513 struct free_nid *i;
1514
1515 spin_lock(&nm_i->free_nid_list_lock);
1516 i = __lookup_free_nid_list(nm_i, nid);
1568 *nid = i->nid;
1569 i->state = NID_ALLOC;
1570 nm_i->fcnt--;
1571 spin_unlock(&nm_i->free_nid_list_lock);
1572 return true;
1573 }
1574 spin_unlock(&nm_i->free_nid_list_lock);
1575

--- 9 unchanged lines hidden (view full) ---

1585 */
1586void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1587{
1588 struct f2fs_nm_info *nm_i = NM_I(sbi);
1589 struct free_nid *i;
1590
1591 spin_lock(&nm_i->free_nid_list_lock);
1592 i = __lookup_free_nid_list(nm_i, nid);
1517 f2fs_bug_on(!i || i->state != NID_ALLOC);
1593 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
1518 __del_from_free_nid_list(nm_i, i);
1519 spin_unlock(&nm_i->free_nid_list_lock);
1520
1521 kmem_cache_free(free_nid_slab, i);
1522}
1523
1524/*
1525 * alloc_nid() should be called prior to this function.

--- 4 unchanged lines hidden (view full) ---

1530 struct free_nid *i;
1531 bool need_free = false;
1532
1533 if (!nid)
1534 return;
1535
1536 spin_lock(&nm_i->free_nid_list_lock);
1537 i = __lookup_free_nid_list(nm_i, nid);
1594 __del_from_free_nid_list(nm_i, i);
1595 spin_unlock(&nm_i->free_nid_list_lock);
1596
1597 kmem_cache_free(free_nid_slab, i);
1598}
1599
1600/*
1601 * alloc_nid() should be called prior to this function.

--- 4 unchanged lines hidden (view full) ---

1606 struct free_nid *i;
1607 bool need_free = false;
1608
1609 if (!nid)
1610 return;
1611
1612 spin_lock(&nm_i->free_nid_list_lock);
1613 i = __lookup_free_nid_list(nm_i, nid);
1538 f2fs_bug_on(!i || i->state != NID_ALLOC);
1614 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
1539 if (!available_free_memory(sbi, FREE_NIDS)) {
1540 __del_from_free_nid_list(nm_i, i);
1541 need_free = true;
1542 } else {
1543 i->state = NID_NEW;
1544 nm_i->fcnt++;
1545 }
1546 spin_unlock(&nm_i->free_nid_list_lock);
1547
1548 if (need_free)
1549 kmem_cache_free(free_nid_slab, i);
1550}
1551
1552void recover_inline_xattr(struct inode *inode, struct page *page)
1553{
1615 if (!available_free_memory(sbi, FREE_NIDS)) {
1616 __del_from_free_nid_list(nm_i, i);
1617 need_free = true;
1618 } else {
1619 i->state = NID_NEW;
1620 nm_i->fcnt++;
1621 }
1622 spin_unlock(&nm_i->free_nid_list_lock);
1623
1624 if (need_free)
1625 kmem_cache_free(free_nid_slab, i);
1626}
1627
1628void recover_inline_xattr(struct inode *inode, struct page *page)
1629{
1554 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1555 void *src_addr, *dst_addr;
1556 size_t inline_size;
1557 struct page *ipage;
1558 struct f2fs_inode *ri;
1559
1630 void *src_addr, *dst_addr;
1631 size_t inline_size;
1632 struct page *ipage;
1633 struct f2fs_inode *ri;
1634
1560 ipage = get_node_page(sbi, inode->i_ino);
1561 f2fs_bug_on(IS_ERR(ipage));
1635 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
1636 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
1562
1563 ri = F2FS_INODE(page);
1564 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1565 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1566 goto update_inode;
1567 }
1568
1569 dst_addr = inline_xattr_addr(ipage);

--- 4 unchanged lines hidden (view full) ---

1574 memcpy(dst_addr, src_addr, inline_size);
1575update_inode:
1576 update_inode(inode, ipage);
1577 f2fs_put_page(ipage, 1);
1578}
1579
1580void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1581{
1637
1638 ri = F2FS_INODE(page);
1639 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1640 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1641 goto update_inode;
1642 }
1643
1644 dst_addr = inline_xattr_addr(ipage);

--- 4 unchanged lines hidden (view full) ---

1649 memcpy(dst_addr, src_addr, inline_size);
1650update_inode:
1651 update_inode(inode, ipage);
1652 f2fs_put_page(ipage, 1);
1653}
1654
1655void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1656{
1582 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1657 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1583 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1584 nid_t new_xnid = nid_of_node(page);
1585 struct node_info ni;
1586
1587 /* 1: invalidate the previous xattr nid */
1588 if (!prev_xnid)
1589 goto recover_xnid;
1590
1591 /* Deallocate node address */
1592 get_node_info(sbi, prev_xnid, &ni);
1658 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1659 nid_t new_xnid = nid_of_node(page);
1660 struct node_info ni;
1661
1662 /* 1: invalidate the previous xattr nid */
1663 if (!prev_xnid)
1664 goto recover_xnid;
1665
1666 /* Deallocate node address */
1667 get_node_info(sbi, prev_xnid, &ni);
1593 f2fs_bug_on(ni.blk_addr == NULL_ADDR);
1668 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
1594 invalidate_blocks(sbi, ni.blk_addr);
1595 dec_valid_node_count(sbi, inode);
1596 set_node_addr(sbi, &ni, NULL_ADDR, false);
1597
1598recover_xnid:
1599 /* 2: allocate new xattr nid */
1600 if (unlikely(!inc_valid_node_count(sbi, inode)))
1669 invalidate_blocks(sbi, ni.blk_addr);
1670 dec_valid_node_count(sbi, inode);
1671 set_node_addr(sbi, &ni, NULL_ADDR, false);
1672
1673recover_xnid:
1674 /* 2: allocate new xattr nid */
1675 if (unlikely(!inc_valid_node_count(sbi, inode)))
1601 f2fs_bug_on(1);
1676 f2fs_bug_on(sbi, 1);
1602
1603 remove_free_nid(NM_I(sbi), new_xnid);
1604 get_node_info(sbi, new_xnid, &ni);
1605 ni.ino = inode->i_ino;
1606 set_node_addr(sbi, &ni, NEW_ADDR, false);
1607 F2FS_I(inode)->i_xattr_nid = new_xnid;
1608
1609 /* 3: update xattr blkaddr */

--- 76 unchanged lines hidden (view full) ---

1686
1687int restore_node_summary(struct f2fs_sb_info *sbi,
1688 unsigned int segno, struct f2fs_summary_block *sum)
1689{
1690 struct f2fs_node *rn;
1691 struct f2fs_summary *sum_entry;
1692 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1693 block_t addr;
1677
1678 remove_free_nid(NM_I(sbi), new_xnid);
1679 get_node_info(sbi, new_xnid, &ni);
1680 ni.ino = inode->i_ino;
1681 set_node_addr(sbi, &ni, NEW_ADDR, false);
1682 F2FS_I(inode)->i_xattr_nid = new_xnid;
1683
1684 /* 3: update xattr blkaddr */

--- 76 unchanged lines hidden (view full) ---

1761
1762int restore_node_summary(struct f2fs_sb_info *sbi,
1763 unsigned int segno, struct f2fs_summary_block *sum)
1764{
1765 struct f2fs_node *rn;
1766 struct f2fs_summary *sum_entry;
1767 struct inode *inode = sbi->sb->s_bdev->bd_inode;
1768 block_t addr;
1694 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1769 int bio_blocks = MAX_BIO_BLOCKS(sbi);
1695 struct page *pages[bio_blocks];
1696 int i, idx, last_offset, nrpages, err = 0;
1697
1698 /* scan the node segment */
1699 last_offset = sbi->blocks_per_seg;
1700 addr = START_BLOCK(sbi, segno);
1701 sum_entry = &sum->entries[0];
1702

--- 25 unchanged lines hidden (view full) ---

1728 }
1729
1730 invalidate_mapping_pages(inode->i_mapping, addr,
1731 addr + nrpages);
1732 }
1733 return err;
1734}
1735
1770 struct page *pages[bio_blocks];
1771 int i, idx, last_offset, nrpages, err = 0;
1772
1773 /* scan the node segment */
1774 last_offset = sbi->blocks_per_seg;
1775 addr = START_BLOCK(sbi, segno);
1776 sum_entry = &sum->entries[0];
1777

--- 25 unchanged lines hidden (view full) ---

1803 }
1804
1805 invalidate_mapping_pages(inode->i_mapping, addr,
1806 addr + nrpages);
1807 }
1808 return err;
1809}
1810
1736static struct nat_entry_set *grab_nat_entry_set(void)
1737{
1738 struct nat_entry_set *nes =
1739 f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
1740
1741 nes->entry_cnt = 0;
1742 INIT_LIST_HEAD(&nes->set_list);
1743 INIT_LIST_HEAD(&nes->entry_list);
1744 return nes;
1745}
1746
1747static void release_nat_entry_set(struct nat_entry_set *nes,
1748 struct f2fs_nm_info *nm_i)
1749{
1750 f2fs_bug_on(!list_empty(&nes->entry_list));
1751
1752 nm_i->dirty_nat_cnt -= nes->entry_cnt;
1753 list_del(&nes->set_list);
1754 kmem_cache_free(nat_entry_set_slab, nes);
1755}
1756
1757static void adjust_nat_entry_set(struct nat_entry_set *nes,
1758 struct list_head *head)
1759{
1760 struct nat_entry_set *next = nes;
1761
1762 if (list_is_last(&nes->set_list, head))
1763 return;
1764
1765 list_for_each_entry_continue(next, head, set_list)
1766 if (nes->entry_cnt <= next->entry_cnt)
1767 break;
1768
1769 list_move_tail(&nes->set_list, &next->set_list);
1770}
1771
1772static void add_nat_entry(struct nat_entry *ne, struct list_head *head)
1773{
1774 struct nat_entry_set *nes;
1775 nid_t start_nid = START_NID(ne->ni.nid);
1776
1777 list_for_each_entry(nes, head, set_list) {
1778 if (nes->start_nid == start_nid) {
1779 list_move_tail(&ne->list, &nes->entry_list);
1780 nes->entry_cnt++;
1781 adjust_nat_entry_set(nes, head);
1782 return;
1783 }
1784 }
1785
1786 nes = grab_nat_entry_set();
1787
1788 nes->start_nid = start_nid;
1789 list_move_tail(&ne->list, &nes->entry_list);
1790 nes->entry_cnt++;
1791 list_add(&nes->set_list, head);
1792}
1793
1794static void merge_nats_in_set(struct f2fs_sb_info *sbi)
1795{
1796 struct f2fs_nm_info *nm_i = NM_I(sbi);
1797 struct list_head *dirty_list = &nm_i->dirty_nat_entries;
1798 struct list_head *set_list = &nm_i->nat_entry_set;
1799 struct nat_entry *ne, *tmp;
1800
1801 write_lock(&nm_i->nat_tree_lock);
1802 list_for_each_entry_safe(ne, tmp, dirty_list, list) {
1803 if (nat_get_blkaddr(ne) == NEW_ADDR)
1804 continue;
1805 add_nat_entry(ne, set_list);
1806 nm_i->dirty_nat_cnt++;
1807 }
1808 write_unlock(&nm_i->nat_tree_lock);
1809}
1810
1811static bool __has_cursum_space(struct f2fs_summary_block *sum, int size)
1812{
1813 if (nats_in_cursum(sum) + size <= NAT_JOURNAL_ENTRIES)
1814 return true;
1815 else
1816 return false;
1817}
1818
1819static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
1820{
1821 struct f2fs_nm_info *nm_i = NM_I(sbi);
1822 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1823 struct f2fs_summary_block *sum = curseg->sum_blk;
1824 int i;
1825
1826 mutex_lock(&curseg->curseg_mutex);

--- 18 unchanged lines hidden (view full) ---

1845found:
1846 __set_nat_cache_dirty(nm_i, ne);
1847 write_unlock(&nm_i->nat_tree_lock);
1848 }
1849 update_nats_in_cursum(sum, -i);
1850 mutex_unlock(&curseg->curseg_mutex);
1851}
1852
1811static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
1812{
1813 struct f2fs_nm_info *nm_i = NM_I(sbi);
1814 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1815 struct f2fs_summary_block *sum = curseg->sum_blk;
1816 int i;
1817
1818 mutex_lock(&curseg->curseg_mutex);

--- 18 unchanged lines hidden (view full) ---

1837found:
1838 __set_nat_cache_dirty(nm_i, ne);
1839 write_unlock(&nm_i->nat_tree_lock);
1840 }
1841 update_nats_in_cursum(sum, -i);
1842 mutex_unlock(&curseg->curseg_mutex);
1843}
1844
1853/*
1854 * This function is called during the checkpointing process.
1855 */
1856void flush_nat_entries(struct f2fs_sb_info *sbi)
1845static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1846 struct list_head *head, int max)
1857{
1847{
1858 struct f2fs_nm_info *nm_i = NM_I(sbi);
1859 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1860 struct f2fs_summary_block *sum = curseg->sum_blk;
1861 struct nat_entry_set *nes, *tmp;
1862 struct list_head *head = &nm_i->nat_entry_set;
1863 bool to_journal = true;
1848 struct nat_entry_set *cur;
1864
1849
1865 /* merge nat entries of dirty list to nat entry set temporarily */
1866 merge_nats_in_set(sbi);
1850 if (nes->entry_cnt >= max)
1851 goto add_out;
1867
1852
1868 /*
1869 * if there are no enough space in journal to store dirty nat
1870 * entries, remove all entries from journal and merge them
1871 * into nat entry set.
1872 */
1873 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt)) {
1874 remove_nats_in_journal(sbi);
1875
1876 /*
1877 * merge nat entries of dirty list to nat entry set temporarily
1878 */
1879 merge_nats_in_set(sbi);
1853 list_for_each_entry(cur, head, set_list) {
1854 if (cur->entry_cnt >= nes->entry_cnt) {
1855 list_add(&nes->set_list, cur->set_list.prev);
1856 return;
1857 }
1880 }
1858 }
1859add_out:
1860 list_add_tail(&nes->set_list, head);
1861}
1881
1862
1882 if (!nm_i->dirty_nat_cnt)
1883 return;
1863static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1864 struct nat_entry_set *set)
1865{
1866 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1867 struct f2fs_summary_block *sum = curseg->sum_blk;
1868 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1869 bool to_journal = true;
1870 struct f2fs_nat_block *nat_blk;
1871 struct nat_entry *ne, *cur;
1872 struct page *page = NULL;
1884
1885 /*
1886 * there are two steps to flush nat entries:
1887 * #1, flush nat entries to journal in current hot data summary block.
1888 * #2, flush nat entries to nat page.
1889 */
1873
1874 /*
1875 * there are two steps to flush nat entries:
1876 * #1, flush nat entries to journal in current hot data summary block.
1877 * #2, flush nat entries to nat page.
1878 */
1890 list_for_each_entry_safe(nes, tmp, head, set_list) {
1891 struct f2fs_nat_block *nat_blk;
1892 struct nat_entry *ne, *cur;
1893 struct page *page;
1894 nid_t start_nid = nes->start_nid;
1879 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1880 to_journal = false;
1895
1881
1896 if (to_journal && !__has_cursum_space(sum, nes->entry_cnt))
1897 to_journal = false;
1882 if (to_journal) {
1883 mutex_lock(&curseg->curseg_mutex);
1884 } else {
1885 page = get_next_nat_page(sbi, start_nid);
1886 nat_blk = page_address(page);
1887 f2fs_bug_on(sbi, !nat_blk);
1888 }
1898
1889
1890 /* flush dirty nats in nat entry set */
1891 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1892 struct f2fs_nat_entry *raw_ne;
1893 nid_t nid = nat_get_nid(ne);
1894 int offset;
1895
1896 if (nat_get_blkaddr(ne) == NEW_ADDR)
1897 continue;
1898
1899 if (to_journal) {
1899 if (to_journal) {
1900 mutex_lock(&curseg->curseg_mutex);
1900 offset = lookup_journal_in_cursum(sum,
1901 NAT_JOURNAL, nid, 1);
1902 f2fs_bug_on(sbi, offset < 0);
1903 raw_ne = &nat_in_journal(sum, offset);
1904 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1901 } else {
1905 } else {
1902 page = get_next_nat_page(sbi, start_nid);
1903 nat_blk = page_address(page);
1904 f2fs_bug_on(!nat_blk);
1906 raw_ne = &nat_blk->entries[nid - start_nid];
1905 }
1907 }
1908 raw_nat_from_node_info(raw_ne, &ne->ni);
1906
1909
1907 /* flush dirty nats in nat entry set */
1908 list_for_each_entry_safe(ne, cur, &nes->entry_list, list) {
1909 struct f2fs_nat_entry *raw_ne;
1910 nid_t nid = nat_get_nid(ne);
1911 int offset;
1910 write_lock(&NM_I(sbi)->nat_tree_lock);
1911 nat_reset_flag(ne);
1912 __clear_nat_cache_dirty(NM_I(sbi), ne);
1913 write_unlock(&NM_I(sbi)->nat_tree_lock);
1912
1914
1913 if (to_journal) {
1914 offset = lookup_journal_in_cursum(sum,
1915 NAT_JOURNAL, nid, 1);
1916 f2fs_bug_on(offset < 0);
1917 raw_ne = &nat_in_journal(sum, offset);
1918 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1919 } else {
1920 raw_ne = &nat_blk->entries[nid - start_nid];
1921 }
1922 raw_nat_from_node_info(raw_ne, &ne->ni);
1915 if (nat_get_blkaddr(ne) == NULL_ADDR)
1916 add_free_nid(sbi, nid, false);
1917 }
1923
1918
1924 if (nat_get_blkaddr(ne) == NULL_ADDR &&
1925 add_free_nid(sbi, nid, false) <= 0) {
1926 write_lock(&nm_i->nat_tree_lock);
1927 __del_from_nat_cache(nm_i, ne);
1928 write_unlock(&nm_i->nat_tree_lock);
1929 } else {
1930 write_lock(&nm_i->nat_tree_lock);
1931 __clear_nat_cache_dirty(nm_i, ne);
1932 write_unlock(&nm_i->nat_tree_lock);
1933 }
1934 }
1919 if (to_journal)
1920 mutex_unlock(&curseg->curseg_mutex);
1921 else
1922 f2fs_put_page(page, 1);
1935
1923
1936 if (to_journal)
1937 mutex_unlock(&curseg->curseg_mutex);
1938 else
1939 f2fs_put_page(page, 1);
1924 if (!set->entry_cnt) {
1925 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1926 kmem_cache_free(nat_entry_set_slab, set);
1927 }
1928}
1940
1929
1941 release_nat_entry_set(nes, nm_i);
1930/*
1931 * This function is called during the checkpointing process.
1932 */
1933void flush_nat_entries(struct f2fs_sb_info *sbi)
1934{
1935 struct f2fs_nm_info *nm_i = NM_I(sbi);
1936 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1937 struct f2fs_summary_block *sum = curseg->sum_blk;
1938 struct nat_entry_set *setvec[NATVEC_SIZE];
1939 struct nat_entry_set *set, *tmp;
1940 unsigned int found;
1941 nid_t set_idx = 0;
1942 LIST_HEAD(sets);
1943
1944 /*
1945 * if there are no enough space in journal to store dirty nat
1946 * entries, remove all entries from journal and merge them
1947 * into nat entry set.
1948 */
1949 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
1950 remove_nats_in_journal(sbi);
1951
1952 if (!nm_i->dirty_nat_cnt)
1953 return;
1954
1955 while ((found = __gang_lookup_nat_set(nm_i,
1956 set_idx, NATVEC_SIZE, setvec))) {
1957 unsigned idx;
1958 set_idx = setvec[found - 1]->set + 1;
1959 for (idx = 0; idx < found; idx++)
1960 __adjust_nat_entry_set(setvec[idx], &sets,
1961 MAX_NAT_JENTRIES(sum));
1942 }
1943
1962 }
1963
1944 f2fs_bug_on(!list_empty(head));
1945 f2fs_bug_on(nm_i->dirty_nat_cnt);
1964 /* flush dirty nats in nat entry set */
1965 list_for_each_entry_safe(set, tmp, &sets, set_list)
1966 __flush_nat_entry_set(sbi, set);
1967
1968 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
1946}
1947
1948static int init_node_manager(struct f2fs_sb_info *sbi)
1949{
1950 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1951 struct f2fs_nm_info *nm_i = NM_I(sbi);
1952 unsigned char *version_bitmap;
1953 unsigned int nat_segs, nat_blocks;

--- 10 unchanged lines hidden (view full) ---

1964 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
1965 nm_i->fcnt = 0;
1966 nm_i->nat_cnt = 0;
1967 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
1968
1969 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
1970 INIT_LIST_HEAD(&nm_i->free_nid_list);
1971 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1969}
1970
1971static int init_node_manager(struct f2fs_sb_info *sbi)
1972{
1973 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1974 struct f2fs_nm_info *nm_i = NM_I(sbi);
1975 unsigned char *version_bitmap;
1976 unsigned int nat_segs, nat_blocks;

--- 10 unchanged lines hidden (view full) ---

1987 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
1988 nm_i->fcnt = 0;
1989 nm_i->nat_cnt = 0;
1990 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
1991
1992 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
1993 INIT_LIST_HEAD(&nm_i->free_nid_list);
1994 INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1995 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_ATOMIC);
1972 INIT_LIST_HEAD(&nm_i->nat_entries);
1996 INIT_LIST_HEAD(&nm_i->nat_entries);
1973 INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1974 INIT_LIST_HEAD(&nm_i->nat_entry_set);
1975
1976 mutex_init(&nm_i->build_lock);
1977 spin_lock_init(&nm_i->free_nid_list_lock);
1978 rwlock_init(&nm_i->nat_tree_lock);
1979
1980 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1981 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1982 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);

--- 32 unchanged lines hidden (view full) ---

2015 unsigned int found;
2016
2017 if (!nm_i)
2018 return;
2019
2020 /* destroy free nid list */
2021 spin_lock(&nm_i->free_nid_list_lock);
2022 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1997
1998 mutex_init(&nm_i->build_lock);
1999 spin_lock_init(&nm_i->free_nid_list_lock);
2000 rwlock_init(&nm_i->nat_tree_lock);
2001
2002 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
2003 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
2004 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);

--- 32 unchanged lines hidden (view full) ---

2037 unsigned int found;
2038
2039 if (!nm_i)
2040 return;
2041
2042 /* destroy free nid list */
2043 spin_lock(&nm_i->free_nid_list_lock);
2044 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
2023 f2fs_bug_on(i->state == NID_ALLOC);
2045 f2fs_bug_on(sbi, i->state == NID_ALLOC);
2024 __del_from_free_nid_list(nm_i, i);
2025 nm_i->fcnt--;
2026 spin_unlock(&nm_i->free_nid_list_lock);
2027 kmem_cache_free(free_nid_slab, i);
2028 spin_lock(&nm_i->free_nid_list_lock);
2029 }
2046 __del_from_free_nid_list(nm_i, i);
2047 nm_i->fcnt--;
2048 spin_unlock(&nm_i->free_nid_list_lock);
2049 kmem_cache_free(free_nid_slab, i);
2050 spin_lock(&nm_i->free_nid_list_lock);
2051 }
2030 f2fs_bug_on(nm_i->fcnt);
2052 f2fs_bug_on(sbi, nm_i->fcnt);
2031 spin_unlock(&nm_i->free_nid_list_lock);
2032
2033 /* destroy nat cache */
2034 write_lock(&nm_i->nat_tree_lock);
2035 while ((found = __gang_lookup_nat_cache(nm_i,
2036 nid, NATVEC_SIZE, natvec))) {
2037 unsigned idx;
2038 nid = nat_get_nid(natvec[found - 1]) + 1;
2039 for (idx = 0; idx < found; idx++)
2040 __del_from_nat_cache(nm_i, natvec[idx]);
2041 }
2053 spin_unlock(&nm_i->free_nid_list_lock);
2054
2055 /* destroy nat cache */
2056 write_lock(&nm_i->nat_tree_lock);
2057 while ((found = __gang_lookup_nat_cache(nm_i,
2058 nid, NATVEC_SIZE, natvec))) {
2059 unsigned idx;
2060 nid = nat_get_nid(natvec[found - 1]) + 1;
2061 for (idx = 0; idx < found; idx++)
2062 __del_from_nat_cache(nm_i, natvec[idx]);
2063 }
2042 f2fs_bug_on(nm_i->nat_cnt);
2064 f2fs_bug_on(sbi, nm_i->nat_cnt);
2043 write_unlock(&nm_i->nat_tree_lock);
2044
2045 kfree(nm_i->nat_bitmap);
2046 sbi->nm_info = NULL;
2047 kfree(nm_i);
2048}
2049
2050int __init create_node_manager_caches(void)

--- 31 unchanged lines hidden ---
2065 write_unlock(&nm_i->nat_tree_lock);
2066
2067 kfree(nm_i->nat_bitmap);
2068 sbi->nm_info = NULL;
2069 kfree(nm_i);
2070}
2071
2072int __init create_node_manager_caches(void)

--- 31 unchanged lines hidden ---