4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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>
27 #include <trace/events/f2fs.h>
29 #define __reverse_ffz(x) __reverse_ffs(~(x))
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;
36 static unsigned long __reverse_ulong(unsigned char *str)
38 unsigned long tmp = 0;
39 int shift = 24, idx = 0;
41 #if BITS_PER_LONG == 64
45 tmp |= (unsigned long)str[idx++] << shift;
46 shift -= BITS_PER_BYTE;
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.
55 static inline unsigned long __reverse_ffs(unsigned long word)
59 #if BITS_PER_LONG == 64
60 if ((word & 0xffffffff00000000UL) == 0)
65 if ((word & 0xffff0000) == 0)
70 if ((word & 0xff00) == 0)
75 if ((word & 0xf0) == 0)
80 if ((word & 0xc) == 0)
85 if ((word & 0x2) == 0)
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.
96 * f2fs_set_bit(0, bitmap) => 1000 0000
97 * f2fs_set_bit(7, bitmap) => 0000 0001
99 static unsigned long __find_rev_next_bit(const unsigned long *addr,
100 unsigned long size, unsigned long offset)
102 const unsigned long *p = addr + BIT_WORD(offset);
103 unsigned long result = size;
109 size -= (offset & ~(BITS_PER_LONG - 1));
110 offset %= BITS_PER_LONG;
116 tmp = __reverse_ulong((unsigned char *)p);
118 tmp &= ~0UL >> offset;
119 if (size < BITS_PER_LONG)
120 tmp &= (~0UL << (BITS_PER_LONG - size));
124 if (size <= BITS_PER_LONG)
126 size -= BITS_PER_LONG;
132 return result - size + __reverse_ffs(tmp);
135 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
136 unsigned long size, unsigned long offset)
138 const unsigned long *p = addr + BIT_WORD(offset);
139 unsigned long result = size;
145 size -= (offset & ~(BITS_PER_LONG - 1));
146 offset %= BITS_PER_LONG;
152 tmp = __reverse_ulong((unsigned char *)p);
155 tmp |= ~0UL << (BITS_PER_LONG - offset);
156 if (size < BITS_PER_LONG)
161 if (size <= BITS_PER_LONG)
163 size -= BITS_PER_LONG;
169 return result - size + __reverse_ffz(tmp);
172 bool need_SSR(struct f2fs_sb_info *sbi)
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);
178 if (test_opt(sbi, LFS))
180 if (sbi->gc_thread && sbi->gc_thread->gc_urgent)
183 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
184 2 * reserved_sections(sbi));
187 void register_inmem_page(struct inode *inode, struct page *page)
189 struct f2fs_inode_info *fi = F2FS_I(inode);
190 struct inmem_pages *new;
192 f2fs_trace_pid(page);
194 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
195 SetPagePrivate(page);
197 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
199 /* add atomic page indices to the list */
201 INIT_LIST_HEAD(&new->list);
203 /* increase reference count with clean state */
204 mutex_lock(&fi->inmem_lock);
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);
210 trace_f2fs_register_inmem_page(page, INMEM);
213 static int __revoke_inmem_pages(struct inode *inode,
214 struct list_head *head, bool drop, bool recover)
216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
217 struct inmem_pages *cur, *tmp;
220 list_for_each_entry_safe(cur, tmp, head, list) {
221 struct page *page = cur->page;
224 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
228 f2fs_wait_on_page_writeback(page, DATA, true);
231 struct dnode_of_data dn;
234 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
236 set_new_dnode(&dn, inode, NULL, NULL, 0);
237 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
239 if (err == -ENOMEM) {
240 congestion_wait(BLK_RW_ASYNC, HZ/50);
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);
253 /* we don't need to invalidate this in the sccessful status */
254 if (drop || recover) {
255 ClearPageUptodate(page);
256 clear_cold_data(page);
258 set_page_private(page, 0);
259 ClearPagePrivate(page);
260 f2fs_put_page(page, 1);
262 list_del(&cur->list);
263 kmem_cache_free(inmem_entry_slab, cur);
264 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
269 void drop_inmem_pages(struct inode *inode)
271 struct f2fs_inode_info *fi = F2FS_I(inode);
273 mutex_lock(&fi->inmem_lock);
274 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
275 mutex_unlock(&fi->inmem_lock);
277 clear_inode_flag(inode, FI_ATOMIC_FILE);
278 clear_inode_flag(inode, FI_HOT_DATA);
279 stat_dec_atomic_write(inode);
282 void drop_inmem_page(struct inode *inode, struct page *page)
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;
289 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
291 mutex_lock(&fi->inmem_lock);
292 list_for_each_entry(cur, head, list) {
293 if (cur->page == page)
297 f2fs_bug_on(sbi, !cur || cur->page != page);
298 list_del(&cur->list);
299 mutex_unlock(&fi->inmem_lock);
301 dec_page_count(sbi, F2FS_INMEM_PAGES);
302 kmem_cache_free(inmem_entry_slab, cur);
304 ClearPageUptodate(page);
305 set_page_private(page, 0);
306 ClearPagePrivate(page);
307 f2fs_put_page(page, 0);
309 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
312 static int __commit_inmem_pages(struct inode *inode,
313 struct list_head *revoke_list)
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 = {
322 .op_flags = REQ_SYNC | REQ_PRIO,
323 .io_type = FS_DATA_IO,
325 pgoff_t last_idx = ULONG_MAX;
328 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
329 struct page *page = cur->page;
332 if (page->mapping == inode->i_mapping) {
333 trace_f2fs_commit_inmem_page(page, INMEM);
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);
343 fio.old_blkaddr = NULL_ADDR;
344 fio.encrypted_page = NULL;
345 fio.need_lock = LOCK_DONE;
346 err = do_write_data_page(&fio);
348 if (err == -ENOMEM) {
349 congestion_wait(BLK_RW_ASYNC, HZ/50);
356 /* record old blkaddr for revoking */
357 cur->old_addr = fio.old_blkaddr;
358 last_idx = page->index;
361 list_move_tail(&cur->list, revoke_list);
364 if (last_idx != ULONG_MAX)
365 f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
368 __revoke_inmem_pages(inode, revoke_list, false, false);
373 int commit_inmem_pages(struct inode *inode)
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;
380 INIT_LIST_HEAD(&revoke_list);
381 f2fs_balance_fs(sbi, true);
384 set_inode_flag(inode, FI_ATOMIC_COMMIT);
386 mutex_lock(&fi->inmem_lock);
387 err = __commit_inmem_pages(inode, &revoke_list);
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.
398 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
402 /* drop all uncommitted pages */
403 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
405 mutex_unlock(&fi->inmem_lock);
407 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
414 * This function balances dirty node and dentry pages.
415 * In addition, it controls garbage collection.
417 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
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);
426 /* balance_fs_bg is able to be pending */
427 if (need && excess_cached_nats(sbi))
428 f2fs_balance_fs_bg(sbi);
431 * We should do GC or end up with checkpoint, if there are so many dirty
432 * dir/node pages without enough free segments.
434 if (has_not_enough_free_secs(sbi, 0, 0)) {
435 mutex_lock(&sbi->gc_mutex);
436 f2fs_gc(sbi, false, false, NULL_SEGNO);
440 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
442 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
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);
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);
453 if (!available_free_memory(sbi, FREE_NIDS))
454 try_to_free_nids(sbi, MAX_FREE_NIDS);
456 build_free_nids(sbi, false, false);
458 if (!is_idle(sbi) && !excess_dirty_nats(sbi))
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;
470 blk_start_plug(&plug);
471 sync_dirty_inodes(sbi, FILE_INODE);
472 blk_finish_plug(&plug);
474 f2fs_sync_fs(sbi->sb, true);
475 stat_inc_bg_cp_count(sbi->stat_info);
479 static int __submit_flush_wait(struct f2fs_sb_info *sbi,
480 struct block_device *bdev)
482 struct bio *bio = f2fs_bio_alloc(0);
485 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
486 bio_set_dev(bio, bdev);
487 ret = submit_bio_wait(bio);
490 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
491 test_opt(sbi, FLUSH_MERGE), ret);
495 static int submit_flush_wait(struct f2fs_sb_info *sbi)
497 int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev);
500 if (!f2fs_is_multi_device(sbi) || ret)
503 for (i = 1; i < sbi->s_ndevs; i++) {
504 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
511 static int issue_flush_thread(void *data)
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;
517 if (kthread_should_stop())
520 sb_start_intwrite(sbi->sb);
522 if (!llist_empty(&fcc->issue_list)) {
523 struct flush_cmd *cmd, *next;
526 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
527 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
529 ret = submit_flush_wait(sbi);
530 atomic_inc(&fcc->issued_flush);
532 llist_for_each_entry_safe(cmd, next,
533 fcc->dispatch_list, llnode) {
535 complete(&cmd->wait);
537 fcc->dispatch_list = NULL;
540 sb_end_intwrite(sbi->sb);
542 wait_event_interruptible(*q,
543 kthread_should_stop() || !llist_empty(&fcc->issue_list));
547 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
549 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
550 struct flush_cmd cmd;
553 if (test_opt(sbi, NOBARRIER))
556 if (!test_opt(sbi, FLUSH_MERGE)) {
557 ret = submit_flush_wait(sbi);
558 atomic_inc(&fcc->issued_flush);
562 if (atomic_inc_return(&fcc->issing_flush) == 1) {
563 ret = submit_flush_wait(sbi);
564 atomic_dec(&fcc->issing_flush);
566 atomic_inc(&fcc->issued_flush);
570 init_completion(&cmd.wait);
572 llist_add(&cmd.llnode, &fcc->issue_list);
574 /* update issue_list before we wake up issue_flush thread */
577 if (waitqueue_active(&fcc->flush_wait_queue))
578 wake_up(&fcc->flush_wait_queue);
580 if (fcc->f2fs_issue_flush) {
581 wait_for_completion(&cmd.wait);
582 atomic_dec(&fcc->issing_flush);
584 struct llist_node *list;
586 list = llist_del_all(&fcc->issue_list);
588 wait_for_completion(&cmd.wait);
589 atomic_dec(&fcc->issing_flush);
591 struct flush_cmd *tmp, *next;
593 ret = submit_flush_wait(sbi);
595 llist_for_each_entry_safe(tmp, next, list, llnode) {
598 atomic_dec(&fcc->issing_flush);
602 complete(&tmp->wait);
610 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
612 dev_t dev = sbi->sb->s_bdev->bd_dev;
613 struct flush_cmd_control *fcc;
616 if (SM_I(sbi)->fcc_info) {
617 fcc = SM_I(sbi)->fcc_info;
618 if (fcc->f2fs_issue_flush)
623 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
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))
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);
640 SM_I(sbi)->fcc_info = NULL;
647 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
649 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
651 if (fcc && fcc->f2fs_issue_flush) {
652 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
654 fcc->f2fs_issue_flush = NULL;
655 kthread_stop(flush_thread);
659 SM_I(sbi)->fcc_info = NULL;
663 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
664 enum dirty_type dirty_type)
666 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
668 /* need not be added */
669 if (IS_CURSEG(sbi, segno))
672 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
673 dirty_i->nr_dirty[dirty_type]++;
675 if (dirty_type == DIRTY) {
676 struct seg_entry *sentry = get_seg_entry(sbi, segno);
677 enum dirty_type t = sentry->type;
679 if (unlikely(t >= DIRTY)) {
683 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
684 dirty_i->nr_dirty[t]++;
688 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
689 enum dirty_type dirty_type)
691 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
693 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
694 dirty_i->nr_dirty[dirty_type]--;
696 if (dirty_type == DIRTY) {
697 struct seg_entry *sentry = get_seg_entry(sbi, segno);
698 enum dirty_type t = sentry->type;
700 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
701 dirty_i->nr_dirty[t]--;
703 if (get_valid_blocks(sbi, segno, true) == 0)
704 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
705 dirty_i->victim_secmap);
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.
714 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
716 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
717 unsigned short valid_blocks;
719 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
722 mutex_lock(&dirty_i->seglist_lock);
724 valid_blocks = get_valid_blocks(sbi, segno, false);
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);
732 /* Recovery routine with SSR needs this */
733 __remove_dirty_segment(sbi, segno, DIRTY);
736 mutex_unlock(&dirty_i->seglist_lock);
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)
743 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
744 struct list_head *pend_list;
745 struct discard_cmd *dc;
747 f2fs_bug_on(sbi, !len);
749 pend_list = &dcc->pend_list[plist_idx(len)];
751 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
752 INIT_LIST_HEAD(&dc->list);
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;
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)
773 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
774 struct discard_cmd *dc;
776 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
778 rb_link_node(&dc->rb_node, parent, p);
779 rb_insert_color(&dc->rb_node, &dcc->root);
784 static void __detach_discard_cmd(struct discard_cmd_control *dcc,
785 struct discard_cmd *dc)
787 if (dc->state == D_DONE)
788 atomic_dec(&dcc->issing_discard);
791 rb_erase(&dc->rb_node, &dcc->root);
792 dcc->undiscard_blks -= dc->len;
794 kmem_cache_free(discard_cmd_slab, dc);
796 atomic_dec(&dcc->discard_cmd_cnt);
799 static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
800 struct discard_cmd *dc)
802 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
804 f2fs_bug_on(sbi, dc->ref);
806 if (dc->error == -EOPNOTSUPP)
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);
816 static void f2fs_submit_discard_endio(struct bio *bio)
818 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
820 dc->error = blk_status_to_errno(bio->bi_status);
822 complete_all(&dc->wait);
826 void __check_sit_bitmap(struct f2fs_sb_info *sbi,
827 block_t start, block_t end)
829 #ifdef CONFIG_F2FS_CHECK_FS
830 struct seg_entry *sentry;
833 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
837 segno = GET_SEGNO(sbi, blk);
838 sentry = get_seg_entry(sbi, segno);
839 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
841 if (end < START_BLOCK(sbi, segno + 1))
842 size = GET_BLKOFF_FROM_SEG0(sbi, end);
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);
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)
857 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
858 struct bio *bio = NULL;
860 if (dc->state != D_PREP)
863 trace_f2fs_issue_discard(dc->bdev, dc->start, dc->len);
865 dc->error = __blkdev_issue_discard(dc->bdev,
866 SECTOR_FROM_BLOCK(dc->start),
867 SECTOR_FROM_BLOCK(dc->len),
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);
875 bio->bi_private = dc;
876 bio->bi_end_io = f2fs_submit_discard_endio;
877 bio->bi_opf |= REQ_SYNC;
879 list_move_tail(&dc->list, &dcc->wait_list);
880 __check_sit_bitmap(sbi, dc->start, dc->start + dc->len);
882 f2fs_update_iostat(sbi, FS_DISCARD, 1);
885 __remove_discard_cmd(sbi, dc);
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)
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;
900 if (insert_p && insert_parent) {
901 parent = insert_parent;
906 p = __lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
908 dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
915 static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
916 struct discard_cmd *dc)
918 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
921 static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
922 struct discard_cmd *dc, block_t blkaddr)
924 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
925 struct discard_info di = dc->di;
926 bool modified = false;
928 if (dc->state == D_DONE || dc->len == 1) {
929 __remove_discard_cmd(sbi, dc);
933 dcc->undiscard_blks -= di.len;
935 if (blkaddr > di.lstart) {
936 dc->len = blkaddr - dc->lstart;
937 dcc->undiscard_blks += dc->len;
938 __relocate_discard_cmd(dcc, dc);
942 if (blkaddr < di.lstart + di.len - 1) {
944 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
945 di.start + blkaddr + 1 - di.lstart,
946 di.lstart + di.len - 1 - blkaddr,
952 dcc->undiscard_blks += dc->len;
953 __relocate_discard_cmd(dcc, dc);
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)
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;
969 mutex_lock(&dcc->cmd_lock);
971 dc = (struct discard_cmd *)__lookup_rb_tree_ret(&dcc->root,
973 (struct rb_entry **)&prev_dc,
974 (struct rb_entry **)&next_dc,
975 &insert_p, &insert_parent, true);
981 di.len = next_dc ? next_dc->lstart - lstart : len;
982 di.len = min(di.len, len);
987 struct rb_node *node;
989 struct discard_cmd *tdc = NULL;
992 di.lstart = prev_dc->lstart + prev_dc->len;
993 if (di.lstart < lstart)
995 if (di.lstart >= end)
998 if (!next_dc || next_dc->lstart > end)
999 di.len = end - di.lstart;
1001 di.len = next_dc->lstart - di.lstart;
1002 di.start = start + di.lstart - lstart;
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);
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);
1028 __remove_discard_cmd(sbi, tdc);
1033 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1034 di.len, NULL, NULL);
1041 node = rb_next(&prev_dc->rb_node);
1042 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1045 mutex_unlock(&dcc->cmd_lock);
1048 static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1049 struct block_device *bdev, block_t blkstart, block_t blklen)
1051 block_t lblkstart = blkstart;
1053 trace_f2fs_queue_discard(bdev, blkstart, blklen);
1055 if (f2fs_is_multi_device(sbi)) {
1056 int devi = f2fs_target_device_index(sbi, blkstart);
1058 blkstart -= FDEV(devi).start_blk;
1060 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
1064 static int __issue_discard_cmd(struct f2fs_sb_info *sbi, bool issue_cond)
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;
1072 bool io_interrupted = false;
1074 mutex_lock(&dcc->cmd_lock);
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);
1084 /* Hurry up to finish fstrim */
1085 if (dcc->pend_list_tag[i] & P_TRIM) {
1086 __submit_discard_cmd(sbi, dc);
1089 if (fatal_signal_pending(current))
1095 __submit_discard_cmd(sbi, dc);
1101 __submit_discard_cmd(sbi, dc);
1104 io_interrupted = true;
1107 if (++iter >= DISCARD_ISSUE_RATE)
1110 if (list_empty(pend_list) && dcc->pend_list_tag[i] & P_TRIM)
1111 dcc->pend_list_tag[i] &= (~P_TRIM);
1114 blk_finish_plug(&plug);
1115 mutex_unlock(&dcc->cmd_lock);
1117 if (!issued && io_interrupted)
1123 static void __drop_discard_cmd(struct f2fs_sb_info *sbi)
1125 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1126 struct list_head *pend_list;
1127 struct discard_cmd *dc, *tmp;
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);
1138 mutex_unlock(&dcc->cmd_lock);
1141 static void __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1142 struct discard_cmd *dc)
1144 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1146 wait_for_completion_io(&dc->wait);
1147 mutex_lock(&dcc->cmd_lock);
1148 f2fs_bug_on(sbi, dc->state != D_DONE);
1151 __remove_discard_cmd(sbi, dc);
1152 mutex_unlock(&dcc->cmd_lock);
1155 static void __wait_discard_cmd(struct f2fs_sb_info *sbi, bool wait_cond)
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;
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);
1176 mutex_unlock(&dcc->cmd_lock);
1179 __wait_one_discard_bio(sbi, dc);
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)
1187 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1188 struct discard_cmd *dc;
1189 bool need_wait = false;
1191 mutex_lock(&dcc->cmd_lock);
1192 dc = (struct discard_cmd *)__lookup_rb_tree(&dcc->root, NULL, blkaddr);
1194 if (dc->state == D_PREP) {
1195 __punch_discard_cmd(sbi, dc, blkaddr);
1201 mutex_unlock(&dcc->cmd_lock);
1204 __wait_one_discard_bio(sbi, dc);
1207 void stop_discard_thread(struct f2fs_sb_info *sbi)
1209 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1211 if (dcc && dcc->f2fs_issue_discard) {
1212 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1214 dcc->f2fs_issue_discard = NULL;
1215 kthread_stop(discard_thread);
1219 /* This comes from f2fs_put_super and f2fs_trim_fs */
1220 void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi, bool umount)
1222 __issue_discard_cmd(sbi, false);
1223 __drop_discard_cmd(sbi);
1224 __wait_discard_cmd(sbi, !umount);
1227 static void mark_discard_range_all(struct f2fs_sb_info *sbi)
1229 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
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);
1238 static int issue_discard_thread(void *data)
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;
1249 wait_event_interruptible_timeout(*q,
1250 kthread_should_stop() || freezing(current) ||
1252 msecs_to_jiffies(wait_ms));
1253 if (try_to_freeze())
1255 if (kthread_should_stop())
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);
1264 sb_start_intwrite(sbi->sb);
1266 issued = __issue_discard_cmd(sbi, true);
1268 __wait_discard_cmd(sbi, true);
1269 wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
1271 wait_ms = DEF_MAX_DISCARD_ISSUE_TIME;
1274 sb_end_intwrite(sbi->sb);
1276 } while (!kthread_should_stop());
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)
1284 sector_t sector, nr_sects;
1285 block_t lblkstart = blkstart;
1288 if (f2fs_is_multi_device(sbi)) {
1289 devi = f2fs_target_device_index(sbi, blkstart);
1290 blkstart -= FDEV(devi).start_blk;
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.
1298 switch (get_blkz_type(sbi, bdev, blkstart)) {
1300 case BLK_ZONE_TYPE_CONVENTIONAL:
1301 if (!blk_queue_discard(bdev_get_queue(bdev)))
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);
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: "",
1317 trace_f2fs_issue_reset_zone(bdev, blkstart);
1318 return blkdev_reset_zones(bdev, sector,
1319 nr_sects, GFP_NOFS);
1321 /* Unknown zone type: broken device ? */
1327 static int __issue_discard_async(struct f2fs_sb_info *sbi,
1328 struct block_device *bdev, block_t blkstart, block_t blklen)
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);
1335 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
1338 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
1339 block_t blkstart, block_t blklen)
1341 sector_t start = blkstart, len = 0;
1342 struct block_device *bdev;
1343 struct seg_entry *se;
1344 unsigned int offset;
1348 bdev = f2fs_target_device(sbi, blkstart, NULL);
1350 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1352 struct block_device *bdev2 =
1353 f2fs_target_device(sbi, i, NULL);
1355 if (bdev2 != bdev) {
1356 err = __issue_discard_async(sbi, bdev,
1366 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1367 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1369 if (!f2fs_test_and_set_bit(offset, se->discard_map))
1370 sbi->discard_blks--;
1374 err = __issue_discard_async(sbi, bdev, start, len);
1378 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
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;
1394 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
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)
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];
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)
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)
1424 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1426 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1427 list_add_tail(&de->list, head);
1430 for (i = start; i < end; i++)
1431 __set_bit_le(i, (void *)de->discard_map);
1433 SM_I(sbi)->dcc_info->nr_discards += end - start;
1438 void release_discard_addrs(struct f2fs_sb_info *sbi)
1440 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
1441 struct discard_entry *entry, *this;
1444 list_for_each_entry_safe(entry, this, head, list) {
1445 list_del(&entry->list);
1446 kmem_cache_free(discard_entry_slab, entry);
1451 * Should call clear_prefree_segments after checkpoint is done.
1453 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1455 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
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);
1464 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
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);
1475 mutex_lock(&dirty_i->seglist_lock);
1479 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1480 if (start >= MAIN_SEGS(sbi))
1482 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1485 for (i = start; i < end; i++)
1486 clear_bit(i, prefree_map);
1488 dirty_i->nr_dirty[PRE] -= end - start;
1490 if (!test_opt(sbi, DISCARD))
1493 if (force && start >= cpc->trim_start &&
1494 (end - 1) <= cpc->trim_end)
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);
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);
1510 start = start_segno + sbi->segs_per_sec;
1516 mutex_unlock(&dirty_i->seglist_lock);
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);
1525 next_pos = find_next_zero_bit_le(entry->discard_map,
1526 sbi->blocks_per_seg, cur_pos);
1527 len = next_pos - cur_pos;
1529 if (f2fs_sb_mounted_blkzoned(sbi->sb) ||
1530 (force && len < cpc->trim_minlen))
1533 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
1535 cpc->trimmed += len;
1538 next_pos = find_next_bit_le(entry->discard_map,
1539 sbi->blocks_per_seg, cur_pos);
1543 is_valid = !is_valid;
1545 if (cur_pos < sbi->blocks_per_seg)
1548 list_del(&entry->list);
1549 dcc->nr_discards -= total_len;
1550 kmem_cache_free(discard_entry_slab, entry);
1553 wake_up_discard_thread(sbi, false);
1556 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1558 dev_t dev = sbi->sb->s_bdev->bd_dev;
1559 struct discard_cmd_control *dcc;
1562 if (SM_I(sbi)->dcc_info) {
1563 dcc = SM_I(sbi)->dcc_info;
1567 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
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;
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;
1588 init_waitqueue_head(&dcc->discard_wait_queue);
1589 SM_I(sbi)->dcc_info = dcc;
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);
1596 SM_I(sbi)->dcc_info = NULL;
1603 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
1605 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1610 stop_discard_thread(sbi);
1613 SM_I(sbi)->dcc_info = NULL;
1616 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
1618 struct sit_info *sit_i = SIT_I(sbi);
1620 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
1621 sit_i->dirty_sentries++;
1628 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1629 unsigned int segno, int modified)
1631 struct seg_entry *se = get_seg_entry(sbi, segno);
1634 __mark_sit_entry_dirty(sbi, segno);
1637 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1639 struct seg_entry *se;
1640 unsigned int segno, offset;
1641 long int new_vblocks;
1643 #ifdef CONFIG_F2FS_CHECK_FS
1647 segno = GET_SEGNO(sbi, blkaddr);
1649 se = get_seg_entry(sbi, segno);
1650 new_vblocks = se->valid_blocks + del;
1651 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1653 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
1654 (new_vblocks > sbi->blocks_per_seg)));
1656 se->valid_blocks = new_vblocks;
1657 se->mtime = get_mtime(sbi);
1658 SIT_I(sbi)->max_mtime = se->mtime;
1660 /* Update valid block bitmap */
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",
1670 f2fs_bug_on(sbi, 1);
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);
1681 if (f2fs_discard_en(sbi) &&
1682 !f2fs_test_and_set_bit(offset, se->discard_map))
1683 sbi->discard_blks--;
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++;
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",
1699 f2fs_bug_on(sbi, 1);
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);
1710 if (f2fs_discard_en(sbi) &&
1711 f2fs_test_and_clear_bit(offset, se->discard_map))
1712 sbi->discard_blks++;
1714 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1715 se->ckpt_valid_blocks += del;
1717 __mark_sit_entry_dirty(sbi, segno);
1719 /* update total number of valid blocks to be written in ckpt area */
1720 SIT_I(sbi)->written_valid_blocks += del;
1722 if (sbi->segs_per_sec > 1)
1723 get_sec_entry(sbi, segno)->valid_blocks += del;
1726 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
1728 update_sit_entry(sbi, new, 1);
1729 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1730 update_sit_entry(sbi, old, -1);
1732 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1733 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
1736 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1738 unsigned int segno = GET_SEGNO(sbi, addr);
1739 struct sit_info *sit_i = SIT_I(sbi);
1741 f2fs_bug_on(sbi, addr == NULL_ADDR);
1742 if (addr == NEW_ADDR)
1745 /* add it into sit main buffer */
1746 mutex_lock(&sit_i->sentry_lock);
1748 update_sit_entry(sbi, addr, -1);
1750 /* add it into dirty seglist */
1751 locate_dirty_segment(sbi, segno);
1753 mutex_unlock(&sit_i->sentry_lock);
1756 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1758 struct sit_info *sit_i = SIT_I(sbi);
1759 unsigned int segno, offset;
1760 struct seg_entry *se;
1763 if (!is_valid_data_blkaddr(sbi, blkaddr))
1766 mutex_lock(&sit_i->sentry_lock);
1768 segno = GET_SEGNO(sbi, blkaddr);
1769 se = get_seg_entry(sbi, segno);
1770 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1772 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1775 mutex_unlock(&sit_i->sentry_lock);
1781 * This function should be resided under the curseg_mutex lock
1783 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
1784 struct f2fs_summary *sum)
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));
1793 * Calculate the number of current summary pages for writing
1795 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
1797 int valid_sum_count = 0;
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;
1805 valid_sum_count += le16_to_cpu(
1806 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1808 valid_sum_count += curseg_blkoff(sbi, i);
1812 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
1813 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1814 if (valid_sum_count <= sum_in_page)
1816 else if ((valid_sum_count - sum_in_page) <=
1817 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
1823 * Caller should put this summary page
1825 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1827 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1830 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1832 struct page *page = grab_meta_page(sbi, blk_addr);
1833 void *dst = page_address(page);
1836 memcpy(dst, src, PAGE_SIZE);
1838 memset(dst, 0, PAGE_SIZE);
1839 set_page_dirty(page);
1840 f2fs_put_page(page, 1);
1843 static void write_sum_page(struct f2fs_sb_info *sbi,
1844 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1846 update_meta_page(sbi, (void *)sum_blk, blk_addr);
1849 static void write_current_sum_page(struct f2fs_sb_info *sbi,
1850 int type, block_t blk_addr)
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;
1857 dst = (struct f2fs_summary_block *)page_address(page);
1859 mutex_lock(&curseg->curseg_mutex);
1861 down_read(&curseg->journal_rwsem);
1862 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1863 up_read(&curseg->journal_rwsem);
1865 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1866 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1868 mutex_unlock(&curseg->curseg_mutex);
1870 set_page_dirty(page);
1871 f2fs_put_page(page, 1);
1874 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
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);
1880 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
1881 return !test_bit(segno, free_i->free_segmap);
1886 * Find a new segment from the free segments bitmap to right order
1887 * This function should be returned with success, otherwise BUG
1889 static void get_new_segment(struct f2fs_sb_info *sbi,
1890 unsigned int *newseg, bool new_sec, int dir)
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;
1902 spin_lock(&free_i->segmap_lock);
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))
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,
1916 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
1919 left_start = hint - 1;
1925 while (test_bit(left_start, free_i->free_secmap)) {
1926 if (left_start > 0) {
1930 left_start = find_next_zero_bit(free_i->free_secmap,
1932 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
1938 segno = GET_SEG_FROM_SEC(sbi, secno);
1939 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
1941 /* give up on finding another zone */
1944 if (sbi->secs_per_zone == 1)
1946 if (zoneno == old_zoneno)
1948 if (dir == ALLOC_LEFT) {
1949 if (!go_left && zoneno + 1 >= total_zones)
1951 if (go_left && zoneno == 0)
1954 for (i = 0; i < NR_CURSEG_TYPE; i++)
1955 if (CURSEG_I(sbi, i)->zone == zoneno)
1958 if (i < NR_CURSEG_TYPE) {
1959 /* zone is in user, try another */
1961 hint = zoneno * sbi->secs_per_zone - 1;
1962 else if (zoneno + 1 >= total_zones)
1965 hint = (zoneno + 1) * sbi->secs_per_zone;
1967 goto find_other_zone;
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);
1974 spin_unlock(&free_i->segmap_lock);
1977 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1979 struct curseg_info *curseg = CURSEG_I(sbi, type);
1980 struct summary_footer *sum_footer;
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;
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);
1996 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
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;
2002 if (test_opt(sbi, NOHEAP) &&
2003 (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
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;
2012 * Allocate a current working segment.
2013 * This function always allocates a free segment in LFS manner.
2015 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2017 struct curseg_info *curseg = CURSEG_I(sbi, type);
2018 unsigned int segno = curseg->segno;
2019 int dir = ALLOC_LEFT;
2021 write_sum_page(sbi, curseg->sum_blk,
2022 GET_SUM_BLOCK(sbi, segno));
2023 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
2026 if (test_opt(sbi, NOHEAP))
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;
2036 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
2037 struct curseg_info *seg, block_t start)
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;
2046 for (i = 0; i < entries; i++)
2047 target_map[i] = ckpt_map[i] | cur_map[i];
2049 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2051 seg->next_blkoff = pos;
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
2059 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2060 struct curseg_info *seg)
2062 if (seg->alloc_type == SSR)
2063 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
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
2072 static void change_curseg(struct f2fs_sb_info *sbi, int type)
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;
2080 write_sum_page(sbi, curseg->sum_blk,
2081 GET_SUM_BLOCK(sbi, curseg->segno));
2082 __set_test_and_inuse(sbi, new_segno);
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);
2089 reset_curseg(sbi, type, 1);
2090 curseg->alloc_type = SSR;
2091 __next_free_blkoff(sbi, curseg, 0);
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);
2099 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
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;
2105 bool reversed = false;
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;
2113 /* For node segments, let's do SSR more intensively */
2114 if (IS_NODESEG(type)) {
2115 if (type >= CURSEG_WARM_NODE) {
2117 i = CURSEG_COLD_NODE;
2119 i = CURSEG_HOT_NODE;
2121 cnt = NR_CURSEG_NODE_TYPE;
2123 if (type >= CURSEG_WARM_DATA) {
2125 i = CURSEG_COLD_DATA;
2127 i = CURSEG_HOT_DATA;
2129 cnt = NR_CURSEG_DATA_TYPE;
2132 for (; cnt-- > 0; reversed ? i-- : i++) {
2135 if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
2136 curseg->next_segno = segno;
2144 * flush out current segment and replace it with new segment
2145 * This function should be returned with success, otherwise BUG
2147 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2148 int type, bool force)
2150 struct curseg_info *curseg = CURSEG_I(sbi, type);
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);
2162 new_curseg(sbi, type, false);
2164 stat_inc_seg_type(sbi, curseg);
2167 void allocate_new_segments(struct f2fs_sb_info *sbi)
2169 struct curseg_info *curseg;
2170 unsigned int old_segno;
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);
2181 static const struct segment_allocation default_salloc_ops = {
2182 .allocate_segment = allocate_segment_by_default,
2185 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2187 __u64 trim_start = cpc->trim_start;
2188 bool has_candidate = false;
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;
2197 mutex_unlock(&SIT_I(sbi)->sentry_lock);
2199 cpc->trim_start = trim_start;
2200 return has_candidate;
2203 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
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;
2211 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
2215 if (end <= MAIN_BLKADDR(sbi))
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;
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));
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;
2236 if (sbi->discard_blks == 0)
2238 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
2239 cpc.trim_end = end_segno;
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);
2246 mutex_lock(&sbi->gc_mutex);
2247 err = write_checkpoint(sbi, &cpc);
2248 mutex_unlock(&sbi->gc_mutex);
2254 /* It's time to issue all the filed discards */
2255 mark_discard_range_all(sbi);
2256 f2fs_wait_discard_bios(sbi, false);
2258 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
2262 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
2264 struct curseg_info *curseg = CURSEG_I(sbi, type);
2265 if (curseg->next_blkoff < sbi->blocks_per_seg)
2270 static int __get_segment_type_2(struct f2fs_io_info *fio)
2272 if (fio->type == DATA)
2273 return CURSEG_HOT_DATA;
2275 return CURSEG_HOT_NODE;
2278 static int __get_segment_type_4(struct f2fs_io_info *fio)
2280 if (fio->type == DATA) {
2281 struct inode *inode = fio->page->mapping->host;
2283 if (S_ISDIR(inode->i_mode))
2284 return CURSEG_HOT_DATA;
2286 return CURSEG_COLD_DATA;
2288 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
2289 return CURSEG_WARM_NODE;
2291 return CURSEG_COLD_NODE;
2295 static int __get_segment_type_6(struct f2fs_io_info *fio)
2297 if (fio->type == DATA) {
2298 struct inode *inode = fio->page->mapping->host;
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;
2306 if (IS_DNODE(fio->page))
2307 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
2309 return CURSEG_COLD_NODE;
2313 static int __get_segment_type(struct f2fs_io_info *fio)
2317 switch (fio->sbi->active_logs) {
2319 type = __get_segment_type_2(fio);
2322 type = __get_segment_type_4(fio);
2325 type = __get_segment_type_6(fio);
2328 f2fs_bug_on(fio->sbi, true);
2333 else if (IS_WARM(type))
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)
2345 struct sit_info *sit_i = SIT_I(sbi);
2346 struct curseg_info *curseg = CURSEG_I(sbi, type);
2348 mutex_lock(&curseg->curseg_mutex);
2349 mutex_lock(&sit_i->sentry_lock);
2351 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
2353 f2fs_wait_discard_bio(sbi, *new_blkaddr);
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.
2360 __add_sum_entry(sbi, type, sum);
2362 __refresh_next_blkoff(sbi, curseg);
2364 stat_inc_block_count(sbi, curseg);
2366 if (!__has_curseg_space(sbi, type))
2367 sit_i->s_ops->allocate_segment(sbi, type, false);
2369 * SIT information should be updated after segment allocation,
2370 * since we need to keep dirty segments precisely under SSR.
2372 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
2374 mutex_unlock(&sit_i->sentry_lock);
2376 if (page && IS_NODESEG(type)) {
2377 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
2379 f2fs_inode_chksum_set(sbi, page);
2383 struct f2fs_bio_info *io;
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);
2393 mutex_unlock(&curseg->curseg_mutex);
2396 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
2398 int type = __get_segment_type(fio);
2402 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
2403 &fio->new_blkaddr, sum, type, fio, true);
2405 /* writeout dirty page into bdev */
2406 err = f2fs_submit_page_write(fio);
2407 if (err == -EAGAIN) {
2408 fio->old_blkaddr = fio->new_blkaddr;
2413 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2414 enum iostat_type io_type)
2416 struct f2fs_io_info fio = {
2420 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
2421 .old_blkaddr = page->index,
2422 .new_blkaddr = page->index,
2424 .encrypted_page = NULL,
2428 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
2429 fio.op_flags &= ~REQ_META;
2431 set_page_writeback(page);
2432 f2fs_submit_page_write(&fio);
2434 f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
2437 void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
2439 struct f2fs_summary sum;
2441 set_summary(&sum, nid, 0, 0);
2442 do_write_page(&sum, fio);
2444 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
2447 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
2449 struct f2fs_sb_info *sbi = fio->sbi;
2450 struct f2fs_summary sum;
2451 struct node_info ni;
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);
2459 f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
2462 int rewrite_data_page(struct f2fs_io_info *fio)
2466 fio->new_blkaddr = fio->old_blkaddr;
2467 stat_inc_inplace_blocks(fio->sbi);
2469 err = f2fs_submit_page_bio(fio);
2471 f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
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)
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;
2485 unsigned short old_blkoff;
2487 segno = GET_SEGNO(sbi, new_blkaddr);
2488 se = get_seg_entry(sbi, segno);
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;
2497 type = CURSEG_WARM_DATA;
2500 if (!IS_CURSEG(sbi, segno))
2501 type = CURSEG_WARM_DATA;
2504 curseg = CURSEG_I(sbi, type);
2506 mutex_lock(&curseg->curseg_mutex);
2507 mutex_lock(&sit_i->sentry_lock);
2509 old_cursegno = curseg->segno;
2510 old_blkoff = curseg->next_blkoff;
2512 /* change the current segment */
2513 if (segno != curseg->segno) {
2514 curseg->next_segno = segno;
2515 change_curseg(sbi, type);
2518 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
2519 __add_sum_entry(sbi, type, sum);
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);
2526 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
2527 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
2529 locate_dirty_segment(sbi, old_cursegno);
2531 if (recover_curseg) {
2532 if (old_cursegno != curseg->segno) {
2533 curseg->next_segno = old_cursegno;
2534 change_curseg(sbi, type);
2536 curseg->next_blkoff = old_blkoff;
2539 mutex_unlock(&sit_i->sentry_lock);
2540 mutex_unlock(&curseg->curseg_mutex);
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)
2548 struct f2fs_summary sum;
2550 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
2552 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
2553 recover_curseg, recover_newaddr);
2555 f2fs_update_data_blkaddr(dn, new_addr);
2558 void f2fs_wait_on_page_writeback(struct page *page,
2559 enum page_type type, bool ordered)
2561 if (PageWriteback(page)) {
2562 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
2564 f2fs_submit_merged_write_cond(sbi, page->mapping->host,
2565 0, page->index, type);
2567 wait_on_page_writeback(page);
2569 wait_for_stable_page(page);
2573 void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr)
2577 if (!is_valid_data_blkaddr(sbi, blkaddr))
2580 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
2582 f2fs_wait_on_page_writeback(cpage, DATA, true);
2583 f2fs_put_page(cpage, 1);
2587 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
2589 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2590 struct curseg_info *seg_i;
2591 unsigned char *kaddr;
2596 start = start_sum_block(sbi);
2598 page = get_meta_page(sbi, start++);
2599 kaddr = (unsigned char *)page_address(page);
2601 /* Step 1: restore nat cache */
2602 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
2603 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
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;
2610 /* Step 3: restore summary entries */
2611 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2612 unsigned short blk_off;
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;
2623 if (seg_i->alloc_type == SSR)
2624 blk_off = sbi->blocks_per_seg;
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 -
2635 f2fs_put_page(page, 1);
2638 page = get_meta_page(sbi, start++);
2639 kaddr = (unsigned char *)page_address(page);
2643 f2fs_put_page(page, 1);
2647 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2649 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2650 struct f2fs_summary_block *sum;
2651 struct curseg_info *curseg;
2653 unsigned short blk_off;
2654 unsigned int segno = 0;
2655 block_t blk_addr = 0;
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 -
2662 if (__exist_node_summaries(sbi))
2663 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2665 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2667 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2669 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2671 if (__exist_node_summaries(sbi))
2672 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2673 type - CURSEG_HOT_NODE);
2675 blk_addr = GET_SUM_BLOCK(sbi, segno);
2678 new = get_meta_page(sbi, blk_addr);
2679 sum = (struct f2fs_summary_block *)page_address(new);
2681 if (IS_NODESEG(type)) {
2682 if (__exist_node_summaries(sbi)) {
2683 struct f2fs_summary *ns = &sum->entries[0];
2685 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2687 ns->ofs_in_node = 0;
2692 err = restore_node_summary(sbi, segno, sum);
2694 f2fs_put_page(new, 1);
2700 /* set uncompleted segment to curseg */
2701 curseg = CURSEG_I(sbi, type);
2702 mutex_lock(&curseg->curseg_mutex);
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);
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);
2720 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
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;
2727 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
2728 int npages = npages_for_summary_flush(sbi, true);
2731 ra_meta_pages(sbi, start_sum_block(sbi), npages,
2734 /* restore for compacted data summary */
2735 if (read_compacted_summaries(sbi))
2737 type = CURSEG_HOT_NODE;
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);
2744 for (; type <= CURSEG_COLD_NODE; type++) {
2745 err = read_normal_summaries(sbi, type);
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)
2758 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2761 unsigned char *kaddr;
2762 struct f2fs_summary *summary;
2763 struct curseg_info *seg_i;
2764 int written_size = 0;
2767 page = grab_meta_page(sbi, blkaddr++);
2768 kaddr = (unsigned char *)page_address(page);
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;
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;
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;
2787 blkoff = curseg_blkoff(sbi, i);
2789 for (j = 0; j < blkoff; j++) {
2791 page = grab_meta_page(sbi, blkaddr++);
2792 kaddr = (unsigned char *)page_address(page);
2795 summary = (struct f2fs_summary *)(kaddr + written_size);
2796 *summary = seg_i->sum_blk->entries[j];
2797 written_size += SUMMARY_SIZE;
2799 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
2803 set_page_dirty(page);
2804 f2fs_put_page(page, 1);
2809 set_page_dirty(page);
2810 f2fs_put_page(page, 1);
2814 static void write_normal_summaries(struct f2fs_sb_info *sbi,
2815 block_t blkaddr, int type)
2818 if (IS_DATASEG(type))
2819 end = type + NR_CURSEG_DATA_TYPE;
2821 end = type + NR_CURSEG_NODE_TYPE;
2823 for (i = type; i < end; i++)
2824 write_current_sum_page(sbi, i, blkaddr + (i - type));
2827 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2829 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
2830 write_compacted_summaries(sbi, start_blk);
2832 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2835 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2837 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
2840 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2841 unsigned int val, int alloc)
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)
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)
2856 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2857 return update_sits_in_cursum(journal, 1);
2862 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2865 return get_meta_page(sbi, current_sit_addr(sbi, segno));
2868 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
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;
2876 src_off = current_sit_addr(sbi, start);
2877 dst_off = next_sit_addr(sbi, src_off);
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));
2884 src_addr = page_address(src_page);
2885 dst_addr = page_address(dst_page);
2886 memcpy(dst_addr, src_addr, PAGE_SIZE);
2888 set_page_dirty(dst_page);
2889 f2fs_put_page(src_page, 1);
2891 set_to_next_sit(sit_i, start);
2896 static struct sit_entry_set *grab_sit_entry_set(void)
2898 struct sit_entry_set *ses =
2899 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
2902 INIT_LIST_HEAD(&ses->set_list);
2906 static void release_sit_entry_set(struct sit_entry_set *ses)
2908 list_del(&ses->set_list);
2909 kmem_cache_free(sit_entry_set_slab, ses);
2912 static void adjust_sit_entry_set(struct sit_entry_set *ses,
2913 struct list_head *head)
2915 struct sit_entry_set *next = ses;
2917 if (list_is_last(&ses->set_list, head))
2920 list_for_each_entry_continue(next, head, set_list)
2921 if (ses->entry_cnt <= next->entry_cnt)
2924 list_move_tail(&ses->set_list, &next->set_list);
2927 static void add_sit_entry(unsigned int segno, struct list_head *head)
2929 struct sit_entry_set *ses;
2930 unsigned int start_segno = START_SEGNO(segno);
2932 list_for_each_entry(ses, head, set_list) {
2933 if (ses->start_segno == start_segno) {
2935 adjust_sit_entry_set(ses, head);
2940 ses = grab_sit_entry_set();
2942 ses->start_segno = start_segno;
2944 list_add(&ses->set_list, head);
2947 static void add_sits_in_set(struct f2fs_sb_info *sbi)
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;
2954 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
2955 add_sit_entry(segno, set_list);
2958 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
2960 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2961 struct f2fs_journal *journal = curseg->journal;
2964 down_write(&curseg->journal_rwsem);
2965 for (i = 0; i < sits_in_cursum(journal); i++) {
2969 segno = le32_to_cpu(segno_in_journal(journal, i));
2970 dirtied = __mark_sit_entry_dirty(sbi, segno);
2973 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
2975 update_sits_in_cursum(journal, -i);
2976 up_write(&curseg->journal_rwsem);
2980 * CP calls this function, which flushes SIT entries including sit_journal,
2981 * and moves prefree segs to free segs.
2983 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
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;
2994 mutex_lock(&sit_i->sentry_lock);
2996 if (!sit_i->dirty_sentries)
3000 * add and account sit entries of dirty bitmap in sit entry
3003 add_sits_in_set(sbi);
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.
3010 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
3011 remove_sits_in_journal(sbi);
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.
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;
3027 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
3031 down_write(&curseg->journal_rwsem);
3033 page = get_next_sit_page(sbi, start_segno);
3034 raw_sit = page_address(page);
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;
3041 se = get_seg_entry(sbi, segno);
3043 /* add discard candidates */
3044 if (!(cpc->reason & CP_DISCARD)) {
3045 cpc->trim_start = segno;
3046 add_discard_addrs(sbi, cpc, false);
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) =
3055 seg_info_to_raw_sit(se,
3056 &sit_in_journal(journal, offset));
3058 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
3059 seg_info_to_raw_sit(se,
3060 &raw_sit->entries[sit_offset]);
3063 __clear_bit(segno, bitmap);
3064 sit_i->dirty_sentries--;
3069 up_write(&curseg->journal_rwsem);
3071 f2fs_put_page(page, 1);
3073 f2fs_bug_on(sbi, ses->entry_cnt);
3074 release_sit_entry_set(ses);
3077 f2fs_bug_on(sbi, !list_empty(head));
3078 f2fs_bug_on(sbi, sit_i->dirty_sentries);
3080 if (cpc->reason & CP_DISCARD) {
3081 __u64 trim_start = cpc->trim_start;
3083 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
3084 add_discard_addrs(sbi, cpc, false);
3086 cpc->trim_start = trim_start;
3088 mutex_unlock(&sit_i->sentry_lock);
3090 set_prefree_as_free_segments(sbi);
3093 static int build_sit_info(struct f2fs_sb_info *sbi)
3095 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3096 struct sit_info *sit_i;
3097 unsigned int sit_segs, start;
3099 unsigned int bitmap_size;
3101 /* allocate memory for SIT information */
3102 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
3106 SM_I(sbi)->sit_info = sit_i;
3108 sit_i->sentries = kvzalloc(MAIN_SEGS(sbi) *
3109 sizeof(struct seg_entry), GFP_KERNEL);
3110 if (!sit_i->sentries)
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)
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)
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)
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)
3142 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
3143 if (!sit_i->tmp_map)
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)
3153 /* get information related with SIT */
3154 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
3156 /* setup SIT bitmap from ckeckpoint pack */
3157 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
3158 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
3160 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
3161 if (!sit_i->sit_bitmap)
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)
3170 /* init SIT information */
3171 sit_i->s_ops = &default_salloc_ops;
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);
3185 static int build_free_segmap(struct f2fs_sb_info *sbi)
3187 struct free_segmap_info *free_i;
3188 unsigned int bitmap_size, sec_bitmap_size;
3190 /* allocate memory for free segmap information */
3191 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
3195 SM_I(sbi)->free_info = free_i;
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)
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)
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);
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);
3219 static int build_curseg(struct f2fs_sb_info *sbi)
3221 struct curseg_info *array;
3224 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
3228 SM_I(sbi)->curseg_array = array;
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)
3235 init_rwsem(&array[i].journal_rwsem);
3236 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
3238 if (!array[i].journal)
3240 array[i].segno = NULL_SEGNO;
3241 array[i].next_blkoff = 0;
3243 return restore_curseg_summaries(sbi);
3246 static int build_sit_entries(struct f2fs_sb_info *sbi)
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;
3257 block_t total_node_blocks = 0;
3260 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
3263 start = start_blk * sit_i->sents_per_block;
3264 end = (start_blk + readed) * sit_i->sents_per_block;
3266 for (; start < end && start < MAIN_SEGS(sbi); start++) {
3267 struct f2fs_sit_block *sit_blk;
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);
3276 err = check_block_count(sbi, start, &sit);
3279 seg_info_from_raw_sit(se, &sit);
3280 if (IS_NODESEG(se->type))
3281 total_node_blocks += se->valid_blocks;
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);
3289 memcpy(se->discard_map,
3291 SIT_VBLOCK_MAP_SIZE);
3292 sbi->discard_blks +=
3293 sbi->blocks_per_seg -
3298 if (sbi->segs_per_sec > 1)
3299 get_sec_entry(sbi, start)->valid_blocks +=
3302 start_blk += readed;
3303 } while (start_blk < sit_blk_cnt);
3305 down_read(&curseg->journal_rwsem);
3306 for (i = 0; i < sits_in_cursum(journal); i++) {
3307 unsigned int old_valid_blocks;
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",
3314 set_sbi_flag(sbi, SBI_NEED_FSCK);
3315 err = -EFSCORRUPTED;
3319 se = &sit_i->sentries[start];
3320 sit = sit_in_journal(journal, i);
3322 old_valid_blocks = se->valid_blocks;
3323 if (IS_NODESEG(se->type))
3324 total_node_blocks -= old_valid_blocks;
3326 err = check_block_count(sbi, start, &sit);
3329 seg_info_from_raw_sit(se, &sit);
3330 if (IS_NODESEG(se->type))
3331 total_node_blocks += se->valid_blocks;
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);
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;
3345 if (sbi->segs_per_sec > 1) {
3346 get_sec_entry(sbi, start)->valid_blocks +=
3348 get_sec_entry(sbi, start)->valid_blocks -=
3352 up_read(&curseg->journal_rwsem);
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;
3365 static void init_free_segmap(struct f2fs_sb_info *sbi)
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);
3375 SIT_I(sbi)->written_valid_blocks +=
3376 sentry->valid_blocks;
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);
3386 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
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;
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))
3399 valid_blocks = get_valid_blocks(sbi, segno, false);
3400 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
3402 if (valid_blocks > sbi->blocks_per_seg) {
3403 f2fs_bug_on(sbi, 1);
3406 mutex_lock(&dirty_i->seglist_lock);
3407 __locate_dirty_segment(sbi, segno, DIRTY);
3408 mutex_unlock(&dirty_i->seglist_lock);
3412 static int init_victim_secmap(struct f2fs_sb_info *sbi)
3414 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3415 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
3417 dirty_i->victim_secmap = kvzalloc(bitmap_size, GFP_KERNEL);
3418 if (!dirty_i->victim_secmap)
3423 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
3425 struct dirty_seglist_info *dirty_i;
3426 unsigned int bitmap_size, i;
3428 /* allocate memory for dirty segments list information */
3429 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
3433 SM_I(sbi)->dirty_info = dirty_i;
3434 mutex_init(&dirty_i->seglist_lock);
3436 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
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])
3444 init_dirty_segmap(sbi);
3445 return init_victim_secmap(sbi);
3448 static int sanity_check_curseg(struct f2fs_sb_info *sbi)
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.
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;
3461 if (f2fs_test_bit(blkofs, se->cur_valid_map))
3464 if (curseg->alloc_type == SSR)
3467 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
3468 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
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;
3484 * Update min, max modified time for cost-benefit GC algorithm
3486 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
3488 struct sit_info *sit_i = SIT_I(sbi);
3491 mutex_lock(&sit_i->sentry_lock);
3493 sit_i->min_mtime = LLONG_MAX;
3495 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
3497 unsigned long long mtime = 0;
3499 for (i = 0; i < sbi->segs_per_sec; i++)
3500 mtime += get_seg_entry(sbi, segno + i)->mtime;
3502 mtime = div_u64(mtime, sbi->segs_per_sec);
3504 if (sit_i->min_mtime > mtime)
3505 sit_i->min_mtime = mtime;
3507 sit_i->max_mtime = get_mtime(sbi);
3508 mutex_unlock(&sit_i->sentry_lock);
3511 int build_segment_manager(struct f2fs_sb_info *sbi)
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;
3518 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
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;
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;
3542 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
3544 INIT_LIST_HEAD(&sm_info->sit_entry_set);
3546 if (!f2fs_readonly(sbi->sb)) {
3547 err = create_flush_cmd_control(sbi);
3552 err = create_discard_cmd_control(sbi);
3556 err = build_sit_info(sbi);
3559 err = build_free_segmap(sbi);
3562 err = build_curseg(sbi);
3566 /* reinit free segmap based on SIT */
3567 err = build_sit_entries(sbi);
3571 init_free_segmap(sbi);
3572 err = build_dirty_segmap(sbi);
3576 err = sanity_check_curseg(sbi);
3580 init_min_max_mtime(sbi);
3584 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
3585 enum dirty_type dirty_type)
3587 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
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);
3595 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
3597 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3598 kvfree(dirty_i->victim_secmap);
3601 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
3603 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
3609 /* discard pre-free/dirty segments list */
3610 for (i = 0; i < NR_DIRTY_TYPE; i++)
3611 discard_dirty_segmap(sbi, i);
3613 destroy_victim_secmap(sbi);
3614 SM_I(sbi)->dirty_info = NULL;
3618 static void destroy_curseg(struct f2fs_sb_info *sbi)
3620 struct curseg_info *array = SM_I(sbi)->curseg_array;
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);
3633 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
3635 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
3638 SM_I(sbi)->free_info = NULL;
3639 kvfree(free_i->free_segmap);
3640 kvfree(free_i->free_secmap);
3644 static void destroy_sit_info(struct f2fs_sb_info *sbi)
3646 struct sit_info *sit_i = SIT_I(sbi);
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);
3658 kfree(sit_i->sentries[start].ckpt_valid_map);
3659 kfree(sit_i->sentries[start].discard_map);
3662 kfree(sit_i->tmp_map);
3664 kvfree(sit_i->sentries);
3665 kvfree(sit_i->sec_entries);
3666 kvfree(sit_i->dirty_sentries_bitmap);
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);
3676 void destroy_segment_manager(struct f2fs_sb_info *sbi)
3678 struct f2fs_sm_info *sm_info = SM_I(sbi);
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;
3692 int __init create_segment_manager_caches(void)
3694 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
3695 sizeof(struct discard_entry));
3696 if (!discard_entry_slab)
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;
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;
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;
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);
3725 void destroy_segment_manager_caches(void)
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);