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