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>
24 #include <trace/events/f2fs.h>
26 #define __reverse_ffz(x) __reverse_ffs(~(x))
28 static struct kmem_cache
*discard_entry_slab
;
29 static struct kmem_cache
*discard_cmd_slab
;
30 static struct kmem_cache
*sit_entry_set_slab
;
31 static struct kmem_cache
*inmem_entry_slab
;
33 static unsigned long __reverse_ulong(unsigned char *str
)
35 unsigned long tmp
= 0;
36 int shift
= 24, idx
= 0;
38 #if BITS_PER_LONG == 64
42 tmp
|= (unsigned long)str
[idx
++] << shift
;
43 shift
-= BITS_PER_BYTE
;
49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50 * MSB and LSB are reversed in a byte by f2fs_set_bit.
52 static inline unsigned long __reverse_ffs(unsigned long word
)
56 #if BITS_PER_LONG == 64
57 if ((word
& 0xffffffff00000000UL
) == 0)
62 if ((word
& 0xffff0000) == 0)
67 if ((word
& 0xff00) == 0)
72 if ((word
& 0xf0) == 0)
77 if ((word
& 0xc) == 0)
82 if ((word
& 0x2) == 0)
88 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
89 * f2fs_set_bit makes MSB and LSB reversed in a byte.
90 * @size must be integral times of unsigned long.
93 * f2fs_set_bit(0, bitmap) => 1000 0000
94 * f2fs_set_bit(7, bitmap) => 0000 0001
96 static unsigned long __find_rev_next_bit(const unsigned long *addr
,
97 unsigned long size
, unsigned long offset
)
99 const unsigned long *p
= addr
+ BIT_WORD(offset
);
100 unsigned long result
= size
;
106 size
-= (offset
& ~(BITS_PER_LONG
- 1));
107 offset
%= BITS_PER_LONG
;
113 tmp
= __reverse_ulong((unsigned char *)p
);
115 tmp
&= ~0UL >> offset
;
116 if (size
< BITS_PER_LONG
)
117 tmp
&= (~0UL << (BITS_PER_LONG
- size
));
121 if (size
<= BITS_PER_LONG
)
123 size
-= BITS_PER_LONG
;
129 return result
- size
+ __reverse_ffs(tmp
);
132 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr
,
133 unsigned long size
, unsigned long offset
)
135 const unsigned long *p
= addr
+ BIT_WORD(offset
);
136 unsigned long result
= size
;
142 size
-= (offset
& ~(BITS_PER_LONG
- 1));
143 offset
%= BITS_PER_LONG
;
149 tmp
= __reverse_ulong((unsigned char *)p
);
152 tmp
|= ~0UL << (BITS_PER_LONG
- offset
);
153 if (size
< BITS_PER_LONG
)
158 if (size
<= BITS_PER_LONG
)
160 size
-= BITS_PER_LONG
;
166 return result
- size
+ __reverse_ffz(tmp
);
169 void register_inmem_page(struct inode
*inode
, struct page
*page
)
171 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
172 struct inmem_pages
*new;
174 f2fs_trace_pid(page
);
176 set_page_private(page
, (unsigned long)ATOMIC_WRITTEN_PAGE
);
177 SetPagePrivate(page
);
179 new = f2fs_kmem_cache_alloc(inmem_entry_slab
, GFP_NOFS
);
181 /* add atomic page indices to the list */
183 INIT_LIST_HEAD(&new->list
);
185 /* increase reference count with clean state */
186 mutex_lock(&fi
->inmem_lock
);
188 list_add_tail(&new->list
, &fi
->inmem_pages
);
189 inc_page_count(F2FS_I_SB(inode
), F2FS_INMEM_PAGES
);
190 mutex_unlock(&fi
->inmem_lock
);
192 trace_f2fs_register_inmem_page(page
, INMEM
);
195 static int __revoke_inmem_pages(struct inode
*inode
,
196 struct list_head
*head
, bool drop
, bool recover
)
198 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
199 struct inmem_pages
*cur
, *tmp
;
202 list_for_each_entry_safe(cur
, tmp
, head
, list
) {
203 struct page
*page
= cur
->page
;
206 trace_f2fs_commit_inmem_page(page
, INMEM_DROP
);
211 struct dnode_of_data dn
;
214 trace_f2fs_commit_inmem_page(page
, INMEM_REVOKE
);
216 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
217 if (get_dnode_of_data(&dn
, page
->index
, LOOKUP_NODE
)) {
221 get_node_info(sbi
, dn
.nid
, &ni
);
222 f2fs_replace_block(sbi
, &dn
, dn
.data_blkaddr
,
223 cur
->old_addr
, ni
.version
, true, true);
227 /* we don't need to invalidate this in the sccessful status */
229 ClearPageUptodate(page
);
230 set_page_private(page
, 0);
231 ClearPagePrivate(page
);
232 f2fs_put_page(page
, 1);
234 list_del(&cur
->list
);
235 kmem_cache_free(inmem_entry_slab
, cur
);
236 dec_page_count(F2FS_I_SB(inode
), F2FS_INMEM_PAGES
);
241 void drop_inmem_pages(struct inode
*inode
)
243 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
245 mutex_lock(&fi
->inmem_lock
);
246 __revoke_inmem_pages(inode
, &fi
->inmem_pages
, true, false);
247 mutex_unlock(&fi
->inmem_lock
);
249 clear_inode_flag(inode
, FI_ATOMIC_FILE
);
250 stat_dec_atomic_write(inode
);
253 static int __commit_inmem_pages(struct inode
*inode
,
254 struct list_head
*revoke_list
)
256 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
257 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
258 struct inmem_pages
*cur
, *tmp
;
259 struct f2fs_io_info fio
= {
263 .op_flags
= REQ_SYNC
| REQ_PRIO
,
264 .encrypted_page
= NULL
,
266 pgoff_t last_idx
= ULONG_MAX
;
269 list_for_each_entry_safe(cur
, tmp
, &fi
->inmem_pages
, list
) {
270 struct page
*page
= cur
->page
;
273 if (page
->mapping
== inode
->i_mapping
) {
274 trace_f2fs_commit_inmem_page(page
, INMEM
);
276 set_page_dirty(page
);
277 f2fs_wait_on_page_writeback(page
, DATA
, true);
278 if (clear_page_dirty_for_io(page
)) {
279 inode_dec_dirty_pages(inode
);
280 remove_dirty_inode(inode
);
284 err
= do_write_data_page(&fio
);
290 /* record old blkaddr for revoking */
291 cur
->old_addr
= fio
.old_blkaddr
;
292 last_idx
= page
->index
;
295 list_move_tail(&cur
->list
, revoke_list
);
298 if (last_idx
!= ULONG_MAX
)
299 f2fs_submit_merged_bio_cond(sbi
, inode
, 0, last_idx
,
303 __revoke_inmem_pages(inode
, revoke_list
, false, false);
308 int commit_inmem_pages(struct inode
*inode
)
310 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
311 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
312 struct list_head revoke_list
;
315 INIT_LIST_HEAD(&revoke_list
);
316 f2fs_balance_fs(sbi
, true);
319 set_inode_flag(inode
, FI_ATOMIC_COMMIT
);
321 mutex_lock(&fi
->inmem_lock
);
322 err
= __commit_inmem_pages(inode
, &revoke_list
);
326 * try to revoke all committed pages, but still we could fail
327 * due to no memory or other reason, if that happened, EAGAIN
328 * will be returned, which means in such case, transaction is
329 * already not integrity, caller should use journal to do the
330 * recovery or rewrite & commit last transaction. For other
331 * error number, revoking was done by filesystem itself.
333 ret
= __revoke_inmem_pages(inode
, &revoke_list
, false, true);
337 /* drop all uncommitted pages */
338 __revoke_inmem_pages(inode
, &fi
->inmem_pages
, true, false);
340 mutex_unlock(&fi
->inmem_lock
);
342 clear_inode_flag(inode
, FI_ATOMIC_COMMIT
);
349 * This function balances dirty node and dentry pages.
350 * In addition, it controls garbage collection.
352 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
, bool need
)
354 #ifdef CONFIG_F2FS_FAULT_INJECTION
355 if (time_to_inject(sbi
, FAULT_CHECKPOINT
))
356 f2fs_stop_checkpoint(sbi
, false);
362 /* balance_fs_bg is able to be pending */
363 if (excess_cached_nats(sbi
))
364 f2fs_balance_fs_bg(sbi
);
367 * We should do GC or end up with checkpoint, if there are so many dirty
368 * dir/node pages without enough free segments.
370 if (has_not_enough_free_secs(sbi
, 0, 0)) {
371 mutex_lock(&sbi
->gc_mutex
);
372 f2fs_gc(sbi
, false, false);
376 void f2fs_balance_fs_bg(struct f2fs_sb_info
*sbi
)
378 /* try to shrink extent cache when there is no enough memory */
379 if (!available_free_memory(sbi
, EXTENT_CACHE
))
380 f2fs_shrink_extent_tree(sbi
, EXTENT_CACHE_SHRINK_NUMBER
);
382 /* check the # of cached NAT entries */
383 if (!available_free_memory(sbi
, NAT_ENTRIES
))
384 try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
);
386 if (!available_free_memory(sbi
, FREE_NIDS
))
387 try_to_free_nids(sbi
, MAX_FREE_NIDS
);
389 build_free_nids(sbi
, false);
394 /* checkpoint is the only way to shrink partial cached entries */
395 if (!available_free_memory(sbi
, NAT_ENTRIES
) ||
396 !available_free_memory(sbi
, INO_ENTRIES
) ||
397 excess_prefree_segs(sbi
) ||
398 excess_dirty_nats(sbi
) ||
399 f2fs_time_over(sbi
, CP_TIME
)) {
400 if (test_opt(sbi
, DATA_FLUSH
)) {
401 struct blk_plug plug
;
403 blk_start_plug(&plug
);
404 sync_dirty_inodes(sbi
, FILE_INODE
);
405 blk_finish_plug(&plug
);
407 f2fs_sync_fs(sbi
->sb
, true);
408 stat_inc_bg_cp_count(sbi
->stat_info
);
412 static int __submit_flush_wait(struct block_device
*bdev
)
414 struct bio
*bio
= f2fs_bio_alloc(0);
417 bio
->bi_opf
= REQ_OP_WRITE
| REQ_PREFLUSH
;
419 ret
= submit_bio_wait(bio
);
424 static int submit_flush_wait(struct f2fs_sb_info
*sbi
)
426 int ret
= __submit_flush_wait(sbi
->sb
->s_bdev
);
429 if (sbi
->s_ndevs
&& !ret
) {
430 for (i
= 1; i
< sbi
->s_ndevs
; i
++) {
431 trace_f2fs_issue_flush(FDEV(i
).bdev
,
432 test_opt(sbi
, NOBARRIER
),
433 test_opt(sbi
, FLUSH_MERGE
));
434 ret
= __submit_flush_wait(FDEV(i
).bdev
);
442 static int issue_flush_thread(void *data
)
444 struct f2fs_sb_info
*sbi
= data
;
445 struct flush_cmd_control
*fcc
= SM_I(sbi
)->fcc_info
;
446 wait_queue_head_t
*q
= &fcc
->flush_wait_queue
;
448 if (kthread_should_stop())
451 if (!llist_empty(&fcc
->issue_list
)) {
452 struct flush_cmd
*cmd
, *next
;
455 fcc
->dispatch_list
= llist_del_all(&fcc
->issue_list
);
456 fcc
->dispatch_list
= llist_reverse_order(fcc
->dispatch_list
);
458 ret
= submit_flush_wait(sbi
);
459 llist_for_each_entry_safe(cmd
, next
,
460 fcc
->dispatch_list
, llnode
) {
462 complete(&cmd
->wait
);
464 fcc
->dispatch_list
= NULL
;
467 wait_event_interruptible(*q
,
468 kthread_should_stop() || !llist_empty(&fcc
->issue_list
));
472 int f2fs_issue_flush(struct f2fs_sb_info
*sbi
)
474 struct flush_cmd_control
*fcc
= SM_I(sbi
)->fcc_info
;
475 struct flush_cmd cmd
;
477 if (test_opt(sbi
, NOBARRIER
))
480 if (!test_opt(sbi
, FLUSH_MERGE
))
481 return submit_flush_wait(sbi
);
483 if (!atomic_read(&fcc
->submit_flush
)) {
486 atomic_inc(&fcc
->submit_flush
);
487 ret
= submit_flush_wait(sbi
);
488 atomic_dec(&fcc
->submit_flush
);
492 init_completion(&cmd
.wait
);
494 atomic_inc(&fcc
->submit_flush
);
495 llist_add(&cmd
.llnode
, &fcc
->issue_list
);
497 if (!fcc
->dispatch_list
)
498 wake_up(&fcc
->flush_wait_queue
);
500 if (fcc
->f2fs_issue_flush
) {
501 wait_for_completion(&cmd
.wait
);
502 atomic_dec(&fcc
->submit_flush
);
504 llist_del_all(&fcc
->issue_list
);
505 atomic_set(&fcc
->submit_flush
, 0);
511 int create_flush_cmd_control(struct f2fs_sb_info
*sbi
)
513 dev_t dev
= sbi
->sb
->s_bdev
->bd_dev
;
514 struct flush_cmd_control
*fcc
;
517 if (SM_I(sbi
)->fcc_info
) {
518 fcc
= SM_I(sbi
)->fcc_info
;
522 fcc
= kzalloc(sizeof(struct flush_cmd_control
), GFP_KERNEL
);
525 atomic_set(&fcc
->submit_flush
, 0);
526 init_waitqueue_head(&fcc
->flush_wait_queue
);
527 init_llist_head(&fcc
->issue_list
);
528 SM_I(sbi
)->fcc_info
= fcc
;
530 fcc
->f2fs_issue_flush
= kthread_run(issue_flush_thread
, sbi
,
531 "f2fs_flush-%u:%u", MAJOR(dev
), MINOR(dev
));
532 if (IS_ERR(fcc
->f2fs_issue_flush
)) {
533 err
= PTR_ERR(fcc
->f2fs_issue_flush
);
535 SM_I(sbi
)->fcc_info
= NULL
;
542 void destroy_flush_cmd_control(struct f2fs_sb_info
*sbi
, bool free
)
544 struct flush_cmd_control
*fcc
= SM_I(sbi
)->fcc_info
;
546 if (fcc
&& fcc
->f2fs_issue_flush
) {
547 struct task_struct
*flush_thread
= fcc
->f2fs_issue_flush
;
549 fcc
->f2fs_issue_flush
= NULL
;
550 kthread_stop(flush_thread
);
554 SM_I(sbi
)->fcc_info
= NULL
;
558 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
559 enum dirty_type dirty_type
)
561 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
563 /* need not be added */
564 if (IS_CURSEG(sbi
, segno
))
567 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
568 dirty_i
->nr_dirty
[dirty_type
]++;
570 if (dirty_type
== DIRTY
) {
571 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
572 enum dirty_type t
= sentry
->type
;
574 if (unlikely(t
>= DIRTY
)) {
578 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[t
]))
579 dirty_i
->nr_dirty
[t
]++;
583 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
584 enum dirty_type dirty_type
)
586 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
588 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
589 dirty_i
->nr_dirty
[dirty_type
]--;
591 if (dirty_type
== DIRTY
) {
592 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
593 enum dirty_type t
= sentry
->type
;
595 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
596 dirty_i
->nr_dirty
[t
]--;
598 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
599 clear_bit(GET_SECNO(sbi
, segno
),
600 dirty_i
->victim_secmap
);
605 * Should not occur error such as -ENOMEM.
606 * Adding dirty entry into seglist is not critical operation.
607 * If a given segment is one of current working segments, it won't be added.
609 static void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
611 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
612 unsigned short valid_blocks
;
614 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
617 mutex_lock(&dirty_i
->seglist_lock
);
619 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
621 if (valid_blocks
== 0) {
622 __locate_dirty_segment(sbi
, segno
, PRE
);
623 __remove_dirty_segment(sbi
, segno
, DIRTY
);
624 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
625 __locate_dirty_segment(sbi
, segno
, DIRTY
);
627 /* Recovery routine with SSR needs this */
628 __remove_dirty_segment(sbi
, segno
, DIRTY
);
631 mutex_unlock(&dirty_i
->seglist_lock
);
634 static void __add_discard_cmd(struct f2fs_sb_info
*sbi
,
635 struct bio
*bio
, block_t lstart
, block_t len
)
637 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
638 struct list_head
*cmd_list
= &(dcc
->discard_cmd_list
);
639 struct discard_cmd
*dc
;
641 dc
= f2fs_kmem_cache_alloc(discard_cmd_slab
, GFP_NOFS
);
642 INIT_LIST_HEAD(&dc
->list
);
644 bio
->bi_private
= dc
;
648 init_completion(&dc
->wait
);
650 mutex_lock(&dcc
->cmd_lock
);
651 list_add_tail(&dc
->list
, cmd_list
);
652 mutex_unlock(&dcc
->cmd_lock
);
655 static void __remove_discard_cmd(struct f2fs_sb_info
*sbi
, struct discard_cmd
*dc
)
657 int err
= dc
->bio
->bi_error
;
659 if (dc
->state
== D_DONE
)
660 atomic_dec(&(SM_I(sbi
)->dcc_info
->submit_discard
));
662 if (err
== -EOPNOTSUPP
)
666 f2fs_msg(sbi
->sb
, KERN_INFO
,
667 "Issue discard failed, ret: %d", err
);
670 kmem_cache_free(discard_cmd_slab
, dc
);
673 /* This should be covered by global mutex, &sit_i->sentry_lock */
674 void f2fs_wait_discard_bio(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
676 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
677 struct list_head
*wait_list
= &(dcc
->discard_cmd_list
);
678 struct discard_cmd
*dc
, *tmp
;
680 mutex_lock(&dcc
->cmd_lock
);
681 list_for_each_entry_safe(dc
, tmp
, wait_list
, list
) {
683 if (blkaddr
== NULL_ADDR
) {
684 if (dc
->state
== D_PREP
) {
685 dc
->state
= D_SUBMIT
;
687 atomic_inc(&dcc
->submit_discard
);
689 wait_for_completion_io(&dc
->wait
);
691 __remove_discard_cmd(sbi
, dc
);
695 if (dc
->lstart
<= blkaddr
&& blkaddr
< dc
->lstart
+ dc
->len
) {
696 if (dc
->state
== D_SUBMIT
)
697 wait_for_completion_io(&dc
->wait
);
699 __remove_discard_cmd(sbi
, dc
);
702 mutex_unlock(&dcc
->cmd_lock
);
705 static void f2fs_submit_discard_endio(struct bio
*bio
)
707 struct discard_cmd
*dc
= (struct discard_cmd
*)bio
->bi_private
;
713 static int issue_discard_thread(void *data
)
715 struct f2fs_sb_info
*sbi
= data
;
716 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
717 wait_queue_head_t
*q
= &dcc
->discard_wait_queue
;
718 struct list_head
*cmd_list
= &dcc
->discard_cmd_list
;
719 struct discard_cmd
*dc
, *tmp
;
720 struct blk_plug plug
;
723 if (kthread_should_stop())
726 blk_start_plug(&plug
);
728 mutex_lock(&dcc
->cmd_lock
);
729 list_for_each_entry_safe(dc
, tmp
, cmd_list
, list
) {
730 if (dc
->state
== D_PREP
) {
731 dc
->state
= D_SUBMIT
;
733 atomic_inc(&dcc
->submit_discard
);
734 if (iter
++ > DISCARD_ISSUE_RATE
)
736 } else if (dc
->state
== D_DONE
) {
737 __remove_discard_cmd(sbi
, dc
);
740 mutex_unlock(&dcc
->cmd_lock
);
742 blk_finish_plug(&plug
);
745 congestion_wait(BLK_RW_SYNC
, HZ
/50);
747 wait_event_interruptible(*q
,
748 kthread_should_stop() || !list_empty(&dcc
->discard_cmd_list
));
753 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
754 static int __f2fs_issue_discard_async(struct f2fs_sb_info
*sbi
,
755 struct block_device
*bdev
, block_t blkstart
, block_t blklen
)
757 struct bio
*bio
= NULL
;
758 block_t lblkstart
= blkstart
;
761 trace_f2fs_issue_discard(bdev
, blkstart
, blklen
);
764 int devi
= f2fs_target_device_index(sbi
, blkstart
);
766 blkstart
-= FDEV(devi
).start_blk
;
768 err
= __blkdev_issue_discard(bdev
,
769 SECTOR_FROM_BLOCK(blkstart
),
770 SECTOR_FROM_BLOCK(blklen
),
773 bio
->bi_end_io
= f2fs_submit_discard_endio
;
774 bio
->bi_opf
|= REQ_SYNC
;
776 __add_discard_cmd(sbi
, bio
, lblkstart
, blklen
);
777 wake_up(&SM_I(sbi
)->dcc_info
->discard_wait_queue
);
782 #ifdef CONFIG_BLK_DEV_ZONED
783 static int __f2fs_issue_discard_zone(struct f2fs_sb_info
*sbi
,
784 struct block_device
*bdev
, block_t blkstart
, block_t blklen
)
786 sector_t nr_sects
= SECTOR_FROM_BLOCK(blklen
);
791 devi
= f2fs_target_device_index(sbi
, blkstart
);
792 blkstart
-= FDEV(devi
).start_blk
;
794 sector
= SECTOR_FROM_BLOCK(blkstart
);
796 if (sector
& (bdev_zone_sectors(bdev
) - 1) ||
797 nr_sects
!= bdev_zone_sectors(bdev
)) {
798 f2fs_msg(sbi
->sb
, KERN_INFO
,
799 "(%d) %s: Unaligned discard attempted (block %x + %x)",
800 devi
, sbi
->s_ndevs
? FDEV(devi
).path
: "",
806 * We need to know the type of the zone: for conventional zones,
807 * use regular discard if the drive supports it. For sequential
808 * zones, reset the zone write pointer.
810 switch (get_blkz_type(sbi
, bdev
, blkstart
)) {
812 case BLK_ZONE_TYPE_CONVENTIONAL
:
813 if (!blk_queue_discard(bdev_get_queue(bdev
)))
815 return __f2fs_issue_discard_async(sbi
, bdev
, blkstart
, blklen
);
816 case BLK_ZONE_TYPE_SEQWRITE_REQ
:
817 case BLK_ZONE_TYPE_SEQWRITE_PREF
:
818 trace_f2fs_issue_reset_zone(bdev
, blkstart
);
819 return blkdev_reset_zones(bdev
, sector
,
822 /* Unknown zone type: broken device ? */
828 static int __issue_discard_async(struct f2fs_sb_info
*sbi
,
829 struct block_device
*bdev
, block_t blkstart
, block_t blklen
)
831 #ifdef CONFIG_BLK_DEV_ZONED
832 if (f2fs_sb_mounted_blkzoned(sbi
->sb
) &&
833 bdev_zoned_model(bdev
) != BLK_ZONED_NONE
)
834 return __f2fs_issue_discard_zone(sbi
, bdev
, blkstart
, blklen
);
836 return __f2fs_issue_discard_async(sbi
, bdev
, blkstart
, blklen
);
839 static int f2fs_issue_discard(struct f2fs_sb_info
*sbi
,
840 block_t blkstart
, block_t blklen
)
842 sector_t start
= blkstart
, len
= 0;
843 struct block_device
*bdev
;
844 struct seg_entry
*se
;
849 bdev
= f2fs_target_device(sbi
, blkstart
, NULL
);
851 for (i
= blkstart
; i
< blkstart
+ blklen
; i
++, len
++) {
853 struct block_device
*bdev2
=
854 f2fs_target_device(sbi
, i
, NULL
);
857 err
= __issue_discard_async(sbi
, bdev
,
867 se
= get_seg_entry(sbi
, GET_SEGNO(sbi
, i
));
868 offset
= GET_BLKOFF_FROM_SEG0(sbi
, i
);
870 if (!f2fs_test_and_set_bit(offset
, se
->discard_map
))
875 err
= __issue_discard_async(sbi
, bdev
, start
, len
);
879 static void __add_discard_entry(struct f2fs_sb_info
*sbi
,
880 struct cp_control
*cpc
, struct seg_entry
*se
,
881 unsigned int start
, unsigned int end
)
883 struct list_head
*head
= &SM_I(sbi
)->dcc_info
->discard_entry_list
;
884 struct discard_entry
*new, *last
;
886 if (!list_empty(head
)) {
887 last
= list_last_entry(head
, struct discard_entry
, list
);
888 if (START_BLOCK(sbi
, cpc
->trim_start
) + start
==
889 last
->blkaddr
+ last
->len
) {
890 last
->len
+= end
- start
;
895 new = f2fs_kmem_cache_alloc(discard_entry_slab
, GFP_NOFS
);
896 INIT_LIST_HEAD(&new->list
);
897 new->blkaddr
= START_BLOCK(sbi
, cpc
->trim_start
) + start
;
898 new->len
= end
- start
;
899 list_add_tail(&new->list
, head
);
901 SM_I(sbi
)->dcc_info
->nr_discards
+= end
- start
;
904 static bool add_discard_addrs(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
,
907 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
908 int max_blocks
= sbi
->blocks_per_seg
;
909 struct seg_entry
*se
= get_seg_entry(sbi
, cpc
->trim_start
);
910 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
911 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
912 unsigned long *discard_map
= (unsigned long *)se
->discard_map
;
913 unsigned long *dmap
= SIT_I(sbi
)->tmp_map
;
914 unsigned int start
= 0, end
= -1;
915 bool force
= (cpc
->reason
== CP_DISCARD
);
918 if (se
->valid_blocks
== max_blocks
|| !f2fs_discard_en(sbi
))
922 if (!test_opt(sbi
, DISCARD
) || !se
->valid_blocks
||
923 SM_I(sbi
)->dcc_info
->nr_discards
>=
924 SM_I(sbi
)->dcc_info
->max_discards
)
928 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
929 for (i
= 0; i
< entries
; i
++)
930 dmap
[i
] = force
? ~ckpt_map
[i
] & ~discard_map
[i
] :
931 (cur_map
[i
] ^ ckpt_map
[i
]) & ckpt_map
[i
];
933 while (force
|| SM_I(sbi
)->dcc_info
->nr_discards
<=
934 SM_I(sbi
)->dcc_info
->max_discards
) {
935 start
= __find_rev_next_bit(dmap
, max_blocks
, end
+ 1);
936 if (start
>= max_blocks
)
939 end
= __find_rev_next_zero_bit(dmap
, max_blocks
, start
+ 1);
940 if (force
&& start
&& end
!= max_blocks
941 && (end
- start
) < cpc
->trim_minlen
)
947 __add_discard_entry(sbi
, cpc
, se
, start
, end
);
952 void release_discard_addrs(struct f2fs_sb_info
*sbi
)
954 struct list_head
*head
= &(SM_I(sbi
)->dcc_info
->discard_entry_list
);
955 struct discard_entry
*entry
, *this;
958 list_for_each_entry_safe(entry
, this, head
, list
) {
959 list_del(&entry
->list
);
960 kmem_cache_free(discard_entry_slab
, entry
);
965 * Should call clear_prefree_segments after checkpoint is done.
967 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
969 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
972 mutex_lock(&dirty_i
->seglist_lock
);
973 for_each_set_bit(segno
, dirty_i
->dirty_segmap
[PRE
], MAIN_SEGS(sbi
))
974 __set_test_and_free(sbi
, segno
);
975 mutex_unlock(&dirty_i
->seglist_lock
);
978 void clear_prefree_segments(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
980 struct list_head
*head
= &(SM_I(sbi
)->dcc_info
->discard_entry_list
);
981 struct discard_entry
*entry
, *this;
982 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
983 unsigned long *prefree_map
= dirty_i
->dirty_segmap
[PRE
];
984 unsigned int start
= 0, end
= -1;
985 unsigned int secno
, start_segno
;
986 bool force
= (cpc
->reason
== CP_DISCARD
);
988 mutex_lock(&dirty_i
->seglist_lock
);
992 start
= find_next_bit(prefree_map
, MAIN_SEGS(sbi
), end
+ 1);
993 if (start
>= MAIN_SEGS(sbi
))
995 end
= find_next_zero_bit(prefree_map
, MAIN_SEGS(sbi
),
998 for (i
= start
; i
< end
; i
++)
999 clear_bit(i
, prefree_map
);
1001 dirty_i
->nr_dirty
[PRE
] -= end
- start
;
1003 if (!test_opt(sbi
, DISCARD
))
1006 if (force
&& start
>= cpc
->trim_start
&&
1007 (end
- 1) <= cpc
->trim_end
)
1010 if (!test_opt(sbi
, LFS
) || sbi
->segs_per_sec
== 1) {
1011 f2fs_issue_discard(sbi
, START_BLOCK(sbi
, start
),
1012 (end
- start
) << sbi
->log_blocks_per_seg
);
1016 secno
= GET_SECNO(sbi
, start
);
1017 start_segno
= secno
* sbi
->segs_per_sec
;
1018 if (!IS_CURSEC(sbi
, secno
) &&
1019 !get_valid_blocks(sbi
, start
, sbi
->segs_per_sec
))
1020 f2fs_issue_discard(sbi
, START_BLOCK(sbi
, start_segno
),
1021 sbi
->segs_per_sec
<< sbi
->log_blocks_per_seg
);
1023 start
= start_segno
+ sbi
->segs_per_sec
;
1027 mutex_unlock(&dirty_i
->seglist_lock
);
1029 /* send small discards */
1030 list_for_each_entry_safe(entry
, this, head
, list
) {
1031 if (force
&& entry
->len
< cpc
->trim_minlen
)
1033 f2fs_issue_discard(sbi
, entry
->blkaddr
, entry
->len
);
1034 cpc
->trimmed
+= entry
->len
;
1036 list_del(&entry
->list
);
1037 SM_I(sbi
)->dcc_info
->nr_discards
-= entry
->len
;
1038 kmem_cache_free(discard_entry_slab
, entry
);
1042 static int create_discard_cmd_control(struct f2fs_sb_info
*sbi
)
1044 dev_t dev
= sbi
->sb
->s_bdev
->bd_dev
;
1045 struct discard_cmd_control
*dcc
;
1048 if (SM_I(sbi
)->dcc_info
) {
1049 dcc
= SM_I(sbi
)->dcc_info
;
1053 dcc
= kzalloc(sizeof(struct discard_cmd_control
), GFP_KERNEL
);
1057 INIT_LIST_HEAD(&dcc
->discard_entry_list
);
1058 INIT_LIST_HEAD(&dcc
->discard_cmd_list
);
1059 mutex_init(&dcc
->cmd_lock
);
1060 atomic_set(&dcc
->submit_discard
, 0);
1061 dcc
->nr_discards
= 0;
1062 dcc
->max_discards
= 0;
1064 init_waitqueue_head(&dcc
->discard_wait_queue
);
1065 SM_I(sbi
)->dcc_info
= dcc
;
1067 dcc
->f2fs_issue_discard
= kthread_run(issue_discard_thread
, sbi
,
1068 "f2fs_discard-%u:%u", MAJOR(dev
), MINOR(dev
));
1069 if (IS_ERR(dcc
->f2fs_issue_discard
)) {
1070 err
= PTR_ERR(dcc
->f2fs_issue_discard
);
1072 SM_I(sbi
)->dcc_info
= NULL
;
1079 static void destroy_discard_cmd_control(struct f2fs_sb_info
*sbi
, bool free
)
1081 struct discard_cmd_control
*dcc
= SM_I(sbi
)->dcc_info
;
1083 if (dcc
&& dcc
->f2fs_issue_discard
) {
1084 struct task_struct
*discard_thread
= dcc
->f2fs_issue_discard
;
1086 dcc
->f2fs_issue_discard
= NULL
;
1087 kthread_stop(discard_thread
);
1091 SM_I(sbi
)->dcc_info
= NULL
;
1095 static bool __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
1097 struct sit_info
*sit_i
= SIT_I(sbi
);
1099 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
)) {
1100 sit_i
->dirty_sentries
++;
1107 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
1108 unsigned int segno
, int modified
)
1110 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
1113 __mark_sit_entry_dirty(sbi
, segno
);
1116 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
1118 struct seg_entry
*se
;
1119 unsigned int segno
, offset
;
1120 long int new_vblocks
;
1122 segno
= GET_SEGNO(sbi
, blkaddr
);
1124 se
= get_seg_entry(sbi
, segno
);
1125 new_vblocks
= se
->valid_blocks
+ del
;
1126 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
1128 f2fs_bug_on(sbi
, (new_vblocks
>> (sizeof(unsigned short) << 3) ||
1129 (new_vblocks
> sbi
->blocks_per_seg
)));
1131 se
->valid_blocks
= new_vblocks
;
1132 se
->mtime
= get_mtime(sbi
);
1133 SIT_I(sbi
)->max_mtime
= se
->mtime
;
1135 /* Update valid block bitmap */
1137 if (f2fs_test_and_set_bit(offset
, se
->cur_valid_map
)) {
1138 #ifdef CONFIG_F2FS_CHECK_FS
1139 if (f2fs_test_and_set_bit(offset
,
1140 se
->cur_valid_map_mir
))
1141 f2fs_bug_on(sbi
, 1);
1145 f2fs_bug_on(sbi
, 1);
1148 if (f2fs_discard_en(sbi
) &&
1149 !f2fs_test_and_set_bit(offset
, se
->discard_map
))
1150 sbi
->discard_blks
--;
1152 if (!f2fs_test_and_clear_bit(offset
, se
->cur_valid_map
)) {
1153 #ifdef CONFIG_F2FS_CHECK_FS
1154 if (!f2fs_test_and_clear_bit(offset
,
1155 se
->cur_valid_map_mir
))
1156 f2fs_bug_on(sbi
, 1);
1160 f2fs_bug_on(sbi
, 1);
1163 if (f2fs_discard_en(sbi
) &&
1164 f2fs_test_and_clear_bit(offset
, se
->discard_map
))
1165 sbi
->discard_blks
++;
1167 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
1168 se
->ckpt_valid_blocks
+= del
;
1170 __mark_sit_entry_dirty(sbi
, segno
);
1172 /* update total number of valid blocks to be written in ckpt area */
1173 SIT_I(sbi
)->written_valid_blocks
+= del
;
1175 if (sbi
->segs_per_sec
> 1)
1176 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
1179 void refresh_sit_entry(struct f2fs_sb_info
*sbi
, block_t old
, block_t
new)
1181 update_sit_entry(sbi
, new, 1);
1182 if (GET_SEGNO(sbi
, old
) != NULL_SEGNO
)
1183 update_sit_entry(sbi
, old
, -1);
1185 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old
));
1186 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, new));
1189 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
1191 unsigned int segno
= GET_SEGNO(sbi
, addr
);
1192 struct sit_info
*sit_i
= SIT_I(sbi
);
1194 f2fs_bug_on(sbi
, addr
== NULL_ADDR
);
1195 if (addr
== NEW_ADDR
)
1198 /* add it into sit main buffer */
1199 mutex_lock(&sit_i
->sentry_lock
);
1201 update_sit_entry(sbi
, addr
, -1);
1203 /* add it into dirty seglist */
1204 locate_dirty_segment(sbi
, segno
);
1206 mutex_unlock(&sit_i
->sentry_lock
);
1209 bool is_checkpointed_data(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1211 struct sit_info
*sit_i
= SIT_I(sbi
);
1212 unsigned int segno
, offset
;
1213 struct seg_entry
*se
;
1216 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
)
1219 mutex_lock(&sit_i
->sentry_lock
);
1221 segno
= GET_SEGNO(sbi
, blkaddr
);
1222 se
= get_seg_entry(sbi
, segno
);
1223 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
1225 if (f2fs_test_bit(offset
, se
->ckpt_valid_map
))
1228 mutex_unlock(&sit_i
->sentry_lock
);
1234 * This function should be resided under the curseg_mutex lock
1236 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
1237 struct f2fs_summary
*sum
)
1239 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1240 void *addr
= curseg
->sum_blk
;
1241 addr
+= curseg
->next_blkoff
* sizeof(struct f2fs_summary
);
1242 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
1246 * Calculate the number of current summary pages for writing
1248 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
, bool for_ra
)
1250 int valid_sum_count
= 0;
1253 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1254 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1255 valid_sum_count
+= sbi
->blocks_per_seg
;
1258 valid_sum_count
+= le16_to_cpu(
1259 F2FS_CKPT(sbi
)->cur_data_blkoff
[i
]);
1261 valid_sum_count
+= curseg_blkoff(sbi
, i
);
1265 sum_in_page
= (PAGE_SIZE
- 2 * SUM_JOURNAL_SIZE
-
1266 SUM_FOOTER_SIZE
) / SUMMARY_SIZE
;
1267 if (valid_sum_count
<= sum_in_page
)
1269 else if ((valid_sum_count
- sum_in_page
) <=
1270 (PAGE_SIZE
- SUM_FOOTER_SIZE
) / SUMMARY_SIZE
)
1276 * Caller should put this summary page
1278 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
1280 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
1283 void update_meta_page(struct f2fs_sb_info
*sbi
, void *src
, block_t blk_addr
)
1285 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
1286 void *dst
= page_address(page
);
1289 memcpy(dst
, src
, PAGE_SIZE
);
1291 memset(dst
, 0, PAGE_SIZE
);
1292 set_page_dirty(page
);
1293 f2fs_put_page(page
, 1);
1296 static void write_sum_page(struct f2fs_sb_info
*sbi
,
1297 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
1299 update_meta_page(sbi
, (void *)sum_blk
, blk_addr
);
1302 static void write_current_sum_page(struct f2fs_sb_info
*sbi
,
1303 int type
, block_t blk_addr
)
1305 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1306 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
1307 struct f2fs_summary_block
*src
= curseg
->sum_blk
;
1308 struct f2fs_summary_block
*dst
;
1310 dst
= (struct f2fs_summary_block
*)page_address(page
);
1312 mutex_lock(&curseg
->curseg_mutex
);
1314 down_read(&curseg
->journal_rwsem
);
1315 memcpy(&dst
->journal
, curseg
->journal
, SUM_JOURNAL_SIZE
);
1316 up_read(&curseg
->journal_rwsem
);
1318 memcpy(dst
->entries
, src
->entries
, SUM_ENTRY_SIZE
);
1319 memcpy(&dst
->footer
, &src
->footer
, SUM_FOOTER_SIZE
);
1321 mutex_unlock(&curseg
->curseg_mutex
);
1323 set_page_dirty(page
);
1324 f2fs_put_page(page
, 1);
1328 * Find a new segment from the free segments bitmap to right order
1329 * This function should be returned with success, otherwise BUG
1331 static void get_new_segment(struct f2fs_sb_info
*sbi
,
1332 unsigned int *newseg
, bool new_sec
, int dir
)
1334 struct free_segmap_info
*free_i
= FREE_I(sbi
);
1335 unsigned int segno
, secno
, zoneno
;
1336 unsigned int total_zones
= MAIN_SECS(sbi
) / sbi
->secs_per_zone
;
1337 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
1338 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
1339 unsigned int left_start
= hint
;
1344 spin_lock(&free_i
->segmap_lock
);
1346 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
1347 segno
= find_next_zero_bit(free_i
->free_segmap
,
1348 (hint
+ 1) * sbi
->segs_per_sec
, *newseg
+ 1);
1349 if (segno
< (hint
+ 1) * sbi
->segs_per_sec
)
1353 secno
= find_next_zero_bit(free_i
->free_secmap
, MAIN_SECS(sbi
), hint
);
1354 if (secno
>= MAIN_SECS(sbi
)) {
1355 if (dir
== ALLOC_RIGHT
) {
1356 secno
= find_next_zero_bit(free_i
->free_secmap
,
1358 f2fs_bug_on(sbi
, secno
>= MAIN_SECS(sbi
));
1361 left_start
= hint
- 1;
1367 while (test_bit(left_start
, free_i
->free_secmap
)) {
1368 if (left_start
> 0) {
1372 left_start
= find_next_zero_bit(free_i
->free_secmap
,
1374 f2fs_bug_on(sbi
, left_start
>= MAIN_SECS(sbi
));
1380 segno
= secno
* sbi
->segs_per_sec
;
1381 zoneno
= secno
/ sbi
->secs_per_zone
;
1383 /* give up on finding another zone */
1386 if (sbi
->secs_per_zone
== 1)
1388 if (zoneno
== old_zoneno
)
1390 if (dir
== ALLOC_LEFT
) {
1391 if (!go_left
&& zoneno
+ 1 >= total_zones
)
1393 if (go_left
&& zoneno
== 0)
1396 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
1397 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
1400 if (i
< NR_CURSEG_TYPE
) {
1401 /* zone is in user, try another */
1403 hint
= zoneno
* sbi
->secs_per_zone
- 1;
1404 else if (zoneno
+ 1 >= total_zones
)
1407 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
1409 goto find_other_zone
;
1412 /* set it as dirty segment in free segmap */
1413 f2fs_bug_on(sbi
, test_bit(segno
, free_i
->free_segmap
));
1414 __set_inuse(sbi
, segno
);
1416 spin_unlock(&free_i
->segmap_lock
);
1419 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
1421 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1422 struct summary_footer
*sum_footer
;
1424 curseg
->segno
= curseg
->next_segno
;
1425 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
1426 curseg
->next_blkoff
= 0;
1427 curseg
->next_segno
= NULL_SEGNO
;
1429 sum_footer
= &(curseg
->sum_blk
->footer
);
1430 memset(sum_footer
, 0, sizeof(struct summary_footer
));
1431 if (IS_DATASEG(type
))
1432 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
1433 if (IS_NODESEG(type
))
1434 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
1435 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
1439 * Allocate a current working segment.
1440 * This function always allocates a free segment in LFS manner.
1442 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
1444 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1445 unsigned int segno
= curseg
->segno
;
1446 int dir
= ALLOC_LEFT
;
1448 write_sum_page(sbi
, curseg
->sum_blk
,
1449 GET_SUM_BLOCK(sbi
, segno
));
1450 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
1453 if (test_opt(sbi
, NOHEAP
))
1456 get_new_segment(sbi
, &segno
, new_sec
, dir
);
1457 curseg
->next_segno
= segno
;
1458 reset_curseg(sbi
, type
, 1);
1459 curseg
->alloc_type
= LFS
;
1462 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
1463 struct curseg_info
*seg
, block_t start
)
1465 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
1466 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
1467 unsigned long *target_map
= SIT_I(sbi
)->tmp_map
;
1468 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
1469 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
1472 for (i
= 0; i
< entries
; i
++)
1473 target_map
[i
] = ckpt_map
[i
] | cur_map
[i
];
1475 pos
= __find_rev_next_zero_bit(target_map
, sbi
->blocks_per_seg
, start
);
1477 seg
->next_blkoff
= pos
;
1481 * If a segment is written by LFS manner, next block offset is just obtained
1482 * by increasing the current block offset. However, if a segment is written by
1483 * SSR manner, next block offset obtained by calling __next_free_blkoff
1485 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
1486 struct curseg_info
*seg
)
1488 if (seg
->alloc_type
== SSR
)
1489 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
1495 * This function always allocates a used segment(from dirty seglist) by SSR
1496 * manner, so it should recover the existing segment information of valid blocks
1498 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
1500 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1501 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1502 unsigned int new_segno
= curseg
->next_segno
;
1503 struct f2fs_summary_block
*sum_node
;
1504 struct page
*sum_page
;
1506 write_sum_page(sbi
, curseg
->sum_blk
,
1507 GET_SUM_BLOCK(sbi
, curseg
->segno
));
1508 __set_test_and_inuse(sbi
, new_segno
);
1510 mutex_lock(&dirty_i
->seglist_lock
);
1511 __remove_dirty_segment(sbi
, new_segno
, PRE
);
1512 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
1513 mutex_unlock(&dirty_i
->seglist_lock
);
1515 reset_curseg(sbi
, type
, 1);
1516 curseg
->alloc_type
= SSR
;
1517 __next_free_blkoff(sbi
, curseg
, 0);
1520 sum_page
= get_sum_page(sbi
, new_segno
);
1521 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
1522 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
1523 f2fs_put_page(sum_page
, 1);
1527 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
1529 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1530 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
1533 /* need_SSR() already forces to do this */
1534 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
, BG_GC
, type
, SSR
))
1537 if (IS_NODESEG(type
))
1540 /* For data segments, let's do SSR more intensively */
1541 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1544 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
1552 * flush out current segment and replace it with new segment
1553 * This function should be returned with success, otherwise BUG
1555 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
1556 int type
, bool force
)
1559 new_curseg(sbi
, type
, true);
1560 else if (!is_set_ckpt_flags(sbi
, CP_CRC_RECOVERY_FLAG
) &&
1561 type
== CURSEG_WARM_NODE
)
1562 new_curseg(sbi
, type
, false);
1563 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
1564 change_curseg(sbi
, type
, true);
1566 new_curseg(sbi
, type
, false);
1568 stat_inc_seg_type(sbi
, CURSEG_I(sbi
, type
));
1571 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
1573 struct curseg_info
*curseg
;
1574 unsigned int old_segno
;
1577 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1578 curseg
= CURSEG_I(sbi
, i
);
1579 old_segno
= curseg
->segno
;
1580 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, i
, true);
1581 locate_dirty_segment(sbi
, old_segno
);
1585 static const struct segment_allocation default_salloc_ops
= {
1586 .allocate_segment
= allocate_segment_by_default
,
1589 bool exist_trim_candidates(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
1591 __u64 trim_start
= cpc
->trim_start
;
1592 bool has_candidate
= false;
1594 mutex_lock(&SIT_I(sbi
)->sentry_lock
);
1595 for (; cpc
->trim_start
<= cpc
->trim_end
; cpc
->trim_start
++) {
1596 if (add_discard_addrs(sbi
, cpc
, true)) {
1597 has_candidate
= true;
1601 mutex_unlock(&SIT_I(sbi
)->sentry_lock
);
1603 cpc
->trim_start
= trim_start
;
1604 return has_candidate
;
1607 int f2fs_trim_fs(struct f2fs_sb_info
*sbi
, struct fstrim_range
*range
)
1609 __u64 start
= F2FS_BYTES_TO_BLK(range
->start
);
1610 __u64 end
= start
+ F2FS_BYTES_TO_BLK(range
->len
) - 1;
1611 unsigned int start_segno
, end_segno
;
1612 struct cp_control cpc
;
1615 if (start
>= MAX_BLKADDR(sbi
) || range
->len
< sbi
->blocksize
)
1619 if (end
<= MAIN_BLKADDR(sbi
))
1622 if (is_sbi_flag_set(sbi
, SBI_NEED_FSCK
)) {
1623 f2fs_msg(sbi
->sb
, KERN_WARNING
,
1624 "Found FS corruption, run fsck to fix.");
1628 /* start/end segment number in main_area */
1629 start_segno
= (start
<= MAIN_BLKADDR(sbi
)) ? 0 : GET_SEGNO(sbi
, start
);
1630 end_segno
= (end
>= MAX_BLKADDR(sbi
)) ? MAIN_SEGS(sbi
) - 1 :
1631 GET_SEGNO(sbi
, end
);
1632 cpc
.reason
= CP_DISCARD
;
1633 cpc
.trim_minlen
= max_t(__u64
, 1, F2FS_BYTES_TO_BLK(range
->minlen
));
1635 /* do checkpoint to issue discard commands safely */
1636 for (; start_segno
<= end_segno
; start_segno
= cpc
.trim_end
+ 1) {
1637 cpc
.trim_start
= start_segno
;
1639 if (sbi
->discard_blks
== 0)
1641 else if (sbi
->discard_blks
< BATCHED_TRIM_BLOCKS(sbi
))
1642 cpc
.trim_end
= end_segno
;
1644 cpc
.trim_end
= min_t(unsigned int,
1645 rounddown(start_segno
+
1646 BATCHED_TRIM_SEGMENTS(sbi
),
1647 sbi
->segs_per_sec
) - 1, end_segno
);
1649 mutex_lock(&sbi
->gc_mutex
);
1650 err
= write_checkpoint(sbi
, &cpc
);
1651 mutex_unlock(&sbi
->gc_mutex
);
1658 range
->len
= F2FS_BLK_TO_BYTES(cpc
.trimmed
);
1662 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
1664 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1665 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
1670 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
1673 return CURSEG_HOT_DATA
;
1675 return CURSEG_HOT_NODE
;
1678 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
1680 if (p_type
== DATA
) {
1681 struct inode
*inode
= page
->mapping
->host
;
1683 if (S_ISDIR(inode
->i_mode
))
1684 return CURSEG_HOT_DATA
;
1686 return CURSEG_COLD_DATA
;
1688 if (IS_DNODE(page
) && is_cold_node(page
))
1689 return CURSEG_WARM_NODE
;
1691 return CURSEG_COLD_NODE
;
1695 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
1697 if (p_type
== DATA
) {
1698 struct inode
*inode
= page
->mapping
->host
;
1700 if (S_ISDIR(inode
->i_mode
))
1701 return CURSEG_HOT_DATA
;
1702 else if (is_cold_data(page
) || file_is_cold(inode
))
1703 return CURSEG_COLD_DATA
;
1705 return CURSEG_WARM_DATA
;
1708 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
1711 return CURSEG_COLD_NODE
;
1715 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
1717 switch (F2FS_P_SB(page
)->active_logs
) {
1719 return __get_segment_type_2(page
, p_type
);
1721 return __get_segment_type_4(page
, p_type
);
1723 /* NR_CURSEG_TYPE(6) logs by default */
1724 f2fs_bug_on(F2FS_P_SB(page
),
1725 F2FS_P_SB(page
)->active_logs
!= NR_CURSEG_TYPE
);
1726 return __get_segment_type_6(page
, p_type
);
1729 void allocate_data_block(struct f2fs_sb_info
*sbi
, struct page
*page
,
1730 block_t old_blkaddr
, block_t
*new_blkaddr
,
1731 struct f2fs_summary
*sum
, int type
)
1733 struct sit_info
*sit_i
= SIT_I(sbi
);
1734 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1736 mutex_lock(&curseg
->curseg_mutex
);
1737 mutex_lock(&sit_i
->sentry_lock
);
1739 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
1741 f2fs_wait_discard_bio(sbi
, *new_blkaddr
);
1744 * __add_sum_entry should be resided under the curseg_mutex
1745 * because, this function updates a summary entry in the
1746 * current summary block.
1748 __add_sum_entry(sbi
, type
, sum
);
1750 __refresh_next_blkoff(sbi
, curseg
);
1752 stat_inc_block_count(sbi
, curseg
);
1755 * SIT information should be updated before segment allocation,
1756 * since SSR needs latest valid block information.
1758 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
1760 if (!__has_curseg_space(sbi
, type
))
1761 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
1763 mutex_unlock(&sit_i
->sentry_lock
);
1765 if (page
&& IS_NODESEG(type
))
1766 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
1768 mutex_unlock(&curseg
->curseg_mutex
);
1771 static void do_write_page(struct f2fs_summary
*sum
, struct f2fs_io_info
*fio
)
1773 int type
= __get_segment_type(fio
->page
, fio
->type
);
1776 if (fio
->type
== NODE
|| fio
->type
== DATA
)
1777 mutex_lock(&fio
->sbi
->wio_mutex
[fio
->type
]);
1779 allocate_data_block(fio
->sbi
, fio
->page
, fio
->old_blkaddr
,
1780 &fio
->new_blkaddr
, sum
, type
);
1782 /* writeout dirty page into bdev */
1783 err
= f2fs_submit_page_mbio(fio
);
1784 if (err
== -EAGAIN
) {
1785 fio
->old_blkaddr
= fio
->new_blkaddr
;
1789 if (fio
->type
== NODE
|| fio
->type
== DATA
)
1790 mutex_unlock(&fio
->sbi
->wio_mutex
[fio
->type
]);
1793 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1795 struct f2fs_io_info fio
= {
1799 .op_flags
= REQ_SYNC
| REQ_META
| REQ_PRIO
,
1800 .old_blkaddr
= page
->index
,
1801 .new_blkaddr
= page
->index
,
1803 .encrypted_page
= NULL
,
1806 if (unlikely(page
->index
>= MAIN_BLKADDR(sbi
)))
1807 fio
.op_flags
&= ~REQ_META
;
1809 set_page_writeback(page
);
1810 f2fs_submit_page_mbio(&fio
);
1813 void write_node_page(unsigned int nid
, struct f2fs_io_info
*fio
)
1815 struct f2fs_summary sum
;
1817 set_summary(&sum
, nid
, 0, 0);
1818 do_write_page(&sum
, fio
);
1821 void write_data_page(struct dnode_of_data
*dn
, struct f2fs_io_info
*fio
)
1823 struct f2fs_sb_info
*sbi
= fio
->sbi
;
1824 struct f2fs_summary sum
;
1825 struct node_info ni
;
1827 f2fs_bug_on(sbi
, dn
->data_blkaddr
== NULL_ADDR
);
1828 get_node_info(sbi
, dn
->nid
, &ni
);
1829 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
1830 do_write_page(&sum
, fio
);
1831 f2fs_update_data_blkaddr(dn
, fio
->new_blkaddr
);
1834 void rewrite_data_page(struct f2fs_io_info
*fio
)
1836 fio
->new_blkaddr
= fio
->old_blkaddr
;
1837 stat_inc_inplace_blocks(fio
->sbi
);
1838 f2fs_submit_page_mbio(fio
);
1841 void __f2fs_replace_block(struct f2fs_sb_info
*sbi
, struct f2fs_summary
*sum
,
1842 block_t old_blkaddr
, block_t new_blkaddr
,
1843 bool recover_curseg
, bool recover_newaddr
)
1845 struct sit_info
*sit_i
= SIT_I(sbi
);
1846 struct curseg_info
*curseg
;
1847 unsigned int segno
, old_cursegno
;
1848 struct seg_entry
*se
;
1850 unsigned short old_blkoff
;
1852 segno
= GET_SEGNO(sbi
, new_blkaddr
);
1853 se
= get_seg_entry(sbi
, segno
);
1856 if (!recover_curseg
) {
1857 /* for recovery flow */
1858 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
1859 if (old_blkaddr
== NULL_ADDR
)
1860 type
= CURSEG_COLD_DATA
;
1862 type
= CURSEG_WARM_DATA
;
1865 if (!IS_CURSEG(sbi
, segno
))
1866 type
= CURSEG_WARM_DATA
;
1869 curseg
= CURSEG_I(sbi
, type
);
1871 mutex_lock(&curseg
->curseg_mutex
);
1872 mutex_lock(&sit_i
->sentry_lock
);
1874 old_cursegno
= curseg
->segno
;
1875 old_blkoff
= curseg
->next_blkoff
;
1877 /* change the current segment */
1878 if (segno
!= curseg
->segno
) {
1879 curseg
->next_segno
= segno
;
1880 change_curseg(sbi
, type
, true);
1883 curseg
->next_blkoff
= GET_BLKOFF_FROM_SEG0(sbi
, new_blkaddr
);
1884 __add_sum_entry(sbi
, type
, sum
);
1886 if (!recover_curseg
|| recover_newaddr
)
1887 update_sit_entry(sbi
, new_blkaddr
, 1);
1888 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
1889 update_sit_entry(sbi
, old_blkaddr
, -1);
1891 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
1892 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, new_blkaddr
));
1894 locate_dirty_segment(sbi
, old_cursegno
);
1896 if (recover_curseg
) {
1897 if (old_cursegno
!= curseg
->segno
) {
1898 curseg
->next_segno
= old_cursegno
;
1899 change_curseg(sbi
, type
, true);
1901 curseg
->next_blkoff
= old_blkoff
;
1904 mutex_unlock(&sit_i
->sentry_lock
);
1905 mutex_unlock(&curseg
->curseg_mutex
);
1908 void f2fs_replace_block(struct f2fs_sb_info
*sbi
, struct dnode_of_data
*dn
,
1909 block_t old_addr
, block_t new_addr
,
1910 unsigned char version
, bool recover_curseg
,
1911 bool recover_newaddr
)
1913 struct f2fs_summary sum
;
1915 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, version
);
1917 __f2fs_replace_block(sbi
, &sum
, old_addr
, new_addr
,
1918 recover_curseg
, recover_newaddr
);
1920 f2fs_update_data_blkaddr(dn
, new_addr
);
1923 void f2fs_wait_on_page_writeback(struct page
*page
,
1924 enum page_type type
, bool ordered
)
1926 if (PageWriteback(page
)) {
1927 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1929 f2fs_submit_merged_bio_cond(sbi
, page
->mapping
->host
,
1930 0, page
->index
, type
, WRITE
);
1932 wait_on_page_writeback(page
);
1934 wait_for_stable_page(page
);
1938 void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info
*sbi
,
1943 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
)
1946 cpage
= find_lock_page(META_MAPPING(sbi
), blkaddr
);
1948 f2fs_wait_on_page_writeback(cpage
, DATA
, true);
1949 f2fs_put_page(cpage
, 1);
1953 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
1955 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1956 struct curseg_info
*seg_i
;
1957 unsigned char *kaddr
;
1962 start
= start_sum_block(sbi
);
1964 page
= get_meta_page(sbi
, start
++);
1965 kaddr
= (unsigned char *)page_address(page
);
1967 /* Step 1: restore nat cache */
1968 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1969 memcpy(seg_i
->journal
, kaddr
, SUM_JOURNAL_SIZE
);
1971 /* Step 2: restore sit cache */
1972 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1973 memcpy(seg_i
->journal
, kaddr
+ SUM_JOURNAL_SIZE
, SUM_JOURNAL_SIZE
);
1974 offset
= 2 * SUM_JOURNAL_SIZE
;
1976 /* Step 3: restore summary entries */
1977 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1978 unsigned short blk_off
;
1981 seg_i
= CURSEG_I(sbi
, i
);
1982 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
1983 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
1984 seg_i
->next_segno
= segno
;
1985 reset_curseg(sbi
, i
, 0);
1986 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1987 seg_i
->next_blkoff
= blk_off
;
1989 if (seg_i
->alloc_type
== SSR
)
1990 blk_off
= sbi
->blocks_per_seg
;
1992 for (j
= 0; j
< blk_off
; j
++) {
1993 struct f2fs_summary
*s
;
1994 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1995 seg_i
->sum_blk
->entries
[j
] = *s
;
1996 offset
+= SUMMARY_SIZE
;
1997 if (offset
+ SUMMARY_SIZE
<= PAGE_SIZE
-
2001 f2fs_put_page(page
, 1);
2004 page
= get_meta_page(sbi
, start
++);
2005 kaddr
= (unsigned char *)page_address(page
);
2009 f2fs_put_page(page
, 1);
2013 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
2015 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
2016 struct f2fs_summary_block
*sum
;
2017 struct curseg_info
*curseg
;
2019 unsigned short blk_off
;
2020 unsigned int segno
= 0;
2021 block_t blk_addr
= 0;
2023 /* get segment number and block addr */
2024 if (IS_DATASEG(type
)) {
2025 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
2026 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
2028 if (__exist_node_summaries(sbi
))
2029 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
2031 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
2033 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
2035 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
2037 if (__exist_node_summaries(sbi
))
2038 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
2039 type
- CURSEG_HOT_NODE
);
2041 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
2044 new = get_meta_page(sbi
, blk_addr
);
2045 sum
= (struct f2fs_summary_block
*)page_address(new);
2047 if (IS_NODESEG(type
)) {
2048 if (__exist_node_summaries(sbi
)) {
2049 struct f2fs_summary
*ns
= &sum
->entries
[0];
2051 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
2053 ns
->ofs_in_node
= 0;
2058 err
= restore_node_summary(sbi
, segno
, sum
);
2060 f2fs_put_page(new, 1);
2066 /* set uncompleted segment to curseg */
2067 curseg
= CURSEG_I(sbi
, type
);
2068 mutex_lock(&curseg
->curseg_mutex
);
2070 /* update journal info */
2071 down_write(&curseg
->journal_rwsem
);
2072 memcpy(curseg
->journal
, &sum
->journal
, SUM_JOURNAL_SIZE
);
2073 up_write(&curseg
->journal_rwsem
);
2075 memcpy(curseg
->sum_blk
->entries
, sum
->entries
, SUM_ENTRY_SIZE
);
2076 memcpy(&curseg
->sum_blk
->footer
, &sum
->footer
, SUM_FOOTER_SIZE
);
2077 curseg
->next_segno
= segno
;
2078 reset_curseg(sbi
, type
, 0);
2079 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
2080 curseg
->next_blkoff
= blk_off
;
2081 mutex_unlock(&curseg
->curseg_mutex
);
2082 f2fs_put_page(new, 1);
2086 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
2088 int type
= CURSEG_HOT_DATA
;
2091 if (is_set_ckpt_flags(sbi
, CP_COMPACT_SUM_FLAG
)) {
2092 int npages
= npages_for_summary_flush(sbi
, true);
2095 ra_meta_pages(sbi
, start_sum_block(sbi
), npages
,
2098 /* restore for compacted data summary */
2099 if (read_compacted_summaries(sbi
))
2101 type
= CURSEG_HOT_NODE
;
2104 if (__exist_node_summaries(sbi
))
2105 ra_meta_pages(sbi
, sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
),
2106 NR_CURSEG_TYPE
- type
, META_CP
, true);
2108 for (; type
<= CURSEG_COLD_NODE
; type
++) {
2109 err
= read_normal_summaries(sbi
, type
);
2117 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
2120 unsigned char *kaddr
;
2121 struct f2fs_summary
*summary
;
2122 struct curseg_info
*seg_i
;
2123 int written_size
= 0;
2126 page
= grab_meta_page(sbi
, blkaddr
++);
2127 kaddr
= (unsigned char *)page_address(page
);
2129 /* Step 1: write nat cache */
2130 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
2131 memcpy(kaddr
, seg_i
->journal
, SUM_JOURNAL_SIZE
);
2132 written_size
+= SUM_JOURNAL_SIZE
;
2134 /* Step 2: write sit cache */
2135 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2136 memcpy(kaddr
+ written_size
, seg_i
->journal
, SUM_JOURNAL_SIZE
);
2137 written_size
+= SUM_JOURNAL_SIZE
;
2139 /* Step 3: write summary entries */
2140 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
2141 unsigned short blkoff
;
2142 seg_i
= CURSEG_I(sbi
, i
);
2143 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
2144 blkoff
= sbi
->blocks_per_seg
;
2146 blkoff
= curseg_blkoff(sbi
, i
);
2148 for (j
= 0; j
< blkoff
; j
++) {
2150 page
= grab_meta_page(sbi
, blkaddr
++);
2151 kaddr
= (unsigned char *)page_address(page
);
2154 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
2155 *summary
= seg_i
->sum_blk
->entries
[j
];
2156 written_size
+= SUMMARY_SIZE
;
2158 if (written_size
+ SUMMARY_SIZE
<= PAGE_SIZE
-
2162 set_page_dirty(page
);
2163 f2fs_put_page(page
, 1);
2168 set_page_dirty(page
);
2169 f2fs_put_page(page
, 1);
2173 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
2174 block_t blkaddr
, int type
)
2177 if (IS_DATASEG(type
))
2178 end
= type
+ NR_CURSEG_DATA_TYPE
;
2180 end
= type
+ NR_CURSEG_NODE_TYPE
;
2182 for (i
= type
; i
< end
; i
++)
2183 write_current_sum_page(sbi
, i
, blkaddr
+ (i
- type
));
2186 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
2188 if (is_set_ckpt_flags(sbi
, CP_COMPACT_SUM_FLAG
))
2189 write_compacted_summaries(sbi
, start_blk
);
2191 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
2194 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
2196 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
2199 int lookup_journal_in_cursum(struct f2fs_journal
*journal
, int type
,
2200 unsigned int val
, int alloc
)
2204 if (type
== NAT_JOURNAL
) {
2205 for (i
= 0; i
< nats_in_cursum(journal
); i
++) {
2206 if (le32_to_cpu(nid_in_journal(journal
, i
)) == val
)
2209 if (alloc
&& __has_cursum_space(journal
, 1, NAT_JOURNAL
))
2210 return update_nats_in_cursum(journal
, 1);
2211 } else if (type
== SIT_JOURNAL
) {
2212 for (i
= 0; i
< sits_in_cursum(journal
); i
++)
2213 if (le32_to_cpu(segno_in_journal(journal
, i
)) == val
)
2215 if (alloc
&& __has_cursum_space(journal
, 1, SIT_JOURNAL
))
2216 return update_sits_in_cursum(journal
, 1);
2221 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
2224 return get_meta_page(sbi
, current_sit_addr(sbi
, segno
));
2227 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
2230 struct sit_info
*sit_i
= SIT_I(sbi
);
2231 struct page
*src_page
, *dst_page
;
2232 pgoff_t src_off
, dst_off
;
2233 void *src_addr
, *dst_addr
;
2235 src_off
= current_sit_addr(sbi
, start
);
2236 dst_off
= next_sit_addr(sbi
, src_off
);
2238 /* get current sit block page without lock */
2239 src_page
= get_meta_page(sbi
, src_off
);
2240 dst_page
= grab_meta_page(sbi
, dst_off
);
2241 f2fs_bug_on(sbi
, PageDirty(src_page
));
2243 src_addr
= page_address(src_page
);
2244 dst_addr
= page_address(dst_page
);
2245 memcpy(dst_addr
, src_addr
, PAGE_SIZE
);
2247 set_page_dirty(dst_page
);
2248 f2fs_put_page(src_page
, 1);
2250 set_to_next_sit(sit_i
, start
);
2255 static struct sit_entry_set
*grab_sit_entry_set(void)
2257 struct sit_entry_set
*ses
=
2258 f2fs_kmem_cache_alloc(sit_entry_set_slab
, GFP_NOFS
);
2261 INIT_LIST_HEAD(&ses
->set_list
);
2265 static void release_sit_entry_set(struct sit_entry_set
*ses
)
2267 list_del(&ses
->set_list
);
2268 kmem_cache_free(sit_entry_set_slab
, ses
);
2271 static void adjust_sit_entry_set(struct sit_entry_set
*ses
,
2272 struct list_head
*head
)
2274 struct sit_entry_set
*next
= ses
;
2276 if (list_is_last(&ses
->set_list
, head
))
2279 list_for_each_entry_continue(next
, head
, set_list
)
2280 if (ses
->entry_cnt
<= next
->entry_cnt
)
2283 list_move_tail(&ses
->set_list
, &next
->set_list
);
2286 static void add_sit_entry(unsigned int segno
, struct list_head
*head
)
2288 struct sit_entry_set
*ses
;
2289 unsigned int start_segno
= START_SEGNO(segno
);
2291 list_for_each_entry(ses
, head
, set_list
) {
2292 if (ses
->start_segno
== start_segno
) {
2294 adjust_sit_entry_set(ses
, head
);
2299 ses
= grab_sit_entry_set();
2301 ses
->start_segno
= start_segno
;
2303 list_add(&ses
->set_list
, head
);
2306 static void add_sits_in_set(struct f2fs_sb_info
*sbi
)
2308 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
2309 struct list_head
*set_list
= &sm_info
->sit_entry_set
;
2310 unsigned long *bitmap
= SIT_I(sbi
)->dirty_sentries_bitmap
;
2313 for_each_set_bit(segno
, bitmap
, MAIN_SEGS(sbi
))
2314 add_sit_entry(segno
, set_list
);
2317 static void remove_sits_in_journal(struct f2fs_sb_info
*sbi
)
2319 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2320 struct f2fs_journal
*journal
= curseg
->journal
;
2323 down_write(&curseg
->journal_rwsem
);
2324 for (i
= 0; i
< sits_in_cursum(journal
); i
++) {
2328 segno
= le32_to_cpu(segno_in_journal(journal
, i
));
2329 dirtied
= __mark_sit_entry_dirty(sbi
, segno
);
2332 add_sit_entry(segno
, &SM_I(sbi
)->sit_entry_set
);
2334 update_sits_in_cursum(journal
, -i
);
2335 up_write(&curseg
->journal_rwsem
);
2339 * CP calls this function, which flushes SIT entries including sit_journal,
2340 * and moves prefree segs to free segs.
2342 void flush_sit_entries(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
2344 struct sit_info
*sit_i
= SIT_I(sbi
);
2345 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
2346 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2347 struct f2fs_journal
*journal
= curseg
->journal
;
2348 struct sit_entry_set
*ses
, *tmp
;
2349 struct list_head
*head
= &SM_I(sbi
)->sit_entry_set
;
2350 bool to_journal
= true;
2351 struct seg_entry
*se
;
2353 mutex_lock(&sit_i
->sentry_lock
);
2355 if (!sit_i
->dirty_sentries
)
2359 * add and account sit entries of dirty bitmap in sit entry
2362 add_sits_in_set(sbi
);
2365 * if there are no enough space in journal to store dirty sit
2366 * entries, remove all entries from journal and add and account
2367 * them in sit entry set.
2369 if (!__has_cursum_space(journal
, sit_i
->dirty_sentries
, SIT_JOURNAL
))
2370 remove_sits_in_journal(sbi
);
2373 * there are two steps to flush sit entries:
2374 * #1, flush sit entries to journal in current cold data summary block.
2375 * #2, flush sit entries to sit page.
2377 list_for_each_entry_safe(ses
, tmp
, head
, set_list
) {
2378 struct page
*page
= NULL
;
2379 struct f2fs_sit_block
*raw_sit
= NULL
;
2380 unsigned int start_segno
= ses
->start_segno
;
2381 unsigned int end
= min(start_segno
+ SIT_ENTRY_PER_BLOCK
,
2382 (unsigned long)MAIN_SEGS(sbi
));
2383 unsigned int segno
= start_segno
;
2386 !__has_cursum_space(journal
, ses
->entry_cnt
, SIT_JOURNAL
))
2390 down_write(&curseg
->journal_rwsem
);
2392 page
= get_next_sit_page(sbi
, start_segno
);
2393 raw_sit
= page_address(page
);
2396 /* flush dirty sit entries in region of current sit set */
2397 for_each_set_bit_from(segno
, bitmap
, end
) {
2398 int offset
, sit_offset
;
2400 se
= get_seg_entry(sbi
, segno
);
2402 /* add discard candidates */
2403 if (cpc
->reason
!= CP_DISCARD
) {
2404 cpc
->trim_start
= segno
;
2405 add_discard_addrs(sbi
, cpc
, false);
2409 offset
= lookup_journal_in_cursum(journal
,
2410 SIT_JOURNAL
, segno
, 1);
2411 f2fs_bug_on(sbi
, offset
< 0);
2412 segno_in_journal(journal
, offset
) =
2414 seg_info_to_raw_sit(se
,
2415 &sit_in_journal(journal
, offset
));
2417 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
2418 seg_info_to_raw_sit(se
,
2419 &raw_sit
->entries
[sit_offset
]);
2422 __clear_bit(segno
, bitmap
);
2423 sit_i
->dirty_sentries
--;
2428 up_write(&curseg
->journal_rwsem
);
2430 f2fs_put_page(page
, 1);
2432 f2fs_bug_on(sbi
, ses
->entry_cnt
);
2433 release_sit_entry_set(ses
);
2436 f2fs_bug_on(sbi
, !list_empty(head
));
2437 f2fs_bug_on(sbi
, sit_i
->dirty_sentries
);
2439 if (cpc
->reason
== CP_DISCARD
) {
2440 __u64 trim_start
= cpc
->trim_start
;
2442 for (; cpc
->trim_start
<= cpc
->trim_end
; cpc
->trim_start
++)
2443 add_discard_addrs(sbi
, cpc
, false);
2445 cpc
->trim_start
= trim_start
;
2447 mutex_unlock(&sit_i
->sentry_lock
);
2449 set_prefree_as_free_segments(sbi
);
2452 static int build_sit_info(struct f2fs_sb_info
*sbi
)
2454 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
2455 struct sit_info
*sit_i
;
2456 unsigned int sit_segs
, start
;
2458 unsigned int bitmap_size
;
2460 /* allocate memory for SIT information */
2461 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
2465 SM_I(sbi
)->sit_info
= sit_i
;
2467 sit_i
->sentries
= f2fs_kvzalloc(MAIN_SEGS(sbi
) *
2468 sizeof(struct seg_entry
), GFP_KERNEL
);
2469 if (!sit_i
->sentries
)
2472 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
2473 sit_i
->dirty_sentries_bitmap
= f2fs_kvzalloc(bitmap_size
, GFP_KERNEL
);
2474 if (!sit_i
->dirty_sentries_bitmap
)
2477 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
2478 sit_i
->sentries
[start
].cur_valid_map
2479 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2480 sit_i
->sentries
[start
].ckpt_valid_map
2481 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2482 if (!sit_i
->sentries
[start
].cur_valid_map
||
2483 !sit_i
->sentries
[start
].ckpt_valid_map
)
2486 #ifdef CONFIG_F2FS_CHECK_FS
2487 sit_i
->sentries
[start
].cur_valid_map_mir
2488 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2489 if (!sit_i
->sentries
[start
].cur_valid_map_mir
)
2493 if (f2fs_discard_en(sbi
)) {
2494 sit_i
->sentries
[start
].discard_map
2495 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2496 if (!sit_i
->sentries
[start
].discard_map
)
2501 sit_i
->tmp_map
= kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2502 if (!sit_i
->tmp_map
)
2505 if (sbi
->segs_per_sec
> 1) {
2506 sit_i
->sec_entries
= f2fs_kvzalloc(MAIN_SECS(sbi
) *
2507 sizeof(struct sec_entry
), GFP_KERNEL
);
2508 if (!sit_i
->sec_entries
)
2512 /* get information related with SIT */
2513 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
2515 /* setup SIT bitmap from ckeckpoint pack */
2516 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
2517 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
2519 sit_i
->sit_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
2520 if (!sit_i
->sit_bitmap
)
2523 #ifdef CONFIG_F2FS_CHECK_FS
2524 sit_i
->sit_bitmap_mir
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
2525 if (!sit_i
->sit_bitmap_mir
)
2529 /* init SIT information */
2530 sit_i
->s_ops
= &default_salloc_ops
;
2532 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
2533 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
2534 sit_i
->written_valid_blocks
= 0;
2535 sit_i
->bitmap_size
= bitmap_size
;
2536 sit_i
->dirty_sentries
= 0;
2537 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
2538 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
2539 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
2540 mutex_init(&sit_i
->sentry_lock
);
2544 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
2546 struct free_segmap_info
*free_i
;
2547 unsigned int bitmap_size
, sec_bitmap_size
;
2549 /* allocate memory for free segmap information */
2550 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
2554 SM_I(sbi
)->free_info
= free_i
;
2556 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
2557 free_i
->free_segmap
= f2fs_kvmalloc(bitmap_size
, GFP_KERNEL
);
2558 if (!free_i
->free_segmap
)
2561 sec_bitmap_size
= f2fs_bitmap_size(MAIN_SECS(sbi
));
2562 free_i
->free_secmap
= f2fs_kvmalloc(sec_bitmap_size
, GFP_KERNEL
);
2563 if (!free_i
->free_secmap
)
2566 /* set all segments as dirty temporarily */
2567 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
2568 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
2570 /* init free segmap information */
2571 free_i
->start_segno
= GET_SEGNO_FROM_SEG0(sbi
, MAIN_BLKADDR(sbi
));
2572 free_i
->free_segments
= 0;
2573 free_i
->free_sections
= 0;
2574 spin_lock_init(&free_i
->segmap_lock
);
2578 static int build_curseg(struct f2fs_sb_info
*sbi
)
2580 struct curseg_info
*array
;
2583 array
= kcalloc(NR_CURSEG_TYPE
, sizeof(*array
), GFP_KERNEL
);
2587 SM_I(sbi
)->curseg_array
= array
;
2589 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
2590 mutex_init(&array
[i
].curseg_mutex
);
2591 array
[i
].sum_blk
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
2592 if (!array
[i
].sum_blk
)
2594 init_rwsem(&array
[i
].journal_rwsem
);
2595 array
[i
].journal
= kzalloc(sizeof(struct f2fs_journal
),
2597 if (!array
[i
].journal
)
2599 array
[i
].segno
= NULL_SEGNO
;
2600 array
[i
].next_blkoff
= 0;
2602 return restore_curseg_summaries(sbi
);
2605 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
2607 struct sit_info
*sit_i
= SIT_I(sbi
);
2608 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2609 struct f2fs_journal
*journal
= curseg
->journal
;
2610 struct seg_entry
*se
;
2611 struct f2fs_sit_entry sit
;
2612 int sit_blk_cnt
= SIT_BLK_CNT(sbi
);
2613 unsigned int i
, start
, end
;
2614 unsigned int readed
, start_blk
= 0;
2617 readed
= ra_meta_pages(sbi
, start_blk
, BIO_MAX_PAGES
,
2620 start
= start_blk
* sit_i
->sents_per_block
;
2621 end
= (start_blk
+ readed
) * sit_i
->sents_per_block
;
2623 for (; start
< end
&& start
< MAIN_SEGS(sbi
); start
++) {
2624 struct f2fs_sit_block
*sit_blk
;
2627 se
= &sit_i
->sentries
[start
];
2628 page
= get_current_sit_page(sbi
, start
);
2629 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
2630 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
2631 f2fs_put_page(page
, 1);
2633 check_block_count(sbi
, start
, &sit
);
2634 seg_info_from_raw_sit(se
, &sit
);
2636 /* build discard map only one time */
2637 if (f2fs_discard_en(sbi
)) {
2638 memcpy(se
->discard_map
, se
->cur_valid_map
,
2639 SIT_VBLOCK_MAP_SIZE
);
2640 sbi
->discard_blks
+= sbi
->blocks_per_seg
-
2644 if (sbi
->segs_per_sec
> 1)
2645 get_sec_entry(sbi
, start
)->valid_blocks
+=
2648 start_blk
+= readed
;
2649 } while (start_blk
< sit_blk_cnt
);
2651 down_read(&curseg
->journal_rwsem
);
2652 for (i
= 0; i
< sits_in_cursum(journal
); i
++) {
2653 unsigned int old_valid_blocks
;
2655 start
= le32_to_cpu(segno_in_journal(journal
, i
));
2656 se
= &sit_i
->sentries
[start
];
2657 sit
= sit_in_journal(journal
, i
);
2659 old_valid_blocks
= se
->valid_blocks
;
2661 check_block_count(sbi
, start
, &sit
);
2662 seg_info_from_raw_sit(se
, &sit
);
2664 if (f2fs_discard_en(sbi
)) {
2665 memcpy(se
->discard_map
, se
->cur_valid_map
,
2666 SIT_VBLOCK_MAP_SIZE
);
2667 sbi
->discard_blks
+= old_valid_blocks
-
2671 if (sbi
->segs_per_sec
> 1)
2672 get_sec_entry(sbi
, start
)->valid_blocks
+=
2673 se
->valid_blocks
- old_valid_blocks
;
2675 up_read(&curseg
->journal_rwsem
);
2678 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
2683 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
2684 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
2685 if (!sentry
->valid_blocks
)
2686 __set_free(sbi
, start
);
2688 SIT_I(sbi
)->written_valid_blocks
+=
2689 sentry
->valid_blocks
;
2692 /* set use the current segments */
2693 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
2694 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
2695 __set_test_and_inuse(sbi
, curseg_t
->segno
);
2699 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
2701 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2702 struct free_segmap_info
*free_i
= FREE_I(sbi
);
2703 unsigned int segno
= 0, offset
= 0;
2704 unsigned short valid_blocks
;
2707 /* find dirty segment based on free segmap */
2708 segno
= find_next_inuse(free_i
, MAIN_SEGS(sbi
), offset
);
2709 if (segno
>= MAIN_SEGS(sbi
))
2712 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
2713 if (valid_blocks
== sbi
->blocks_per_seg
|| !valid_blocks
)
2715 if (valid_blocks
> sbi
->blocks_per_seg
) {
2716 f2fs_bug_on(sbi
, 1);
2719 mutex_lock(&dirty_i
->seglist_lock
);
2720 __locate_dirty_segment(sbi
, segno
, DIRTY
);
2721 mutex_unlock(&dirty_i
->seglist_lock
);
2725 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
2727 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2728 unsigned int bitmap_size
= f2fs_bitmap_size(MAIN_SECS(sbi
));
2730 dirty_i
->victim_secmap
= f2fs_kvzalloc(bitmap_size
, GFP_KERNEL
);
2731 if (!dirty_i
->victim_secmap
)
2736 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
2738 struct dirty_seglist_info
*dirty_i
;
2739 unsigned int bitmap_size
, i
;
2741 /* allocate memory for dirty segments list information */
2742 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
2746 SM_I(sbi
)->dirty_info
= dirty_i
;
2747 mutex_init(&dirty_i
->seglist_lock
);
2749 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
2751 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
2752 dirty_i
->dirty_segmap
[i
] = f2fs_kvzalloc(bitmap_size
, GFP_KERNEL
);
2753 if (!dirty_i
->dirty_segmap
[i
])
2757 init_dirty_segmap(sbi
);
2758 return init_victim_secmap(sbi
);
2762 * Update min, max modified time for cost-benefit GC algorithm
2764 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
2766 struct sit_info
*sit_i
= SIT_I(sbi
);
2769 mutex_lock(&sit_i
->sentry_lock
);
2771 sit_i
->min_mtime
= LLONG_MAX
;
2773 for (segno
= 0; segno
< MAIN_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
2775 unsigned long long mtime
= 0;
2777 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
2778 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
2780 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
2782 if (sit_i
->min_mtime
> mtime
)
2783 sit_i
->min_mtime
= mtime
;
2785 sit_i
->max_mtime
= get_mtime(sbi
);
2786 mutex_unlock(&sit_i
->sentry_lock
);
2789 int build_segment_manager(struct f2fs_sb_info
*sbi
)
2791 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
2792 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
2793 struct f2fs_sm_info
*sm_info
;
2796 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
2801 sbi
->sm_info
= sm_info
;
2802 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
2803 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
2804 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
2805 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
2806 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
2807 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
2808 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
2809 sm_info
->rec_prefree_segments
= sm_info
->main_segments
*
2810 DEF_RECLAIM_PREFREE_SEGMENTS
/ 100;
2811 if (sm_info
->rec_prefree_segments
> DEF_MAX_RECLAIM_PREFREE_SEGMENTS
)
2812 sm_info
->rec_prefree_segments
= DEF_MAX_RECLAIM_PREFREE_SEGMENTS
;
2814 if (!test_opt(sbi
, LFS
))
2815 sm_info
->ipu_policy
= 1 << F2FS_IPU_FSYNC
;
2816 sm_info
->min_ipu_util
= DEF_MIN_IPU_UTIL
;
2817 sm_info
->min_fsync_blocks
= DEF_MIN_FSYNC_BLOCKS
;
2819 sm_info
->trim_sections
= DEF_BATCHED_TRIM_SECTIONS
;
2821 INIT_LIST_HEAD(&sm_info
->sit_entry_set
);
2823 if (test_opt(sbi
, FLUSH_MERGE
) && !f2fs_readonly(sbi
->sb
)) {
2824 err
= create_flush_cmd_control(sbi
);
2829 err
= create_discard_cmd_control(sbi
);
2833 err
= build_sit_info(sbi
);
2836 err
= build_free_segmap(sbi
);
2839 err
= build_curseg(sbi
);
2843 /* reinit free segmap based on SIT */
2844 build_sit_entries(sbi
);
2846 init_free_segmap(sbi
);
2847 err
= build_dirty_segmap(sbi
);
2851 init_min_max_mtime(sbi
);
2855 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
2856 enum dirty_type dirty_type
)
2858 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2860 mutex_lock(&dirty_i
->seglist_lock
);
2861 kvfree(dirty_i
->dirty_segmap
[dirty_type
]);
2862 dirty_i
->nr_dirty
[dirty_type
] = 0;
2863 mutex_unlock(&dirty_i
->seglist_lock
);
2866 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
2868 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2869 kvfree(dirty_i
->victim_secmap
);
2872 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
2874 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2880 /* discard pre-free/dirty segments list */
2881 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
2882 discard_dirty_segmap(sbi
, i
);
2884 destroy_victim_secmap(sbi
);
2885 SM_I(sbi
)->dirty_info
= NULL
;
2889 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
2891 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
2896 SM_I(sbi
)->curseg_array
= NULL
;
2897 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
2898 kfree(array
[i
].sum_blk
);
2899 kfree(array
[i
].journal
);
2904 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
2906 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
2909 SM_I(sbi
)->free_info
= NULL
;
2910 kvfree(free_i
->free_segmap
);
2911 kvfree(free_i
->free_secmap
);
2915 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
2917 struct sit_info
*sit_i
= SIT_I(sbi
);
2923 if (sit_i
->sentries
) {
2924 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
2925 kfree(sit_i
->sentries
[start
].cur_valid_map
);
2926 #ifdef CONFIG_F2FS_CHECK_FS
2927 kfree(sit_i
->sentries
[start
].cur_valid_map_mir
);
2929 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
2930 kfree(sit_i
->sentries
[start
].discard_map
);
2933 kfree(sit_i
->tmp_map
);
2935 kvfree(sit_i
->sentries
);
2936 kvfree(sit_i
->sec_entries
);
2937 kvfree(sit_i
->dirty_sentries_bitmap
);
2939 SM_I(sbi
)->sit_info
= NULL
;
2940 kfree(sit_i
->sit_bitmap
);
2941 #ifdef CONFIG_F2FS_CHECK_FS
2942 kfree(sit_i
->sit_bitmap_mir
);
2947 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
2949 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
2953 destroy_flush_cmd_control(sbi
, true);
2954 destroy_discard_cmd_control(sbi
, true);
2955 destroy_dirty_segmap(sbi
);
2956 destroy_curseg(sbi
);
2957 destroy_free_segmap(sbi
);
2958 destroy_sit_info(sbi
);
2959 sbi
->sm_info
= NULL
;
2963 int __init
create_segment_manager_caches(void)
2965 discard_entry_slab
= f2fs_kmem_cache_create("discard_entry",
2966 sizeof(struct discard_entry
));
2967 if (!discard_entry_slab
)
2970 discard_cmd_slab
= f2fs_kmem_cache_create("discard_cmd",
2971 sizeof(struct discard_cmd
));
2972 if (!discard_cmd_slab
)
2973 goto destroy_discard_entry
;
2975 sit_entry_set_slab
= f2fs_kmem_cache_create("sit_entry_set",
2976 sizeof(struct sit_entry_set
));
2977 if (!sit_entry_set_slab
)
2978 goto destroy_discard_cmd
;
2980 inmem_entry_slab
= f2fs_kmem_cache_create("inmem_page_entry",
2981 sizeof(struct inmem_pages
));
2982 if (!inmem_entry_slab
)
2983 goto destroy_sit_entry_set
;
2986 destroy_sit_entry_set
:
2987 kmem_cache_destroy(sit_entry_set_slab
);
2988 destroy_discard_cmd
:
2989 kmem_cache_destroy(discard_cmd_slab
);
2990 destroy_discard_entry
:
2991 kmem_cache_destroy(discard_entry_slab
);
2996 void destroy_segment_manager_caches(void)
2998 kmem_cache_destroy(sit_entry_set_slab
);
2999 kmem_cache_destroy(discard_cmd_slab
);
3000 kmem_cache_destroy(discard_entry_slab
);
3001 kmem_cache_destroy(inmem_entry_slab
);