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