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