1c21344fba306ef377d3be7f29b9f5ebe8da5901
[firefly-linux-kernel-4.4.55.git] / fs / f2fs / node.c
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
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"
22 #include <trace/events/f2fs.h>
23
24 static struct kmem_cache *nat_entry_slab;
25 static struct kmem_cache *free_nid_slab;
26
27 static void clear_node_page_dirty(struct page *page)
28 {
29         struct address_space *mapping = page->mapping;
30         struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
31         unsigned int long flags;
32
33         if (PageDirty(page)) {
34                 spin_lock_irqsave(&mapping->tree_lock, flags);
35                 radix_tree_tag_clear(&mapping->page_tree,
36                                 page_index(page),
37                                 PAGECACHE_TAG_DIRTY);
38                 spin_unlock_irqrestore(&mapping->tree_lock, flags);
39
40                 clear_page_dirty_for_io(page);
41                 dec_page_count(sbi, F2FS_DIRTY_NODES);
42         }
43         ClearPageUptodate(page);
44 }
45
46 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
47 {
48         pgoff_t index = current_nat_addr(sbi, nid);
49         return get_meta_page(sbi, index);
50 }
51
52 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
53 {
54         struct page *src_page;
55         struct page *dst_page;
56         pgoff_t src_off;
57         pgoff_t dst_off;
58         void *src_addr;
59         void *dst_addr;
60         struct f2fs_nm_info *nm_i = NM_I(sbi);
61
62         src_off = current_nat_addr(sbi, nid);
63         dst_off = next_nat_addr(sbi, src_off);
64
65         /* get current nat block page with lock */
66         src_page = get_meta_page(sbi, src_off);
67
68         /* Dirty src_page means that it is already the new target NAT page. */
69         if (PageDirty(src_page))
70                 return src_page;
71
72         dst_page = grab_meta_page(sbi, dst_off);
73
74         src_addr = page_address(src_page);
75         dst_addr = page_address(dst_page);
76         memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
77         set_page_dirty(dst_page);
78         f2fs_put_page(src_page, 1);
79
80         set_to_next_nat(nm_i, nid);
81
82         return dst_page;
83 }
84
85 /*
86  * Readahead NAT pages
87  */
88 static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid)
89 {
90         struct address_space *mapping = sbi->meta_inode->i_mapping;
91         struct f2fs_nm_info *nm_i = NM_I(sbi);
92         struct blk_plug plug;
93         struct page *page;
94         pgoff_t index;
95         int i;
96
97         blk_start_plug(&plug);
98
99         for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
100                 if (nid >= nm_i->max_nid)
101                         nid = 0;
102                 index = current_nat_addr(sbi, nid);
103
104                 page = grab_cache_page(mapping, index);
105                 if (!page)
106                         continue;
107                 if (PageUptodate(page)) {
108                         f2fs_put_page(page, 1);
109                         continue;
110                 }
111                 if (f2fs_readpage(sbi, page, index, READ))
112                         continue;
113
114                 f2fs_put_page(page, 0);
115         }
116         blk_finish_plug(&plug);
117 }
118
119 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
120 {
121         return radix_tree_lookup(&nm_i->nat_root, n);
122 }
123
124 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
125                 nid_t start, unsigned int nr, struct nat_entry **ep)
126 {
127         return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
128 }
129
130 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
131 {
132         list_del(&e->list);
133         radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
134         nm_i->nat_cnt--;
135         kmem_cache_free(nat_entry_slab, e);
136 }
137
138 int is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
139 {
140         struct f2fs_nm_info *nm_i = NM_I(sbi);
141         struct nat_entry *e;
142         int is_cp = 1;
143
144         read_lock(&nm_i->nat_tree_lock);
145         e = __lookup_nat_cache(nm_i, nid);
146         if (e && !e->checkpointed)
147                 is_cp = 0;
148         read_unlock(&nm_i->nat_tree_lock);
149         return is_cp;
150 }
151
152 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
153 {
154         struct nat_entry *new;
155
156         new = kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
157         if (!new)
158                 return NULL;
159         if (radix_tree_insert(&nm_i->nat_root, nid, new)) {
160                 kmem_cache_free(nat_entry_slab, new);
161                 return NULL;
162         }
163         memset(new, 0, sizeof(struct nat_entry));
164         nat_set_nid(new, nid);
165         list_add_tail(&new->list, &nm_i->nat_entries);
166         nm_i->nat_cnt++;
167         return new;
168 }
169
170 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
171                                                 struct f2fs_nat_entry *ne)
172 {
173         struct nat_entry *e;
174 retry:
175         write_lock(&nm_i->nat_tree_lock);
176         e = __lookup_nat_cache(nm_i, nid);
177         if (!e) {
178                 e = grab_nat_entry(nm_i, nid);
179                 if (!e) {
180                         write_unlock(&nm_i->nat_tree_lock);
181                         goto retry;
182                 }
183                 nat_set_blkaddr(e, le32_to_cpu(ne->block_addr));
184                 nat_set_ino(e, le32_to_cpu(ne->ino));
185                 nat_set_version(e, ne->version);
186                 e->checkpointed = true;
187         }
188         write_unlock(&nm_i->nat_tree_lock);
189 }
190
191 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
192                         block_t new_blkaddr)
193 {
194         struct f2fs_nm_info *nm_i = NM_I(sbi);
195         struct nat_entry *e;
196 retry:
197         write_lock(&nm_i->nat_tree_lock);
198         e = __lookup_nat_cache(nm_i, ni->nid);
199         if (!e) {
200                 e = grab_nat_entry(nm_i, ni->nid);
201                 if (!e) {
202                         write_unlock(&nm_i->nat_tree_lock);
203                         goto retry;
204                 }
205                 e->ni = *ni;
206                 e->checkpointed = true;
207                 BUG_ON(ni->blk_addr == NEW_ADDR);
208         } else if (new_blkaddr == NEW_ADDR) {
209                 /*
210                  * when nid is reallocated,
211                  * previous nat entry can be remained in nat cache.
212                  * So, reinitialize it with new information.
213                  */
214                 e->ni = *ni;
215                 BUG_ON(ni->blk_addr != NULL_ADDR);
216         }
217
218         if (new_blkaddr == NEW_ADDR)
219                 e->checkpointed = false;
220
221         /* sanity check */
222         BUG_ON(nat_get_blkaddr(e) != ni->blk_addr);
223         BUG_ON(nat_get_blkaddr(e) == NULL_ADDR &&
224                         new_blkaddr == NULL_ADDR);
225         BUG_ON(nat_get_blkaddr(e) == NEW_ADDR &&
226                         new_blkaddr == NEW_ADDR);
227         BUG_ON(nat_get_blkaddr(e) != NEW_ADDR &&
228                         nat_get_blkaddr(e) != NULL_ADDR &&
229                         new_blkaddr == NEW_ADDR);
230
231         /* increament version no as node is removed */
232         if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
233                 unsigned char version = nat_get_version(e);
234                 nat_set_version(e, inc_node_version(version));
235         }
236
237         /* change address */
238         nat_set_blkaddr(e, new_blkaddr);
239         __set_nat_cache_dirty(nm_i, e);
240         write_unlock(&nm_i->nat_tree_lock);
241 }
242
243 static int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
244 {
245         struct f2fs_nm_info *nm_i = NM_I(sbi);
246
247         if (nm_i->nat_cnt <= NM_WOUT_THRESHOLD)
248                 return 0;
249
250         write_lock(&nm_i->nat_tree_lock);
251         while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
252                 struct nat_entry *ne;
253                 ne = list_first_entry(&nm_i->nat_entries,
254                                         struct nat_entry, list);
255                 __del_from_nat_cache(nm_i, ne);
256                 nr_shrink--;
257         }
258         write_unlock(&nm_i->nat_tree_lock);
259         return nr_shrink;
260 }
261
262 /*
263  * This function returns always success
264  */
265 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
266 {
267         struct f2fs_nm_info *nm_i = NM_I(sbi);
268         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
269         struct f2fs_summary_block *sum = curseg->sum_blk;
270         nid_t start_nid = START_NID(nid);
271         struct f2fs_nat_block *nat_blk;
272         struct page *page = NULL;
273         struct f2fs_nat_entry ne;
274         struct nat_entry *e;
275         int i;
276
277         memset(&ne, 0, sizeof(struct f2fs_nat_entry));
278         ni->nid = nid;
279
280         /* Check nat cache */
281         read_lock(&nm_i->nat_tree_lock);
282         e = __lookup_nat_cache(nm_i, nid);
283         if (e) {
284                 ni->ino = nat_get_ino(e);
285                 ni->blk_addr = nat_get_blkaddr(e);
286                 ni->version = nat_get_version(e);
287         }
288         read_unlock(&nm_i->nat_tree_lock);
289         if (e)
290                 return;
291
292         /* Check current segment summary */
293         mutex_lock(&curseg->curseg_mutex);
294         i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
295         if (i >= 0) {
296                 ne = nat_in_journal(sum, i);
297                 node_info_from_raw_nat(ni, &ne);
298         }
299         mutex_unlock(&curseg->curseg_mutex);
300         if (i >= 0)
301                 goto cache;
302
303         /* Fill node_info from nat page */
304         page = get_current_nat_page(sbi, start_nid);
305         nat_blk = (struct f2fs_nat_block *)page_address(page);
306         ne = nat_blk->entries[nid - start_nid];
307         node_info_from_raw_nat(ni, &ne);
308         f2fs_put_page(page, 1);
309 cache:
310         /* cache nat entry */
311         cache_nat_entry(NM_I(sbi), nid, &ne);
312 }
313
314 /*
315  * The maximum depth is four.
316  * Offset[0] will have raw inode offset.
317  */
318 static int get_node_path(long block, int offset[4], unsigned int noffset[4])
319 {
320         const long direct_index = ADDRS_PER_INODE;
321         const long direct_blks = ADDRS_PER_BLOCK;
322         const long dptrs_per_blk = NIDS_PER_BLOCK;
323         const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
324         const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
325         int n = 0;
326         int level = 0;
327
328         noffset[0] = 0;
329
330         if (block < direct_index) {
331                 offset[n] = block;
332                 goto got;
333         }
334         block -= direct_index;
335         if (block < direct_blks) {
336                 offset[n++] = NODE_DIR1_BLOCK;
337                 noffset[n] = 1;
338                 offset[n] = block;
339                 level = 1;
340                 goto got;
341         }
342         block -= direct_blks;
343         if (block < direct_blks) {
344                 offset[n++] = NODE_DIR2_BLOCK;
345                 noffset[n] = 2;
346                 offset[n] = block;
347                 level = 1;
348                 goto got;
349         }
350         block -= direct_blks;
351         if (block < indirect_blks) {
352                 offset[n++] = NODE_IND1_BLOCK;
353                 noffset[n] = 3;
354                 offset[n++] = block / direct_blks;
355                 noffset[n] = 4 + offset[n - 1];
356                 offset[n] = block % direct_blks;
357                 level = 2;
358                 goto got;
359         }
360         block -= indirect_blks;
361         if (block < indirect_blks) {
362                 offset[n++] = NODE_IND2_BLOCK;
363                 noffset[n] = 4 + dptrs_per_blk;
364                 offset[n++] = block / direct_blks;
365                 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
366                 offset[n] = block % direct_blks;
367                 level = 2;
368                 goto got;
369         }
370         block -= indirect_blks;
371         if (block < dindirect_blks) {
372                 offset[n++] = NODE_DIND_BLOCK;
373                 noffset[n] = 5 + (dptrs_per_blk * 2);
374                 offset[n++] = block / indirect_blks;
375                 noffset[n] = 6 + (dptrs_per_blk * 2) +
376                               offset[n - 1] * (dptrs_per_blk + 1);
377                 offset[n++] = (block / direct_blks) % dptrs_per_blk;
378                 noffset[n] = 7 + (dptrs_per_blk * 2) +
379                               offset[n - 2] * (dptrs_per_blk + 1) +
380                               offset[n - 1];
381                 offset[n] = block % direct_blks;
382                 level = 3;
383                 goto got;
384         } else {
385                 BUG();
386         }
387 got:
388         return level;
389 }
390
391 /*
392  * Caller should call f2fs_put_dnode(dn).
393  * Also, it should grab and release a mutex by calling mutex_lock_op() and
394  * mutex_unlock_op() only if ro is not set RDONLY_NODE.
395  * In the case of RDONLY_NODE, we don't need to care about mutex.
396  */
397 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
398 {
399         struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
400         struct page *npage[4];
401         struct page *parent;
402         int offset[4];
403         unsigned int noffset[4];
404         nid_t nids[4];
405         int level, i;
406         int err = 0;
407
408         level = get_node_path(index, offset, noffset);
409
410         nids[0] = dn->inode->i_ino;
411         npage[0] = dn->inode_page;
412
413         if (!npage[0]) {
414                 npage[0] = get_node_page(sbi, nids[0]);
415                 if (IS_ERR(npage[0]))
416                         return PTR_ERR(npage[0]);
417         }
418         parent = npage[0];
419         if (level != 0)
420                 nids[1] = get_nid(parent, offset[0], true);
421         dn->inode_page = npage[0];
422         dn->inode_page_locked = true;
423
424         /* get indirect or direct nodes */
425         for (i = 1; i <= level; i++) {
426                 bool done = false;
427
428                 if (!nids[i] && mode == ALLOC_NODE) {
429                         /* alloc new node */
430                         if (!alloc_nid(sbi, &(nids[i]))) {
431                                 err = -ENOSPC;
432                                 goto release_pages;
433                         }
434
435                         dn->nid = nids[i];
436                         npage[i] = new_node_page(dn, noffset[i], NULL);
437                         if (IS_ERR(npage[i])) {
438                                 alloc_nid_failed(sbi, nids[i]);
439                                 err = PTR_ERR(npage[i]);
440                                 goto release_pages;
441                         }
442
443                         set_nid(parent, offset[i - 1], nids[i], i == 1);
444                         alloc_nid_done(sbi, nids[i]);
445                         done = true;
446                 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
447                         npage[i] = get_node_page_ra(parent, offset[i - 1]);
448                         if (IS_ERR(npage[i])) {
449                                 err = PTR_ERR(npage[i]);
450                                 goto release_pages;
451                         }
452                         done = true;
453                 }
454                 if (i == 1) {
455                         dn->inode_page_locked = false;
456                         unlock_page(parent);
457                 } else {
458                         f2fs_put_page(parent, 1);
459                 }
460
461                 if (!done) {
462                         npage[i] = get_node_page(sbi, nids[i]);
463                         if (IS_ERR(npage[i])) {
464                                 err = PTR_ERR(npage[i]);
465                                 f2fs_put_page(npage[0], 0);
466                                 goto release_out;
467                         }
468                 }
469                 if (i < level) {
470                         parent = npage[i];
471                         nids[i + 1] = get_nid(parent, offset[i], false);
472                 }
473         }
474         dn->nid = nids[level];
475         dn->ofs_in_node = offset[level];
476         dn->node_page = npage[level];
477         dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
478         return 0;
479
480 release_pages:
481         f2fs_put_page(parent, 1);
482         if (i > 1)
483                 f2fs_put_page(npage[0], 0);
484 release_out:
485         dn->inode_page = NULL;
486         dn->node_page = NULL;
487         return err;
488 }
489
490 static void truncate_node(struct dnode_of_data *dn)
491 {
492         struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
493         struct node_info ni;
494
495         get_node_info(sbi, dn->nid, &ni);
496         if (dn->inode->i_blocks == 0) {
497                 BUG_ON(ni.blk_addr != NULL_ADDR);
498                 goto invalidate;
499         }
500         BUG_ON(ni.blk_addr == NULL_ADDR);
501
502         /* Deallocate node address */
503         invalidate_blocks(sbi, ni.blk_addr);
504         dec_valid_node_count(sbi, dn->inode, 1);
505         set_node_addr(sbi, &ni, NULL_ADDR);
506
507         if (dn->nid == dn->inode->i_ino) {
508                 remove_orphan_inode(sbi, dn->nid);
509                 dec_valid_inode_count(sbi);
510         } else {
511                 sync_inode_page(dn);
512         }
513 invalidate:
514         clear_node_page_dirty(dn->node_page);
515         F2FS_SET_SB_DIRT(sbi);
516
517         f2fs_put_page(dn->node_page, 1);
518         dn->node_page = NULL;
519         trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
520 }
521
522 static int truncate_dnode(struct dnode_of_data *dn)
523 {
524         struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
525         struct page *page;
526
527         if (dn->nid == 0)
528                 return 1;
529
530         /* get direct node */
531         page = get_node_page(sbi, dn->nid);
532         if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
533                 return 1;
534         else if (IS_ERR(page))
535                 return PTR_ERR(page);
536
537         /* Make dnode_of_data for parameter */
538         dn->node_page = page;
539         dn->ofs_in_node = 0;
540         truncate_data_blocks(dn);
541         truncate_node(dn);
542         return 1;
543 }
544
545 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
546                                                 int ofs, int depth)
547 {
548         struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
549         struct dnode_of_data rdn = *dn;
550         struct page *page;
551         struct f2fs_node *rn;
552         nid_t child_nid;
553         unsigned int child_nofs;
554         int freed = 0;
555         int i, ret;
556
557         if (dn->nid == 0)
558                 return NIDS_PER_BLOCK + 1;
559
560         trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
561
562         page = get_node_page(sbi, dn->nid);
563         if (IS_ERR(page)) {
564                 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
565                 return PTR_ERR(page);
566         }
567
568         rn = F2FS_NODE(page);
569         if (depth < 3) {
570                 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
571                         child_nid = le32_to_cpu(rn->in.nid[i]);
572                         if (child_nid == 0)
573                                 continue;
574                         rdn.nid = child_nid;
575                         ret = truncate_dnode(&rdn);
576                         if (ret < 0)
577                                 goto out_err;
578                         set_nid(page, i, 0, false);
579                 }
580         } else {
581                 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
582                 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
583                         child_nid = le32_to_cpu(rn->in.nid[i]);
584                         if (child_nid == 0) {
585                                 child_nofs += NIDS_PER_BLOCK + 1;
586                                 continue;
587                         }
588                         rdn.nid = child_nid;
589                         ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
590                         if (ret == (NIDS_PER_BLOCK + 1)) {
591                                 set_nid(page, i, 0, false);
592                                 child_nofs += ret;
593                         } else if (ret < 0 && ret != -ENOENT) {
594                                 goto out_err;
595                         }
596                 }
597                 freed = child_nofs;
598         }
599
600         if (!ofs) {
601                 /* remove current indirect node */
602                 dn->node_page = page;
603                 truncate_node(dn);
604                 freed++;
605         } else {
606                 f2fs_put_page(page, 1);
607         }
608         trace_f2fs_truncate_nodes_exit(dn->inode, freed);
609         return freed;
610
611 out_err:
612         f2fs_put_page(page, 1);
613         trace_f2fs_truncate_nodes_exit(dn->inode, ret);
614         return ret;
615 }
616
617 static int truncate_partial_nodes(struct dnode_of_data *dn,
618                         struct f2fs_inode *ri, int *offset, int depth)
619 {
620         struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
621         struct page *pages[2];
622         nid_t nid[3];
623         nid_t child_nid;
624         int err = 0;
625         int i;
626         int idx = depth - 2;
627
628         nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
629         if (!nid[0])
630                 return 0;
631
632         /* get indirect nodes in the path */
633         for (i = 0; i < depth - 1; i++) {
634                 /* refernece count'll be increased */
635                 pages[i] = get_node_page(sbi, nid[i]);
636                 if (IS_ERR(pages[i])) {
637                         depth = i + 1;
638                         err = PTR_ERR(pages[i]);
639                         goto fail;
640                 }
641                 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
642         }
643
644         /* free direct nodes linked to a partial indirect node */
645         for (i = offset[depth - 1]; i < NIDS_PER_BLOCK; i++) {
646                 child_nid = get_nid(pages[idx], i, false);
647                 if (!child_nid)
648                         continue;
649                 dn->nid = child_nid;
650                 err = truncate_dnode(dn);
651                 if (err < 0)
652                         goto fail;
653                 set_nid(pages[idx], i, 0, false);
654         }
655
656         if (offset[depth - 1] == 0) {
657                 dn->node_page = pages[idx];
658                 dn->nid = nid[idx];
659                 truncate_node(dn);
660         } else {
661                 f2fs_put_page(pages[idx], 1);
662         }
663         offset[idx]++;
664         offset[depth - 1] = 0;
665 fail:
666         for (i = depth - 3; i >= 0; i--)
667                 f2fs_put_page(pages[i], 1);
668
669         trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
670
671         return err;
672 }
673
674 /*
675  * All the block addresses of data and nodes should be nullified.
676  */
677 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
678 {
679         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
680         struct address_space *node_mapping = sbi->node_inode->i_mapping;
681         int err = 0, cont = 1;
682         int level, offset[4], noffset[4];
683         unsigned int nofs = 0;
684         struct f2fs_node *rn;
685         struct dnode_of_data dn;
686         struct page *page;
687
688         trace_f2fs_truncate_inode_blocks_enter(inode, from);
689
690         level = get_node_path(from, offset, noffset);
691 restart:
692         page = get_node_page(sbi, inode->i_ino);
693         if (IS_ERR(page)) {
694                 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
695                 return PTR_ERR(page);
696         }
697
698         set_new_dnode(&dn, inode, page, NULL, 0);
699         unlock_page(page);
700
701         rn = F2FS_NODE(page);
702         switch (level) {
703         case 0:
704         case 1:
705                 nofs = noffset[1];
706                 break;
707         case 2:
708                 nofs = noffset[1];
709                 if (!offset[level - 1])
710                         goto skip_partial;
711                 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
712                 if (err < 0 && err != -ENOENT)
713                         goto fail;
714                 nofs += 1 + NIDS_PER_BLOCK;
715                 break;
716         case 3:
717                 nofs = 5 + 2 * NIDS_PER_BLOCK;
718                 if (!offset[level - 1])
719                         goto skip_partial;
720                 err = truncate_partial_nodes(&dn, &rn->i, offset, level);
721                 if (err < 0 && err != -ENOENT)
722                         goto fail;
723                 break;
724         default:
725                 BUG();
726         }
727
728 skip_partial:
729         while (cont) {
730                 dn.nid = le32_to_cpu(rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]);
731                 switch (offset[0]) {
732                 case NODE_DIR1_BLOCK:
733                 case NODE_DIR2_BLOCK:
734                         err = truncate_dnode(&dn);
735                         break;
736
737                 case NODE_IND1_BLOCK:
738                 case NODE_IND2_BLOCK:
739                         err = truncate_nodes(&dn, nofs, offset[1], 2);
740                         break;
741
742                 case NODE_DIND_BLOCK:
743                         err = truncate_nodes(&dn, nofs, offset[1], 3);
744                         cont = 0;
745                         break;
746
747                 default:
748                         BUG();
749                 }
750                 if (err < 0 && err != -ENOENT)
751                         goto fail;
752                 if (offset[1] == 0 &&
753                                 rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK]) {
754                         lock_page(page);
755                         if (page->mapping != node_mapping) {
756                                 f2fs_put_page(page, 1);
757                                 goto restart;
758                         }
759                         wait_on_page_writeback(page);
760                         rn->i.i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
761                         set_page_dirty(page);
762                         unlock_page(page);
763                 }
764                 offset[1] = 0;
765                 offset[0]++;
766                 nofs += err;
767         }
768 fail:
769         f2fs_put_page(page, 0);
770         trace_f2fs_truncate_inode_blocks_exit(inode, err);
771         return err > 0 ? 0 : err;
772 }
773
774 /*
775  * Caller should grab and release a mutex by calling mutex_lock_op() and
776  * mutex_unlock_op().
777  */
778 int remove_inode_page(struct inode *inode)
779 {
780         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
781         struct page *page;
782         nid_t ino = inode->i_ino;
783         struct dnode_of_data dn;
784
785         page = get_node_page(sbi, ino);
786         if (IS_ERR(page))
787                 return PTR_ERR(page);
788
789         if (F2FS_I(inode)->i_xattr_nid) {
790                 nid_t nid = F2FS_I(inode)->i_xattr_nid;
791                 struct page *npage = get_node_page(sbi, nid);
792
793                 if (IS_ERR(npage))
794                         return PTR_ERR(npage);
795
796                 F2FS_I(inode)->i_xattr_nid = 0;
797                 set_new_dnode(&dn, inode, page, npage, nid);
798                 dn.inode_page_locked = 1;
799                 truncate_node(&dn);
800         }
801
802         /* 0 is possible, after f2fs_new_inode() is failed */
803         BUG_ON(inode->i_blocks != 0 && inode->i_blocks != 1);
804         set_new_dnode(&dn, inode, page, page, ino);
805         truncate_node(&dn);
806         return 0;
807 }
808
809 struct page *new_inode_page(struct inode *inode, const struct qstr *name)
810 {
811         struct dnode_of_data dn;
812
813         /* allocate inode page for new inode */
814         set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
815
816         /* caller should f2fs_put_page(page, 1); */
817         return new_node_page(&dn, 0, NULL);
818 }
819
820 struct page *new_node_page(struct dnode_of_data *dn,
821                                 unsigned int ofs, struct page *ipage)
822 {
823         struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
824         struct address_space *mapping = sbi->node_inode->i_mapping;
825         struct node_info old_ni, new_ni;
826         struct page *page;
827         int err;
828
829         if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
830                 return ERR_PTR(-EPERM);
831
832         page = grab_cache_page(mapping, dn->nid);
833         if (!page)
834                 return ERR_PTR(-ENOMEM);
835
836         if (!inc_valid_node_count(sbi, dn->inode, 1)) {
837                 err = -ENOSPC;
838                 goto fail;
839         }
840
841         get_node_info(sbi, dn->nid, &old_ni);
842
843         /* Reinitialize old_ni with new node page */
844         BUG_ON(old_ni.blk_addr != NULL_ADDR);
845         new_ni = old_ni;
846         new_ni.ino = dn->inode->i_ino;
847         set_node_addr(sbi, &new_ni, NEW_ADDR);
848
849         fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
850         set_cold_node(dn->inode, page);
851         SetPageUptodate(page);
852         set_page_dirty(page);
853
854         if (ofs == XATTR_NODE_OFFSET)
855                 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
856
857         dn->node_page = page;
858         if (ipage)
859                 update_inode(dn->inode, ipage);
860         else
861                 sync_inode_page(dn);
862         if (ofs == 0)
863                 inc_valid_inode_count(sbi);
864
865         return page;
866
867 fail:
868         clear_node_page_dirty(page);
869         f2fs_put_page(page, 1);
870         return ERR_PTR(err);
871 }
872
873 /*
874  * Caller should do after getting the following values.
875  * 0: f2fs_put_page(page, 0)
876  * LOCKED_PAGE: f2fs_put_page(page, 1)
877  * error: nothing
878  */
879 static int read_node_page(struct page *page, int type)
880 {
881         struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
882         struct node_info ni;
883
884         get_node_info(sbi, page->index, &ni);
885
886         if (ni.blk_addr == NULL_ADDR) {
887                 f2fs_put_page(page, 1);
888                 return -ENOENT;
889         }
890
891         if (PageUptodate(page))
892                 return LOCKED_PAGE;
893
894         return f2fs_readpage(sbi, page, ni.blk_addr, type);
895 }
896
897 /*
898  * Readahead a node page
899  */
900 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
901 {
902         struct address_space *mapping = sbi->node_inode->i_mapping;
903         struct page *apage;
904         int err;
905
906         apage = find_get_page(mapping, nid);
907         if (apage && PageUptodate(apage)) {
908                 f2fs_put_page(apage, 0);
909                 return;
910         }
911         f2fs_put_page(apage, 0);
912
913         apage = grab_cache_page(mapping, nid);
914         if (!apage)
915                 return;
916
917         err = read_node_page(apage, READA);
918         if (err == 0)
919                 f2fs_put_page(apage, 0);
920         else if (err == LOCKED_PAGE)
921                 f2fs_put_page(apage, 1);
922 }
923
924 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
925 {
926         struct address_space *mapping = sbi->node_inode->i_mapping;
927         struct page *page;
928         int err;
929 repeat:
930         page = grab_cache_page(mapping, nid);
931         if (!page)
932                 return ERR_PTR(-ENOMEM);
933
934         err = read_node_page(page, READ_SYNC);
935         if (err < 0)
936                 return ERR_PTR(err);
937         else if (err == LOCKED_PAGE)
938                 goto got_it;
939
940         lock_page(page);
941         if (!PageUptodate(page)) {
942                 f2fs_put_page(page, 1);
943                 return ERR_PTR(-EIO);
944         }
945         if (page->mapping != mapping) {
946                 f2fs_put_page(page, 1);
947                 goto repeat;
948         }
949 got_it:
950         BUG_ON(nid != nid_of_node(page));
951         mark_page_accessed(page);
952         return page;
953 }
954
955 /*
956  * Return a locked page for the desired node page.
957  * And, readahead MAX_RA_NODE number of node pages.
958  */
959 struct page *get_node_page_ra(struct page *parent, int start)
960 {
961         struct f2fs_sb_info *sbi = F2FS_SB(parent->mapping->host->i_sb);
962         struct address_space *mapping = sbi->node_inode->i_mapping;
963         struct blk_plug plug;
964         struct page *page;
965         int err, i, end;
966         nid_t nid;
967
968         /* First, try getting the desired direct node. */
969         nid = get_nid(parent, start, false);
970         if (!nid)
971                 return ERR_PTR(-ENOENT);
972 repeat:
973         page = grab_cache_page(mapping, nid);
974         if (!page)
975                 return ERR_PTR(-ENOMEM);
976
977         err = read_node_page(page, READ_SYNC);
978         if (err < 0)
979                 return ERR_PTR(err);
980         else if (err == LOCKED_PAGE)
981                 goto page_hit;
982
983         blk_start_plug(&plug);
984
985         /* Then, try readahead for siblings of the desired node */
986         end = start + MAX_RA_NODE;
987         end = min(end, NIDS_PER_BLOCK);
988         for (i = start + 1; i < end; i++) {
989                 nid = get_nid(parent, i, false);
990                 if (!nid)
991                         continue;
992                 ra_node_page(sbi, nid);
993         }
994
995         blk_finish_plug(&plug);
996
997         lock_page(page);
998         if (page->mapping != mapping) {
999                 f2fs_put_page(page, 1);
1000                 goto repeat;
1001         }
1002 page_hit:
1003         if (!PageUptodate(page)) {
1004                 f2fs_put_page(page, 1);
1005                 return ERR_PTR(-EIO);
1006         }
1007         mark_page_accessed(page);
1008         return page;
1009 }
1010
1011 void sync_inode_page(struct dnode_of_data *dn)
1012 {
1013         if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1014                 update_inode(dn->inode, dn->node_page);
1015         } else if (dn->inode_page) {
1016                 if (!dn->inode_page_locked)
1017                         lock_page(dn->inode_page);
1018                 update_inode(dn->inode, dn->inode_page);
1019                 if (!dn->inode_page_locked)
1020                         unlock_page(dn->inode_page);
1021         } else {
1022                 update_inode_page(dn->inode);
1023         }
1024 }
1025
1026 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1027                                         struct writeback_control *wbc)
1028 {
1029         struct address_space *mapping = sbi->node_inode->i_mapping;
1030         pgoff_t index, end;
1031         struct pagevec pvec;
1032         int step = ino ? 2 : 0;
1033         int nwritten = 0, wrote = 0;
1034
1035         pagevec_init(&pvec, 0);
1036
1037 next_step:
1038         index = 0;
1039         end = LONG_MAX;
1040
1041         while (index <= end) {
1042                 int i, nr_pages;
1043                 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
1044                                 PAGECACHE_TAG_DIRTY,
1045                                 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1046                 if (nr_pages == 0)
1047                         break;
1048
1049                 for (i = 0; i < nr_pages; i++) {
1050                         struct page *page = pvec.pages[i];
1051
1052                         /*
1053                          * flushing sequence with step:
1054                          * 0. indirect nodes
1055                          * 1. dentry dnodes
1056                          * 2. file dnodes
1057                          */
1058                         if (step == 0 && IS_DNODE(page))
1059                                 continue;
1060                         if (step == 1 && (!IS_DNODE(page) ||
1061                                                 is_cold_node(page)))
1062                                 continue;
1063                         if (step == 2 && (!IS_DNODE(page) ||
1064                                                 !is_cold_node(page)))
1065                                 continue;
1066
1067                         /*
1068                          * If an fsync mode,
1069                          * we should not skip writing node pages.
1070                          */
1071                         if (ino && ino_of_node(page) == ino)
1072                                 lock_page(page);
1073                         else if (!trylock_page(page))
1074                                 continue;
1075
1076                         if (unlikely(page->mapping != mapping)) {
1077 continue_unlock:
1078                                 unlock_page(page);
1079                                 continue;
1080                         }
1081                         if (ino && ino_of_node(page) != ino)
1082                                 goto continue_unlock;
1083
1084                         if (!PageDirty(page)) {
1085                                 /* someone wrote it for us */
1086                                 goto continue_unlock;
1087                         }
1088
1089                         if (!clear_page_dirty_for_io(page))
1090                                 goto continue_unlock;
1091
1092                         /* called by fsync() */
1093                         if (ino && IS_DNODE(page)) {
1094                                 int mark = !is_checkpointed_node(sbi, ino);
1095                                 set_fsync_mark(page, 1);
1096                                 if (IS_INODE(page))
1097                                         set_dentry_mark(page, mark);
1098                                 nwritten++;
1099                         } else {
1100                                 set_fsync_mark(page, 0);
1101                                 set_dentry_mark(page, 0);
1102                         }
1103                         mapping->a_ops->writepage(page, wbc);
1104                         wrote++;
1105
1106                         if (--wbc->nr_to_write == 0)
1107                                 break;
1108                 }
1109                 pagevec_release(&pvec);
1110                 cond_resched();
1111
1112                 if (wbc->nr_to_write == 0) {
1113                         step = 2;
1114                         break;
1115                 }
1116         }
1117
1118         if (step < 2) {
1119                 step++;
1120                 goto next_step;
1121         }
1122
1123         if (wrote)
1124                 f2fs_submit_bio(sbi, NODE, wbc->sync_mode == WB_SYNC_ALL);
1125
1126         return nwritten;
1127 }
1128
1129 static int f2fs_write_node_page(struct page *page,
1130                                 struct writeback_control *wbc)
1131 {
1132         struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1133         nid_t nid;
1134         block_t new_addr;
1135         struct node_info ni;
1136
1137         wait_on_page_writeback(page);
1138
1139         /* get old block addr of this node page */
1140         nid = nid_of_node(page);
1141         BUG_ON(page->index != nid);
1142
1143         get_node_info(sbi, nid, &ni);
1144
1145         /* This page is already truncated */
1146         if (ni.blk_addr == NULL_ADDR) {
1147                 dec_page_count(sbi, F2FS_DIRTY_NODES);
1148                 unlock_page(page);
1149                 return 0;
1150         }
1151
1152         if (wbc->for_reclaim) {
1153                 dec_page_count(sbi, F2FS_DIRTY_NODES);
1154                 wbc->pages_skipped++;
1155                 set_page_dirty(page);
1156                 return AOP_WRITEPAGE_ACTIVATE;
1157         }
1158
1159         mutex_lock(&sbi->node_write);
1160         set_page_writeback(page);
1161         write_node_page(sbi, page, nid, ni.blk_addr, &new_addr);
1162         set_node_addr(sbi, &ni, new_addr);
1163         dec_page_count(sbi, F2FS_DIRTY_NODES);
1164         mutex_unlock(&sbi->node_write);
1165         unlock_page(page);
1166         return 0;
1167 }
1168
1169 /*
1170  * It is very important to gather dirty pages and write at once, so that we can
1171  * submit a big bio without interfering other data writes.
1172  * Be default, 512 pages (2MB), a segment size, is quite reasonable.
1173  */
1174 #define COLLECT_DIRTY_NODES     512
1175 static int f2fs_write_node_pages(struct address_space *mapping,
1176                             struct writeback_control *wbc)
1177 {
1178         struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1179         long nr_to_write = wbc->nr_to_write;
1180
1181         /* First check balancing cached NAT entries */
1182         if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK)) {
1183                 f2fs_sync_fs(sbi->sb, true);
1184                 return 0;
1185         }
1186
1187         /* collect a number of dirty node pages and write together */
1188         if (get_pages(sbi, F2FS_DIRTY_NODES) < COLLECT_DIRTY_NODES)
1189                 return 0;
1190
1191         /* if mounting is failed, skip writing node pages */
1192         wbc->nr_to_write = max_hw_blocks(sbi);
1193         sync_node_pages(sbi, 0, wbc);
1194         wbc->nr_to_write = nr_to_write - (max_hw_blocks(sbi) - wbc->nr_to_write);
1195         return 0;
1196 }
1197
1198 static int f2fs_set_node_page_dirty(struct page *page)
1199 {
1200         struct address_space *mapping = page->mapping;
1201         struct f2fs_sb_info *sbi = F2FS_SB(mapping->host->i_sb);
1202
1203         SetPageUptodate(page);
1204         if (!PageDirty(page)) {
1205                 __set_page_dirty_nobuffers(page);
1206                 inc_page_count(sbi, F2FS_DIRTY_NODES);
1207                 SetPagePrivate(page);
1208                 return 1;
1209         }
1210         return 0;
1211 }
1212
1213 static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1214                                       unsigned int length)
1215 {
1216         struct inode *inode = page->mapping->host;
1217         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1218         if (PageDirty(page))
1219                 dec_page_count(sbi, F2FS_DIRTY_NODES);
1220         ClearPagePrivate(page);
1221 }
1222
1223 static int f2fs_release_node_page(struct page *page, gfp_t wait)
1224 {
1225         ClearPagePrivate(page);
1226         return 1;
1227 }
1228
1229 /*
1230  * Structure of the f2fs node operations
1231  */
1232 const struct address_space_operations f2fs_node_aops = {
1233         .writepage      = f2fs_write_node_page,
1234         .writepages     = f2fs_write_node_pages,
1235         .set_page_dirty = f2fs_set_node_page_dirty,
1236         .invalidatepage = f2fs_invalidate_node_page,
1237         .releasepage    = f2fs_release_node_page,
1238 };
1239
1240 static struct free_nid *__lookup_free_nid_list(nid_t n, struct list_head *head)
1241 {
1242         struct list_head *this;
1243         struct free_nid *i;
1244         list_for_each(this, head) {
1245                 i = list_entry(this, struct free_nid, list);
1246                 if (i->nid == n)
1247                         return i;
1248         }
1249         return NULL;
1250 }
1251
1252 static void __del_from_free_nid_list(struct free_nid *i)
1253 {
1254         list_del(&i->list);
1255         kmem_cache_free(free_nid_slab, i);
1256 }
1257
1258 static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
1259 {
1260         struct free_nid *i;
1261         struct nat_entry *ne;
1262         bool allocated = false;
1263
1264         if (nm_i->fcnt > 2 * MAX_FREE_NIDS)
1265                 return -1;
1266
1267         /* 0 nid should not be used */
1268         if (nid == 0)
1269                 return 0;
1270
1271         if (!build)
1272                 goto retry;
1273
1274         /* do not add allocated nids */
1275         read_lock(&nm_i->nat_tree_lock);
1276         ne = __lookup_nat_cache(nm_i, nid);
1277         if (ne && nat_get_blkaddr(ne) != NULL_ADDR)
1278                 allocated = true;
1279         read_unlock(&nm_i->nat_tree_lock);
1280         if (allocated)
1281                 return 0;
1282 retry:
1283         i = kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1284         if (!i) {
1285                 cond_resched();
1286                 goto retry;
1287         }
1288         i->nid = nid;
1289         i->state = NID_NEW;
1290
1291         spin_lock(&nm_i->free_nid_list_lock);
1292         if (__lookup_free_nid_list(nid, &nm_i->free_nid_list)) {
1293                 spin_unlock(&nm_i->free_nid_list_lock);
1294                 kmem_cache_free(free_nid_slab, i);
1295                 return 0;
1296         }
1297         list_add_tail(&i->list, &nm_i->free_nid_list);
1298         nm_i->fcnt++;
1299         spin_unlock(&nm_i->free_nid_list_lock);
1300         return 1;
1301 }
1302
1303 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1304 {
1305         struct free_nid *i;
1306         spin_lock(&nm_i->free_nid_list_lock);
1307         i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1308         if (i && i->state == NID_NEW) {
1309                 __del_from_free_nid_list(i);
1310                 nm_i->fcnt--;
1311         }
1312         spin_unlock(&nm_i->free_nid_list_lock);
1313 }
1314
1315 static void scan_nat_page(struct f2fs_nm_info *nm_i,
1316                         struct page *nat_page, nid_t start_nid)
1317 {
1318         struct f2fs_nat_block *nat_blk = page_address(nat_page);
1319         block_t blk_addr;
1320         int i;
1321
1322         i = start_nid % NAT_ENTRY_PER_BLOCK;
1323
1324         for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1325
1326                 if (start_nid >= nm_i->max_nid)
1327                         break;
1328
1329                 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1330                 BUG_ON(blk_addr == NEW_ADDR);
1331                 if (blk_addr == NULL_ADDR) {
1332                         if (add_free_nid(nm_i, start_nid, true) < 0)
1333                                 break;
1334                 }
1335         }
1336 }
1337
1338 static void build_free_nids(struct f2fs_sb_info *sbi)
1339 {
1340         struct f2fs_nm_info *nm_i = NM_I(sbi);
1341         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1342         struct f2fs_summary_block *sum = curseg->sum_blk;
1343         int i = 0;
1344         nid_t nid = nm_i->next_scan_nid;
1345
1346         /* Enough entries */
1347         if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1348                 return;
1349
1350         /* readahead nat pages to be scanned */
1351         ra_nat_pages(sbi, nid);
1352
1353         while (1) {
1354                 struct page *page = get_current_nat_page(sbi, nid);
1355
1356                 scan_nat_page(nm_i, page, nid);
1357                 f2fs_put_page(page, 1);
1358
1359                 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1360                 if (nid >= nm_i->max_nid)
1361                         nid = 0;
1362
1363                 if (i++ == FREE_NID_PAGES)
1364                         break;
1365         }
1366
1367         /* go to the next free nat pages to find free nids abundantly */
1368         nm_i->next_scan_nid = nid;
1369
1370         /* find free nids from current sum_pages */
1371         mutex_lock(&curseg->curseg_mutex);
1372         for (i = 0; i < nats_in_cursum(sum); i++) {
1373                 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1374                 nid = le32_to_cpu(nid_in_journal(sum, i));
1375                 if (addr == NULL_ADDR)
1376                         add_free_nid(nm_i, nid, true);
1377                 else
1378                         remove_free_nid(nm_i, nid);
1379         }
1380         mutex_unlock(&curseg->curseg_mutex);
1381 }
1382
1383 /*
1384  * If this function returns success, caller can obtain a new nid
1385  * from second parameter of this function.
1386  * The returned nid could be used ino as well as nid when inode is created.
1387  */
1388 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1389 {
1390         struct f2fs_nm_info *nm_i = NM_I(sbi);
1391         struct free_nid *i = NULL;
1392         struct list_head *this;
1393 retry:
1394         if (sbi->total_valid_node_count + 1 >= nm_i->max_nid)
1395                 return false;
1396
1397         spin_lock(&nm_i->free_nid_list_lock);
1398
1399         /* We should not use stale free nids created by build_free_nids */
1400         if (nm_i->fcnt && !sbi->on_build_free_nids) {
1401                 BUG_ON(list_empty(&nm_i->free_nid_list));
1402                 list_for_each(this, &nm_i->free_nid_list) {
1403                         i = list_entry(this, struct free_nid, list);
1404                         if (i->state == NID_NEW)
1405                                 break;
1406                 }
1407
1408                 BUG_ON(i->state != NID_NEW);
1409                 *nid = i->nid;
1410                 i->state = NID_ALLOC;
1411                 nm_i->fcnt--;
1412                 spin_unlock(&nm_i->free_nid_list_lock);
1413                 return true;
1414         }
1415         spin_unlock(&nm_i->free_nid_list_lock);
1416
1417         /* Let's scan nat pages and its caches to get free nids */
1418         mutex_lock(&nm_i->build_lock);
1419         sbi->on_build_free_nids = 1;
1420         build_free_nids(sbi);
1421         sbi->on_build_free_nids = 0;
1422         mutex_unlock(&nm_i->build_lock);
1423         goto retry;
1424 }
1425
1426 /*
1427  * alloc_nid() should be called prior to this function.
1428  */
1429 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1430 {
1431         struct f2fs_nm_info *nm_i = NM_I(sbi);
1432         struct free_nid *i;
1433
1434         spin_lock(&nm_i->free_nid_list_lock);
1435         i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1436         BUG_ON(!i || i->state != NID_ALLOC);
1437         __del_from_free_nid_list(i);
1438         spin_unlock(&nm_i->free_nid_list_lock);
1439 }
1440
1441 /*
1442  * alloc_nid() should be called prior to this function.
1443  */
1444 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1445 {
1446         struct f2fs_nm_info *nm_i = NM_I(sbi);
1447         struct free_nid *i;
1448
1449         spin_lock(&nm_i->free_nid_list_lock);
1450         i = __lookup_free_nid_list(nid, &nm_i->free_nid_list);
1451         BUG_ON(!i || i->state != NID_ALLOC);
1452         if (nm_i->fcnt > 2 * MAX_FREE_NIDS) {
1453                 __del_from_free_nid_list(i);
1454         } else {
1455                 i->state = NID_NEW;
1456                 nm_i->fcnt++;
1457         }
1458         spin_unlock(&nm_i->free_nid_list_lock);
1459 }
1460
1461 void recover_node_page(struct f2fs_sb_info *sbi, struct page *page,
1462                 struct f2fs_summary *sum, struct node_info *ni,
1463                 block_t new_blkaddr)
1464 {
1465         rewrite_node_page(sbi, page, sum, ni->blk_addr, new_blkaddr);
1466         set_node_addr(sbi, ni, new_blkaddr);
1467         clear_node_page_dirty(page);
1468 }
1469
1470 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1471 {
1472         struct address_space *mapping = sbi->node_inode->i_mapping;
1473         struct f2fs_node *src, *dst;
1474         nid_t ino = ino_of_node(page);
1475         struct node_info old_ni, new_ni;
1476         struct page *ipage;
1477
1478         ipage = grab_cache_page(mapping, ino);
1479         if (!ipage)
1480                 return -ENOMEM;
1481
1482         /* Should not use this inode  from free nid list */
1483         remove_free_nid(NM_I(sbi), ino);
1484
1485         get_node_info(sbi, ino, &old_ni);
1486         SetPageUptodate(ipage);
1487         fill_node_footer(ipage, ino, ino, 0, true);
1488
1489         src = F2FS_NODE(page);
1490         dst = F2FS_NODE(ipage);
1491
1492         memcpy(dst, src, (unsigned long)&src->i.i_ext - (unsigned long)&src->i);
1493         dst->i.i_size = 0;
1494         dst->i.i_blocks = cpu_to_le64(1);
1495         dst->i.i_links = cpu_to_le32(1);
1496         dst->i.i_xattr_nid = 0;
1497
1498         new_ni = old_ni;
1499         new_ni.ino = ino;
1500
1501         if (!inc_valid_node_count(sbi, NULL, 1))
1502                 WARN_ON(1);
1503         set_node_addr(sbi, &new_ni, NEW_ADDR);
1504         inc_valid_inode_count(sbi);
1505         f2fs_put_page(ipage, 1);
1506         return 0;
1507 }
1508
1509 int restore_node_summary(struct f2fs_sb_info *sbi,
1510                         unsigned int segno, struct f2fs_summary_block *sum)
1511 {
1512         struct f2fs_node *rn;
1513         struct f2fs_summary *sum_entry;
1514         struct page *page;
1515         block_t addr;
1516         int i, last_offset;
1517
1518         /* alloc temporal page for read node */
1519         page = alloc_page(GFP_NOFS | __GFP_ZERO);
1520         if (IS_ERR(page))
1521                 return PTR_ERR(page);
1522         lock_page(page);
1523
1524         /* scan the node segment */
1525         last_offset = sbi->blocks_per_seg;
1526         addr = START_BLOCK(sbi, segno);
1527         sum_entry = &sum->entries[0];
1528
1529         for (i = 0; i < last_offset; i++, sum_entry++) {
1530                 /*
1531                  * In order to read next node page,
1532                  * we must clear PageUptodate flag.
1533                  */
1534                 ClearPageUptodate(page);
1535
1536                 if (f2fs_readpage(sbi, page, addr, READ_SYNC))
1537                         goto out;
1538
1539                 lock_page(page);
1540                 rn = F2FS_NODE(page);
1541                 sum_entry->nid = rn->footer.nid;
1542                 sum_entry->version = 0;
1543                 sum_entry->ofs_in_node = 0;
1544                 addr++;
1545         }
1546         unlock_page(page);
1547 out:
1548         __free_pages(page, 0);
1549         return 0;
1550 }
1551
1552 static bool flush_nats_in_journal(struct f2fs_sb_info *sbi)
1553 {
1554         struct f2fs_nm_info *nm_i = NM_I(sbi);
1555         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1556         struct f2fs_summary_block *sum = curseg->sum_blk;
1557         int i;
1558
1559         mutex_lock(&curseg->curseg_mutex);
1560
1561         if (nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) {
1562                 mutex_unlock(&curseg->curseg_mutex);
1563                 return false;
1564         }
1565
1566         for (i = 0; i < nats_in_cursum(sum); i++) {
1567                 struct nat_entry *ne;
1568                 struct f2fs_nat_entry raw_ne;
1569                 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1570
1571                 raw_ne = nat_in_journal(sum, i);
1572 retry:
1573                 write_lock(&nm_i->nat_tree_lock);
1574                 ne = __lookup_nat_cache(nm_i, nid);
1575                 if (ne) {
1576                         __set_nat_cache_dirty(nm_i, ne);
1577                         write_unlock(&nm_i->nat_tree_lock);
1578                         continue;
1579                 }
1580                 ne = grab_nat_entry(nm_i, nid);
1581                 if (!ne) {
1582                         write_unlock(&nm_i->nat_tree_lock);
1583                         goto retry;
1584                 }
1585                 nat_set_blkaddr(ne, le32_to_cpu(raw_ne.block_addr));
1586                 nat_set_ino(ne, le32_to_cpu(raw_ne.ino));
1587                 nat_set_version(ne, raw_ne.version);
1588                 __set_nat_cache_dirty(nm_i, ne);
1589                 write_unlock(&nm_i->nat_tree_lock);
1590         }
1591         update_nats_in_cursum(sum, -i);
1592         mutex_unlock(&curseg->curseg_mutex);
1593         return true;
1594 }
1595
1596 /*
1597  * This function is called during the checkpointing process.
1598  */
1599 void flush_nat_entries(struct f2fs_sb_info *sbi)
1600 {
1601         struct f2fs_nm_info *nm_i = NM_I(sbi);
1602         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1603         struct f2fs_summary_block *sum = curseg->sum_blk;
1604         struct list_head *cur, *n;
1605         struct page *page = NULL;
1606         struct f2fs_nat_block *nat_blk = NULL;
1607         nid_t start_nid = 0, end_nid = 0;
1608         bool flushed;
1609
1610         flushed = flush_nats_in_journal(sbi);
1611
1612         if (!flushed)
1613                 mutex_lock(&curseg->curseg_mutex);
1614
1615         /* 1) flush dirty nat caches */
1616         list_for_each_safe(cur, n, &nm_i->dirty_nat_entries) {
1617                 struct nat_entry *ne;
1618                 nid_t nid;
1619                 struct f2fs_nat_entry raw_ne;
1620                 int offset = -1;
1621                 block_t new_blkaddr;
1622
1623                 ne = list_entry(cur, struct nat_entry, list);
1624                 nid = nat_get_nid(ne);
1625
1626                 if (nat_get_blkaddr(ne) == NEW_ADDR)
1627                         continue;
1628                 if (flushed)
1629                         goto to_nat_page;
1630
1631                 /* if there is room for nat enries in curseg->sumpage */
1632                 offset = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 1);
1633                 if (offset >= 0) {
1634                         raw_ne = nat_in_journal(sum, offset);
1635                         goto flush_now;
1636                 }
1637 to_nat_page:
1638                 if (!page || (start_nid > nid || nid > end_nid)) {
1639                         if (page) {
1640                                 f2fs_put_page(page, 1);
1641                                 page = NULL;
1642                         }
1643                         start_nid = START_NID(nid);
1644                         end_nid = start_nid + NAT_ENTRY_PER_BLOCK - 1;
1645
1646                         /*
1647                          * get nat block with dirty flag, increased reference
1648                          * count, mapped and lock
1649                          */
1650                         page = get_next_nat_page(sbi, start_nid);
1651                         nat_blk = page_address(page);
1652                 }
1653
1654                 BUG_ON(!nat_blk);
1655                 raw_ne = nat_blk->entries[nid - start_nid];
1656 flush_now:
1657                 new_blkaddr = nat_get_blkaddr(ne);
1658
1659                 raw_ne.ino = cpu_to_le32(nat_get_ino(ne));
1660                 raw_ne.block_addr = cpu_to_le32(new_blkaddr);
1661                 raw_ne.version = nat_get_version(ne);
1662
1663                 if (offset < 0) {
1664                         nat_blk->entries[nid - start_nid] = raw_ne;
1665                 } else {
1666                         nat_in_journal(sum, offset) = raw_ne;
1667                         nid_in_journal(sum, offset) = cpu_to_le32(nid);
1668                 }
1669
1670                 if (nat_get_blkaddr(ne) == NULL_ADDR &&
1671                                 add_free_nid(NM_I(sbi), nid, false) <= 0) {
1672                         write_lock(&nm_i->nat_tree_lock);
1673                         __del_from_nat_cache(nm_i, ne);
1674                         write_unlock(&nm_i->nat_tree_lock);
1675                 } else {
1676                         write_lock(&nm_i->nat_tree_lock);
1677                         __clear_nat_cache_dirty(nm_i, ne);
1678                         ne->checkpointed = true;
1679                         write_unlock(&nm_i->nat_tree_lock);
1680                 }
1681         }
1682         if (!flushed)
1683                 mutex_unlock(&curseg->curseg_mutex);
1684         f2fs_put_page(page, 1);
1685
1686         /* 2) shrink nat caches if necessary */
1687         try_to_free_nats(sbi, nm_i->nat_cnt - NM_WOUT_THRESHOLD);
1688 }
1689
1690 static int init_node_manager(struct f2fs_sb_info *sbi)
1691 {
1692         struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1693         struct f2fs_nm_info *nm_i = NM_I(sbi);
1694         unsigned char *version_bitmap;
1695         unsigned int nat_segs, nat_blocks;
1696
1697         nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1698
1699         /* segment_count_nat includes pair segment so divide to 2. */
1700         nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1701         nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1702         nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1703         nm_i->fcnt = 0;
1704         nm_i->nat_cnt = 0;
1705
1706         INIT_LIST_HEAD(&nm_i->free_nid_list);
1707         INIT_RADIX_TREE(&nm_i->nat_root, GFP_ATOMIC);
1708         INIT_LIST_HEAD(&nm_i->nat_entries);
1709         INIT_LIST_HEAD(&nm_i->dirty_nat_entries);
1710
1711         mutex_init(&nm_i->build_lock);
1712         spin_lock_init(&nm_i->free_nid_list_lock);
1713         rwlock_init(&nm_i->nat_tree_lock);
1714
1715         nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
1716         nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
1717         version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1718         if (!version_bitmap)
1719                 return -EFAULT;
1720
1721         nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1722                                         GFP_KERNEL);
1723         if (!nm_i->nat_bitmap)
1724                 return -ENOMEM;
1725         return 0;
1726 }
1727
1728 int build_node_manager(struct f2fs_sb_info *sbi)
1729 {
1730         int err;
1731
1732         sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1733         if (!sbi->nm_info)
1734                 return -ENOMEM;
1735
1736         err = init_node_manager(sbi);
1737         if (err)
1738                 return err;
1739
1740         build_free_nids(sbi);
1741         return 0;
1742 }
1743
1744 void destroy_node_manager(struct f2fs_sb_info *sbi)
1745 {
1746         struct f2fs_nm_info *nm_i = NM_I(sbi);
1747         struct free_nid *i, *next_i;
1748         struct nat_entry *natvec[NATVEC_SIZE];
1749         nid_t nid = 0;
1750         unsigned int found;
1751
1752         if (!nm_i)
1753                 return;
1754
1755         /* destroy free nid list */
1756         spin_lock(&nm_i->free_nid_list_lock);
1757         list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
1758                 BUG_ON(i->state == NID_ALLOC);
1759                 __del_from_free_nid_list(i);
1760                 nm_i->fcnt--;
1761         }
1762         BUG_ON(nm_i->fcnt);
1763         spin_unlock(&nm_i->free_nid_list_lock);
1764
1765         /* destroy nat cache */
1766         write_lock(&nm_i->nat_tree_lock);
1767         while ((found = __gang_lookup_nat_cache(nm_i,
1768                                         nid, NATVEC_SIZE, natvec))) {
1769                 unsigned idx;
1770                 for (idx = 0; idx < found; idx++) {
1771                         struct nat_entry *e = natvec[idx];
1772                         nid = nat_get_nid(e) + 1;
1773                         __del_from_nat_cache(nm_i, e);
1774                 }
1775         }
1776         BUG_ON(nm_i->nat_cnt);
1777         write_unlock(&nm_i->nat_tree_lock);
1778
1779         kfree(nm_i->nat_bitmap);
1780         sbi->nm_info = NULL;
1781         kfree(nm_i);
1782 }
1783
1784 int __init create_node_manager_caches(void)
1785 {
1786         nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
1787                         sizeof(struct nat_entry), NULL);
1788         if (!nat_entry_slab)
1789                 return -ENOMEM;
1790
1791         free_nid_slab = f2fs_kmem_cache_create("free_nid",
1792                         sizeof(struct free_nid), NULL);
1793         if (!free_nid_slab) {
1794                 kmem_cache_destroy(nat_entry_slab);
1795                 return -ENOMEM;
1796         }
1797         return 0;
1798 }
1799
1800 void destroy_node_manager_caches(void)
1801 {
1802         kmem_cache_destroy(free_nid_slab);
1803         kmem_cache_destroy(nat_entry_slab);
1804 }