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
*sit_entry_set_slab
;
30 static struct kmem_cache
*inmem_entry_slab
;
32 static unsigned long __reverse_ulong(unsigned char *str
)
34 unsigned long tmp
= 0;
35 int shift
= 24, idx
= 0;
37 #if BITS_PER_LONG == 64
41 tmp
|= (unsigned long)str
[idx
++] << shift
;
42 shift
-= BITS_PER_BYTE
;
48 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
49 * MSB and LSB are reversed in a byte by f2fs_set_bit.
51 static inline unsigned long __reverse_ffs(unsigned long word
)
55 #if BITS_PER_LONG == 64
56 if ((word
& 0xffffffff00000000UL
) == 0)
61 if ((word
& 0xffff0000) == 0)
66 if ((word
& 0xff00) == 0)
71 if ((word
& 0xf0) == 0)
76 if ((word
& 0xc) == 0)
81 if ((word
& 0x2) == 0)
87 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
88 * f2fs_set_bit makes MSB and LSB reversed in a byte.
91 * f2fs_set_bit(0, bitmap) => 1000 0000
92 * f2fs_set_bit(7, bitmap) => 0000 0001
94 static unsigned long __find_rev_next_bit(const unsigned long *addr
,
95 unsigned long size
, unsigned long offset
)
97 const unsigned long *p
= addr
+ BIT_WORD(offset
);
98 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
105 offset
%= BITS_PER_LONG
;
109 tmp
= __reverse_ulong((unsigned char *)p
);
110 tmp
&= ~0UL >> offset
;
112 if (size
< BITS_PER_LONG
)
117 size
-= BITS_PER_LONG
;
118 result
+= BITS_PER_LONG
;
121 while (size
& ~(BITS_PER_LONG
-1)) {
122 tmp
= __reverse_ulong((unsigned char *)p
);
125 result
+= BITS_PER_LONG
;
126 size
-= BITS_PER_LONG
;
132 tmp
= __reverse_ulong((unsigned char *)p
);
134 tmp
&= (~0UL << (BITS_PER_LONG
- size
));
135 if (!tmp
) /* Are any bits set? */
136 return result
+ size
; /* Nope. */
138 return result
+ __reverse_ffs(tmp
);
141 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr
,
142 unsigned long size
, unsigned long offset
)
144 const unsigned long *p
= addr
+ BIT_WORD(offset
);
145 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
152 offset
%= BITS_PER_LONG
;
156 tmp
= __reverse_ulong((unsigned char *)p
);
157 tmp
|= ~((~0UL << offset
) >> offset
);
159 if (size
< BITS_PER_LONG
)
164 size
-= BITS_PER_LONG
;
165 result
+= BITS_PER_LONG
;
168 while (size
& ~(BITS_PER_LONG
- 1)) {
169 tmp
= __reverse_ulong((unsigned char *)p
);
172 result
+= BITS_PER_LONG
;
173 size
-= BITS_PER_LONG
;
179 tmp
= __reverse_ulong((unsigned char *)p
);
181 tmp
|= ~(~0UL << (BITS_PER_LONG
- size
));
182 if (tmp
== ~0UL) /* Are any bits zero? */
183 return result
+ size
; /* Nope. */
185 return result
+ __reverse_ffz(tmp
);
188 void register_inmem_page(struct inode
*inode
, struct page
*page
)
190 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
191 struct inmem_pages
*new;
193 f2fs_trace_pid(page
);
195 set_page_private(page
, (unsigned long)ATOMIC_WRITTEN_PAGE
);
196 SetPagePrivate(page
);
198 new = f2fs_kmem_cache_alloc(inmem_entry_slab
, GFP_NOFS
);
200 /* add atomic page indices to the list */
202 INIT_LIST_HEAD(&new->list
);
204 /* increase reference count with clean state */
205 mutex_lock(&fi
->inmem_lock
);
207 list_add_tail(&new->list
, &fi
->inmem_pages
);
208 inc_page_count(F2FS_I_SB(inode
), F2FS_INMEM_PAGES
);
209 mutex_unlock(&fi
->inmem_lock
);
211 trace_f2fs_register_inmem_page(page
, INMEM
);
214 int commit_inmem_pages(struct inode
*inode
, bool abort
)
216 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
217 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
218 struct inmem_pages
*cur
, *tmp
;
219 bool submit_bio
= false;
220 struct f2fs_io_info fio
= {
223 .rw
= WRITE_SYNC
| REQ_PRIO
,
224 .encrypted_page
= NULL
,
229 * The abort is true only when f2fs_evict_inode is called.
230 * Basically, the f2fs_evict_inode doesn't produce any data writes, so
231 * that we don't need to call f2fs_balance_fs.
232 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this
233 * inode becomes free by iget_locked in f2fs_iget.
236 f2fs_balance_fs(sbi
);
240 mutex_lock(&fi
->inmem_lock
);
241 list_for_each_entry_safe(cur
, tmp
, &fi
->inmem_pages
, list
) {
242 lock_page(cur
->page
);
244 if (cur
->page
->mapping
== inode
->i_mapping
) {
245 set_page_dirty(cur
->page
);
246 f2fs_wait_on_page_writeback(cur
->page
, DATA
);
247 if (clear_page_dirty_for_io(cur
->page
))
248 inode_dec_dirty_pages(inode
);
249 trace_f2fs_commit_inmem_page(cur
->page
, INMEM
);
250 fio
.page
= cur
->page
;
251 err
= do_write_data_page(&fio
);
253 unlock_page(cur
->page
);
256 clear_cold_data(cur
->page
);
260 trace_f2fs_commit_inmem_page(cur
->page
, INMEM_DROP
);
262 set_page_private(cur
->page
, 0);
263 ClearPagePrivate(cur
->page
);
264 f2fs_put_page(cur
->page
, 1);
266 list_del(&cur
->list
);
267 kmem_cache_free(inmem_entry_slab
, cur
);
268 dec_page_count(F2FS_I_SB(inode
), F2FS_INMEM_PAGES
);
270 mutex_unlock(&fi
->inmem_lock
);
275 f2fs_submit_merged_bio(sbi
, DATA
, WRITE
);
281 * This function balances dirty node and dentry pages.
282 * In addition, it controls garbage collection.
284 void f2fs_balance_fs(struct f2fs_sb_info
*sbi
)
287 * We should do GC or end up with checkpoint, if there are so many dirty
288 * dir/node pages without enough free segments.
290 if (has_not_enough_free_secs(sbi
, 0)) {
291 mutex_lock(&sbi
->gc_mutex
);
296 void f2fs_balance_fs_bg(struct f2fs_sb_info
*sbi
)
298 /* try to shrink extent cache when there is no enough memory */
299 if (!available_free_memory(sbi
, EXTENT_CACHE
))
300 f2fs_shrink_extent_tree(sbi
, EXTENT_CACHE_SHRINK_NUMBER
);
302 /* check the # of cached NAT entries */
303 if (!available_free_memory(sbi
, NAT_ENTRIES
))
304 try_to_free_nats(sbi
, NAT_ENTRY_PER_BLOCK
);
306 if (!available_free_memory(sbi
, FREE_NIDS
))
307 try_to_free_nids(sbi
, NAT_ENTRY_PER_BLOCK
* FREE_NID_PAGES
);
309 /* checkpoint is the only way to shrink partial cached entries */
310 if (!available_free_memory(sbi
, NAT_ENTRIES
) ||
311 excess_prefree_segs(sbi
) ||
312 !available_free_memory(sbi
, INO_ENTRIES
) ||
313 jiffies
> sbi
->cp_expires
)
314 f2fs_sync_fs(sbi
->sb
, true);
317 static int issue_flush_thread(void *data
)
319 struct f2fs_sb_info
*sbi
= data
;
320 struct flush_cmd_control
*fcc
= SM_I(sbi
)->cmd_control_info
;
321 wait_queue_head_t
*q
= &fcc
->flush_wait_queue
;
323 if (kthread_should_stop())
326 if (!llist_empty(&fcc
->issue_list
)) {
328 struct flush_cmd
*cmd
, *next
;
331 bio
= f2fs_bio_alloc(0);
333 fcc
->dispatch_list
= llist_del_all(&fcc
->issue_list
);
334 fcc
->dispatch_list
= llist_reverse_order(fcc
->dispatch_list
);
336 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
337 ret
= submit_bio_wait(WRITE_FLUSH
, bio
);
339 llist_for_each_entry_safe(cmd
, next
,
340 fcc
->dispatch_list
, llnode
) {
342 complete(&cmd
->wait
);
345 fcc
->dispatch_list
= NULL
;
348 wait_event_interruptible(*q
,
349 kthread_should_stop() || !llist_empty(&fcc
->issue_list
));
353 int f2fs_issue_flush(struct f2fs_sb_info
*sbi
)
355 struct flush_cmd_control
*fcc
= SM_I(sbi
)->cmd_control_info
;
356 struct flush_cmd cmd
;
358 trace_f2fs_issue_flush(sbi
->sb
, test_opt(sbi
, NOBARRIER
),
359 test_opt(sbi
, FLUSH_MERGE
));
361 if (test_opt(sbi
, NOBARRIER
))
364 if (!test_opt(sbi
, FLUSH_MERGE
)) {
365 struct bio
*bio
= f2fs_bio_alloc(0);
368 bio
->bi_bdev
= sbi
->sb
->s_bdev
;
369 ret
= submit_bio_wait(WRITE_FLUSH
, bio
);
374 init_completion(&cmd
.wait
);
376 llist_add(&cmd
.llnode
, &fcc
->issue_list
);
378 if (!fcc
->dispatch_list
)
379 wake_up(&fcc
->flush_wait_queue
);
381 wait_for_completion(&cmd
.wait
);
386 int create_flush_cmd_control(struct f2fs_sb_info
*sbi
)
388 dev_t dev
= sbi
->sb
->s_bdev
->bd_dev
;
389 struct flush_cmd_control
*fcc
;
392 fcc
= kzalloc(sizeof(struct flush_cmd_control
), GFP_KERNEL
);
395 init_waitqueue_head(&fcc
->flush_wait_queue
);
396 init_llist_head(&fcc
->issue_list
);
397 SM_I(sbi
)->cmd_control_info
= fcc
;
398 fcc
->f2fs_issue_flush
= kthread_run(issue_flush_thread
, sbi
,
399 "f2fs_flush-%u:%u", MAJOR(dev
), MINOR(dev
));
400 if (IS_ERR(fcc
->f2fs_issue_flush
)) {
401 err
= PTR_ERR(fcc
->f2fs_issue_flush
);
403 SM_I(sbi
)->cmd_control_info
= NULL
;
410 void destroy_flush_cmd_control(struct f2fs_sb_info
*sbi
)
412 struct flush_cmd_control
*fcc
= SM_I(sbi
)->cmd_control_info
;
414 if (fcc
&& fcc
->f2fs_issue_flush
)
415 kthread_stop(fcc
->f2fs_issue_flush
);
417 SM_I(sbi
)->cmd_control_info
= NULL
;
420 static void __locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
421 enum dirty_type dirty_type
)
423 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
425 /* need not be added */
426 if (IS_CURSEG(sbi
, segno
))
429 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
430 dirty_i
->nr_dirty
[dirty_type
]++;
432 if (dirty_type
== DIRTY
) {
433 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
434 enum dirty_type t
= sentry
->type
;
436 if (unlikely(t
>= DIRTY
)) {
440 if (!test_and_set_bit(segno
, dirty_i
->dirty_segmap
[t
]))
441 dirty_i
->nr_dirty
[t
]++;
445 static void __remove_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
,
446 enum dirty_type dirty_type
)
448 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
450 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[dirty_type
]))
451 dirty_i
->nr_dirty
[dirty_type
]--;
453 if (dirty_type
== DIRTY
) {
454 struct seg_entry
*sentry
= get_seg_entry(sbi
, segno
);
455 enum dirty_type t
= sentry
->type
;
457 if (test_and_clear_bit(segno
, dirty_i
->dirty_segmap
[t
]))
458 dirty_i
->nr_dirty
[t
]--;
460 if (get_valid_blocks(sbi
, segno
, sbi
->segs_per_sec
) == 0)
461 clear_bit(GET_SECNO(sbi
, segno
),
462 dirty_i
->victim_secmap
);
467 * Should not occur error such as -ENOMEM.
468 * Adding dirty entry into seglist is not critical operation.
469 * If a given segment is one of current working segments, it won't be added.
471 static void locate_dirty_segment(struct f2fs_sb_info
*sbi
, unsigned int segno
)
473 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
474 unsigned short valid_blocks
;
476 if (segno
== NULL_SEGNO
|| IS_CURSEG(sbi
, segno
))
479 mutex_lock(&dirty_i
->seglist_lock
);
481 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
483 if (valid_blocks
== 0) {
484 __locate_dirty_segment(sbi
, segno
, PRE
);
485 __remove_dirty_segment(sbi
, segno
, DIRTY
);
486 } else if (valid_blocks
< sbi
->blocks_per_seg
) {
487 __locate_dirty_segment(sbi
, segno
, DIRTY
);
489 /* Recovery routine with SSR needs this */
490 __remove_dirty_segment(sbi
, segno
, DIRTY
);
493 mutex_unlock(&dirty_i
->seglist_lock
);
496 static int f2fs_issue_discard(struct f2fs_sb_info
*sbi
,
497 block_t blkstart
, block_t blklen
)
499 sector_t start
= SECTOR_FROM_BLOCK(blkstart
);
500 sector_t len
= SECTOR_FROM_BLOCK(blklen
);
501 struct seg_entry
*se
;
505 for (i
= blkstart
; i
< blkstart
+ blklen
; i
++) {
506 se
= get_seg_entry(sbi
, GET_SEGNO(sbi
, i
));
507 offset
= GET_BLKOFF_FROM_SEG0(sbi
, i
);
509 if (!f2fs_test_and_set_bit(offset
, se
->discard_map
))
512 trace_f2fs_issue_discard(sbi
->sb
, blkstart
, blklen
);
513 return blkdev_issue_discard(sbi
->sb
->s_bdev
, start
, len
, GFP_NOFS
, 0);
516 bool discard_next_dnode(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
520 if (test_opt(sbi
, DISCARD
)) {
521 struct seg_entry
*se
= get_seg_entry(sbi
,
522 GET_SEGNO(sbi
, blkaddr
));
523 unsigned int offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
525 if (f2fs_test_bit(offset
, se
->discard_map
))
528 err
= f2fs_issue_discard(sbi
, blkaddr
, 1);
532 update_meta_page(sbi
, NULL
, blkaddr
);
538 static void __add_discard_entry(struct f2fs_sb_info
*sbi
,
539 struct cp_control
*cpc
, struct seg_entry
*se
,
540 unsigned int start
, unsigned int end
)
542 struct list_head
*head
= &SM_I(sbi
)->discard_list
;
543 struct discard_entry
*new, *last
;
545 if (!list_empty(head
)) {
546 last
= list_last_entry(head
, struct discard_entry
, list
);
547 if (START_BLOCK(sbi
, cpc
->trim_start
) + start
==
548 last
->blkaddr
+ last
->len
) {
549 last
->len
+= end
- start
;
554 new = f2fs_kmem_cache_alloc(discard_entry_slab
, GFP_NOFS
);
555 INIT_LIST_HEAD(&new->list
);
556 new->blkaddr
= START_BLOCK(sbi
, cpc
->trim_start
) + start
;
557 new->len
= end
- start
;
558 list_add_tail(&new->list
, head
);
560 SM_I(sbi
)->nr_discards
+= end
- start
;
563 static void add_discard_addrs(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
565 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
566 int max_blocks
= sbi
->blocks_per_seg
;
567 struct seg_entry
*se
= get_seg_entry(sbi
, cpc
->trim_start
);
568 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
569 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
570 unsigned long *discard_map
= (unsigned long *)se
->discard_map
;
571 unsigned long *dmap
= SIT_I(sbi
)->tmp_map
;
572 unsigned int start
= 0, end
= -1;
573 bool force
= (cpc
->reason
== CP_DISCARD
);
576 if (se
->valid_blocks
== max_blocks
)
580 if (!test_opt(sbi
, DISCARD
) || !se
->valid_blocks
||
581 SM_I(sbi
)->nr_discards
>= SM_I(sbi
)->max_discards
)
585 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
586 for (i
= 0; i
< entries
; i
++)
587 dmap
[i
] = force
? ~ckpt_map
[i
] & ~discard_map
[i
] :
588 (cur_map
[i
] ^ ckpt_map
[i
]) & ckpt_map
[i
];
590 while (force
|| SM_I(sbi
)->nr_discards
<= SM_I(sbi
)->max_discards
) {
591 start
= __find_rev_next_bit(dmap
, max_blocks
, end
+ 1);
592 if (start
>= max_blocks
)
595 end
= __find_rev_next_zero_bit(dmap
, max_blocks
, start
+ 1);
596 __add_discard_entry(sbi
, cpc
, se
, start
, end
);
600 void release_discard_addrs(struct f2fs_sb_info
*sbi
)
602 struct list_head
*head
= &(SM_I(sbi
)->discard_list
);
603 struct discard_entry
*entry
, *this;
606 list_for_each_entry_safe(entry
, this, head
, list
) {
607 list_del(&entry
->list
);
608 kmem_cache_free(discard_entry_slab
, entry
);
613 * Should call clear_prefree_segments after checkpoint is done.
615 static void set_prefree_as_free_segments(struct f2fs_sb_info
*sbi
)
617 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
620 mutex_lock(&dirty_i
->seglist_lock
);
621 for_each_set_bit(segno
, dirty_i
->dirty_segmap
[PRE
], MAIN_SEGS(sbi
))
622 __set_test_and_free(sbi
, segno
);
623 mutex_unlock(&dirty_i
->seglist_lock
);
626 void clear_prefree_segments(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
628 struct list_head
*head
= &(SM_I(sbi
)->discard_list
);
629 struct discard_entry
*entry
, *this;
630 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
631 unsigned long *prefree_map
= dirty_i
->dirty_segmap
[PRE
];
632 unsigned int start
= 0, end
= -1;
634 mutex_lock(&dirty_i
->seglist_lock
);
638 start
= find_next_bit(prefree_map
, MAIN_SEGS(sbi
), end
+ 1);
639 if (start
>= MAIN_SEGS(sbi
))
641 end
= find_next_zero_bit(prefree_map
, MAIN_SEGS(sbi
),
644 for (i
= start
; i
< end
; i
++)
645 clear_bit(i
, prefree_map
);
647 dirty_i
->nr_dirty
[PRE
] -= end
- start
;
649 if (!test_opt(sbi
, DISCARD
))
652 f2fs_issue_discard(sbi
, START_BLOCK(sbi
, start
),
653 (end
- start
) << sbi
->log_blocks_per_seg
);
655 mutex_unlock(&dirty_i
->seglist_lock
);
657 /* send small discards */
658 list_for_each_entry_safe(entry
, this, head
, list
) {
659 if (cpc
->reason
== CP_DISCARD
&& entry
->len
< cpc
->trim_minlen
)
661 f2fs_issue_discard(sbi
, entry
->blkaddr
, entry
->len
);
662 cpc
->trimmed
+= entry
->len
;
664 list_del(&entry
->list
);
665 SM_I(sbi
)->nr_discards
-= entry
->len
;
666 kmem_cache_free(discard_entry_slab
, entry
);
670 static bool __mark_sit_entry_dirty(struct f2fs_sb_info
*sbi
, unsigned int segno
)
672 struct sit_info
*sit_i
= SIT_I(sbi
);
674 if (!__test_and_set_bit(segno
, sit_i
->dirty_sentries_bitmap
)) {
675 sit_i
->dirty_sentries
++;
682 static void __set_sit_entry_type(struct f2fs_sb_info
*sbi
, int type
,
683 unsigned int segno
, int modified
)
685 struct seg_entry
*se
= get_seg_entry(sbi
, segno
);
688 __mark_sit_entry_dirty(sbi
, segno
);
691 static void update_sit_entry(struct f2fs_sb_info
*sbi
, block_t blkaddr
, int del
)
693 struct seg_entry
*se
;
694 unsigned int segno
, offset
;
695 long int new_vblocks
;
697 segno
= GET_SEGNO(sbi
, blkaddr
);
699 se
= get_seg_entry(sbi
, segno
);
700 new_vblocks
= se
->valid_blocks
+ del
;
701 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
703 f2fs_bug_on(sbi
, (new_vblocks
>> (sizeof(unsigned short) << 3) ||
704 (new_vblocks
> sbi
->blocks_per_seg
)));
706 se
->valid_blocks
= new_vblocks
;
707 se
->mtime
= get_mtime(sbi
);
708 SIT_I(sbi
)->max_mtime
= se
->mtime
;
710 /* Update valid block bitmap */
712 if (f2fs_test_and_set_bit(offset
, se
->cur_valid_map
))
714 if (!f2fs_test_and_set_bit(offset
, se
->discard_map
))
717 if (!f2fs_test_and_clear_bit(offset
, se
->cur_valid_map
))
719 if (f2fs_test_and_clear_bit(offset
, se
->discard_map
))
722 if (!f2fs_test_bit(offset
, se
->ckpt_valid_map
))
723 se
->ckpt_valid_blocks
+= del
;
725 __mark_sit_entry_dirty(sbi
, segno
);
727 /* update total number of valid blocks to be written in ckpt area */
728 SIT_I(sbi
)->written_valid_blocks
+= del
;
730 if (sbi
->segs_per_sec
> 1)
731 get_sec_entry(sbi
, segno
)->valid_blocks
+= del
;
734 void refresh_sit_entry(struct f2fs_sb_info
*sbi
, block_t old
, block_t
new)
736 update_sit_entry(sbi
, new, 1);
737 if (GET_SEGNO(sbi
, old
) != NULL_SEGNO
)
738 update_sit_entry(sbi
, old
, -1);
740 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old
));
741 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, new));
744 void invalidate_blocks(struct f2fs_sb_info
*sbi
, block_t addr
)
746 unsigned int segno
= GET_SEGNO(sbi
, addr
);
747 struct sit_info
*sit_i
= SIT_I(sbi
);
749 f2fs_bug_on(sbi
, addr
== NULL_ADDR
);
750 if (addr
== NEW_ADDR
)
753 /* add it into sit main buffer */
754 mutex_lock(&sit_i
->sentry_lock
);
756 update_sit_entry(sbi
, addr
, -1);
758 /* add it into dirty seglist */
759 locate_dirty_segment(sbi
, segno
);
761 mutex_unlock(&sit_i
->sentry_lock
);
764 bool is_checkpointed_data(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
766 struct sit_info
*sit_i
= SIT_I(sbi
);
767 unsigned int segno
, offset
;
768 struct seg_entry
*se
;
771 if (blkaddr
== NEW_ADDR
|| blkaddr
== NULL_ADDR
)
774 mutex_lock(&sit_i
->sentry_lock
);
776 segno
= GET_SEGNO(sbi
, blkaddr
);
777 se
= get_seg_entry(sbi
, segno
);
778 offset
= GET_BLKOFF_FROM_SEG0(sbi
, blkaddr
);
780 if (f2fs_test_bit(offset
, se
->ckpt_valid_map
))
783 mutex_unlock(&sit_i
->sentry_lock
);
789 * This function should be resided under the curseg_mutex lock
791 static void __add_sum_entry(struct f2fs_sb_info
*sbi
, int type
,
792 struct f2fs_summary
*sum
)
794 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
795 void *addr
= curseg
->sum_blk
;
796 addr
+= curseg
->next_blkoff
* sizeof(struct f2fs_summary
);
797 memcpy(addr
, sum
, sizeof(struct f2fs_summary
));
801 * Calculate the number of current summary pages for writing
803 int npages_for_summary_flush(struct f2fs_sb_info
*sbi
, bool for_ra
)
805 int valid_sum_count
= 0;
808 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
809 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
810 valid_sum_count
+= sbi
->blocks_per_seg
;
813 valid_sum_count
+= le16_to_cpu(
814 F2FS_CKPT(sbi
)->cur_data_blkoff
[i
]);
816 valid_sum_count
+= curseg_blkoff(sbi
, i
);
820 sum_in_page
= (PAGE_CACHE_SIZE
- 2 * SUM_JOURNAL_SIZE
-
821 SUM_FOOTER_SIZE
) / SUMMARY_SIZE
;
822 if (valid_sum_count
<= sum_in_page
)
824 else if ((valid_sum_count
- sum_in_page
) <=
825 (PAGE_CACHE_SIZE
- SUM_FOOTER_SIZE
) / SUMMARY_SIZE
)
831 * Caller should put this summary page
833 struct page
*get_sum_page(struct f2fs_sb_info
*sbi
, unsigned int segno
)
835 return get_meta_page(sbi
, GET_SUM_BLOCK(sbi
, segno
));
838 void update_meta_page(struct f2fs_sb_info
*sbi
, void *src
, block_t blk_addr
)
840 struct page
*page
= grab_meta_page(sbi
, blk_addr
);
841 void *dst
= page_address(page
);
844 memcpy(dst
, src
, PAGE_CACHE_SIZE
);
846 memset(dst
, 0, PAGE_CACHE_SIZE
);
847 set_page_dirty(page
);
848 f2fs_put_page(page
, 1);
851 static void write_sum_page(struct f2fs_sb_info
*sbi
,
852 struct f2fs_summary_block
*sum_blk
, block_t blk_addr
)
854 update_meta_page(sbi
, (void *)sum_blk
, blk_addr
);
857 static int is_next_segment_free(struct f2fs_sb_info
*sbi
, int type
)
859 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
860 unsigned int segno
= curseg
->segno
+ 1;
861 struct free_segmap_info
*free_i
= FREE_I(sbi
);
863 if (segno
< MAIN_SEGS(sbi
) && segno
% sbi
->segs_per_sec
)
864 return !test_bit(segno
, free_i
->free_segmap
);
869 * Find a new segment from the free segments bitmap to right order
870 * This function should be returned with success, otherwise BUG
872 static void get_new_segment(struct f2fs_sb_info
*sbi
,
873 unsigned int *newseg
, bool new_sec
, int dir
)
875 struct free_segmap_info
*free_i
= FREE_I(sbi
);
876 unsigned int segno
, secno
, zoneno
;
877 unsigned int total_zones
= MAIN_SECS(sbi
) / sbi
->secs_per_zone
;
878 unsigned int hint
= *newseg
/ sbi
->segs_per_sec
;
879 unsigned int old_zoneno
= GET_ZONENO_FROM_SEGNO(sbi
, *newseg
);
880 unsigned int left_start
= hint
;
885 spin_lock(&free_i
->segmap_lock
);
887 if (!new_sec
&& ((*newseg
+ 1) % sbi
->segs_per_sec
)) {
888 segno
= find_next_zero_bit(free_i
->free_segmap
,
889 MAIN_SEGS(sbi
), *newseg
+ 1);
890 if (segno
- *newseg
< sbi
->segs_per_sec
-
891 (*newseg
% sbi
->segs_per_sec
))
895 secno
= find_next_zero_bit(free_i
->free_secmap
, MAIN_SECS(sbi
), hint
);
896 if (secno
>= MAIN_SECS(sbi
)) {
897 if (dir
== ALLOC_RIGHT
) {
898 secno
= find_next_zero_bit(free_i
->free_secmap
,
900 f2fs_bug_on(sbi
, secno
>= MAIN_SECS(sbi
));
903 left_start
= hint
- 1;
909 while (test_bit(left_start
, free_i
->free_secmap
)) {
910 if (left_start
> 0) {
914 left_start
= find_next_zero_bit(free_i
->free_secmap
,
916 f2fs_bug_on(sbi
, left_start
>= MAIN_SECS(sbi
));
922 segno
= secno
* sbi
->segs_per_sec
;
923 zoneno
= secno
/ sbi
->secs_per_zone
;
925 /* give up on finding another zone */
928 if (sbi
->secs_per_zone
== 1)
930 if (zoneno
== old_zoneno
)
932 if (dir
== ALLOC_LEFT
) {
933 if (!go_left
&& zoneno
+ 1 >= total_zones
)
935 if (go_left
&& zoneno
== 0)
938 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
939 if (CURSEG_I(sbi
, i
)->zone
== zoneno
)
942 if (i
< NR_CURSEG_TYPE
) {
943 /* zone is in user, try another */
945 hint
= zoneno
* sbi
->secs_per_zone
- 1;
946 else if (zoneno
+ 1 >= total_zones
)
949 hint
= (zoneno
+ 1) * sbi
->secs_per_zone
;
951 goto find_other_zone
;
954 /* set it as dirty segment in free segmap */
955 f2fs_bug_on(sbi
, test_bit(segno
, free_i
->free_segmap
));
956 __set_inuse(sbi
, segno
);
958 spin_unlock(&free_i
->segmap_lock
);
961 static void reset_curseg(struct f2fs_sb_info
*sbi
, int type
, int modified
)
963 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
964 struct summary_footer
*sum_footer
;
966 curseg
->segno
= curseg
->next_segno
;
967 curseg
->zone
= GET_ZONENO_FROM_SEGNO(sbi
, curseg
->segno
);
968 curseg
->next_blkoff
= 0;
969 curseg
->next_segno
= NULL_SEGNO
;
971 sum_footer
= &(curseg
->sum_blk
->footer
);
972 memset(sum_footer
, 0, sizeof(struct summary_footer
));
973 if (IS_DATASEG(type
))
974 SET_SUM_TYPE(sum_footer
, SUM_TYPE_DATA
);
975 if (IS_NODESEG(type
))
976 SET_SUM_TYPE(sum_footer
, SUM_TYPE_NODE
);
977 __set_sit_entry_type(sbi
, type
, curseg
->segno
, modified
);
981 * Allocate a current working segment.
982 * This function always allocates a free segment in LFS manner.
984 static void new_curseg(struct f2fs_sb_info
*sbi
, int type
, bool new_sec
)
986 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
987 unsigned int segno
= curseg
->segno
;
988 int dir
= ALLOC_LEFT
;
990 write_sum_page(sbi
, curseg
->sum_blk
,
991 GET_SUM_BLOCK(sbi
, segno
));
992 if (type
== CURSEG_WARM_DATA
|| type
== CURSEG_COLD_DATA
)
995 if (test_opt(sbi
, NOHEAP
))
998 get_new_segment(sbi
, &segno
, new_sec
, dir
);
999 curseg
->next_segno
= segno
;
1000 reset_curseg(sbi
, type
, 1);
1001 curseg
->alloc_type
= LFS
;
1004 static void __next_free_blkoff(struct f2fs_sb_info
*sbi
,
1005 struct curseg_info
*seg
, block_t start
)
1007 struct seg_entry
*se
= get_seg_entry(sbi
, seg
->segno
);
1008 int entries
= SIT_VBLOCK_MAP_SIZE
/ sizeof(unsigned long);
1009 unsigned long *target_map
= SIT_I(sbi
)->tmp_map
;
1010 unsigned long *ckpt_map
= (unsigned long *)se
->ckpt_valid_map
;
1011 unsigned long *cur_map
= (unsigned long *)se
->cur_valid_map
;
1014 for (i
= 0; i
< entries
; i
++)
1015 target_map
[i
] = ckpt_map
[i
] | cur_map
[i
];
1017 pos
= __find_rev_next_zero_bit(target_map
, sbi
->blocks_per_seg
, start
);
1019 seg
->next_blkoff
= pos
;
1023 * If a segment is written by LFS manner, next block offset is just obtained
1024 * by increasing the current block offset. However, if a segment is written by
1025 * SSR manner, next block offset obtained by calling __next_free_blkoff
1027 static void __refresh_next_blkoff(struct f2fs_sb_info
*sbi
,
1028 struct curseg_info
*seg
)
1030 if (seg
->alloc_type
== SSR
)
1031 __next_free_blkoff(sbi
, seg
, seg
->next_blkoff
+ 1);
1037 * This function always allocates a used segment(from dirty seglist) by SSR
1038 * manner, so it should recover the existing segment information of valid blocks
1040 static void change_curseg(struct f2fs_sb_info
*sbi
, int type
, bool reuse
)
1042 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
1043 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1044 unsigned int new_segno
= curseg
->next_segno
;
1045 struct f2fs_summary_block
*sum_node
;
1046 struct page
*sum_page
;
1048 write_sum_page(sbi
, curseg
->sum_blk
,
1049 GET_SUM_BLOCK(sbi
, curseg
->segno
));
1050 __set_test_and_inuse(sbi
, new_segno
);
1052 mutex_lock(&dirty_i
->seglist_lock
);
1053 __remove_dirty_segment(sbi
, new_segno
, PRE
);
1054 __remove_dirty_segment(sbi
, new_segno
, DIRTY
);
1055 mutex_unlock(&dirty_i
->seglist_lock
);
1057 reset_curseg(sbi
, type
, 1);
1058 curseg
->alloc_type
= SSR
;
1059 __next_free_blkoff(sbi
, curseg
, 0);
1062 sum_page
= get_sum_page(sbi
, new_segno
);
1063 sum_node
= (struct f2fs_summary_block
*)page_address(sum_page
);
1064 memcpy(curseg
->sum_blk
, sum_node
, SUM_ENTRY_SIZE
);
1065 f2fs_put_page(sum_page
, 1);
1069 static int get_ssr_segment(struct f2fs_sb_info
*sbi
, int type
)
1071 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1072 const struct victim_selection
*v_ops
= DIRTY_I(sbi
)->v_ops
;
1074 if (IS_NODESEG(type
) || !has_not_enough_free_secs(sbi
, 0))
1075 return v_ops
->get_victim(sbi
,
1076 &(curseg
)->next_segno
, BG_GC
, type
, SSR
);
1078 /* For data segments, let's do SSR more intensively */
1079 for (; type
>= CURSEG_HOT_DATA
; type
--)
1080 if (v_ops
->get_victim(sbi
, &(curseg
)->next_segno
,
1087 * flush out current segment and replace it with new segment
1088 * This function should be returned with success, otherwise BUG
1090 static void allocate_segment_by_default(struct f2fs_sb_info
*sbi
,
1091 int type
, bool force
)
1093 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1096 new_curseg(sbi
, type
, true);
1097 else if (type
== CURSEG_WARM_NODE
)
1098 new_curseg(sbi
, type
, false);
1099 else if (curseg
->alloc_type
== LFS
&& is_next_segment_free(sbi
, type
))
1100 new_curseg(sbi
, type
, false);
1101 else if (need_SSR(sbi
) && get_ssr_segment(sbi
, type
))
1102 change_curseg(sbi
, type
, true);
1104 new_curseg(sbi
, type
, false);
1106 stat_inc_seg_type(sbi
, curseg
);
1109 static void __allocate_new_segments(struct f2fs_sb_info
*sbi
, int type
)
1111 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1112 unsigned int old_segno
;
1114 old_segno
= curseg
->segno
;
1115 SIT_I(sbi
)->s_ops
->allocate_segment(sbi
, type
, true);
1116 locate_dirty_segment(sbi
, old_segno
);
1119 void allocate_new_segments(struct f2fs_sb_info
*sbi
)
1123 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++)
1124 __allocate_new_segments(sbi
, i
);
1127 static const struct segment_allocation default_salloc_ops
= {
1128 .allocate_segment
= allocate_segment_by_default
,
1131 int f2fs_trim_fs(struct f2fs_sb_info
*sbi
, struct fstrim_range
*range
)
1133 __u64 start
= F2FS_BYTES_TO_BLK(range
->start
);
1134 __u64 end
= start
+ F2FS_BYTES_TO_BLK(range
->len
) - 1;
1135 unsigned int start_segno
, end_segno
;
1136 struct cp_control cpc
;
1138 if (start
>= MAX_BLKADDR(sbi
) || range
->len
< sbi
->blocksize
)
1142 if (end
<= MAIN_BLKADDR(sbi
))
1145 /* start/end segment number in main_area */
1146 start_segno
= (start
<= MAIN_BLKADDR(sbi
)) ? 0 : GET_SEGNO(sbi
, start
);
1147 end_segno
= (end
>= MAX_BLKADDR(sbi
)) ? MAIN_SEGS(sbi
) - 1 :
1148 GET_SEGNO(sbi
, end
);
1149 cpc
.reason
= CP_DISCARD
;
1150 cpc
.trim_minlen
= max_t(__u64
, 1, F2FS_BYTES_TO_BLK(range
->minlen
));
1152 /* do checkpoint to issue discard commands safely */
1153 for (; start_segno
<= end_segno
; start_segno
= cpc
.trim_end
+ 1) {
1154 cpc
.trim_start
= start_segno
;
1156 if (sbi
->discard_blks
== 0)
1158 else if (sbi
->discard_blks
< BATCHED_TRIM_BLOCKS(sbi
))
1159 cpc
.trim_end
= end_segno
;
1161 cpc
.trim_end
= min_t(unsigned int,
1162 rounddown(start_segno
+
1163 BATCHED_TRIM_SEGMENTS(sbi
),
1164 sbi
->segs_per_sec
) - 1, end_segno
);
1166 mutex_lock(&sbi
->gc_mutex
);
1167 write_checkpoint(sbi
, &cpc
);
1168 mutex_unlock(&sbi
->gc_mutex
);
1171 range
->len
= F2FS_BLK_TO_BYTES(cpc
.trimmed
);
1175 static bool __has_curseg_space(struct f2fs_sb_info
*sbi
, int type
)
1177 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
1178 if (curseg
->next_blkoff
< sbi
->blocks_per_seg
)
1183 static int __get_segment_type_2(struct page
*page
, enum page_type p_type
)
1186 return CURSEG_HOT_DATA
;
1188 return CURSEG_HOT_NODE
;
1191 static int __get_segment_type_4(struct page
*page
, enum page_type p_type
)
1193 if (p_type
== DATA
) {
1194 struct inode
*inode
= page
->mapping
->host
;
1196 if (S_ISDIR(inode
->i_mode
))
1197 return CURSEG_HOT_DATA
;
1199 return CURSEG_COLD_DATA
;
1201 if (IS_DNODE(page
) && is_cold_node(page
))
1202 return CURSEG_WARM_NODE
;
1204 return CURSEG_COLD_NODE
;
1208 static int __get_segment_type_6(struct page
*page
, enum page_type p_type
)
1210 if (p_type
== DATA
) {
1211 struct inode
*inode
= page
->mapping
->host
;
1213 if (S_ISDIR(inode
->i_mode
))
1214 return CURSEG_HOT_DATA
;
1215 else if (is_cold_data(page
) || file_is_cold(inode
))
1216 return CURSEG_COLD_DATA
;
1218 return CURSEG_WARM_DATA
;
1221 return is_cold_node(page
) ? CURSEG_WARM_NODE
:
1224 return CURSEG_COLD_NODE
;
1228 static int __get_segment_type(struct page
*page
, enum page_type p_type
)
1230 switch (F2FS_P_SB(page
)->active_logs
) {
1232 return __get_segment_type_2(page
, p_type
);
1234 return __get_segment_type_4(page
, p_type
);
1236 /* NR_CURSEG_TYPE(6) logs by default */
1237 f2fs_bug_on(F2FS_P_SB(page
),
1238 F2FS_P_SB(page
)->active_logs
!= NR_CURSEG_TYPE
);
1239 return __get_segment_type_6(page
, p_type
);
1242 void allocate_data_block(struct f2fs_sb_info
*sbi
, struct page
*page
,
1243 block_t old_blkaddr
, block_t
*new_blkaddr
,
1244 struct f2fs_summary
*sum
, int type
)
1246 struct sit_info
*sit_i
= SIT_I(sbi
);
1247 struct curseg_info
*curseg
;
1248 bool direct_io
= (type
== CURSEG_DIRECT_IO
);
1250 type
= direct_io
? CURSEG_WARM_DATA
: type
;
1252 curseg
= CURSEG_I(sbi
, type
);
1254 mutex_lock(&curseg
->curseg_mutex
);
1255 mutex_lock(&sit_i
->sentry_lock
);
1257 /* direct_io'ed data is aligned to the segment for better performance */
1258 if (direct_io
&& curseg
->next_blkoff
&&
1259 !has_not_enough_free_secs(sbi
, 0))
1260 __allocate_new_segments(sbi
, type
);
1262 *new_blkaddr
= NEXT_FREE_BLKADDR(sbi
, curseg
);
1265 * __add_sum_entry should be resided under the curseg_mutex
1266 * because, this function updates a summary entry in the
1267 * current summary block.
1269 __add_sum_entry(sbi
, type
, sum
);
1271 __refresh_next_blkoff(sbi
, curseg
);
1273 stat_inc_block_count(sbi
, curseg
);
1275 if (!__has_curseg_space(sbi
, type
))
1276 sit_i
->s_ops
->allocate_segment(sbi
, type
, false);
1278 * SIT information should be updated before segment allocation,
1279 * since SSR needs latest valid block information.
1281 refresh_sit_entry(sbi
, old_blkaddr
, *new_blkaddr
);
1283 mutex_unlock(&sit_i
->sentry_lock
);
1285 if (page
&& IS_NODESEG(type
))
1286 fill_node_footer_blkaddr(page
, NEXT_FREE_BLKADDR(sbi
, curseg
));
1288 mutex_unlock(&curseg
->curseg_mutex
);
1291 static void do_write_page(struct f2fs_summary
*sum
, struct f2fs_io_info
*fio
)
1293 int type
= __get_segment_type(fio
->page
, fio
->type
);
1295 allocate_data_block(fio
->sbi
, fio
->page
, fio
->blk_addr
,
1296 &fio
->blk_addr
, sum
, type
);
1298 /* writeout dirty page into bdev */
1299 f2fs_submit_page_mbio(fio
);
1302 void write_meta_page(struct f2fs_sb_info
*sbi
, struct page
*page
)
1304 struct f2fs_io_info fio
= {
1307 .rw
= WRITE_SYNC
| REQ_META
| REQ_PRIO
,
1308 .blk_addr
= page
->index
,
1310 .encrypted_page
= NULL
,
1313 if (unlikely(page
->index
>= MAIN_BLKADDR(sbi
)))
1314 fio
.rw
&= ~REQ_META
;
1316 set_page_writeback(page
);
1317 f2fs_submit_page_mbio(&fio
);
1320 void write_node_page(unsigned int nid
, struct f2fs_io_info
*fio
)
1322 struct f2fs_summary sum
;
1324 set_summary(&sum
, nid
, 0, 0);
1325 do_write_page(&sum
, fio
);
1328 void write_data_page(struct dnode_of_data
*dn
, struct f2fs_io_info
*fio
)
1330 struct f2fs_sb_info
*sbi
= fio
->sbi
;
1331 struct f2fs_summary sum
;
1332 struct node_info ni
;
1334 f2fs_bug_on(sbi
, dn
->data_blkaddr
== NULL_ADDR
);
1335 get_node_info(sbi
, dn
->nid
, &ni
);
1336 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, ni
.version
);
1337 do_write_page(&sum
, fio
);
1338 dn
->data_blkaddr
= fio
->blk_addr
;
1341 void rewrite_data_page(struct f2fs_io_info
*fio
)
1343 stat_inc_inplace_blocks(fio
->sbi
);
1344 f2fs_submit_page_mbio(fio
);
1347 static void __f2fs_replace_block(struct f2fs_sb_info
*sbi
,
1348 struct f2fs_summary
*sum
,
1349 block_t old_blkaddr
, block_t new_blkaddr
,
1350 bool recover_curseg
)
1352 struct sit_info
*sit_i
= SIT_I(sbi
);
1353 struct curseg_info
*curseg
;
1354 unsigned int segno
, old_cursegno
;
1355 struct seg_entry
*se
;
1357 unsigned short old_blkoff
;
1359 segno
= GET_SEGNO(sbi
, new_blkaddr
);
1360 se
= get_seg_entry(sbi
, segno
);
1363 if (!recover_curseg
) {
1364 /* for recovery flow */
1365 if (se
->valid_blocks
== 0 && !IS_CURSEG(sbi
, segno
)) {
1366 if (old_blkaddr
== NULL_ADDR
)
1367 type
= CURSEG_COLD_DATA
;
1369 type
= CURSEG_WARM_DATA
;
1372 if (!IS_CURSEG(sbi
, segno
))
1373 type
= CURSEG_WARM_DATA
;
1376 curseg
= CURSEG_I(sbi
, type
);
1378 mutex_lock(&curseg
->curseg_mutex
);
1379 mutex_lock(&sit_i
->sentry_lock
);
1381 old_cursegno
= curseg
->segno
;
1382 old_blkoff
= curseg
->next_blkoff
;
1384 /* change the current segment */
1385 if (segno
!= curseg
->segno
) {
1386 curseg
->next_segno
= segno
;
1387 change_curseg(sbi
, type
, true);
1390 curseg
->next_blkoff
= GET_BLKOFF_FROM_SEG0(sbi
, new_blkaddr
);
1391 __add_sum_entry(sbi
, type
, sum
);
1393 if (!recover_curseg
)
1394 update_sit_entry(sbi
, new_blkaddr
, 1);
1395 if (GET_SEGNO(sbi
, old_blkaddr
) != NULL_SEGNO
)
1396 update_sit_entry(sbi
, old_blkaddr
, -1);
1398 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, old_blkaddr
));
1399 locate_dirty_segment(sbi
, GET_SEGNO(sbi
, new_blkaddr
));
1401 locate_dirty_segment(sbi
, old_cursegno
);
1403 if (recover_curseg
) {
1404 if (old_cursegno
!= curseg
->segno
) {
1405 curseg
->next_segno
= old_cursegno
;
1406 change_curseg(sbi
, type
, true);
1408 curseg
->next_blkoff
= old_blkoff
;
1411 mutex_unlock(&sit_i
->sentry_lock
);
1412 mutex_unlock(&curseg
->curseg_mutex
);
1415 void f2fs_replace_block(struct f2fs_sb_info
*sbi
, struct dnode_of_data
*dn
,
1416 block_t old_addr
, block_t new_addr
,
1417 unsigned char version
, bool recover_curseg
)
1419 struct f2fs_summary sum
;
1421 set_summary(&sum
, dn
->nid
, dn
->ofs_in_node
, version
);
1423 __f2fs_replace_block(sbi
, &sum
, old_addr
, new_addr
, recover_curseg
);
1425 dn
->data_blkaddr
= new_addr
;
1426 set_data_blkaddr(dn
);
1427 f2fs_update_extent_cache(dn
);
1430 static inline bool is_merged_page(struct f2fs_sb_info
*sbi
,
1431 struct page
*page
, enum page_type type
)
1433 enum page_type btype
= PAGE_TYPE_OF_BIO(type
);
1434 struct f2fs_bio_info
*io
= &sbi
->write_io
[btype
];
1435 struct bio_vec
*bvec
;
1436 struct page
*target
;
1439 down_read(&io
->io_rwsem
);
1441 up_read(&io
->io_rwsem
);
1445 bio_for_each_segment_all(bvec
, io
->bio
, i
) {
1447 if (bvec
->bv_page
->mapping
) {
1448 target
= bvec
->bv_page
;
1450 struct f2fs_crypto_ctx
*ctx
;
1452 /* encrypted page */
1453 ctx
= (struct f2fs_crypto_ctx
*)page_private(
1455 target
= ctx
->w
.control_page
;
1458 if (page
== target
) {
1459 up_read(&io
->io_rwsem
);
1464 up_read(&io
->io_rwsem
);
1468 void f2fs_wait_on_page_writeback(struct page
*page
,
1469 enum page_type type
)
1471 if (PageWriteback(page
)) {
1472 struct f2fs_sb_info
*sbi
= F2FS_P_SB(page
);
1474 if (is_merged_page(sbi
, page
, type
))
1475 f2fs_submit_merged_bio(sbi
, type
, WRITE
);
1476 wait_on_page_writeback(page
);
1480 void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info
*sbi
,
1485 if (blkaddr
== NEW_ADDR
)
1488 f2fs_bug_on(sbi
, blkaddr
== NULL_ADDR
);
1490 cpage
= find_lock_page(META_MAPPING(sbi
), blkaddr
);
1492 f2fs_wait_on_page_writeback(cpage
, DATA
);
1493 f2fs_put_page(cpage
, 1);
1497 static int read_compacted_summaries(struct f2fs_sb_info
*sbi
)
1499 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1500 struct curseg_info
*seg_i
;
1501 unsigned char *kaddr
;
1506 start
= start_sum_block(sbi
);
1508 page
= get_meta_page(sbi
, start
++);
1509 kaddr
= (unsigned char *)page_address(page
);
1511 /* Step 1: restore nat cache */
1512 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1513 memcpy(&seg_i
->sum_blk
->n_nats
, kaddr
, SUM_JOURNAL_SIZE
);
1515 /* Step 2: restore sit cache */
1516 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1517 memcpy(&seg_i
->sum_blk
->n_sits
, kaddr
+ SUM_JOURNAL_SIZE
,
1519 offset
= 2 * SUM_JOURNAL_SIZE
;
1521 /* Step 3: restore summary entries */
1522 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1523 unsigned short blk_off
;
1526 seg_i
= CURSEG_I(sbi
, i
);
1527 segno
= le32_to_cpu(ckpt
->cur_data_segno
[i
]);
1528 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[i
]);
1529 seg_i
->next_segno
= segno
;
1530 reset_curseg(sbi
, i
, 0);
1531 seg_i
->alloc_type
= ckpt
->alloc_type
[i
];
1532 seg_i
->next_blkoff
= blk_off
;
1534 if (seg_i
->alloc_type
== SSR
)
1535 blk_off
= sbi
->blocks_per_seg
;
1537 for (j
= 0; j
< blk_off
; j
++) {
1538 struct f2fs_summary
*s
;
1539 s
= (struct f2fs_summary
*)(kaddr
+ offset
);
1540 seg_i
->sum_blk
->entries
[j
] = *s
;
1541 offset
+= SUMMARY_SIZE
;
1542 if (offset
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1546 f2fs_put_page(page
, 1);
1549 page
= get_meta_page(sbi
, start
++);
1550 kaddr
= (unsigned char *)page_address(page
);
1554 f2fs_put_page(page
, 1);
1558 static int read_normal_summaries(struct f2fs_sb_info
*sbi
, int type
)
1560 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1561 struct f2fs_summary_block
*sum
;
1562 struct curseg_info
*curseg
;
1564 unsigned short blk_off
;
1565 unsigned int segno
= 0;
1566 block_t blk_addr
= 0;
1568 /* get segment number and block addr */
1569 if (IS_DATASEG(type
)) {
1570 segno
= le32_to_cpu(ckpt
->cur_data_segno
[type
]);
1571 blk_off
= le16_to_cpu(ckpt
->cur_data_blkoff
[type
-
1573 if (__exist_node_summaries(sbi
))
1574 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
);
1576 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_DATA_TYPE
, type
);
1578 segno
= le32_to_cpu(ckpt
->cur_node_segno
[type
-
1580 blk_off
= le16_to_cpu(ckpt
->cur_node_blkoff
[type
-
1582 if (__exist_node_summaries(sbi
))
1583 blk_addr
= sum_blk_addr(sbi
, NR_CURSEG_NODE_TYPE
,
1584 type
- CURSEG_HOT_NODE
);
1586 blk_addr
= GET_SUM_BLOCK(sbi
, segno
);
1589 new = get_meta_page(sbi
, blk_addr
);
1590 sum
= (struct f2fs_summary_block
*)page_address(new);
1592 if (IS_NODESEG(type
)) {
1593 if (__exist_node_summaries(sbi
)) {
1594 struct f2fs_summary
*ns
= &sum
->entries
[0];
1596 for (i
= 0; i
< sbi
->blocks_per_seg
; i
++, ns
++) {
1598 ns
->ofs_in_node
= 0;
1603 err
= restore_node_summary(sbi
, segno
, sum
);
1605 f2fs_put_page(new, 1);
1611 /* set uncompleted segment to curseg */
1612 curseg
= CURSEG_I(sbi
, type
);
1613 mutex_lock(&curseg
->curseg_mutex
);
1614 memcpy(curseg
->sum_blk
, sum
, PAGE_CACHE_SIZE
);
1615 curseg
->next_segno
= segno
;
1616 reset_curseg(sbi
, type
, 0);
1617 curseg
->alloc_type
= ckpt
->alloc_type
[type
];
1618 curseg
->next_blkoff
= blk_off
;
1619 mutex_unlock(&curseg
->curseg_mutex
);
1620 f2fs_put_page(new, 1);
1624 static int restore_curseg_summaries(struct f2fs_sb_info
*sbi
)
1626 int type
= CURSEG_HOT_DATA
;
1629 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
)) {
1630 int npages
= npages_for_summary_flush(sbi
, true);
1633 ra_meta_pages(sbi
, start_sum_block(sbi
), npages
,
1636 /* restore for compacted data summary */
1637 if (read_compacted_summaries(sbi
))
1639 type
= CURSEG_HOT_NODE
;
1642 if (__exist_node_summaries(sbi
))
1643 ra_meta_pages(sbi
, sum_blk_addr(sbi
, NR_CURSEG_TYPE
, type
),
1644 NR_CURSEG_TYPE
- type
, META_CP
, true);
1646 for (; type
<= CURSEG_COLD_NODE
; type
++) {
1647 err
= read_normal_summaries(sbi
, type
);
1655 static void write_compacted_summaries(struct f2fs_sb_info
*sbi
, block_t blkaddr
)
1658 unsigned char *kaddr
;
1659 struct f2fs_summary
*summary
;
1660 struct curseg_info
*seg_i
;
1661 int written_size
= 0;
1664 page
= grab_meta_page(sbi
, blkaddr
++);
1665 kaddr
= (unsigned char *)page_address(page
);
1667 /* Step 1: write nat cache */
1668 seg_i
= CURSEG_I(sbi
, CURSEG_HOT_DATA
);
1669 memcpy(kaddr
, &seg_i
->sum_blk
->n_nats
, SUM_JOURNAL_SIZE
);
1670 written_size
+= SUM_JOURNAL_SIZE
;
1672 /* Step 2: write sit cache */
1673 seg_i
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1674 memcpy(kaddr
+ written_size
, &seg_i
->sum_blk
->n_sits
,
1676 written_size
+= SUM_JOURNAL_SIZE
;
1678 /* Step 3: write summary entries */
1679 for (i
= CURSEG_HOT_DATA
; i
<= CURSEG_COLD_DATA
; i
++) {
1680 unsigned short blkoff
;
1681 seg_i
= CURSEG_I(sbi
, i
);
1682 if (sbi
->ckpt
->alloc_type
[i
] == SSR
)
1683 blkoff
= sbi
->blocks_per_seg
;
1685 blkoff
= curseg_blkoff(sbi
, i
);
1687 for (j
= 0; j
< blkoff
; j
++) {
1689 page
= grab_meta_page(sbi
, blkaddr
++);
1690 kaddr
= (unsigned char *)page_address(page
);
1693 summary
= (struct f2fs_summary
*)(kaddr
+ written_size
);
1694 *summary
= seg_i
->sum_blk
->entries
[j
];
1695 written_size
+= SUMMARY_SIZE
;
1697 if (written_size
+ SUMMARY_SIZE
<= PAGE_CACHE_SIZE
-
1701 set_page_dirty(page
);
1702 f2fs_put_page(page
, 1);
1707 set_page_dirty(page
);
1708 f2fs_put_page(page
, 1);
1712 static void write_normal_summaries(struct f2fs_sb_info
*sbi
,
1713 block_t blkaddr
, int type
)
1716 if (IS_DATASEG(type
))
1717 end
= type
+ NR_CURSEG_DATA_TYPE
;
1719 end
= type
+ NR_CURSEG_NODE_TYPE
;
1721 for (i
= type
; i
< end
; i
++) {
1722 struct curseg_info
*sum
= CURSEG_I(sbi
, i
);
1723 mutex_lock(&sum
->curseg_mutex
);
1724 write_sum_page(sbi
, sum
->sum_blk
, blkaddr
+ (i
- type
));
1725 mutex_unlock(&sum
->curseg_mutex
);
1729 void write_data_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1731 if (is_set_ckpt_flags(F2FS_CKPT(sbi
), CP_COMPACT_SUM_FLAG
))
1732 write_compacted_summaries(sbi
, start_blk
);
1734 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_DATA
);
1737 void write_node_summaries(struct f2fs_sb_info
*sbi
, block_t start_blk
)
1739 write_normal_summaries(sbi
, start_blk
, CURSEG_HOT_NODE
);
1742 int lookup_journal_in_cursum(struct f2fs_summary_block
*sum
, int type
,
1743 unsigned int val
, int alloc
)
1747 if (type
== NAT_JOURNAL
) {
1748 for (i
= 0; i
< nats_in_cursum(sum
); i
++) {
1749 if (le32_to_cpu(nid_in_journal(sum
, i
)) == val
)
1752 if (alloc
&& nats_in_cursum(sum
) < NAT_JOURNAL_ENTRIES
)
1753 return update_nats_in_cursum(sum
, 1);
1754 } else if (type
== SIT_JOURNAL
) {
1755 for (i
= 0; i
< sits_in_cursum(sum
); i
++)
1756 if (le32_to_cpu(segno_in_journal(sum
, i
)) == val
)
1758 if (alloc
&& sits_in_cursum(sum
) < SIT_JOURNAL_ENTRIES
)
1759 return update_sits_in_cursum(sum
, 1);
1764 static struct page
*get_current_sit_page(struct f2fs_sb_info
*sbi
,
1767 return get_meta_page(sbi
, current_sit_addr(sbi
, segno
));
1770 static struct page
*get_next_sit_page(struct f2fs_sb_info
*sbi
,
1773 struct sit_info
*sit_i
= SIT_I(sbi
);
1774 struct page
*src_page
, *dst_page
;
1775 pgoff_t src_off
, dst_off
;
1776 void *src_addr
, *dst_addr
;
1778 src_off
= current_sit_addr(sbi
, start
);
1779 dst_off
= next_sit_addr(sbi
, src_off
);
1781 /* get current sit block page without lock */
1782 src_page
= get_meta_page(sbi
, src_off
);
1783 dst_page
= grab_meta_page(sbi
, dst_off
);
1784 f2fs_bug_on(sbi
, PageDirty(src_page
));
1786 src_addr
= page_address(src_page
);
1787 dst_addr
= page_address(dst_page
);
1788 memcpy(dst_addr
, src_addr
, PAGE_CACHE_SIZE
);
1790 set_page_dirty(dst_page
);
1791 f2fs_put_page(src_page
, 1);
1793 set_to_next_sit(sit_i
, start
);
1798 static struct sit_entry_set
*grab_sit_entry_set(void)
1800 struct sit_entry_set
*ses
=
1801 f2fs_kmem_cache_alloc(sit_entry_set_slab
, GFP_NOFS
);
1804 INIT_LIST_HEAD(&ses
->set_list
);
1808 static void release_sit_entry_set(struct sit_entry_set
*ses
)
1810 list_del(&ses
->set_list
);
1811 kmem_cache_free(sit_entry_set_slab
, ses
);
1814 static void adjust_sit_entry_set(struct sit_entry_set
*ses
,
1815 struct list_head
*head
)
1817 struct sit_entry_set
*next
= ses
;
1819 if (list_is_last(&ses
->set_list
, head
))
1822 list_for_each_entry_continue(next
, head
, set_list
)
1823 if (ses
->entry_cnt
<= next
->entry_cnt
)
1826 list_move_tail(&ses
->set_list
, &next
->set_list
);
1829 static void add_sit_entry(unsigned int segno
, struct list_head
*head
)
1831 struct sit_entry_set
*ses
;
1832 unsigned int start_segno
= START_SEGNO(segno
);
1834 list_for_each_entry(ses
, head
, set_list
) {
1835 if (ses
->start_segno
== start_segno
) {
1837 adjust_sit_entry_set(ses
, head
);
1842 ses
= grab_sit_entry_set();
1844 ses
->start_segno
= start_segno
;
1846 list_add(&ses
->set_list
, head
);
1849 static void add_sits_in_set(struct f2fs_sb_info
*sbi
)
1851 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
1852 struct list_head
*set_list
= &sm_info
->sit_entry_set
;
1853 unsigned long *bitmap
= SIT_I(sbi
)->dirty_sentries_bitmap
;
1856 for_each_set_bit(segno
, bitmap
, MAIN_SEGS(sbi
))
1857 add_sit_entry(segno
, set_list
);
1860 static void remove_sits_in_journal(struct f2fs_sb_info
*sbi
)
1862 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1863 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1866 for (i
= sits_in_cursum(sum
) - 1; i
>= 0; i
--) {
1870 segno
= le32_to_cpu(segno_in_journal(sum
, i
));
1871 dirtied
= __mark_sit_entry_dirty(sbi
, segno
);
1874 add_sit_entry(segno
, &SM_I(sbi
)->sit_entry_set
);
1876 update_sits_in_cursum(sum
, -sits_in_cursum(sum
));
1880 * CP calls this function, which flushes SIT entries including sit_journal,
1881 * and moves prefree segs to free segs.
1883 void flush_sit_entries(struct f2fs_sb_info
*sbi
, struct cp_control
*cpc
)
1885 struct sit_info
*sit_i
= SIT_I(sbi
);
1886 unsigned long *bitmap
= sit_i
->dirty_sentries_bitmap
;
1887 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
1888 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
1889 struct sit_entry_set
*ses
, *tmp
;
1890 struct list_head
*head
= &SM_I(sbi
)->sit_entry_set
;
1891 bool to_journal
= true;
1892 struct seg_entry
*se
;
1894 mutex_lock(&curseg
->curseg_mutex
);
1895 mutex_lock(&sit_i
->sentry_lock
);
1897 if (!sit_i
->dirty_sentries
)
1901 * add and account sit entries of dirty bitmap in sit entry
1904 add_sits_in_set(sbi
);
1907 * if there are no enough space in journal to store dirty sit
1908 * entries, remove all entries from journal and add and account
1909 * them in sit entry set.
1911 if (!__has_cursum_space(sum
, sit_i
->dirty_sentries
, SIT_JOURNAL
))
1912 remove_sits_in_journal(sbi
);
1915 * there are two steps to flush sit entries:
1916 * #1, flush sit entries to journal in current cold data summary block.
1917 * #2, flush sit entries to sit page.
1919 list_for_each_entry_safe(ses
, tmp
, head
, set_list
) {
1920 struct page
*page
= NULL
;
1921 struct f2fs_sit_block
*raw_sit
= NULL
;
1922 unsigned int start_segno
= ses
->start_segno
;
1923 unsigned int end
= min(start_segno
+ SIT_ENTRY_PER_BLOCK
,
1924 (unsigned long)MAIN_SEGS(sbi
));
1925 unsigned int segno
= start_segno
;
1928 !__has_cursum_space(sum
, ses
->entry_cnt
, SIT_JOURNAL
))
1932 page
= get_next_sit_page(sbi
, start_segno
);
1933 raw_sit
= page_address(page
);
1936 /* flush dirty sit entries in region of current sit set */
1937 for_each_set_bit_from(segno
, bitmap
, end
) {
1938 int offset
, sit_offset
;
1940 se
= get_seg_entry(sbi
, segno
);
1942 /* add discard candidates */
1943 if (cpc
->reason
!= CP_DISCARD
) {
1944 cpc
->trim_start
= segno
;
1945 add_discard_addrs(sbi
, cpc
);
1949 offset
= lookup_journal_in_cursum(sum
,
1950 SIT_JOURNAL
, segno
, 1);
1951 f2fs_bug_on(sbi
, offset
< 0);
1952 segno_in_journal(sum
, offset
) =
1954 seg_info_to_raw_sit(se
,
1955 &sit_in_journal(sum
, offset
));
1957 sit_offset
= SIT_ENTRY_OFFSET(sit_i
, segno
);
1958 seg_info_to_raw_sit(se
,
1959 &raw_sit
->entries
[sit_offset
]);
1962 __clear_bit(segno
, bitmap
);
1963 sit_i
->dirty_sentries
--;
1968 f2fs_put_page(page
, 1);
1970 f2fs_bug_on(sbi
, ses
->entry_cnt
);
1971 release_sit_entry_set(ses
);
1974 f2fs_bug_on(sbi
, !list_empty(head
));
1975 f2fs_bug_on(sbi
, sit_i
->dirty_sentries
);
1977 if (cpc
->reason
== CP_DISCARD
) {
1978 for (; cpc
->trim_start
<= cpc
->trim_end
; cpc
->trim_start
++)
1979 add_discard_addrs(sbi
, cpc
);
1981 mutex_unlock(&sit_i
->sentry_lock
);
1982 mutex_unlock(&curseg
->curseg_mutex
);
1984 set_prefree_as_free_segments(sbi
);
1987 static int build_sit_info(struct f2fs_sb_info
*sbi
)
1989 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
1990 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
1991 struct sit_info
*sit_i
;
1992 unsigned int sit_segs
, start
;
1993 char *src_bitmap
, *dst_bitmap
;
1994 unsigned int bitmap_size
;
1996 /* allocate memory for SIT information */
1997 sit_i
= kzalloc(sizeof(struct sit_info
), GFP_KERNEL
);
2001 SM_I(sbi
)->sit_info
= sit_i
;
2003 sit_i
->sentries
= f2fs_kvzalloc(MAIN_SEGS(sbi
) *
2004 sizeof(struct seg_entry
), GFP_KERNEL
);
2005 if (!sit_i
->sentries
)
2008 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
2009 sit_i
->dirty_sentries_bitmap
= f2fs_kvzalloc(bitmap_size
, GFP_KERNEL
);
2010 if (!sit_i
->dirty_sentries_bitmap
)
2013 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
2014 sit_i
->sentries
[start
].cur_valid_map
2015 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2016 sit_i
->sentries
[start
].ckpt_valid_map
2017 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2018 sit_i
->sentries
[start
].discard_map
2019 = kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2020 if (!sit_i
->sentries
[start
].cur_valid_map
||
2021 !sit_i
->sentries
[start
].ckpt_valid_map
||
2022 !sit_i
->sentries
[start
].discard_map
)
2026 sit_i
->tmp_map
= kzalloc(SIT_VBLOCK_MAP_SIZE
, GFP_KERNEL
);
2027 if (!sit_i
->tmp_map
)
2030 if (sbi
->segs_per_sec
> 1) {
2031 sit_i
->sec_entries
= f2fs_kvzalloc(MAIN_SECS(sbi
) *
2032 sizeof(struct sec_entry
), GFP_KERNEL
);
2033 if (!sit_i
->sec_entries
)
2037 /* get information related with SIT */
2038 sit_segs
= le32_to_cpu(raw_super
->segment_count_sit
) >> 1;
2040 /* setup SIT bitmap from ckeckpoint pack */
2041 bitmap_size
= __bitmap_size(sbi
, SIT_BITMAP
);
2042 src_bitmap
= __bitmap_ptr(sbi
, SIT_BITMAP
);
2044 dst_bitmap
= kmemdup(src_bitmap
, bitmap_size
, GFP_KERNEL
);
2048 /* init SIT information */
2049 sit_i
->s_ops
= &default_salloc_ops
;
2051 sit_i
->sit_base_addr
= le32_to_cpu(raw_super
->sit_blkaddr
);
2052 sit_i
->sit_blocks
= sit_segs
<< sbi
->log_blocks_per_seg
;
2053 sit_i
->written_valid_blocks
= le64_to_cpu(ckpt
->valid_block_count
);
2054 sit_i
->sit_bitmap
= dst_bitmap
;
2055 sit_i
->bitmap_size
= bitmap_size
;
2056 sit_i
->dirty_sentries
= 0;
2057 sit_i
->sents_per_block
= SIT_ENTRY_PER_BLOCK
;
2058 sit_i
->elapsed_time
= le64_to_cpu(sbi
->ckpt
->elapsed_time
);
2059 sit_i
->mounted_time
= CURRENT_TIME_SEC
.tv_sec
;
2060 mutex_init(&sit_i
->sentry_lock
);
2064 static int build_free_segmap(struct f2fs_sb_info
*sbi
)
2066 struct free_segmap_info
*free_i
;
2067 unsigned int bitmap_size
, sec_bitmap_size
;
2069 /* allocate memory for free segmap information */
2070 free_i
= kzalloc(sizeof(struct free_segmap_info
), GFP_KERNEL
);
2074 SM_I(sbi
)->free_info
= free_i
;
2076 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
2077 free_i
->free_segmap
= f2fs_kvmalloc(bitmap_size
, GFP_KERNEL
);
2078 if (!free_i
->free_segmap
)
2081 sec_bitmap_size
= f2fs_bitmap_size(MAIN_SECS(sbi
));
2082 free_i
->free_secmap
= f2fs_kvmalloc(sec_bitmap_size
, GFP_KERNEL
);
2083 if (!free_i
->free_secmap
)
2086 /* set all segments as dirty temporarily */
2087 memset(free_i
->free_segmap
, 0xff, bitmap_size
);
2088 memset(free_i
->free_secmap
, 0xff, sec_bitmap_size
);
2090 /* init free segmap information */
2091 free_i
->start_segno
= GET_SEGNO_FROM_SEG0(sbi
, MAIN_BLKADDR(sbi
));
2092 free_i
->free_segments
= 0;
2093 free_i
->free_sections
= 0;
2094 spin_lock_init(&free_i
->segmap_lock
);
2098 static int build_curseg(struct f2fs_sb_info
*sbi
)
2100 struct curseg_info
*array
;
2103 array
= kcalloc(NR_CURSEG_TYPE
, sizeof(*array
), GFP_KERNEL
);
2107 SM_I(sbi
)->curseg_array
= array
;
2109 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++) {
2110 mutex_init(&array
[i
].curseg_mutex
);
2111 array
[i
].sum_blk
= kzalloc(PAGE_CACHE_SIZE
, GFP_KERNEL
);
2112 if (!array
[i
].sum_blk
)
2114 array
[i
].segno
= NULL_SEGNO
;
2115 array
[i
].next_blkoff
= 0;
2117 return restore_curseg_summaries(sbi
);
2120 static void build_sit_entries(struct f2fs_sb_info
*sbi
)
2122 struct sit_info
*sit_i
= SIT_I(sbi
);
2123 struct curseg_info
*curseg
= CURSEG_I(sbi
, CURSEG_COLD_DATA
);
2124 struct f2fs_summary_block
*sum
= curseg
->sum_blk
;
2125 int sit_blk_cnt
= SIT_BLK_CNT(sbi
);
2126 unsigned int i
, start
, end
;
2127 unsigned int readed
, start_blk
= 0;
2128 int nrpages
= MAX_BIO_BLOCKS(sbi
);
2131 readed
= ra_meta_pages(sbi
, start_blk
, nrpages
, META_SIT
, true);
2133 start
= start_blk
* sit_i
->sents_per_block
;
2134 end
= (start_blk
+ readed
) * sit_i
->sents_per_block
;
2136 for (; start
< end
&& start
< MAIN_SEGS(sbi
); start
++) {
2137 struct seg_entry
*se
= &sit_i
->sentries
[start
];
2138 struct f2fs_sit_block
*sit_blk
;
2139 struct f2fs_sit_entry sit
;
2142 mutex_lock(&curseg
->curseg_mutex
);
2143 for (i
= 0; i
< sits_in_cursum(sum
); i
++) {
2144 if (le32_to_cpu(segno_in_journal(sum
, i
))
2146 sit
= sit_in_journal(sum
, i
);
2147 mutex_unlock(&curseg
->curseg_mutex
);
2151 mutex_unlock(&curseg
->curseg_mutex
);
2153 page
= get_current_sit_page(sbi
, start
);
2154 sit_blk
= (struct f2fs_sit_block
*)page_address(page
);
2155 sit
= sit_blk
->entries
[SIT_ENTRY_OFFSET(sit_i
, start
)];
2156 f2fs_put_page(page
, 1);
2158 check_block_count(sbi
, start
, &sit
);
2159 seg_info_from_raw_sit(se
, &sit
);
2161 /* build discard map only one time */
2162 memcpy(se
->discard_map
, se
->cur_valid_map
, SIT_VBLOCK_MAP_SIZE
);
2163 sbi
->discard_blks
+= sbi
->blocks_per_seg
- se
->valid_blocks
;
2165 if (sbi
->segs_per_sec
> 1) {
2166 struct sec_entry
*e
= get_sec_entry(sbi
, start
);
2167 e
->valid_blocks
+= se
->valid_blocks
;
2170 start_blk
+= readed
;
2171 } while (start_blk
< sit_blk_cnt
);
2174 static void init_free_segmap(struct f2fs_sb_info
*sbi
)
2179 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
2180 struct seg_entry
*sentry
= get_seg_entry(sbi
, start
);
2181 if (!sentry
->valid_blocks
)
2182 __set_free(sbi
, start
);
2185 /* set use the current segments */
2186 for (type
= CURSEG_HOT_DATA
; type
<= CURSEG_COLD_NODE
; type
++) {
2187 struct curseg_info
*curseg_t
= CURSEG_I(sbi
, type
);
2188 __set_test_and_inuse(sbi
, curseg_t
->segno
);
2192 static void init_dirty_segmap(struct f2fs_sb_info
*sbi
)
2194 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2195 struct free_segmap_info
*free_i
= FREE_I(sbi
);
2196 unsigned int segno
= 0, offset
= 0;
2197 unsigned short valid_blocks
;
2200 /* find dirty segment based on free segmap */
2201 segno
= find_next_inuse(free_i
, MAIN_SEGS(sbi
), offset
);
2202 if (segno
>= MAIN_SEGS(sbi
))
2205 valid_blocks
= get_valid_blocks(sbi
, segno
, 0);
2206 if (valid_blocks
== sbi
->blocks_per_seg
|| !valid_blocks
)
2208 if (valid_blocks
> sbi
->blocks_per_seg
) {
2209 f2fs_bug_on(sbi
, 1);
2212 mutex_lock(&dirty_i
->seglist_lock
);
2213 __locate_dirty_segment(sbi
, segno
, DIRTY
);
2214 mutex_unlock(&dirty_i
->seglist_lock
);
2218 static int init_victim_secmap(struct f2fs_sb_info
*sbi
)
2220 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2221 unsigned int bitmap_size
= f2fs_bitmap_size(MAIN_SECS(sbi
));
2223 dirty_i
->victim_secmap
= f2fs_kvzalloc(bitmap_size
, GFP_KERNEL
);
2224 if (!dirty_i
->victim_secmap
)
2229 static int build_dirty_segmap(struct f2fs_sb_info
*sbi
)
2231 struct dirty_seglist_info
*dirty_i
;
2232 unsigned int bitmap_size
, i
;
2234 /* allocate memory for dirty segments list information */
2235 dirty_i
= kzalloc(sizeof(struct dirty_seglist_info
), GFP_KERNEL
);
2239 SM_I(sbi
)->dirty_info
= dirty_i
;
2240 mutex_init(&dirty_i
->seglist_lock
);
2242 bitmap_size
= f2fs_bitmap_size(MAIN_SEGS(sbi
));
2244 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++) {
2245 dirty_i
->dirty_segmap
[i
] = f2fs_kvzalloc(bitmap_size
, GFP_KERNEL
);
2246 if (!dirty_i
->dirty_segmap
[i
])
2250 init_dirty_segmap(sbi
);
2251 return init_victim_secmap(sbi
);
2255 * Update min, max modified time for cost-benefit GC algorithm
2257 static void init_min_max_mtime(struct f2fs_sb_info
*sbi
)
2259 struct sit_info
*sit_i
= SIT_I(sbi
);
2262 mutex_lock(&sit_i
->sentry_lock
);
2264 sit_i
->min_mtime
= LLONG_MAX
;
2266 for (segno
= 0; segno
< MAIN_SEGS(sbi
); segno
+= sbi
->segs_per_sec
) {
2268 unsigned long long mtime
= 0;
2270 for (i
= 0; i
< sbi
->segs_per_sec
; i
++)
2271 mtime
+= get_seg_entry(sbi
, segno
+ i
)->mtime
;
2273 mtime
= div_u64(mtime
, sbi
->segs_per_sec
);
2275 if (sit_i
->min_mtime
> mtime
)
2276 sit_i
->min_mtime
= mtime
;
2278 sit_i
->max_mtime
= get_mtime(sbi
);
2279 mutex_unlock(&sit_i
->sentry_lock
);
2282 int build_segment_manager(struct f2fs_sb_info
*sbi
)
2284 struct f2fs_super_block
*raw_super
= F2FS_RAW_SUPER(sbi
);
2285 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
2286 struct f2fs_sm_info
*sm_info
;
2289 sm_info
= kzalloc(sizeof(struct f2fs_sm_info
), GFP_KERNEL
);
2294 sbi
->sm_info
= sm_info
;
2295 sm_info
->seg0_blkaddr
= le32_to_cpu(raw_super
->segment0_blkaddr
);
2296 sm_info
->main_blkaddr
= le32_to_cpu(raw_super
->main_blkaddr
);
2297 sm_info
->segment_count
= le32_to_cpu(raw_super
->segment_count
);
2298 sm_info
->reserved_segments
= le32_to_cpu(ckpt
->rsvd_segment_count
);
2299 sm_info
->ovp_segments
= le32_to_cpu(ckpt
->overprov_segment_count
);
2300 sm_info
->main_segments
= le32_to_cpu(raw_super
->segment_count_main
);
2301 sm_info
->ssa_blkaddr
= le32_to_cpu(raw_super
->ssa_blkaddr
);
2302 sm_info
->rec_prefree_segments
= sm_info
->main_segments
*
2303 DEF_RECLAIM_PREFREE_SEGMENTS
/ 100;
2304 sm_info
->ipu_policy
= 1 << F2FS_IPU_FSYNC
;
2305 sm_info
->min_ipu_util
= DEF_MIN_IPU_UTIL
;
2306 sm_info
->min_fsync_blocks
= DEF_MIN_FSYNC_BLOCKS
;
2308 INIT_LIST_HEAD(&sm_info
->discard_list
);
2309 sm_info
->nr_discards
= 0;
2310 sm_info
->max_discards
= 0;
2312 sm_info
->trim_sections
= DEF_BATCHED_TRIM_SECTIONS
;
2314 INIT_LIST_HEAD(&sm_info
->sit_entry_set
);
2316 if (test_opt(sbi
, FLUSH_MERGE
) && !f2fs_readonly(sbi
->sb
)) {
2317 err
= create_flush_cmd_control(sbi
);
2322 err
= build_sit_info(sbi
);
2325 err
= build_free_segmap(sbi
);
2328 err
= build_curseg(sbi
);
2332 /* reinit free segmap based on SIT */
2333 build_sit_entries(sbi
);
2335 init_free_segmap(sbi
);
2336 err
= build_dirty_segmap(sbi
);
2340 init_min_max_mtime(sbi
);
2344 static void discard_dirty_segmap(struct f2fs_sb_info
*sbi
,
2345 enum dirty_type dirty_type
)
2347 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2349 mutex_lock(&dirty_i
->seglist_lock
);
2350 kvfree(dirty_i
->dirty_segmap
[dirty_type
]);
2351 dirty_i
->nr_dirty
[dirty_type
] = 0;
2352 mutex_unlock(&dirty_i
->seglist_lock
);
2355 static void destroy_victim_secmap(struct f2fs_sb_info
*sbi
)
2357 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2358 kvfree(dirty_i
->victim_secmap
);
2361 static void destroy_dirty_segmap(struct f2fs_sb_info
*sbi
)
2363 struct dirty_seglist_info
*dirty_i
= DIRTY_I(sbi
);
2369 /* discard pre-free/dirty segments list */
2370 for (i
= 0; i
< NR_DIRTY_TYPE
; i
++)
2371 discard_dirty_segmap(sbi
, i
);
2373 destroy_victim_secmap(sbi
);
2374 SM_I(sbi
)->dirty_info
= NULL
;
2378 static void destroy_curseg(struct f2fs_sb_info
*sbi
)
2380 struct curseg_info
*array
= SM_I(sbi
)->curseg_array
;
2385 SM_I(sbi
)->curseg_array
= NULL
;
2386 for (i
= 0; i
< NR_CURSEG_TYPE
; i
++)
2387 kfree(array
[i
].sum_blk
);
2391 static void destroy_free_segmap(struct f2fs_sb_info
*sbi
)
2393 struct free_segmap_info
*free_i
= SM_I(sbi
)->free_info
;
2396 SM_I(sbi
)->free_info
= NULL
;
2397 kvfree(free_i
->free_segmap
);
2398 kvfree(free_i
->free_secmap
);
2402 static void destroy_sit_info(struct f2fs_sb_info
*sbi
)
2404 struct sit_info
*sit_i
= SIT_I(sbi
);
2410 if (sit_i
->sentries
) {
2411 for (start
= 0; start
< MAIN_SEGS(sbi
); start
++) {
2412 kfree(sit_i
->sentries
[start
].cur_valid_map
);
2413 kfree(sit_i
->sentries
[start
].ckpt_valid_map
);
2414 kfree(sit_i
->sentries
[start
].discard_map
);
2417 kfree(sit_i
->tmp_map
);
2419 kvfree(sit_i
->sentries
);
2420 kvfree(sit_i
->sec_entries
);
2421 kvfree(sit_i
->dirty_sentries_bitmap
);
2423 SM_I(sbi
)->sit_info
= NULL
;
2424 kfree(sit_i
->sit_bitmap
);
2428 void destroy_segment_manager(struct f2fs_sb_info
*sbi
)
2430 struct f2fs_sm_info
*sm_info
= SM_I(sbi
);
2434 destroy_flush_cmd_control(sbi
);
2435 destroy_dirty_segmap(sbi
);
2436 destroy_curseg(sbi
);
2437 destroy_free_segmap(sbi
);
2438 destroy_sit_info(sbi
);
2439 sbi
->sm_info
= NULL
;
2443 int __init
create_segment_manager_caches(void)
2445 discard_entry_slab
= f2fs_kmem_cache_create("discard_entry",
2446 sizeof(struct discard_entry
));
2447 if (!discard_entry_slab
)
2450 sit_entry_set_slab
= f2fs_kmem_cache_create("sit_entry_set",
2451 sizeof(struct sit_entry_set
));
2452 if (!sit_entry_set_slab
)
2453 goto destory_discard_entry
;
2455 inmem_entry_slab
= f2fs_kmem_cache_create("inmem_page_entry",
2456 sizeof(struct inmem_pages
));
2457 if (!inmem_entry_slab
)
2458 goto destroy_sit_entry_set
;
2461 destroy_sit_entry_set
:
2462 kmem_cache_destroy(sit_entry_set_slab
);
2463 destory_discard_entry
:
2464 kmem_cache_destroy(discard_entry_slab
);
2469 void destroy_segment_manager_caches(void)
2471 kmem_cache_destroy(sit_entry_set_slab
);
2472 kmem_cache_destroy(discard_entry_slab
);
2473 kmem_cache_destroy(inmem_entry_slab
);