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
);
229 struct dnode_of_data dn
;
232 trace_f2fs_commit_inmem_page(page
, INMEM_REVOKE
);
234 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
235 err
= get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
);
237 if (err
== -ENOMEM
) {
238 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
245 get_node_info(sbi
, dn
.nid
, &ni
);
246 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
,
247 cur
->old_addr
, ni
.version
, true, true);
251 /* we don't need to invalidate this in the sccessful status */
253 ClearPageUptodate(page
);
254 set_page_private(page
, 0);
255 ClearPagePrivate(page
);
256 f2fs_put_page(page
, 1);
258 list_del(&cur
->list
);
259 kmem_cache_free(inmem_entry_slab
, cur
);
260 dec_page_count(F2FS_I_SB(inode
), F2FS_INMEM_PAGES
);
265 void drop_inmem_pages(struct inode
*inode
)
267 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
269 mutex_lock(&fi
->inmem_lock
);
270 __revoke_inmem_pages(inode
, &fi
->inmem_pages
, true, false);
271 mutex_unlock(&fi
->inmem_lock
);
273 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
274 clear_inode_flag(inode
, FI_HOT_DATA
);
275 stat_dec_atomic_write(inode
);
278 void drop_inmem_page(struct inode
*inode
, struct page
*page
)
280 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
281 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
282 struct list_head
*head
= &fi
->inmem_pages
;
283 struct inmem_pages
*cur
= NULL
;
285 f2fs_bug_on(sbi
, !IS_ATOMIC_WRITTEN_PAGE(page
));
287 mutex_lock(&fi
->inmem_lock
);
288 list_for_each_entry(cur
, head
, list
) {
289 if (cur
->page
== page
)
293 f2fs_bug_on(sbi
, !cur
|| cur
->page
!= page
);
294 list_del(&cur
->list
);
295 mutex_unlock(&fi
->inmem_lock
);
297 dec_page_count(sbi
, F2FS_INMEM_PAGES
);
298 kmem_cache_free(inmem_entry_slab
, cur
);
300 ClearPageUptodate(page
);
301 set_page_private(page
, 0);
302 ClearPagePrivate(page
);
303 f2fs_put_page(page
, 0);
305 trace_f2fs_commit_inmem_page(page
, INMEM_INVALIDATE
);
308 static int __commit_inmem_pages(struct inode
*inode
,
309 struct list_head
*revoke_list
)
311 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
312 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
313 struct inmem_pages
*cur
, *tmp
;
314 struct f2fs_io_info fio
= {
318 .op_flags
= REQ_SYNC
| REQ_PRIO
,
319 .io_type
= FS_DATA_IO
,
321 pgoff_t last_idx
= ULONG_MAX
;
324 list_for_each_entry_safe(cur
, tmp
, &fi
->inmem_pages
, list
) {
325 struct page
*page
= cur
->page
;
328 if (page
->mapping
== inode
->i_mapping
) {
329 trace_f2fs_commit_inmem_page(page
, INMEM
);
331 set_page_dirty(page
);
332 f2fs_wait_on_page_writeback(page
, DATA
, true);
333 if (clear_page_dirty_for_io(page
)) {
334 inode_dec_dirty_pages(inode
);
335 remove_dirty_inode(inode
);
339 fio
.old_blkaddr
= NULL_ADDR
;
340 fio
.encrypted_page
= NULL
;
341 fio
.need_lock
= LOCK_DONE
;
342 err
= do_write_data_page(&fio
);
344 if (err
== -ENOMEM
) {
345 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
352 /* record old blkaddr for revoking */
353 cur
->old_addr
= fio
.old_blkaddr
;
354 last_idx
= page
->index
;
357 list_move_tail(&cur
->list
, revoke_list
);
360 if (last_idx
!= ULONG_MAX
)
361 f2fs_submit_merged_write_cond(sbi
, inode
, 0, last_idx
, DATA
);
364 __revoke_inmem_pages(inode
, revoke_list
, false, false);
369 int commit_inmem_pages(struct inode
*inode
)
371 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
372 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
373 struct list_head revoke_list
;
376 INIT_LIST_HEAD(&revoke_list
);
377 f2fs_balance_fs(sbi
, true);
380 set_inode_flag(inode
, FI_ATOMIC_COMMIT
);
382 mutex_lock(&fi
->inmem_lock
);
383 err
= __commit_inmem_pages(inode
, &revoke_list
);
387 * try to revoke all committed pages, but still we could fail
388 * due to no memory or other reason, if that happened, EAGAIN
389 * will be returned, which means in such case, transaction is
390 * already not integrity, caller should use journal to do the
391 * recovery or rewrite & commit last transaction. For other
392 * error number, revoking was done by filesystem itself.
394 ret
= __revoke_inmem_pages(inode
, &revoke_list
, false, true);
398 /* drop all uncommitted pages */
399 __revoke_inmem_pages(inode
, &fi
->inmem_pages
, true, false);
401 mutex_unlock(&fi
->inmem_lock
);
403 clear_inode_flag(inode
, FI_ATOMIC_COMMIT
);
410 * This function balances dirty node and dentry pages.
411 * In addition, it controls garbage collection.
413 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
, bool need
)
415 #ifdef CONFIG_F2FS_FAULT_INJECTION
416 if (time_to_inject(sbi
, FAULT_CHECKPOINT
)) {
417 f2fs_show_injection_info(FAULT_CHECKPOINT
);
418 f2fs_stop_checkpoint(sbi
, false);
422 /* balance_fs_bg is able to be pending */
423 if (need
&& excess_cached_nats(sbi
))
424 f2fs_balance_fs_bg(sbi
);
427 * We should do GC or end up with checkpoint, if there are so many dirty
428 * dir/node pages without enough free segments.
430 if (has_not_enough_free_secs(sbi
, 0, 0)) {
431 mutex_lock(&sbi
->gc_mutex
);
432 f2fs_gc(sbi
, false, false, NULL_SEGNO
);
436 void f2fs_balance_fs_bg(struct f2fs_sb_info
*sbi
)
438 /* try to shrink extent cache when there is no enough memory */
439 if (!available_free_memory(sbi
, EXTENT_CACHE
))
440 f2fs_shrink_extent_tree(sbi
, EXTENT_CACHE_SHRINK_NUMBER
);
442 /* check the # of cached NAT entries */
443 if (!available_free_memory(sbi
, NAT_ENTRIES
))
444 try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
);
446 if (!available_free_memory(sbi
, FREE_NIDS
))
447 try_to_free_nids(sbi
, MAX_FREE_NIDS
);
449 build_free_nids(sbi
, false, false);
451 if (!is_idle(sbi
) && !excess_dirty_nats(sbi
))
454 /* checkpoint is the only way to shrink partial cached entries */
455 if (!available_free_memory(sbi
, NAT_ENTRIES
) ||
456 !available_free_memory(sbi
, INO_ENTRIES
) ||
457 excess_prefree_segs(sbi
) ||
458 excess_dirty_nats(sbi
) ||
459 f2fs_time_over(sbi
, CP_TIME
)) {
460 if (test_opt(sbi
, DATA_FLUSH
)) {
461 struct blk_plug plug
;
463 blk_start_plug(&plug
);
464 sync_dirty_inodes(sbi
, FILE_INODE
);
465 blk_finish_plug(&plug
);
467 f2fs_sync_fs(sbi
->sb
, true);
468 stat_inc_bg_cp_count(sbi
->stat_info
);
472 static int __submit_flush_wait(struct f2fs_sb_info
*sbi
,
473 struct block_device
*bdev
)
475 struct bio
*bio
= f2fs_bio_alloc(0);
478 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_PREFLUSH
;
479 bio_set_dev(bio
, bdev
);
480 ret
= submit_bio_wait(bio
);
483 trace_f2fs_issue_flush(bdev
, test_opt(sbi
, NOBARRIER
),
484 test_opt(sbi
, FLUSH_MERGE
), ret
);
488 static int submit_flush_wait(struct f2fs_sb_info
*sbi
)
490 int ret
= __submit_flush_wait(sbi
, sbi
->sb
->s_bdev
);
493 if (!sbi
->s_ndevs
|| ret
)
496 for (i
= 1; i
< sbi
->s_ndevs
; i
++) {
497 ret
= __submit_flush_wait(sbi
, FDEV(i
).bdev
);
504 static int issue_flush_thread(void *data
)
506 struct f2fs_sb_info
*sbi
= data
;
507 struct flush_cmd_control
*fcc
= SM_I(sbi
)->fcc_info
;
508 wait_queue_head_t
*q
= &fcc
->flush_wait_queue
;
510 if (kthread_should_stop())
513 sb_start_intwrite(sbi
->sb
);
515 if (!llist_empty(&fcc
->issue_list
)) {
516 struct flush_cmd
*cmd
, *next
;
519 fcc
->dispatch_list
= llist_del_all(&fcc
->issue_list
);
520 fcc
->dispatch_list
= llist_reverse_order(fcc
->dispatch_list
);
522 ret
= submit_flush_wait(sbi
);
523 atomic_inc(&fcc
->issued_flush
);
525 llist_for_each_entry_safe(cmd
, next
,
526 fcc
->dispatch_list
, llnode
) {
528 complete(&cmd
->wait
);
530 fcc
->dispatch_list
= NULL
;
533 sb_end_intwrite(sbi
->sb
);
535 wait_event_interruptible(*q
,
536 kthread_should_stop() || !llist_empty(&fcc
->issue_list
));
540 int f2fs_issue_flush(struct f2fs_sb_info
*sbi
)
542 struct flush_cmd_control
*fcc
= SM_I(sbi
)->fcc_info
;
543 struct flush_cmd cmd
;
546 if (test_opt(sbi
, NOBARRIER
))
549 if (!test_opt(sbi
, FLUSH_MERGE
)) {
550 ret
= submit_flush_wait(sbi
);
551 atomic_inc(&fcc
->issued_flush
);
555 if (atomic_inc_return(&fcc
->issing_flush
) == 1) {
556 ret
= submit_flush_wait(sbi
);
557 atomic_dec(&fcc
->issing_flush
);
559 atomic_inc(&fcc
->issued_flush
);
563 init_completion(&cmd
.wait
);
565 llist_add(&cmd
.llnode
, &fcc
->issue_list
);
567 /* update issue_list before we wake up issue_flush thread */
570 if (waitqueue_active(&fcc
->flush_wait_queue
))
571 wake_up(&fcc
->flush_wait_queue
);
573 if (fcc
->f2fs_issue_flush
) {
574 wait_for_completion(&cmd
.wait
);
575 atomic_dec(&fcc
->issing_flush
);
577 struct llist_node
*list
;
579 list
= llist_del_all(&fcc
->issue_list
);
581 wait_for_completion(&cmd
.wait
);
582 atomic_dec(&fcc
->issing_flush
);
584 struct flush_cmd
*tmp
, *next
;
586 ret
= submit_flush_wait(sbi
);
588 llist_for_each_entry_safe(tmp
, next
, list
, llnode
) {
591 atomic_dec(&fcc
->issing_flush
);
595 complete(&tmp
->wait
);
603 int create_flush_cmd_control(struct f2fs_sb_info
*sbi
)
605 dev_t dev
= sbi
->sb
->s_bdev
->bd_dev
;
606 struct flush_cmd_control
*fcc
;
609 if (SM_I(sbi
)->fcc_info
) {
610 fcc
= SM_I(sbi
)->fcc_info
;
611 if (fcc
->f2fs_issue_flush
)
616 fcc
= kzalloc(sizeof(struct flush_cmd_control
), GFP_KERNEL
);
619 atomic_set(&fcc
->issued_flush
, 0);
620 atomic_set(&fcc
->issing_flush
, 0);
621 init_waitqueue_head(&fcc
->flush_wait_queue
);
622 init_llist_head(&fcc
->issue_list
);
623 SM_I(sbi
)->fcc_info
= fcc
;
624 if (!test_opt(sbi
, FLUSH_MERGE
))
628 fcc
->f2fs_issue_flush
= kthread_run(issue_flush_thread
, sbi
,
629 "f2fs_flush-%u:%u", MAJOR(dev
), MINOR(dev
));
630 if (IS_ERR(fcc
->f2fs_issue_flush
)) {
631 err
= PTR_ERR(fcc
->f2fs_issue_flush
);
633 SM_I(sbi
)->fcc_info
= NULL
;
640 void destroy_flush_cmd_control(struct f2fs_sb_info
*sbi
, bool free
)
642 struct flush_cmd_control
*fcc
= SM_I(sbi
)->fcc_info
;
644 if (fcc
&& fcc
->f2fs_issue_flush
) {
645 struct task_struct
*flush_thread
= fcc
->f2fs_issue_flush
;
647 fcc
->f2fs_issue_flush
= NULL
;
648 kthread_stop(flush_thread
);
652 SM_I(sbi
)->fcc_info
= NULL
;
656 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
657 enum dirty_type dirty_type
)
659 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
661 /* need not be added */
662 if (IS_CURSEG(sbi
, segno
))
665 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
666 dirty_i
->nr_dirty
[dirty_type
]++;
668 if (dirty_type
== DIRTY
) {
669 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
670 enum dirty_type t
= sentry
->type
;
672 if (unlikely(t
>= DIRTY
)) {
676 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[t
]))
677 dirty_i
->nr_dirty
[t
]++;
681 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
682 enum dirty_type dirty_type
)
684 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
686 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
687 dirty_i
->nr_dirty
[dirty_type
]--;
689 if (dirty_type
== DIRTY
) {
690 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
691 enum dirty_type t
= sentry
->type
;
693 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
694 dirty_i
->nr_dirty
[t
]--;
696 if (get_valid_blocks(sbi
, segno
, true) == 0)
697 clear_bit(GET_SEC_FROM_SEG(sbi
, segno
),
698 dirty_i
->victim_secmap
);
703 * Should not occur error such as -ENOMEM.
704 * Adding dirty entry into seglist is not critical operation.
705 * If a given segment is one of current working segments, it won't be added.
707 static void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
709 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
710 unsigned short valid_blocks
;
712 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
715 mutex_lock(&dirty_i
->seglist_lock
);
717 valid_blocks
= get_valid_blocks(sbi
, segno
, false);
719 if (valid_blocks
== 0) {
720 __locate_dirty_segment(sbi
, segno
, PRE
);
721 __remove_dirty_segment(sbi
, segno
, DIRTY
);
722 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
723 __locate_dirty_segment(sbi
, segno
, DIRTY
);
725 /* Recovery routine with SSR needs this */
726 __remove_dirty_segment(sbi
, segno
, DIRTY
);
729 mutex_unlock(&dirty_i
->seglist_lock
);
732 static struct discard_cmd
*__create_discard_cmd(struct f2fs_sb_info
*sbi
,
733 struct block_device
*bdev
, block_t lstart
,
734 block_t start
, block_t len
)
736 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
737 struct list_head
*pend_list
;
738 struct discard_cmd
*dc
;
740 f2fs_bug_on(sbi
, !len
);
742 pend_list
= &dcc
->pend_list
[plist_idx(len
)];
744 dc
= f2fs_kmem_cache_alloc(discard_cmd_slab
, GFP_NOFS
);
745 INIT_LIST_HEAD(&dc
->list
);
753 init_completion(&dc
->wait
);
754 list_add_tail(&dc
->list
, pend_list
);
755 atomic_inc(&dcc
->discard_cmd_cnt
);
756 dcc
->undiscard_blks
+= len
;
761 static struct discard_cmd
*__attach_discard_cmd(struct f2fs_sb_info
*sbi
,
762 struct block_device
*bdev
, block_t lstart
,
763 block_t start
, block_t len
,
764 struct rb_node
*parent
, struct rb_node
**p
)
766 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
767 struct discard_cmd
*dc
;
769 dc
= __create_discard_cmd(sbi
, bdev
, lstart
, start
, len
);
771 rb_link_node(&dc
->rb_node
, parent
, p
);
772 rb_insert_color(&dc
->rb_node
, &dcc
->root
);
777 static void __detach_discard_cmd(struct discard_cmd_control
*dcc
,
778 struct discard_cmd
*dc
)
780 if (dc
->state
== D_DONE
)
781 atomic_dec(&dcc
->issing_discard
);
784 rb_erase(&dc
->rb_node
, &dcc
->root
);
785 dcc
->undiscard_blks
-= dc
->len
;
787 kmem_cache_free(discard_cmd_slab
, dc
);
789 atomic_dec(&dcc
->discard_cmd_cnt
);
792 static void __remove_discard_cmd(struct f2fs_sb_info
*sbi
,
793 struct discard_cmd
*dc
)
795 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
797 f2fs_bug_on(sbi
, dc
->ref
);
799 if (dc
->error
== -EOPNOTSUPP
)
803 f2fs_msg(sbi
->sb
, KERN_INFO
,
804 "Issue discard(%u, %u, %u) failed, ret: %d",
805 dc
->lstart
, dc
->start
, dc
->len
, dc
->error
);
806 __detach_discard_cmd(dcc
, dc
);
809 static void f2fs_submit_discard_endio(struct bio
*bio
)
811 struct discard_cmd
*dc
= (struct discard_cmd
*)bio
->bi_private
;
813 dc
->error
= blk_status_to_errno(bio
->bi_status
);
815 complete_all(&dc
->wait
);
819 void __check_sit_bitmap(struct f2fs_sb_info
*sbi
,
820 block_t start
, block_t end
)
822 #ifdef CONFIG_F2FS_CHECK_FS
823 struct seg_entry
*sentry
;
826 unsigned long offset
, size
, max_blocks
= sbi
->blocks_per_seg
;
830 segno
= GET_SEGNO(sbi
, blk
);
831 sentry
= get_seg_entry(sbi
, segno
);
832 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blk
);
834 if (end
< START_BLOCK(sbi
, segno
+ 1))
835 size
= GET_BLKOFF_FROM_SEG0(sbi
, end
);
838 map
= (unsigned long *)(sentry
->cur_valid_map
);
839 offset
= __find_rev_next_bit(map
, size
, offset
);
840 f2fs_bug_on(sbi
, offset
!= size
);
841 blk
= START_BLOCK(sbi
, segno
+ 1);
846 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
847 static void __submit_discard_cmd(struct f2fs_sb_info
*sbi
,
848 struct discard_cmd
*dc
)
850 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
851 struct bio
*bio
= NULL
;
853 if (dc
->state
!= D_PREP
)
856 trace_f2fs_issue_discard(dc
->bdev
, dc
->start
, dc
->len
);
858 dc
->error
= __blkdev_issue_discard(dc
->bdev
,
859 SECTOR_FROM_BLOCK(dc
->start
),
860 SECTOR_FROM_BLOCK(dc
->len
),
863 /* should keep before submission to avoid D_DONE right away */
864 dc
->state
= D_SUBMIT
;
865 atomic_inc(&dcc
->issued_discard
);
866 atomic_inc(&dcc
->issing_discard
);
868 bio
->bi_private
= dc
;
869 bio
->bi_end_io
= f2fs_submit_discard_endio
;
870 bio
->bi_opf
|= REQ_SYNC
;
872 list_move_tail(&dc
->list
, &dcc
->wait_list
);
873 __check_sit_bitmap(sbi
, dc
->start
, dc
->start
+ dc
->len
);
875 f2fs_update_iostat(sbi
, FS_DISCARD
, 1);
878 __remove_discard_cmd(sbi
, dc
);
882 static struct discard_cmd
*__insert_discard_tree(struct f2fs_sb_info
*sbi
,
883 struct block_device
*bdev
, block_t lstart
,
884 block_t start
, block_t len
,
885 struct rb_node
**insert_p
,
886 struct rb_node
*insert_parent
)
888 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
889 struct rb_node
**p
= &dcc
->root
.rb_node
;
890 struct rb_node
*parent
= NULL
;
891 struct discard_cmd
*dc
= NULL
;
893 if (insert_p
&& insert_parent
) {
894 parent
= insert_parent
;
899 p
= __lookup_rb_tree_for_insert(sbi
, &dcc
->root
, &parent
, lstart
);
901 dc
= __attach_discard_cmd(sbi
, bdev
, lstart
, start
, len
, parent
, p
);
908 static void __relocate_discard_cmd(struct discard_cmd_control
*dcc
,
909 struct discard_cmd
*dc
)
911 list_move_tail(&dc
->list
, &dcc
->pend_list
[plist_idx(dc
->len
)]);
914 static void __punch_discard_cmd(struct f2fs_sb_info
*sbi
,
915 struct discard_cmd
*dc
, block_t blkaddr
)
917 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
918 struct discard_info di
= dc
->di
;
919 bool modified
= false;
921 if (dc
->state
== D_DONE
|| dc
->len
== 1) {
922 __remove_discard_cmd(sbi
, dc
);
926 dcc
->undiscard_blks
-= di
.len
;
928 if (blkaddr
> di
.lstart
) {
929 dc
->len
= blkaddr
- dc
->lstart
;
930 dcc
->undiscard_blks
+= dc
->len
;
931 __relocate_discard_cmd(dcc
, dc
);
935 if (blkaddr
< di
.lstart
+ di
.len
- 1) {
937 __insert_discard_tree(sbi
, dc
->bdev
, blkaddr
+ 1,
938 di
.start
+ blkaddr
+ 1 - di
.lstart
,
939 di
.lstart
+ di
.len
- 1 - blkaddr
,
945 dcc
->undiscard_blks
+= dc
->len
;
946 __relocate_discard_cmd(dcc
, dc
);
951 static void __update_discard_tree_range(struct f2fs_sb_info
*sbi
,
952 struct block_device
*bdev
, block_t lstart
,
953 block_t start
, block_t len
)
955 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
956 struct discard_cmd
*prev_dc
= NULL
, *next_dc
= NULL
;
957 struct discard_cmd
*dc
;
958 struct discard_info di
= {0};
959 struct rb_node
**insert_p
= NULL
, *insert_parent
= NULL
;
960 block_t end
= lstart
+ len
;
962 mutex_lock(&dcc
->cmd_lock
);
964 dc
= (struct discard_cmd
*)__lookup_rb_tree_ret(&dcc
->root
,
966 (struct rb_entry
**)&prev_dc
,
967 (struct rb_entry
**)&next_dc
,
968 &insert_p
, &insert_parent
, true);
974 di
.len
= next_dc
? next_dc
->lstart
- lstart
: len
;
975 di
.len
= min(di
.len
, len
);
980 struct rb_node
*node
;
982 struct discard_cmd
*tdc
= NULL
;
985 di
.lstart
= prev_dc
->lstart
+ prev_dc
->len
;
986 if (di
.lstart
< lstart
)
988 if (di
.lstart
>= end
)
991 if (!next_dc
|| next_dc
->lstart
> end
)
992 di
.len
= end
- di
.lstart
;
994 di
.len
= next_dc
->lstart
- di
.lstart
;
995 di
.start
= start
+ di
.lstart
- lstart
;
1001 if (prev_dc
&& prev_dc
->state
== D_PREP
&&
1002 prev_dc
->bdev
== bdev
&&
1003 __is_discard_back_mergeable(&di
, &prev_dc
->di
)) {
1004 prev_dc
->di
.len
+= di
.len
;
1005 dcc
->undiscard_blks
+= di
.len
;
1006 __relocate_discard_cmd(dcc
, prev_dc
);
1012 if (next_dc
&& next_dc
->state
== D_PREP
&&
1013 next_dc
->bdev
== bdev
&&
1014 __is_discard_front_mergeable(&di
, &next_dc
->di
)) {
1015 next_dc
->di
.lstart
= di
.lstart
;
1016 next_dc
->di
.len
+= di
.len
;
1017 next_dc
->di
.start
= di
.start
;
1018 dcc
->undiscard_blks
+= di
.len
;
1019 __relocate_discard_cmd(dcc
, next_dc
);
1021 __remove_discard_cmd(sbi
, tdc
);
1026 __insert_discard_tree(sbi
, bdev
, di
.lstart
, di
.start
,
1027 di
.len
, NULL
, NULL
);
1034 node
= rb_next(&prev_dc
->rb_node
);
1035 next_dc
= rb_entry_safe(node
, struct discard_cmd
, rb_node
);
1038 mutex_unlock(&dcc
->cmd_lock
);
1041 static int __queue_discard_cmd(struct f2fs_sb_info
*sbi
,
1042 struct block_device
*bdev
, block_t blkstart
, block_t blklen
)
1044 block_t lblkstart
= blkstart
;
1046 trace_f2fs_queue_discard(bdev
, blkstart
, blklen
);
1049 int devi
= f2fs_target_device_index(sbi
, blkstart
);
1051 blkstart
-= FDEV(devi
).start_blk
;
1053 __update_discard_tree_range(sbi
, bdev
, lblkstart
, blkstart
, blklen
);
1057 static int __issue_discard_cmd(struct f2fs_sb_info
*sbi
, bool issue_cond
)
1059 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1060 struct list_head
*pend_list
;
1061 struct discard_cmd
*dc
, *tmp
;
1062 struct blk_plug plug
;
1063 int iter
= 0, issued
= 0;
1065 bool io_interrupted
= false;
1067 mutex_lock(&dcc
->cmd_lock
);
1069 !__check_rb_tree_consistence(sbi
, &dcc
->root
));
1070 blk_start_plug(&plug
);
1071 for (i
= MAX_PLIST_NUM
- 1;
1072 i
>= 0 && plist_issue(dcc
->pend_list_tag
[i
]); i
--) {
1073 pend_list
= &dcc
->pend_list
[i
];
1074 list_for_each_entry_safe(dc
, tmp
, pend_list
, list
) {
1075 f2fs_bug_on(sbi
, dc
->state
!= D_PREP
);
1077 /* Hurry up to finish fstrim */
1078 if (dcc
->pend_list_tag
[i
] & P_TRIM
) {
1079 __submit_discard_cmd(sbi
, dc
);
1082 if (fatal_signal_pending(current
))
1088 __submit_discard_cmd(sbi
, dc
);
1094 __submit_discard_cmd(sbi
, dc
);
1097 io_interrupted
= true;
1100 if (++iter
>= DISCARD_ISSUE_RATE
)
1103 if (list_empty(pend_list
) && dcc
->pend_list_tag
[i
] & P_TRIM
)
1104 dcc
->pend_list_tag
[i
] &= (~P_TRIM
);
1107 blk_finish_plug(&plug
);
1108 mutex_unlock(&dcc
->cmd_lock
);
1110 if (!issued
&& io_interrupted
)
1116 static void __drop_discard_cmd(struct f2fs_sb_info
*sbi
)
1118 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1119 struct list_head
*pend_list
;
1120 struct discard_cmd
*dc
, *tmp
;
1123 mutex_lock(&dcc
->cmd_lock
);
1124 for (i
= MAX_PLIST_NUM
- 1; i
>= 0; i
--) {
1125 pend_list
= &dcc
->pend_list
[i
];
1126 list_for_each_entry_safe(dc
, tmp
, pend_list
, list
) {
1127 f2fs_bug_on(sbi
, dc
->state
!= D_PREP
);
1128 __remove_discard_cmd(sbi
, dc
);
1131 mutex_unlock(&dcc
->cmd_lock
);
1134 static void __wait_one_discard_bio(struct f2fs_sb_info
*sbi
,
1135 struct discard_cmd
*dc
)
1137 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1139 wait_for_completion_io(&dc
->wait
);
1140 mutex_lock(&dcc
->cmd_lock
);
1141 f2fs_bug_on(sbi
, dc
->state
!= D_DONE
);
1144 __remove_discard_cmd(sbi
, dc
);
1145 mutex_unlock(&dcc
->cmd_lock
);
1148 static void __wait_discard_cmd(struct f2fs_sb_info
*sbi
, bool wait_cond
)
1150 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1151 struct list_head
*wait_list
= &(dcc
->wait_list
);
1152 struct discard_cmd
*dc
, *tmp
;
1158 mutex_lock(&dcc
->cmd_lock
);
1159 list_for_each_entry_safe(dc
, tmp
, wait_list
, list
) {
1160 if (!wait_cond
|| (dc
->state
== D_DONE
&& !dc
->ref
)) {
1161 wait_for_completion_io(&dc
->wait
);
1162 __remove_discard_cmd(sbi
, dc
);
1169 mutex_unlock(&dcc
->cmd_lock
);
1172 __wait_one_discard_bio(sbi
, dc
);
1177 /* This should be covered by global mutex, &sit_i->sentry_lock */
1178 void f2fs_wait_discard_bio(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1180 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1181 struct discard_cmd
*dc
;
1182 bool need_wait
= false;
1184 mutex_lock(&dcc
->cmd_lock
);
1185 dc
= (struct discard_cmd
*)__lookup_rb_tree(&dcc
->root
, NULL
, blkaddr
);
1187 if (dc
->state
== D_PREP
) {
1188 __punch_discard_cmd(sbi
, dc
, blkaddr
);
1194 mutex_unlock(&dcc
->cmd_lock
);
1197 __wait_one_discard_bio(sbi
, dc
);
1200 void stop_discard_thread(struct f2fs_sb_info
*sbi
)
1202 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1204 if (dcc
&& dcc
->f2fs_issue_discard
) {
1205 struct task_struct
*discard_thread
= dcc
->f2fs_issue_discard
;
1207 dcc
->f2fs_issue_discard
= NULL
;
1208 kthread_stop(discard_thread
);
1212 /* This comes from f2fs_put_super and f2fs_trim_fs */
1213 void f2fs_wait_discard_bios(struct f2fs_sb_info
*sbi
, bool umount
)
1215 __issue_discard_cmd(sbi
, false);
1216 __drop_discard_cmd(sbi
);
1217 __wait_discard_cmd(sbi
, !umount
);
1220 static void mark_discard_range_all(struct f2fs_sb_info
*sbi
)
1222 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1225 mutex_lock(&dcc
->cmd_lock
);
1226 for (i
= 0; i
< MAX_PLIST_NUM
; i
++)
1227 dcc
->pend_list_tag
[i
] |= P_TRIM
;
1228 mutex_unlock(&dcc
->cmd_lock
);
1231 static int issue_discard_thread(void *data
)
1233 struct f2fs_sb_info
*sbi
= data
;
1234 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1235 wait_queue_head_t
*q
= &dcc
->discard_wait_queue
;
1236 unsigned int wait_ms
= DEF_MIN_DISCARD_ISSUE_TIME
;
1242 wait_event_interruptible_timeout(*q
,
1243 kthread_should_stop() || freezing(current
) ||
1245 msecs_to_jiffies(wait_ms
));
1246 if (try_to_freeze())
1248 if (kthread_should_stop())
1251 if (dcc
->discard_wake
) {
1252 dcc
->discard_wake
= 0;
1253 if (sbi
->gc_thread
&& sbi
->gc_thread
->gc_urgent
)
1254 mark_discard_range_all(sbi
);
1257 sb_start_intwrite(sbi
->sb
);
1259 issued
= __issue_discard_cmd(sbi
, true);
1261 __wait_discard_cmd(sbi
, true);
1262 wait_ms
= DEF_MIN_DISCARD_ISSUE_TIME
;
1264 wait_ms
= DEF_MAX_DISCARD_ISSUE_TIME
;
1267 sb_end_intwrite(sbi
->sb
);
1269 } while (!kthread_should_stop());
1273 #ifdef CONFIG_BLK_DEV_ZONED
1274 static int __f2fs_issue_discard_zone(struct f2fs_sb_info
*sbi
,
1275 struct block_device
*bdev
, block_t blkstart
, block_t blklen
)
1277 sector_t sector
, nr_sects
;
1278 block_t lblkstart
= blkstart
;
1282 devi
= f2fs_target_device_index(sbi
, blkstart
);
1283 blkstart
-= FDEV(devi
).start_blk
;
1287 * We need to know the type of the zone: for conventional zones,
1288 * use regular discard if the drive supports it. For sequential
1289 * zones, reset the zone write pointer.
1291 switch (get_blkz_type(sbi
, bdev
, blkstart
)) {
1293 case BLK_ZONE_TYPE_CONVENTIONAL
:
1294 if (!blk_queue_discard(bdev_get_queue(bdev
)))
1296 return __queue_discard_cmd(sbi
, bdev
, lblkstart
, blklen
);
1297 case BLK_ZONE_TYPE_SEQWRITE_REQ
:
1298 case BLK_ZONE_TYPE_SEQWRITE_PREF
:
1299 sector
= SECTOR_FROM_BLOCK(blkstart
);
1300 nr_sects
= SECTOR_FROM_BLOCK(blklen
);
1302 if (sector
& (bdev_zone_sectors(bdev
) - 1) ||
1303 nr_sects
!= bdev_zone_sectors(bdev
)) {
1304 f2fs_msg(sbi
->sb
, KERN_INFO
,
1305 "(%d) %s: Unaligned discard attempted (block %x + %x)",
1306 devi
, sbi
->s_ndevs
? FDEV(devi
).path
: "",
1310 trace_f2fs_issue_reset_zone(bdev
, blkstart
);
1311 return blkdev_reset_zones(bdev
, sector
,
1312 nr_sects
, GFP_NOFS
);
1314 /* Unknown zone type: broken device ? */
1320 static int __issue_discard_async(struct f2fs_sb_info
*sbi
,
1321 struct block_device
*bdev
, block_t blkstart
, block_t blklen
)
1323 #ifdef CONFIG_BLK_DEV_ZONED
1324 if (f2fs_sb_mounted_blkzoned(sbi
->sb
) &&
1325 bdev_zoned_model(bdev
) != BLK_ZONED_NONE
)
1326 return __f2fs_issue_discard_zone(sbi
, bdev
, blkstart
, blklen
);
1328 return __queue_discard_cmd(sbi
, bdev
, blkstart
, blklen
);
1331 static int f2fs_issue_discard(struct f2fs_sb_info
*sbi
,
1332 block_t blkstart
, block_t blklen
)
1334 sector_t start
= blkstart
, len
= 0;
1335 struct block_device
*bdev
;
1336 struct seg_entry
*se
;
1337 unsigned int offset
;
1341 bdev
= f2fs_target_device(sbi
, blkstart
, NULL
);
1343 for (i
= blkstart
; i
< blkstart
+ blklen
; i
++, len
++) {
1345 struct block_device
*bdev2
=
1346 f2fs_target_device(sbi
, i
, NULL
);
1348 if (bdev2
!= bdev
) {
1349 err
= __issue_discard_async(sbi
, bdev
,
1359 se
= get_seg_entry(sbi
, GET_SEGNO(sbi
, i
));
1360 offset
= GET_BLKOFF_FROM_SEG0(sbi
, i
);
1362 if (!f2fs_test_and_set_bit(offset
, se
->discard_map
))
1363 sbi
->discard_blks
--;
1367 err
= __issue_discard_async(sbi
, bdev
, start
, len
);
1371 static bool add_discard_addrs(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
,
1374 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
1375 int max_blocks
= sbi
->blocks_per_seg
;
1376 struct seg_entry
*se
= get_seg_entry(sbi
, cpc
->trim_start
);
1377 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
1378 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
1379 unsigned long *discard_map
= (unsigned long *)se
->discard_map
;
1380 unsigned long *dmap
= SIT_I(sbi
)->tmp_map
;
1381 unsigned int start
= 0, end
= -1;
1382 bool force
= (cpc
->reason
& CP_DISCARD
);
1383 struct discard_entry
*de
= NULL
;
1384 struct list_head
*head
= &SM_I(sbi
)->dcc_info
->entry_list
;
1387 if (se
->valid_blocks
== max_blocks
|| !f2fs_discard_en(sbi
))
1391 if (!test_opt(sbi
, DISCARD
) || !se
->valid_blocks
||
1392 SM_I(sbi
)->dcc_info
->nr_discards
>=
1393 SM_I(sbi
)->dcc_info
->max_discards
)
1397 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1398 for (i
= 0; i
< entries
; i
++)
1399 dmap
[i
] = force
? ~ckpt_map
[i
] & ~discard_map
[i
] :
1400 (cur_map
[i
] ^ ckpt_map
[i
]) & ckpt_map
[i
];
1402 while (force
|| SM_I(sbi
)->dcc_info
->nr_discards
<=
1403 SM_I(sbi
)->dcc_info
->max_discards
) {
1404 start
= __find_rev_next_bit(dmap
, max_blocks
, end
+ 1);
1405 if (start
>= max_blocks
)
1408 end
= __find_rev_next_zero_bit(dmap
, max_blocks
, start
+ 1);
1409 if (force
&& start
&& end
!= max_blocks
1410 && (end
- start
) < cpc
->trim_minlen
)
1417 de
= f2fs_kmem_cache_alloc(discard_entry_slab
,
1419 de
->start_blkaddr
= START_BLOCK(sbi
, cpc
->trim_start
);
1420 list_add_tail(&de
->list
, head
);
1423 for (i
= start
; i
< end
; i
++)
1424 __set_bit_le(i
, (void *)de
->discard_map
);
1426 SM_I(sbi
)->dcc_info
->nr_discards
+= end
- start
;
1431 void release_discard_addrs(struct f2fs_sb_info
*sbi
)
1433 struct list_head
*head
= &(SM_I(sbi
)->dcc_info
->entry_list
);
1434 struct discard_entry
*entry
, *this;
1437 list_for_each_entry_safe(entry
, this, head
, list
) {
1438 list_del(&entry
->list
);
1439 kmem_cache_free(discard_entry_slab
, entry
);
1444 * Should call clear_prefree_segments after checkpoint is done.
1446 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
1448 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1451 mutex_lock(&dirty_i
->seglist_lock
);
1452 for_each_set_bit(segno
, dirty_i
->dirty_segmap
[PRE
], MAIN_SEGS(sbi
))
1453 __set_test_and_free(sbi
, segno
);
1454 mutex_unlock(&dirty_i
->seglist_lock
);
1457 void clear_prefree_segments(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
1459 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1460 struct list_head
*head
= &dcc
->entry_list
;
1461 struct discard_entry
*entry
, *this;
1462 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1463 unsigned long *prefree_map
= dirty_i
->dirty_segmap
[PRE
];
1464 unsigned int start
= 0, end
= -1;
1465 unsigned int secno
, start_segno
;
1466 bool force
= (cpc
->reason
& CP_DISCARD
);
1468 mutex_lock(&dirty_i
->seglist_lock
);
1472 start
= find_next_bit(prefree_map
, MAIN_SEGS(sbi
), end
+ 1);
1473 if (start
>= MAIN_SEGS(sbi
))
1475 end
= find_next_zero_bit(prefree_map
, MAIN_SEGS(sbi
),
1478 for (i
= start
; i
< end
; i
++)
1479 clear_bit(i
, prefree_map
);
1481 dirty_i
->nr_dirty
[PRE
] -= end
- start
;
1483 if (!test_opt(sbi
, DISCARD
))
1486 if (force
&& start
>= cpc
->trim_start
&&
1487 (end
- 1) <= cpc
->trim_end
)
1490 if (!test_opt(sbi
, LFS
) || sbi
->segs_per_sec
== 1) {
1491 f2fs_issue_discard(sbi
, START_BLOCK(sbi
, start
),
1492 (end
- start
) << sbi
->log_blocks_per_seg
);
1496 secno
= GET_SEC_FROM_SEG(sbi
, start
);
1497 start_segno
= GET_SEG_FROM_SEC(sbi
, secno
);
1498 if (!IS_CURSEC(sbi
, secno
) &&
1499 !get_valid_blocks(sbi
, start
, true))
1500 f2fs_issue_discard(sbi
, START_BLOCK(sbi
, start_segno
),
1501 sbi
->segs_per_sec
<< sbi
->log_blocks_per_seg
);
1503 start
= start_segno
+ sbi
->segs_per_sec
;
1509 mutex_unlock(&dirty_i
->seglist_lock
);
1511 /* send small discards */
1512 list_for_each_entry_safe(entry
, this, head
, list
) {
1513 unsigned int cur_pos
= 0, next_pos
, len
, total_len
= 0;
1514 bool is_valid
= test_bit_le(0, entry
->discard_map
);
1518 next_pos
= find_next_zero_bit_le(entry
->discard_map
,
1519 sbi
->blocks_per_seg
, cur_pos
);
1520 len
= next_pos
- cur_pos
;
1522 if (f2fs_sb_mounted_blkzoned(sbi
->sb
) ||
1523 (force
&& len
< cpc
->trim_minlen
))
1526 f2fs_issue_discard(sbi
, entry
->start_blkaddr
+ cur_pos
,
1528 cpc
->trimmed
+= len
;
1531 next_pos
= find_next_bit_le(entry
->discard_map
,
1532 sbi
->blocks_per_seg
, cur_pos
);
1536 is_valid
= !is_valid
;
1538 if (cur_pos
< sbi
->blocks_per_seg
)
1541 list_del(&entry
->list
);
1542 dcc
->nr_discards
-= total_len
;
1543 kmem_cache_free(discard_entry_slab
, entry
);
1546 wake_up_discard_thread(sbi
, false);
1549 static int create_discard_cmd_control(struct f2fs_sb_info
*sbi
)
1551 dev_t dev
= sbi
->sb
->s_bdev
->bd_dev
;
1552 struct discard_cmd_control
*dcc
;
1555 if (SM_I(sbi
)->dcc_info
) {
1556 dcc
= SM_I(sbi
)->dcc_info
;
1560 dcc
= kzalloc(sizeof(struct discard_cmd_control
), GFP_KERNEL
);
1564 dcc
->discard_granularity
= DEFAULT_DISCARD_GRANULARITY
;
1565 INIT_LIST_HEAD(&dcc
->entry_list
);
1566 for (i
= 0; i
< MAX_PLIST_NUM
; i
++) {
1567 INIT_LIST_HEAD(&dcc
->pend_list
[i
]);
1568 if (i
>= dcc
->discard_granularity
- 1)
1569 dcc
->pend_list_tag
[i
] |= P_ACTIVE
;
1571 INIT_LIST_HEAD(&dcc
->wait_list
);
1572 mutex_init(&dcc
->cmd_lock
);
1573 atomic_set(&dcc
->issued_discard
, 0);
1574 atomic_set(&dcc
->issing_discard
, 0);
1575 atomic_set(&dcc
->discard_cmd_cnt
, 0);
1576 dcc
->nr_discards
= 0;
1577 dcc
->max_discards
= MAIN_SEGS(sbi
) << sbi
->log_blocks_per_seg
;
1578 dcc
->undiscard_blks
= 0;
1579 dcc
->root
= RB_ROOT
;
1581 init_waitqueue_head(&dcc
->discard_wait_queue
);
1582 SM_I(sbi
)->dcc_info
= dcc
;
1584 dcc
->f2fs_issue_discard
= kthread_run(issue_discard_thread
, sbi
,
1585 "f2fs_discard-%u:%u", MAJOR(dev
), MINOR(dev
));
1586 if (IS_ERR(dcc
->f2fs_issue_discard
)) {
1587 err
= PTR_ERR(dcc
->f2fs_issue_discard
);
1589 SM_I(sbi
)->dcc_info
= NULL
;
1596 static void destroy_discard_cmd_control(struct f2fs_sb_info
*sbi
)
1598 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1603 stop_discard_thread(sbi
);
1606 SM_I(sbi
)->dcc_info
= NULL
;
1609 static bool __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
1611 struct sit_info
*sit_i
= SIT_I(sbi
);
1613 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
)) {
1614 sit_i
->dirty_sentries
++;
1621 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
1622 unsigned int segno
, int modified
)
1624 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1627 __mark_sit_entry_dirty(sbi
, segno
);
1630 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
1632 struct seg_entry
*se
;
1633 unsigned int segno
, offset
;
1634 long int new_vblocks
;
1636 #ifdef CONFIG_F2FS_CHECK_FS
1640 segno
= GET_SEGNO(sbi
, blkaddr
);
1642 se
= get_seg_entry(sbi
, segno
);
1643 new_vblocks
= se
->valid_blocks
+ del
;
1644 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
1646 f2fs_bug_on(sbi
, (new_vblocks
>> (sizeof(unsigned short) << 3) ||
1647 (new_vblocks
> sbi
->blocks_per_seg
)));
1649 se
->valid_blocks
= new_vblocks
;
1650 se
->mtime
= get_mtime(sbi
);
1651 SIT_I(sbi
)->max_mtime
= se
->mtime
;
1653 /* Update valid block bitmap */
1655 exist
= f2fs_test_and_set_bit(offset
, se
->cur_valid_map
);
1656 #ifdef CONFIG_F2FS_CHECK_FS
1657 mir_exist
= f2fs_test_and_set_bit(offset
,
1658 se
->cur_valid_map_mir
);
1659 if (unlikely(exist
!= mir_exist
)) {
1660 f2fs_msg(sbi
->sb
, KERN_ERR
, "Inconsistent error "
1661 "when setting bitmap, blk:%u, old bit:%d",
1663 f2fs_bug_on(sbi
, 1);
1666 if (unlikely(exist
)) {
1667 f2fs_msg(sbi
->sb
, KERN_ERR
,
1668 "Bitmap was wrongly set, blk:%u", blkaddr
);
1669 f2fs_bug_on(sbi
, 1);
1674 if (f2fs_discard_en(sbi
) &&
1675 !f2fs_test_and_set_bit(offset
, se
->discard_map
))
1676 sbi
->discard_blks
--;
1678 /* don't overwrite by SSR to keep node chain */
1679 if (se
->type
== CURSEG_WARM_NODE
) {
1680 if (!f2fs_test_and_set_bit(offset
, se
->ckpt_valid_map
))
1681 se
->ckpt_valid_blocks
++;
1684 exist
= f2fs_test_and_clear_bit(offset
, se
->cur_valid_map
);
1685 #ifdef CONFIG_F2FS_CHECK_FS
1686 mir_exist
= f2fs_test_and_clear_bit(offset
,
1687 se
->cur_valid_map_mir
);
1688 if (unlikely(exist
!= mir_exist
)) {
1689 f2fs_msg(sbi
->sb
, KERN_ERR
, "Inconsistent error "
1690 "when clearing bitmap, blk:%u, old bit:%d",
1692 f2fs_bug_on(sbi
, 1);
1695 if (unlikely(!exist
)) {
1696 f2fs_msg(sbi
->sb
, KERN_ERR
,
1697 "Bitmap was wrongly cleared, blk:%u", blkaddr
);
1698 f2fs_bug_on(sbi
, 1);
1703 if (f2fs_discard_en(sbi
) &&
1704 f2fs_test_and_clear_bit(offset
, se
->discard_map
))
1705 sbi
->discard_blks
++;
1707 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
1708 se
->ckpt_valid_blocks
+= del
;
1710 __mark_sit_entry_dirty(sbi
, segno
);
1712 /* update total number of valid blocks to be written in ckpt area */
1713 SIT_I(sbi
)->written_valid_blocks
+= del
;
1715 if (sbi
->segs_per_sec
> 1)
1716 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
1719 void refresh_sit_entry(struct f2fs_sb_info
*sbi
, block_t old
, block_t
new)
1721 update_sit_entry(sbi
, new, 1);
1722 if (GET_SEGNO(sbi
, old
) != NULL_SEGNO
)
1723 update_sit_entry(sbi
, old
, -1);
1725 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old
));
1726 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, new));
1729 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
1731 unsigned int segno
= GET_SEGNO(sbi
, addr
);
1732 struct sit_info
*sit_i
= SIT_I(sbi
);
1734 f2fs_bug_on(sbi
, addr
== NULL_ADDR
);
1735 if (addr
== NEW_ADDR
)
1738 /* add it into sit main buffer */
1739 mutex_lock(&sit_i
->sentry_lock
);
1741 update_sit_entry(sbi
, addr
, -1);
1743 /* add it into dirty seglist */
1744 locate_dirty_segment(sbi
, segno
);
1746 mutex_unlock(&sit_i
->sentry_lock
);
1749 bool is_checkpointed_data(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1751 struct sit_info
*sit_i
= SIT_I(sbi
);
1752 unsigned int segno
, offset
;
1753 struct seg_entry
*se
;
1756 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
)
1759 mutex_lock(&sit_i
->sentry_lock
);
1761 segno
= GET_SEGNO(sbi
, blkaddr
);
1762 se
= get_seg_entry(sbi
, segno
);
1763 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
1765 if (f2fs_test_bit(offset
, se
->ckpt_valid_map
))
1768 mutex_unlock(&sit_i
->sentry_lock
);
1774 * This function should be resided under the curseg_mutex lock
1776 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
1777 struct f2fs_summary
*sum
)
1779 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1780 void *addr
= curseg
->sum_blk
;
1781 addr
+= curseg
->next_blkoff
* sizeof(struct f2fs_summary
);
1782 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
1786 * Calculate the number of current summary pages for writing
1788 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
, bool for_ra
)
1790 int valid_sum_count
= 0;
1793 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1794 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1795 valid_sum_count
+= sbi
->blocks_per_seg
;
1798 valid_sum_count
+= le16_to_cpu(
1799 F2FS_CKPT(sbi
)->cur_data_blkoff
[i
]);
1801 valid_sum_count
+= curseg_blkoff(sbi
, i
);
1805 sum_in_page
= (PAGE_SIZE
- 2 * SUM_JOURNAL_SIZE
-
1806 SUM_FOOTER_SIZE
) / SUMMARY_SIZE
;
1807 if (valid_sum_count
<= sum_in_page
)
1809 else if ((valid_sum_count
- sum_in_page
) <=
1810 (PAGE_SIZE
- SUM_FOOTER_SIZE
) / SUMMARY_SIZE
)
1816 * Caller should put this summary page
1818 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
1820 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
1823 void update_meta_page(struct f2fs_sb_info
*sbi
, void *src
, block_t blk_addr
)
1825 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
1826 void *dst
= page_address(page
);
1829 memcpy(dst
, src
, PAGE_SIZE
);
1831 memset(dst
, 0, PAGE_SIZE
);
1832 set_page_dirty(page
);
1833 f2fs_put_page(page
, 1);
1836 static void write_sum_page(struct f2fs_sb_info
*sbi
,
1837 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
1839 update_meta_page(sbi
, (void *)sum_blk
, blk_addr
);
1842 static void write_current_sum_page(struct f2fs_sb_info
*sbi
,
1843 int type
, block_t blk_addr
)
1845 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1846 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
1847 struct f2fs_summary_block
*src
= curseg
->sum_blk
;
1848 struct f2fs_summary_block
*dst
;
1850 dst
= (struct f2fs_summary_block
*)page_address(page
);
1852 mutex_lock(&curseg
->curseg_mutex
);
1854 down_read(&curseg
->journal_rwsem
);
1855 memcpy(&dst
->journal
, curseg
->journal
, SUM_JOURNAL_SIZE
);
1856 up_read(&curseg
->journal_rwsem
);
1858 memcpy(dst
->entries
, src
->entries
, SUM_ENTRY_SIZE
);
1859 memcpy(&dst
->footer
, &src
->footer
, SUM_FOOTER_SIZE
);
1861 mutex_unlock(&curseg
->curseg_mutex
);
1863 set_page_dirty(page
);
1864 f2fs_put_page(page
, 1);
1867 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
1869 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1870 unsigned int segno
= curseg
->segno
+ 1;
1871 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1873 if (segno
< MAIN_SEGS(sbi
) && segno
% sbi
->segs_per_sec
)
1874 return !test_bit(segno
, free_i
->free_segmap
);
1879 * Find a new segment from the free segments bitmap to right order
1880 * This function should be returned with success, otherwise BUG
1882 static void get_new_segment(struct f2fs_sb_info
*sbi
,
1883 unsigned int *newseg
, bool new_sec
, int dir
)
1885 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1886 unsigned int segno
, secno
, zoneno
;
1887 unsigned int total_zones
= MAIN_SECS(sbi
) / sbi
->secs_per_zone
;
1888 unsigned int hint
= GET_SEC_FROM_SEG(sbi
, *newseg
);
1889 unsigned int old_zoneno
= GET_ZONE_FROM_SEG(sbi
, *newseg
);
1890 unsigned int left_start
= hint
;
1895 spin_lock(&free_i
->segmap_lock
);
1897 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
1898 segno
= find_next_zero_bit(free_i
->free_segmap
,
1899 GET_SEG_FROM_SEC(sbi
, hint
+ 1), *newseg
+ 1);
1900 if (segno
< GET_SEG_FROM_SEC(sbi
, hint
+ 1))
1904 secno
= find_next_zero_bit(free_i
->free_secmap
, MAIN_SECS(sbi
), hint
);
1905 if (secno
>= MAIN_SECS(sbi
)) {
1906 if (dir
== ALLOC_RIGHT
) {
1907 secno
= find_next_zero_bit(free_i
->free_secmap
,
1909 f2fs_bug_on(sbi
, secno
>= MAIN_SECS(sbi
));
1912 left_start
= hint
- 1;
1918 while (test_bit(left_start
, free_i
->free_secmap
)) {
1919 if (left_start
> 0) {
1923 left_start
= find_next_zero_bit(free_i
->free_secmap
,
1925 f2fs_bug_on(sbi
, left_start
>= MAIN_SECS(sbi
));
1931 segno
= GET_SEG_FROM_SEC(sbi
, secno
);
1932 zoneno
= GET_ZONE_FROM_SEC(sbi
, secno
);
1934 /* give up on finding another zone */
1937 if (sbi
->secs_per_zone
== 1)
1939 if (zoneno
== old_zoneno
)
1941 if (dir
== ALLOC_LEFT
) {
1942 if (!go_left
&& zoneno
+ 1 >= total_zones
)
1944 if (go_left
&& zoneno
== 0)
1947 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1948 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
1951 if (i
< NR_CURSEG_TYPE
) {
1952 /* zone is in user, try another */
1954 hint
= zoneno
* sbi
->secs_per_zone
- 1;
1955 else if (zoneno
+ 1 >= total_zones
)
1958 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
1960 goto find_other_zone
;
1963 /* set it as dirty segment in free segmap */
1964 f2fs_bug_on(sbi
, test_bit(segno
, free_i
->free_segmap
));
1965 __set_inuse(sbi
, segno
);
1967 spin_unlock(&free_i
->segmap_lock
);
1970 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
1972 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1973 struct summary_footer
*sum_footer
;
1975 curseg
->segno
= curseg
->next_segno
;
1976 curseg
->zone
= GET_ZONE_FROM_SEG(sbi
, curseg
->segno
);
1977 curseg
->next_blkoff
= 0;
1978 curseg
->next_segno
= NULL_SEGNO
;
1980 sum_footer
= &(curseg
->sum_blk
->footer
);
1981 memset(sum_footer
, 0, sizeof(struct summary_footer
));
1982 if (IS_DATASEG(type
))
1983 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
1984 if (IS_NODESEG(type
))
1985 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
1986 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
1989 static unsigned int __get_next_segno(struct f2fs_sb_info
*sbi
, int type
)
1991 /* if segs_per_sec is large than 1, we need to keep original policy. */
1992 if (sbi
->segs_per_sec
!= 1)
1993 return CURSEG_I(sbi
, type
)->segno
;
1995 if (type
== CURSEG_HOT_DATA
|| IS_NODESEG(type
))
1998 if (SIT_I(sbi
)->last_victim
[ALLOC_NEXT
])
1999 return SIT_I(sbi
)->last_victim
[ALLOC_NEXT
];
2000 return CURSEG_I(sbi
, type
)->segno
;
2004 * Allocate a current working segment.
2005 * This function always allocates a free segment in LFS manner.
2007 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
2009 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
2010 unsigned int segno
= curseg
->segno
;
2011 int dir
= ALLOC_LEFT
;
2013 write_sum_page(sbi
, curseg
->sum_blk
,
2014 GET_SUM_BLOCK(sbi
, segno
));
2015 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
2018 if (test_opt(sbi
, NOHEAP
))
2021 segno
= __get_next_segno(sbi
, type
);
2022 get_new_segment(sbi
, &segno
, new_sec
, dir
);
2023 curseg
->next_segno
= segno
;
2024 reset_curseg(sbi
, type
, 1);
2025 curseg
->alloc_type
= LFS
;
2028 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
2029 struct curseg_info
*seg
, block_t start
)
2031 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
2032 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
2033 unsigned long *target_map
= SIT_I(sbi
)->tmp_map
;
2034 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
2035 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
2038 for (i
= 0; i
< entries
; i
++)
2039 target_map
[i
] = ckpt_map
[i
] | cur_map
[i
];
2041 pos
= __find_rev_next_zero_bit(target_map
, sbi
->blocks_per_seg
, start
);
2043 seg
->next_blkoff
= pos
;
2047 * If a segment is written by LFS manner, next block offset is just obtained
2048 * by increasing the current block offset. However, if a segment is written by
2049 * SSR manner, next block offset obtained by calling __next_free_blkoff
2051 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
2052 struct curseg_info
*seg
)
2054 if (seg
->alloc_type
== SSR
)
2055 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
2061 * This function always allocates a used segment(from dirty seglist) by SSR
2062 * manner, so it should recover the existing segment information of valid blocks
2064 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
)
2066 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2067 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
2068 unsigned int new_segno
= curseg
->next_segno
;
2069 struct f2fs_summary_block
*sum_node
;
2070 struct page
*sum_page
;
2072 write_sum_page(sbi
, curseg
->sum_blk
,
2073 GET_SUM_BLOCK(sbi
, curseg
->segno
));
2074 __set_test_and_inuse(sbi
, new_segno
);
2076 mutex_lock(&dirty_i
->seglist_lock
);
2077 __remove_dirty_segment(sbi
, new_segno
, PRE
);
2078 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
2079 mutex_unlock(&dirty_i
->seglist_lock
);
2081 reset_curseg(sbi
, type
, 1);
2082 curseg
->alloc_type
= SSR
;
2083 __next_free_blkoff(sbi
, curseg
, 0);
2085 sum_page
= get_sum_page(sbi
, new_segno
);
2086 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
2087 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
2088 f2fs_put_page(sum_page
, 1);
2091 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
2093 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
2094 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
2095 unsigned segno
= NULL_SEGNO
;
2097 bool reversed
= false;
2099 /* need_SSR() already forces to do this */
2100 if (v_ops
->get_victim(sbi
, &segno
, BG_GC
, type
, SSR
)) {
2101 curseg
->next_segno
= segno
;
2105 /* For node segments, let's do SSR more intensively */
2106 if (IS_NODESEG(type
)) {
2107 if (type
>= CURSEG_WARM_NODE
) {
2109 i
= CURSEG_COLD_NODE
;
2111 i
= CURSEG_HOT_NODE
;
2113 cnt
= NR_CURSEG_NODE_TYPE
;
2115 if (type
>= CURSEG_WARM_DATA
) {
2117 i
= CURSEG_COLD_DATA
;
2119 i
= CURSEG_HOT_DATA
;
2121 cnt
= NR_CURSEG_DATA_TYPE
;
2124 for (; cnt
-- > 0; reversed
? i
-- : i
++) {
2127 if (v_ops
->get_victim(sbi
, &segno
, BG_GC
, i
, SSR
)) {
2128 curseg
->next_segno
= segno
;
2136 * flush out current segment and replace it with new segment
2137 * This function should be returned with success, otherwise BUG
2139 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
2140 int type
, bool force
)
2142 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
2145 new_curseg(sbi
, type
, true);
2146 else if (!is_set_ckpt_flags(sbi
, CP_CRC_RECOVERY_FLAG
) &&
2147 type
== CURSEG_WARM_NODE
)
2148 new_curseg(sbi
, type
, false);
2149 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
2150 new_curseg(sbi
, type
, false);
2151 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
2152 change_curseg(sbi
, type
);
2154 new_curseg(sbi
, type
, false);
2156 stat_inc_seg_type(sbi
, curseg
);
2159 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
2161 struct curseg_info
*curseg
;
2162 unsigned int old_segno
;
2165 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
2166 curseg
= CURSEG_I(sbi
, i
);
2167 old_segno
= curseg
->segno
;
2168 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
2169 locate_dirty_segment(sbi
, old_segno
);
2173 static const struct segment_allocation default_salloc_ops
= {
2174 .allocate_segment
= allocate_segment_by_default
,
2177 bool exist_trim_candidates(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
2179 __u64 trim_start
= cpc
->trim_start
;
2180 bool has_candidate
= false;
2182 mutex_lock(&SIT_I(sbi
)->sentry_lock
);
2183 for (; cpc
->trim_start
<= cpc
->trim_end
; cpc
->trim_start
++) {
2184 if (add_discard_addrs(sbi
, cpc
, true)) {
2185 has_candidate
= true;
2189 mutex_unlock(&SIT_I(sbi
)->sentry_lock
);
2191 cpc
->trim_start
= trim_start
;
2192 return has_candidate
;
2195 int f2fs_trim_fs(struct f2fs_sb_info
*sbi
, struct fstrim_range
*range
)
2197 __u64 start
= F2FS_BYTES_TO_BLK(range
->start
);
2198 __u64 end
= start
+ F2FS_BYTES_TO_BLK(range
->len
) - 1;
2199 unsigned int start_segno
, end_segno
;
2200 struct cp_control cpc
;
2203 if (start
>= MAX_BLKADDR(sbi
) || range
->len
< sbi
->blocksize
)
2207 if (end
<= MAIN_BLKADDR(sbi
))
2210 if (is_sbi_flag_set(sbi
, SBI_NEED_FSCK
)) {
2211 f2fs_msg(sbi
->sb
, KERN_WARNING
,
2212 "Found FS corruption, run fsck to fix.");
2216 /* start/end segment number in main_area */
2217 start_segno
= (start
<= MAIN_BLKADDR(sbi
)) ? 0 : GET_SEGNO(sbi
, start
);
2218 end_segno
= (end
>= MAX_BLKADDR(sbi
)) ? MAIN_SEGS(sbi
) - 1 :
2219 GET_SEGNO(sbi
, end
);
2220 cpc
.reason
= CP_DISCARD
;
2221 cpc
.trim_minlen
= max_t(__u64
, 1, F2FS_BYTES_TO_BLK(range
->minlen
));
2223 /* do checkpoint to issue discard commands safely */
2224 for (; start_segno
<= end_segno
; start_segno
= cpc
.trim_end
+ 1) {
2225 cpc
.trim_start
= start_segno
;
2227 if (sbi
->discard_blks
== 0)
2229 else if (sbi
->discard_blks
< BATCHED_TRIM_BLOCKS(sbi
))
2230 cpc
.trim_end
= end_segno
;
2232 cpc
.trim_end
= min_t(unsigned int,
2233 rounddown(start_segno
+
2234 BATCHED_TRIM_SEGMENTS(sbi
),
2235 sbi
->segs_per_sec
) - 1, end_segno
);
2237 mutex_lock(&sbi
->gc_mutex
);
2238 err
= write_checkpoint(sbi
, &cpc
);
2239 mutex_unlock(&sbi
->gc_mutex
);
2245 /* It's time to issue all the filed discards */
2246 mark_discard_range_all(sbi
);
2247 f2fs_wait_discard_bios(sbi
, false);
2249 range
->len
= F2FS_BLK_TO_BYTES(cpc
.trimmed
);
2253 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
2255 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
2256 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
2261 static int __get_segment_type_2(struct f2fs_io_info
*fio
)
2263 if (fio
->type
== DATA
)
2264 return CURSEG_HOT_DATA
;
2266 return CURSEG_HOT_NODE
;
2269 static int __get_segment_type_4(struct f2fs_io_info
*fio
)
2271 if (fio
->type
== DATA
) {
2272 struct inode
*inode
= fio
->page
->mapping
->host
;
2274 if (S_ISDIR(inode
->i_mode
))
2275 return CURSEG_HOT_DATA
;
2277 return CURSEG_COLD_DATA
;
2279 if (IS_DNODE(fio
->page
) && is_cold_node(fio
->page
))
2280 return CURSEG_WARM_NODE
;
2282 return CURSEG_COLD_NODE
;
2286 static int __get_segment_type_6(struct f2fs_io_info
*fio
)
2288 if (fio
->type
== DATA
) {
2289 struct inode
*inode
= fio
->page
->mapping
->host
;
2291 if (is_cold_data(fio
->page
) || file_is_cold(inode
))
2292 return CURSEG_COLD_DATA
;
2293 if (is_inode_flag_set(inode
, FI_HOT_DATA
))
2294 return CURSEG_HOT_DATA
;
2295 return CURSEG_WARM_DATA
;
2297 if (IS_DNODE(fio
->page
))
2298 return is_cold_node(fio
->page
) ? CURSEG_WARM_NODE
:
2300 return CURSEG_COLD_NODE
;
2304 static int __get_segment_type(struct f2fs_io_info
*fio
)
2308 switch (fio
->sbi
->active_logs
) {
2310 type
= __get_segment_type_2(fio
);
2313 type
= __get_segment_type_4(fio
);
2316 type
= __get_segment_type_6(fio
);
2319 f2fs_bug_on(fio
->sbi
, true);
2324 else if (IS_WARM(type
))
2331 void allocate_data_block(struct f2fs_sb_info
*sbi
, struct page
*page
,
2332 block_t old_blkaddr
, block_t
*new_blkaddr
,
2333 struct f2fs_summary
*sum
, int type
,
2334 struct f2fs_io_info
*fio
, bool add_list
)
2336 struct sit_info
*sit_i
= SIT_I(sbi
);
2337 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
2339 mutex_lock(&curseg
->curseg_mutex
);
2340 mutex_lock(&sit_i
->sentry_lock
);
2342 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
2344 f2fs_wait_discard_bio(sbi
, *new_blkaddr
);
2347 * __add_sum_entry should be resided under the curseg_mutex
2348 * because, this function updates a summary entry in the
2349 * current summary block.
2351 __add_sum_entry(sbi
, type
, sum
);
2353 __refresh_next_blkoff(sbi
, curseg
);
2355 stat_inc_block_count(sbi
, curseg
);
2357 if (!__has_curseg_space(sbi
, type
))
2358 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
2360 * SIT information should be updated after segment allocation,
2361 * since we need to keep dirty segments precisely under SSR.
2363 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
2365 mutex_unlock(&sit_i
->sentry_lock
);
2367 if (page
&& IS_NODESEG(type
)) {
2368 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
2370 f2fs_inode_chksum_set(sbi
, page
);
2374 struct f2fs_bio_info
*io
;
2376 INIT_LIST_HEAD(&fio
->list
);
2377 fio
->in_list
= true;
2378 io
= sbi
->write_io
[fio
->type
] + fio
->temp
;
2379 spin_lock(&io
->io_lock
);
2380 list_add_tail(&fio
->list
, &io
->io_list
);
2381 spin_unlock(&io
->io_lock
);
2384 mutex_unlock(&curseg
->curseg_mutex
);
2387 static void do_write_page(struct f2fs_summary
*sum
, struct f2fs_io_info
*fio
)
2389 int type
= __get_segment_type(fio
);
2393 allocate_data_block(fio
->sbi
, fio
->page
, fio
->old_blkaddr
,
2394 &fio
->new_blkaddr
, sum
, type
, fio
, true);
2396 /* writeout dirty page into bdev */
2397 err
= f2fs_submit_page_write(fio
);
2398 if (err
== -EAGAIN
) {
2399 fio
->old_blkaddr
= fio
->new_blkaddr
;
2404 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
,
2405 enum iostat_type io_type
)
2407 struct f2fs_io_info fio
= {
2411 .op_flags
= REQ_SYNC
| REQ_META
| REQ_PRIO
,
2412 .old_blkaddr
= page
->index
,
2413 .new_blkaddr
= page
->index
,
2415 .encrypted_page
= NULL
,
2419 if (unlikely(page
->index
>= MAIN_BLKADDR(sbi
)))
2420 fio
.op_flags
&= ~REQ_META
;
2422 set_page_writeback(page
);
2423 f2fs_submit_page_write(&fio
);
2425 f2fs_update_iostat(sbi
, io_type
, F2FS_BLKSIZE
);
2428 void write_node_page(unsigned int nid
, struct f2fs_io_info
*fio
)
2430 struct f2fs_summary sum
;
2432 set_summary(&sum
, nid
, 0, 0);
2433 do_write_page(&sum
, fio
);
2435 f2fs_update_iostat(fio
->sbi
, fio
->io_type
, F2FS_BLKSIZE
);
2438 void write_data_page(struct dnode_of_data
*dn
, struct f2fs_io_info
*fio
)
2440 struct f2fs_sb_info
*sbi
= fio
->sbi
;
2441 struct f2fs_summary sum
;
2442 struct node_info ni
;
2444 f2fs_bug_on(sbi
, dn
->data_blkaddr
== NULL_ADDR
);
2445 get_node_info(sbi
, dn
->nid
, &ni
);
2446 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
2447 do_write_page(&sum
, fio
);
2448 f2fs_update_data_blkaddr(dn
, fio
->new_blkaddr
);
2450 f2fs_update_iostat(sbi
, fio
->io_type
, F2FS_BLKSIZE
);
2453 int rewrite_data_page(struct f2fs_io_info
*fio
)
2457 fio
->new_blkaddr
= fio
->old_blkaddr
;
2458 stat_inc_inplace_blocks(fio
->sbi
);
2460 err
= f2fs_submit_page_bio(fio
);
2462 f2fs_update_iostat(fio
->sbi
, fio
->io_type
, F2FS_BLKSIZE
);
2467 void __f2fs_replace_block(struct f2fs_sb_info
*sbi
, struct f2fs_summary
*sum
,
2468 block_t old_blkaddr
, block_t new_blkaddr
,
2469 bool recover_curseg
, bool recover_newaddr
)
2471 struct sit_info
*sit_i
= SIT_I(sbi
);
2472 struct curseg_info
*curseg
;
2473 unsigned int segno
, old_cursegno
;
2474 struct seg_entry
*se
;
2476 unsigned short old_blkoff
;
2478 segno
= GET_SEGNO(sbi
, new_blkaddr
);
2479 se
= get_seg_entry(sbi
, segno
);
2482 if (!recover_curseg
) {
2483 /* for recovery flow */
2484 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
2485 if (old_blkaddr
== NULL_ADDR
)
2486 type
= CURSEG_COLD_DATA
;
2488 type
= CURSEG_WARM_DATA
;
2491 if (!IS_CURSEG(sbi
, segno
))
2492 type
= CURSEG_WARM_DATA
;
2495 curseg
= CURSEG_I(sbi
, type
);
2497 mutex_lock(&curseg
->curseg_mutex
);
2498 mutex_lock(&sit_i
->sentry_lock
);
2500 old_cursegno
= curseg
->segno
;
2501 old_blkoff
= curseg
->next_blkoff
;
2503 /* change the current segment */
2504 if (segno
!= curseg
->segno
) {
2505 curseg
->next_segno
= segno
;
2506 change_curseg(sbi
, type
);
2509 curseg
->next_blkoff
= GET_BLKOFF_FROM_SEG0(sbi
, new_blkaddr
);
2510 __add_sum_entry(sbi
, type
, sum
);
2512 if (!recover_curseg
|| recover_newaddr
)
2513 update_sit_entry(sbi
, new_blkaddr
, 1);
2514 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
2515 update_sit_entry(sbi
, old_blkaddr
, -1);
2517 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
2518 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, new_blkaddr
));
2520 locate_dirty_segment(sbi
, old_cursegno
);
2522 if (recover_curseg
) {
2523 if (old_cursegno
!= curseg
->segno
) {
2524 curseg
->next_segno
= old_cursegno
;
2525 change_curseg(sbi
, type
);
2527 curseg
->next_blkoff
= old_blkoff
;
2530 mutex_unlock(&sit_i
->sentry_lock
);
2531 mutex_unlock(&curseg
->curseg_mutex
);
2534 void f2fs_replace_block(struct f2fs_sb_info
*sbi
, struct dnode_of_data
*dn
,
2535 block_t old_addr
, block_t new_addr
,
2536 unsigned char version
, bool recover_curseg
,
2537 bool recover_newaddr
)
2539 struct f2fs_summary sum
;
2541 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, version
);
2543 __f2fs_replace_block(sbi
, &sum
, old_addr
, new_addr
,
2544 recover_curseg
, recover_newaddr
);
2546 f2fs_update_data_blkaddr(dn
, new_addr
);
2549 void f2fs_wait_on_page_writeback(struct page
*page
,
2550 enum page_type type
, bool ordered
)
2552 if (PageWriteback(page
)) {
2553 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
2555 f2fs_submit_merged_write_cond(sbi
, page
->mapping
->host
,
2556 0, page
->index
, type
);
2558 wait_on_page_writeback(page
);
2560 wait_for_stable_page(page
);
2564 void f2fs_wait_on_block_writeback(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
2568 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
)
2571 cpage
= find_lock_page(META_MAPPING(sbi
), blkaddr
);
2573 f2fs_wait_on_page_writeback(cpage
, DATA
, true);
2574 f2fs_put_page(cpage
, 1);
2578 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
2580 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
2581 struct curseg_info
*seg_i
;
2582 unsigned char *kaddr
;
2587 start
= start_sum_block(sbi
);
2589 page
= get_meta_page(sbi
, start
++);
2590 kaddr
= (unsigned char *)page_address(page
);
2592 /* Step 1: restore nat cache */
2593 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
2594 memcpy(seg_i
->journal
, kaddr
, SUM_JOURNAL_SIZE
);
2596 /* Step 2: restore sit cache */
2597 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2598 memcpy(seg_i
->journal
, kaddr
+ SUM_JOURNAL_SIZE
, SUM_JOURNAL_SIZE
);
2599 offset
= 2 * SUM_JOURNAL_SIZE
;
2601 /* Step 3: restore summary entries */
2602 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
2603 unsigned short blk_off
;
2606 seg_i
= CURSEG_I(sbi
, i
);
2607 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
2608 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
2609 seg_i
->next_segno
= segno
;
2610 reset_curseg(sbi
, i
, 0);
2611 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
2612 seg_i
->next_blkoff
= blk_off
;
2614 if (seg_i
->alloc_type
== SSR
)
2615 blk_off
= sbi
->blocks_per_seg
;
2617 for (j
= 0; j
< blk_off
; j
++) {
2618 struct f2fs_summary
*s
;
2619 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
2620 seg_i
->sum_blk
->entries
[j
] = *s
;
2621 offset
+= SUMMARY_SIZE
;
2622 if (offset
+ SUMMARY_SIZE
<= PAGE_SIZE
-
2626 f2fs_put_page(page
, 1);
2629 page
= get_meta_page(sbi
, start
++);
2630 kaddr
= (unsigned char *)page_address(page
);
2634 f2fs_put_page(page
, 1);
2638 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
2640 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
2641 struct f2fs_summary_block
*sum
;
2642 struct curseg_info
*curseg
;
2644 unsigned short blk_off
;
2645 unsigned int segno
= 0;
2646 block_t blk_addr
= 0;
2648 /* get segment number and block addr */
2649 if (IS_DATASEG(type
)) {
2650 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
2651 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
2653 if (__exist_node_summaries(sbi
))
2654 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
2656 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
2658 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
2660 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
2662 if (__exist_node_summaries(sbi
))
2663 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
2664 type
- CURSEG_HOT_NODE
);
2666 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
2669 new = get_meta_page(sbi
, blk_addr
);
2670 sum
= (struct f2fs_summary_block
*)page_address(new);
2672 if (IS_NODESEG(type
)) {
2673 if (__exist_node_summaries(sbi
)) {
2674 struct f2fs_summary
*ns
= &sum
->entries
[0];
2676 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
2678 ns
->ofs_in_node
= 0;
2683 err
= restore_node_summary(sbi
, segno
, sum
);
2685 f2fs_put_page(new, 1);
2691 /* set uncompleted segment to curseg */
2692 curseg
= CURSEG_I(sbi
, type
);
2693 mutex_lock(&curseg
->curseg_mutex
);
2695 /* update journal info */
2696 down_write(&curseg
->journal_rwsem
);
2697 memcpy(curseg
->journal
, &sum
->journal
, SUM_JOURNAL_SIZE
);
2698 up_write(&curseg
->journal_rwsem
);
2700 memcpy(curseg
->sum_blk
->entries
, sum
->entries
, SUM_ENTRY_SIZE
);
2701 memcpy(&curseg
->sum_blk
->footer
, &sum
->footer
, SUM_FOOTER_SIZE
);
2702 curseg
->next_segno
= segno
;
2703 reset_curseg(sbi
, type
, 0);
2704 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
2705 curseg
->next_blkoff
= blk_off
;
2706 mutex_unlock(&curseg
->curseg_mutex
);
2707 f2fs_put_page(new, 1);
2711 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
2713 struct f2fs_journal
*sit_j
= CURSEG_I(sbi
, CURSEG_COLD_DATA
)->journal
;
2714 struct f2fs_journal
*nat_j
= CURSEG_I(sbi
, CURSEG_HOT_DATA
)->journal
;
2715 int type
= CURSEG_HOT_DATA
;
2718 if (is_set_ckpt_flags(sbi
, CP_COMPACT_SUM_FLAG
)) {
2719 int npages
= npages_for_summary_flush(sbi
, true);
2722 ra_meta_pages(sbi
, start_sum_block(sbi
), npages
,
2725 /* restore for compacted data summary */
2726 if (read_compacted_summaries(sbi
))
2728 type
= CURSEG_HOT_NODE
;
2731 if (__exist_node_summaries(sbi
))
2732 ra_meta_pages(sbi
, sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
),
2733 NR_CURSEG_TYPE
- type
, META_CP
, true);
2735 for (; type
<= CURSEG_COLD_NODE
; type
++) {
2736 err
= read_normal_summaries(sbi
, type
);
2741 /* sanity check for summary blocks */
2742 if (nats_in_cursum(nat_j
) > NAT_JOURNAL_ENTRIES
||
2743 sits_in_cursum(sit_j
) > SIT_JOURNAL_ENTRIES
)
2749 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
2752 unsigned char *kaddr
;
2753 struct f2fs_summary
*summary
;
2754 struct curseg_info
*seg_i
;
2755 int written_size
= 0;
2758 page
= grab_meta_page(sbi
, blkaddr
++);
2759 kaddr
= (unsigned char *)page_address(page
);
2761 /* Step 1: write nat cache */
2762 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
2763 memcpy(kaddr
, seg_i
->journal
, SUM_JOURNAL_SIZE
);
2764 written_size
+= SUM_JOURNAL_SIZE
;
2766 /* Step 2: write sit cache */
2767 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2768 memcpy(kaddr
+ written_size
, seg_i
->journal
, SUM_JOURNAL_SIZE
);
2769 written_size
+= SUM_JOURNAL_SIZE
;
2771 /* Step 3: write summary entries */
2772 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
2773 unsigned short blkoff
;
2774 seg_i
= CURSEG_I(sbi
, i
);
2775 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
2776 blkoff
= sbi
->blocks_per_seg
;
2778 blkoff
= curseg_blkoff(sbi
, i
);
2780 for (j
= 0; j
< blkoff
; j
++) {
2782 page
= grab_meta_page(sbi
, blkaddr
++);
2783 kaddr
= (unsigned char *)page_address(page
);
2786 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
2787 *summary
= seg_i
->sum_blk
->entries
[j
];
2788 written_size
+= SUMMARY_SIZE
;
2790 if (written_size
+ SUMMARY_SIZE
<= PAGE_SIZE
-
2794 set_page_dirty(page
);
2795 f2fs_put_page(page
, 1);
2800 set_page_dirty(page
);
2801 f2fs_put_page(page
, 1);
2805 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
2806 block_t blkaddr
, int type
)
2809 if (IS_DATASEG(type
))
2810 end
= type
+ NR_CURSEG_DATA_TYPE
;
2812 end
= type
+ NR_CURSEG_NODE_TYPE
;
2814 for (i
= type
; i
< end
; i
++)
2815 write_current_sum_page(sbi
, i
, blkaddr
+ (i
- type
));
2818 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
2820 if (is_set_ckpt_flags(sbi
, CP_COMPACT_SUM_FLAG
))
2821 write_compacted_summaries(sbi
, start_blk
);
2823 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
2826 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
2828 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
2831 int lookup_journal_in_cursum(struct f2fs_journal
*journal
, int type
,
2832 unsigned int val
, int alloc
)
2836 if (type
== NAT_JOURNAL
) {
2837 for (i
= 0; i
< nats_in_cursum(journal
); i
++) {
2838 if (le32_to_cpu(nid_in_journal(journal
, i
)) == val
)
2841 if (alloc
&& __has_cursum_space(journal
, 1, NAT_JOURNAL
))
2842 return update_nats_in_cursum(journal
, 1);
2843 } else if (type
== SIT_JOURNAL
) {
2844 for (i
= 0; i
< sits_in_cursum(journal
); i
++)
2845 if (le32_to_cpu(segno_in_journal(journal
, i
)) == val
)
2847 if (alloc
&& __has_cursum_space(journal
, 1, SIT_JOURNAL
))
2848 return update_sits_in_cursum(journal
, 1);
2853 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
2856 return get_meta_page(sbi
, current_sit_addr(sbi
, segno
));
2859 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
2862 struct sit_info
*sit_i
= SIT_I(sbi
);
2863 struct page
*src_page
, *dst_page
;
2864 pgoff_t src_off
, dst_off
;
2865 void *src_addr
, *dst_addr
;
2867 src_off
= current_sit_addr(sbi
, start
);
2868 dst_off
= next_sit_addr(sbi
, src_off
);
2870 /* get current sit block page without lock */
2871 src_page
= get_meta_page(sbi
, src_off
);
2872 dst_page
= grab_meta_page(sbi
, dst_off
);
2873 f2fs_bug_on(sbi
, PageDirty(src_page
));
2875 src_addr
= page_address(src_page
);
2876 dst_addr
= page_address(dst_page
);
2877 memcpy(dst_addr
, src_addr
, PAGE_SIZE
);
2879 set_page_dirty(dst_page
);
2880 f2fs_put_page(src_page
, 1);
2882 set_to_next_sit(sit_i
, start
);
2887 static struct sit_entry_set
*grab_sit_entry_set(void)
2889 struct sit_entry_set
*ses
=
2890 f2fs_kmem_cache_alloc(sit_entry_set_slab
, GFP_NOFS
);
2893 INIT_LIST_HEAD(&ses
->set_list
);
2897 static void release_sit_entry_set(struct sit_entry_set
*ses
)
2899 list_del(&ses
->set_list
);
2900 kmem_cache_free(sit_entry_set_slab
, ses
);
2903 static void adjust_sit_entry_set(struct sit_entry_set
*ses
,
2904 struct list_head
*head
)
2906 struct sit_entry_set
*next
= ses
;
2908 if (list_is_last(&ses
->set_list
, head
))
2911 list_for_each_entry_continue(next
, head
, set_list
)
2912 if (ses
->entry_cnt
<= next
->entry_cnt
)
2915 list_move_tail(&ses
->set_list
, &next
->set_list
);
2918 static void add_sit_entry(unsigned int segno
, struct list_head
*head
)
2920 struct sit_entry_set
*ses
;
2921 unsigned int start_segno
= START_SEGNO(segno
);
2923 list_for_each_entry(ses
, head
, set_list
) {
2924 if (ses
->start_segno
== start_segno
) {
2926 adjust_sit_entry_set(ses
, head
);
2931 ses
= grab_sit_entry_set();
2933 ses
->start_segno
= start_segno
;
2935 list_add(&ses
->set_list
, head
);
2938 static void add_sits_in_set(struct f2fs_sb_info
*sbi
)
2940 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
2941 struct list_head
*set_list
= &sm_info
->sit_entry_set
;
2942 unsigned long *bitmap
= SIT_I(sbi
)->dirty_sentries_bitmap
;
2945 for_each_set_bit(segno
, bitmap
, MAIN_SEGS(sbi
))
2946 add_sit_entry(segno
, set_list
);
2949 static void remove_sits_in_journal(struct f2fs_sb_info
*sbi
)
2951 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2952 struct f2fs_journal
*journal
= curseg
->journal
;
2955 down_write(&curseg
->journal_rwsem
);
2956 for (i
= 0; i
< sits_in_cursum(journal
); i
++) {
2960 segno
= le32_to_cpu(segno_in_journal(journal
, i
));
2961 dirtied
= __mark_sit_entry_dirty(sbi
, segno
);
2964 add_sit_entry(segno
, &SM_I(sbi
)->sit_entry_set
);
2966 update_sits_in_cursum(journal
, -i
);
2967 up_write(&curseg
->journal_rwsem
);
2971 * CP calls this function, which flushes SIT entries including sit_journal,
2972 * and moves prefree segs to free segs.
2974 void flush_sit_entries(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
2976 struct sit_info
*sit_i
= SIT_I(sbi
);
2977 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
2978 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2979 struct f2fs_journal
*journal
= curseg
->journal
;
2980 struct sit_entry_set
*ses
, *tmp
;
2981 struct list_head
*head
= &SM_I(sbi
)->sit_entry_set
;
2982 bool to_journal
= true;
2983 struct seg_entry
*se
;
2985 mutex_lock(&sit_i
->sentry_lock
);
2987 if (!sit_i
->dirty_sentries
)
2991 * add and account sit entries of dirty bitmap in sit entry
2994 add_sits_in_set(sbi
);
2997 * if there are no enough space in journal to store dirty sit
2998 * entries, remove all entries from journal and add and account
2999 * them in sit entry set.
3001 if (!__has_cursum_space(journal
, sit_i
->dirty_sentries
, SIT_JOURNAL
))
3002 remove_sits_in_journal(sbi
);
3005 * there are two steps to flush sit entries:
3006 * #1, flush sit entries to journal in current cold data summary block.
3007 * #2, flush sit entries to sit page.
3009 list_for_each_entry_safe(ses
, tmp
, head
, set_list
) {
3010 struct page
*page
= NULL
;
3011 struct f2fs_sit_block
*raw_sit
= NULL
;
3012 unsigned int start_segno
= ses
->start_segno
;
3013 unsigned int end
= min(start_segno
+ SIT_ENTRY_PER_BLOCK
,
3014 (unsigned long)MAIN_SEGS(sbi
));
3015 unsigned int segno
= start_segno
;
3018 !__has_cursum_space(journal
, ses
->entry_cnt
, SIT_JOURNAL
))
3022 down_write(&curseg
->journal_rwsem
);
3024 page
= get_next_sit_page(sbi
, start_segno
);
3025 raw_sit
= page_address(page
);
3028 /* flush dirty sit entries in region of current sit set */
3029 for_each_set_bit_from(segno
, bitmap
, end
) {
3030 int offset
, sit_offset
;
3032 se
= get_seg_entry(sbi
, segno
);
3034 /* add discard candidates */
3035 if (!(cpc
->reason
& CP_DISCARD
)) {
3036 cpc
->trim_start
= segno
;
3037 add_discard_addrs(sbi
, cpc
, false);
3041 offset
= lookup_journal_in_cursum(journal
,
3042 SIT_JOURNAL
, segno
, 1);
3043 f2fs_bug_on(sbi
, offset
< 0);
3044 segno_in_journal(journal
, offset
) =
3046 seg_info_to_raw_sit(se
,
3047 &sit_in_journal(journal
, offset
));
3049 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
3050 seg_info_to_raw_sit(se
,
3051 &raw_sit
->entries
[sit_offset
]);
3054 __clear_bit(segno
, bitmap
);
3055 sit_i
->dirty_sentries
--;
3060 up_write(&curseg
->journal_rwsem
);
3062 f2fs_put_page(page
, 1);
3064 f2fs_bug_on(sbi
, ses
->entry_cnt
);
3065 release_sit_entry_set(ses
);
3068 f2fs_bug_on(sbi
, !list_empty(head
));
3069 f2fs_bug_on(sbi
, sit_i
->dirty_sentries
);
3071 if (cpc
->reason
& CP_DISCARD
) {
3072 __u64 trim_start
= cpc
->trim_start
;
3074 for (; cpc
->trim_start
<= cpc
->trim_end
; cpc
->trim_start
++)
3075 add_discard_addrs(sbi
, cpc
, false);
3077 cpc
->trim_start
= trim_start
;
3079 mutex_unlock(&sit_i
->sentry_lock
);
3081 set_prefree_as_free_segments(sbi
);
3084 static int build_sit_info(struct f2fs_sb_info
*sbi
)
3086 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
3087 struct sit_info
*sit_i
;
3088 unsigned int sit_segs
, start
;
3090 unsigned int bitmap_size
;
3092 /* allocate memory for SIT information */
3093 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
3097 SM_I(sbi
)->sit_info
= sit_i
;
3099 sit_i
->sentries
= kvzalloc(MAIN_SEGS(sbi
) *
3100 sizeof(struct seg_entry
), GFP_KERNEL
);
3101 if (!sit_i
->sentries
)
3104 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
3105 sit_i
->dirty_sentries_bitmap
= kvzalloc(bitmap_size
, GFP_KERNEL
);
3106 if (!sit_i
->dirty_sentries_bitmap
)
3109 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
3110 sit_i
->sentries
[start
].cur_valid_map
3111 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
3112 sit_i
->sentries
[start
].ckpt_valid_map
3113 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
3114 if (!sit_i
->sentries
[start
].cur_valid_map
||
3115 !sit_i
->sentries
[start
].ckpt_valid_map
)
3118 #ifdef CONFIG_F2FS_CHECK_FS
3119 sit_i
->sentries
[start
].cur_valid_map_mir
3120 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
3121 if (!sit_i
->sentries
[start
].cur_valid_map_mir
)
3125 if (f2fs_discard_en(sbi
)) {
3126 sit_i
->sentries
[start
].discard_map
3127 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
3128 if (!sit_i
->sentries
[start
].discard_map
)
3133 sit_i
->tmp_map
= kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
3134 if (!sit_i
->tmp_map
)
3137 if (sbi
->segs_per_sec
> 1) {
3138 sit_i
->sec_entries
= kvzalloc(MAIN_SECS(sbi
) *
3139 sizeof(struct sec_entry
), GFP_KERNEL
);
3140 if (!sit_i
->sec_entries
)
3144 /* get information related with SIT */
3145 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
3147 /* setup SIT bitmap from ckeckpoint pack */
3148 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
3149 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
3151 sit_i
->sit_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
3152 if (!sit_i
->sit_bitmap
)
3155 #ifdef CONFIG_F2FS_CHECK_FS
3156 sit_i
->sit_bitmap_mir
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
3157 if (!sit_i
->sit_bitmap_mir
)
3161 /* init SIT information */
3162 sit_i
->s_ops
= &default_salloc_ops
;
3164 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
3165 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
3166 sit_i
->written_valid_blocks
= 0;
3167 sit_i
->bitmap_size
= bitmap_size
;
3168 sit_i
->dirty_sentries
= 0;
3169 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
3170 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
3171 sit_i
->mounted_time
= ktime_get_real_seconds();
3172 mutex_init(&sit_i
->sentry_lock
);
3176 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
3178 struct free_segmap_info
*free_i
;
3179 unsigned int bitmap_size
, sec_bitmap_size
;
3181 /* allocate memory for free segmap information */
3182 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
3186 SM_I(sbi
)->free_info
= free_i
;
3188 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
3189 free_i
->free_segmap
= kvmalloc(bitmap_size
, GFP_KERNEL
);
3190 if (!free_i
->free_segmap
)
3193 sec_bitmap_size
= f2fs_bitmap_size(MAIN_SECS(sbi
));
3194 free_i
->free_secmap
= kvmalloc(sec_bitmap_size
, GFP_KERNEL
);
3195 if (!free_i
->free_secmap
)
3198 /* set all segments as dirty temporarily */
3199 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
3200 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
3202 /* init free segmap information */
3203 free_i
->start_segno
= GET_SEGNO_FROM_SEG0(sbi
, MAIN_BLKADDR(sbi
));
3204 free_i
->free_segments
= 0;
3205 free_i
->free_sections
= 0;
3206 spin_lock_init(&free_i
->segmap_lock
);
3210 static int build_curseg(struct f2fs_sb_info
*sbi
)
3212 struct curseg_info
*array
;
3215 array
= kcalloc(NR_CURSEG_TYPE
, sizeof(*array
), GFP_KERNEL
);
3219 SM_I(sbi
)->curseg_array
= array
;
3221 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
3222 mutex_init(&array
[i
].curseg_mutex
);
3223 array
[i
].sum_blk
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
3224 if (!array
[i
].sum_blk
)
3226 init_rwsem(&array
[i
].journal_rwsem
);
3227 array
[i
].journal
= kzalloc(sizeof(struct f2fs_journal
),
3229 if (!array
[i
].journal
)
3231 array
[i
].segno
= NULL_SEGNO
;
3232 array
[i
].next_blkoff
= 0;
3234 return restore_curseg_summaries(sbi
);
3237 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
3239 struct sit_info
*sit_i
= SIT_I(sbi
);
3240 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
3241 struct f2fs_journal
*journal
= curseg
->journal
;
3242 struct seg_entry
*se
;
3243 struct f2fs_sit_entry sit
;
3244 int sit_blk_cnt
= SIT_BLK_CNT(sbi
);
3245 unsigned int i
, start
, end
;
3246 unsigned int readed
, start_blk
= 0;
3249 readed
= ra_meta_pages(sbi
, start_blk
, BIO_MAX_PAGES
,
3252 start
= start_blk
* sit_i
->sents_per_block
;
3253 end
= (start_blk
+ readed
) * sit_i
->sents_per_block
;
3255 for (; start
< end
&& start
< MAIN_SEGS(sbi
); start
++) {
3256 struct f2fs_sit_block
*sit_blk
;
3259 se
= &sit_i
->sentries
[start
];
3260 page
= get_current_sit_page(sbi
, start
);
3261 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
3262 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
3263 f2fs_put_page(page
, 1);
3265 check_block_count(sbi
, start
, &sit
);
3266 seg_info_from_raw_sit(se
, &sit
);
3268 /* build discard map only one time */
3269 if (f2fs_discard_en(sbi
)) {
3270 if (is_set_ckpt_flags(sbi
, CP_TRIMMED_FLAG
)) {
3271 memset(se
->discard_map
, 0xff,
3272 SIT_VBLOCK_MAP_SIZE
);
3274 memcpy(se
->discard_map
,
3276 SIT_VBLOCK_MAP_SIZE
);
3277 sbi
->discard_blks
+=
3278 sbi
->blocks_per_seg
-
3283 if (sbi
->segs_per_sec
> 1)
3284 get_sec_entry(sbi
, start
)->valid_blocks
+=
3287 start_blk
+= readed
;
3288 } while (start_blk
< sit_blk_cnt
);
3290 down_read(&curseg
->journal_rwsem
);
3291 for (i
= 0; i
< sits_in_cursum(journal
); i
++) {
3292 unsigned int old_valid_blocks
;
3294 start
= le32_to_cpu(segno_in_journal(journal
, i
));
3295 se
= &sit_i
->sentries
[start
];
3296 sit
= sit_in_journal(journal
, i
);
3298 old_valid_blocks
= se
->valid_blocks
;
3300 check_block_count(sbi
, start
, &sit
);
3301 seg_info_from_raw_sit(se
, &sit
);
3303 if (f2fs_discard_en(sbi
)) {
3304 if (is_set_ckpt_flags(sbi
, CP_TRIMMED_FLAG
)) {
3305 memset(se
->discard_map
, 0xff,
3306 SIT_VBLOCK_MAP_SIZE
);
3308 memcpy(se
->discard_map
, se
->cur_valid_map
,
3309 SIT_VBLOCK_MAP_SIZE
);
3310 sbi
->discard_blks
+= old_valid_blocks
-
3315 if (sbi
->segs_per_sec
> 1)
3316 get_sec_entry(sbi
, start
)->valid_blocks
+=
3317 se
->valid_blocks
- old_valid_blocks
;
3319 up_read(&curseg
->journal_rwsem
);
3322 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
3327 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
3328 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
3329 if (!sentry
->valid_blocks
)
3330 __set_free(sbi
, start
);
3332 SIT_I(sbi
)->written_valid_blocks
+=
3333 sentry
->valid_blocks
;
3336 /* set use the current segments */
3337 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
3338 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
3339 __set_test_and_inuse(sbi
, curseg_t
->segno
);
3343 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
3345 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
3346 struct free_segmap_info
*free_i
= FREE_I(sbi
);
3347 unsigned int segno
= 0, offset
= 0;
3348 unsigned short valid_blocks
;
3351 /* find dirty segment based on free segmap */
3352 segno
= find_next_inuse(free_i
, MAIN_SEGS(sbi
), offset
);
3353 if (segno
>= MAIN_SEGS(sbi
))
3356 valid_blocks
= get_valid_blocks(sbi
, segno
, false);
3357 if (valid_blocks
== sbi
->blocks_per_seg
|| !valid_blocks
)
3359 if (valid_blocks
> sbi
->blocks_per_seg
) {
3360 f2fs_bug_on(sbi
, 1);
3363 mutex_lock(&dirty_i
->seglist_lock
);
3364 __locate_dirty_segment(sbi
, segno
, DIRTY
);
3365 mutex_unlock(&dirty_i
->seglist_lock
);
3369 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
3371 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
3372 unsigned int bitmap_size
= f2fs_bitmap_size(MAIN_SECS(sbi
));
3374 dirty_i
->victim_secmap
= kvzalloc(bitmap_size
, GFP_KERNEL
);
3375 if (!dirty_i
->victim_secmap
)
3380 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
3382 struct dirty_seglist_info
*dirty_i
;
3383 unsigned int bitmap_size
, i
;
3385 /* allocate memory for dirty segments list information */
3386 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
3390 SM_I(sbi
)->dirty_info
= dirty_i
;
3391 mutex_init(&dirty_i
->seglist_lock
);
3393 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
3395 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
3396 dirty_i
->dirty_segmap
[i
] = kvzalloc(bitmap_size
, GFP_KERNEL
);
3397 if (!dirty_i
->dirty_segmap
[i
])
3401 init_dirty_segmap(sbi
);
3402 return init_victim_secmap(sbi
);
3406 * Update min, max modified time for cost-benefit GC algorithm
3408 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
3410 struct sit_info
*sit_i
= SIT_I(sbi
);
3413 mutex_lock(&sit_i
->sentry_lock
);
3415 sit_i
->min_mtime
= LLONG_MAX
;
3417 for (segno
= 0; segno
< MAIN_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
3419 unsigned long long mtime
= 0;
3421 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
3422 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
3424 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
3426 if (sit_i
->min_mtime
> mtime
)
3427 sit_i
->min_mtime
= mtime
;
3429 sit_i
->max_mtime
= get_mtime(sbi
);
3430 mutex_unlock(&sit_i
->sentry_lock
);
3433 int build_segment_manager(struct f2fs_sb_info
*sbi
)
3435 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
3436 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
3437 struct f2fs_sm_info
*sm_info
;
3440 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
3445 sbi
->sm_info
= sm_info
;
3446 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
3447 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
3448 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
3449 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
3450 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
3451 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
3452 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
3453 sm_info
->rec_prefree_segments
= sm_info
->main_segments
*
3454 DEF_RECLAIM_PREFREE_SEGMENTS
/ 100;
3455 if (sm_info
->rec_prefree_segments
> DEF_MAX_RECLAIM_PREFREE_SEGMENTS
)
3456 sm_info
->rec_prefree_segments
= DEF_MAX_RECLAIM_PREFREE_SEGMENTS
;
3458 if (!test_opt(sbi
, LFS
))
3459 sm_info
->ipu_policy
= 1 << F2FS_IPU_FSYNC
;
3460 sm_info
->min_ipu_util
= DEF_MIN_IPU_UTIL
;
3461 sm_info
->min_fsync_blocks
= DEF_MIN_FSYNC_BLOCKS
;
3462 sm_info
->min_hot_blocks
= DEF_MIN_HOT_BLOCKS
;
3464 sm_info
->trim_sections
= DEF_BATCHED_TRIM_SECTIONS
;
3466 INIT_LIST_HEAD(&sm_info
->sit_entry_set
);
3468 if (!f2fs_readonly(sbi
->sb
)) {
3469 err
= create_flush_cmd_control(sbi
);
3474 err
= create_discard_cmd_control(sbi
);
3478 err
= build_sit_info(sbi
);
3481 err
= build_free_segmap(sbi
);
3484 err
= build_curseg(sbi
);
3488 /* reinit free segmap based on SIT */
3489 build_sit_entries(sbi
);
3491 init_free_segmap(sbi
);
3492 err
= build_dirty_segmap(sbi
);
3496 init_min_max_mtime(sbi
);
3500 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
3501 enum dirty_type dirty_type
)
3503 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
3505 mutex_lock(&dirty_i
->seglist_lock
);
3506 kvfree(dirty_i
->dirty_segmap
[dirty_type
]);
3507 dirty_i
->nr_dirty
[dirty_type
] = 0;
3508 mutex_unlock(&dirty_i
->seglist_lock
);
3511 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
3513 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
3514 kvfree(dirty_i
->victim_secmap
);
3517 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
3519 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
3525 /* discard pre-free/dirty segments list */
3526 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
3527 discard_dirty_segmap(sbi
, i
);
3529 destroy_victim_secmap(sbi
);
3530 SM_I(sbi
)->dirty_info
= NULL
;
3534 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
3536 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
3541 SM_I(sbi
)->curseg_array
= NULL
;
3542 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
3543 kfree(array
[i
].sum_blk
);
3544 kfree(array
[i
].journal
);
3549 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
3551 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
3554 SM_I(sbi
)->free_info
= NULL
;
3555 kvfree(free_i
->free_segmap
);
3556 kvfree(free_i
->free_secmap
);
3560 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
3562 struct sit_info
*sit_i
= SIT_I(sbi
);
3568 if (sit_i
->sentries
) {
3569 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
3570 kfree(sit_i
->sentries
[start
].cur_valid_map
);
3571 #ifdef CONFIG_F2FS_CHECK_FS
3572 kfree(sit_i
->sentries
[start
].cur_valid_map_mir
);
3574 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
3575 kfree(sit_i
->sentries
[start
].discard_map
);
3578 kfree(sit_i
->tmp_map
);
3580 kvfree(sit_i
->sentries
);
3581 kvfree(sit_i
->sec_entries
);
3582 kvfree(sit_i
->dirty_sentries_bitmap
);
3584 SM_I(sbi
)->sit_info
= NULL
;
3585 kfree(sit_i
->sit_bitmap
);
3586 #ifdef CONFIG_F2FS_CHECK_FS
3587 kfree(sit_i
->sit_bitmap_mir
);
3592 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
3594 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
3598 destroy_flush_cmd_control(sbi
, true);
3599 destroy_discard_cmd_control(sbi
);
3600 destroy_dirty_segmap(sbi
);
3601 destroy_curseg(sbi
);
3602 destroy_free_segmap(sbi
);
3603 destroy_sit_info(sbi
);
3604 sbi
->sm_info
= NULL
;
3608 int __init
create_segment_manager_caches(void)
3610 discard_entry_slab
= f2fs_kmem_cache_create("discard_entry",
3611 sizeof(struct discard_entry
));
3612 if (!discard_entry_slab
)
3615 discard_cmd_slab
= f2fs_kmem_cache_create("discard_cmd",
3616 sizeof(struct discard_cmd
));
3617 if (!discard_cmd_slab
)
3618 goto destroy_discard_entry
;
3620 sit_entry_set_slab
= f2fs_kmem_cache_create("sit_entry_set",
3621 sizeof(struct sit_entry_set
));
3622 if (!sit_entry_set_slab
)
3623 goto destroy_discard_cmd
;
3625 inmem_entry_slab
= f2fs_kmem_cache_create("inmem_page_entry",
3626 sizeof(struct inmem_pages
));
3627 if (!inmem_entry_slab
)
3628 goto destroy_sit_entry_set
;
3631 destroy_sit_entry_set
:
3632 kmem_cache_destroy(sit_entry_set_slab
);
3633 destroy_discard_cmd
:
3634 kmem_cache_destroy(discard_cmd_slab
);
3635 destroy_discard_entry
:
3636 kmem_cache_destroy(discard_entry_slab
);
3641 void destroy_segment_manager_caches(void)
3643 kmem_cache_destroy(sit_entry_set_slab
);
3644 kmem_cache_destroy(discard_cmd_slab
);
3645 kmem_cache_destroy(discard_entry_slab
);
3646 kmem_cache_destroy(inmem_entry_slab
);