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