f2fs: reorganize code for ra_node_page
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / f2fs / segment.c
1 /*
2  * fs/f2fs/segment.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/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/prefetch.h>
16 #include <linux/vmalloc.h>
17
18 #include "f2fs.h"
19 #include "segment.h"
20 #include "node.h"
21
22 /*
23  * This function balances dirty node and dentry pages.
24  * In addition, it controls garbage collection.
25  */
26 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
27 {
28         /*
29          * We should do GC or end up with checkpoint, if there are so many dirty
30          * dir/node pages without enough free segments.
31          */
32         if (has_not_enough_free_secs(sbi)) {
33                 mutex_lock(&sbi->gc_mutex);
34                 f2fs_gc(sbi);
35         }
36 }
37
38 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
39                 enum dirty_type dirty_type)
40 {
41         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
42
43         /* need not be added */
44         if (IS_CURSEG(sbi, segno))
45                 return;
46
47         if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
48                 dirty_i->nr_dirty[dirty_type]++;
49
50         if (dirty_type == DIRTY) {
51                 struct seg_entry *sentry = get_seg_entry(sbi, segno);
52                 dirty_type = sentry->type;
53                 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
54                         dirty_i->nr_dirty[dirty_type]++;
55         }
56 }
57
58 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
59                 enum dirty_type dirty_type)
60 {
61         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
62
63         if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
64                 dirty_i->nr_dirty[dirty_type]--;
65
66         if (dirty_type == DIRTY) {
67                 struct seg_entry *sentry = get_seg_entry(sbi, segno);
68                 dirty_type = sentry->type;
69                 if (test_and_clear_bit(segno,
70                                         dirty_i->dirty_segmap[dirty_type]))
71                         dirty_i->nr_dirty[dirty_type]--;
72                 clear_bit(segno, dirty_i->victim_segmap[FG_GC]);
73                 clear_bit(segno, dirty_i->victim_segmap[BG_GC]);
74         }
75 }
76
77 /*
78  * Should not occur error such as -ENOMEM.
79  * Adding dirty entry into seglist is not critical operation.
80  * If a given segment is one of current working segments, it won't be added.
81  */
82 void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
83 {
84         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
85         unsigned short valid_blocks;
86
87         if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
88                 return;
89
90         mutex_lock(&dirty_i->seglist_lock);
91
92         valid_blocks = get_valid_blocks(sbi, segno, 0);
93
94         if (valid_blocks == 0) {
95                 __locate_dirty_segment(sbi, segno, PRE);
96                 __remove_dirty_segment(sbi, segno, DIRTY);
97         } else if (valid_blocks < sbi->blocks_per_seg) {
98                 __locate_dirty_segment(sbi, segno, DIRTY);
99         } else {
100                 /* Recovery routine with SSR needs this */
101                 __remove_dirty_segment(sbi, segno, DIRTY);
102         }
103
104         mutex_unlock(&dirty_i->seglist_lock);
105         return;
106 }
107
108 /*
109  * Should call clear_prefree_segments after checkpoint is done.
110  */
111 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
112 {
113         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
114         unsigned int segno, offset = 0;
115         unsigned int total_segs = TOTAL_SEGS(sbi);
116
117         mutex_lock(&dirty_i->seglist_lock);
118         while (1) {
119                 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
120                                 offset);
121                 if (segno >= total_segs)
122                         break;
123                 __set_test_and_free(sbi, segno);
124                 offset = segno + 1;
125         }
126         mutex_unlock(&dirty_i->seglist_lock);
127 }
128
129 void clear_prefree_segments(struct f2fs_sb_info *sbi)
130 {
131         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
132         unsigned int segno, offset = 0;
133         unsigned int total_segs = TOTAL_SEGS(sbi);
134
135         mutex_lock(&dirty_i->seglist_lock);
136         while (1) {
137                 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
138                                 offset);
139                 if (segno >= total_segs)
140                         break;
141
142                 offset = segno + 1;
143                 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
144                         dirty_i->nr_dirty[PRE]--;
145
146                 /* Let's use trim */
147                 if (test_opt(sbi, DISCARD))
148                         blkdev_issue_discard(sbi->sb->s_bdev,
149                                         START_BLOCK(sbi, segno) <<
150                                         sbi->log_sectors_per_block,
151                                         1 << (sbi->log_sectors_per_block +
152                                                 sbi->log_blocks_per_seg),
153                                         GFP_NOFS, 0);
154         }
155         mutex_unlock(&dirty_i->seglist_lock);
156 }
157
158 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
159 {
160         struct sit_info *sit_i = SIT_I(sbi);
161         if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
162                 sit_i->dirty_sentries++;
163 }
164
165 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
166                                         unsigned int segno, int modified)
167 {
168         struct seg_entry *se = get_seg_entry(sbi, segno);
169         se->type = type;
170         if (modified)
171                 __mark_sit_entry_dirty(sbi, segno);
172 }
173
174 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
175 {
176         struct seg_entry *se;
177         unsigned int segno, offset;
178         long int new_vblocks;
179
180         segno = GET_SEGNO(sbi, blkaddr);
181
182         se = get_seg_entry(sbi, segno);
183         new_vblocks = se->valid_blocks + del;
184         offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
185
186         BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
187                                 (new_vblocks > sbi->blocks_per_seg)));
188
189         se->valid_blocks = new_vblocks;
190         se->mtime = get_mtime(sbi);
191         SIT_I(sbi)->max_mtime = se->mtime;
192
193         /* Update valid block bitmap */
194         if (del > 0) {
195                 if (f2fs_set_bit(offset, se->cur_valid_map))
196                         BUG();
197         } else {
198                 if (!f2fs_clear_bit(offset, se->cur_valid_map))
199                         BUG();
200         }
201         if (!f2fs_test_bit(offset, se->ckpt_valid_map))
202                 se->ckpt_valid_blocks += del;
203
204         __mark_sit_entry_dirty(sbi, segno);
205
206         /* update total number of valid blocks to be written in ckpt area */
207         SIT_I(sbi)->written_valid_blocks += del;
208
209         if (sbi->segs_per_sec > 1)
210                 get_sec_entry(sbi, segno)->valid_blocks += del;
211 }
212
213 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
214                         block_t old_blkaddr, block_t new_blkaddr)
215 {
216         update_sit_entry(sbi, new_blkaddr, 1);
217         if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
218                 update_sit_entry(sbi, old_blkaddr, -1);
219 }
220
221 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
222 {
223         unsigned int segno = GET_SEGNO(sbi, addr);
224         struct sit_info *sit_i = SIT_I(sbi);
225
226         BUG_ON(addr == NULL_ADDR);
227         if (addr == NEW_ADDR)
228                 return;
229
230         /* add it into sit main buffer */
231         mutex_lock(&sit_i->sentry_lock);
232
233         update_sit_entry(sbi, addr, -1);
234
235         /* add it into dirty seglist */
236         locate_dirty_segment(sbi, segno);
237
238         mutex_unlock(&sit_i->sentry_lock);
239 }
240
241 /*
242  * This function should be resided under the curseg_mutex lock
243  */
244 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
245                 struct f2fs_summary *sum, unsigned short offset)
246 {
247         struct curseg_info *curseg = CURSEG_I(sbi, type);
248         void *addr = curseg->sum_blk;
249         addr += offset * sizeof(struct f2fs_summary);
250         memcpy(addr, sum, sizeof(struct f2fs_summary));
251         return;
252 }
253
254 /*
255  * Calculate the number of current summary pages for writing
256  */
257 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
258 {
259         int total_size_bytes = 0;
260         int valid_sum_count = 0;
261         int i, sum_space;
262
263         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
264                 if (sbi->ckpt->alloc_type[i] == SSR)
265                         valid_sum_count += sbi->blocks_per_seg;
266                 else
267                         valid_sum_count += curseg_blkoff(sbi, i);
268         }
269
270         total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
271                         + sizeof(struct nat_journal) + 2
272                         + sizeof(struct sit_journal) + 2;
273         sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
274         if (total_size_bytes < sum_space)
275                 return 1;
276         else if (total_size_bytes < 2 * sum_space)
277                 return 2;
278         return 3;
279 }
280
281 /*
282  * Caller should put this summary page
283  */
284 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
285 {
286         return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
287 }
288
289 static void write_sum_page(struct f2fs_sb_info *sbi,
290                         struct f2fs_summary_block *sum_blk, block_t blk_addr)
291 {
292         struct page *page = grab_meta_page(sbi, blk_addr);
293         void *kaddr = page_address(page);
294         memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
295         set_page_dirty(page);
296         f2fs_put_page(page, 1);
297 }
298
299 static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi,
300                                         int ofs_unit, int type)
301 {
302         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
303         unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
304         unsigned int segno, next_segno, i;
305         int ofs = 0;
306
307         /*
308          * If there is not enough reserved sections,
309          * we should not reuse prefree segments.
310          */
311         if (has_not_enough_free_secs(sbi))
312                 return NULL_SEGNO;
313
314         /*
315          * NODE page should not reuse prefree segment,
316          * since those information is used for SPOR.
317          */
318         if (IS_NODESEG(type))
319                 return NULL_SEGNO;
320 next:
321         segno = find_next_bit(prefree_segmap, TOTAL_SEGS(sbi), ofs++);
322         ofs = ((segno / ofs_unit) * ofs_unit) + ofs_unit;
323         if (segno < TOTAL_SEGS(sbi)) {
324                 /* skip intermediate segments in a section */
325                 if (segno % ofs_unit)
326                         goto next;
327
328                 /* skip if whole section is not prefree */
329                 next_segno = find_next_zero_bit(prefree_segmap,
330                                                 TOTAL_SEGS(sbi), segno + 1);
331                 if (next_segno - segno < ofs_unit)
332                         goto next;
333
334                 /* skip if whole section was not free at the last checkpoint */
335                 for (i = 0; i < ofs_unit; i++)
336                         if (get_seg_entry(sbi, segno)->ckpt_valid_blocks)
337                                 goto next;
338                 return segno;
339         }
340         return NULL_SEGNO;
341 }
342
343 /*
344  * Find a new segment from the free segments bitmap to right order
345  * This function should be returned with success, otherwise BUG
346  */
347 static void get_new_segment(struct f2fs_sb_info *sbi,
348                         unsigned int *newseg, bool new_sec, int dir)
349 {
350         struct free_segmap_info *free_i = FREE_I(sbi);
351         unsigned int total_secs = sbi->total_sections;
352         unsigned int segno, secno, zoneno;
353         unsigned int total_zones = sbi->total_sections / sbi->secs_per_zone;
354         unsigned int hint = *newseg / sbi->segs_per_sec;
355         unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
356         unsigned int left_start = hint;
357         bool init = true;
358         int go_left = 0;
359         int i;
360
361         write_lock(&free_i->segmap_lock);
362
363         if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
364                 segno = find_next_zero_bit(free_i->free_segmap,
365                                         TOTAL_SEGS(sbi), *newseg + 1);
366                 if (segno < TOTAL_SEGS(sbi))
367                         goto got_it;
368         }
369 find_other_zone:
370         secno = find_next_zero_bit(free_i->free_secmap, total_secs, hint);
371         if (secno >= total_secs) {
372                 if (dir == ALLOC_RIGHT) {
373                         secno = find_next_zero_bit(free_i->free_secmap,
374                                                 total_secs, 0);
375                         BUG_ON(secno >= total_secs);
376                 } else {
377                         go_left = 1;
378                         left_start = hint - 1;
379                 }
380         }
381         if (go_left == 0)
382                 goto skip_left;
383
384         while (test_bit(left_start, free_i->free_secmap)) {
385                 if (left_start > 0) {
386                         left_start--;
387                         continue;
388                 }
389                 left_start = find_next_zero_bit(free_i->free_secmap,
390                                                 total_secs, 0);
391                 BUG_ON(left_start >= total_secs);
392                 break;
393         }
394         secno = left_start;
395 skip_left:
396         hint = secno;
397         segno = secno * sbi->segs_per_sec;
398         zoneno = secno / sbi->secs_per_zone;
399
400         /* give up on finding another zone */
401         if (!init)
402                 goto got_it;
403         if (sbi->secs_per_zone == 1)
404                 goto got_it;
405         if (zoneno == old_zoneno)
406                 goto got_it;
407         if (dir == ALLOC_LEFT) {
408                 if (!go_left && zoneno + 1 >= total_zones)
409                         goto got_it;
410                 if (go_left && zoneno == 0)
411                         goto got_it;
412         }
413         for (i = 0; i < NR_CURSEG_TYPE; i++)
414                 if (CURSEG_I(sbi, i)->zone == zoneno)
415                         break;
416
417         if (i < NR_CURSEG_TYPE) {
418                 /* zone is in user, try another */
419                 if (go_left)
420                         hint = zoneno * sbi->secs_per_zone - 1;
421                 else if (zoneno + 1 >= total_zones)
422                         hint = 0;
423                 else
424                         hint = (zoneno + 1) * sbi->secs_per_zone;
425                 init = false;
426                 goto find_other_zone;
427         }
428 got_it:
429         /* set it as dirty segment in free segmap */
430         BUG_ON(test_bit(segno, free_i->free_segmap));
431         __set_inuse(sbi, segno);
432         *newseg = segno;
433         write_unlock(&free_i->segmap_lock);
434 }
435
436 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
437 {
438         struct curseg_info *curseg = CURSEG_I(sbi, type);
439         struct summary_footer *sum_footer;
440
441         curseg->segno = curseg->next_segno;
442         curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
443         curseg->next_blkoff = 0;
444         curseg->next_segno = NULL_SEGNO;
445
446         sum_footer = &(curseg->sum_blk->footer);
447         memset(sum_footer, 0, sizeof(struct summary_footer));
448         if (IS_DATASEG(type))
449                 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
450         if (IS_NODESEG(type))
451                 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
452         __set_sit_entry_type(sbi, type, curseg->segno, modified);
453 }
454
455 /*
456  * Allocate a current working segment.
457  * This function always allocates a free segment in LFS manner.
458  */
459 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
460 {
461         struct curseg_info *curseg = CURSEG_I(sbi, type);
462         unsigned int segno = curseg->segno;
463         int dir = ALLOC_LEFT;
464
465         write_sum_page(sbi, curseg->sum_blk,
466                                 GET_SUM_BLOCK(sbi, curseg->segno));
467         if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
468                 dir = ALLOC_RIGHT;
469
470         if (test_opt(sbi, NOHEAP))
471                 dir = ALLOC_RIGHT;
472
473         get_new_segment(sbi, &segno, new_sec, dir);
474         curseg->next_segno = segno;
475         reset_curseg(sbi, type, 1);
476         curseg->alloc_type = LFS;
477 }
478
479 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
480                         struct curseg_info *seg, block_t start)
481 {
482         struct seg_entry *se = get_seg_entry(sbi, seg->segno);
483         block_t ofs;
484         for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
485                 if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
486                         && !f2fs_test_bit(ofs, se->cur_valid_map))
487                         break;
488         }
489         seg->next_blkoff = ofs;
490 }
491
492 /*
493  * If a segment is written by LFS manner, next block offset is just obtained
494  * by increasing the current block offset. However, if a segment is written by
495  * SSR manner, next block offset obtained by calling __next_free_blkoff
496  */
497 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
498                                 struct curseg_info *seg)
499 {
500         if (seg->alloc_type == SSR)
501                 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
502         else
503                 seg->next_blkoff++;
504 }
505
506 /*
507  * This function always allocates a used segment (from dirty seglist) by SSR
508  * manner, so it should recover the existing segment information of valid blocks
509  */
510 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
511 {
512         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
513         struct curseg_info *curseg = CURSEG_I(sbi, type);
514         unsigned int new_segno = curseg->next_segno;
515         struct f2fs_summary_block *sum_node;
516         struct page *sum_page;
517
518         write_sum_page(sbi, curseg->sum_blk,
519                                 GET_SUM_BLOCK(sbi, curseg->segno));
520         __set_test_and_inuse(sbi, new_segno);
521
522         mutex_lock(&dirty_i->seglist_lock);
523         __remove_dirty_segment(sbi, new_segno, PRE);
524         __remove_dirty_segment(sbi, new_segno, DIRTY);
525         mutex_unlock(&dirty_i->seglist_lock);
526
527         reset_curseg(sbi, type, 1);
528         curseg->alloc_type = SSR;
529         __next_free_blkoff(sbi, curseg, 0);
530
531         if (reuse) {
532                 sum_page = get_sum_page(sbi, new_segno);
533                 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
534                 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
535                 f2fs_put_page(sum_page, 1);
536         }
537 }
538
539 /*
540  * flush out current segment and replace it with new segment
541  * This function should be returned with success, otherwise BUG
542  */
543 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
544                                                 int type, bool force)
545 {
546         struct curseg_info *curseg = CURSEG_I(sbi, type);
547         unsigned int ofs_unit;
548
549         if (force) {
550                 new_curseg(sbi, type, true);
551                 goto out;
552         }
553
554         ofs_unit = need_SSR(sbi) ? 1 : sbi->segs_per_sec;
555         curseg->next_segno = check_prefree_segments(sbi, ofs_unit, type);
556
557         if (curseg->next_segno != NULL_SEGNO)
558                 change_curseg(sbi, type, false);
559         else if (type == CURSEG_WARM_NODE)
560                 new_curseg(sbi, type, false);
561         else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
562                 change_curseg(sbi, type, true);
563         else
564                 new_curseg(sbi, type, false);
565 out:
566         sbi->segment_count[curseg->alloc_type]++;
567 }
568
569 void allocate_new_segments(struct f2fs_sb_info *sbi)
570 {
571         struct curseg_info *curseg;
572         unsigned int old_curseg;
573         int i;
574
575         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
576                 curseg = CURSEG_I(sbi, i);
577                 old_curseg = curseg->segno;
578                 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
579                 locate_dirty_segment(sbi, old_curseg);
580         }
581 }
582
583 static const struct segment_allocation default_salloc_ops = {
584         .allocate_segment = allocate_segment_by_default,
585 };
586
587 static void f2fs_end_io_write(struct bio *bio, int err)
588 {
589         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
590         struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
591         struct bio_private *p = bio->bi_private;
592
593         do {
594                 struct page *page = bvec->bv_page;
595
596                 if (--bvec >= bio->bi_io_vec)
597                         prefetchw(&bvec->bv_page->flags);
598                 if (!uptodate) {
599                         SetPageError(page);
600                         if (page->mapping)
601                                 set_bit(AS_EIO, &page->mapping->flags);
602                         set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
603                         p->sbi->sb->s_flags |= MS_RDONLY;
604                 }
605                 end_page_writeback(page);
606                 dec_page_count(p->sbi, F2FS_WRITEBACK);
607         } while (bvec >= bio->bi_io_vec);
608
609         if (p->is_sync)
610                 complete(p->wait);
611         kfree(p);
612         bio_put(bio);
613 }
614
615 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
616 {
617         struct bio *bio;
618         struct bio_private *priv;
619 retry:
620         priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
621         if (!priv) {
622                 cond_resched();
623                 goto retry;
624         }
625
626         /* No failure on bio allocation */
627         bio = bio_alloc(GFP_NOIO, npages);
628         bio->bi_bdev = bdev;
629         bio->bi_private = priv;
630         return bio;
631 }
632
633 static void do_submit_bio(struct f2fs_sb_info *sbi,
634                                 enum page_type type, bool sync)
635 {
636         int rw = sync ? WRITE_SYNC : WRITE;
637         enum page_type btype = type > META ? META : type;
638
639         if (type >= META_FLUSH)
640                 rw = WRITE_FLUSH_FUA;
641
642         if (sbi->bio[btype]) {
643                 struct bio_private *p = sbi->bio[btype]->bi_private;
644                 p->sbi = sbi;
645                 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
646                 if (type == META_FLUSH) {
647                         DECLARE_COMPLETION_ONSTACK(wait);
648                         p->is_sync = true;
649                         p->wait = &wait;
650                         submit_bio(rw, sbi->bio[btype]);
651                         wait_for_completion(&wait);
652                 } else {
653                         p->is_sync = false;
654                         submit_bio(rw, sbi->bio[btype]);
655                 }
656                 sbi->bio[btype] = NULL;
657         }
658 }
659
660 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
661 {
662         down_write(&sbi->bio_sem);
663         do_submit_bio(sbi, type, sync);
664         up_write(&sbi->bio_sem);
665 }
666
667 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
668                                 block_t blk_addr, enum page_type type)
669 {
670         struct block_device *bdev = sbi->sb->s_bdev;
671
672         verify_block_addr(sbi, blk_addr);
673
674         down_write(&sbi->bio_sem);
675
676         inc_page_count(sbi, F2FS_WRITEBACK);
677
678         if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
679                 do_submit_bio(sbi, type, false);
680 alloc_new:
681         if (sbi->bio[type] == NULL) {
682                 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
683                 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
684                 /*
685                  * The end_io will be assigned at the sumbission phase.
686                  * Until then, let bio_add_page() merge consecutive IOs as much
687                  * as possible.
688                  */
689         }
690
691         if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
692                                                         PAGE_CACHE_SIZE) {
693                 do_submit_bio(sbi, type, false);
694                 goto alloc_new;
695         }
696
697         sbi->last_block_in_bio[type] = blk_addr;
698
699         up_write(&sbi->bio_sem);
700 }
701
702 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
703 {
704         struct curseg_info *curseg = CURSEG_I(sbi, type);
705         if (curseg->next_blkoff < sbi->blocks_per_seg)
706                 return true;
707         return false;
708 }
709
710 static int __get_segment_type_2(struct page *page, enum page_type p_type)
711 {
712         if (p_type == DATA)
713                 return CURSEG_HOT_DATA;
714         else
715                 return CURSEG_HOT_NODE;
716 }
717
718 static int __get_segment_type_4(struct page *page, enum page_type p_type)
719 {
720         if (p_type == DATA) {
721                 struct inode *inode = page->mapping->host;
722
723                 if (S_ISDIR(inode->i_mode))
724                         return CURSEG_HOT_DATA;
725                 else
726                         return CURSEG_COLD_DATA;
727         } else {
728                 if (IS_DNODE(page) && !is_cold_node(page))
729                         return CURSEG_HOT_NODE;
730                 else
731                         return CURSEG_COLD_NODE;
732         }
733 }
734
735 static int __get_segment_type_6(struct page *page, enum page_type p_type)
736 {
737         if (p_type == DATA) {
738                 struct inode *inode = page->mapping->host;
739
740                 if (S_ISDIR(inode->i_mode))
741                         return CURSEG_HOT_DATA;
742                 else if (is_cold_data(page) || is_cold_file(inode))
743                         return CURSEG_COLD_DATA;
744                 else
745                         return CURSEG_WARM_DATA;
746         } else {
747                 if (IS_DNODE(page))
748                         return is_cold_node(page) ? CURSEG_WARM_NODE :
749                                                 CURSEG_HOT_NODE;
750                 else
751                         return CURSEG_COLD_NODE;
752         }
753 }
754
755 static int __get_segment_type(struct page *page, enum page_type p_type)
756 {
757         struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
758         switch (sbi->active_logs) {
759         case 2:
760                 return __get_segment_type_2(page, p_type);
761         case 4:
762                 return __get_segment_type_4(page, p_type);
763         }
764         /* NR_CURSEG_TYPE(6) logs by default */
765         BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
766         return __get_segment_type_6(page, p_type);
767 }
768
769 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
770                         block_t old_blkaddr, block_t *new_blkaddr,
771                         struct f2fs_summary *sum, enum page_type p_type)
772 {
773         struct sit_info *sit_i = SIT_I(sbi);
774         struct curseg_info *curseg;
775         unsigned int old_cursegno;
776         int type;
777
778         type = __get_segment_type(page, p_type);
779         curseg = CURSEG_I(sbi, type);
780
781         mutex_lock(&curseg->curseg_mutex);
782
783         *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
784         old_cursegno = curseg->segno;
785
786         /*
787          * __add_sum_entry should be resided under the curseg_mutex
788          * because, this function updates a summary entry in the
789          * current summary block.
790          */
791         __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
792
793         mutex_lock(&sit_i->sentry_lock);
794         __refresh_next_blkoff(sbi, curseg);
795         sbi->block_count[curseg->alloc_type]++;
796
797         /*
798          * SIT information should be updated before segment allocation,
799          * since SSR needs latest valid block information.
800          */
801         refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
802
803         if (!__has_curseg_space(sbi, type))
804                 sit_i->s_ops->allocate_segment(sbi, type, false);
805
806         locate_dirty_segment(sbi, old_cursegno);
807         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
808         mutex_unlock(&sit_i->sentry_lock);
809
810         if (p_type == NODE)
811                 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
812
813         /* writeout dirty page into bdev */
814         submit_write_page(sbi, page, *new_blkaddr, p_type);
815
816         mutex_unlock(&curseg->curseg_mutex);
817 }
818
819 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
820 {
821         set_page_writeback(page);
822         submit_write_page(sbi, page, page->index, META);
823 }
824
825 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
826                 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
827 {
828         struct f2fs_summary sum;
829         set_summary(&sum, nid, 0, 0);
830         do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
831 }
832
833 void write_data_page(struct inode *inode, struct page *page,
834                 struct dnode_of_data *dn, block_t old_blkaddr,
835                 block_t *new_blkaddr)
836 {
837         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
838         struct f2fs_summary sum;
839         struct node_info ni;
840
841         BUG_ON(old_blkaddr == NULL_ADDR);
842         get_node_info(sbi, dn->nid, &ni);
843         set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
844
845         do_write_page(sbi, page, old_blkaddr,
846                         new_blkaddr, &sum, DATA);
847 }
848
849 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
850                                         block_t old_blk_addr)
851 {
852         submit_write_page(sbi, page, old_blk_addr, DATA);
853 }
854
855 void recover_data_page(struct f2fs_sb_info *sbi,
856                         struct page *page, struct f2fs_summary *sum,
857                         block_t old_blkaddr, block_t new_blkaddr)
858 {
859         struct sit_info *sit_i = SIT_I(sbi);
860         struct curseg_info *curseg;
861         unsigned int segno, old_cursegno;
862         struct seg_entry *se;
863         int type;
864
865         segno = GET_SEGNO(sbi, new_blkaddr);
866         se = get_seg_entry(sbi, segno);
867         type = se->type;
868
869         if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
870                 if (old_blkaddr == NULL_ADDR)
871                         type = CURSEG_COLD_DATA;
872                 else
873                         type = CURSEG_WARM_DATA;
874         }
875         curseg = CURSEG_I(sbi, type);
876
877         mutex_lock(&curseg->curseg_mutex);
878         mutex_lock(&sit_i->sentry_lock);
879
880         old_cursegno = curseg->segno;
881
882         /* change the current segment */
883         if (segno != curseg->segno) {
884                 curseg->next_segno = segno;
885                 change_curseg(sbi, type, true);
886         }
887
888         curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
889                                         (sbi->blocks_per_seg - 1);
890         __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
891
892         refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
893
894         locate_dirty_segment(sbi, old_cursegno);
895         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
896
897         mutex_unlock(&sit_i->sentry_lock);
898         mutex_unlock(&curseg->curseg_mutex);
899 }
900
901 void rewrite_node_page(struct f2fs_sb_info *sbi,
902                         struct page *page, struct f2fs_summary *sum,
903                         block_t old_blkaddr, block_t new_blkaddr)
904 {
905         struct sit_info *sit_i = SIT_I(sbi);
906         int type = CURSEG_WARM_NODE;
907         struct curseg_info *curseg;
908         unsigned int segno, old_cursegno;
909         block_t next_blkaddr = next_blkaddr_of_node(page);
910         unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
911
912         curseg = CURSEG_I(sbi, type);
913
914         mutex_lock(&curseg->curseg_mutex);
915         mutex_lock(&sit_i->sentry_lock);
916
917         segno = GET_SEGNO(sbi, new_blkaddr);
918         old_cursegno = curseg->segno;
919
920         /* change the current segment */
921         if (segno != curseg->segno) {
922                 curseg->next_segno = segno;
923                 change_curseg(sbi, type, true);
924         }
925         curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
926                                         (sbi->blocks_per_seg - 1);
927         __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
928
929         /* change the current log to the next block addr in advance */
930         if (next_segno != segno) {
931                 curseg->next_segno = next_segno;
932                 change_curseg(sbi, type, true);
933         }
934         curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
935                                         (sbi->blocks_per_seg - 1);
936
937         /* rewrite node page */
938         set_page_writeback(page);
939         submit_write_page(sbi, page, new_blkaddr, NODE);
940         f2fs_submit_bio(sbi, NODE, true);
941         refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
942
943         locate_dirty_segment(sbi, old_cursegno);
944         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
945
946         mutex_unlock(&sit_i->sentry_lock);
947         mutex_unlock(&curseg->curseg_mutex);
948 }
949
950 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
951 {
952         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
953         struct curseg_info *seg_i;
954         unsigned char *kaddr;
955         struct page *page;
956         block_t start;
957         int i, j, offset;
958
959         start = start_sum_block(sbi);
960
961         page = get_meta_page(sbi, start++);
962         kaddr = (unsigned char *)page_address(page);
963
964         /* Step 1: restore nat cache */
965         seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
966         memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
967
968         /* Step 2: restore sit cache */
969         seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
970         memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
971                                                 SUM_JOURNAL_SIZE);
972         offset = 2 * SUM_JOURNAL_SIZE;
973
974         /* Step 3: restore summary entries */
975         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
976                 unsigned short blk_off;
977                 unsigned int segno;
978
979                 seg_i = CURSEG_I(sbi, i);
980                 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
981                 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
982                 seg_i->next_segno = segno;
983                 reset_curseg(sbi, i, 0);
984                 seg_i->alloc_type = ckpt->alloc_type[i];
985                 seg_i->next_blkoff = blk_off;
986
987                 if (seg_i->alloc_type == SSR)
988                         blk_off = sbi->blocks_per_seg;
989
990                 for (j = 0; j < blk_off; j++) {
991                         struct f2fs_summary *s;
992                         s = (struct f2fs_summary *)(kaddr + offset);
993                         seg_i->sum_blk->entries[j] = *s;
994                         offset += SUMMARY_SIZE;
995                         if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
996                                                 SUM_FOOTER_SIZE)
997                                 continue;
998
999                         f2fs_put_page(page, 1);
1000                         page = NULL;
1001
1002                         page = get_meta_page(sbi, start++);
1003                         kaddr = (unsigned char *)page_address(page);
1004                         offset = 0;
1005                 }
1006         }
1007         f2fs_put_page(page, 1);
1008         return 0;
1009 }
1010
1011 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1012 {
1013         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1014         struct f2fs_summary_block *sum;
1015         struct curseg_info *curseg;
1016         struct page *new;
1017         unsigned short blk_off;
1018         unsigned int segno = 0;
1019         block_t blk_addr = 0;
1020
1021         /* get segment number and block addr */
1022         if (IS_DATASEG(type)) {
1023                 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1024                 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1025                                                         CURSEG_HOT_DATA]);
1026                 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1027                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1028                 else
1029                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1030         } else {
1031                 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1032                                                         CURSEG_HOT_NODE]);
1033                 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1034                                                         CURSEG_HOT_NODE]);
1035                 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1036                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1037                                                         type - CURSEG_HOT_NODE);
1038                 else
1039                         blk_addr = GET_SUM_BLOCK(sbi, segno);
1040         }
1041
1042         new = get_meta_page(sbi, blk_addr);
1043         sum = (struct f2fs_summary_block *)page_address(new);
1044
1045         if (IS_NODESEG(type)) {
1046                 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1047                         struct f2fs_summary *ns = &sum->entries[0];
1048                         int i;
1049                         for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1050                                 ns->version = 0;
1051                                 ns->ofs_in_node = 0;
1052                         }
1053                 } else {
1054                         if (restore_node_summary(sbi, segno, sum)) {
1055                                 f2fs_put_page(new, 1);
1056                                 return -EINVAL;
1057                         }
1058                 }
1059         }
1060
1061         /* set uncompleted segment to curseg */
1062         curseg = CURSEG_I(sbi, type);
1063         mutex_lock(&curseg->curseg_mutex);
1064         memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1065         curseg->next_segno = segno;
1066         reset_curseg(sbi, type, 0);
1067         curseg->alloc_type = ckpt->alloc_type[type];
1068         curseg->next_blkoff = blk_off;
1069         mutex_unlock(&curseg->curseg_mutex);
1070         f2fs_put_page(new, 1);
1071         return 0;
1072 }
1073
1074 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1075 {
1076         int type = CURSEG_HOT_DATA;
1077
1078         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1079                 /* restore for compacted data summary */
1080                 if (read_compacted_summaries(sbi))
1081                         return -EINVAL;
1082                 type = CURSEG_HOT_NODE;
1083         }
1084
1085         for (; type <= CURSEG_COLD_NODE; type++)
1086                 if (read_normal_summaries(sbi, type))
1087                         return -EINVAL;
1088         return 0;
1089 }
1090
1091 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1092 {
1093         struct page *page;
1094         unsigned char *kaddr;
1095         struct f2fs_summary *summary;
1096         struct curseg_info *seg_i;
1097         int written_size = 0;
1098         int i, j;
1099
1100         page = grab_meta_page(sbi, blkaddr++);
1101         kaddr = (unsigned char *)page_address(page);
1102
1103         /* Step 1: write nat cache */
1104         seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1105         memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1106         written_size += SUM_JOURNAL_SIZE;
1107
1108         /* Step 2: write sit cache */
1109         seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1110         memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1111                                                 SUM_JOURNAL_SIZE);
1112         written_size += SUM_JOURNAL_SIZE;
1113
1114         set_page_dirty(page);
1115
1116         /* Step 3: write summary entries */
1117         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1118                 unsigned short blkoff;
1119                 seg_i = CURSEG_I(sbi, i);
1120                 if (sbi->ckpt->alloc_type[i] == SSR)
1121                         blkoff = sbi->blocks_per_seg;
1122                 else
1123                         blkoff = curseg_blkoff(sbi, i);
1124
1125                 for (j = 0; j < blkoff; j++) {
1126                         if (!page) {
1127                                 page = grab_meta_page(sbi, blkaddr++);
1128                                 kaddr = (unsigned char *)page_address(page);
1129                                 written_size = 0;
1130                         }
1131                         summary = (struct f2fs_summary *)(kaddr + written_size);
1132                         *summary = seg_i->sum_blk->entries[j];
1133                         written_size += SUMMARY_SIZE;
1134                         set_page_dirty(page);
1135
1136                         if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1137                                                         SUM_FOOTER_SIZE)
1138                                 continue;
1139
1140                         f2fs_put_page(page, 1);
1141                         page = NULL;
1142                 }
1143         }
1144         if (page)
1145                 f2fs_put_page(page, 1);
1146 }
1147
1148 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1149                                         block_t blkaddr, int type)
1150 {
1151         int i, end;
1152         if (IS_DATASEG(type))
1153                 end = type + NR_CURSEG_DATA_TYPE;
1154         else
1155                 end = type + NR_CURSEG_NODE_TYPE;
1156
1157         for (i = type; i < end; i++) {
1158                 struct curseg_info *sum = CURSEG_I(sbi, i);
1159                 mutex_lock(&sum->curseg_mutex);
1160                 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1161                 mutex_unlock(&sum->curseg_mutex);
1162         }
1163 }
1164
1165 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1166 {
1167         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1168                 write_compacted_summaries(sbi, start_blk);
1169         else
1170                 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1171 }
1172
1173 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1174 {
1175         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1176                 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1177         return;
1178 }
1179
1180 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1181                                         unsigned int val, int alloc)
1182 {
1183         int i;
1184
1185         if (type == NAT_JOURNAL) {
1186                 for (i = 0; i < nats_in_cursum(sum); i++) {
1187                         if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1188                                 return i;
1189                 }
1190                 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1191                         return update_nats_in_cursum(sum, 1);
1192         } else if (type == SIT_JOURNAL) {
1193                 for (i = 0; i < sits_in_cursum(sum); i++)
1194                         if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1195                                 return i;
1196                 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1197                         return update_sits_in_cursum(sum, 1);
1198         }
1199         return -1;
1200 }
1201
1202 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1203                                         unsigned int segno)
1204 {
1205         struct sit_info *sit_i = SIT_I(sbi);
1206         unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1207         block_t blk_addr = sit_i->sit_base_addr + offset;
1208
1209         check_seg_range(sbi, segno);
1210
1211         /* calculate sit block address */
1212         if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1213                 blk_addr += sit_i->sit_blocks;
1214
1215         return get_meta_page(sbi, blk_addr);
1216 }
1217
1218 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1219                                         unsigned int start)
1220 {
1221         struct sit_info *sit_i = SIT_I(sbi);
1222         struct page *src_page, *dst_page;
1223         pgoff_t src_off, dst_off;
1224         void *src_addr, *dst_addr;
1225
1226         src_off = current_sit_addr(sbi, start);
1227         dst_off = next_sit_addr(sbi, src_off);
1228
1229         /* get current sit block page without lock */
1230         src_page = get_meta_page(sbi, src_off);
1231         dst_page = grab_meta_page(sbi, dst_off);
1232         BUG_ON(PageDirty(src_page));
1233
1234         src_addr = page_address(src_page);
1235         dst_addr = page_address(dst_page);
1236         memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1237
1238         set_page_dirty(dst_page);
1239         f2fs_put_page(src_page, 1);
1240
1241         set_to_next_sit(sit_i, start);
1242
1243         return dst_page;
1244 }
1245
1246 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1247 {
1248         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1249         struct f2fs_summary_block *sum = curseg->sum_blk;
1250         int i;
1251
1252         /*
1253          * If the journal area in the current summary is full of sit entries,
1254          * all the sit entries will be flushed. Otherwise the sit entries
1255          * are not able to replace with newly hot sit entries.
1256          */
1257         if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1258                 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1259                         unsigned int segno;
1260                         segno = le32_to_cpu(segno_in_journal(sum, i));
1261                         __mark_sit_entry_dirty(sbi, segno);
1262                 }
1263                 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1264                 return 1;
1265         }
1266         return 0;
1267 }
1268
1269 /*
1270  * CP calls this function, which flushes SIT entries including sit_journal,
1271  * and moves prefree segs to free segs.
1272  */
1273 void flush_sit_entries(struct f2fs_sb_info *sbi)
1274 {
1275         struct sit_info *sit_i = SIT_I(sbi);
1276         unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1277         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1278         struct f2fs_summary_block *sum = curseg->sum_blk;
1279         unsigned long nsegs = TOTAL_SEGS(sbi);
1280         struct page *page = NULL;
1281         struct f2fs_sit_block *raw_sit = NULL;
1282         unsigned int start = 0, end = 0;
1283         unsigned int segno = -1;
1284         bool flushed;
1285
1286         mutex_lock(&curseg->curseg_mutex);
1287         mutex_lock(&sit_i->sentry_lock);
1288
1289         /*
1290          * "flushed" indicates whether sit entries in journal are flushed
1291          * to the SIT area or not.
1292          */
1293         flushed = flush_sits_in_journal(sbi);
1294
1295         while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1296                 struct seg_entry *se = get_seg_entry(sbi, segno);
1297                 int sit_offset, offset;
1298
1299                 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1300
1301                 if (flushed)
1302                         goto to_sit_page;
1303
1304                 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1305                 if (offset >= 0) {
1306                         segno_in_journal(sum, offset) = cpu_to_le32(segno);
1307                         seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1308                         goto flush_done;
1309                 }
1310 to_sit_page:
1311                 if (!page || (start > segno) || (segno > end)) {
1312                         if (page) {
1313                                 f2fs_put_page(page, 1);
1314                                 page = NULL;
1315                         }
1316
1317                         start = START_SEGNO(sit_i, segno);
1318                         end = start + SIT_ENTRY_PER_BLOCK - 1;
1319
1320                         /* read sit block that will be updated */
1321                         page = get_next_sit_page(sbi, start);
1322                         raw_sit = page_address(page);
1323                 }
1324
1325                 /* udpate entry in SIT block */
1326                 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1327 flush_done:
1328                 __clear_bit(segno, bitmap);
1329                 sit_i->dirty_sentries--;
1330         }
1331         mutex_unlock(&sit_i->sentry_lock);
1332         mutex_unlock(&curseg->curseg_mutex);
1333
1334         /* writeout last modified SIT block */
1335         f2fs_put_page(page, 1);
1336
1337         set_prefree_as_free_segments(sbi);
1338 }
1339
1340 static int build_sit_info(struct f2fs_sb_info *sbi)
1341 {
1342         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1343         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1344         struct sit_info *sit_i;
1345         unsigned int sit_segs, start;
1346         char *src_bitmap, *dst_bitmap;
1347         unsigned int bitmap_size;
1348
1349         /* allocate memory for SIT information */
1350         sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1351         if (!sit_i)
1352                 return -ENOMEM;
1353
1354         SM_I(sbi)->sit_info = sit_i;
1355
1356         sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1357         if (!sit_i->sentries)
1358                 return -ENOMEM;
1359
1360         bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1361         sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1362         if (!sit_i->dirty_sentries_bitmap)
1363                 return -ENOMEM;
1364
1365         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1366                 sit_i->sentries[start].cur_valid_map
1367                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1368                 sit_i->sentries[start].ckpt_valid_map
1369                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1370                 if (!sit_i->sentries[start].cur_valid_map
1371                                 || !sit_i->sentries[start].ckpt_valid_map)
1372                         return -ENOMEM;
1373         }
1374
1375         if (sbi->segs_per_sec > 1) {
1376                 sit_i->sec_entries = vzalloc(sbi->total_sections *
1377                                         sizeof(struct sec_entry));
1378                 if (!sit_i->sec_entries)
1379                         return -ENOMEM;
1380         }
1381
1382         /* get information related with SIT */
1383         sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1384
1385         /* setup SIT bitmap from ckeckpoint pack */
1386         bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1387         src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1388
1389         dst_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1390         if (!dst_bitmap)
1391                 return -ENOMEM;
1392         memcpy(dst_bitmap, src_bitmap, bitmap_size);
1393
1394         /* init SIT information */
1395         sit_i->s_ops = &default_salloc_ops;
1396
1397         sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1398         sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1399         sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1400         sit_i->sit_bitmap = dst_bitmap;
1401         sit_i->bitmap_size = bitmap_size;
1402         sit_i->dirty_sentries = 0;
1403         sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1404         sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1405         sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1406         mutex_init(&sit_i->sentry_lock);
1407         return 0;
1408 }
1409
1410 static int build_free_segmap(struct f2fs_sb_info *sbi)
1411 {
1412         struct f2fs_sm_info *sm_info = SM_I(sbi);
1413         struct free_segmap_info *free_i;
1414         unsigned int bitmap_size, sec_bitmap_size;
1415
1416         /* allocate memory for free segmap information */
1417         free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1418         if (!free_i)
1419                 return -ENOMEM;
1420
1421         SM_I(sbi)->free_info = free_i;
1422
1423         bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1424         free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1425         if (!free_i->free_segmap)
1426                 return -ENOMEM;
1427
1428         sec_bitmap_size = f2fs_bitmap_size(sbi->total_sections);
1429         free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1430         if (!free_i->free_secmap)
1431                 return -ENOMEM;
1432
1433         /* set all segments as dirty temporarily */
1434         memset(free_i->free_segmap, 0xff, bitmap_size);
1435         memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1436
1437         /* init free segmap information */
1438         free_i->start_segno =
1439                 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1440         free_i->free_segments = 0;
1441         free_i->free_sections = 0;
1442         rwlock_init(&free_i->segmap_lock);
1443         return 0;
1444 }
1445
1446 static int build_curseg(struct f2fs_sb_info *sbi)
1447 {
1448         struct curseg_info *array;
1449         int i;
1450
1451         array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1452         if (!array)
1453                 return -ENOMEM;
1454
1455         SM_I(sbi)->curseg_array = array;
1456
1457         for (i = 0; i < NR_CURSEG_TYPE; i++) {
1458                 mutex_init(&array[i].curseg_mutex);
1459                 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1460                 if (!array[i].sum_blk)
1461                         return -ENOMEM;
1462                 array[i].segno = NULL_SEGNO;
1463                 array[i].next_blkoff = 0;
1464         }
1465         return restore_curseg_summaries(sbi);
1466 }
1467
1468 static void build_sit_entries(struct f2fs_sb_info *sbi)
1469 {
1470         struct sit_info *sit_i = SIT_I(sbi);
1471         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1472         struct f2fs_summary_block *sum = curseg->sum_blk;
1473         unsigned int start;
1474
1475         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1476                 struct seg_entry *se = &sit_i->sentries[start];
1477                 struct f2fs_sit_block *sit_blk;
1478                 struct f2fs_sit_entry sit;
1479                 struct page *page;
1480                 int i;
1481
1482                 mutex_lock(&curseg->curseg_mutex);
1483                 for (i = 0; i < sits_in_cursum(sum); i++) {
1484                         if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1485                                 sit = sit_in_journal(sum, i);
1486                                 mutex_unlock(&curseg->curseg_mutex);
1487                                 goto got_it;
1488                         }
1489                 }
1490                 mutex_unlock(&curseg->curseg_mutex);
1491                 page = get_current_sit_page(sbi, start);
1492                 sit_blk = (struct f2fs_sit_block *)page_address(page);
1493                 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1494                 f2fs_put_page(page, 1);
1495 got_it:
1496                 check_block_count(sbi, start, &sit);
1497                 seg_info_from_raw_sit(se, &sit);
1498                 if (sbi->segs_per_sec > 1) {
1499                         struct sec_entry *e = get_sec_entry(sbi, start);
1500                         e->valid_blocks += se->valid_blocks;
1501                 }
1502         }
1503 }
1504
1505 static void init_free_segmap(struct f2fs_sb_info *sbi)
1506 {
1507         unsigned int start;
1508         int type;
1509
1510         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1511                 struct seg_entry *sentry = get_seg_entry(sbi, start);
1512                 if (!sentry->valid_blocks)
1513                         __set_free(sbi, start);
1514         }
1515
1516         /* set use the current segments */
1517         for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1518                 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1519                 __set_test_and_inuse(sbi, curseg_t->segno);
1520         }
1521 }
1522
1523 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1524 {
1525         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1526         struct free_segmap_info *free_i = FREE_I(sbi);
1527         unsigned int segno = 0, offset = 0;
1528         unsigned short valid_blocks;
1529
1530         while (segno < TOTAL_SEGS(sbi)) {
1531                 /* find dirty segment based on free segmap */
1532                 segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1533                 if (segno >= TOTAL_SEGS(sbi))
1534                         break;
1535                 offset = segno + 1;
1536                 valid_blocks = get_valid_blocks(sbi, segno, 0);
1537                 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1538                         continue;
1539                 mutex_lock(&dirty_i->seglist_lock);
1540                 __locate_dirty_segment(sbi, segno, DIRTY);
1541                 mutex_unlock(&dirty_i->seglist_lock);
1542         }
1543 }
1544
1545 static int init_victim_segmap(struct f2fs_sb_info *sbi)
1546 {
1547         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1548         unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1549
1550         dirty_i->victim_segmap[FG_GC] = kzalloc(bitmap_size, GFP_KERNEL);
1551         dirty_i->victim_segmap[BG_GC] = kzalloc(bitmap_size, GFP_KERNEL);
1552         if (!dirty_i->victim_segmap[FG_GC] || !dirty_i->victim_segmap[BG_GC])
1553                 return -ENOMEM;
1554         return 0;
1555 }
1556
1557 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1558 {
1559         struct dirty_seglist_info *dirty_i;
1560         unsigned int bitmap_size, i;
1561
1562         /* allocate memory for dirty segments list information */
1563         dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1564         if (!dirty_i)
1565                 return -ENOMEM;
1566
1567         SM_I(sbi)->dirty_info = dirty_i;
1568         mutex_init(&dirty_i->seglist_lock);
1569
1570         bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1571
1572         for (i = 0; i < NR_DIRTY_TYPE; i++) {
1573                 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1574                 if (!dirty_i->dirty_segmap[i])
1575                         return -ENOMEM;
1576         }
1577
1578         init_dirty_segmap(sbi);
1579         return init_victim_segmap(sbi);
1580 }
1581
1582 /*
1583  * Update min, max modified time for cost-benefit GC algorithm
1584  */
1585 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1586 {
1587         struct sit_info *sit_i = SIT_I(sbi);
1588         unsigned int segno;
1589
1590         mutex_lock(&sit_i->sentry_lock);
1591
1592         sit_i->min_mtime = LLONG_MAX;
1593
1594         for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1595                 unsigned int i;
1596                 unsigned long long mtime = 0;
1597
1598                 for (i = 0; i < sbi->segs_per_sec; i++)
1599                         mtime += get_seg_entry(sbi, segno + i)->mtime;
1600
1601                 mtime = div_u64(mtime, sbi->segs_per_sec);
1602
1603                 if (sit_i->min_mtime > mtime)
1604                         sit_i->min_mtime = mtime;
1605         }
1606         sit_i->max_mtime = get_mtime(sbi);
1607         mutex_unlock(&sit_i->sentry_lock);
1608 }
1609
1610 int build_segment_manager(struct f2fs_sb_info *sbi)
1611 {
1612         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1613         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1614         struct f2fs_sm_info *sm_info;
1615         int err;
1616
1617         sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1618         if (!sm_info)
1619                 return -ENOMEM;
1620
1621         /* init sm info */
1622         sbi->sm_info = sm_info;
1623         INIT_LIST_HEAD(&sm_info->wblist_head);
1624         spin_lock_init(&sm_info->wblist_lock);
1625         sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1626         sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1627         sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1628         sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1629         sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1630         sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1631         sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1632
1633         err = build_sit_info(sbi);
1634         if (err)
1635                 return err;
1636         err = build_free_segmap(sbi);
1637         if (err)
1638                 return err;
1639         err = build_curseg(sbi);
1640         if (err)
1641                 return err;
1642
1643         /* reinit free segmap based on SIT */
1644         build_sit_entries(sbi);
1645
1646         init_free_segmap(sbi);
1647         err = build_dirty_segmap(sbi);
1648         if (err)
1649                 return err;
1650
1651         init_min_max_mtime(sbi);
1652         return 0;
1653 }
1654
1655 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1656                 enum dirty_type dirty_type)
1657 {
1658         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1659
1660         mutex_lock(&dirty_i->seglist_lock);
1661         kfree(dirty_i->dirty_segmap[dirty_type]);
1662         dirty_i->nr_dirty[dirty_type] = 0;
1663         mutex_unlock(&dirty_i->seglist_lock);
1664 }
1665
1666 void reset_victim_segmap(struct f2fs_sb_info *sbi)
1667 {
1668         unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1669         memset(DIRTY_I(sbi)->victim_segmap[FG_GC], 0, bitmap_size);
1670 }
1671
1672 static void destroy_victim_segmap(struct f2fs_sb_info *sbi)
1673 {
1674         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1675
1676         kfree(dirty_i->victim_segmap[FG_GC]);
1677         kfree(dirty_i->victim_segmap[BG_GC]);
1678 }
1679
1680 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1681 {
1682         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1683         int i;
1684
1685         if (!dirty_i)
1686                 return;
1687
1688         /* discard pre-free/dirty segments list */
1689         for (i = 0; i < NR_DIRTY_TYPE; i++)
1690                 discard_dirty_segmap(sbi, i);
1691
1692         destroy_victim_segmap(sbi);
1693         SM_I(sbi)->dirty_info = NULL;
1694         kfree(dirty_i);
1695 }
1696
1697 static void destroy_curseg(struct f2fs_sb_info *sbi)
1698 {
1699         struct curseg_info *array = SM_I(sbi)->curseg_array;
1700         int i;
1701
1702         if (!array)
1703                 return;
1704         SM_I(sbi)->curseg_array = NULL;
1705         for (i = 0; i < NR_CURSEG_TYPE; i++)
1706                 kfree(array[i].sum_blk);
1707         kfree(array);
1708 }
1709
1710 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1711 {
1712         struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1713         if (!free_i)
1714                 return;
1715         SM_I(sbi)->free_info = NULL;
1716         kfree(free_i->free_segmap);
1717         kfree(free_i->free_secmap);
1718         kfree(free_i);
1719 }
1720
1721 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1722 {
1723         struct sit_info *sit_i = SIT_I(sbi);
1724         unsigned int start;
1725
1726         if (!sit_i)
1727                 return;
1728
1729         if (sit_i->sentries) {
1730                 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1731                         kfree(sit_i->sentries[start].cur_valid_map);
1732                         kfree(sit_i->sentries[start].ckpt_valid_map);
1733                 }
1734         }
1735         vfree(sit_i->sentries);
1736         vfree(sit_i->sec_entries);
1737         kfree(sit_i->dirty_sentries_bitmap);
1738
1739         SM_I(sbi)->sit_info = NULL;
1740         kfree(sit_i->sit_bitmap);
1741         kfree(sit_i);
1742 }
1743
1744 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1745 {
1746         struct f2fs_sm_info *sm_info = SM_I(sbi);
1747         destroy_dirty_segmap(sbi);
1748         destroy_curseg(sbi);
1749         destroy_free_segmap(sbi);
1750         destroy_sit_info(sbi);
1751         sbi->sm_info = NULL;
1752         kfree(sm_info);
1753 }