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