f2fs: remove set_page_dirty for atomic f2fs_end_io_write
[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                 }
635                 end_page_writeback(page);
636                 dec_page_count(p->sbi, F2FS_WRITEBACK);
637         } while (bvec >= bio->bi_io_vec);
638
639         if (p->is_sync)
640                 complete(p->wait);
641         kfree(p);
642         bio_put(bio);
643 }
644
645 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
646 {
647         struct bio *bio;
648         struct bio_private *priv;
649 retry:
650         priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
651         if (!priv) {
652                 cond_resched();
653                 goto retry;
654         }
655
656         /* No failure on bio allocation */
657         bio = bio_alloc(GFP_NOIO, npages);
658         bio->bi_bdev = bdev;
659         bio->bi_private = priv;
660         return bio;
661 }
662
663 static void do_submit_bio(struct f2fs_sb_info *sbi,
664                                 enum page_type type, bool sync)
665 {
666         int rw = sync ? WRITE_SYNC : WRITE;
667         enum page_type btype = type > META ? META : type;
668
669         if (type >= META_FLUSH)
670                 rw = WRITE_FLUSH_FUA;
671
672         if (sbi->bio[btype]) {
673                 struct bio_private *p = sbi->bio[btype]->bi_private;
674                 p->sbi = sbi;
675                 sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
676                 if (type == META_FLUSH) {
677                         DECLARE_COMPLETION_ONSTACK(wait);
678                         p->is_sync = true;
679                         p->wait = &wait;
680                         submit_bio(rw, sbi->bio[btype]);
681                         wait_for_completion(&wait);
682                 } else {
683                         p->is_sync = false;
684                         submit_bio(rw, sbi->bio[btype]);
685                 }
686                 sbi->bio[btype] = NULL;
687         }
688 }
689
690 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
691 {
692         down_write(&sbi->bio_sem);
693         do_submit_bio(sbi, type, sync);
694         up_write(&sbi->bio_sem);
695 }
696
697 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
698                                 block_t blk_addr, enum page_type type)
699 {
700         struct block_device *bdev = sbi->sb->s_bdev;
701
702         verify_block_addr(sbi, blk_addr);
703
704         down_write(&sbi->bio_sem);
705
706         inc_page_count(sbi, F2FS_WRITEBACK);
707
708         if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
709                 do_submit_bio(sbi, type, false);
710 alloc_new:
711         if (sbi->bio[type] == NULL) {
712                 sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
713                 sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
714                 /*
715                  * The end_io will be assigned at the sumbission phase.
716                  * Until then, let bio_add_page() merge consecutive IOs as much
717                  * as possible.
718                  */
719         }
720
721         if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
722                                                         PAGE_CACHE_SIZE) {
723                 do_submit_bio(sbi, type, false);
724                 goto alloc_new;
725         }
726
727         sbi->last_block_in_bio[type] = blk_addr;
728
729         up_write(&sbi->bio_sem);
730 }
731
732 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
733 {
734         struct curseg_info *curseg = CURSEG_I(sbi, type);
735         if (curseg->next_blkoff < sbi->blocks_per_seg)
736                 return true;
737         return false;
738 }
739
740 static int __get_segment_type_2(struct page *page, enum page_type p_type)
741 {
742         if (p_type == DATA)
743                 return CURSEG_HOT_DATA;
744         else
745                 return CURSEG_HOT_NODE;
746 }
747
748 static int __get_segment_type_4(struct page *page, enum page_type p_type)
749 {
750         if (p_type == DATA) {
751                 struct inode *inode = page->mapping->host;
752
753                 if (S_ISDIR(inode->i_mode))
754                         return CURSEG_HOT_DATA;
755                 else
756                         return CURSEG_COLD_DATA;
757         } else {
758                 if (IS_DNODE(page) && !is_cold_node(page))
759                         return CURSEG_HOT_NODE;
760                 else
761                         return CURSEG_COLD_NODE;
762         }
763 }
764
765 static int __get_segment_type_6(struct page *page, enum page_type p_type)
766 {
767         if (p_type == DATA) {
768                 struct inode *inode = page->mapping->host;
769
770                 if (S_ISDIR(inode->i_mode))
771                         return CURSEG_HOT_DATA;
772                 else if (is_cold_data(page) || is_cold_file(inode))
773                         return CURSEG_COLD_DATA;
774                 else
775                         return CURSEG_WARM_DATA;
776         } else {
777                 if (IS_DNODE(page))
778                         return is_cold_node(page) ? CURSEG_WARM_NODE :
779                                                 CURSEG_HOT_NODE;
780                 else
781                         return CURSEG_COLD_NODE;
782         }
783 }
784
785 static int __get_segment_type(struct page *page, enum page_type p_type)
786 {
787         struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
788         switch (sbi->active_logs) {
789         case 2:
790                 return __get_segment_type_2(page, p_type);
791         case 4:
792                 return __get_segment_type_4(page, p_type);
793         case 6:
794                 return __get_segment_type_6(page, p_type);
795         default:
796                 BUG();
797         }
798 }
799
800 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
801                         block_t old_blkaddr, block_t *new_blkaddr,
802                         struct f2fs_summary *sum, enum page_type p_type)
803 {
804         struct sit_info *sit_i = SIT_I(sbi);
805         struct curseg_info *curseg;
806         unsigned int old_cursegno;
807         int type;
808
809         type = __get_segment_type(page, p_type);
810         curseg = CURSEG_I(sbi, type);
811
812         mutex_lock(&curseg->curseg_mutex);
813
814         *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
815         old_cursegno = curseg->segno;
816
817         /*
818          * __add_sum_entry should be resided under the curseg_mutex
819          * because, this function updates a summary entry in the
820          * current summary block.
821          */
822         __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
823
824         mutex_lock(&sit_i->sentry_lock);
825         __refresh_next_blkoff(sbi, curseg);
826         sbi->block_count[curseg->alloc_type]++;
827
828         /*
829          * SIT information should be updated before segment allocation,
830          * since SSR needs latest valid block information.
831          */
832         refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
833
834         if (!__has_curseg_space(sbi, type))
835                 sit_i->s_ops->allocate_segment(sbi, type, false);
836
837         locate_dirty_segment(sbi, old_cursegno);
838         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
839         mutex_unlock(&sit_i->sentry_lock);
840
841         if (p_type == NODE)
842                 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
843
844         /* writeout dirty page into bdev */
845         submit_write_page(sbi, page, *new_blkaddr, p_type);
846
847         mutex_unlock(&curseg->curseg_mutex);
848 }
849
850 int write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
851                         struct writeback_control *wbc)
852 {
853         if (wbc->for_reclaim)
854                 return AOP_WRITEPAGE_ACTIVATE;
855
856         set_page_writeback(page);
857         submit_write_page(sbi, page, page->index, META);
858         return 0;
859 }
860
861 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
862                 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
863 {
864         struct f2fs_summary sum;
865         set_summary(&sum, nid, 0, 0);
866         do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
867 }
868
869 void write_data_page(struct inode *inode, struct page *page,
870                 struct dnode_of_data *dn, block_t old_blkaddr,
871                 block_t *new_blkaddr)
872 {
873         struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
874         struct f2fs_summary sum;
875         struct node_info ni;
876
877         BUG_ON(old_blkaddr == NULL_ADDR);
878         get_node_info(sbi, dn->nid, &ni);
879         set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
880
881         do_write_page(sbi, page, old_blkaddr,
882                         new_blkaddr, &sum, DATA);
883 }
884
885 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
886                                         block_t old_blk_addr)
887 {
888         submit_write_page(sbi, page, old_blk_addr, DATA);
889 }
890
891 void recover_data_page(struct f2fs_sb_info *sbi,
892                         struct page *page, struct f2fs_summary *sum,
893                         block_t old_blkaddr, block_t new_blkaddr)
894 {
895         struct sit_info *sit_i = SIT_I(sbi);
896         struct curseg_info *curseg;
897         unsigned int segno, old_cursegno;
898         struct seg_entry *se;
899         int type;
900
901         segno = GET_SEGNO(sbi, new_blkaddr);
902         se = get_seg_entry(sbi, segno);
903         type = se->type;
904
905         if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
906                 if (old_blkaddr == NULL_ADDR)
907                         type = CURSEG_COLD_DATA;
908                 else
909                         type = CURSEG_WARM_DATA;
910         }
911         curseg = CURSEG_I(sbi, type);
912
913         mutex_lock(&curseg->curseg_mutex);
914         mutex_lock(&sit_i->sentry_lock);
915
916         old_cursegno = curseg->segno;
917
918         /* change the current segment */
919         if (segno != curseg->segno) {
920                 curseg->next_segno = segno;
921                 change_curseg(sbi, type, true);
922         }
923
924         curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
925                                         (sbi->blocks_per_seg - 1);
926         __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
927
928         refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
929
930         locate_dirty_segment(sbi, old_cursegno);
931         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
932
933         mutex_unlock(&sit_i->sentry_lock);
934         mutex_unlock(&curseg->curseg_mutex);
935 }
936
937 void rewrite_node_page(struct f2fs_sb_info *sbi,
938                         struct page *page, struct f2fs_summary *sum,
939                         block_t old_blkaddr, block_t new_blkaddr)
940 {
941         struct sit_info *sit_i = SIT_I(sbi);
942         int type = CURSEG_WARM_NODE;
943         struct curseg_info *curseg;
944         unsigned int segno, old_cursegno;
945         block_t next_blkaddr = next_blkaddr_of_node(page);
946         unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
947
948         curseg = CURSEG_I(sbi, type);
949
950         mutex_lock(&curseg->curseg_mutex);
951         mutex_lock(&sit_i->sentry_lock);
952
953         segno = GET_SEGNO(sbi, new_blkaddr);
954         old_cursegno = curseg->segno;
955
956         /* change the current segment */
957         if (segno != curseg->segno) {
958                 curseg->next_segno = segno;
959                 change_curseg(sbi, type, true);
960         }
961         curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
962                                         (sbi->blocks_per_seg - 1);
963         __add_sum_entry(sbi, type, sum, curseg->next_blkoff);
964
965         /* change the current log to the next block addr in advance */
966         if (next_segno != segno) {
967                 curseg->next_segno = next_segno;
968                 change_curseg(sbi, type, true);
969         }
970         curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
971                                         (sbi->blocks_per_seg - 1);
972
973         /* rewrite node page */
974         set_page_writeback(page);
975         submit_write_page(sbi, page, new_blkaddr, NODE);
976         f2fs_submit_bio(sbi, NODE, true);
977         refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
978
979         locate_dirty_segment(sbi, old_cursegno);
980         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
981
982         mutex_unlock(&sit_i->sentry_lock);
983         mutex_unlock(&curseg->curseg_mutex);
984 }
985
986 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
987 {
988         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
989         struct curseg_info *seg_i;
990         unsigned char *kaddr;
991         struct page *page;
992         block_t start;
993         int i, j, offset;
994
995         start = start_sum_block(sbi);
996
997         page = get_meta_page(sbi, start++);
998         kaddr = (unsigned char *)page_address(page);
999
1000         /* Step 1: restore nat cache */
1001         seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1002         memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1003
1004         /* Step 2: restore sit cache */
1005         seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1006         memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1007                                                 SUM_JOURNAL_SIZE);
1008         offset = 2 * SUM_JOURNAL_SIZE;
1009
1010         /* Step 3: restore summary entries */
1011         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1012                 unsigned short blk_off;
1013                 unsigned int segno;
1014
1015                 seg_i = CURSEG_I(sbi, i);
1016                 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1017                 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1018                 seg_i->next_segno = segno;
1019                 reset_curseg(sbi, i, 0);
1020                 seg_i->alloc_type = ckpt->alloc_type[i];
1021                 seg_i->next_blkoff = blk_off;
1022
1023                 if (seg_i->alloc_type == SSR)
1024                         blk_off = sbi->blocks_per_seg;
1025
1026                 for (j = 0; j < blk_off; j++) {
1027                         struct f2fs_summary *s;
1028                         s = (struct f2fs_summary *)(kaddr + offset);
1029                         seg_i->sum_blk->entries[j] = *s;
1030                         offset += SUMMARY_SIZE;
1031                         if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1032                                                 SUM_FOOTER_SIZE)
1033                                 continue;
1034
1035                         f2fs_put_page(page, 1);
1036                         page = NULL;
1037
1038                         page = get_meta_page(sbi, start++);
1039                         kaddr = (unsigned char *)page_address(page);
1040                         offset = 0;
1041                 }
1042         }
1043         f2fs_put_page(page, 1);
1044         return 0;
1045 }
1046
1047 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1048 {
1049         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1050         struct f2fs_summary_block *sum;
1051         struct curseg_info *curseg;
1052         struct page *new;
1053         unsigned short blk_off;
1054         unsigned int segno = 0;
1055         block_t blk_addr = 0;
1056
1057         /* get segment number and block addr */
1058         if (IS_DATASEG(type)) {
1059                 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1060                 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1061                                                         CURSEG_HOT_DATA]);
1062                 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1063                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1064                 else
1065                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1066         } else {
1067                 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1068                                                         CURSEG_HOT_NODE]);
1069                 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1070                                                         CURSEG_HOT_NODE]);
1071                 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1072                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1073                                                         type - CURSEG_HOT_NODE);
1074                 else
1075                         blk_addr = GET_SUM_BLOCK(sbi, segno);
1076         }
1077
1078         new = get_meta_page(sbi, blk_addr);
1079         sum = (struct f2fs_summary_block *)page_address(new);
1080
1081         if (IS_NODESEG(type)) {
1082                 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1083                         struct f2fs_summary *ns = &sum->entries[0];
1084                         int i;
1085                         for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1086                                 ns->version = 0;
1087                                 ns->ofs_in_node = 0;
1088                         }
1089                 } else {
1090                         if (restore_node_summary(sbi, segno, sum)) {
1091                                 f2fs_put_page(new, 1);
1092                                 return -EINVAL;
1093                         }
1094                 }
1095         }
1096
1097         /* set uncompleted segment to curseg */
1098         curseg = CURSEG_I(sbi, type);
1099         mutex_lock(&curseg->curseg_mutex);
1100         memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1101         curseg->next_segno = segno;
1102         reset_curseg(sbi, type, 0);
1103         curseg->alloc_type = ckpt->alloc_type[type];
1104         curseg->next_blkoff = blk_off;
1105         mutex_unlock(&curseg->curseg_mutex);
1106         f2fs_put_page(new, 1);
1107         return 0;
1108 }
1109
1110 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1111 {
1112         int type = CURSEG_HOT_DATA;
1113
1114         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1115                 /* restore for compacted data summary */
1116                 if (read_compacted_summaries(sbi))
1117                         return -EINVAL;
1118                 type = CURSEG_HOT_NODE;
1119         }
1120
1121         for (; type <= CURSEG_COLD_NODE; type++)
1122                 if (read_normal_summaries(sbi, type))
1123                         return -EINVAL;
1124         return 0;
1125 }
1126
1127 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1128 {
1129         struct page *page;
1130         unsigned char *kaddr;
1131         struct f2fs_summary *summary;
1132         struct curseg_info *seg_i;
1133         int written_size = 0;
1134         int i, j;
1135
1136         page = grab_meta_page(sbi, blkaddr++);
1137         kaddr = (unsigned char *)page_address(page);
1138
1139         /* Step 1: write nat cache */
1140         seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1141         memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1142         written_size += SUM_JOURNAL_SIZE;
1143
1144         /* Step 2: write sit cache */
1145         seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1146         memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1147                                                 SUM_JOURNAL_SIZE);
1148         written_size += SUM_JOURNAL_SIZE;
1149
1150         set_page_dirty(page);
1151
1152         /* Step 3: write summary entries */
1153         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1154                 unsigned short blkoff;
1155                 seg_i = CURSEG_I(sbi, i);
1156                 if (sbi->ckpt->alloc_type[i] == SSR)
1157                         blkoff = sbi->blocks_per_seg;
1158                 else
1159                         blkoff = curseg_blkoff(sbi, i);
1160
1161                 for (j = 0; j < blkoff; j++) {
1162                         if (!page) {
1163                                 page = grab_meta_page(sbi, blkaddr++);
1164                                 kaddr = (unsigned char *)page_address(page);
1165                                 written_size = 0;
1166                         }
1167                         summary = (struct f2fs_summary *)(kaddr + written_size);
1168                         *summary = seg_i->sum_blk->entries[j];
1169                         written_size += SUMMARY_SIZE;
1170                         set_page_dirty(page);
1171
1172                         if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1173                                                         SUM_FOOTER_SIZE)
1174                                 continue;
1175
1176                         f2fs_put_page(page, 1);
1177                         page = NULL;
1178                 }
1179         }
1180         if (page)
1181                 f2fs_put_page(page, 1);
1182 }
1183
1184 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1185                                         block_t blkaddr, int type)
1186 {
1187         int i, end;
1188         if (IS_DATASEG(type))
1189                 end = type + NR_CURSEG_DATA_TYPE;
1190         else
1191                 end = type + NR_CURSEG_NODE_TYPE;
1192
1193         for (i = type; i < end; i++) {
1194                 struct curseg_info *sum = CURSEG_I(sbi, i);
1195                 mutex_lock(&sum->curseg_mutex);
1196                 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1197                 mutex_unlock(&sum->curseg_mutex);
1198         }
1199 }
1200
1201 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1202 {
1203         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1204                 write_compacted_summaries(sbi, start_blk);
1205         else
1206                 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1207 }
1208
1209 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1210 {
1211         if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1212                 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1213         return;
1214 }
1215
1216 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1217                                         unsigned int val, int alloc)
1218 {
1219         int i;
1220
1221         if (type == NAT_JOURNAL) {
1222                 for (i = 0; i < nats_in_cursum(sum); i++) {
1223                         if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1224                                 return i;
1225                 }
1226                 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1227                         return update_nats_in_cursum(sum, 1);
1228         } else if (type == SIT_JOURNAL) {
1229                 for (i = 0; i < sits_in_cursum(sum); i++)
1230                         if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1231                                 return i;
1232                 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1233                         return update_sits_in_cursum(sum, 1);
1234         }
1235         return -1;
1236 }
1237
1238 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1239                                         unsigned int segno)
1240 {
1241         struct sit_info *sit_i = SIT_I(sbi);
1242         unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1243         block_t blk_addr = sit_i->sit_base_addr + offset;
1244
1245         check_seg_range(sbi, segno);
1246
1247         /* calculate sit block address */
1248         if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1249                 blk_addr += sit_i->sit_blocks;
1250
1251         return get_meta_page(sbi, blk_addr);
1252 }
1253
1254 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1255                                         unsigned int start)
1256 {
1257         struct sit_info *sit_i = SIT_I(sbi);
1258         struct page *src_page, *dst_page;
1259         pgoff_t src_off, dst_off;
1260         void *src_addr, *dst_addr;
1261
1262         src_off = current_sit_addr(sbi, start);
1263         dst_off = next_sit_addr(sbi, src_off);
1264
1265         /* get current sit block page without lock */
1266         src_page = get_meta_page(sbi, src_off);
1267         dst_page = grab_meta_page(sbi, dst_off);
1268         BUG_ON(PageDirty(src_page));
1269
1270         src_addr = page_address(src_page);
1271         dst_addr = page_address(dst_page);
1272         memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1273
1274         set_page_dirty(dst_page);
1275         f2fs_put_page(src_page, 1);
1276
1277         set_to_next_sit(sit_i, start);
1278
1279         return dst_page;
1280 }
1281
1282 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1283 {
1284         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1285         struct f2fs_summary_block *sum = curseg->sum_blk;
1286         int i;
1287
1288         /*
1289          * If the journal area in the current summary is full of sit entries,
1290          * all the sit entries will be flushed. Otherwise the sit entries
1291          * are not able to replace with newly hot sit entries.
1292          */
1293         if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1294                 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1295                         unsigned int segno;
1296                         segno = le32_to_cpu(segno_in_journal(sum, i));
1297                         __mark_sit_entry_dirty(sbi, segno);
1298                 }
1299                 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1300                 return 1;
1301         }
1302         return 0;
1303 }
1304
1305 /*
1306  * CP calls this function, which flushes SIT entries including sit_journal,
1307  * and moves prefree segs to free segs.
1308  */
1309 void flush_sit_entries(struct f2fs_sb_info *sbi)
1310 {
1311         struct sit_info *sit_i = SIT_I(sbi);
1312         unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1313         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1314         struct f2fs_summary_block *sum = curseg->sum_blk;
1315         unsigned long nsegs = TOTAL_SEGS(sbi);
1316         struct page *page = NULL;
1317         struct f2fs_sit_block *raw_sit = NULL;
1318         unsigned int start = 0, end = 0;
1319         unsigned int segno = -1;
1320         bool flushed;
1321
1322         mutex_lock(&curseg->curseg_mutex);
1323         mutex_lock(&sit_i->sentry_lock);
1324
1325         /*
1326          * "flushed" indicates whether sit entries in journal are flushed
1327          * to the SIT area or not.
1328          */
1329         flushed = flush_sits_in_journal(sbi);
1330
1331         while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1332                 struct seg_entry *se = get_seg_entry(sbi, segno);
1333                 int sit_offset, offset;
1334
1335                 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1336
1337                 if (flushed)
1338                         goto to_sit_page;
1339
1340                 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1341                 if (offset >= 0) {
1342                         segno_in_journal(sum, offset) = cpu_to_le32(segno);
1343                         seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1344                         goto flush_done;
1345                 }
1346 to_sit_page:
1347                 if (!page || (start > segno) || (segno > end)) {
1348                         if (page) {
1349                                 f2fs_put_page(page, 1);
1350                                 page = NULL;
1351                         }
1352
1353                         start = START_SEGNO(sit_i, segno);
1354                         end = start + SIT_ENTRY_PER_BLOCK - 1;
1355
1356                         /* read sit block that will be updated */
1357                         page = get_next_sit_page(sbi, start);
1358                         raw_sit = page_address(page);
1359                 }
1360
1361                 /* udpate entry in SIT block */
1362                 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1363 flush_done:
1364                 __clear_bit(segno, bitmap);
1365                 sit_i->dirty_sentries--;
1366         }
1367         mutex_unlock(&sit_i->sentry_lock);
1368         mutex_unlock(&curseg->curseg_mutex);
1369
1370         /* writeout last modified SIT block */
1371         f2fs_put_page(page, 1);
1372
1373         set_prefree_as_free_segments(sbi);
1374 }
1375
1376 static int build_sit_info(struct f2fs_sb_info *sbi)
1377 {
1378         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1379         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1380         struct sit_info *sit_i;
1381         unsigned int sit_segs, start;
1382         char *src_bitmap, *dst_bitmap;
1383         unsigned int bitmap_size;
1384
1385         /* allocate memory for SIT information */
1386         sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1387         if (!sit_i)
1388                 return -ENOMEM;
1389
1390         SM_I(sbi)->sit_info = sit_i;
1391
1392         sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1393         if (!sit_i->sentries)
1394                 return -ENOMEM;
1395
1396         bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1397         sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1398         if (!sit_i->dirty_sentries_bitmap)
1399                 return -ENOMEM;
1400
1401         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1402                 sit_i->sentries[start].cur_valid_map
1403                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1404                 sit_i->sentries[start].ckpt_valid_map
1405                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1406                 if (!sit_i->sentries[start].cur_valid_map
1407                                 || !sit_i->sentries[start].ckpt_valid_map)
1408                         return -ENOMEM;
1409         }
1410
1411         if (sbi->segs_per_sec > 1) {
1412                 sit_i->sec_entries = vzalloc(sbi->total_sections *
1413                                         sizeof(struct sec_entry));
1414                 if (!sit_i->sec_entries)
1415                         return -ENOMEM;
1416         }
1417
1418         /* get information related with SIT */
1419         sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1420
1421         /* setup SIT bitmap from ckeckpoint pack */
1422         bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1423         src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1424
1425         dst_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1426         if (!dst_bitmap)
1427                 return -ENOMEM;
1428         memcpy(dst_bitmap, src_bitmap, bitmap_size);
1429
1430         /* init SIT information */
1431         sit_i->s_ops = &default_salloc_ops;
1432
1433         sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1434         sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1435         sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1436         sit_i->sit_bitmap = dst_bitmap;
1437         sit_i->bitmap_size = bitmap_size;
1438         sit_i->dirty_sentries = 0;
1439         sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1440         sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1441         sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1442         mutex_init(&sit_i->sentry_lock);
1443         return 0;
1444 }
1445
1446 static int build_free_segmap(struct f2fs_sb_info *sbi)
1447 {
1448         struct f2fs_sm_info *sm_info = SM_I(sbi);
1449         struct free_segmap_info *free_i;
1450         unsigned int bitmap_size, sec_bitmap_size;
1451
1452         /* allocate memory for free segmap information */
1453         free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1454         if (!free_i)
1455                 return -ENOMEM;
1456
1457         SM_I(sbi)->free_info = free_i;
1458
1459         bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1460         free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1461         if (!free_i->free_segmap)
1462                 return -ENOMEM;
1463
1464         sec_bitmap_size = f2fs_bitmap_size(sbi->total_sections);
1465         free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1466         if (!free_i->free_secmap)
1467                 return -ENOMEM;
1468
1469         /* set all segments as dirty temporarily */
1470         memset(free_i->free_segmap, 0xff, bitmap_size);
1471         memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1472
1473         /* init free segmap information */
1474         free_i->start_segno =
1475                 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1476         free_i->free_segments = 0;
1477         free_i->free_sections = 0;
1478         rwlock_init(&free_i->segmap_lock);
1479         return 0;
1480 }
1481
1482 static int build_curseg(struct f2fs_sb_info *sbi)
1483 {
1484         struct curseg_info *array;
1485         int i;
1486
1487         array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1488         if (!array)
1489                 return -ENOMEM;
1490
1491         SM_I(sbi)->curseg_array = array;
1492
1493         for (i = 0; i < NR_CURSEG_TYPE; i++) {
1494                 mutex_init(&array[i].curseg_mutex);
1495                 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1496                 if (!array[i].sum_blk)
1497                         return -ENOMEM;
1498                 array[i].segno = NULL_SEGNO;
1499                 array[i].next_blkoff = 0;
1500         }
1501         return restore_curseg_summaries(sbi);
1502 }
1503
1504 static void build_sit_entries(struct f2fs_sb_info *sbi)
1505 {
1506         struct sit_info *sit_i = SIT_I(sbi);
1507         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1508         struct f2fs_summary_block *sum = curseg->sum_blk;
1509         unsigned int start;
1510
1511         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1512                 struct seg_entry *se = &sit_i->sentries[start];
1513                 struct f2fs_sit_block *sit_blk;
1514                 struct f2fs_sit_entry sit;
1515                 struct page *page;
1516                 int i;
1517
1518                 mutex_lock(&curseg->curseg_mutex);
1519                 for (i = 0; i < sits_in_cursum(sum); i++) {
1520                         if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1521                                 sit = sit_in_journal(sum, i);
1522                                 mutex_unlock(&curseg->curseg_mutex);
1523                                 goto got_it;
1524                         }
1525                 }
1526                 mutex_unlock(&curseg->curseg_mutex);
1527                 page = get_current_sit_page(sbi, start);
1528                 sit_blk = (struct f2fs_sit_block *)page_address(page);
1529                 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1530                 f2fs_put_page(page, 1);
1531 got_it:
1532                 check_block_count(sbi, start, &sit);
1533                 seg_info_from_raw_sit(se, &sit);
1534                 if (sbi->segs_per_sec > 1) {
1535                         struct sec_entry *e = get_sec_entry(sbi, start);
1536                         e->valid_blocks += se->valid_blocks;
1537                 }
1538         }
1539 }
1540
1541 static void init_free_segmap(struct f2fs_sb_info *sbi)
1542 {
1543         unsigned int start;
1544         int type;
1545
1546         for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1547                 struct seg_entry *sentry = get_seg_entry(sbi, start);
1548                 if (!sentry->valid_blocks)
1549                         __set_free(sbi, start);
1550         }
1551
1552         /* set use the current segments */
1553         for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1554                 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1555                 __set_test_and_inuse(sbi, curseg_t->segno);
1556         }
1557 }
1558
1559 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1560 {
1561         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1562         struct free_segmap_info *free_i = FREE_I(sbi);
1563         unsigned int segno = 0, offset = 0;
1564         unsigned short valid_blocks;
1565
1566         while (segno < TOTAL_SEGS(sbi)) {
1567                 /* find dirty segment based on free segmap */
1568                 segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1569                 if (segno >= TOTAL_SEGS(sbi))
1570                         break;
1571                 offset = segno + 1;
1572                 valid_blocks = get_valid_blocks(sbi, segno, 0);
1573                 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1574                         continue;
1575                 mutex_lock(&dirty_i->seglist_lock);
1576                 __locate_dirty_segment(sbi, segno, DIRTY);
1577                 mutex_unlock(&dirty_i->seglist_lock);
1578         }
1579 }
1580
1581 static int init_victim_segmap(struct f2fs_sb_info *sbi)
1582 {
1583         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1584         unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1585
1586         dirty_i->victim_segmap[FG_GC] = kzalloc(bitmap_size, GFP_KERNEL);
1587         dirty_i->victim_segmap[BG_GC] = kzalloc(bitmap_size, GFP_KERNEL);
1588         if (!dirty_i->victim_segmap[FG_GC] || !dirty_i->victim_segmap[BG_GC])
1589                 return -ENOMEM;
1590         return 0;
1591 }
1592
1593 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1594 {
1595         struct dirty_seglist_info *dirty_i;
1596         unsigned int bitmap_size, i;
1597
1598         /* allocate memory for dirty segments list information */
1599         dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1600         if (!dirty_i)
1601                 return -ENOMEM;
1602
1603         SM_I(sbi)->dirty_info = dirty_i;
1604         mutex_init(&dirty_i->seglist_lock);
1605
1606         bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1607
1608         for (i = 0; i < NR_DIRTY_TYPE; i++) {
1609                 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1610                 dirty_i->nr_dirty[i] = 0;
1611                 if (!dirty_i->dirty_segmap[i])
1612                         return -ENOMEM;
1613         }
1614
1615         init_dirty_segmap(sbi);
1616         return init_victim_segmap(sbi);
1617 }
1618
1619 /*
1620  * Update min, max modified time for cost-benefit GC algorithm
1621  */
1622 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1623 {
1624         struct sit_info *sit_i = SIT_I(sbi);
1625         unsigned int segno;
1626
1627         mutex_lock(&sit_i->sentry_lock);
1628
1629         sit_i->min_mtime = LLONG_MAX;
1630
1631         for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1632                 unsigned int i;
1633                 unsigned long long mtime = 0;
1634
1635                 for (i = 0; i < sbi->segs_per_sec; i++)
1636                         mtime += get_seg_entry(sbi, segno + i)->mtime;
1637
1638                 mtime = div_u64(mtime, sbi->segs_per_sec);
1639
1640                 if (sit_i->min_mtime > mtime)
1641                         sit_i->min_mtime = mtime;
1642         }
1643         sit_i->max_mtime = get_mtime(sbi);
1644         mutex_unlock(&sit_i->sentry_lock);
1645 }
1646
1647 int build_segment_manager(struct f2fs_sb_info *sbi)
1648 {
1649         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1650         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1651         struct f2fs_sm_info *sm_info;
1652         int err;
1653
1654         sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1655         if (!sm_info)
1656                 return -ENOMEM;
1657
1658         /* init sm info */
1659         sbi->sm_info = sm_info;
1660         INIT_LIST_HEAD(&sm_info->wblist_head);
1661         spin_lock_init(&sm_info->wblist_lock);
1662         sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1663         sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1664         sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1665         sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1666         sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1667         sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1668         sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1669
1670         err = build_sit_info(sbi);
1671         if (err)
1672                 return err;
1673         err = build_free_segmap(sbi);
1674         if (err)
1675                 return err;
1676         err = build_curseg(sbi);
1677         if (err)
1678                 return err;
1679
1680         /* reinit free segmap based on SIT */
1681         build_sit_entries(sbi);
1682
1683         init_free_segmap(sbi);
1684         err = build_dirty_segmap(sbi);
1685         if (err)
1686                 return err;
1687
1688         init_min_max_mtime(sbi);
1689         return 0;
1690 }
1691
1692 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1693                 enum dirty_type dirty_type)
1694 {
1695         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1696
1697         mutex_lock(&dirty_i->seglist_lock);
1698         kfree(dirty_i->dirty_segmap[dirty_type]);
1699         dirty_i->nr_dirty[dirty_type] = 0;
1700         mutex_unlock(&dirty_i->seglist_lock);
1701 }
1702
1703 void reset_victim_segmap(struct f2fs_sb_info *sbi)
1704 {
1705         unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1706         memset(DIRTY_I(sbi)->victim_segmap[FG_GC], 0, bitmap_size);
1707 }
1708
1709 static void destroy_victim_segmap(struct f2fs_sb_info *sbi)
1710 {
1711         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1712
1713         kfree(dirty_i->victim_segmap[FG_GC]);
1714         kfree(dirty_i->victim_segmap[BG_GC]);
1715 }
1716
1717 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1718 {
1719         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1720         int i;
1721
1722         if (!dirty_i)
1723                 return;
1724
1725         /* discard pre-free/dirty segments list */
1726         for (i = 0; i < NR_DIRTY_TYPE; i++)
1727                 discard_dirty_segmap(sbi, i);
1728
1729         destroy_victim_segmap(sbi);
1730         SM_I(sbi)->dirty_info = NULL;
1731         kfree(dirty_i);
1732 }
1733
1734 static void destroy_curseg(struct f2fs_sb_info *sbi)
1735 {
1736         struct curseg_info *array = SM_I(sbi)->curseg_array;
1737         int i;
1738
1739         if (!array)
1740                 return;
1741         SM_I(sbi)->curseg_array = NULL;
1742         for (i = 0; i < NR_CURSEG_TYPE; i++)
1743                 kfree(array[i].sum_blk);
1744         kfree(array);
1745 }
1746
1747 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1748 {
1749         struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1750         if (!free_i)
1751                 return;
1752         SM_I(sbi)->free_info = NULL;
1753         kfree(free_i->free_segmap);
1754         kfree(free_i->free_secmap);
1755         kfree(free_i);
1756 }
1757
1758 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1759 {
1760         struct sit_info *sit_i = SIT_I(sbi);
1761         unsigned int start;
1762
1763         if (!sit_i)
1764                 return;
1765
1766         if (sit_i->sentries) {
1767                 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1768                         kfree(sit_i->sentries[start].cur_valid_map);
1769                         kfree(sit_i->sentries[start].ckpt_valid_map);
1770                 }
1771         }
1772         vfree(sit_i->sentries);
1773         vfree(sit_i->sec_entries);
1774         kfree(sit_i->dirty_sentries_bitmap);
1775
1776         SM_I(sbi)->sit_info = NULL;
1777         kfree(sit_i->sit_bitmap);
1778         kfree(sit_i);
1779 }
1780
1781 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1782 {
1783         struct f2fs_sm_info *sm_info = SM_I(sbi);
1784         destroy_dirty_segmap(sbi);
1785         destroy_curseg(sbi);
1786         destroy_free_segmap(sbi);
1787         destroy_sit_info(sbi);
1788         sbi->sm_info = NULL;
1789         kfree(sm_info);
1790 }