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