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