f2fs: allocate node and hot data in the beginning of partition
[linux-block.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 *discard_cmd_slab;
30 static struct kmem_cache *sit_entry_set_slab;
31 static struct kmem_cache *inmem_entry_slab;
32
33 static unsigned long __reverse_ulong(unsigned char *str)
34 {
35         unsigned long tmp = 0;
36         int shift = 24, idx = 0;
37
38 #if BITS_PER_LONG == 64
39         shift = 56;
40 #endif
41         while (shift >= 0) {
42                 tmp |= (unsigned long)str[idx++] << shift;
43                 shift -= BITS_PER_BYTE;
44         }
45         return tmp;
46 }
47
48 /*
49  * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50  * MSB and LSB are reversed in a byte by f2fs_set_bit.
51  */
52 static inline unsigned long __reverse_ffs(unsigned long word)
53 {
54         int num = 0;
55
56 #if BITS_PER_LONG == 64
57         if ((word & 0xffffffff00000000UL) == 0)
58                 num += 32;
59         else
60                 word >>= 32;
61 #endif
62         if ((word & 0xffff0000) == 0)
63                 num += 16;
64         else
65                 word >>= 16;
66
67         if ((word & 0xff00) == 0)
68                 num += 8;
69         else
70                 word >>= 8;
71
72         if ((word & 0xf0) == 0)
73                 num += 4;
74         else
75                 word >>= 4;
76
77         if ((word & 0xc) == 0)
78                 num += 2;
79         else
80                 word >>= 2;
81
82         if ((word & 0x2) == 0)
83                 num += 1;
84         return num;
85 }
86
87 /*
88  * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
89  * f2fs_set_bit makes MSB and LSB reversed in a byte.
90  * @size must be integral times of unsigned long.
91  * Example:
92  *                             MSB <--> LSB
93  *   f2fs_set_bit(0, bitmap) => 1000 0000
94  *   f2fs_set_bit(7, bitmap) => 0000 0001
95  */
96 static unsigned long __find_rev_next_bit(const unsigned long *addr,
97                         unsigned long size, unsigned long offset)
98 {
99         const unsigned long *p = addr + BIT_WORD(offset);
100         unsigned long result = size;
101         unsigned long tmp;
102
103         if (offset >= size)
104                 return size;
105
106         size -= (offset & ~(BITS_PER_LONG - 1));
107         offset %= BITS_PER_LONG;
108
109         while (1) {
110                 if (*p == 0)
111                         goto pass;
112
113                 tmp = __reverse_ulong((unsigned char *)p);
114
115                 tmp &= ~0UL >> offset;
116                 if (size < BITS_PER_LONG)
117                         tmp &= (~0UL << (BITS_PER_LONG - size));
118                 if (tmp)
119                         goto found;
120 pass:
121                 if (size <= BITS_PER_LONG)
122                         break;
123                 size -= BITS_PER_LONG;
124                 offset = 0;
125                 p++;
126         }
127         return result;
128 found:
129         return result - size + __reverse_ffs(tmp);
130 }
131
132 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
133                         unsigned long size, unsigned long offset)
134 {
135         const unsigned long *p = addr + BIT_WORD(offset);
136         unsigned long result = size;
137         unsigned long tmp;
138
139         if (offset >= size)
140                 return size;
141
142         size -= (offset & ~(BITS_PER_LONG - 1));
143         offset %= BITS_PER_LONG;
144
145         while (1) {
146                 if (*p == ~0UL)
147                         goto pass;
148
149                 tmp = __reverse_ulong((unsigned char *)p);
150
151                 if (offset)
152                         tmp |= ~0UL << (BITS_PER_LONG - offset);
153                 if (size < BITS_PER_LONG)
154                         tmp |= ~0UL >> size;
155                 if (tmp != ~0UL)
156                         goto found;
157 pass:
158                 if (size <= BITS_PER_LONG)
159                         break;
160                 size -= BITS_PER_LONG;
161                 offset = 0;
162                 p++;
163         }
164         return result;
165 found:
166         return result - size + __reverse_ffz(tmp);
167 }
168
169 void register_inmem_page(struct inode *inode, struct page *page)
170 {
171         struct f2fs_inode_info *fi = F2FS_I(inode);
172         struct inmem_pages *new;
173
174         f2fs_trace_pid(page);
175
176         set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
177         SetPagePrivate(page);
178
179         new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
180
181         /* add atomic page indices to the list */
182         new->page = page;
183         INIT_LIST_HEAD(&new->list);
184
185         /* increase reference count with clean state */
186         mutex_lock(&fi->inmem_lock);
187         get_page(page);
188         list_add_tail(&new->list, &fi->inmem_pages);
189         inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
190         mutex_unlock(&fi->inmem_lock);
191
192         trace_f2fs_register_inmem_page(page, INMEM);
193 }
194
195 static int __revoke_inmem_pages(struct inode *inode,
196                                 struct list_head *head, bool drop, bool recover)
197 {
198         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
199         struct inmem_pages *cur, *tmp;
200         int err = 0;
201
202         list_for_each_entry_safe(cur, tmp, head, list) {
203                 struct page *page = cur->page;
204
205                 if (drop)
206                         trace_f2fs_commit_inmem_page(page, INMEM_DROP);
207
208                 lock_page(page);
209
210                 if (recover) {
211                         struct dnode_of_data dn;
212                         struct node_info ni;
213
214                         trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
215
216                         set_new_dnode(&dn, inode, NULL, NULL, 0);
217                         if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
218                                 err = -EAGAIN;
219                                 goto next;
220                         }
221                         get_node_info(sbi, dn.nid, &ni);
222                         f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
223                                         cur->old_addr, ni.version, true, true);
224                         f2fs_put_dnode(&dn);
225                 }
226 next:
227                 /* we don't need to invalidate this in the sccessful status */
228                 if (drop || recover)
229                         ClearPageUptodate(page);
230                 set_page_private(page, 0);
231                 ClearPagePrivate(page);
232                 f2fs_put_page(page, 1);
233
234                 list_del(&cur->list);
235                 kmem_cache_free(inmem_entry_slab, cur);
236                 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
237         }
238         return err;
239 }
240
241 void drop_inmem_pages(struct inode *inode)
242 {
243         struct f2fs_inode_info *fi = F2FS_I(inode);
244
245         mutex_lock(&fi->inmem_lock);
246         __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
247         mutex_unlock(&fi->inmem_lock);
248
249         clear_inode_flag(inode, FI_ATOMIC_FILE);
250         stat_dec_atomic_write(inode);
251 }
252
253 void drop_inmem_page(struct inode *inode, struct page *page)
254 {
255         struct f2fs_inode_info *fi = F2FS_I(inode);
256         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257         struct list_head *head = &fi->inmem_pages;
258         struct inmem_pages *cur = NULL;
259
260         f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
261
262         mutex_lock(&fi->inmem_lock);
263         list_for_each_entry(cur, head, list) {
264                 if (cur->page == page)
265                         break;
266         }
267
268         f2fs_bug_on(sbi, !cur || cur->page != page);
269         list_del(&cur->list);
270         mutex_unlock(&fi->inmem_lock);
271
272         dec_page_count(sbi, F2FS_INMEM_PAGES);
273         kmem_cache_free(inmem_entry_slab, cur);
274
275         ClearPageUptodate(page);
276         set_page_private(page, 0);
277         ClearPagePrivate(page);
278         f2fs_put_page(page, 0);
279
280         trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
281 }
282
283 static int __commit_inmem_pages(struct inode *inode,
284                                         struct list_head *revoke_list)
285 {
286         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
287         struct f2fs_inode_info *fi = F2FS_I(inode);
288         struct inmem_pages *cur, *tmp;
289         struct f2fs_io_info fio = {
290                 .sbi = sbi,
291                 .type = DATA,
292                 .op = REQ_OP_WRITE,
293                 .op_flags = REQ_SYNC | REQ_PRIO,
294                 .encrypted_page = NULL,
295         };
296         pgoff_t last_idx = ULONG_MAX;
297         int err = 0;
298
299         list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
300                 struct page *page = cur->page;
301
302                 lock_page(page);
303                 if (page->mapping == inode->i_mapping) {
304                         trace_f2fs_commit_inmem_page(page, INMEM);
305
306                         set_page_dirty(page);
307                         f2fs_wait_on_page_writeback(page, DATA, true);
308                         if (clear_page_dirty_for_io(page)) {
309                                 inode_dec_dirty_pages(inode);
310                                 remove_dirty_inode(inode);
311                         }
312
313                         fio.page = page;
314                         err = do_write_data_page(&fio);
315                         if (err) {
316                                 unlock_page(page);
317                                 break;
318                         }
319
320                         /* record old blkaddr for revoking */
321                         cur->old_addr = fio.old_blkaddr;
322                         last_idx = page->index;
323                 }
324                 unlock_page(page);
325                 list_move_tail(&cur->list, revoke_list);
326         }
327
328         if (last_idx != ULONG_MAX)
329                 f2fs_submit_merged_bio_cond(sbi, inode, 0, last_idx,
330                                                         DATA, WRITE);
331
332         if (!err)
333                 __revoke_inmem_pages(inode, revoke_list, false, false);
334
335         return err;
336 }
337
338 int commit_inmem_pages(struct inode *inode)
339 {
340         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
341         struct f2fs_inode_info *fi = F2FS_I(inode);
342         struct list_head revoke_list;
343         int err;
344
345         INIT_LIST_HEAD(&revoke_list);
346         f2fs_balance_fs(sbi, true);
347         f2fs_lock_op(sbi);
348
349         set_inode_flag(inode, FI_ATOMIC_COMMIT);
350
351         mutex_lock(&fi->inmem_lock);
352         err = __commit_inmem_pages(inode, &revoke_list);
353         if (err) {
354                 int ret;
355                 /*
356                  * try to revoke all committed pages, but still we could fail
357                  * due to no memory or other reason, if that happened, EAGAIN
358                  * will be returned, which means in such case, transaction is
359                  * already not integrity, caller should use journal to do the
360                  * recovery or rewrite & commit last transaction. For other
361                  * error number, revoking was done by filesystem itself.
362                  */
363                 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
364                 if (ret)
365                         err = ret;
366
367                 /* drop all uncommitted pages */
368                 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
369         }
370         mutex_unlock(&fi->inmem_lock);
371
372         clear_inode_flag(inode, FI_ATOMIC_COMMIT);
373
374         f2fs_unlock_op(sbi);
375         return err;
376 }
377
378 /*
379  * This function balances dirty node and dentry pages.
380  * In addition, it controls garbage collection.
381  */
382 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
383 {
384 #ifdef CONFIG_F2FS_FAULT_INJECTION
385         if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
386                 f2fs_show_injection_info(FAULT_CHECKPOINT);
387                 f2fs_stop_checkpoint(sbi, false);
388         }
389 #endif
390
391         if (!need)
392                 return;
393
394         /* balance_fs_bg is able to be pending */
395         if (excess_cached_nats(sbi))
396                 f2fs_balance_fs_bg(sbi);
397
398         /*
399          * We should do GC or end up with checkpoint, if there are so many dirty
400          * dir/node pages without enough free segments.
401          */
402         if (has_not_enough_free_secs(sbi, 0, 0)) {
403                 mutex_lock(&sbi->gc_mutex);
404                 f2fs_gc(sbi, false, false);
405         }
406 }
407
408 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
409 {
410         /* try to shrink extent cache when there is no enough memory */
411         if (!available_free_memory(sbi, EXTENT_CACHE))
412                 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
413
414         /* check the # of cached NAT entries */
415         if (!available_free_memory(sbi, NAT_ENTRIES))
416                 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
417
418         if (!available_free_memory(sbi, FREE_NIDS))
419                 try_to_free_nids(sbi, MAX_FREE_NIDS);
420         else
421                 build_free_nids(sbi, false, false);
422
423         if (!is_idle(sbi))
424                 return;
425
426         /* checkpoint is the only way to shrink partial cached entries */
427         if (!available_free_memory(sbi, NAT_ENTRIES) ||
428                         !available_free_memory(sbi, INO_ENTRIES) ||
429                         excess_prefree_segs(sbi) ||
430                         excess_dirty_nats(sbi) ||
431                         f2fs_time_over(sbi, CP_TIME)) {
432                 if (test_opt(sbi, DATA_FLUSH)) {
433                         struct blk_plug plug;
434
435                         blk_start_plug(&plug);
436                         sync_dirty_inodes(sbi, FILE_INODE);
437                         blk_finish_plug(&plug);
438                 }
439                 f2fs_sync_fs(sbi->sb, true);
440                 stat_inc_bg_cp_count(sbi->stat_info);
441         }
442 }
443
444 static int __submit_flush_wait(struct f2fs_sb_info *sbi,
445                                 struct block_device *bdev)
446 {
447         struct bio *bio = f2fs_bio_alloc(0);
448         int ret;
449
450         bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
451         bio->bi_bdev = bdev;
452         ret = submit_bio_wait(bio);
453         bio_put(bio);
454
455         trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
456                                 test_opt(sbi, FLUSH_MERGE), ret);
457         return ret;
458 }
459
460 static int submit_flush_wait(struct f2fs_sb_info *sbi)
461 {
462         int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev);
463         int i;
464
465         if (!sbi->s_ndevs || ret)
466                 return ret;
467
468         for (i = 1; i < sbi->s_ndevs; i++) {
469                 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
470                 if (ret)
471                         break;
472         }
473         return ret;
474 }
475
476 static int issue_flush_thread(void *data)
477 {
478         struct f2fs_sb_info *sbi = data;
479         struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
480         wait_queue_head_t *q = &fcc->flush_wait_queue;
481 repeat:
482         if (kthread_should_stop())
483                 return 0;
484
485         if (!llist_empty(&fcc->issue_list)) {
486                 struct flush_cmd *cmd, *next;
487                 int ret;
488
489                 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
490                 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
491
492                 ret = submit_flush_wait(sbi);
493                 llist_for_each_entry_safe(cmd, next,
494                                           fcc->dispatch_list, llnode) {
495                         cmd->ret = ret;
496                         complete(&cmd->wait);
497                 }
498                 fcc->dispatch_list = NULL;
499         }
500
501         wait_event_interruptible(*q,
502                 kthread_should_stop() || !llist_empty(&fcc->issue_list));
503         goto repeat;
504 }
505
506 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
507 {
508         struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
509         struct flush_cmd cmd;
510
511         if (test_opt(sbi, NOBARRIER))
512                 return 0;
513
514         if (!test_opt(sbi, FLUSH_MERGE))
515                 return submit_flush_wait(sbi);
516
517         if (!atomic_read(&fcc->submit_flush)) {
518                 int ret;
519
520                 atomic_inc(&fcc->submit_flush);
521                 ret = submit_flush_wait(sbi);
522                 atomic_dec(&fcc->submit_flush);
523                 return ret;
524         }
525
526         init_completion(&cmd.wait);
527
528         atomic_inc(&fcc->submit_flush);
529         llist_add(&cmd.llnode, &fcc->issue_list);
530
531         if (!fcc->dispatch_list)
532                 wake_up(&fcc->flush_wait_queue);
533
534         if (fcc->f2fs_issue_flush) {
535                 wait_for_completion(&cmd.wait);
536                 atomic_dec(&fcc->submit_flush);
537         } else {
538                 llist_del_all(&fcc->issue_list);
539                 atomic_set(&fcc->submit_flush, 0);
540         }
541
542         return cmd.ret;
543 }
544
545 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
546 {
547         dev_t dev = sbi->sb->s_bdev->bd_dev;
548         struct flush_cmd_control *fcc;
549         int err = 0;
550
551         if (SM_I(sbi)->fcc_info) {
552                 fcc = SM_I(sbi)->fcc_info;
553                 goto init_thread;
554         }
555
556         fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
557         if (!fcc)
558                 return -ENOMEM;
559         atomic_set(&fcc->submit_flush, 0);
560         init_waitqueue_head(&fcc->flush_wait_queue);
561         init_llist_head(&fcc->issue_list);
562         SM_I(sbi)->fcc_info = fcc;
563 init_thread:
564         fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
565                                 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
566         if (IS_ERR(fcc->f2fs_issue_flush)) {
567                 err = PTR_ERR(fcc->f2fs_issue_flush);
568                 kfree(fcc);
569                 SM_I(sbi)->fcc_info = NULL;
570                 return err;
571         }
572
573         return err;
574 }
575
576 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
577 {
578         struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
579
580         if (fcc && fcc->f2fs_issue_flush) {
581                 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
582
583                 fcc->f2fs_issue_flush = NULL;
584                 kthread_stop(flush_thread);
585         }
586         if (free) {
587                 kfree(fcc);
588                 SM_I(sbi)->fcc_info = NULL;
589         }
590 }
591
592 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
593                 enum dirty_type dirty_type)
594 {
595         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
596
597         /* need not be added */
598         if (IS_CURSEG(sbi, segno))
599                 return;
600
601         if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
602                 dirty_i->nr_dirty[dirty_type]++;
603
604         if (dirty_type == DIRTY) {
605                 struct seg_entry *sentry = get_seg_entry(sbi, segno);
606                 enum dirty_type t = sentry->type;
607
608                 if (unlikely(t >= DIRTY)) {
609                         f2fs_bug_on(sbi, 1);
610                         return;
611                 }
612                 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
613                         dirty_i->nr_dirty[t]++;
614         }
615 }
616
617 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
618                 enum dirty_type dirty_type)
619 {
620         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
621
622         if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
623                 dirty_i->nr_dirty[dirty_type]--;
624
625         if (dirty_type == DIRTY) {
626                 struct seg_entry *sentry = get_seg_entry(sbi, segno);
627                 enum dirty_type t = sentry->type;
628
629                 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
630                         dirty_i->nr_dirty[t]--;
631
632                 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
633                         clear_bit(GET_SECNO(sbi, segno),
634                                                 dirty_i->victim_secmap);
635         }
636 }
637
638 /*
639  * Should not occur error such as -ENOMEM.
640  * Adding dirty entry into seglist is not critical operation.
641  * If a given segment is one of current working segments, it won't be added.
642  */
643 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
644 {
645         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
646         unsigned short valid_blocks;
647
648         if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
649                 return;
650
651         mutex_lock(&dirty_i->seglist_lock);
652
653         valid_blocks = get_valid_blocks(sbi, segno, 0);
654
655         if (valid_blocks == 0) {
656                 __locate_dirty_segment(sbi, segno, PRE);
657                 __remove_dirty_segment(sbi, segno, DIRTY);
658         } else if (valid_blocks < sbi->blocks_per_seg) {
659                 __locate_dirty_segment(sbi, segno, DIRTY);
660         } else {
661                 /* Recovery routine with SSR needs this */
662                 __remove_dirty_segment(sbi, segno, DIRTY);
663         }
664
665         mutex_unlock(&dirty_i->seglist_lock);
666 }
667
668 static void __add_discard_cmd(struct f2fs_sb_info *sbi,
669                 struct block_device *bdev, block_t lstart,
670                 block_t start, block_t len)
671 {
672         struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
673         struct list_head *cmd_list = &(dcc->discard_cmd_list);
674         struct discard_cmd *dc;
675
676         dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
677         INIT_LIST_HEAD(&dc->list);
678         dc->bdev = bdev;
679         dc->lstart = lstart;
680         dc->start = start;
681         dc->len = len;
682         dc->state = D_PREP;
683         dc->error = 0;
684         init_completion(&dc->wait);
685
686         mutex_lock(&dcc->cmd_lock);
687         list_add_tail(&dc->list, cmd_list);
688         mutex_unlock(&dcc->cmd_lock);
689 }
690
691 static void __remove_discard_cmd(struct f2fs_sb_info *sbi, struct discard_cmd *dc)
692 {
693         if (dc->state == D_DONE)
694                 atomic_dec(&(SM_I(sbi)->dcc_info->submit_discard));
695
696         if (dc->error == -EOPNOTSUPP)
697                 dc->error = 0;
698
699         if (dc->error)
700                 f2fs_msg(sbi->sb, KERN_INFO,
701                                 "Issue discard failed, ret: %d", dc->error);
702         list_del(&dc->list);
703         kmem_cache_free(discard_cmd_slab, dc);
704 }
705
706 static void f2fs_submit_discard_endio(struct bio *bio)
707 {
708         struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
709
710         complete(&dc->wait);
711         dc->error = bio->bi_error;
712         dc->state = D_DONE;
713         bio_put(bio);
714 }
715
716 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
717 static void __submit_discard_cmd(struct f2fs_sb_info *sbi,
718                                 struct discard_cmd *dc)
719 {
720         struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
721         struct bio *bio = NULL;
722
723         if (dc->state != D_PREP)
724                 return;
725
726         dc->error = __blkdev_issue_discard(dc->bdev,
727                                 SECTOR_FROM_BLOCK(dc->start),
728                                 SECTOR_FROM_BLOCK(dc->len),
729                                 GFP_NOFS, 0, &bio);
730         if (!dc->error) {
731                 /* should keep before submission to avoid D_DONE right away */
732                 dc->state = D_SUBMIT;
733                 atomic_inc(&dcc->submit_discard);
734                 if (bio) {
735                         bio->bi_private = dc;
736                         bio->bi_end_io = f2fs_submit_discard_endio;
737                         bio->bi_opf |= REQ_SYNC;
738                         submit_bio(bio);
739                 }
740         } else {
741                 __remove_discard_cmd(sbi, dc);
742         }
743 }
744
745 static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
746                 struct block_device *bdev, block_t blkstart, block_t blklen)
747 {
748         block_t lblkstart = blkstart;
749
750         trace_f2fs_issue_discard(bdev, blkstart, blklen);
751
752         if (sbi->s_ndevs) {
753                 int devi = f2fs_target_device_index(sbi, blkstart);
754
755                 blkstart -= FDEV(devi).start_blk;
756         }
757         __add_discard_cmd(sbi, bdev, lblkstart, blkstart, blklen);
758         wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue);
759         return 0;
760 }
761
762 static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
763                                 struct discard_cmd *dc, block_t blkaddr)
764 {
765         block_t end_block = START_BLOCK(sbi, GET_SEGNO(sbi, blkaddr) + 1);
766
767         if (dc->state == D_DONE || dc->lstart + dc->len <= end_block) {
768                 __remove_discard_cmd(sbi, dc);
769                 return;
770         }
771
772         if (blkaddr - dc->lstart < dc->lstart + dc->len - end_block) {
773                 dc->start += (end_block - dc->lstart);
774                 dc->len -= (end_block - dc->lstart);
775                 dc->lstart = end_block;
776         } else {
777                 dc->len = blkaddr - dc->lstart;
778         }
779 }
780
781 /* This should be covered by global mutex, &sit_i->sentry_lock */
782 void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
783 {
784         struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
785         struct list_head *wait_list = &(dcc->discard_cmd_list);
786         struct discard_cmd *dc, *tmp;
787         struct blk_plug plug;
788
789         mutex_lock(&dcc->cmd_lock);
790
791         blk_start_plug(&plug);
792
793         list_for_each_entry_safe(dc, tmp, wait_list, list) {
794
795                 if (blkaddr == NULL_ADDR) {
796                         __submit_discard_cmd(sbi, dc);
797                         continue;
798                 }
799
800                 if (dc->lstart <= blkaddr && blkaddr < dc->lstart + dc->len) {
801                         if (dc->state == D_SUBMIT)
802                                 wait_for_completion_io(&dc->wait);
803                         __punch_discard_cmd(sbi, dc, blkaddr);
804                 }
805         }
806         blk_finish_plug(&plug);
807
808         /* this comes from f2fs_put_super */
809         if (blkaddr == NULL_ADDR) {
810                 list_for_each_entry_safe(dc, tmp, wait_list, list) {
811                         wait_for_completion_io(&dc->wait);
812                         __remove_discard_cmd(sbi, dc);
813                 }
814         }
815         mutex_unlock(&dcc->cmd_lock);
816 }
817
818 static int issue_discard_thread(void *data)
819 {
820         struct f2fs_sb_info *sbi = data;
821         struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
822         wait_queue_head_t *q = &dcc->discard_wait_queue;
823         struct list_head *cmd_list = &dcc->discard_cmd_list;
824         struct discard_cmd *dc, *tmp;
825         struct blk_plug plug;
826         int iter = 0;
827 repeat:
828         if (kthread_should_stop())
829                 return 0;
830
831         blk_start_plug(&plug);
832
833         mutex_lock(&dcc->cmd_lock);
834         list_for_each_entry_safe(dc, tmp, cmd_list, list) {
835
836                 if (is_idle(sbi))
837                         __submit_discard_cmd(sbi, dc);
838
839                 if (dc->state == D_PREP && iter++ > DISCARD_ISSUE_RATE)
840                         break;
841                 if (dc->state == D_DONE)
842                         __remove_discard_cmd(sbi, dc);
843         }
844         mutex_unlock(&dcc->cmd_lock);
845
846         blk_finish_plug(&plug);
847
848         iter = 0;
849         congestion_wait(BLK_RW_SYNC, HZ/50);
850
851         wait_event_interruptible(*q,
852                 kthread_should_stop() || !list_empty(&dcc->discard_cmd_list));
853         goto repeat;
854 }
855
856 #ifdef CONFIG_BLK_DEV_ZONED
857 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
858                 struct block_device *bdev, block_t blkstart, block_t blklen)
859 {
860         sector_t sector, nr_sects;
861         block_t lblkstart = blkstart;
862         int devi = 0;
863
864         if (sbi->s_ndevs) {
865                 devi = f2fs_target_device_index(sbi, blkstart);
866                 blkstart -= FDEV(devi).start_blk;
867         }
868
869         /*
870          * We need to know the type of the zone: for conventional zones,
871          * use regular discard if the drive supports it. For sequential
872          * zones, reset the zone write pointer.
873          */
874         switch (get_blkz_type(sbi, bdev, blkstart)) {
875
876         case BLK_ZONE_TYPE_CONVENTIONAL:
877                 if (!blk_queue_discard(bdev_get_queue(bdev)))
878                         return 0;
879                 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
880         case BLK_ZONE_TYPE_SEQWRITE_REQ:
881         case BLK_ZONE_TYPE_SEQWRITE_PREF:
882                 sector = SECTOR_FROM_BLOCK(blkstart);
883                 nr_sects = SECTOR_FROM_BLOCK(blklen);
884
885                 if (sector & (bdev_zone_sectors(bdev) - 1) ||
886                                 nr_sects != bdev_zone_sectors(bdev)) {
887                         f2fs_msg(sbi->sb, KERN_INFO,
888                                 "(%d) %s: Unaligned discard attempted (block %x + %x)",
889                                 devi, sbi->s_ndevs ? FDEV(devi).path: "",
890                                 blkstart, blklen);
891                         return -EIO;
892                 }
893                 trace_f2fs_issue_reset_zone(bdev, blkstart);
894                 return blkdev_reset_zones(bdev, sector,
895                                           nr_sects, GFP_NOFS);
896         default:
897                 /* Unknown zone type: broken device ? */
898                 return -EIO;
899         }
900 }
901 #endif
902
903 static int __issue_discard_async(struct f2fs_sb_info *sbi,
904                 struct block_device *bdev, block_t blkstart, block_t blklen)
905 {
906 #ifdef CONFIG_BLK_DEV_ZONED
907         if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
908                                 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
909                 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
910 #endif
911         return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
912 }
913
914 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
915                                 block_t blkstart, block_t blklen)
916 {
917         sector_t start = blkstart, len = 0;
918         struct block_device *bdev;
919         struct seg_entry *se;
920         unsigned int offset;
921         block_t i;
922         int err = 0;
923
924         bdev = f2fs_target_device(sbi, blkstart, NULL);
925
926         for (i = blkstart; i < blkstart + blklen; i++, len++) {
927                 if (i != start) {
928                         struct block_device *bdev2 =
929                                 f2fs_target_device(sbi, i, NULL);
930
931                         if (bdev2 != bdev) {
932                                 err = __issue_discard_async(sbi, bdev,
933                                                 start, len);
934                                 if (err)
935                                         return err;
936                                 bdev = bdev2;
937                                 start = i;
938                                 len = 0;
939                         }
940                 }
941
942                 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
943                 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
944
945                 if (!f2fs_test_and_set_bit(offset, se->discard_map))
946                         sbi->discard_blks--;
947         }
948
949         if (len)
950                 err = __issue_discard_async(sbi, bdev, start, len);
951         return err;
952 }
953
954 static void __add_discard_entry(struct f2fs_sb_info *sbi,
955                 struct cp_control *cpc, struct seg_entry *se,
956                 unsigned int start, unsigned int end)
957 {
958         struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list;
959         struct discard_entry *new, *last;
960
961         if (!list_empty(head)) {
962                 last = list_last_entry(head, struct discard_entry, list);
963                 if (START_BLOCK(sbi, cpc->trim_start) + start ==
964                                 last->blkaddr + last->len &&
965                                 last->len < MAX_DISCARD_BLOCKS(sbi)) {
966                         last->len += end - start;
967                         goto done;
968                 }
969         }
970
971         new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
972         INIT_LIST_HEAD(&new->list);
973         new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
974         new->len = end - start;
975         list_add_tail(&new->list, head);
976 done:
977         SM_I(sbi)->dcc_info->nr_discards += end - start;
978 }
979
980 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
981                                                         bool check_only)
982 {
983         int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
984         int max_blocks = sbi->blocks_per_seg;
985         struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
986         unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
987         unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
988         unsigned long *discard_map = (unsigned long *)se->discard_map;
989         unsigned long *dmap = SIT_I(sbi)->tmp_map;
990         unsigned int start = 0, end = -1;
991         bool force = (cpc->reason == CP_DISCARD);
992         int i;
993
994         if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
995                 return false;
996
997         if (!force) {
998                 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
999                         SM_I(sbi)->dcc_info->nr_discards >=
1000                                 SM_I(sbi)->dcc_info->max_discards)
1001                         return false;
1002         }
1003
1004         /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1005         for (i = 0; i < entries; i++)
1006                 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
1007                                 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
1008
1009         while (force || SM_I(sbi)->dcc_info->nr_discards <=
1010                                 SM_I(sbi)->dcc_info->max_discards) {
1011                 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1012                 if (start >= max_blocks)
1013                         break;
1014
1015                 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
1016                 if (force && start && end != max_blocks
1017                                         && (end - start) < cpc->trim_minlen)
1018                         continue;
1019
1020                 if (check_only)
1021                         return true;
1022
1023                 __add_discard_entry(sbi, cpc, se, start, end);
1024         }
1025         return false;
1026 }
1027
1028 void release_discard_addrs(struct f2fs_sb_info *sbi)
1029 {
1030         struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
1031         struct discard_entry *entry, *this;
1032
1033         /* drop caches */
1034         list_for_each_entry_safe(entry, this, head, list) {
1035                 list_del(&entry->list);
1036                 kmem_cache_free(discard_entry_slab, entry);
1037         }
1038 }
1039
1040 /*
1041  * Should call clear_prefree_segments after checkpoint is done.
1042  */
1043 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1044 {
1045         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1046         unsigned int segno;
1047
1048         mutex_lock(&dirty_i->seglist_lock);
1049         for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
1050                 __set_test_and_free(sbi, segno);
1051         mutex_unlock(&dirty_i->seglist_lock);
1052 }
1053
1054 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1055 {
1056         struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
1057         struct discard_entry *entry, *this;
1058         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1059         unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
1060         unsigned int start = 0, end = -1;
1061         unsigned int secno, start_segno;
1062         bool force = (cpc->reason == CP_DISCARD);
1063
1064         mutex_lock(&dirty_i->seglist_lock);
1065
1066         while (1) {
1067                 int i;
1068                 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1069                 if (start >= MAIN_SEGS(sbi))
1070                         break;
1071                 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1072                                                                 start + 1);
1073
1074                 for (i = start; i < end; i++)
1075                         clear_bit(i, prefree_map);
1076
1077                 dirty_i->nr_dirty[PRE] -= end - start;
1078
1079                 if (!test_opt(sbi, DISCARD))
1080                         continue;
1081
1082                 if (force && start >= cpc->trim_start &&
1083                                         (end - 1) <= cpc->trim_end)
1084                                 continue;
1085
1086                 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1087                         f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
1088                                 (end - start) << sbi->log_blocks_per_seg);
1089                         continue;
1090                 }
1091 next:
1092                 secno = GET_SECNO(sbi, start);
1093                 start_segno = secno * sbi->segs_per_sec;
1094                 if (!IS_CURSEC(sbi, secno) &&
1095                         !get_valid_blocks(sbi, start, sbi->segs_per_sec))
1096                         f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1097                                 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1098
1099                 start = start_segno + sbi->segs_per_sec;
1100                 if (start < end)
1101                         goto next;
1102                 else
1103                         end = start - 1;
1104         }
1105         mutex_unlock(&dirty_i->seglist_lock);
1106
1107         /* send small discards */
1108         list_for_each_entry_safe(entry, this, head, list) {
1109                 if (force && entry->len < cpc->trim_minlen)
1110                         goto skip;
1111                 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
1112                 cpc->trimmed += entry->len;
1113 skip:
1114                 list_del(&entry->list);
1115                 SM_I(sbi)->dcc_info->nr_discards -= entry->len;
1116                 kmem_cache_free(discard_entry_slab, entry);
1117         }
1118 }
1119
1120 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1121 {
1122         dev_t dev = sbi->sb->s_bdev->bd_dev;
1123         struct discard_cmd_control *dcc;
1124         int err = 0;
1125
1126         if (SM_I(sbi)->dcc_info) {
1127                 dcc = SM_I(sbi)->dcc_info;
1128                 goto init_thread;
1129         }
1130
1131         dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1132         if (!dcc)
1133                 return -ENOMEM;
1134
1135         INIT_LIST_HEAD(&dcc->discard_entry_list);
1136         INIT_LIST_HEAD(&dcc->discard_cmd_list);
1137         mutex_init(&dcc->cmd_lock);
1138         atomic_set(&dcc->submit_discard, 0);
1139         dcc->nr_discards = 0;
1140         dcc->max_discards = 0;
1141
1142         init_waitqueue_head(&dcc->discard_wait_queue);
1143         SM_I(sbi)->dcc_info = dcc;
1144 init_thread:
1145         dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1146                                 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1147         if (IS_ERR(dcc->f2fs_issue_discard)) {
1148                 err = PTR_ERR(dcc->f2fs_issue_discard);
1149                 kfree(dcc);
1150                 SM_I(sbi)->dcc_info = NULL;
1151                 return err;
1152         }
1153
1154         return err;
1155 }
1156
1157 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi, bool free)
1158 {
1159         struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1160
1161         if (dcc && dcc->f2fs_issue_discard) {
1162                 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1163
1164                 dcc->f2fs_issue_discard = NULL;
1165                 kthread_stop(discard_thread);
1166         }
1167         if (free) {
1168                 kfree(dcc);
1169                 SM_I(sbi)->dcc_info = NULL;
1170         }
1171 }
1172
1173 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
1174 {
1175         struct sit_info *sit_i = SIT_I(sbi);
1176
1177         if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
1178                 sit_i->dirty_sentries++;
1179                 return false;
1180         }
1181
1182         return true;
1183 }
1184
1185 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1186                                         unsigned int segno, int modified)
1187 {
1188         struct seg_entry *se = get_seg_entry(sbi, segno);
1189         se->type = type;
1190         if (modified)
1191                 __mark_sit_entry_dirty(sbi, segno);
1192 }
1193
1194 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1195 {
1196         struct seg_entry *se;
1197         unsigned int segno, offset;
1198         long int new_vblocks;
1199
1200         segno = GET_SEGNO(sbi, blkaddr);
1201
1202         se = get_seg_entry(sbi, segno);
1203         new_vblocks = se->valid_blocks + del;
1204         offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1205
1206         f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
1207                                 (new_vblocks > sbi->blocks_per_seg)));
1208
1209         se->valid_blocks = new_vblocks;
1210         se->mtime = get_mtime(sbi);
1211         SIT_I(sbi)->max_mtime = se->mtime;
1212
1213         /* Update valid block bitmap */
1214         if (del > 0) {
1215                 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) {
1216 #ifdef CONFIG_F2FS_CHECK_FS
1217                         if (f2fs_test_and_set_bit(offset,
1218                                                 se->cur_valid_map_mir))
1219                                 f2fs_bug_on(sbi, 1);
1220                         else
1221                                 WARN_ON(1);
1222 #else
1223                         f2fs_bug_on(sbi, 1);
1224 #endif
1225                 }
1226                 if (f2fs_discard_en(sbi) &&
1227                         !f2fs_test_and_set_bit(offset, se->discard_map))
1228                         sbi->discard_blks--;
1229
1230                 /* don't overwrite by SSR to keep node chain */
1231                 if (se->type == CURSEG_WARM_NODE) {
1232                         if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
1233                                 se->ckpt_valid_blocks++;
1234                 }
1235         } else {
1236                 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) {
1237 #ifdef CONFIG_F2FS_CHECK_FS
1238                         if (!f2fs_test_and_clear_bit(offset,
1239                                                 se->cur_valid_map_mir))
1240                                 f2fs_bug_on(sbi, 1);
1241                         else
1242                                 WARN_ON(1);
1243 #else
1244                         f2fs_bug_on(sbi, 1);
1245 #endif
1246                 }
1247                 if (f2fs_discard_en(sbi) &&
1248                         f2fs_test_and_clear_bit(offset, se->discard_map))
1249                         sbi->discard_blks++;
1250         }
1251         if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1252                 se->ckpt_valid_blocks += del;
1253
1254         __mark_sit_entry_dirty(sbi, segno);
1255
1256         /* update total number of valid blocks to be written in ckpt area */
1257         SIT_I(sbi)->written_valid_blocks += del;
1258
1259         if (sbi->segs_per_sec > 1)
1260                 get_sec_entry(sbi, segno)->valid_blocks += del;
1261 }
1262
1263 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
1264 {
1265         update_sit_entry(sbi, new, 1);
1266         if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1267                 update_sit_entry(sbi, old, -1);
1268
1269         locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1270         locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
1271 }
1272
1273 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1274 {
1275         unsigned int segno = GET_SEGNO(sbi, addr);
1276         struct sit_info *sit_i = SIT_I(sbi);
1277
1278         f2fs_bug_on(sbi, addr == NULL_ADDR);
1279         if (addr == NEW_ADDR)
1280                 return;
1281
1282         /* add it into sit main buffer */
1283         mutex_lock(&sit_i->sentry_lock);
1284
1285         update_sit_entry(sbi, addr, -1);
1286
1287         /* add it into dirty seglist */
1288         locate_dirty_segment(sbi, segno);
1289
1290         mutex_unlock(&sit_i->sentry_lock);
1291 }
1292
1293 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1294 {
1295         struct sit_info *sit_i = SIT_I(sbi);
1296         unsigned int segno, offset;
1297         struct seg_entry *se;
1298         bool is_cp = false;
1299
1300         if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1301                 return true;
1302
1303         mutex_lock(&sit_i->sentry_lock);
1304
1305         segno = GET_SEGNO(sbi, blkaddr);
1306         se = get_seg_entry(sbi, segno);
1307         offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1308
1309         if (f2fs_test_bit(offset, se->ckpt_valid_map))
1310                 is_cp = true;
1311
1312         mutex_unlock(&sit_i->sentry_lock);
1313
1314         return is_cp;
1315 }
1316
1317 /*
1318  * This function should be resided under the curseg_mutex lock
1319  */
1320 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
1321                                         struct f2fs_summary *sum)
1322 {
1323         struct curseg_info *curseg = CURSEG_I(sbi, type);
1324         void *addr = curseg->sum_blk;
1325         addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
1326         memcpy(addr, sum, sizeof(struct f2fs_summary));
1327 }
1328
1329 /*
1330  * Calculate the number of current summary pages for writing
1331  */
1332 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
1333 {
1334         int valid_sum_count = 0;
1335         int i, sum_in_page;
1336
1337         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1338                 if (sbi->ckpt->alloc_type[i] == SSR)
1339                         valid_sum_count += sbi->blocks_per_seg;
1340                 else {
1341                         if (for_ra)
1342                                 valid_sum_count += le16_to_cpu(
1343                                         F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1344                         else
1345                                 valid_sum_count += curseg_blkoff(sbi, i);
1346                 }
1347         }
1348
1349         sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
1350                         SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1351         if (valid_sum_count <= sum_in_page)
1352                 return 1;
1353         else if ((valid_sum_count - sum_in_page) <=
1354                 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
1355                 return 2;
1356         return 3;
1357 }
1358
1359 /*
1360  * Caller should put this summary page
1361  */
1362 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1363 {
1364         return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1365 }
1366
1367 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1368 {
1369         struct page *page = grab_meta_page(sbi, blk_addr);
1370         void *dst = page_address(page);
1371
1372         if (src)
1373                 memcpy(dst, src, PAGE_SIZE);
1374         else
1375                 memset(dst, 0, PAGE_SIZE);
1376         set_page_dirty(page);
1377         f2fs_put_page(page, 1);
1378 }
1379
1380 static void write_sum_page(struct f2fs_sb_info *sbi,
1381                         struct f2fs_summary_block *sum_blk, block_t blk_addr)
1382 {
1383         update_meta_page(sbi, (void *)sum_blk, blk_addr);
1384 }
1385
1386 static void write_current_sum_page(struct f2fs_sb_info *sbi,
1387                                                 int type, block_t blk_addr)
1388 {
1389         struct curseg_info *curseg = CURSEG_I(sbi, type);
1390         struct page *page = grab_meta_page(sbi, blk_addr);
1391         struct f2fs_summary_block *src = curseg->sum_blk;
1392         struct f2fs_summary_block *dst;
1393
1394         dst = (struct f2fs_summary_block *)page_address(page);
1395
1396         mutex_lock(&curseg->curseg_mutex);
1397
1398         down_read(&curseg->journal_rwsem);
1399         memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1400         up_read(&curseg->journal_rwsem);
1401
1402         memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1403         memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1404
1405         mutex_unlock(&curseg->curseg_mutex);
1406
1407         set_page_dirty(page);
1408         f2fs_put_page(page, 1);
1409 }
1410
1411 /*
1412  * Find a new segment from the free segments bitmap to right order
1413  * This function should be returned with success, otherwise BUG
1414  */
1415 static void get_new_segment(struct f2fs_sb_info *sbi,
1416                         unsigned int *newseg, bool new_sec, int dir)
1417 {
1418         struct free_segmap_info *free_i = FREE_I(sbi);
1419         unsigned int segno, secno, zoneno;
1420         unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
1421         unsigned int hint = *newseg / sbi->segs_per_sec;
1422         unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
1423         unsigned int left_start = hint;
1424         bool init = true;
1425         int go_left = 0;
1426         int i;
1427
1428         spin_lock(&free_i->segmap_lock);
1429
1430         if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1431                 segno = find_next_zero_bit(free_i->free_segmap,
1432                                 (hint + 1) * sbi->segs_per_sec, *newseg + 1);
1433                 if (segno < (hint + 1) * sbi->segs_per_sec)
1434                         goto got_it;
1435         }
1436 find_other_zone:
1437         secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1438         if (secno >= MAIN_SECS(sbi)) {
1439                 if (dir == ALLOC_RIGHT) {
1440                         secno = find_next_zero_bit(free_i->free_secmap,
1441                                                         MAIN_SECS(sbi), 0);
1442                         f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
1443                 } else {
1444                         go_left = 1;
1445                         left_start = hint - 1;
1446                 }
1447         }
1448         if (go_left == 0)
1449                 goto skip_left;
1450
1451         while (test_bit(left_start, free_i->free_secmap)) {
1452                 if (left_start > 0) {
1453                         left_start--;
1454                         continue;
1455                 }
1456                 left_start = find_next_zero_bit(free_i->free_secmap,
1457                                                         MAIN_SECS(sbi), 0);
1458                 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
1459                 break;
1460         }
1461         secno = left_start;
1462 skip_left:
1463         hint = secno;
1464         segno = secno * sbi->segs_per_sec;
1465         zoneno = secno / sbi->secs_per_zone;
1466
1467         /* give up on finding another zone */
1468         if (!init)
1469                 goto got_it;
1470         if (sbi->secs_per_zone == 1)
1471                 goto got_it;
1472         if (zoneno == old_zoneno)
1473                 goto got_it;
1474         if (dir == ALLOC_LEFT) {
1475                 if (!go_left && zoneno + 1 >= total_zones)
1476                         goto got_it;
1477                 if (go_left && zoneno == 0)
1478                         goto got_it;
1479         }
1480         for (i = 0; i < NR_CURSEG_TYPE; i++)
1481                 if (CURSEG_I(sbi, i)->zone == zoneno)
1482                         break;
1483
1484         if (i < NR_CURSEG_TYPE) {
1485                 /* zone is in user, try another */
1486                 if (go_left)
1487                         hint = zoneno * sbi->secs_per_zone - 1;
1488                 else if (zoneno + 1 >= total_zones)
1489                         hint = 0;
1490                 else
1491                         hint = (zoneno + 1) * sbi->secs_per_zone;
1492                 init = false;
1493                 goto find_other_zone;
1494         }
1495 got_it:
1496         /* set it as dirty segment in free segmap */
1497         f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
1498         __set_inuse(sbi, segno);
1499         *newseg = segno;
1500         spin_unlock(&free_i->segmap_lock);
1501 }
1502
1503 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1504 {
1505         struct curseg_info *curseg = CURSEG_I(sbi, type);
1506         struct summary_footer *sum_footer;
1507
1508         curseg->segno = curseg->next_segno;
1509         curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
1510         curseg->next_blkoff = 0;
1511         curseg->next_segno = NULL_SEGNO;
1512
1513         sum_footer = &(curseg->sum_blk->footer);
1514         memset(sum_footer, 0, sizeof(struct summary_footer));
1515         if (IS_DATASEG(type))
1516                 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1517         if (IS_NODESEG(type))
1518                 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1519         __set_sit_entry_type(sbi, type, curseg->segno, modified);
1520 }
1521
1522 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
1523 {
1524         if (type == CURSEG_HOT_DATA || IS_NODESEG(type))
1525                 return 0;
1526
1527         return CURSEG_I(sbi, type)->segno;
1528 }
1529
1530 /*
1531  * Allocate a current working segment.
1532  * This function always allocates a free segment in LFS manner.
1533  */
1534 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1535 {
1536         struct curseg_info *curseg = CURSEG_I(sbi, type);
1537         unsigned int segno = curseg->segno;
1538         int dir = ALLOC_LEFT;
1539
1540         write_sum_page(sbi, curseg->sum_blk,
1541                                 GET_SUM_BLOCK(sbi, segno));
1542         if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1543                 dir = ALLOC_RIGHT;
1544
1545         if (test_opt(sbi, NOHEAP))
1546                 dir = ALLOC_RIGHT;
1547
1548         segno = __get_next_segno(sbi, type);
1549         get_new_segment(sbi, &segno, new_sec, dir);
1550         curseg->next_segno = segno;
1551         reset_curseg(sbi, type, 1);
1552         curseg->alloc_type = LFS;
1553 }
1554
1555 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1556                         struct curseg_info *seg, block_t start)
1557 {
1558         struct seg_entry *se = get_seg_entry(sbi, seg->segno);
1559         int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1560         unsigned long *target_map = SIT_I(sbi)->tmp_map;
1561         unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1562         unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1563         int i, pos;
1564
1565         for (i = 0; i < entries; i++)
1566                 target_map[i] = ckpt_map[i] | cur_map[i];
1567
1568         pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1569
1570         seg->next_blkoff = pos;
1571 }
1572
1573 /*
1574  * If a segment is written by LFS manner, next block offset is just obtained
1575  * by increasing the current block offset. However, if a segment is written by
1576  * SSR manner, next block offset obtained by calling __next_free_blkoff
1577  */
1578 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1579                                 struct curseg_info *seg)
1580 {
1581         if (seg->alloc_type == SSR)
1582                 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1583         else
1584                 seg->next_blkoff++;
1585 }
1586
1587 /*
1588  * This function always allocates a used segment(from dirty seglist) by SSR
1589  * manner, so it should recover the existing segment information of valid blocks
1590  */
1591 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1592 {
1593         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1594         struct curseg_info *curseg = CURSEG_I(sbi, type);
1595         unsigned int new_segno = curseg->next_segno;
1596         struct f2fs_summary_block *sum_node;
1597         struct page *sum_page;
1598
1599         write_sum_page(sbi, curseg->sum_blk,
1600                                 GET_SUM_BLOCK(sbi, curseg->segno));
1601         __set_test_and_inuse(sbi, new_segno);
1602
1603         mutex_lock(&dirty_i->seglist_lock);
1604         __remove_dirty_segment(sbi, new_segno, PRE);
1605         __remove_dirty_segment(sbi, new_segno, DIRTY);
1606         mutex_unlock(&dirty_i->seglist_lock);
1607
1608         reset_curseg(sbi, type, 1);
1609         curseg->alloc_type = SSR;
1610         __next_free_blkoff(sbi, curseg, 0);
1611
1612         if (reuse) {
1613                 sum_page = get_sum_page(sbi, new_segno);
1614                 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1615                 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1616                 f2fs_put_page(sum_page, 1);
1617         }
1618 }
1619
1620 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1621 {
1622         struct curseg_info *curseg = CURSEG_I(sbi, type);
1623         const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1624         int i, cnt;
1625         bool reversed = false;
1626
1627         /* need_SSR() already forces to do this */
1628         if (v_ops->get_victim(sbi, &(curseg)->next_segno, BG_GC, type, SSR))
1629                 return 1;
1630
1631         /* For node segments, let's do SSR more intensively */
1632         if (IS_NODESEG(type)) {
1633                 if (type >= CURSEG_WARM_NODE) {
1634                         reversed = true;
1635                         i = CURSEG_COLD_NODE;
1636                 } else {
1637                         i = CURSEG_HOT_NODE;
1638                 }
1639                 cnt = NR_CURSEG_NODE_TYPE;
1640         } else {
1641                 if (type >= CURSEG_WARM_DATA) {
1642                         reversed = true;
1643                         i = CURSEG_COLD_DATA;
1644                 } else {
1645                         i = CURSEG_HOT_DATA;
1646                 }
1647                 cnt = NR_CURSEG_DATA_TYPE;
1648         }
1649
1650         for (; cnt-- > 0; reversed ? i-- : i++) {
1651                 if (i == type)
1652                         continue;
1653                 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1654                                                 BG_GC, i, SSR))
1655                         return 1;
1656         }
1657         return 0;
1658 }
1659
1660 /*
1661  * flush out current segment and replace it with new segment
1662  * This function should be returned with success, otherwise BUG
1663  */
1664 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1665                                                 int type, bool force)
1666 {
1667         if (force)
1668                 new_curseg(sbi, type, true);
1669         else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
1670                                         type == CURSEG_WARM_NODE)
1671                 new_curseg(sbi, type, false);
1672         else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1673                 change_curseg(sbi, type, true);
1674         else
1675                 new_curseg(sbi, type, false);
1676
1677         stat_inc_seg_type(sbi, CURSEG_I(sbi, type));
1678 }
1679
1680 void allocate_new_segments(struct f2fs_sb_info *sbi)
1681 {
1682         struct curseg_info *curseg;
1683         unsigned int old_segno;
1684         int i;
1685
1686         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1687                 curseg = CURSEG_I(sbi, i);
1688                 old_segno = curseg->segno;
1689                 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
1690                 locate_dirty_segment(sbi, old_segno);
1691         }
1692 }
1693
1694 static const struct segment_allocation default_salloc_ops = {
1695         .allocate_segment = allocate_segment_by_default,
1696 };
1697
1698 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1699 {
1700         __u64 trim_start = cpc->trim_start;
1701         bool has_candidate = false;
1702
1703         mutex_lock(&SIT_I(sbi)->sentry_lock);
1704         for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
1705                 if (add_discard_addrs(sbi, cpc, true)) {
1706                         has_candidate = true;
1707                         break;
1708                 }
1709         }
1710         mutex_unlock(&SIT_I(sbi)->sentry_lock);
1711
1712         cpc->trim_start = trim_start;
1713         return has_candidate;
1714 }
1715
1716 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1717 {
1718         __u64 start = F2FS_BYTES_TO_BLK(range->start);
1719         __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
1720         unsigned int start_segno, end_segno;
1721         struct cp_control cpc;
1722         int err = 0;
1723
1724         if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
1725                 return -EINVAL;
1726
1727         cpc.trimmed = 0;
1728         if (end <= MAIN_BLKADDR(sbi))
1729                 goto out;
1730
1731         if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1732                 f2fs_msg(sbi->sb, KERN_WARNING,
1733                         "Found FS corruption, run fsck to fix.");
1734                 goto out;
1735         }
1736
1737         /* start/end segment number in main_area */
1738         start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1739         end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1740                                                 GET_SEGNO(sbi, end);
1741         cpc.reason = CP_DISCARD;
1742         cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
1743
1744         /* do checkpoint to issue discard commands safely */
1745         for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1746                 cpc.trim_start = start_segno;
1747
1748                 if (sbi->discard_blks == 0)
1749                         break;
1750                 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1751                         cpc.trim_end = end_segno;
1752                 else
1753                         cpc.trim_end = min_t(unsigned int,
1754                                 rounddown(start_segno +
1755                                 BATCHED_TRIM_SEGMENTS(sbi),
1756                                 sbi->segs_per_sec) - 1, end_segno);
1757
1758                 mutex_lock(&sbi->gc_mutex);
1759                 err = write_checkpoint(sbi, &cpc);
1760                 mutex_unlock(&sbi->gc_mutex);
1761                 if (err)
1762                         break;
1763
1764                 schedule();
1765         }
1766 out:
1767         range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
1768         return err;
1769 }
1770
1771 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1772 {
1773         struct curseg_info *curseg = CURSEG_I(sbi, type);
1774         if (curseg->next_blkoff < sbi->blocks_per_seg)
1775                 return true;
1776         return false;
1777 }
1778
1779 static int __get_segment_type_2(struct page *page, enum page_type p_type)
1780 {
1781         if (p_type == DATA)
1782                 return CURSEG_HOT_DATA;
1783         else
1784                 return CURSEG_HOT_NODE;
1785 }
1786
1787 static int __get_segment_type_4(struct page *page, enum page_type p_type)
1788 {
1789         if (p_type == DATA) {
1790                 struct inode *inode = page->mapping->host;
1791
1792                 if (S_ISDIR(inode->i_mode))
1793                         return CURSEG_HOT_DATA;
1794                 else
1795                         return CURSEG_COLD_DATA;
1796         } else {
1797                 if (IS_DNODE(page) && is_cold_node(page))
1798                         return CURSEG_WARM_NODE;
1799                 else
1800                         return CURSEG_COLD_NODE;
1801         }
1802 }
1803
1804 static int __get_segment_type_6(struct page *page, enum page_type p_type)
1805 {
1806         if (p_type == DATA) {
1807                 struct inode *inode = page->mapping->host;
1808
1809                 if (S_ISDIR(inode->i_mode))
1810                         return CURSEG_HOT_DATA;
1811                 else if (is_cold_data(page) || file_is_cold(inode))
1812                         return CURSEG_COLD_DATA;
1813                 else
1814                         return CURSEG_WARM_DATA;
1815         } else {
1816                 if (IS_DNODE(page))
1817                         return is_cold_node(page) ? CURSEG_WARM_NODE :
1818                                                 CURSEG_HOT_NODE;
1819                 else
1820                         return CURSEG_COLD_NODE;
1821         }
1822 }
1823
1824 static int __get_segment_type(struct page *page, enum page_type p_type)
1825 {
1826         switch (F2FS_P_SB(page)->active_logs) {
1827         case 2:
1828                 return __get_segment_type_2(page, p_type);
1829         case 4:
1830                 return __get_segment_type_4(page, p_type);
1831         }
1832         /* NR_CURSEG_TYPE(6) logs by default */
1833         f2fs_bug_on(F2FS_P_SB(page),
1834                 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
1835         return __get_segment_type_6(page, p_type);
1836 }
1837
1838 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1839                 block_t old_blkaddr, block_t *new_blkaddr,
1840                 struct f2fs_summary *sum, int type)
1841 {
1842         struct sit_info *sit_i = SIT_I(sbi);
1843         struct curseg_info *curseg = CURSEG_I(sbi, type);
1844
1845         mutex_lock(&curseg->curseg_mutex);
1846         mutex_lock(&sit_i->sentry_lock);
1847
1848         *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1849
1850         f2fs_wait_discard_bio(sbi, *new_blkaddr);
1851
1852         /*
1853          * __add_sum_entry should be resided under the curseg_mutex
1854          * because, this function updates a summary entry in the
1855          * current summary block.
1856          */
1857         __add_sum_entry(sbi, type, sum);
1858
1859         __refresh_next_blkoff(sbi, curseg);
1860
1861         stat_inc_block_count(sbi, curseg);
1862
1863         /*
1864          * SIT information should be updated before segment allocation,
1865          * since SSR needs latest valid block information.
1866          */
1867         refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1868
1869         if (!__has_curseg_space(sbi, type))
1870                 sit_i->s_ops->allocate_segment(sbi, type, false);
1871
1872         mutex_unlock(&sit_i->sentry_lock);
1873
1874         if (page && IS_NODESEG(type))
1875                 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1876
1877         mutex_unlock(&curseg->curseg_mutex);
1878 }
1879
1880 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
1881 {
1882         int type = __get_segment_type(fio->page, fio->type);
1883         int err;
1884
1885         if (fio->type == NODE || fio->type == DATA)
1886                 mutex_lock(&fio->sbi->wio_mutex[fio->type]);
1887 reallocate:
1888         allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
1889                                         &fio->new_blkaddr, sum, type);
1890
1891         /* writeout dirty page into bdev */
1892         err = f2fs_submit_page_mbio(fio);
1893         if (err == -EAGAIN) {
1894                 fio->old_blkaddr = fio->new_blkaddr;
1895                 goto reallocate;
1896         }
1897
1898         if (fio->type == NODE || fio->type == DATA)
1899                 mutex_unlock(&fio->sbi->wio_mutex[fio->type]);
1900 }
1901
1902 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1903 {
1904         struct f2fs_io_info fio = {
1905                 .sbi = sbi,
1906                 .type = META,
1907                 .op = REQ_OP_WRITE,
1908                 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
1909                 .old_blkaddr = page->index,
1910                 .new_blkaddr = page->index,
1911                 .page = page,
1912                 .encrypted_page = NULL,
1913         };
1914
1915         if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
1916                 fio.op_flags &= ~REQ_META;
1917
1918         set_page_writeback(page);
1919         f2fs_submit_page_mbio(&fio);
1920 }
1921
1922 void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
1923 {
1924         struct f2fs_summary sum;
1925
1926         set_summary(&sum, nid, 0, 0);
1927         do_write_page(&sum, fio);
1928 }
1929
1930 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
1931 {
1932         struct f2fs_sb_info *sbi = fio->sbi;
1933         struct f2fs_summary sum;
1934         struct node_info ni;
1935
1936         f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
1937         get_node_info(sbi, dn->nid, &ni);
1938         set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1939         do_write_page(&sum, fio);
1940         f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
1941 }
1942
1943 void rewrite_data_page(struct f2fs_io_info *fio)
1944 {
1945         fio->new_blkaddr = fio->old_blkaddr;
1946         stat_inc_inplace_blocks(fio->sbi);
1947         f2fs_submit_page_mbio(fio);
1948 }
1949
1950 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1951                                 block_t old_blkaddr, block_t new_blkaddr,
1952                                 bool recover_curseg, bool recover_newaddr)
1953 {
1954         struct sit_info *sit_i = SIT_I(sbi);
1955         struct curseg_info *curseg;
1956         unsigned int segno, old_cursegno;
1957         struct seg_entry *se;
1958         int type;
1959         unsigned short old_blkoff;
1960
1961         segno = GET_SEGNO(sbi, new_blkaddr);
1962         se = get_seg_entry(sbi, segno);
1963         type = se->type;
1964
1965         if (!recover_curseg) {
1966                 /* for recovery flow */
1967                 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1968                         if (old_blkaddr == NULL_ADDR)
1969                                 type = CURSEG_COLD_DATA;
1970                         else
1971                                 type = CURSEG_WARM_DATA;
1972                 }
1973         } else {
1974                 if (!IS_CURSEG(sbi, segno))
1975                         type = CURSEG_WARM_DATA;
1976         }
1977
1978         curseg = CURSEG_I(sbi, type);
1979
1980         mutex_lock(&curseg->curseg_mutex);
1981         mutex_lock(&sit_i->sentry_lock);
1982
1983         old_cursegno = curseg->segno;
1984         old_blkoff = curseg->next_blkoff;
1985
1986         /* change the current segment */
1987         if (segno != curseg->segno) {
1988                 curseg->next_segno = segno;
1989                 change_curseg(sbi, type, true);
1990         }
1991
1992         curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1993         __add_sum_entry(sbi, type, sum);
1994
1995         if (!recover_curseg || recover_newaddr)
1996                 update_sit_entry(sbi, new_blkaddr, 1);
1997         if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1998                 update_sit_entry(sbi, old_blkaddr, -1);
1999
2000         locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2001         locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2002
2003         locate_dirty_segment(sbi, old_cursegno);
2004
2005         if (recover_curseg) {
2006                 if (old_cursegno != curseg->segno) {
2007                         curseg->next_segno = old_cursegno;
2008                         change_curseg(sbi, type, true);
2009                 }
2010                 curseg->next_blkoff = old_blkoff;
2011         }
2012
2013         mutex_unlock(&sit_i->sentry_lock);
2014         mutex_unlock(&curseg->curseg_mutex);
2015 }
2016
2017 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2018                                 block_t old_addr, block_t new_addr,
2019                                 unsigned char version, bool recover_curseg,
2020                                 bool recover_newaddr)
2021 {
2022         struct f2fs_summary sum;
2023
2024         set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2025
2026         __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2027                                         recover_curseg, recover_newaddr);
2028
2029         f2fs_update_data_blkaddr(dn, new_addr);
2030 }
2031
2032 void f2fs_wait_on_page_writeback(struct page *page,
2033                                 enum page_type type, bool ordered)
2034 {
2035         if (PageWriteback(page)) {
2036                 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2037
2038                 f2fs_submit_merged_bio_cond(sbi, page->mapping->host,
2039                                                 0, page->index, type, WRITE);
2040                 if (ordered)
2041                         wait_on_page_writeback(page);
2042                 else
2043                         wait_for_stable_page(page);
2044         }
2045 }
2046
2047 void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2048                                                         block_t blkaddr)
2049 {
2050         struct page *cpage;
2051
2052         if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
2053                 return;
2054
2055         cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2056         if (cpage) {
2057                 f2fs_wait_on_page_writeback(cpage, DATA, true);
2058                 f2fs_put_page(cpage, 1);
2059         }
2060 }
2061
2062 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2063 {
2064         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2065         struct curseg_info *seg_i;
2066         unsigned char *kaddr;
2067         struct page *page;
2068         block_t start;
2069         int i, j, offset;
2070
2071         start = start_sum_block(sbi);
2072
2073         page = get_meta_page(sbi, start++);
2074         kaddr = (unsigned char *)page_address(page);
2075
2076         /* Step 1: restore nat cache */
2077         seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
2078         memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
2079
2080         /* Step 2: restore sit cache */
2081         seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
2082         memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
2083         offset = 2 * SUM_JOURNAL_SIZE;
2084
2085         /* Step 3: restore summary entries */
2086         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2087                 unsigned short blk_off;
2088                 unsigned int segno;
2089
2090                 seg_i = CURSEG_I(sbi, i);
2091                 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
2092                 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
2093                 seg_i->next_segno = segno;
2094                 reset_curseg(sbi, i, 0);
2095                 seg_i->alloc_type = ckpt->alloc_type[i];
2096                 seg_i->next_blkoff = blk_off;
2097
2098                 if (seg_i->alloc_type == SSR)
2099                         blk_off = sbi->blocks_per_seg;
2100
2101                 for (j = 0; j < blk_off; j++) {
2102                         struct f2fs_summary *s;
2103                         s = (struct f2fs_summary *)(kaddr + offset);
2104                         seg_i->sum_blk->entries[j] = *s;
2105                         offset += SUMMARY_SIZE;
2106                         if (offset + SUMMARY_SIZE <= PAGE_SIZE -
2107                                                 SUM_FOOTER_SIZE)
2108                                 continue;
2109
2110                         f2fs_put_page(page, 1);
2111                         page = NULL;
2112
2113                         page = get_meta_page(sbi, start++);
2114                         kaddr = (unsigned char *)page_address(page);
2115                         offset = 0;
2116                 }
2117         }
2118         f2fs_put_page(page, 1);
2119         return 0;
2120 }
2121
2122 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2123 {
2124         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2125         struct f2fs_summary_block *sum;
2126         struct curseg_info *curseg;
2127         struct page *new;
2128         unsigned short blk_off;
2129         unsigned int segno = 0;
2130         block_t blk_addr = 0;
2131
2132         /* get segment number and block addr */
2133         if (IS_DATASEG(type)) {
2134                 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2135                 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2136                                                         CURSEG_HOT_DATA]);
2137                 if (__exist_node_summaries(sbi))
2138                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2139                 else
2140                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2141         } else {
2142                 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2143                                                         CURSEG_HOT_NODE]);
2144                 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2145                                                         CURSEG_HOT_NODE]);
2146                 if (__exist_node_summaries(sbi))
2147                         blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2148                                                         type - CURSEG_HOT_NODE);
2149                 else
2150                         blk_addr = GET_SUM_BLOCK(sbi, segno);
2151         }
2152
2153         new = get_meta_page(sbi, blk_addr);
2154         sum = (struct f2fs_summary_block *)page_address(new);
2155
2156         if (IS_NODESEG(type)) {
2157                 if (__exist_node_summaries(sbi)) {
2158                         struct f2fs_summary *ns = &sum->entries[0];
2159                         int i;
2160                         for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2161                                 ns->version = 0;
2162                                 ns->ofs_in_node = 0;
2163                         }
2164                 } else {
2165                         int err;
2166
2167                         err = restore_node_summary(sbi, segno, sum);
2168                         if (err) {
2169                                 f2fs_put_page(new, 1);
2170                                 return err;
2171                         }
2172                 }
2173         }
2174
2175         /* set uncompleted segment to curseg */
2176         curseg = CURSEG_I(sbi, type);
2177         mutex_lock(&curseg->curseg_mutex);
2178
2179         /* update journal info */
2180         down_write(&curseg->journal_rwsem);
2181         memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2182         up_write(&curseg->journal_rwsem);
2183
2184         memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2185         memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
2186         curseg->next_segno = segno;
2187         reset_curseg(sbi, type, 0);
2188         curseg->alloc_type = ckpt->alloc_type[type];
2189         curseg->next_blkoff = blk_off;
2190         mutex_unlock(&curseg->curseg_mutex);
2191         f2fs_put_page(new, 1);
2192         return 0;
2193 }
2194
2195 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2196 {
2197         int type = CURSEG_HOT_DATA;
2198         int err;
2199
2200         if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
2201                 int npages = npages_for_summary_flush(sbi, true);
2202
2203                 if (npages >= 2)
2204                         ra_meta_pages(sbi, start_sum_block(sbi), npages,
2205                                                         META_CP, true);
2206
2207                 /* restore for compacted data summary */
2208                 if (read_compacted_summaries(sbi))
2209                         return -EINVAL;
2210                 type = CURSEG_HOT_NODE;
2211         }
2212
2213         if (__exist_node_summaries(sbi))
2214                 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
2215                                         NR_CURSEG_TYPE - type, META_CP, true);
2216
2217         for (; type <= CURSEG_COLD_NODE; type++) {
2218                 err = read_normal_summaries(sbi, type);
2219                 if (err)
2220                         return err;
2221         }
2222
2223         return 0;
2224 }
2225
2226 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2227 {
2228         struct page *page;
2229         unsigned char *kaddr;
2230         struct f2fs_summary *summary;
2231         struct curseg_info *seg_i;
2232         int written_size = 0;
2233         int i, j;
2234
2235         page = grab_meta_page(sbi, blkaddr++);
2236         kaddr = (unsigned char *)page_address(page);
2237
2238         /* Step 1: write nat cache */
2239         seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
2240         memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
2241         written_size += SUM_JOURNAL_SIZE;
2242
2243         /* Step 2: write sit cache */
2244         seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
2245         memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
2246         written_size += SUM_JOURNAL_SIZE;
2247
2248         /* Step 3: write summary entries */
2249         for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2250                 unsigned short blkoff;
2251                 seg_i = CURSEG_I(sbi, i);
2252                 if (sbi->ckpt->alloc_type[i] == SSR)
2253                         blkoff = sbi->blocks_per_seg;
2254                 else
2255                         blkoff = curseg_blkoff(sbi, i);
2256
2257                 for (j = 0; j < blkoff; j++) {
2258                         if (!page) {
2259                                 page = grab_meta_page(sbi, blkaddr++);
2260                                 kaddr = (unsigned char *)page_address(page);
2261                                 written_size = 0;
2262                         }
2263                         summary = (struct f2fs_summary *)(kaddr + written_size);
2264                         *summary = seg_i->sum_blk->entries[j];
2265                         written_size += SUMMARY_SIZE;
2266
2267                         if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
2268                                                         SUM_FOOTER_SIZE)
2269                                 continue;
2270
2271                         set_page_dirty(page);
2272                         f2fs_put_page(page, 1);
2273                         page = NULL;
2274                 }
2275         }
2276         if (page) {
2277                 set_page_dirty(page);
2278                 f2fs_put_page(page, 1);
2279         }
2280 }
2281
2282 static void write_normal_summaries(struct f2fs_sb_info *sbi,
2283                                         block_t blkaddr, int type)
2284 {
2285         int i, end;
2286         if (IS_DATASEG(type))
2287                 end = type + NR_CURSEG_DATA_TYPE;
2288         else
2289                 end = type + NR_CURSEG_NODE_TYPE;
2290
2291         for (i = type; i < end; i++)
2292                 write_current_sum_page(sbi, i, blkaddr + (i - type));
2293 }
2294
2295 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2296 {
2297         if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
2298                 write_compacted_summaries(sbi, start_blk);
2299         else
2300                 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2301 }
2302
2303 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2304 {
2305         write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
2306 }
2307
2308 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2309                                         unsigned int val, int alloc)
2310 {
2311         int i;
2312
2313         if (type == NAT_JOURNAL) {
2314                 for (i = 0; i < nats_in_cursum(journal); i++) {
2315                         if (le32_to_cpu(nid_in_journal(journal, i)) == val)
2316                                 return i;
2317                 }
2318                 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2319                         return update_nats_in_cursum(journal, 1);
2320         } else if (type == SIT_JOURNAL) {
2321                 for (i = 0; i < sits_in_cursum(journal); i++)
2322                         if (le32_to_cpu(segno_in_journal(journal, i)) == val)
2323                                 return i;
2324                 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2325                         return update_sits_in_cursum(journal, 1);
2326         }
2327         return -1;
2328 }
2329
2330 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2331                                         unsigned int segno)
2332 {
2333         return get_meta_page(sbi, current_sit_addr(sbi, segno));
2334 }
2335
2336 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2337                                         unsigned int start)
2338 {
2339         struct sit_info *sit_i = SIT_I(sbi);
2340         struct page *src_page, *dst_page;
2341         pgoff_t src_off, dst_off;
2342         void *src_addr, *dst_addr;
2343
2344         src_off = current_sit_addr(sbi, start);
2345         dst_off = next_sit_addr(sbi, src_off);
2346
2347         /* get current sit block page without lock */
2348         src_page = get_meta_page(sbi, src_off);
2349         dst_page = grab_meta_page(sbi, dst_off);
2350         f2fs_bug_on(sbi, PageDirty(src_page));
2351
2352         src_addr = page_address(src_page);
2353         dst_addr = page_address(dst_page);
2354         memcpy(dst_addr, src_addr, PAGE_SIZE);
2355
2356         set_page_dirty(dst_page);
2357         f2fs_put_page(src_page, 1);
2358
2359         set_to_next_sit(sit_i, start);
2360
2361         return dst_page;
2362 }
2363
2364 static struct sit_entry_set *grab_sit_entry_set(void)
2365 {
2366         struct sit_entry_set *ses =
2367                         f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
2368
2369         ses->entry_cnt = 0;
2370         INIT_LIST_HEAD(&ses->set_list);
2371         return ses;
2372 }
2373
2374 static void release_sit_entry_set(struct sit_entry_set *ses)
2375 {
2376         list_del(&ses->set_list);
2377         kmem_cache_free(sit_entry_set_slab, ses);
2378 }
2379
2380 static void adjust_sit_entry_set(struct sit_entry_set *ses,
2381                                                 struct list_head *head)
2382 {
2383         struct sit_entry_set *next = ses;
2384
2385         if (list_is_last(&ses->set_list, head))
2386                 return;
2387
2388         list_for_each_entry_continue(next, head, set_list)
2389                 if (ses->entry_cnt <= next->entry_cnt)
2390                         break;
2391
2392         list_move_tail(&ses->set_list, &next->set_list);
2393 }
2394
2395 static void add_sit_entry(unsigned int segno, struct list_head *head)
2396 {
2397         struct sit_entry_set *ses;
2398         unsigned int start_segno = START_SEGNO(segno);
2399
2400         list_for_each_entry(ses, head, set_list) {
2401                 if (ses->start_segno == start_segno) {
2402                         ses->entry_cnt++;
2403                         adjust_sit_entry_set(ses, head);
2404                         return;
2405                 }
2406         }
2407
2408         ses = grab_sit_entry_set();
2409
2410         ses->start_segno = start_segno;
2411         ses->entry_cnt++;
2412         list_add(&ses->set_list, head);
2413 }
2414
2415 static void add_sits_in_set(struct f2fs_sb_info *sbi)
2416 {
2417         struct f2fs_sm_info *sm_info = SM_I(sbi);
2418         struct list_head *set_list = &sm_info->sit_entry_set;
2419         unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
2420         unsigned int segno;
2421
2422         for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
2423                 add_sit_entry(segno, set_list);
2424 }
2425
2426 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
2427 {
2428         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2429         struct f2fs_journal *journal = curseg->journal;
2430         int i;
2431
2432         down_write(&curseg->journal_rwsem);
2433         for (i = 0; i < sits_in_cursum(journal); i++) {
2434                 unsigned int segno;
2435                 bool dirtied;
2436
2437                 segno = le32_to_cpu(segno_in_journal(journal, i));
2438                 dirtied = __mark_sit_entry_dirty(sbi, segno);
2439
2440                 if (!dirtied)
2441                         add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
2442         }
2443         update_sits_in_cursum(journal, -i);
2444         up_write(&curseg->journal_rwsem);
2445 }
2446
2447 /*
2448  * CP calls this function, which flushes SIT entries including sit_journal,
2449  * and moves prefree segs to free segs.
2450  */
2451 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2452 {
2453         struct sit_info *sit_i = SIT_I(sbi);
2454         unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2455         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2456         struct f2fs_journal *journal = curseg->journal;
2457         struct sit_entry_set *ses, *tmp;
2458         struct list_head *head = &SM_I(sbi)->sit_entry_set;
2459         bool to_journal = true;
2460         struct seg_entry *se;
2461
2462         mutex_lock(&sit_i->sentry_lock);
2463
2464         if (!sit_i->dirty_sentries)
2465                 goto out;
2466
2467         /*
2468          * add and account sit entries of dirty bitmap in sit entry
2469          * set temporarily
2470          */
2471         add_sits_in_set(sbi);
2472
2473         /*
2474          * if there are no enough space in journal to store dirty sit
2475          * entries, remove all entries from journal and add and account
2476          * them in sit entry set.
2477          */
2478         if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
2479                 remove_sits_in_journal(sbi);
2480
2481         /*
2482          * there are two steps to flush sit entries:
2483          * #1, flush sit entries to journal in current cold data summary block.
2484          * #2, flush sit entries to sit page.
2485          */
2486         list_for_each_entry_safe(ses, tmp, head, set_list) {
2487                 struct page *page = NULL;
2488                 struct f2fs_sit_block *raw_sit = NULL;
2489                 unsigned int start_segno = ses->start_segno;
2490                 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
2491                                                 (unsigned long)MAIN_SEGS(sbi));
2492                 unsigned int segno = start_segno;
2493
2494                 if (to_journal &&
2495                         !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
2496                         to_journal = false;
2497
2498                 if (to_journal) {
2499                         down_write(&curseg->journal_rwsem);
2500                 } else {
2501                         page = get_next_sit_page(sbi, start_segno);
2502                         raw_sit = page_address(page);
2503                 }
2504
2505                 /* flush dirty sit entries in region of current sit set */
2506                 for_each_set_bit_from(segno, bitmap, end) {
2507                         int offset, sit_offset;
2508
2509                         se = get_seg_entry(sbi, segno);
2510
2511                         /* add discard candidates */
2512                         if (cpc->reason != CP_DISCARD) {
2513                                 cpc->trim_start = segno;
2514                                 add_discard_addrs(sbi, cpc, false);
2515                         }
2516
2517                         if (to_journal) {
2518                                 offset = lookup_journal_in_cursum(journal,
2519                                                         SIT_JOURNAL, segno, 1);
2520                                 f2fs_bug_on(sbi, offset < 0);
2521                                 segno_in_journal(journal, offset) =
2522                                                         cpu_to_le32(segno);
2523                                 seg_info_to_raw_sit(se,
2524                                         &sit_in_journal(journal, offset));
2525                         } else {
2526                                 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2527                                 seg_info_to_raw_sit(se,
2528                                                 &raw_sit->entries[sit_offset]);
2529                         }
2530
2531                         __clear_bit(segno, bitmap);
2532                         sit_i->dirty_sentries--;
2533                         ses->entry_cnt--;
2534                 }
2535
2536                 if (to_journal)
2537                         up_write(&curseg->journal_rwsem);
2538                 else
2539                         f2fs_put_page(page, 1);
2540
2541                 f2fs_bug_on(sbi, ses->entry_cnt);
2542                 release_sit_entry_set(ses);
2543         }
2544
2545         f2fs_bug_on(sbi, !list_empty(head));
2546         f2fs_bug_on(sbi, sit_i->dirty_sentries);
2547 out:
2548         if (cpc->reason == CP_DISCARD) {
2549                 __u64 trim_start = cpc->trim_start;
2550
2551                 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
2552                         add_discard_addrs(sbi, cpc, false);
2553
2554                 cpc->trim_start = trim_start;
2555         }
2556         mutex_unlock(&sit_i->sentry_lock);
2557
2558         set_prefree_as_free_segments(sbi);
2559 }
2560
2561 static int build_sit_info(struct f2fs_sb_info *sbi)
2562 {
2563         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2564         struct sit_info *sit_i;
2565         unsigned int sit_segs, start;
2566         char *src_bitmap;
2567         unsigned int bitmap_size;
2568
2569         /* allocate memory for SIT information */
2570         sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
2571         if (!sit_i)
2572                 return -ENOMEM;
2573
2574         SM_I(sbi)->sit_info = sit_i;
2575
2576         sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
2577                                         sizeof(struct seg_entry), GFP_KERNEL);
2578         if (!sit_i->sentries)
2579                 return -ENOMEM;
2580
2581         bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2582         sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2583         if (!sit_i->dirty_sentries_bitmap)
2584                 return -ENOMEM;
2585
2586         for (start = 0; start < MAIN_SEGS(sbi); start++) {
2587                 sit_i->sentries[start].cur_valid_map
2588                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2589                 sit_i->sentries[start].ckpt_valid_map
2590                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2591                 if (!sit_i->sentries[start].cur_valid_map ||
2592                                 !sit_i->sentries[start].ckpt_valid_map)
2593                         return -ENOMEM;
2594
2595 #ifdef CONFIG_F2FS_CHECK_FS
2596                 sit_i->sentries[start].cur_valid_map_mir
2597                         = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2598                 if (!sit_i->sentries[start].cur_valid_map_mir)
2599                         return -ENOMEM;
2600 #endif
2601
2602                 if (f2fs_discard_en(sbi)) {
2603                         sit_i->sentries[start].discard_map
2604                                 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2605                         if (!sit_i->sentries[start].discard_map)
2606                                 return -ENOMEM;
2607                 }
2608         }
2609
2610         sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2611         if (!sit_i->tmp_map)
2612                 return -ENOMEM;
2613
2614         if (sbi->segs_per_sec > 1) {
2615                 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2616                                         sizeof(struct sec_entry), GFP_KERNEL);
2617                 if (!sit_i->sec_entries)
2618                         return -ENOMEM;
2619         }
2620
2621         /* get information related with SIT */
2622         sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2623
2624         /* setup SIT bitmap from ckeckpoint pack */
2625         bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2626         src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2627
2628         sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2629         if (!sit_i->sit_bitmap)
2630                 return -ENOMEM;
2631
2632 #ifdef CONFIG_F2FS_CHECK_FS
2633         sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2634         if (!sit_i->sit_bitmap_mir)
2635                 return -ENOMEM;
2636 #endif
2637
2638         /* init SIT information */
2639         sit_i->s_ops = &default_salloc_ops;
2640
2641         sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2642         sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2643         sit_i->written_valid_blocks = 0;
2644         sit_i->bitmap_size = bitmap_size;
2645         sit_i->dirty_sentries = 0;
2646         sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2647         sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2648         sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2649         mutex_init(&sit_i->sentry_lock);
2650         return 0;
2651 }
2652
2653 static int build_free_segmap(struct f2fs_sb_info *sbi)
2654 {
2655         struct free_segmap_info *free_i;
2656         unsigned int bitmap_size, sec_bitmap_size;
2657
2658         /* allocate memory for free segmap information */
2659         free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2660         if (!free_i)
2661                 return -ENOMEM;
2662
2663         SM_I(sbi)->free_info = free_i;
2664
2665         bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2666         free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
2667         if (!free_i->free_segmap)
2668                 return -ENOMEM;
2669
2670         sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2671         free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
2672         if (!free_i->free_secmap)
2673                 return -ENOMEM;
2674
2675         /* set all segments as dirty temporarily */
2676         memset(free_i->free_segmap, 0xff, bitmap_size);
2677         memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2678
2679         /* init free segmap information */
2680         free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
2681         free_i->free_segments = 0;
2682         free_i->free_sections = 0;
2683         spin_lock_init(&free_i->segmap_lock);
2684         return 0;
2685 }
2686
2687 static int build_curseg(struct f2fs_sb_info *sbi)
2688 {
2689         struct curseg_info *array;
2690         int i;
2691
2692         array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
2693         if (!array)
2694                 return -ENOMEM;
2695
2696         SM_I(sbi)->curseg_array = array;
2697
2698         for (i = 0; i < NR_CURSEG_TYPE; i++) {
2699                 mutex_init(&array[i].curseg_mutex);
2700                 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
2701                 if (!array[i].sum_blk)
2702                         return -ENOMEM;
2703                 init_rwsem(&array[i].journal_rwsem);
2704                 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
2705                                                         GFP_KERNEL);
2706                 if (!array[i].journal)
2707                         return -ENOMEM;
2708                 array[i].segno = NULL_SEGNO;
2709                 array[i].next_blkoff = 0;
2710         }
2711         return restore_curseg_summaries(sbi);
2712 }
2713
2714 static void build_sit_entries(struct f2fs_sb_info *sbi)
2715 {
2716         struct sit_info *sit_i = SIT_I(sbi);
2717         struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2718         struct f2fs_journal *journal = curseg->journal;
2719         struct seg_entry *se;
2720         struct f2fs_sit_entry sit;
2721         int sit_blk_cnt = SIT_BLK_CNT(sbi);
2722         unsigned int i, start, end;
2723         unsigned int readed, start_blk = 0;
2724
2725         do {
2726                 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
2727                                                         META_SIT, true);
2728
2729                 start = start_blk * sit_i->sents_per_block;
2730                 end = (start_blk + readed) * sit_i->sents_per_block;
2731
2732                 for (; start < end && start < MAIN_SEGS(sbi); start++) {
2733                         struct f2fs_sit_block *sit_blk;
2734                         struct page *page;
2735
2736                         se = &sit_i->sentries[start];
2737                         page = get_current_sit_page(sbi, start);
2738                         sit_blk = (struct f2fs_sit_block *)page_address(page);
2739                         sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2740                         f2fs_put_page(page, 1);
2741
2742                         check_block_count(sbi, start, &sit);
2743                         seg_info_from_raw_sit(se, &sit);
2744
2745                         /* build discard map only one time */
2746                         if (f2fs_discard_en(sbi)) {
2747                                 memcpy(se->discard_map, se->cur_valid_map,
2748                                                         SIT_VBLOCK_MAP_SIZE);
2749                                 sbi->discard_blks += sbi->blocks_per_seg -
2750                                                         se->valid_blocks;
2751                         }
2752
2753                         if (sbi->segs_per_sec > 1)
2754                                 get_sec_entry(sbi, start)->valid_blocks +=
2755                                                         se->valid_blocks;
2756                 }
2757                 start_blk += readed;
2758         } while (start_blk < sit_blk_cnt);
2759
2760         down_read(&curseg->journal_rwsem);
2761         for (i = 0; i < sits_in_cursum(journal); i++) {
2762                 unsigned int old_valid_blocks;
2763
2764                 start = le32_to_cpu(segno_in_journal(journal, i));
2765                 se = &sit_i->sentries[start];
2766                 sit = sit_in_journal(journal, i);
2767
2768                 old_valid_blocks = se->valid_blocks;
2769
2770                 check_block_count(sbi, start, &sit);
2771                 seg_info_from_raw_sit(se, &sit);
2772
2773                 if (f2fs_discard_en(sbi)) {
2774                         memcpy(se->discard_map, se->cur_valid_map,
2775                                                 SIT_VBLOCK_MAP_SIZE);
2776                         sbi->discard_blks += old_valid_blocks -
2777                                                 se->valid_blocks;
2778                 }
2779
2780                 if (sbi->segs_per_sec > 1)
2781                         get_sec_entry(sbi, start)->valid_blocks +=
2782                                 se->valid_blocks - old_valid_blocks;
2783         }
2784         up_read(&curseg->journal_rwsem);
2785 }
2786
2787 static void init_free_segmap(struct f2fs_sb_info *sbi)
2788 {
2789         unsigned int start;
2790         int type;
2791
2792         for (start = 0; start < MAIN_SEGS(sbi); start++) {
2793                 struct seg_entry *sentry = get_seg_entry(sbi, start);
2794                 if (!sentry->valid_blocks)
2795                         __set_free(sbi, start);
2796                 else
2797                         SIT_I(sbi)->written_valid_blocks +=
2798                                                 sentry->valid_blocks;
2799         }
2800
2801         /* set use the current segments */
2802         for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2803                 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2804                 __set_test_and_inuse(sbi, curseg_t->segno);
2805         }
2806 }
2807
2808 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2809 {
2810         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2811         struct free_segmap_info *free_i = FREE_I(sbi);
2812         unsigned int segno = 0, offset = 0;
2813         unsigned short valid_blocks;
2814
2815         while (1) {
2816                 /* find dirty segment based on free segmap */
2817                 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2818                 if (segno >= MAIN_SEGS(sbi))
2819                         break;
2820                 offset = segno + 1;
2821                 valid_blocks = get_valid_blocks(sbi, segno, 0);
2822                 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
2823                         continue;
2824                 if (valid_blocks > sbi->blocks_per_seg) {
2825                         f2fs_bug_on(sbi, 1);
2826                         continue;
2827                 }
2828                 mutex_lock(&dirty_i->seglist_lock);
2829                 __locate_dirty_segment(sbi, segno, DIRTY);
2830                 mutex_unlock(&dirty_i->seglist_lock);
2831         }
2832 }
2833
2834 static int init_victim_secmap(struct f2fs_sb_info *sbi)
2835 {
2836         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2837         unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2838
2839         dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2840         if (!dirty_i->victim_secmap)
2841                 return -ENOMEM;
2842         return 0;
2843 }
2844
2845 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2846 {
2847         struct dirty_seglist_info *dirty_i;
2848         unsigned int bitmap_size, i;
2849
2850         /* allocate memory for dirty segments list information */
2851         dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2852         if (!dirty_i)
2853                 return -ENOMEM;
2854
2855         SM_I(sbi)->dirty_info = dirty_i;
2856         mutex_init(&dirty_i->seglist_lock);
2857
2858         bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2859
2860         for (i = 0; i < NR_DIRTY_TYPE; i++) {
2861                 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2862                 if (!dirty_i->dirty_segmap[i])
2863                         return -ENOMEM;
2864         }
2865
2866         init_dirty_segmap(sbi);
2867         return init_victim_secmap(sbi);
2868 }
2869
2870 /*
2871  * Update min, max modified time for cost-benefit GC algorithm
2872  */
2873 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2874 {
2875         struct sit_info *sit_i = SIT_I(sbi);
2876         unsigned int segno;
2877
2878         mutex_lock(&sit_i->sentry_lock);
2879
2880         sit_i->min_mtime = LLONG_MAX;
2881
2882         for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
2883                 unsigned int i;
2884                 unsigned long long mtime = 0;
2885
2886                 for (i = 0; i < sbi->segs_per_sec; i++)
2887                         mtime += get_seg_entry(sbi, segno + i)->mtime;
2888
2889                 mtime = div_u64(mtime, sbi->segs_per_sec);
2890
2891                 if (sit_i->min_mtime > mtime)
2892                         sit_i->min_mtime = mtime;
2893         }
2894         sit_i->max_mtime = get_mtime(sbi);
2895         mutex_unlock(&sit_i->sentry_lock);
2896 }
2897
2898 int build_segment_manager(struct f2fs_sb_info *sbi)
2899 {
2900         struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2901         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2902         struct f2fs_sm_info *sm_info;
2903         int err;
2904
2905         sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2906         if (!sm_info)
2907                 return -ENOMEM;
2908
2909         /* init sm info */
2910         sbi->sm_info = sm_info;
2911         sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2912         sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2913         sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2914         sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2915         sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2916         sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2917         sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2918         sm_info->rec_prefree_segments = sm_info->main_segments *
2919                                         DEF_RECLAIM_PREFREE_SEGMENTS / 100;
2920         if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
2921                 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
2922
2923         if (!test_opt(sbi, LFS))
2924                 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
2925         sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
2926         sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
2927
2928         sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2929
2930         INIT_LIST_HEAD(&sm_info->sit_entry_set);
2931
2932         if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2933                 err = create_flush_cmd_control(sbi);
2934                 if (err)
2935                         return err;
2936         }
2937
2938         err = create_discard_cmd_control(sbi);
2939         if (err)
2940                 return err;
2941
2942         err = build_sit_info(sbi);
2943         if (err)
2944                 return err;
2945         err = build_free_segmap(sbi);
2946         if (err)
2947                 return err;
2948         err = build_curseg(sbi);
2949         if (err)
2950                 return err;
2951
2952         /* reinit free segmap based on SIT */
2953         build_sit_entries(sbi);
2954
2955         init_free_segmap(sbi);
2956         err = build_dirty_segmap(sbi);
2957         if (err)
2958                 return err;
2959
2960         init_min_max_mtime(sbi);
2961         return 0;
2962 }
2963
2964 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2965                 enum dirty_type dirty_type)
2966 {
2967         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2968
2969         mutex_lock(&dirty_i->seglist_lock);
2970         kvfree(dirty_i->dirty_segmap[dirty_type]);
2971         dirty_i->nr_dirty[dirty_type] = 0;
2972         mutex_unlock(&dirty_i->seglist_lock);
2973 }
2974
2975 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
2976 {
2977         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2978         kvfree(dirty_i->victim_secmap);
2979 }
2980
2981 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2982 {
2983         struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2984         int i;
2985
2986         if (!dirty_i)
2987                 return;
2988
2989         /* discard pre-free/dirty segments list */
2990         for (i = 0; i < NR_DIRTY_TYPE; i++)
2991                 discard_dirty_segmap(sbi, i);
2992
2993         destroy_victim_secmap(sbi);
2994         SM_I(sbi)->dirty_info = NULL;
2995         kfree(dirty_i);
2996 }
2997
2998 static void destroy_curseg(struct f2fs_sb_info *sbi)
2999 {
3000         struct curseg_info *array = SM_I(sbi)->curseg_array;
3001         int i;
3002
3003         if (!array)
3004                 return;
3005         SM_I(sbi)->curseg_array = NULL;
3006         for (i = 0; i < NR_CURSEG_TYPE; i++) {
3007                 kfree(array[i].sum_blk);
3008                 kfree(array[i].journal);
3009         }
3010         kfree(array);
3011 }
3012
3013 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3014 {
3015         struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3016         if (!free_i)
3017                 return;
3018         SM_I(sbi)->free_info = NULL;
3019         kvfree(free_i->free_segmap);
3020         kvfree(free_i->free_secmap);
3021         kfree(free_i);
3022 }
3023
3024 static void destroy_sit_info(struct f2fs_sb_info *sbi)
3025 {
3026         struct sit_info *sit_i = SIT_I(sbi);
3027         unsigned int start;
3028
3029         if (!sit_i)
3030                 return;
3031
3032         if (sit_i->sentries) {
3033                 for (start = 0; start < MAIN_SEGS(sbi); start++) {
3034                         kfree(sit_i->sentries[start].cur_valid_map);
3035 #ifdef CONFIG_F2FS_CHECK_FS
3036                         kfree(sit_i->sentries[start].cur_valid_map_mir);
3037 #endif
3038                         kfree(sit_i->sentries[start].ckpt_valid_map);
3039                         kfree(sit_i->sentries[start].discard_map);
3040                 }
3041         }
3042         kfree(sit_i->tmp_map);
3043
3044         kvfree(sit_i->sentries);
3045         kvfree(sit_i->sec_entries);
3046         kvfree(sit_i->dirty_sentries_bitmap);
3047
3048         SM_I(sbi)->sit_info = NULL;
3049         kfree(sit_i->sit_bitmap);
3050 #ifdef CONFIG_F2FS_CHECK_FS
3051         kfree(sit_i->sit_bitmap_mir);
3052 #endif
3053         kfree(sit_i);
3054 }
3055
3056 void destroy_segment_manager(struct f2fs_sb_info *sbi)
3057 {
3058         struct f2fs_sm_info *sm_info = SM_I(sbi);
3059
3060         if (!sm_info)
3061                 return;
3062         destroy_flush_cmd_control(sbi, true);
3063         destroy_discard_cmd_control(sbi, true);
3064         destroy_dirty_segmap(sbi);
3065         destroy_curseg(sbi);
3066         destroy_free_segmap(sbi);
3067         destroy_sit_info(sbi);
3068         sbi->sm_info = NULL;
3069         kfree(sm_info);
3070 }
3071
3072 int __init create_segment_manager_caches(void)
3073 {
3074         discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
3075                         sizeof(struct discard_entry));
3076         if (!discard_entry_slab)
3077                 goto fail;
3078
3079         discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
3080                         sizeof(struct discard_cmd));
3081         if (!discard_cmd_slab)
3082                 goto destroy_discard_entry;
3083
3084         sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
3085                         sizeof(struct sit_entry_set));
3086         if (!sit_entry_set_slab)
3087                 goto destroy_discard_cmd;
3088
3089         inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
3090                         sizeof(struct inmem_pages));
3091         if (!inmem_entry_slab)
3092                 goto destroy_sit_entry_set;
3093         return 0;
3094
3095 destroy_sit_entry_set:
3096         kmem_cache_destroy(sit_entry_set_slab);
3097 destroy_discard_cmd:
3098         kmem_cache_destroy(discard_cmd_slab);
3099 destroy_discard_entry:
3100         kmem_cache_destroy(discard_entry_slab);
3101 fail:
3102         return -ENOMEM;
3103 }
3104
3105 void destroy_segment_manager_caches(void)
3106 {
3107         kmem_cache_destroy(sit_entry_set_slab);
3108         kmem_cache_destroy(discard_cmd_slab);
3109         kmem_cache_destroy(discard_entry_slab);
3110         kmem_cache_destroy(inmem_entry_slab);
3111 }