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