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