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