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