1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/segment.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
12 #include <linux/prefetch.h>
13 #include <linux/kthread.h>
14 #include <linux/swap.h>
15 #include <linux/timer.h>
16 #include <linux/freezer.h>
17 #include <linux/sched/signal.h>
18 #include <linux/random.h>
19
20 #include "f2fs.h"
21 #include "segment.h"
22 #include "node.h"
23 #include "gc.h"
24 #include "iostat.h"
25 #include <trace/events/f2fs.h>
26
27 #define __reverse_ffz(x) __reverse_ffs(~(x))
28
29 static struct kmem_cache *discard_entry_slab;
30 static struct kmem_cache *discard_cmd_slab;
31 static struct kmem_cache *sit_entry_set_slab;
32 static struct kmem_cache *revoke_entry_slab;
33
__reverse_ulong(unsigned char * str)34 static unsigned long __reverse_ulong(unsigned char *str)
35 {
36 unsigned long tmp = 0;
37 int shift = 24, idx = 0;
38
39 #if BITS_PER_LONG == 64
40 shift = 56;
41 #endif
42 while (shift >= 0) {
43 tmp |= (unsigned long)str[idx++] << shift;
44 shift -= BITS_PER_BYTE;
45 }
46 return tmp;
47 }
48
49 /*
50 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
51 * MSB and LSB are reversed in a byte by f2fs_set_bit.
52 */
__reverse_ffs(unsigned long word)53 static inline unsigned long __reverse_ffs(unsigned long word)
54 {
55 int num = 0;
56
57 #if BITS_PER_LONG == 64
58 if ((word & 0xffffffff00000000UL) == 0)
59 num += 32;
60 else
61 word >>= 32;
62 #endif
63 if ((word & 0xffff0000) == 0)
64 num += 16;
65 else
66 word >>= 16;
67
68 if ((word & 0xff00) == 0)
69 num += 8;
70 else
71 word >>= 8;
72
73 if ((word & 0xf0) == 0)
74 num += 4;
75 else
76 word >>= 4;
77
78 if ((word & 0xc) == 0)
79 num += 2;
80 else
81 word >>= 2;
82
83 if ((word & 0x2) == 0)
84 num += 1;
85 return num;
86 }
87
88 /*
89 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
90 * f2fs_set_bit makes MSB and LSB reversed in a byte.
91 * @size must be integral times of unsigned long.
92 * Example:
93 * MSB <--> LSB
94 * f2fs_set_bit(0, bitmap) => 1000 0000
95 * f2fs_set_bit(7, bitmap) => 0000 0001
96 */
__find_rev_next_bit(const unsigned long * addr,unsigned long size,unsigned long offset)97 static unsigned long __find_rev_next_bit(const unsigned long *addr,
98 unsigned long size, unsigned long offset)
99 {
100 const unsigned long *p = addr + BIT_WORD(offset);
101 unsigned long result = size;
102 unsigned long tmp;
103
104 if (offset >= size)
105 return size;
106
107 size -= (offset & ~(BITS_PER_LONG - 1));
108 offset %= BITS_PER_LONG;
109
110 while (1) {
111 if (*p == 0)
112 goto pass;
113
114 tmp = __reverse_ulong((unsigned char *)p);
115
116 tmp &= ~0UL >> offset;
117 if (size < BITS_PER_LONG)
118 tmp &= (~0UL << (BITS_PER_LONG - size));
119 if (tmp)
120 goto found;
121 pass:
122 if (size <= BITS_PER_LONG)
123 break;
124 size -= BITS_PER_LONG;
125 offset = 0;
126 p++;
127 }
128 return result;
129 found:
130 return result - size + __reverse_ffs(tmp);
131 }
132
__find_rev_next_zero_bit(const unsigned long * addr,unsigned long size,unsigned long offset)133 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
134 unsigned long size, unsigned long offset)
135 {
136 const unsigned long *p = addr + BIT_WORD(offset);
137 unsigned long result = size;
138 unsigned long tmp;
139
140 if (offset >= size)
141 return size;
142
143 size -= (offset & ~(BITS_PER_LONG - 1));
144 offset %= BITS_PER_LONG;
145
146 while (1) {
147 if (*p == ~0UL)
148 goto pass;
149
150 tmp = __reverse_ulong((unsigned char *)p);
151
152 if (offset)
153 tmp |= ~0UL << (BITS_PER_LONG - offset);
154 if (size < BITS_PER_LONG)
155 tmp |= ~0UL >> size;
156 if (tmp != ~0UL)
157 goto found;
158 pass:
159 if (size <= BITS_PER_LONG)
160 break;
161 size -= BITS_PER_LONG;
162 offset = 0;
163 p++;
164 }
165 return result;
166 found:
167 return result - size + __reverse_ffz(tmp);
168 }
169
f2fs_need_SSR(struct f2fs_sb_info * sbi)170 bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
171 {
172 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
173 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
174 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
175
176 if (f2fs_lfs_mode(sbi))
177 return false;
178 if (sbi->gc_mode == GC_URGENT_HIGH)
179 return true;
180 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
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
f2fs_abort_atomic_write(struct inode * inode,bool clean)187 void f2fs_abort_atomic_write(struct inode *inode, bool clean)
188 {
189 struct f2fs_inode_info *fi = F2FS_I(inode);
190
191 if (!f2fs_is_atomic_file(inode))
192 return;
193
194 release_atomic_write_cnt(inode);
195 clear_inode_flag(inode, FI_ATOMIC_COMMITTED);
196 clear_inode_flag(inode, FI_ATOMIC_REPLACE);
197 clear_inode_flag(inode, FI_ATOMIC_FILE);
198 stat_dec_atomic_inode(inode);
199
200 F2FS_I(inode)->atomic_write_task = NULL;
201
202 if (clean) {
203 truncate_inode_pages_final(inode->i_mapping);
204 f2fs_i_size_write(inode, fi->original_i_size);
205 fi->original_i_size = 0;
206 }
207 }
208
__replace_atomic_write_block(struct inode * inode,pgoff_t index,block_t new_addr,block_t * old_addr,bool recover)209 static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
210 block_t new_addr, block_t *old_addr, bool recover)
211 {
212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
213 struct dnode_of_data dn;
214 struct node_info ni;
215 int err;
216
217 retry:
218 set_new_dnode(&dn, inode, NULL, NULL, 0);
219 err = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
220 if (err) {
221 if (err == -ENOMEM) {
222 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
223 goto retry;
224 }
225 return err;
226 }
227
228 err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
229 if (err) {
230 f2fs_put_dnode(&dn);
231 return err;
232 }
233
234 if (recover) {
235 /* dn.data_blkaddr is always valid */
236 if (!__is_valid_data_blkaddr(new_addr)) {
237 if (new_addr == NULL_ADDR)
238 dec_valid_block_count(sbi, inode, 1);
239 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
240 f2fs_update_data_blkaddr(&dn, new_addr);
241 } else {
242 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
243 new_addr, ni.version, true, true);
244 }
245 } else {
246 blkcnt_t count = 1;
247
248 err = inc_valid_block_count(sbi, inode, &count);
249 if (err) {
250 f2fs_put_dnode(&dn);
251 return err;
252 }
253
254 *old_addr = dn.data_blkaddr;
255 f2fs_truncate_data_blocks_range(&dn, 1);
256 dec_valid_block_count(sbi, F2FS_I(inode)->cow_inode, count);
257
258 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr,
259 ni.version, true, false);
260 }
261
262 f2fs_put_dnode(&dn);
263
264 trace_f2fs_replace_atomic_write_block(inode, F2FS_I(inode)->cow_inode,
265 index, old_addr ? *old_addr : 0, new_addr, recover);
266 return 0;
267 }
268
__complete_revoke_list(struct inode * inode,struct list_head * head,bool revoke)269 static void __complete_revoke_list(struct inode *inode, struct list_head *head,
270 bool revoke)
271 {
272 struct revoke_entry *cur, *tmp;
273 pgoff_t start_index = 0;
274 bool truncate = is_inode_flag_set(inode, FI_ATOMIC_REPLACE);
275
276 list_for_each_entry_safe(cur, tmp, head, list) {
277 if (revoke) {
278 __replace_atomic_write_block(inode, cur->index,
279 cur->old_addr, NULL, true);
280 } else if (truncate) {
281 f2fs_truncate_hole(inode, start_index, cur->index);
282 start_index = cur->index + 1;
283 }
284
285 list_del(&cur->list);
286 kmem_cache_free(revoke_entry_slab, cur);
287 }
288
289 if (!revoke && truncate)
290 f2fs_do_truncate_blocks(inode, start_index * PAGE_SIZE, false);
291 }
292
__f2fs_commit_atomic_write(struct inode * inode)293 static int __f2fs_commit_atomic_write(struct inode *inode)
294 {
295 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
296 struct f2fs_inode_info *fi = F2FS_I(inode);
297 struct inode *cow_inode = fi->cow_inode;
298 struct revoke_entry *new;
299 struct list_head revoke_list;
300 block_t blkaddr;
301 struct dnode_of_data dn;
302 pgoff_t len = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
303 pgoff_t off = 0, blen, index;
304 int ret = 0, i;
305
306 INIT_LIST_HEAD(&revoke_list);
307
308 while (len) {
309 blen = min_t(pgoff_t, ADDRS_PER_BLOCK(cow_inode), len);
310
311 set_new_dnode(&dn, cow_inode, NULL, NULL, 0);
312 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
313 if (ret && ret != -ENOENT) {
314 goto out;
315 } else if (ret == -ENOENT) {
316 ret = 0;
317 if (dn.max_level == 0)
318 goto out;
319 goto next;
320 }
321
322 blen = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, cow_inode),
323 len);
324 index = off;
325 for (i = 0; i < blen; i++, dn.ofs_in_node++, index++) {
326 blkaddr = f2fs_data_blkaddr(&dn);
327
328 if (!__is_valid_data_blkaddr(blkaddr)) {
329 continue;
330 } else if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
331 DATA_GENERIC_ENHANCE)) {
332 f2fs_put_dnode(&dn);
333 ret = -EFSCORRUPTED;
334 f2fs_handle_error(sbi,
335 ERROR_INVALID_BLKADDR);
336 goto out;
337 }
338
339 new = f2fs_kmem_cache_alloc(revoke_entry_slab, GFP_NOFS,
340 true, NULL);
341
342 ret = __replace_atomic_write_block(inode, index, blkaddr,
343 &new->old_addr, false);
344 if (ret) {
345 f2fs_put_dnode(&dn);
346 kmem_cache_free(revoke_entry_slab, new);
347 goto out;
348 }
349
350 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
351 new->index = index;
352 list_add_tail(&new->list, &revoke_list);
353 }
354 f2fs_put_dnode(&dn);
355 next:
356 off += blen;
357 len -= blen;
358 }
359
360 out:
361 if (ret) {
362 sbi->revoked_atomic_block += fi->atomic_write_cnt;
363 } else {
364 sbi->committed_atomic_block += fi->atomic_write_cnt;
365 set_inode_flag(inode, FI_ATOMIC_COMMITTED);
366 }
367
368 __complete_revoke_list(inode, &revoke_list, ret ? true : false);
369
370 return ret;
371 }
372
f2fs_commit_atomic_write(struct inode * inode)373 int f2fs_commit_atomic_write(struct inode *inode)
374 {
375 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
376 struct f2fs_inode_info *fi = F2FS_I(inode);
377 int err;
378
379 err = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
380 if (err)
381 return err;
382
383 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
384 f2fs_lock_op(sbi);
385
386 err = __f2fs_commit_atomic_write(inode);
387
388 f2fs_unlock_op(sbi);
389 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
390
391 return err;
392 }
393
394 /*
395 * This function balances dirty node and dentry pages.
396 * In addition, it controls garbage collection.
397 */
f2fs_balance_fs(struct f2fs_sb_info * sbi,bool need)398 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
399 {
400 if (time_to_inject(sbi, FAULT_CHECKPOINT))
401 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_FAULT_INJECT);
402
403 /* balance_fs_bg is able to be pending */
404 if (need && excess_cached_nats(sbi))
405 f2fs_balance_fs_bg(sbi, false);
406
407 if (!f2fs_is_checkpoint_ready(sbi))
408 return;
409
410 /*
411 * We should do GC or end up with checkpoint, if there are so many dirty
412 * dir/node pages without enough free segments.
413 */
414 if (has_enough_free_secs(sbi, 0, 0))
415 return;
416
417 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
418 sbi->gc_thread->f2fs_gc_task) {
419 DEFINE_WAIT(wait);
420
421 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait,
422 TASK_UNINTERRUPTIBLE);
423 wake_up(&sbi->gc_thread->gc_wait_queue_head);
424 io_schedule();
425 finish_wait(&sbi->gc_thread->fggc_wq, &wait);
426 } else {
427 struct f2fs_gc_control gc_control = {
428 .victim_segno = NULL_SEGNO,
429 .init_gc_type = BG_GC,
430 .no_bg_gc = true,
431 .should_migrate_blocks = false,
432 .err_gc_skipped = false,
433 .nr_free_secs = 1 };
434 f2fs_down_write(&sbi->gc_lock);
435 f2fs_gc(sbi, &gc_control);
436 }
437 }
438
excess_dirty_threshold(struct f2fs_sb_info * sbi)439 static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi)
440 {
441 int factor = f2fs_rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2;
442 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS);
443 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
444 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES);
445 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META);
446 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
447 unsigned int threshold = sbi->blocks_per_seg * factor *
448 DEFAULT_DIRTY_THRESHOLD;
449 unsigned int global_threshold = threshold * 3 / 2;
450
451 if (dents >= threshold || qdata >= threshold ||
452 nodes >= threshold || meta >= threshold ||
453 imeta >= threshold)
454 return true;
455 return dents + qdata + nodes + meta + imeta > global_threshold;
456 }
457
f2fs_balance_fs_bg(struct f2fs_sb_info * sbi,bool from_bg)458 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
459 {
460 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
461 return;
462
463 /* try to shrink extent cache when there is no enough memory */
464 if (!f2fs_available_free_memory(sbi, READ_EXTENT_CACHE))
465 f2fs_shrink_read_extent_tree(sbi,
466 READ_EXTENT_CACHE_SHRINK_NUMBER);
467
468 /* try to shrink age extent cache when there is no enough memory */
469 if (!f2fs_available_free_memory(sbi, AGE_EXTENT_CACHE))
470 f2fs_shrink_age_extent_tree(sbi,
471 AGE_EXTENT_CACHE_SHRINK_NUMBER);
472
473 /* check the # of cached NAT entries */
474 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
475 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
476
477 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
478 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
479 else
480 f2fs_build_free_nids(sbi, false, false);
481
482 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) ||
483 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi))
484 goto do_sync;
485
486 /* there is background inflight IO or foreground operation recently */
487 if (is_inflight_io(sbi, REQ_TIME) ||
488 (!f2fs_time_over(sbi, REQ_TIME) && f2fs_rwsem_is_locked(&sbi->cp_rwsem)))
489 return;
490
491 /* exceed periodical checkpoint timeout threshold */
492 if (f2fs_time_over(sbi, CP_TIME))
493 goto do_sync;
494
495 /* checkpoint is the only way to shrink partial cached entries */
496 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) &&
497 f2fs_available_free_memory(sbi, INO_ENTRIES))
498 return;
499
500 do_sync:
501 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
502 struct blk_plug plug;
503
504 mutex_lock(&sbi->flush_lock);
505
506 blk_start_plug(&plug);
507 f2fs_sync_dirty_inodes(sbi, FILE_INODE, false);
508 blk_finish_plug(&plug);
509
510 mutex_unlock(&sbi->flush_lock);
511 }
512 f2fs_sync_fs(sbi->sb, 1);
513 stat_inc_bg_cp_count(sbi->stat_info);
514 }
515
__submit_flush_wait(struct f2fs_sb_info * sbi,struct block_device * bdev)516 static int __submit_flush_wait(struct f2fs_sb_info *sbi,
517 struct block_device *bdev)
518 {
519 int ret = blkdev_issue_flush(bdev);
520
521 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
522 test_opt(sbi, FLUSH_MERGE), ret);
523 if (!ret)
524 f2fs_update_iostat(sbi, NULL, FS_FLUSH_IO, 0);
525 return ret;
526 }
527
submit_flush_wait(struct f2fs_sb_info * sbi,nid_t ino)528 static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
529 {
530 int ret = 0;
531 int i;
532
533 if (!f2fs_is_multi_device(sbi))
534 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
535
536 for (i = 0; i < sbi->s_ndevs; i++) {
537 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
538 continue;
539 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
540 if (ret)
541 break;
542 }
543 return ret;
544 }
545
issue_flush_thread(void * data)546 static int issue_flush_thread(void *data)
547 {
548 struct f2fs_sb_info *sbi = data;
549 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
550 wait_queue_head_t *q = &fcc->flush_wait_queue;
551 repeat:
552 if (kthread_should_stop())
553 return 0;
554
555 if (!llist_empty(&fcc->issue_list)) {
556 struct flush_cmd *cmd, *next;
557 int ret;
558
559 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
560 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
561
562 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
563
564 ret = submit_flush_wait(sbi, cmd->ino);
565 atomic_inc(&fcc->issued_flush);
566
567 llist_for_each_entry_safe(cmd, next,
568 fcc->dispatch_list, llnode) {
569 cmd->ret = ret;
570 complete(&cmd->wait);
571 }
572 fcc->dispatch_list = NULL;
573 }
574
575 wait_event_interruptible(*q,
576 kthread_should_stop() || !llist_empty(&fcc->issue_list));
577 goto repeat;
578 }
579
f2fs_issue_flush(struct f2fs_sb_info * sbi,nid_t ino)580 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
581 {
582 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
583 struct flush_cmd cmd;
584 int ret;
585
586 if (test_opt(sbi, NOBARRIER))
587 return 0;
588
589 if (!test_opt(sbi, FLUSH_MERGE)) {
590 atomic_inc(&fcc->queued_flush);
591 ret = submit_flush_wait(sbi, ino);
592 atomic_dec(&fcc->queued_flush);
593 atomic_inc(&fcc->issued_flush);
594 return ret;
595 }
596
597 if (atomic_inc_return(&fcc->queued_flush) == 1 ||
598 f2fs_is_multi_device(sbi)) {
599 ret = submit_flush_wait(sbi, ino);
600 atomic_dec(&fcc->queued_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 /*
612 * update issue_list before we wake up issue_flush thread, this
613 * smp_mb() pairs with another barrier in ___wait_event(), see
614 * more details in comments of waitqueue_active().
615 */
616 smp_mb();
617
618 if (waitqueue_active(&fcc->flush_wait_queue))
619 wake_up(&fcc->flush_wait_queue);
620
621 if (fcc->f2fs_issue_flush) {
622 wait_for_completion(&cmd.wait);
623 atomic_dec(&fcc->queued_flush);
624 } else {
625 struct llist_node *list;
626
627 list = llist_del_all(&fcc->issue_list);
628 if (!list) {
629 wait_for_completion(&cmd.wait);
630 atomic_dec(&fcc->queued_flush);
631 } else {
632 struct flush_cmd *tmp, *next;
633
634 ret = submit_flush_wait(sbi, ino);
635
636 llist_for_each_entry_safe(tmp, next, list, llnode) {
637 if (tmp == &cmd) {
638 cmd.ret = ret;
639 atomic_dec(&fcc->queued_flush);
640 continue;
641 }
642 tmp->ret = ret;
643 complete(&tmp->wait);
644 }
645 }
646 }
647
648 return cmd.ret;
649 }
650
f2fs_create_flush_cmd_control(struct f2fs_sb_info * sbi)651 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
652 {
653 dev_t dev = sbi->sb->s_bdev->bd_dev;
654 struct flush_cmd_control *fcc;
655
656 if (SM_I(sbi)->fcc_info) {
657 fcc = SM_I(sbi)->fcc_info;
658 if (fcc->f2fs_issue_flush)
659 return 0;
660 goto init_thread;
661 }
662
663 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
664 if (!fcc)
665 return -ENOMEM;
666 atomic_set(&fcc->issued_flush, 0);
667 atomic_set(&fcc->queued_flush, 0);
668 init_waitqueue_head(&fcc->flush_wait_queue);
669 init_llist_head(&fcc->issue_list);
670 SM_I(sbi)->fcc_info = fcc;
671 if (!test_opt(sbi, FLUSH_MERGE))
672 return 0;
673
674 init_thread:
675 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
676 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
677 if (IS_ERR(fcc->f2fs_issue_flush)) {
678 int err = PTR_ERR(fcc->f2fs_issue_flush);
679
680 fcc->f2fs_issue_flush = NULL;
681 return err;
682 }
683
684 return 0;
685 }
686
f2fs_destroy_flush_cmd_control(struct f2fs_sb_info * sbi,bool free)687 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
688 {
689 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
690
691 if (fcc && fcc->f2fs_issue_flush) {
692 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
693
694 fcc->f2fs_issue_flush = NULL;
695 kthread_stop(flush_thread);
696 }
697 if (free) {
698 kfree(fcc);
699 SM_I(sbi)->fcc_info = NULL;
700 }
701 }
702
f2fs_flush_device_cache(struct f2fs_sb_info * sbi)703 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
704 {
705 int ret = 0, i;
706
707 if (!f2fs_is_multi_device(sbi))
708 return 0;
709
710 if (test_opt(sbi, NOBARRIER))
711 return 0;
712
713 for (i = 1; i < sbi->s_ndevs; i++) {
714 int count = DEFAULT_RETRY_IO_COUNT;
715
716 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
717 continue;
718
719 do {
720 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
721 if (ret)
722 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
723 } while (ret && --count);
724
725 if (ret) {
726 f2fs_stop_checkpoint(sbi, false,
727 STOP_CP_REASON_FLUSH_FAIL);
728 break;
729 }
730
731 spin_lock(&sbi->dev_lock);
732 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
733 spin_unlock(&sbi->dev_lock);
734 }
735
736 return ret;
737 }
738
__locate_dirty_segment(struct f2fs_sb_info * sbi,unsigned int segno,enum dirty_type dirty_type)739 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
740 enum dirty_type dirty_type)
741 {
742 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
743
744 /* need not be added */
745 if (IS_CURSEG(sbi, segno))
746 return;
747
748 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
749 dirty_i->nr_dirty[dirty_type]++;
750
751 if (dirty_type == DIRTY) {
752 struct seg_entry *sentry = get_seg_entry(sbi, segno);
753 enum dirty_type t = sentry->type;
754
755 if (unlikely(t >= DIRTY)) {
756 f2fs_bug_on(sbi, 1);
757 return;
758 }
759 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
760 dirty_i->nr_dirty[t]++;
761
762 if (__is_large_section(sbi)) {
763 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
764 block_t valid_blocks =
765 get_valid_blocks(sbi, segno, true);
766
767 f2fs_bug_on(sbi, unlikely(!valid_blocks ||
768 valid_blocks == CAP_BLKS_PER_SEC(sbi)));
769
770 if (!IS_CURSEC(sbi, secno))
771 set_bit(secno, dirty_i->dirty_secmap);
772 }
773 }
774 }
775
__remove_dirty_segment(struct f2fs_sb_info * sbi,unsigned int segno,enum dirty_type dirty_type)776 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
777 enum dirty_type dirty_type)
778 {
779 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
780 block_t valid_blocks;
781
782 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
783 dirty_i->nr_dirty[dirty_type]--;
784
785 if (dirty_type == DIRTY) {
786 struct seg_entry *sentry = get_seg_entry(sbi, segno);
787 enum dirty_type t = sentry->type;
788
789 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
790 dirty_i->nr_dirty[t]--;
791
792 valid_blocks = get_valid_blocks(sbi, segno, true);
793 if (valid_blocks == 0) {
794 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
795 dirty_i->victim_secmap);
796 #ifdef CONFIG_F2FS_CHECK_FS
797 clear_bit(segno, SIT_I(sbi)->invalid_segmap);
798 #endif
799 }
800 if (__is_large_section(sbi)) {
801 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
802
803 if (!valid_blocks ||
804 valid_blocks == CAP_BLKS_PER_SEC(sbi)) {
805 clear_bit(secno, dirty_i->dirty_secmap);
806 return;
807 }
808
809 if (!IS_CURSEC(sbi, secno))
810 set_bit(secno, dirty_i->dirty_secmap);
811 }
812 }
813 }
814
815 /*
816 * Should not occur error such as -ENOMEM.
817 * Adding dirty entry into seglist is not critical operation.
818 * If a given segment is one of current working segments, it won't be added.
819 */
locate_dirty_segment(struct f2fs_sb_info * sbi,unsigned int segno)820 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
821 {
822 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
823 unsigned short valid_blocks, ckpt_valid_blocks;
824 unsigned int usable_blocks;
825
826 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
827 return;
828
829 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
830 mutex_lock(&dirty_i->seglist_lock);
831
832 valid_blocks = get_valid_blocks(sbi, segno, false);
833 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false);
834
835 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
836 ckpt_valid_blocks == usable_blocks)) {
837 __locate_dirty_segment(sbi, segno, PRE);
838 __remove_dirty_segment(sbi, segno, DIRTY);
839 } else if (valid_blocks < usable_blocks) {
840 __locate_dirty_segment(sbi, segno, DIRTY);
841 } else {
842 /* Recovery routine with SSR needs this */
843 __remove_dirty_segment(sbi, segno, DIRTY);
844 }
845
846 mutex_unlock(&dirty_i->seglist_lock);
847 }
848
849 /* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
f2fs_dirty_to_prefree(struct f2fs_sb_info * sbi)850 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
851 {
852 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
853 unsigned int segno;
854
855 mutex_lock(&dirty_i->seglist_lock);
856 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
857 if (get_valid_blocks(sbi, segno, false))
858 continue;
859 if (IS_CURSEG(sbi, segno))
860 continue;
861 __locate_dirty_segment(sbi, segno, PRE);
862 __remove_dirty_segment(sbi, segno, DIRTY);
863 }
864 mutex_unlock(&dirty_i->seglist_lock);
865 }
866
f2fs_get_unusable_blocks(struct f2fs_sb_info * sbi)867 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
868 {
869 int ovp_hole_segs =
870 (overprovision_segments(sbi) - reserved_segments(sbi));
871 block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg;
872 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
873 block_t holes[2] = {0, 0}; /* DATA and NODE */
874 block_t unusable;
875 struct seg_entry *se;
876 unsigned int segno;
877
878 mutex_lock(&dirty_i->seglist_lock);
879 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
880 se = get_seg_entry(sbi, segno);
881 if (IS_NODESEG(se->type))
882 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
883 se->valid_blocks;
884 else
885 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
886 se->valid_blocks;
887 }
888 mutex_unlock(&dirty_i->seglist_lock);
889
890 unusable = max(holes[DATA], holes[NODE]);
891 if (unusable > ovp_holes)
892 return unusable - ovp_holes;
893 return 0;
894 }
895
f2fs_disable_cp_again(struct f2fs_sb_info * sbi,block_t unusable)896 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
897 {
898 int ovp_hole_segs =
899 (overprovision_segments(sbi) - reserved_segments(sbi));
900 if (unusable > F2FS_OPTION(sbi).unusable_cap)
901 return -EAGAIN;
902 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
903 dirty_segments(sbi) > ovp_hole_segs)
904 return -EAGAIN;
905 return 0;
906 }
907
908 /* This is only used by SBI_CP_DISABLED */
get_free_segment(struct f2fs_sb_info * sbi)909 static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
910 {
911 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
912 unsigned int segno = 0;
913
914 mutex_lock(&dirty_i->seglist_lock);
915 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
916 if (get_valid_blocks(sbi, segno, false))
917 continue;
918 if (get_ckpt_valid_blocks(sbi, segno, false))
919 continue;
920 mutex_unlock(&dirty_i->seglist_lock);
921 return segno;
922 }
923 mutex_unlock(&dirty_i->seglist_lock);
924 return NULL_SEGNO;
925 }
926
__create_discard_cmd(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t lstart,block_t start,block_t len)927 static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
928 struct block_device *bdev, block_t lstart,
929 block_t start, block_t len)
930 {
931 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
932 struct list_head *pend_list;
933 struct discard_cmd *dc;
934
935 f2fs_bug_on(sbi, !len);
936
937 pend_list = &dcc->pend_list[plist_idx(len)];
938
939 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL);
940 INIT_LIST_HEAD(&dc->list);
941 dc->bdev = bdev;
942 dc->di.lstart = lstart;
943 dc->di.start = start;
944 dc->di.len = len;
945 dc->ref = 0;
946 dc->state = D_PREP;
947 dc->queued = 0;
948 dc->error = 0;
949 init_completion(&dc->wait);
950 list_add_tail(&dc->list, pend_list);
951 spin_lock_init(&dc->lock);
952 dc->bio_ref = 0;
953 atomic_inc(&dcc->discard_cmd_cnt);
954 dcc->undiscard_blks += len;
955
956 return dc;
957 }
958
f2fs_check_discard_tree(struct f2fs_sb_info * sbi)959 static bool f2fs_check_discard_tree(struct f2fs_sb_info *sbi)
960 {
961 #ifdef CONFIG_F2FS_CHECK_FS
962 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
963 struct rb_node *cur = rb_first_cached(&dcc->root), *next;
964 struct discard_cmd *cur_dc, *next_dc;
965
966 while (cur) {
967 next = rb_next(cur);
968 if (!next)
969 return true;
970
971 cur_dc = rb_entry(cur, struct discard_cmd, rb_node);
972 next_dc = rb_entry(next, struct discard_cmd, rb_node);
973
974 if (cur_dc->di.lstart + cur_dc->di.len > next_dc->di.lstart) {
975 f2fs_info(sbi, "broken discard_rbtree, "
976 "cur(%u, %u) next(%u, %u)",
977 cur_dc->di.lstart, cur_dc->di.len,
978 next_dc->di.lstart, next_dc->di.len);
979 return false;
980 }
981 cur = next;
982 }
983 #endif
984 return true;
985 }
986
__lookup_discard_cmd(struct f2fs_sb_info * sbi,block_t blkaddr)987 static struct discard_cmd *__lookup_discard_cmd(struct f2fs_sb_info *sbi,
988 block_t blkaddr)
989 {
990 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
991 struct rb_node *node = dcc->root.rb_root.rb_node;
992 struct discard_cmd *dc;
993
994 while (node) {
995 dc = rb_entry(node, struct discard_cmd, rb_node);
996
997 if (blkaddr < dc->di.lstart)
998 node = node->rb_left;
999 else if (blkaddr >= dc->di.lstart + dc->di.len)
1000 node = node->rb_right;
1001 else
1002 return dc;
1003 }
1004 return NULL;
1005 }
1006
__lookup_discard_cmd_ret(struct rb_root_cached * root,block_t blkaddr,struct discard_cmd ** prev_entry,struct discard_cmd ** next_entry,struct rb_node *** insert_p,struct rb_node ** insert_parent)1007 static struct discard_cmd *__lookup_discard_cmd_ret(struct rb_root_cached *root,
1008 block_t blkaddr,
1009 struct discard_cmd **prev_entry,
1010 struct discard_cmd **next_entry,
1011 struct rb_node ***insert_p,
1012 struct rb_node **insert_parent)
1013 {
1014 struct rb_node **pnode = &root->rb_root.rb_node;
1015 struct rb_node *parent = NULL, *tmp_node;
1016 struct discard_cmd *dc;
1017
1018 *insert_p = NULL;
1019 *insert_parent = NULL;
1020 *prev_entry = NULL;
1021 *next_entry = NULL;
1022
1023 if (RB_EMPTY_ROOT(&root->rb_root))
1024 return NULL;
1025
1026 while (*pnode) {
1027 parent = *pnode;
1028 dc = rb_entry(*pnode, struct discard_cmd, rb_node);
1029
1030 if (blkaddr < dc->di.lstart)
1031 pnode = &(*pnode)->rb_left;
1032 else if (blkaddr >= dc->di.lstart + dc->di.len)
1033 pnode = &(*pnode)->rb_right;
1034 else
1035 goto lookup_neighbors;
1036 }
1037
1038 *insert_p = pnode;
1039 *insert_parent = parent;
1040
1041 dc = rb_entry(parent, struct discard_cmd, rb_node);
1042 tmp_node = parent;
1043 if (parent && blkaddr > dc->di.lstart)
1044 tmp_node = rb_next(parent);
1045 *next_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
1046
1047 tmp_node = parent;
1048 if (parent && blkaddr < dc->di.lstart)
1049 tmp_node = rb_prev(parent);
1050 *prev_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
1051 return NULL;
1052
1053 lookup_neighbors:
1054 /* lookup prev node for merging backward later */
1055 tmp_node = rb_prev(&dc->rb_node);
1056 *prev_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
1057
1058 /* lookup next node for merging frontward later */
1059 tmp_node = rb_next(&dc->rb_node);
1060 *next_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node);
1061 return dc;
1062 }
1063
__detach_discard_cmd(struct discard_cmd_control * dcc,struct discard_cmd * dc)1064 static void __detach_discard_cmd(struct discard_cmd_control *dcc,
1065 struct discard_cmd *dc)
1066 {
1067 if (dc->state == D_DONE)
1068 atomic_sub(dc->queued, &dcc->queued_discard);
1069
1070 list_del(&dc->list);
1071 rb_erase_cached(&dc->rb_node, &dcc->root);
1072 dcc->undiscard_blks -= dc->di.len;
1073
1074 kmem_cache_free(discard_cmd_slab, dc);
1075
1076 atomic_dec(&dcc->discard_cmd_cnt);
1077 }
1078
__remove_discard_cmd(struct f2fs_sb_info * sbi,struct discard_cmd * dc)1079 static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
1080 struct discard_cmd *dc)
1081 {
1082 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1083 unsigned long flags;
1084
1085 trace_f2fs_remove_discard(dc->bdev, dc->di.start, dc->di.len);
1086
1087 spin_lock_irqsave(&dc->lock, flags);
1088 if (dc->bio_ref) {
1089 spin_unlock_irqrestore(&dc->lock, flags);
1090 return;
1091 }
1092 spin_unlock_irqrestore(&dc->lock, flags);
1093
1094 f2fs_bug_on(sbi, dc->ref);
1095
1096 if (dc->error == -EOPNOTSUPP)
1097 dc->error = 0;
1098
1099 if (dc->error)
1100 printk_ratelimited(
1101 "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d",
1102 KERN_INFO, sbi->sb->s_id,
1103 dc->di.lstart, dc->di.start, dc->di.len, dc->error);
1104 __detach_discard_cmd(dcc, dc);
1105 }
1106
f2fs_submit_discard_endio(struct bio * bio)1107 static void f2fs_submit_discard_endio(struct bio *bio)
1108 {
1109 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
1110 unsigned long flags;
1111
1112 spin_lock_irqsave(&dc->lock, flags);
1113 if (!dc->error)
1114 dc->error = blk_status_to_errno(bio->bi_status);
1115 dc->bio_ref--;
1116 if (!dc->bio_ref && dc->state == D_SUBMIT) {
1117 dc->state = D_DONE;
1118 complete_all(&dc->wait);
1119 }
1120 spin_unlock_irqrestore(&dc->lock, flags);
1121 bio_put(bio);
1122 }
1123
__check_sit_bitmap(struct f2fs_sb_info * sbi,block_t start,block_t end)1124 static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
1125 block_t start, block_t end)
1126 {
1127 #ifdef CONFIG_F2FS_CHECK_FS
1128 struct seg_entry *sentry;
1129 unsigned int segno;
1130 block_t blk = start;
1131 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
1132 unsigned long *map;
1133
1134 while (blk < end) {
1135 segno = GET_SEGNO(sbi, blk);
1136 sentry = get_seg_entry(sbi, segno);
1137 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
1138
1139 if (end < START_BLOCK(sbi, segno + 1))
1140 size = GET_BLKOFF_FROM_SEG0(sbi, end);
1141 else
1142 size = max_blocks;
1143 map = (unsigned long *)(sentry->cur_valid_map);
1144 offset = __find_rev_next_bit(map, size, offset);
1145 f2fs_bug_on(sbi, offset != size);
1146 blk = START_BLOCK(sbi, segno + 1);
1147 }
1148 #endif
1149 }
1150
__init_discard_policy(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy,int discard_type,unsigned int granularity)1151 static void __init_discard_policy(struct f2fs_sb_info *sbi,
1152 struct discard_policy *dpolicy,
1153 int discard_type, unsigned int granularity)
1154 {
1155 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1156
1157 /* common policy */
1158 dpolicy->type = discard_type;
1159 dpolicy->sync = true;
1160 dpolicy->ordered = false;
1161 dpolicy->granularity = granularity;
1162
1163 dpolicy->max_requests = dcc->max_discard_request;
1164 dpolicy->io_aware_gran = dcc->discard_io_aware_gran;
1165 dpolicy->timeout = false;
1166
1167 if (discard_type == DPOLICY_BG) {
1168 dpolicy->min_interval = dcc->min_discard_issue_time;
1169 dpolicy->mid_interval = dcc->mid_discard_issue_time;
1170 dpolicy->max_interval = dcc->max_discard_issue_time;
1171 dpolicy->io_aware = true;
1172 dpolicy->sync = false;
1173 dpolicy->ordered = true;
1174 if (utilization(sbi) > dcc->discard_urgent_util) {
1175 dpolicy->granularity = MIN_DISCARD_GRANULARITY;
1176 if (atomic_read(&dcc->discard_cmd_cnt))
1177 dpolicy->max_interval =
1178 dcc->min_discard_issue_time;
1179 }
1180 } else if (discard_type == DPOLICY_FORCE) {
1181 dpolicy->min_interval = dcc->min_discard_issue_time;
1182 dpolicy->mid_interval = dcc->mid_discard_issue_time;
1183 dpolicy->max_interval = dcc->max_discard_issue_time;
1184 dpolicy->io_aware = false;
1185 } else if (discard_type == DPOLICY_FSTRIM) {
1186 dpolicy->io_aware = false;
1187 } else if (discard_type == DPOLICY_UMOUNT) {
1188 dpolicy->io_aware = false;
1189 /* we need to issue all to keep CP_TRIMMED_FLAG */
1190 dpolicy->granularity = MIN_DISCARD_GRANULARITY;
1191 dpolicy->timeout = true;
1192 }
1193 }
1194
1195 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1196 struct block_device *bdev, block_t lstart,
1197 block_t start, block_t len);
1198 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
__submit_discard_cmd(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy,struct discard_cmd * dc,int * issued)1199 static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
1200 struct discard_policy *dpolicy,
1201 struct discard_cmd *dc, int *issued)
1202 {
1203 struct block_device *bdev = dc->bdev;
1204 struct request_queue *q = bdev_get_queue(bdev);
1205 unsigned int max_discard_blocks =
1206 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
1207 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1208 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1209 &(dcc->fstrim_list) : &(dcc->wait_list);
1210 int flag = dpolicy->sync ? REQ_SYNC : 0;
1211 block_t lstart, start, len, total_len;
1212 int err = 0;
1213
1214 if (dc->state != D_PREP)
1215 return 0;
1216
1217 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1218 return 0;
1219
1220 trace_f2fs_issue_discard(bdev, dc->di.start, dc->di.len);
1221
1222 lstart = dc->di.lstart;
1223 start = dc->di.start;
1224 len = dc->di.len;
1225 total_len = len;
1226
1227 dc->di.len = 0;
1228
1229 while (total_len && *issued < dpolicy->max_requests && !err) {
1230 struct bio *bio = NULL;
1231 unsigned long flags;
1232 bool last = true;
1233
1234 if (len > max_discard_blocks) {
1235 len = max_discard_blocks;
1236 last = false;
1237 }
1238
1239 (*issued)++;
1240 if (*issued == dpolicy->max_requests)
1241 last = true;
1242
1243 dc->di.len += len;
1244
1245 if (time_to_inject(sbi, FAULT_DISCARD)) {
1246 err = -EIO;
1247 } else {
1248 err = __blkdev_issue_discard(bdev,
1249 SECTOR_FROM_BLOCK(start),
1250 SECTOR_FROM_BLOCK(len),
1251 GFP_NOFS, 0, &bio);
1252 }
1253 if (err) {
1254 spin_lock_irqsave(&dc->lock, flags);
1255 if (dc->state == D_PARTIAL)
1256 dc->state = D_SUBMIT;
1257 spin_unlock_irqrestore(&dc->lock, flags);
1258
1259 break;
1260 }
1261
1262 f2fs_bug_on(sbi, !bio);
1263
1264 /*
1265 * should keep before submission to avoid D_DONE
1266 * right away
1267 */
1268 spin_lock_irqsave(&dc->lock, flags);
1269 if (last)
1270 dc->state = D_SUBMIT;
1271 else
1272 dc->state = D_PARTIAL;
1273 dc->bio_ref++;
1274 spin_unlock_irqrestore(&dc->lock, flags);
1275
1276 atomic_inc(&dcc->queued_discard);
1277 dc->queued++;
1278 list_move_tail(&dc->list, wait_list);
1279
1280 /* sanity check on discard range */
1281 __check_sit_bitmap(sbi, lstart, lstart + len);
1282
1283 bio->bi_private = dc;
1284 bio->bi_end_io = f2fs_submit_discard_endio;
1285 bio->bi_opf |= flag;
1286 submit_bio(bio);
1287
1288 atomic_inc(&dcc->issued_discard);
1289
1290 f2fs_update_iostat(sbi, NULL, FS_DISCARD_IO, len * F2FS_BLKSIZE);
1291
1292 lstart += len;
1293 start += len;
1294 total_len -= len;
1295 len = total_len;
1296 }
1297
1298 if (!err && len) {
1299 dcc->undiscard_blks -= len;
1300 __update_discard_tree_range(sbi, bdev, lstart, start, len);
1301 }
1302 return err;
1303 }
1304
__insert_discard_cmd(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t lstart,block_t start,block_t len)1305 static void __insert_discard_cmd(struct f2fs_sb_info *sbi,
1306 struct block_device *bdev, block_t lstart,
1307 block_t start, block_t len)
1308 {
1309 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1310 struct rb_node **p = &dcc->root.rb_root.rb_node;
1311 struct rb_node *parent = NULL;
1312 struct discard_cmd *dc;
1313 bool leftmost = true;
1314
1315 /* look up rb tree to find parent node */
1316 while (*p) {
1317 parent = *p;
1318 dc = rb_entry(parent, struct discard_cmd, rb_node);
1319
1320 if (lstart < dc->di.lstart) {
1321 p = &(*p)->rb_left;
1322 } else if (lstart >= dc->di.lstart + dc->di.len) {
1323 p = &(*p)->rb_right;
1324 leftmost = false;
1325 } else {
1326 f2fs_bug_on(sbi, 1);
1327 }
1328 }
1329
1330 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
1331
1332 rb_link_node(&dc->rb_node, parent, p);
1333 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
1334 }
1335
__relocate_discard_cmd(struct discard_cmd_control * dcc,struct discard_cmd * dc)1336 static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1337 struct discard_cmd *dc)
1338 {
1339 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->di.len)]);
1340 }
1341
__punch_discard_cmd(struct f2fs_sb_info * sbi,struct discard_cmd * dc,block_t blkaddr)1342 static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1343 struct discard_cmd *dc, block_t blkaddr)
1344 {
1345 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1346 struct discard_info di = dc->di;
1347 bool modified = false;
1348
1349 if (dc->state == D_DONE || dc->di.len == 1) {
1350 __remove_discard_cmd(sbi, dc);
1351 return;
1352 }
1353
1354 dcc->undiscard_blks -= di.len;
1355
1356 if (blkaddr > di.lstart) {
1357 dc->di.len = blkaddr - dc->di.lstart;
1358 dcc->undiscard_blks += dc->di.len;
1359 __relocate_discard_cmd(dcc, dc);
1360 modified = true;
1361 }
1362
1363 if (blkaddr < di.lstart + di.len - 1) {
1364 if (modified) {
1365 __insert_discard_cmd(sbi, dc->bdev, blkaddr + 1,
1366 di.start + blkaddr + 1 - di.lstart,
1367 di.lstart + di.len - 1 - blkaddr);
1368 } else {
1369 dc->di.lstart++;
1370 dc->di.len--;
1371 dc->di.start++;
1372 dcc->undiscard_blks += dc->di.len;
1373 __relocate_discard_cmd(dcc, dc);
1374 }
1375 }
1376 }
1377
__update_discard_tree_range(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t lstart,block_t start,block_t len)1378 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1379 struct block_device *bdev, block_t lstart,
1380 block_t start, block_t len)
1381 {
1382 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1383 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1384 struct discard_cmd *dc;
1385 struct discard_info di = {0};
1386 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1387 struct request_queue *q = bdev_get_queue(bdev);
1388 unsigned int max_discard_blocks =
1389 SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
1390 block_t end = lstart + len;
1391
1392 dc = __lookup_discard_cmd_ret(&dcc->root, lstart,
1393 &prev_dc, &next_dc, &insert_p, &insert_parent);
1394 if (dc)
1395 prev_dc = dc;
1396
1397 if (!prev_dc) {
1398 di.lstart = lstart;
1399 di.len = next_dc ? next_dc->di.lstart - lstart : len;
1400 di.len = min(di.len, len);
1401 di.start = start;
1402 }
1403
1404 while (1) {
1405 struct rb_node *node;
1406 bool merged = false;
1407 struct discard_cmd *tdc = NULL;
1408
1409 if (prev_dc) {
1410 di.lstart = prev_dc->di.lstart + prev_dc->di.len;
1411 if (di.lstart < lstart)
1412 di.lstart = lstart;
1413 if (di.lstart >= end)
1414 break;
1415
1416 if (!next_dc || next_dc->di.lstart > end)
1417 di.len = end - di.lstart;
1418 else
1419 di.len = next_dc->di.lstart - di.lstart;
1420 di.start = start + di.lstart - lstart;
1421 }
1422
1423 if (!di.len)
1424 goto next;
1425
1426 if (prev_dc && prev_dc->state == D_PREP &&
1427 prev_dc->bdev == bdev &&
1428 __is_discard_back_mergeable(&di, &prev_dc->di,
1429 max_discard_blocks)) {
1430 prev_dc->di.len += di.len;
1431 dcc->undiscard_blks += di.len;
1432 __relocate_discard_cmd(dcc, prev_dc);
1433 di = prev_dc->di;
1434 tdc = prev_dc;
1435 merged = true;
1436 }
1437
1438 if (next_dc && next_dc->state == D_PREP &&
1439 next_dc->bdev == bdev &&
1440 __is_discard_front_mergeable(&di, &next_dc->di,
1441 max_discard_blocks)) {
1442 next_dc->di.lstart = di.lstart;
1443 next_dc->di.len += di.len;
1444 next_dc->di.start = di.start;
1445 dcc->undiscard_blks += di.len;
1446 __relocate_discard_cmd(dcc, next_dc);
1447 if (tdc)
1448 __remove_discard_cmd(sbi, tdc);
1449 merged = true;
1450 }
1451
1452 if (!merged)
1453 __insert_discard_cmd(sbi, bdev,
1454 di.lstart, di.start, di.len);
1455 next:
1456 prev_dc = next_dc;
1457 if (!prev_dc)
1458 break;
1459
1460 node = rb_next(&prev_dc->rb_node);
1461 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1462 }
1463 }
1464
__queue_discard_cmd(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t blkstart,block_t blklen)1465 static void __queue_discard_cmd(struct f2fs_sb_info *sbi,
1466 struct block_device *bdev, block_t blkstart, block_t blklen)
1467 {
1468 block_t lblkstart = blkstart;
1469
1470 if (!f2fs_bdev_support_discard(bdev))
1471 return;
1472
1473 trace_f2fs_queue_discard(bdev, blkstart, blklen);
1474
1475 if (f2fs_is_multi_device(sbi)) {
1476 int devi = f2fs_target_device_index(sbi, blkstart);
1477
1478 blkstart -= FDEV(devi).start_blk;
1479 }
1480 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
1481 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
1482 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
1483 }
1484
__issue_discard_cmd_orderly(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy,int * issued)1485 static void __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1486 struct discard_policy *dpolicy, int *issued)
1487 {
1488 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1489 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1490 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1491 struct discard_cmd *dc;
1492 struct blk_plug plug;
1493 bool io_interrupted = false;
1494
1495 mutex_lock(&dcc->cmd_lock);
1496 dc = __lookup_discard_cmd_ret(&dcc->root, dcc->next_pos,
1497 &prev_dc, &next_dc, &insert_p, &insert_parent);
1498 if (!dc)
1499 dc = next_dc;
1500
1501 blk_start_plug(&plug);
1502
1503 while (dc) {
1504 struct rb_node *node;
1505 int err = 0;
1506
1507 if (dc->state != D_PREP)
1508 goto next;
1509
1510 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
1511 io_interrupted = true;
1512 break;
1513 }
1514
1515 dcc->next_pos = dc->di.lstart + dc->di.len;
1516 err = __submit_discard_cmd(sbi, dpolicy, dc, issued);
1517
1518 if (*issued >= dpolicy->max_requests)
1519 break;
1520 next:
1521 node = rb_next(&dc->rb_node);
1522 if (err)
1523 __remove_discard_cmd(sbi, dc);
1524 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1525 }
1526
1527 blk_finish_plug(&plug);
1528
1529 if (!dc)
1530 dcc->next_pos = 0;
1531
1532 mutex_unlock(&dcc->cmd_lock);
1533
1534 if (!(*issued) && io_interrupted)
1535 *issued = -1;
1536 }
1537 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1538 struct discard_policy *dpolicy);
1539
__issue_discard_cmd(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy)1540 static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1541 struct discard_policy *dpolicy)
1542 {
1543 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1544 struct list_head *pend_list;
1545 struct discard_cmd *dc, *tmp;
1546 struct blk_plug plug;
1547 int i, issued;
1548 bool io_interrupted = false;
1549
1550 if (dpolicy->timeout)
1551 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
1552
1553 retry:
1554 issued = 0;
1555 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1556 if (dpolicy->timeout &&
1557 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
1558 break;
1559
1560 if (i + 1 < dpolicy->granularity)
1561 break;
1562
1563 if (i + 1 < dcc->max_ordered_discard && dpolicy->ordered) {
1564 __issue_discard_cmd_orderly(sbi, dpolicy, &issued);
1565 return issued;
1566 }
1567
1568 pend_list = &dcc->pend_list[i];
1569
1570 mutex_lock(&dcc->cmd_lock);
1571 if (list_empty(pend_list))
1572 goto next;
1573 if (unlikely(dcc->rbtree_check))
1574 f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi));
1575 blk_start_plug(&plug);
1576 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1577 f2fs_bug_on(sbi, dc->state != D_PREP);
1578
1579 if (dpolicy->timeout &&
1580 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
1581 break;
1582
1583 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1584 !is_idle(sbi, DISCARD_TIME)) {
1585 io_interrupted = true;
1586 break;
1587 }
1588
1589 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
1590
1591 if (issued >= dpolicy->max_requests)
1592 break;
1593 }
1594 blk_finish_plug(&plug);
1595 next:
1596 mutex_unlock(&dcc->cmd_lock);
1597
1598 if (issued >= dpolicy->max_requests || io_interrupted)
1599 break;
1600 }
1601
1602 if (dpolicy->type == DPOLICY_UMOUNT && issued) {
1603 __wait_all_discard_cmd(sbi, dpolicy);
1604 goto retry;
1605 }
1606
1607 if (!issued && io_interrupted)
1608 issued = -1;
1609
1610 return issued;
1611 }
1612
__drop_discard_cmd(struct f2fs_sb_info * sbi)1613 static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
1614 {
1615 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1616 struct list_head *pend_list;
1617 struct discard_cmd *dc, *tmp;
1618 int i;
1619 bool dropped = false;
1620
1621 mutex_lock(&dcc->cmd_lock);
1622 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1623 pend_list = &dcc->pend_list[i];
1624 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1625 f2fs_bug_on(sbi, dc->state != D_PREP);
1626 __remove_discard_cmd(sbi, dc);
1627 dropped = true;
1628 }
1629 }
1630 mutex_unlock(&dcc->cmd_lock);
1631
1632 return dropped;
1633 }
1634
f2fs_drop_discard_cmd(struct f2fs_sb_info * sbi)1635 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
1636 {
1637 __drop_discard_cmd(sbi);
1638 }
1639
__wait_one_discard_bio(struct f2fs_sb_info * sbi,struct discard_cmd * dc)1640 static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1641 struct discard_cmd *dc)
1642 {
1643 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1644 unsigned int len = 0;
1645
1646 wait_for_completion_io(&dc->wait);
1647 mutex_lock(&dcc->cmd_lock);
1648 f2fs_bug_on(sbi, dc->state != D_DONE);
1649 dc->ref--;
1650 if (!dc->ref) {
1651 if (!dc->error)
1652 len = dc->di.len;
1653 __remove_discard_cmd(sbi, dc);
1654 }
1655 mutex_unlock(&dcc->cmd_lock);
1656
1657 return len;
1658 }
1659
__wait_discard_cmd_range(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy,block_t start,block_t end)1660 static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
1661 struct discard_policy *dpolicy,
1662 block_t start, block_t end)
1663 {
1664 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1665 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1666 &(dcc->fstrim_list) : &(dcc->wait_list);
1667 struct discard_cmd *dc = NULL, *iter, *tmp;
1668 unsigned int trimmed = 0;
1669
1670 next:
1671 dc = NULL;
1672
1673 mutex_lock(&dcc->cmd_lock);
1674 list_for_each_entry_safe(iter, tmp, wait_list, list) {
1675 if (iter->di.lstart + iter->di.len <= start ||
1676 end <= iter->di.lstart)
1677 continue;
1678 if (iter->di.len < dpolicy->granularity)
1679 continue;
1680 if (iter->state == D_DONE && !iter->ref) {
1681 wait_for_completion_io(&iter->wait);
1682 if (!iter->error)
1683 trimmed += iter->di.len;
1684 __remove_discard_cmd(sbi, iter);
1685 } else {
1686 iter->ref++;
1687 dc = iter;
1688 break;
1689 }
1690 }
1691 mutex_unlock(&dcc->cmd_lock);
1692
1693 if (dc) {
1694 trimmed += __wait_one_discard_bio(sbi, dc);
1695 goto next;
1696 }
1697
1698 return trimmed;
1699 }
1700
__wait_all_discard_cmd(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy)1701 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1702 struct discard_policy *dpolicy)
1703 {
1704 struct discard_policy dp;
1705 unsigned int discard_blks;
1706
1707 if (dpolicy)
1708 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
1709
1710 /* wait all */
1711 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, MIN_DISCARD_GRANULARITY);
1712 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1713 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, MIN_DISCARD_GRANULARITY);
1714 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1715
1716 return discard_blks;
1717 }
1718
1719 /* This should be covered by global mutex, &sit_i->sentry_lock */
f2fs_wait_discard_bio(struct f2fs_sb_info * sbi,block_t blkaddr)1720 static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
1721 {
1722 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1723 struct discard_cmd *dc;
1724 bool need_wait = false;
1725
1726 mutex_lock(&dcc->cmd_lock);
1727 dc = __lookup_discard_cmd(sbi, blkaddr);
1728 if (dc) {
1729 if (dc->state == D_PREP) {
1730 __punch_discard_cmd(sbi, dc, blkaddr);
1731 } else {
1732 dc->ref++;
1733 need_wait = true;
1734 }
1735 }
1736 mutex_unlock(&dcc->cmd_lock);
1737
1738 if (need_wait)
1739 __wait_one_discard_bio(sbi, dc);
1740 }
1741
f2fs_stop_discard_thread(struct f2fs_sb_info * sbi)1742 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
1743 {
1744 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1745
1746 if (dcc && dcc->f2fs_issue_discard) {
1747 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1748
1749 dcc->f2fs_issue_discard = NULL;
1750 kthread_stop(discard_thread);
1751 }
1752 }
1753
1754 /**
1755 * f2fs_issue_discard_timeout() - Issue all discard cmd within UMOUNT_DISCARD_TIMEOUT
1756 * @sbi: the f2fs_sb_info data for discard cmd to issue
1757 *
1758 * When UMOUNT_DISCARD_TIMEOUT is exceeded, all remaining discard commands will be dropped
1759 *
1760 * Return true if issued all discard cmd or no discard cmd need issue, otherwise return false.
1761 */
f2fs_issue_discard_timeout(struct f2fs_sb_info * sbi)1762 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
1763 {
1764 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1765 struct discard_policy dpolicy;
1766 bool dropped;
1767
1768 if (!atomic_read(&dcc->discard_cmd_cnt))
1769 return true;
1770
1771 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1772 dcc->discard_granularity);
1773 __issue_discard_cmd(sbi, &dpolicy);
1774 dropped = __drop_discard_cmd(sbi);
1775
1776 /* just to make sure there is no pending discard commands */
1777 __wait_all_discard_cmd(sbi, NULL);
1778
1779 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
1780 return !dropped;
1781 }
1782
issue_discard_thread(void * data)1783 static int issue_discard_thread(void *data)
1784 {
1785 struct f2fs_sb_info *sbi = data;
1786 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1787 wait_queue_head_t *q = &dcc->discard_wait_queue;
1788 struct discard_policy dpolicy;
1789 unsigned int wait_ms = dcc->min_discard_issue_time;
1790 int issued;
1791
1792 set_freezable();
1793
1794 do {
1795 wait_event_interruptible_timeout(*q,
1796 kthread_should_stop() || freezing(current) ||
1797 dcc->discard_wake,
1798 msecs_to_jiffies(wait_ms));
1799
1800 if (sbi->gc_mode == GC_URGENT_HIGH ||
1801 !f2fs_available_free_memory(sbi, DISCARD_CACHE))
1802 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE,
1803 MIN_DISCARD_GRANULARITY);
1804 else
1805 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
1806 dcc->discard_granularity);
1807
1808 if (dcc->discard_wake)
1809 dcc->discard_wake = false;
1810
1811 /* clean up pending candidates before going to sleep */
1812 if (atomic_read(&dcc->queued_discard))
1813 __wait_all_discard_cmd(sbi, NULL);
1814
1815 if (try_to_freeze())
1816 continue;
1817 if (f2fs_readonly(sbi->sb))
1818 continue;
1819 if (kthread_should_stop())
1820 return 0;
1821 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK) ||
1822 !atomic_read(&dcc->discard_cmd_cnt)) {
1823 wait_ms = dpolicy.max_interval;
1824 continue;
1825 }
1826
1827 sb_start_intwrite(sbi->sb);
1828
1829 issued = __issue_discard_cmd(sbi, &dpolicy);
1830 if (issued > 0) {
1831 __wait_all_discard_cmd(sbi, &dpolicy);
1832 wait_ms = dpolicy.min_interval;
1833 } else if (issued == -1) {
1834 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
1835 if (!wait_ms)
1836 wait_ms = dpolicy.mid_interval;
1837 } else {
1838 wait_ms = dpolicy.max_interval;
1839 }
1840 if (!atomic_read(&dcc->discard_cmd_cnt))
1841 wait_ms = dpolicy.max_interval;
1842
1843 sb_end_intwrite(sbi->sb);
1844
1845 } while (!kthread_should_stop());
1846 return 0;
1847 }
1848
1849 #ifdef CONFIG_BLK_DEV_ZONED
__f2fs_issue_discard_zone(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t blkstart,block_t blklen)1850 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1851 struct block_device *bdev, block_t blkstart, block_t blklen)
1852 {
1853 sector_t sector, nr_sects;
1854 block_t lblkstart = blkstart;
1855 int devi = 0;
1856 u64 remainder = 0;
1857
1858 if (f2fs_is_multi_device(sbi)) {
1859 devi = f2fs_target_device_index(sbi, blkstart);
1860 if (blkstart < FDEV(devi).start_blk ||
1861 blkstart > FDEV(devi).end_blk) {
1862 f2fs_err(sbi, "Invalid block %x", blkstart);
1863 return -EIO;
1864 }
1865 blkstart -= FDEV(devi).start_blk;
1866 }
1867
1868 /* For sequential zones, reset the zone write pointer */
1869 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
1870 sector = SECTOR_FROM_BLOCK(blkstart);
1871 nr_sects = SECTOR_FROM_BLOCK(blklen);
1872 div64_u64_rem(sector, bdev_zone_sectors(bdev), &remainder);
1873
1874 if (remainder || nr_sects != bdev_zone_sectors(bdev)) {
1875 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
1876 devi, sbi->s_ndevs ? FDEV(devi).path : "",
1877 blkstart, blklen);
1878 return -EIO;
1879 }
1880 trace_f2fs_issue_reset_zone(bdev, blkstart);
1881 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1882 sector, nr_sects, GFP_NOFS);
1883 }
1884
1885 /* For conventional zones, use regular discard if supported */
1886 __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
1887 return 0;
1888 }
1889 #endif
1890
__issue_discard_async(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t blkstart,block_t blklen)1891 static int __issue_discard_async(struct f2fs_sb_info *sbi,
1892 struct block_device *bdev, block_t blkstart, block_t blklen)
1893 {
1894 #ifdef CONFIG_BLK_DEV_ZONED
1895 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
1896 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1897 #endif
1898 __queue_discard_cmd(sbi, bdev, blkstart, blklen);
1899 return 0;
1900 }
1901
f2fs_issue_discard(struct f2fs_sb_info * sbi,block_t blkstart,block_t blklen)1902 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
1903 block_t blkstart, block_t blklen)
1904 {
1905 sector_t start = blkstart, len = 0;
1906 struct block_device *bdev;
1907 struct seg_entry *se;
1908 unsigned int offset;
1909 block_t i;
1910 int err = 0;
1911
1912 bdev = f2fs_target_device(sbi, blkstart, NULL);
1913
1914 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1915 if (i != start) {
1916 struct block_device *bdev2 =
1917 f2fs_target_device(sbi, i, NULL);
1918
1919 if (bdev2 != bdev) {
1920 err = __issue_discard_async(sbi, bdev,
1921 start, len);
1922 if (err)
1923 return err;
1924 bdev = bdev2;
1925 start = i;
1926 len = 0;
1927 }
1928 }
1929
1930 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1931 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1932
1933 if (f2fs_block_unit_discard(sbi) &&
1934 !f2fs_test_and_set_bit(offset, se->discard_map))
1935 sbi->discard_blks--;
1936 }
1937
1938 if (len)
1939 err = __issue_discard_async(sbi, bdev, start, len);
1940 return err;
1941 }
1942
add_discard_addrs(struct f2fs_sb_info * sbi,struct cp_control * cpc,bool check_only)1943 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1944 bool check_only)
1945 {
1946 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1947 int max_blocks = sbi->blocks_per_seg;
1948 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
1949 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1950 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1951 unsigned long *discard_map = (unsigned long *)se->discard_map;
1952 unsigned long *dmap = SIT_I(sbi)->tmp_map;
1953 unsigned int start = 0, end = -1;
1954 bool force = (cpc->reason & CP_DISCARD);
1955 struct discard_entry *de = NULL;
1956 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
1957 int i;
1958
1959 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi) ||
1960 !f2fs_block_unit_discard(sbi))
1961 return false;
1962
1963 if (!force) {
1964 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
1965 SM_I(sbi)->dcc_info->nr_discards >=
1966 SM_I(sbi)->dcc_info->max_discards)
1967 return false;
1968 }
1969
1970 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1971 for (i = 0; i < entries; i++)
1972 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
1973 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
1974
1975 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1976 SM_I(sbi)->dcc_info->max_discards) {
1977 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1978 if (start >= max_blocks)
1979 break;
1980
1981 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
1982 if (force && start && end != max_blocks
1983 && (end - start) < cpc->trim_minlen)
1984 continue;
1985
1986 if (check_only)
1987 return true;
1988
1989 if (!de) {
1990 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1991 GFP_F2FS_ZERO, true, NULL);
1992 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1993 list_add_tail(&de->list, head);
1994 }
1995
1996 for (i = start; i < end; i++)
1997 __set_bit_le(i, (void *)de->discard_map);
1998
1999 SM_I(sbi)->dcc_info->nr_discards += end - start;
2000 }
2001 return false;
2002 }
2003
release_discard_addr(struct discard_entry * entry)2004 static void release_discard_addr(struct discard_entry *entry)
2005 {
2006 list_del(&entry->list);
2007 kmem_cache_free(discard_entry_slab, entry);
2008 }
2009
f2fs_release_discard_addrs(struct f2fs_sb_info * sbi)2010 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
2011 {
2012 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
2013 struct discard_entry *entry, *this;
2014
2015 /* drop caches */
2016 list_for_each_entry_safe(entry, this, head, list)
2017 release_discard_addr(entry);
2018 }
2019
2020 /*
2021 * Should call f2fs_clear_prefree_segments after checkpoint is done.
2022 */
set_prefree_as_free_segments(struct f2fs_sb_info * sbi)2023 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
2024 {
2025 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2026 unsigned int segno;
2027
2028 mutex_lock(&dirty_i->seglist_lock);
2029 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
2030 __set_test_and_free(sbi, segno, false);
2031 mutex_unlock(&dirty_i->seglist_lock);
2032 }
2033
f2fs_clear_prefree_segments(struct f2fs_sb_info * sbi,struct cp_control * cpc)2034 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
2035 struct cp_control *cpc)
2036 {
2037 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2038 struct list_head *head = &dcc->entry_list;
2039 struct discard_entry *entry, *this;
2040 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2041 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
2042 unsigned int start = 0, end = -1;
2043 unsigned int secno, start_segno;
2044 bool force = (cpc->reason & CP_DISCARD);
2045 bool section_alignment = F2FS_OPTION(sbi).discard_unit ==
2046 DISCARD_UNIT_SECTION;
2047
2048 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi))
2049 section_alignment = true;
2050
2051 mutex_lock(&dirty_i->seglist_lock);
2052
2053 while (1) {
2054 int i;
2055
2056 if (section_alignment && end != -1)
2057 end--;
2058 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
2059 if (start >= MAIN_SEGS(sbi))
2060 break;
2061 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
2062 start + 1);
2063
2064 if (section_alignment) {
2065 start = rounddown(start, sbi->segs_per_sec);
2066 end = roundup(end, sbi->segs_per_sec);
2067 }
2068
2069 for (i = start; i < end; i++) {
2070 if (test_and_clear_bit(i, prefree_map))
2071 dirty_i->nr_dirty[PRE]--;
2072 }
2073
2074 if (!f2fs_realtime_discard_enable(sbi))
2075 continue;
2076
2077 if (force && start >= cpc->trim_start &&
2078 (end - 1) <= cpc->trim_end)
2079 continue;
2080
2081 /* Should cover 2MB zoned device for zone-based reset */
2082 if (!f2fs_sb_has_blkzoned(sbi) &&
2083 (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi))) {
2084 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
2085 (end - start) << sbi->log_blocks_per_seg);
2086 continue;
2087 }
2088 next:
2089 secno = GET_SEC_FROM_SEG(sbi, start);
2090 start_segno = GET_SEG_FROM_SEC(sbi, secno);
2091 if (!IS_CURSEC(sbi, secno) &&
2092 !get_valid_blocks(sbi, start, true))
2093 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
2094 sbi->segs_per_sec << sbi->log_blocks_per_seg);
2095
2096 start = start_segno + sbi->segs_per_sec;
2097 if (start < end)
2098 goto next;
2099 else
2100 end = start - 1;
2101 }
2102 mutex_unlock(&dirty_i->seglist_lock);
2103
2104 if (!f2fs_block_unit_discard(sbi))
2105 goto wakeup;
2106
2107 /* send small discards */
2108 list_for_each_entry_safe(entry, this, head, list) {
2109 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
2110 bool is_valid = test_bit_le(0, entry->discard_map);
2111
2112 find_next:
2113 if (is_valid) {
2114 next_pos = find_next_zero_bit_le(entry->discard_map,
2115 sbi->blocks_per_seg, cur_pos);
2116 len = next_pos - cur_pos;
2117
2118 if (f2fs_sb_has_blkzoned(sbi) ||
2119 (force && len < cpc->trim_minlen))
2120 goto skip;
2121
2122 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
2123 len);
2124 total_len += len;
2125 } else {
2126 next_pos = find_next_bit_le(entry->discard_map,
2127 sbi->blocks_per_seg, cur_pos);
2128 }
2129 skip:
2130 cur_pos = next_pos;
2131 is_valid = !is_valid;
2132
2133 if (cur_pos < sbi->blocks_per_seg)
2134 goto find_next;
2135
2136 release_discard_addr(entry);
2137 dcc->nr_discards -= total_len;
2138 }
2139
2140 wakeup:
2141 wake_up_discard_thread(sbi, false);
2142 }
2143
f2fs_start_discard_thread(struct f2fs_sb_info * sbi)2144 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
2145 {
2146 dev_t dev = sbi->sb->s_bdev->bd_dev;
2147 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2148 int err = 0;
2149
2150 if (!f2fs_realtime_discard_enable(sbi))
2151 return 0;
2152
2153 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
2154 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
2155 if (IS_ERR(dcc->f2fs_issue_discard)) {
2156 err = PTR_ERR(dcc->f2fs_issue_discard);
2157 dcc->f2fs_issue_discard = NULL;
2158 }
2159
2160 return err;
2161 }
2162
create_discard_cmd_control(struct f2fs_sb_info * sbi)2163 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
2164 {
2165 struct discard_cmd_control *dcc;
2166 int err = 0, i;
2167
2168 if (SM_I(sbi)->dcc_info) {
2169 dcc = SM_I(sbi)->dcc_info;
2170 goto init_thread;
2171 }
2172
2173 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
2174 if (!dcc)
2175 return -ENOMEM;
2176
2177 dcc->discard_io_aware_gran = MAX_PLIST_NUM;
2178 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
2179 dcc->max_ordered_discard = DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY;
2180 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2181 dcc->discard_granularity = sbi->blocks_per_seg;
2182 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2183 dcc->discard_granularity = BLKS_PER_SEC(sbi);
2184
2185 INIT_LIST_HEAD(&dcc->entry_list);
2186 for (i = 0; i < MAX_PLIST_NUM; i++)
2187 INIT_LIST_HEAD(&dcc->pend_list[i]);
2188 INIT_LIST_HEAD(&dcc->wait_list);
2189 INIT_LIST_HEAD(&dcc->fstrim_list);
2190 mutex_init(&dcc->cmd_lock);
2191 atomic_set(&dcc->issued_discard, 0);
2192 atomic_set(&dcc->queued_discard, 0);
2193 atomic_set(&dcc->discard_cmd_cnt, 0);
2194 dcc->nr_discards = 0;
2195 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
2196 dcc->max_discard_request = DEF_MAX_DISCARD_REQUEST;
2197 dcc->min_discard_issue_time = DEF_MIN_DISCARD_ISSUE_TIME;
2198 dcc->mid_discard_issue_time = DEF_MID_DISCARD_ISSUE_TIME;
2199 dcc->max_discard_issue_time = DEF_MAX_DISCARD_ISSUE_TIME;
2200 dcc->discard_urgent_util = DEF_DISCARD_URGENT_UTIL;
2201 dcc->undiscard_blks = 0;
2202 dcc->next_pos = 0;
2203 dcc->root = RB_ROOT_CACHED;
2204 dcc->rbtree_check = false;
2205
2206 init_waitqueue_head(&dcc->discard_wait_queue);
2207 SM_I(sbi)->dcc_info = dcc;
2208 init_thread:
2209 err = f2fs_start_discard_thread(sbi);
2210 if (err) {
2211 kfree(dcc);
2212 SM_I(sbi)->dcc_info = NULL;
2213 }
2214
2215 return err;
2216 }
2217
destroy_discard_cmd_control(struct f2fs_sb_info * sbi)2218 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
2219 {
2220 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2221
2222 if (!dcc)
2223 return;
2224
2225 f2fs_stop_discard_thread(sbi);
2226
2227 /*
2228 * Recovery can cache discard commands, so in error path of
2229 * fill_super(), it needs to give a chance to handle them.
2230 */
2231 f2fs_issue_discard_timeout(sbi);
2232
2233 kfree(dcc);
2234 SM_I(sbi)->dcc_info = NULL;
2235 }
2236
__mark_sit_entry_dirty(struct f2fs_sb_info * sbi,unsigned int segno)2237 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
2238 {
2239 struct sit_info *sit_i = SIT_I(sbi);
2240
2241 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
2242 sit_i->dirty_sentries++;
2243 return false;
2244 }
2245
2246 return true;
2247 }
2248
__set_sit_entry_type(struct f2fs_sb_info * sbi,int type,unsigned int segno,int modified)2249 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
2250 unsigned int segno, int modified)
2251 {
2252 struct seg_entry *se = get_seg_entry(sbi, segno);
2253
2254 se->type = type;
2255 if (modified)
2256 __mark_sit_entry_dirty(sbi, segno);
2257 }
2258
get_segment_mtime(struct f2fs_sb_info * sbi,block_t blkaddr)2259 static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi,
2260 block_t blkaddr)
2261 {
2262 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2263
2264 if (segno == NULL_SEGNO)
2265 return 0;
2266 return get_seg_entry(sbi, segno)->mtime;
2267 }
2268
update_segment_mtime(struct f2fs_sb_info * sbi,block_t blkaddr,unsigned long long old_mtime)2269 static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
2270 unsigned long long old_mtime)
2271 {
2272 struct seg_entry *se;
2273 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2274 unsigned long long ctime = get_mtime(sbi, false);
2275 unsigned long long mtime = old_mtime ? old_mtime : ctime;
2276
2277 if (segno == NULL_SEGNO)
2278 return;
2279
2280 se = get_seg_entry(sbi, segno);
2281
2282 if (!se->mtime)
2283 se->mtime = mtime;
2284 else
2285 se->mtime = div_u64(se->mtime * se->valid_blocks + mtime,
2286 se->valid_blocks + 1);
2287
2288 if (ctime > SIT_I(sbi)->max_mtime)
2289 SIT_I(sbi)->max_mtime = ctime;
2290 }
2291
update_sit_entry(struct f2fs_sb_info * sbi,block_t blkaddr,int del)2292 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
2293 {
2294 struct seg_entry *se;
2295 unsigned int segno, offset;
2296 long int new_vblocks;
2297 bool exist;
2298 #ifdef CONFIG_F2FS_CHECK_FS
2299 bool mir_exist;
2300 #endif
2301
2302 segno = GET_SEGNO(sbi, blkaddr);
2303
2304 se = get_seg_entry(sbi, segno);
2305 new_vblocks = se->valid_blocks + del;
2306 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2307
2308 f2fs_bug_on(sbi, (new_vblocks < 0 ||
2309 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
2310
2311 se->valid_blocks = new_vblocks;
2312
2313 /* Update valid block bitmap */
2314 if (del > 0) {
2315 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
2316 #ifdef CONFIG_F2FS_CHECK_FS
2317 mir_exist = f2fs_test_and_set_bit(offset,
2318 se->cur_valid_map_mir);
2319 if (unlikely(exist != mir_exist)) {
2320 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
2321 blkaddr, exist);
2322 f2fs_bug_on(sbi, 1);
2323 }
2324 #endif
2325 if (unlikely(exist)) {
2326 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u",
2327 blkaddr);
2328 f2fs_bug_on(sbi, 1);
2329 se->valid_blocks--;
2330 del = 0;
2331 }
2332
2333 if (f2fs_block_unit_discard(sbi) &&
2334 !f2fs_test_and_set_bit(offset, se->discard_map))
2335 sbi->discard_blks--;
2336
2337 /*
2338 * SSR should never reuse block which is checkpointed
2339 * or newly invalidated.
2340 */
2341 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
2342 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2343 se->ckpt_valid_blocks++;
2344 }
2345 } else {
2346 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
2347 #ifdef CONFIG_F2FS_CHECK_FS
2348 mir_exist = f2fs_test_and_clear_bit(offset,
2349 se->cur_valid_map_mir);
2350 if (unlikely(exist != mir_exist)) {
2351 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
2352 blkaddr, exist);
2353 f2fs_bug_on(sbi, 1);
2354 }
2355 #endif
2356 if (unlikely(!exist)) {
2357 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u",
2358 blkaddr);
2359 f2fs_bug_on(sbi, 1);
2360 se->valid_blocks++;
2361 del = 0;
2362 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2363 /*
2364 * If checkpoints are off, we must not reuse data that
2365 * was used in the previous checkpoint. If it was used
2366 * before, we must track that to know how much space we
2367 * really have.
2368 */
2369 if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
2370 spin_lock(&sbi->stat_lock);
2371 sbi->unusable_block_count++;
2372 spin_unlock(&sbi->stat_lock);
2373 }
2374 }
2375
2376 if (f2fs_block_unit_discard(sbi) &&
2377 f2fs_test_and_clear_bit(offset, se->discard_map))
2378 sbi->discard_blks++;
2379 }
2380 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2381 se->ckpt_valid_blocks += del;
2382
2383 __mark_sit_entry_dirty(sbi, segno);
2384
2385 /* update total number of valid blocks to be written in ckpt area */
2386 SIT_I(sbi)->written_valid_blocks += del;
2387
2388 if (__is_large_section(sbi))
2389 get_sec_entry(sbi, segno)->valid_blocks += del;
2390 }
2391
f2fs_invalidate_blocks(struct f2fs_sb_info * sbi,block_t addr)2392 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
2393 {
2394 unsigned int segno = GET_SEGNO(sbi, addr);
2395 struct sit_info *sit_i = SIT_I(sbi);
2396
2397 f2fs_bug_on(sbi, addr == NULL_ADDR);
2398 if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
2399 return;
2400
2401 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
2402 f2fs_invalidate_compress_page(sbi, addr);
2403
2404 /* add it into sit main buffer */
2405 down_write(&sit_i->sentry_lock);
2406
2407 update_segment_mtime(sbi, addr, 0);
2408 update_sit_entry(sbi, addr, -1);
2409
2410 /* add it into dirty seglist */
2411 locate_dirty_segment(sbi, segno);
2412
2413 up_write(&sit_i->sentry_lock);
2414 }
2415
f2fs_is_checkpointed_data(struct f2fs_sb_info * sbi,block_t blkaddr)2416 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
2417 {
2418 struct sit_info *sit_i = SIT_I(sbi);
2419 unsigned int segno, offset;
2420 struct seg_entry *se;
2421 bool is_cp = false;
2422
2423 if (!__is_valid_data_blkaddr(blkaddr))
2424 return true;
2425
2426 down_read(&sit_i->sentry_lock);
2427
2428 segno = GET_SEGNO(sbi, blkaddr);
2429 se = get_seg_entry(sbi, segno);
2430 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2431
2432 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2433 is_cp = true;
2434
2435 up_read(&sit_i->sentry_lock);
2436
2437 return is_cp;
2438 }
2439
f2fs_curseg_valid_blocks(struct f2fs_sb_info * sbi,int type)2440 static unsigned short f2fs_curseg_valid_blocks(struct f2fs_sb_info *sbi, int type)
2441 {
2442 struct curseg_info *curseg = CURSEG_I(sbi, type);
2443
2444 if (sbi->ckpt->alloc_type[type] == SSR)
2445 return sbi->blocks_per_seg;
2446 return curseg->next_blkoff;
2447 }
2448
2449 /*
2450 * Calculate the number of current summary pages for writing
2451 */
f2fs_npages_for_summary_flush(struct f2fs_sb_info * sbi,bool for_ra)2452 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
2453 {
2454 int valid_sum_count = 0;
2455 int i, sum_in_page;
2456
2457 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2458 if (sbi->ckpt->alloc_type[i] != SSR && for_ra)
2459 valid_sum_count +=
2460 le16_to_cpu(F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2461 else
2462 valid_sum_count += f2fs_curseg_valid_blocks(sbi, i);
2463 }
2464
2465 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
2466 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2467 if (valid_sum_count <= sum_in_page)
2468 return 1;
2469 else if ((valid_sum_count - sum_in_page) <=
2470 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
2471 return 2;
2472 return 3;
2473 }
2474
2475 /*
2476 * Caller should put this summary page
2477 */
f2fs_get_sum_page(struct f2fs_sb_info * sbi,unsigned int segno)2478 struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
2479 {
2480 if (unlikely(f2fs_cp_error(sbi)))
2481 return ERR_PTR(-EIO);
2482 return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno));
2483 }
2484
f2fs_update_meta_page(struct f2fs_sb_info * sbi,void * src,block_t blk_addr)2485 void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2486 void *src, block_t blk_addr)
2487 {
2488 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
2489
2490 memcpy(page_address(page), src, PAGE_SIZE);
2491 set_page_dirty(page);
2492 f2fs_put_page(page, 1);
2493 }
2494
write_sum_page(struct f2fs_sb_info * sbi,struct f2fs_summary_block * sum_blk,block_t blk_addr)2495 static void write_sum_page(struct f2fs_sb_info *sbi,
2496 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2497 {
2498 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
2499 }
2500
write_current_sum_page(struct f2fs_sb_info * sbi,int type,block_t blk_addr)2501 static void write_current_sum_page(struct f2fs_sb_info *sbi,
2502 int type, block_t blk_addr)
2503 {
2504 struct curseg_info *curseg = CURSEG_I(sbi, type);
2505 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
2506 struct f2fs_summary_block *src = curseg->sum_blk;
2507 struct f2fs_summary_block *dst;
2508
2509 dst = (struct f2fs_summary_block *)page_address(page);
2510 memset(dst, 0, PAGE_SIZE);
2511
2512 mutex_lock(&curseg->curseg_mutex);
2513
2514 down_read(&curseg->journal_rwsem);
2515 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2516 up_read(&curseg->journal_rwsem);
2517
2518 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2519 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2520
2521 mutex_unlock(&curseg->curseg_mutex);
2522
2523 set_page_dirty(page);
2524 f2fs_put_page(page, 1);
2525 }
2526
is_next_segment_free(struct f2fs_sb_info * sbi,struct curseg_info * curseg,int type)2527 static int is_next_segment_free(struct f2fs_sb_info *sbi,
2528 struct curseg_info *curseg, int type)
2529 {
2530 unsigned int segno = curseg->segno + 1;
2531 struct free_segmap_info *free_i = FREE_I(sbi);
2532
2533 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2534 return !test_bit(segno, free_i->free_segmap);
2535 return 0;
2536 }
2537
2538 /*
2539 * Find a new segment from the free segments bitmap to right order
2540 * This function should be returned with success, otherwise BUG
2541 */
get_new_segment(struct f2fs_sb_info * sbi,unsigned int * newseg,bool new_sec,int dir)2542 static void get_new_segment(struct f2fs_sb_info *sbi,
2543 unsigned int *newseg, bool new_sec, int dir)
2544 {
2545 struct free_segmap_info *free_i = FREE_I(sbi);
2546 unsigned int segno, secno, zoneno;
2547 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
2548 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2549 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
2550 unsigned int left_start = hint;
2551 bool init = true;
2552 int go_left = 0;
2553 int i;
2554
2555 spin_lock(&free_i->segmap_lock);
2556
2557 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2558 segno = find_next_zero_bit(free_i->free_segmap,
2559 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2560 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
2561 goto got_it;
2562 }
2563 find_other_zone:
2564 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2565 if (secno >= MAIN_SECS(sbi)) {
2566 if (dir == ALLOC_RIGHT) {
2567 secno = find_next_zero_bit(free_i->free_secmap,
2568 MAIN_SECS(sbi), 0);
2569 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
2570 } else {
2571 go_left = 1;
2572 left_start = hint - 1;
2573 }
2574 }
2575 if (go_left == 0)
2576 goto skip_left;
2577
2578 while (test_bit(left_start, free_i->free_secmap)) {
2579 if (left_start > 0) {
2580 left_start--;
2581 continue;
2582 }
2583 left_start = find_next_zero_bit(free_i->free_secmap,
2584 MAIN_SECS(sbi), 0);
2585 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
2586 break;
2587 }
2588 secno = left_start;
2589 skip_left:
2590 segno = GET_SEG_FROM_SEC(sbi, secno);
2591 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
2592
2593 /* give up on finding another zone */
2594 if (!init)
2595 goto got_it;
2596 if (sbi->secs_per_zone == 1)
2597 goto got_it;
2598 if (zoneno == old_zoneno)
2599 goto got_it;
2600 if (dir == ALLOC_LEFT) {
2601 if (!go_left && zoneno + 1 >= total_zones)
2602 goto got_it;
2603 if (go_left && zoneno == 0)
2604 goto got_it;
2605 }
2606 for (i = 0; i < NR_CURSEG_TYPE; i++)
2607 if (CURSEG_I(sbi, i)->zone == zoneno)
2608 break;
2609
2610 if (i < NR_CURSEG_TYPE) {
2611 /* zone is in user, try another */
2612 if (go_left)
2613 hint = zoneno * sbi->secs_per_zone - 1;
2614 else if (zoneno + 1 >= total_zones)
2615 hint = 0;
2616 else
2617 hint = (zoneno + 1) * sbi->secs_per_zone;
2618 init = false;
2619 goto find_other_zone;
2620 }
2621 got_it:
2622 /* set it as dirty segment in free segmap */
2623 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
2624 __set_inuse(sbi, segno);
2625 *newseg = segno;
2626 spin_unlock(&free_i->segmap_lock);
2627 }
2628
reset_curseg(struct f2fs_sb_info * sbi,int type,int modified)2629 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2630 {
2631 struct curseg_info *curseg = CURSEG_I(sbi, type);
2632 struct summary_footer *sum_footer;
2633 unsigned short seg_type = curseg->seg_type;
2634
2635 curseg->inited = true;
2636 curseg->segno = curseg->next_segno;
2637 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
2638 curseg->next_blkoff = 0;
2639 curseg->next_segno = NULL_SEGNO;
2640
2641 sum_footer = &(curseg->sum_blk->footer);
2642 memset(sum_footer, 0, sizeof(struct summary_footer));
2643
2644 sanity_check_seg_type(sbi, seg_type);
2645
2646 if (IS_DATASEG(seg_type))
2647 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2648 if (IS_NODESEG(seg_type))
2649 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2650 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified);
2651 }
2652
__get_next_segno(struct f2fs_sb_info * sbi,int type)2653 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2654 {
2655 struct curseg_info *curseg = CURSEG_I(sbi, type);
2656 unsigned short seg_type = curseg->seg_type;
2657
2658 sanity_check_seg_type(sbi, seg_type);
2659 if (f2fs_need_rand_seg(sbi))
2660 return prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
2661
2662 /* if segs_per_sec is large than 1, we need to keep original policy. */
2663 if (__is_large_section(sbi))
2664 return curseg->segno;
2665
2666 /* inmem log may not locate on any segment after mount */
2667 if (!curseg->inited)
2668 return 0;
2669
2670 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2671 return 0;
2672
2673 if (test_opt(sbi, NOHEAP) &&
2674 (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type)))
2675 return 0;
2676
2677 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2678 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
2679
2680 /* find segments from 0 to reuse freed segments */
2681 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2682 return 0;
2683
2684 return curseg->segno;
2685 }
2686
2687 /*
2688 * Allocate a current working segment.
2689 * This function always allocates a free segment in LFS manner.
2690 */
new_curseg(struct f2fs_sb_info * sbi,int type,bool new_sec)2691 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2692 {
2693 struct curseg_info *curseg = CURSEG_I(sbi, type);
2694 unsigned short seg_type = curseg->seg_type;
2695 unsigned int segno = curseg->segno;
2696 int dir = ALLOC_LEFT;
2697
2698 if (curseg->inited)
2699 write_sum_page(sbi, curseg->sum_blk,
2700 GET_SUM_BLOCK(sbi, segno));
2701 if (seg_type == CURSEG_WARM_DATA || seg_type == CURSEG_COLD_DATA)
2702 dir = ALLOC_RIGHT;
2703
2704 if (test_opt(sbi, NOHEAP))
2705 dir = ALLOC_RIGHT;
2706
2707 segno = __get_next_segno(sbi, type);
2708 get_new_segment(sbi, &segno, new_sec, dir);
2709 curseg->next_segno = segno;
2710 reset_curseg(sbi, type, 1);
2711 curseg->alloc_type = LFS;
2712 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2713 curseg->fragment_remained_chunk =
2714 prandom_u32() % sbi->max_fragment_chunk + 1;
2715 }
2716
__next_free_blkoff(struct f2fs_sb_info * sbi,int segno,block_t start)2717 static int __next_free_blkoff(struct f2fs_sb_info *sbi,
2718 int segno, block_t start)
2719 {
2720 struct seg_entry *se = get_seg_entry(sbi, segno);
2721 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
2722 unsigned long *target_map = SIT_I(sbi)->tmp_map;
2723 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2724 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2725 int i;
2726
2727 for (i = 0; i < entries; i++)
2728 target_map[i] = ckpt_map[i] | cur_map[i];
2729
2730 return __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2731 }
2732
f2fs_find_next_ssr_block(struct f2fs_sb_info * sbi,struct curseg_info * seg)2733 static int f2fs_find_next_ssr_block(struct f2fs_sb_info *sbi,
2734 struct curseg_info *seg)
2735 {
2736 return __next_free_blkoff(sbi, seg->segno, seg->next_blkoff + 1);
2737 }
2738
f2fs_segment_has_free_slot(struct f2fs_sb_info * sbi,int segno)2739 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
2740 {
2741 return __next_free_blkoff(sbi, segno, 0) < sbi->blocks_per_seg;
2742 }
2743
2744 /*
2745 * This function always allocates a used segment(from dirty seglist) by SSR
2746 * manner, so it should recover the existing segment information of valid blocks
2747 */
change_curseg(struct f2fs_sb_info * sbi,int type)2748 static void change_curseg(struct f2fs_sb_info *sbi, int type)
2749 {
2750 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2751 struct curseg_info *curseg = CURSEG_I(sbi, type);
2752 unsigned int new_segno = curseg->next_segno;
2753 struct f2fs_summary_block *sum_node;
2754 struct page *sum_page;
2755
2756 write_sum_page(sbi, curseg->sum_blk, GET_SUM_BLOCK(sbi, curseg->segno));
2757
2758 __set_test_and_inuse(sbi, new_segno);
2759
2760 mutex_lock(&dirty_i->seglist_lock);
2761 __remove_dirty_segment(sbi, new_segno, PRE);
2762 __remove_dirty_segment(sbi, new_segno, DIRTY);
2763 mutex_unlock(&dirty_i->seglist_lock);
2764
2765 reset_curseg(sbi, type, 1);
2766 curseg->alloc_type = SSR;
2767 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
2768
2769 sum_page = f2fs_get_sum_page(sbi, new_segno);
2770 if (IS_ERR(sum_page)) {
2771 /* GC won't be able to use stale summary pages by cp_error */
2772 memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE);
2773 return;
2774 }
2775 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2776 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2777 f2fs_put_page(sum_page, 1);
2778 }
2779
2780 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2781 int alloc_mode, unsigned long long age);
2782
get_atssr_segment(struct f2fs_sb_info * sbi,int type,int target_type,int alloc_mode,unsigned long long age)2783 static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
2784 int target_type, int alloc_mode,
2785 unsigned long long age)
2786 {
2787 struct curseg_info *curseg = CURSEG_I(sbi, type);
2788
2789 curseg->seg_type = target_type;
2790
2791 if (get_ssr_segment(sbi, type, alloc_mode, age)) {
2792 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
2793
2794 curseg->seg_type = se->type;
2795 change_curseg(sbi, type);
2796 } else {
2797 /* allocate cold segment by default */
2798 curseg->seg_type = CURSEG_COLD_DATA;
2799 new_curseg(sbi, type, true);
2800 }
2801 stat_inc_seg_type(sbi, curseg);
2802 }
2803
__f2fs_init_atgc_curseg(struct f2fs_sb_info * sbi)2804 static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
2805 {
2806 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
2807
2808 if (!sbi->am.atgc_enabled)
2809 return;
2810
2811 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2812
2813 mutex_lock(&curseg->curseg_mutex);
2814 down_write(&SIT_I(sbi)->sentry_lock);
2815
2816 get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0);
2817
2818 up_write(&SIT_I(sbi)->sentry_lock);
2819 mutex_unlock(&curseg->curseg_mutex);
2820
2821 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2822
2823 }
f2fs_init_inmem_curseg(struct f2fs_sb_info * sbi)2824 void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
2825 {
2826 __f2fs_init_atgc_curseg(sbi);
2827 }
2828
__f2fs_save_inmem_curseg(struct f2fs_sb_info * sbi,int type)2829 static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
2830 {
2831 struct curseg_info *curseg = CURSEG_I(sbi, type);
2832
2833 mutex_lock(&curseg->curseg_mutex);
2834 if (!curseg->inited)
2835 goto out;
2836
2837 if (get_valid_blocks(sbi, curseg->segno, false)) {
2838 write_sum_page(sbi, curseg->sum_blk,
2839 GET_SUM_BLOCK(sbi, curseg->segno));
2840 } else {
2841 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2842 __set_test_and_free(sbi, curseg->segno, true);
2843 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2844 }
2845 out:
2846 mutex_unlock(&curseg->curseg_mutex);
2847 }
2848
f2fs_save_inmem_curseg(struct f2fs_sb_info * sbi)2849 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi)
2850 {
2851 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2852
2853 if (sbi->am.atgc_enabled)
2854 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2855 }
2856
__f2fs_restore_inmem_curseg(struct f2fs_sb_info * sbi,int type)2857 static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type)
2858 {
2859 struct curseg_info *curseg = CURSEG_I(sbi, type);
2860
2861 mutex_lock(&curseg->curseg_mutex);
2862 if (!curseg->inited)
2863 goto out;
2864 if (get_valid_blocks(sbi, curseg->segno, false))
2865 goto out;
2866
2867 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2868 __set_test_and_inuse(sbi, curseg->segno);
2869 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2870 out:
2871 mutex_unlock(&curseg->curseg_mutex);
2872 }
2873
f2fs_restore_inmem_curseg(struct f2fs_sb_info * sbi)2874 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi)
2875 {
2876 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2877
2878 if (sbi->am.atgc_enabled)
2879 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2880 }
2881
get_ssr_segment(struct f2fs_sb_info * sbi,int type,int alloc_mode,unsigned long long age)2882 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2883 int alloc_mode, unsigned long long age)
2884 {
2885 struct curseg_info *curseg = CURSEG_I(sbi, type);
2886 unsigned segno = NULL_SEGNO;
2887 unsigned short seg_type = curseg->seg_type;
2888 int i, cnt;
2889 bool reversed = false;
2890
2891 sanity_check_seg_type(sbi, seg_type);
2892
2893 /* f2fs_need_SSR() already forces to do this */
2894 if (!f2fs_get_victim(sbi, &segno, BG_GC, seg_type, alloc_mode, age)) {
2895 curseg->next_segno = segno;
2896 return 1;
2897 }
2898
2899 /* For node segments, let's do SSR more intensively */
2900 if (IS_NODESEG(seg_type)) {
2901 if (seg_type >= CURSEG_WARM_NODE) {
2902 reversed = true;
2903 i = CURSEG_COLD_NODE;
2904 } else {
2905 i = CURSEG_HOT_NODE;
2906 }
2907 cnt = NR_CURSEG_NODE_TYPE;
2908 } else {
2909 if (seg_type >= CURSEG_WARM_DATA) {
2910 reversed = true;
2911 i = CURSEG_COLD_DATA;
2912 } else {
2913 i = CURSEG_HOT_DATA;
2914 }
2915 cnt = NR_CURSEG_DATA_TYPE;
2916 }
2917
2918 for (; cnt-- > 0; reversed ? i-- : i++) {
2919 if (i == seg_type)
2920 continue;
2921 if (!f2fs_get_victim(sbi, &segno, BG_GC, i, alloc_mode, age)) {
2922 curseg->next_segno = segno;
2923 return 1;
2924 }
2925 }
2926
2927 /* find valid_blocks=0 in dirty list */
2928 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2929 segno = get_free_segment(sbi);
2930 if (segno != NULL_SEGNO) {
2931 curseg->next_segno = segno;
2932 return 1;
2933 }
2934 }
2935 return 0;
2936 }
2937
need_new_seg(struct f2fs_sb_info * sbi,int type)2938 static bool need_new_seg(struct f2fs_sb_info *sbi, int type)
2939 {
2940 struct curseg_info *curseg = CURSEG_I(sbi, type);
2941
2942 if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2943 curseg->seg_type == CURSEG_WARM_NODE)
2944 return true;
2945 if (curseg->alloc_type == LFS &&
2946 is_next_segment_free(sbi, curseg, type) &&
2947 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2948 return true;
2949 if (!f2fs_need_SSR(sbi) || !get_ssr_segment(sbi, type, SSR, 0))
2950 return true;
2951 return false;
2952 }
2953
f2fs_allocate_segment_for_resize(struct f2fs_sb_info * sbi,int type,unsigned int start,unsigned int end)2954 void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
2955 unsigned int start, unsigned int end)
2956 {
2957 struct curseg_info *curseg = CURSEG_I(sbi, type);
2958 unsigned int segno;
2959
2960 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2961 mutex_lock(&curseg->curseg_mutex);
2962 down_write(&SIT_I(sbi)->sentry_lock);
2963
2964 segno = CURSEG_I(sbi, type)->segno;
2965 if (segno < start || segno > end)
2966 goto unlock;
2967
2968 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
2969 change_curseg(sbi, type);
2970 else
2971 new_curseg(sbi, type, true);
2972
2973 stat_inc_seg_type(sbi, curseg);
2974
2975 locate_dirty_segment(sbi, segno);
2976 unlock:
2977 up_write(&SIT_I(sbi)->sentry_lock);
2978
2979 if (segno != curseg->segno)
2980 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
2981 type, segno, curseg->segno);
2982
2983 mutex_unlock(&curseg->curseg_mutex);
2984 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2985 }
2986
__allocate_new_segment(struct f2fs_sb_info * sbi,int type,bool new_sec,bool force)2987 static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
2988 bool new_sec, bool force)
2989 {
2990 struct curseg_info *curseg = CURSEG_I(sbi, type);
2991 unsigned int old_segno;
2992
2993 if (!force && curseg->inited &&
2994 !curseg->next_blkoff &&
2995 !get_valid_blocks(sbi, curseg->segno, new_sec) &&
2996 !get_ckpt_valid_blocks(sbi, curseg->segno, new_sec))
2997 return;
2998
2999 old_segno = curseg->segno;
3000 new_curseg(sbi, type, true);
3001 stat_inc_seg_type(sbi, curseg);
3002 locate_dirty_segment(sbi, old_segno);
3003 }
3004
f2fs_allocate_new_section(struct f2fs_sb_info * sbi,int type,bool force)3005 void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force)
3006 {
3007 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3008 down_write(&SIT_I(sbi)->sentry_lock);
3009 __allocate_new_segment(sbi, type, true, force);
3010 up_write(&SIT_I(sbi)->sentry_lock);
3011 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3012 }
3013
f2fs_allocate_new_segments(struct f2fs_sb_info * sbi)3014 void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
3015 {
3016 int i;
3017
3018 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3019 down_write(&SIT_I(sbi)->sentry_lock);
3020 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
3021 __allocate_new_segment(sbi, i, false, false);
3022 up_write(&SIT_I(sbi)->sentry_lock);
3023 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3024 }
3025
f2fs_exist_trim_candidates(struct f2fs_sb_info * sbi,struct cp_control * cpc)3026 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3027 struct cp_control *cpc)
3028 {
3029 __u64 trim_start = cpc->trim_start;
3030 bool has_candidate = false;
3031
3032 down_write(&SIT_I(sbi)->sentry_lock);
3033 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
3034 if (add_discard_addrs(sbi, cpc, true)) {
3035 has_candidate = true;
3036 break;
3037 }
3038 }
3039 up_write(&SIT_I(sbi)->sentry_lock);
3040
3041 cpc->trim_start = trim_start;
3042 return has_candidate;
3043 }
3044
__issue_discard_cmd_range(struct f2fs_sb_info * sbi,struct discard_policy * dpolicy,unsigned int start,unsigned int end)3045 static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
3046 struct discard_policy *dpolicy,
3047 unsigned int start, unsigned int end)
3048 {
3049 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
3050 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
3051 struct rb_node **insert_p = NULL, *insert_parent = NULL;
3052 struct discard_cmd *dc;
3053 struct blk_plug plug;
3054 int issued;
3055 unsigned int trimmed = 0;
3056
3057 next:
3058 issued = 0;
3059
3060 mutex_lock(&dcc->cmd_lock);
3061 if (unlikely(dcc->rbtree_check))
3062 f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi));
3063
3064 dc = __lookup_discard_cmd_ret(&dcc->root, start,
3065 &prev_dc, &next_dc, &insert_p, &insert_parent);
3066 if (!dc)
3067 dc = next_dc;
3068
3069 blk_start_plug(&plug);
3070
3071 while (dc && dc->di.lstart <= end) {
3072 struct rb_node *node;
3073 int err = 0;
3074
3075 if (dc->di.len < dpolicy->granularity)
3076 goto skip;
3077
3078 if (dc->state != D_PREP) {
3079 list_move_tail(&dc->list, &dcc->fstrim_list);
3080 goto skip;
3081 }
3082
3083 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
3084
3085 if (issued >= dpolicy->max_requests) {
3086 start = dc->di.lstart + dc->di.len;
3087
3088 if (err)
3089 __remove_discard_cmd(sbi, dc);
3090
3091 blk_finish_plug(&plug);
3092 mutex_unlock(&dcc->cmd_lock);
3093 trimmed += __wait_all_discard_cmd(sbi, NULL);
3094 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
3095 goto next;
3096 }
3097 skip:
3098 node = rb_next(&dc->rb_node);
3099 if (err)
3100 __remove_discard_cmd(sbi, dc);
3101 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
3102
3103 if (fatal_signal_pending(current))
3104 break;
3105 }
3106
3107 blk_finish_plug(&plug);
3108 mutex_unlock(&dcc->cmd_lock);
3109
3110 return trimmed;
3111 }
3112
f2fs_trim_fs(struct f2fs_sb_info * sbi,struct fstrim_range * range)3113 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
3114 {
3115 __u64 start = F2FS_BYTES_TO_BLK(range->start);
3116 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
3117 unsigned int start_segno, end_segno;
3118 block_t start_block, end_block;
3119 struct cp_control cpc;
3120 struct discard_policy dpolicy;
3121 unsigned long long trimmed = 0;
3122 int err = 0;
3123 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
3124
3125 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
3126 return -EINVAL;
3127
3128 if (end < MAIN_BLKADDR(sbi))
3129 goto out;
3130
3131 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
3132 f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
3133 return -EFSCORRUPTED;
3134 }
3135
3136 /* start/end segment number in main_area */
3137 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
3138 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
3139 GET_SEGNO(sbi, end);
3140 if (need_align) {
3141 start_segno = rounddown(start_segno, sbi->segs_per_sec);
3142 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
3143 }
3144
3145 cpc.reason = CP_DISCARD;
3146 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
3147 cpc.trim_start = start_segno;
3148 cpc.trim_end = end_segno;
3149
3150 if (sbi->discard_blks == 0)
3151 goto out;
3152
3153 f2fs_down_write(&sbi->gc_lock);
3154 err = f2fs_write_checkpoint(sbi, &cpc);
3155 f2fs_up_write(&sbi->gc_lock);
3156 if (err)
3157 goto out;
3158
3159 /*
3160 * We filed discard candidates, but actually we don't need to wait for
3161 * all of them, since they'll be issued in idle time along with runtime
3162 * discard option. User configuration looks like using runtime discard
3163 * or periodic fstrim instead of it.
3164 */
3165 if (f2fs_realtime_discard_enable(sbi))
3166 goto out;
3167
3168 start_block = START_BLOCK(sbi, start_segno);
3169 end_block = START_BLOCK(sbi, end_segno + 1);
3170
3171 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
3172 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
3173 start_block, end_block);
3174
3175 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
3176 start_block, end_block);
3177 out:
3178 if (!err)
3179 range->len = F2FS_BLK_TO_BYTES(trimmed);
3180 return err;
3181 }
3182
f2fs_rw_hint_to_seg_type(enum rw_hint hint)3183 int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
3184 {
3185 switch (hint) {
3186 case WRITE_LIFE_SHORT:
3187 return CURSEG_HOT_DATA;
3188 case WRITE_LIFE_EXTREME:
3189 return CURSEG_COLD_DATA;
3190 default:
3191 return CURSEG_WARM_DATA;
3192 }
3193 }
3194
__get_segment_type_2(struct f2fs_io_info * fio)3195 static int __get_segment_type_2(struct f2fs_io_info *fio)
3196 {
3197 if (fio->type == DATA)
3198 return CURSEG_HOT_DATA;
3199 else
3200 return CURSEG_HOT_NODE;
3201 }
3202
__get_segment_type_4(struct f2fs_io_info * fio)3203 static int __get_segment_type_4(struct f2fs_io_info *fio)
3204 {
3205 if (fio->type == DATA) {
3206 struct inode *inode = fio->page->mapping->host;
3207
3208 if (S_ISDIR(inode->i_mode))
3209 return CURSEG_HOT_DATA;
3210 else
3211 return CURSEG_COLD_DATA;
3212 } else {
3213 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
3214 return CURSEG_WARM_NODE;
3215 else
3216 return CURSEG_COLD_NODE;
3217 }
3218 }
3219
__get_age_segment_type(struct inode * inode,pgoff_t pgofs)3220 static int __get_age_segment_type(struct inode *inode, pgoff_t pgofs)
3221 {
3222 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3223 struct extent_info ei = {};
3224
3225 if (f2fs_lookup_age_extent_cache(inode, pgofs, &ei)) {
3226 if (!ei.age)
3227 return NO_CHECK_TYPE;
3228 if (ei.age <= sbi->hot_data_age_threshold)
3229 return CURSEG_HOT_DATA;
3230 if (ei.age <= sbi->warm_data_age_threshold)
3231 return CURSEG_WARM_DATA;
3232 return CURSEG_COLD_DATA;
3233 }
3234 return NO_CHECK_TYPE;
3235 }
3236
__get_segment_type_6(struct f2fs_io_info * fio)3237 static int __get_segment_type_6(struct f2fs_io_info *fio)
3238 {
3239 if (fio->type == DATA) {
3240 struct inode *inode = fio->page->mapping->host;
3241 int type;
3242
3243 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
3244 return CURSEG_COLD_DATA_PINNED;
3245
3246 if (page_private_gcing(fio->page)) {
3247 if (fio->sbi->am.atgc_enabled &&
3248 (fio->io_type == FS_DATA_IO) &&
3249 (fio->sbi->gc_mode != GC_URGENT_HIGH))
3250 return CURSEG_ALL_DATA_ATGC;
3251 else
3252 return CURSEG_COLD_DATA;
3253 }
3254 if (file_is_cold(inode) || f2fs_need_compress_data(inode))
3255 return CURSEG_COLD_DATA;
3256
3257 type = __get_age_segment_type(inode, fio->page->index);
3258 if (type != NO_CHECK_TYPE)
3259 return type;
3260
3261 if (file_is_hot(inode) ||
3262 is_inode_flag_set(inode, FI_HOT_DATA) ||
3263 f2fs_is_cow_file(inode))
3264 return CURSEG_HOT_DATA;
3265 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
3266 } else {
3267 if (IS_DNODE(fio->page))
3268 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
3269 CURSEG_HOT_NODE;
3270 return CURSEG_COLD_NODE;
3271 }
3272 }
3273
__get_segment_type(struct f2fs_io_info * fio)3274 static int __get_segment_type(struct f2fs_io_info *fio)
3275 {
3276 int type = 0;
3277
3278 switch (F2FS_OPTION(fio->sbi).active_logs) {
3279 case 2:
3280 type = __get_segment_type_2(fio);
3281 break;
3282 case 4:
3283 type = __get_segment_type_4(fio);
3284 break;
3285 case 6:
3286 type = __get_segment_type_6(fio);
3287 break;
3288 default:
3289 f2fs_bug_on(fio->sbi, true);
3290 }
3291
3292 if (IS_HOT(type))
3293 fio->temp = HOT;
3294 else if (IS_WARM(type))
3295 fio->temp = WARM;
3296 else
3297 fio->temp = COLD;
3298 return type;
3299 }
3300
f2fs_randomize_chunk(struct f2fs_sb_info * sbi,struct curseg_info * seg)3301 static void f2fs_randomize_chunk(struct f2fs_sb_info *sbi,
3302 struct curseg_info *seg)
3303 {
3304 /* To allocate block chunks in different sizes, use random number */
3305 if (--seg->fragment_remained_chunk > 0)
3306 return;
3307
3308 seg->fragment_remained_chunk =
3309 prandom_u32() % sbi->max_fragment_chunk + 1;
3310 seg->next_blkoff +=
3311 prandom_u32() % sbi->max_fragment_hole + 1;
3312 }
3313
f2fs_allocate_data_block(struct f2fs_sb_info * sbi,struct page * page,block_t old_blkaddr,block_t * new_blkaddr,struct f2fs_summary * sum,int type,struct f2fs_io_info * fio)3314 void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3315 block_t old_blkaddr, block_t *new_blkaddr,
3316 struct f2fs_summary *sum, int type,
3317 struct f2fs_io_info *fio)
3318 {
3319 struct sit_info *sit_i = SIT_I(sbi);
3320 struct curseg_info *curseg = CURSEG_I(sbi, type);
3321 unsigned long long old_mtime;
3322 bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
3323 struct seg_entry *se = NULL;
3324 bool segment_full = false;
3325
3326 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3327
3328 mutex_lock(&curseg->curseg_mutex);
3329 down_write(&sit_i->sentry_lock);
3330
3331 if (from_gc) {
3332 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
3333 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
3334 sanity_check_seg_type(sbi, se->type);
3335 f2fs_bug_on(sbi, IS_NODESEG(se->type));
3336 }
3337 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3338
3339 f2fs_bug_on(sbi, curseg->next_blkoff >= sbi->blocks_per_seg);
3340
3341 f2fs_wait_discard_bio(sbi, *new_blkaddr);
3342
3343 curseg->sum_blk->entries[curseg->next_blkoff] = *sum;
3344 if (curseg->alloc_type == SSR) {
3345 curseg->next_blkoff = f2fs_find_next_ssr_block(sbi, curseg);
3346 } else {
3347 curseg->next_blkoff++;
3348 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
3349 f2fs_randomize_chunk(sbi, curseg);
3350 }
3351 if (curseg->next_blkoff >= f2fs_usable_blks_in_seg(sbi, curseg->segno))
3352 segment_full = true;
3353 stat_inc_block_count(sbi, curseg);
3354
3355 if (from_gc) {
3356 old_mtime = get_segment_mtime(sbi, old_blkaddr);
3357 } else {
3358 update_segment_mtime(sbi, old_blkaddr, 0);
3359 old_mtime = 0;
3360 }
3361 update_segment_mtime(sbi, *new_blkaddr, old_mtime);
3362
3363 /*
3364 * SIT information should be updated before segment allocation,
3365 * since SSR needs latest valid block information.
3366 */
3367 update_sit_entry(sbi, *new_blkaddr, 1);
3368 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3369 update_sit_entry(sbi, old_blkaddr, -1);
3370
3371 /*
3372 * If the current segment is full, flush it out and replace it with a
3373 * new segment.
3374 */
3375 if (segment_full) {
3376 if (from_gc) {
3377 get_atssr_segment(sbi, type, se->type,
3378 AT_SSR, se->mtime);
3379 } else {
3380 if (need_new_seg(sbi, type))
3381 new_curseg(sbi, type, false);
3382 else
3383 change_curseg(sbi, type);
3384 stat_inc_seg_type(sbi, curseg);
3385 }
3386 }
3387 /*
3388 * segment dirty status should be updated after segment allocation,
3389 * so we just need to update status only one time after previous
3390 * segment being closed.
3391 */
3392 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3393 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
3394
3395 if (IS_DATASEG(type))
3396 atomic64_inc(&sbi->allocated_data_blocks);
3397
3398 up_write(&sit_i->sentry_lock);
3399
3400 if (page && IS_NODESEG(type)) {
3401 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
3402
3403 f2fs_inode_chksum_set(sbi, page);
3404 }
3405
3406 if (fio) {
3407 struct f2fs_bio_info *io;
3408
3409 if (F2FS_IO_ALIGNED(sbi))
3410 fio->retry = 0;
3411
3412 INIT_LIST_HEAD(&fio->list);
3413 fio->in_list = 1;
3414 io = sbi->write_io[fio->type] + fio->temp;
3415 spin_lock(&io->io_lock);
3416 list_add_tail(&fio->list, &io->io_list);
3417 spin_unlock(&io->io_lock);
3418 }
3419
3420 mutex_unlock(&curseg->curseg_mutex);
3421
3422 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3423 }
3424
f2fs_update_device_state(struct f2fs_sb_info * sbi,nid_t ino,block_t blkaddr,unsigned int blkcnt)3425 void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
3426 block_t blkaddr, unsigned int blkcnt)
3427 {
3428 if (!f2fs_is_multi_device(sbi))
3429 return;
3430
3431 while (1) {
3432 unsigned int devidx = f2fs_target_device_index(sbi, blkaddr);
3433 unsigned int blks = FDEV(devidx).end_blk - blkaddr + 1;
3434
3435 /* update device state for fsync */
3436 f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO);
3437
3438 /* update device state for checkpoint */
3439 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
3440 spin_lock(&sbi->dev_lock);
3441 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
3442 spin_unlock(&sbi->dev_lock);
3443 }
3444
3445 if (blkcnt <= blks)
3446 break;
3447 blkcnt -= blks;
3448 blkaddr += blks;
3449 }
3450 }
3451
do_write_page(struct f2fs_summary * sum,struct f2fs_io_info * fio)3452 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
3453 {
3454 int type = __get_segment_type(fio);
3455 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA);
3456
3457 if (keep_order)
3458 f2fs_down_read(&fio->sbi->io_order_lock);
3459 reallocate:
3460 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
3461 &fio->new_blkaddr, sum, type, fio);
3462 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) {
3463 invalidate_mapping_pages(META_MAPPING(fio->sbi),
3464 fio->old_blkaddr, fio->old_blkaddr);
3465 f2fs_invalidate_compress_page(fio->sbi, fio->old_blkaddr);
3466 }
3467
3468 /* writeout dirty page into bdev */
3469 f2fs_submit_page_write(fio);
3470 if (fio->retry) {
3471 fio->old_blkaddr = fio->new_blkaddr;
3472 goto reallocate;
3473 }
3474
3475 f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1);
3476
3477 if (keep_order)
3478 f2fs_up_read(&fio->sbi->io_order_lock);
3479 }
3480
f2fs_do_write_meta_page(struct f2fs_sb_info * sbi,struct page * page,enum iostat_type io_type)3481 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3482 enum iostat_type io_type)
3483 {
3484 struct f2fs_io_info fio = {
3485 .sbi = sbi,
3486 .type = META,
3487 .temp = HOT,
3488 .op = REQ_OP_WRITE,
3489 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
3490 .old_blkaddr = page->index,
3491 .new_blkaddr = page->index,
3492 .page = page,
3493 .encrypted_page = NULL,
3494 .in_list = 0,
3495 };
3496
3497 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
3498 fio.op_flags &= ~REQ_META;
3499
3500 set_page_writeback(page);
3501 f2fs_submit_page_write(&fio);
3502
3503 stat_inc_meta_count(sbi, page->index);
3504 f2fs_update_iostat(sbi, NULL, io_type, F2FS_BLKSIZE);
3505 }
3506
f2fs_do_write_node_page(unsigned int nid,struct f2fs_io_info * fio)3507 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
3508 {
3509 struct f2fs_summary sum;
3510
3511 set_summary(&sum, nid, 0, 0);
3512 do_write_page(&sum, fio);
3513
3514 f2fs_update_iostat(fio->sbi, NULL, fio->io_type, F2FS_BLKSIZE);
3515 }
3516
f2fs_outplace_write_data(struct dnode_of_data * dn,struct f2fs_io_info * fio)3517 void f2fs_outplace_write_data(struct dnode_of_data *dn,
3518 struct f2fs_io_info *fio)
3519 {
3520 struct f2fs_sb_info *sbi = fio->sbi;
3521 struct f2fs_summary sum;
3522
3523 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
3524 if (fio->io_type == FS_DATA_IO || fio->io_type == FS_CP_DATA_IO)
3525 f2fs_update_age_extent_cache(dn);
3526 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
3527 do_write_page(&sum, fio);
3528 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
3529
3530 f2fs_update_iostat(sbi, dn->inode, fio->io_type, F2FS_BLKSIZE);
3531 }
3532
f2fs_inplace_write_data(struct f2fs_io_info * fio)3533 int f2fs_inplace_write_data(struct f2fs_io_info *fio)
3534 {
3535 int err;
3536 struct f2fs_sb_info *sbi = fio->sbi;
3537 unsigned int segno;
3538
3539 fio->new_blkaddr = fio->old_blkaddr;
3540 /* i/o temperature is needed for passing down write hints */
3541 __get_segment_type(fio);
3542
3543 segno = GET_SEGNO(sbi, fio->new_blkaddr);
3544
3545 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
3546 set_sbi_flag(sbi, SBI_NEED_FSCK);
3547 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
3548 __func__, segno);
3549 err = -EFSCORRUPTED;
3550 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE);
3551 goto drop_bio;
3552 }
3553
3554 if (f2fs_cp_error(sbi)) {
3555 err = -EIO;
3556 goto drop_bio;
3557 }
3558
3559 if (fio->post_read)
3560 invalidate_mapping_pages(META_MAPPING(sbi),
3561 fio->new_blkaddr, fio->new_blkaddr);
3562
3563 stat_inc_inplace_blocks(fio->sbi);
3564
3565 if (fio->bio && !IS_F2FS_IPU_NOCACHE(sbi))
3566 err = f2fs_merge_page_bio(fio);
3567 else
3568 err = f2fs_submit_page_bio(fio);
3569 if (!err) {
3570 f2fs_update_device_state(fio->sbi, fio->ino,
3571 fio->new_blkaddr, 1);
3572 f2fs_update_iostat(fio->sbi, fio->page->mapping->host,
3573 fio->io_type, F2FS_BLKSIZE);
3574 }
3575
3576 return err;
3577 drop_bio:
3578 if (fio->bio && *(fio->bio)) {
3579 struct bio *bio = *(fio->bio);
3580
3581 bio->bi_status = BLK_STS_IOERR;
3582 bio_endio(bio);
3583 *(fio->bio) = NULL;
3584 }
3585 return err;
3586 }
3587
__f2fs_get_curseg(struct f2fs_sb_info * sbi,unsigned int segno)3588 static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3589 unsigned int segno)
3590 {
3591 int i;
3592
3593 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3594 if (CURSEG_I(sbi, i)->segno == segno)
3595 break;
3596 }
3597 return i;
3598 }
3599
f2fs_do_replace_block(struct f2fs_sb_info * sbi,struct f2fs_summary * sum,block_t old_blkaddr,block_t new_blkaddr,bool recover_curseg,bool recover_newaddr,bool from_gc)3600 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3601 block_t old_blkaddr, block_t new_blkaddr,
3602 bool recover_curseg, bool recover_newaddr,
3603 bool from_gc)
3604 {
3605 struct sit_info *sit_i = SIT_I(sbi);
3606 struct curseg_info *curseg;
3607 unsigned int segno, old_cursegno;
3608 struct seg_entry *se;
3609 int type;
3610 unsigned short old_blkoff;
3611 unsigned char old_alloc_type;
3612
3613 segno = GET_SEGNO(sbi, new_blkaddr);
3614 se = get_seg_entry(sbi, segno);
3615 type = se->type;
3616
3617 f2fs_down_write(&SM_I(sbi)->curseg_lock);
3618
3619 if (!recover_curseg) {
3620 /* for recovery flow */
3621 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3622 if (old_blkaddr == NULL_ADDR)
3623 type = CURSEG_COLD_DATA;
3624 else
3625 type = CURSEG_WARM_DATA;
3626 }
3627 } else {
3628 if (IS_CURSEG(sbi, segno)) {
3629 /* se->type is volatile as SSR allocation */
3630 type = __f2fs_get_curseg(sbi, segno);
3631 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3632 } else {
3633 type = CURSEG_WARM_DATA;
3634 }
3635 }
3636
3637 f2fs_bug_on(sbi, !IS_DATASEG(type));
3638 curseg = CURSEG_I(sbi, type);
3639
3640 mutex_lock(&curseg->curseg_mutex);
3641 down_write(&sit_i->sentry_lock);
3642
3643 old_cursegno = curseg->segno;
3644 old_blkoff = curseg->next_blkoff;
3645 old_alloc_type = curseg->alloc_type;
3646
3647 /* change the current segment */
3648 if (segno != curseg->segno) {
3649 curseg->next_segno = segno;
3650 change_curseg(sbi, type);
3651 }
3652
3653 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
3654 curseg->sum_blk->entries[curseg->next_blkoff] = *sum;
3655
3656 if (!recover_curseg || recover_newaddr) {
3657 if (!from_gc)
3658 update_segment_mtime(sbi, new_blkaddr, 0);
3659 update_sit_entry(sbi, new_blkaddr, 1);
3660 }
3661 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3662 invalidate_mapping_pages(META_MAPPING(sbi),
3663 old_blkaddr, old_blkaddr);
3664 f2fs_invalidate_compress_page(sbi, old_blkaddr);
3665 if (!from_gc)
3666 update_segment_mtime(sbi, old_blkaddr, 0);
3667 update_sit_entry(sbi, old_blkaddr, -1);
3668 }
3669
3670 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3671 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3672
3673 locate_dirty_segment(sbi, old_cursegno);
3674
3675 if (recover_curseg) {
3676 if (old_cursegno != curseg->segno) {
3677 curseg->next_segno = old_cursegno;
3678 change_curseg(sbi, type);
3679 }
3680 curseg->next_blkoff = old_blkoff;
3681 curseg->alloc_type = old_alloc_type;
3682 }
3683
3684 up_write(&sit_i->sentry_lock);
3685 mutex_unlock(&curseg->curseg_mutex);
3686 f2fs_up_write(&SM_I(sbi)->curseg_lock);
3687 }
3688
f2fs_replace_block(struct f2fs_sb_info * sbi,struct dnode_of_data * dn,block_t old_addr,block_t new_addr,unsigned char version,bool recover_curseg,bool recover_newaddr)3689 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3690 block_t old_addr, block_t new_addr,
3691 unsigned char version, bool recover_curseg,
3692 bool recover_newaddr)
3693 {
3694 struct f2fs_summary sum;
3695
3696 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3697
3698 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
3699 recover_curseg, recover_newaddr, false);
3700
3701 f2fs_update_data_blkaddr(dn, new_addr);
3702 }
3703
f2fs_wait_on_page_writeback(struct page * page,enum page_type type,bool ordered,bool locked)3704 void f2fs_wait_on_page_writeback(struct page *page,
3705 enum page_type type, bool ordered, bool locked)
3706 {
3707 if (PageWriteback(page)) {
3708 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3709
3710 /* submit cached LFS IO */
3711 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type);
3712 /* submit cached IPU IO */
3713 f2fs_submit_merged_ipu_write(sbi, NULL, page);
3714 if (ordered) {
3715 wait_on_page_writeback(page);
3716 f2fs_bug_on(sbi, locked && PageWriteback(page));
3717 } else {
3718 wait_for_stable_page(page);
3719 }
3720 }
3721 }
3722
f2fs_wait_on_block_writeback(struct inode * inode,block_t blkaddr)3723 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
3724 {
3725 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3726 struct page *cpage;
3727
3728 if (!f2fs_post_read_required(inode))
3729 return;
3730
3731 if (!__is_valid_data_blkaddr(blkaddr))
3732 return;
3733
3734 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3735 if (cpage) {
3736 f2fs_wait_on_page_writeback(cpage, DATA, true, true);
3737 f2fs_put_page(cpage, 1);
3738 }
3739 }
3740
f2fs_wait_on_block_writeback_range(struct inode * inode,block_t blkaddr,block_t len)3741 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3742 block_t len)
3743 {
3744 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3745 block_t i;
3746
3747 if (!f2fs_post_read_required(inode))
3748 return;
3749
3750 for (i = 0; i < len; i++)
3751 f2fs_wait_on_block_writeback(inode, blkaddr + i);
3752
3753 invalidate_mapping_pages(META_MAPPING(sbi), blkaddr, blkaddr + len - 1);
3754 }
3755
read_compacted_summaries(struct f2fs_sb_info * sbi)3756 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
3757 {
3758 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3759 struct curseg_info *seg_i;
3760 unsigned char *kaddr;
3761 struct page *page;
3762 block_t start;
3763 int i, j, offset;
3764
3765 start = start_sum_block(sbi);
3766
3767 page = f2fs_get_meta_page(sbi, start++);
3768 if (IS_ERR(page))
3769 return PTR_ERR(page);
3770 kaddr = (unsigned char *)page_address(page);
3771
3772 /* Step 1: restore nat cache */
3773 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
3774 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
3775
3776 /* Step 2: restore sit cache */
3777 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
3778 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
3779 offset = 2 * SUM_JOURNAL_SIZE;
3780
3781 /* Step 3: restore summary entries */
3782 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3783 unsigned short blk_off;
3784 unsigned int segno;
3785
3786 seg_i = CURSEG_I(sbi, i);
3787 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3788 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3789 seg_i->next_segno = segno;
3790 reset_curseg(sbi, i, 0);
3791 seg_i->alloc_type = ckpt->alloc_type[i];
3792 seg_i->next_blkoff = blk_off;
3793
3794 if (seg_i->alloc_type == SSR)
3795 blk_off = sbi->blocks_per_seg;
3796
3797 for (j = 0; j < blk_off; j++) {
3798 struct f2fs_summary *s;
3799
3800 s = (struct f2fs_summary *)(kaddr + offset);
3801 seg_i->sum_blk->entries[j] = *s;
3802 offset += SUMMARY_SIZE;
3803 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
3804 SUM_FOOTER_SIZE)
3805 continue;
3806
3807 f2fs_put_page(page, 1);
3808 page = NULL;
3809
3810 page = f2fs_get_meta_page(sbi, start++);
3811 if (IS_ERR(page))
3812 return PTR_ERR(page);
3813 kaddr = (unsigned char *)page_address(page);
3814 offset = 0;
3815 }
3816 }
3817 f2fs_put_page(page, 1);
3818 return 0;
3819 }
3820
read_normal_summaries(struct f2fs_sb_info * sbi,int type)3821 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3822 {
3823 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3824 struct f2fs_summary_block *sum;
3825 struct curseg_info *curseg;
3826 struct page *new;
3827 unsigned short blk_off;
3828 unsigned int segno = 0;
3829 block_t blk_addr = 0;
3830 int err = 0;
3831
3832 /* get segment number and block addr */
3833 if (IS_DATASEG(type)) {
3834 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3835 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3836 CURSEG_HOT_DATA]);
3837 if (__exist_node_summaries(sbi))
3838 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type);
3839 else
3840 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3841 } else {
3842 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3843 CURSEG_HOT_NODE]);
3844 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3845 CURSEG_HOT_NODE]);
3846 if (__exist_node_summaries(sbi))
3847 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3848 type - CURSEG_HOT_NODE);
3849 else
3850 blk_addr = GET_SUM_BLOCK(sbi, segno);
3851 }
3852
3853 new = f2fs_get_meta_page(sbi, blk_addr);
3854 if (IS_ERR(new))
3855 return PTR_ERR(new);
3856 sum = (struct f2fs_summary_block *)page_address(new);
3857
3858 if (IS_NODESEG(type)) {
3859 if (__exist_node_summaries(sbi)) {
3860 struct f2fs_summary *ns = &sum->entries[0];
3861 int i;
3862
3863 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3864 ns->version = 0;
3865 ns->ofs_in_node = 0;
3866 }
3867 } else {
3868 err = f2fs_restore_node_summary(sbi, segno, sum);
3869 if (err)
3870 goto out;
3871 }
3872 }
3873
3874 /* set uncompleted segment to curseg */
3875 curseg = CURSEG_I(sbi, type);
3876 mutex_lock(&curseg->curseg_mutex);
3877
3878 /* update journal info */
3879 down_write(&curseg->journal_rwsem);
3880 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3881 up_write(&curseg->journal_rwsem);
3882
3883 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3884 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
3885 curseg->next_segno = segno;
3886 reset_curseg(sbi, type, 0);
3887 curseg->alloc_type = ckpt->alloc_type[type];
3888 curseg->next_blkoff = blk_off;
3889 mutex_unlock(&curseg->curseg_mutex);
3890 out:
3891 f2fs_put_page(new, 1);
3892 return err;
3893 }
3894
restore_curseg_summaries(struct f2fs_sb_info * sbi)3895 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3896 {
3897 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3898 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
3899 int type = CURSEG_HOT_DATA;
3900 int err;
3901
3902 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
3903 int npages = f2fs_npages_for_summary_flush(sbi, true);
3904
3905 if (npages >= 2)
3906 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
3907 META_CP, true);
3908
3909 /* restore for compacted data summary */
3910 err = read_compacted_summaries(sbi);
3911 if (err)
3912 return err;
3913 type = CURSEG_HOT_NODE;
3914 }
3915
3916 if (__exist_node_summaries(sbi))
3917 f2fs_ra_meta_pages(sbi,
3918 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
3919 NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
3920
3921 for (; type <= CURSEG_COLD_NODE; type++) {
3922 err = read_normal_summaries(sbi, type);
3923 if (err)
3924 return err;
3925 }
3926
3927 /* sanity check for summary blocks */
3928 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
3929 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) {
3930 f2fs_err(sbi, "invalid journal entries nats %u sits %u",
3931 nats_in_cursum(nat_j), sits_in_cursum(sit_j));
3932 return -EINVAL;
3933 }
3934
3935 return 0;
3936 }
3937
write_compacted_summaries(struct f2fs_sb_info * sbi,block_t blkaddr)3938 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
3939 {
3940 struct page *page;
3941 unsigned char *kaddr;
3942 struct f2fs_summary *summary;
3943 struct curseg_info *seg_i;
3944 int written_size = 0;
3945 int i, j;
3946
3947 page = f2fs_grab_meta_page(sbi, blkaddr++);
3948 kaddr = (unsigned char *)page_address(page);
3949 memset(kaddr, 0, PAGE_SIZE);
3950
3951 /* Step 1: write nat cache */
3952 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
3953 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
3954 written_size += SUM_JOURNAL_SIZE;
3955
3956 /* Step 2: write sit cache */
3957 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
3958 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
3959 written_size += SUM_JOURNAL_SIZE;
3960
3961 /* Step 3: write summary entries */
3962 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3963 seg_i = CURSEG_I(sbi, i);
3964 for (j = 0; j < f2fs_curseg_valid_blocks(sbi, i); j++) {
3965 if (!page) {
3966 page = f2fs_grab_meta_page(sbi, blkaddr++);
3967 kaddr = (unsigned char *)page_address(page);
3968 memset(kaddr, 0, PAGE_SIZE);
3969 written_size = 0;
3970 }
3971 summary = (struct f2fs_summary *)(kaddr + written_size);
3972 *summary = seg_i->sum_blk->entries[j];
3973 written_size += SUMMARY_SIZE;
3974
3975 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
3976 SUM_FOOTER_SIZE)
3977 continue;
3978
3979 set_page_dirty(page);
3980 f2fs_put_page(page, 1);
3981 page = NULL;
3982 }
3983 }
3984 if (page) {
3985 set_page_dirty(page);
3986 f2fs_put_page(page, 1);
3987 }
3988 }
3989
write_normal_summaries(struct f2fs_sb_info * sbi,block_t blkaddr,int type)3990 static void write_normal_summaries(struct f2fs_sb_info *sbi,
3991 block_t blkaddr, int type)
3992 {
3993 int i, end;
3994
3995 if (IS_DATASEG(type))
3996 end = type + NR_CURSEG_DATA_TYPE;
3997 else
3998 end = type + NR_CURSEG_NODE_TYPE;
3999
4000 for (i = type; i < end; i++)
4001 write_current_sum_page(sbi, i, blkaddr + (i - type));
4002 }
4003
f2fs_write_data_summaries(struct f2fs_sb_info * sbi,block_t start_blk)4004 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
4005 {
4006 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
4007 write_compacted_summaries(sbi, start_blk);
4008 else
4009 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
4010 }
4011
f2fs_write_node_summaries(struct f2fs_sb_info * sbi,block_t start_blk)4012 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
4013 {
4014 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
4015 }
4016
f2fs_lookup_journal_in_cursum(struct f2fs_journal * journal,int type,unsigned int val,int alloc)4017 int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
4018 unsigned int val, int alloc)
4019 {
4020 int i;
4021
4022 if (type == NAT_JOURNAL) {
4023 for (i = 0; i < nats_in_cursum(journal); i++) {
4024 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
4025 return i;
4026 }
4027 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
4028 return update_nats_in_cursum(journal, 1);
4029 } else if (type == SIT_JOURNAL) {
4030 for (i = 0; i < sits_in_cursum(journal); i++)
4031 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
4032 return i;
4033 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
4034 return update_sits_in_cursum(journal, 1);
4035 }
4036 return -1;
4037 }
4038
get_current_sit_page(struct f2fs_sb_info * sbi,unsigned int segno)4039 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
4040 unsigned int segno)
4041 {
4042 return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno));
4043 }
4044
get_next_sit_page(struct f2fs_sb_info * sbi,unsigned int start)4045 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
4046 unsigned int start)
4047 {
4048 struct sit_info *sit_i = SIT_I(sbi);
4049 struct page *page;
4050 pgoff_t src_off, dst_off;
4051
4052 src_off = current_sit_addr(sbi, start);
4053 dst_off = next_sit_addr(sbi, src_off);
4054
4055 page = f2fs_grab_meta_page(sbi, dst_off);
4056 seg_info_to_sit_page(sbi, page, start);
4057
4058 set_page_dirty(page);
4059 set_to_next_sit(sit_i, start);
4060
4061 return page;
4062 }
4063
grab_sit_entry_set(void)4064 static struct sit_entry_set *grab_sit_entry_set(void)
4065 {
4066 struct sit_entry_set *ses =
4067 f2fs_kmem_cache_alloc(sit_entry_set_slab,
4068 GFP_NOFS, true, NULL);
4069
4070 ses->entry_cnt = 0;
4071 INIT_LIST_HEAD(&ses->set_list);
4072 return ses;
4073 }
4074
release_sit_entry_set(struct sit_entry_set * ses)4075 static void release_sit_entry_set(struct sit_entry_set *ses)
4076 {
4077 list_del(&ses->set_list);
4078 kmem_cache_free(sit_entry_set_slab, ses);
4079 }
4080
adjust_sit_entry_set(struct sit_entry_set * ses,struct list_head * head)4081 static void adjust_sit_entry_set(struct sit_entry_set *ses,
4082 struct list_head *head)
4083 {
4084 struct sit_entry_set *next = ses;
4085
4086 if (list_is_last(&ses->set_list, head))
4087 return;
4088
4089 list_for_each_entry_continue(next, head, set_list)
4090 if (ses->entry_cnt <= next->entry_cnt) {
4091 list_move_tail(&ses->set_list, &next->set_list);
4092 return;
4093 }
4094
4095 list_move_tail(&ses->set_list, head);
4096 }
4097
add_sit_entry(unsigned int segno,struct list_head * head)4098 static void add_sit_entry(unsigned int segno, struct list_head *head)
4099 {
4100 struct sit_entry_set *ses;
4101 unsigned int start_segno = START_SEGNO(segno);
4102
4103 list_for_each_entry(ses, head, set_list) {
4104 if (ses->start_segno == start_segno) {
4105 ses->entry_cnt++;
4106 adjust_sit_entry_set(ses, head);
4107 return;
4108 }
4109 }
4110
4111 ses = grab_sit_entry_set();
4112
4113 ses->start_segno = start_segno;
4114 ses->entry_cnt++;
4115 list_add(&ses->set_list, head);
4116 }
4117
add_sits_in_set(struct f2fs_sb_info * sbi)4118 static void add_sits_in_set(struct f2fs_sb_info *sbi)
4119 {
4120 struct f2fs_sm_info *sm_info = SM_I(sbi);
4121 struct list_head *set_list = &sm_info->sit_entry_set;
4122 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
4123 unsigned int segno;
4124
4125 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
4126 add_sit_entry(segno, set_list);
4127 }
4128
remove_sits_in_journal(struct f2fs_sb_info * sbi)4129 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
4130 {
4131 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4132 struct f2fs_journal *journal = curseg->journal;
4133 int i;
4134
4135 down_write(&curseg->journal_rwsem);
4136 for (i = 0; i < sits_in_cursum(journal); i++) {
4137 unsigned int segno;
4138 bool dirtied;
4139
4140 segno = le32_to_cpu(segno_in_journal(journal, i));
4141 dirtied = __mark_sit_entry_dirty(sbi, segno);
4142
4143 if (!dirtied)
4144 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
4145 }
4146 update_sits_in_cursum(journal, -i);
4147 up_write(&curseg->journal_rwsem);
4148 }
4149
4150 /*
4151 * CP calls this function, which flushes SIT entries including sit_journal,
4152 * and moves prefree segs to free segs.
4153 */
f2fs_flush_sit_entries(struct f2fs_sb_info * sbi,struct cp_control * cpc)4154 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
4155 {
4156 struct sit_info *sit_i = SIT_I(sbi);
4157 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
4158 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4159 struct f2fs_journal *journal = curseg->journal;
4160 struct sit_entry_set *ses, *tmp;
4161 struct list_head *head = &SM_I(sbi)->sit_entry_set;
4162 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
4163 struct seg_entry *se;
4164
4165 down_write(&sit_i->sentry_lock);
4166
4167 if (!sit_i->dirty_sentries)
4168 goto out;
4169
4170 /*
4171 * add and account sit entries of dirty bitmap in sit entry
4172 * set temporarily
4173 */
4174 add_sits_in_set(sbi);
4175
4176 /*
4177 * if there are no enough space in journal to store dirty sit
4178 * entries, remove all entries from journal and add and account
4179 * them in sit entry set.
4180 */
4181 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
4182 !to_journal)
4183 remove_sits_in_journal(sbi);
4184
4185 /*
4186 * there are two steps to flush sit entries:
4187 * #1, flush sit entries to journal in current cold data summary block.
4188 * #2, flush sit entries to sit page.
4189 */
4190 list_for_each_entry_safe(ses, tmp, head, set_list) {
4191 struct page *page = NULL;
4192 struct f2fs_sit_block *raw_sit = NULL;
4193 unsigned int start_segno = ses->start_segno;
4194 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
4195 (unsigned long)MAIN_SEGS(sbi));
4196 unsigned int segno = start_segno;
4197
4198 if (to_journal &&
4199 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
4200 to_journal = false;
4201
4202 if (to_journal) {
4203 down_write(&curseg->journal_rwsem);
4204 } else {
4205 page = get_next_sit_page(sbi, start_segno);
4206 raw_sit = page_address(page);
4207 }
4208
4209 /* flush dirty sit entries in region of current sit set */
4210 for_each_set_bit_from(segno, bitmap, end) {
4211 int offset, sit_offset;
4212
4213 se = get_seg_entry(sbi, segno);
4214 #ifdef CONFIG_F2FS_CHECK_FS
4215 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
4216 SIT_VBLOCK_MAP_SIZE))
4217 f2fs_bug_on(sbi, 1);
4218 #endif
4219
4220 /* add discard candidates */
4221 if (!(cpc->reason & CP_DISCARD)) {
4222 cpc->trim_start = segno;
4223 add_discard_addrs(sbi, cpc, false);
4224 }
4225
4226 if (to_journal) {
4227 offset = f2fs_lookup_journal_in_cursum(journal,
4228 SIT_JOURNAL, segno, 1);
4229 f2fs_bug_on(sbi, offset < 0);
4230 segno_in_journal(journal, offset) =
4231 cpu_to_le32(segno);
4232 seg_info_to_raw_sit(se,
4233 &sit_in_journal(journal, offset));
4234 check_block_count(sbi, segno,
4235 &sit_in_journal(journal, offset));
4236 } else {
4237 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
4238 seg_info_to_raw_sit(se,
4239 &raw_sit->entries[sit_offset]);
4240 check_block_count(sbi, segno,
4241 &raw_sit->entries[sit_offset]);
4242 }
4243
4244 __clear_bit(segno, bitmap);
4245 sit_i->dirty_sentries--;
4246 ses->entry_cnt--;
4247 }
4248
4249 if (to_journal)
4250 up_write(&curseg->journal_rwsem);
4251 else
4252 f2fs_put_page(page, 1);
4253
4254 f2fs_bug_on(sbi, ses->entry_cnt);
4255 release_sit_entry_set(ses);
4256 }
4257
4258 f2fs_bug_on(sbi, !list_empty(head));
4259 f2fs_bug_on(sbi, sit_i->dirty_sentries);
4260 out:
4261 if (cpc->reason & CP_DISCARD) {
4262 __u64 trim_start = cpc->trim_start;
4263
4264 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
4265 add_discard_addrs(sbi, cpc, false);
4266
4267 cpc->trim_start = trim_start;
4268 }
4269 up_write(&sit_i->sentry_lock);
4270
4271 set_prefree_as_free_segments(sbi);
4272 }
4273
build_sit_info(struct f2fs_sb_info * sbi)4274 static int build_sit_info(struct f2fs_sb_info *sbi)
4275 {
4276 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4277 struct sit_info *sit_i;
4278 unsigned int sit_segs, start;
4279 char *src_bitmap, *bitmap;
4280 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
4281 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0;
4282
4283 /* allocate memory for SIT information */
4284 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
4285 if (!sit_i)
4286 return -ENOMEM;
4287
4288 SM_I(sbi)->sit_info = sit_i;
4289
4290 sit_i->sentries =
4291 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
4292 MAIN_SEGS(sbi)),
4293 GFP_KERNEL);
4294 if (!sit_i->sentries)
4295 return -ENOMEM;
4296
4297 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4298 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
4299 GFP_KERNEL);
4300 if (!sit_i->dirty_sentries_bitmap)
4301 return -ENOMEM;
4302
4303 #ifdef CONFIG_F2FS_CHECK_FS
4304 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map);
4305 #else
4306 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map);
4307 #endif
4308 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4309 if (!sit_i->bitmap)
4310 return -ENOMEM;
4311
4312 bitmap = sit_i->bitmap;
4313
4314 for (start = 0; start < MAIN_SEGS(sbi); start++) {
4315 sit_i->sentries[start].cur_valid_map = bitmap;
4316 bitmap += SIT_VBLOCK_MAP_SIZE;
4317
4318 sit_i->sentries[start].ckpt_valid_map = bitmap;
4319 bitmap += SIT_VBLOCK_MAP_SIZE;
4320
4321 #ifdef CONFIG_F2FS_CHECK_FS
4322 sit_i->sentries[start].cur_valid_map_mir = bitmap;
4323 bitmap += SIT_VBLOCK_MAP_SIZE;
4324 #endif
4325
4326 if (discard_map) {
4327 sit_i->sentries[start].discard_map = bitmap;
4328 bitmap += SIT_VBLOCK_MAP_SIZE;
4329 }
4330 }
4331
4332 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
4333 if (!sit_i->tmp_map)
4334 return -ENOMEM;
4335
4336 if (__is_large_section(sbi)) {
4337 sit_i->sec_entries =
4338 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
4339 MAIN_SECS(sbi)),
4340 GFP_KERNEL);
4341 if (!sit_i->sec_entries)
4342 return -ENOMEM;
4343 }
4344
4345 /* get information related with SIT */
4346 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
4347
4348 /* setup SIT bitmap from ckeckpoint pack */
4349 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
4350 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
4351
4352 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
4353 if (!sit_i->sit_bitmap)
4354 return -ENOMEM;
4355
4356 #ifdef CONFIG_F2FS_CHECK_FS
4357 sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4358 sit_bitmap_size, GFP_KERNEL);
4359 if (!sit_i->sit_bitmap_mir)
4360 return -ENOMEM;
4361
4362 sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4363 main_bitmap_size, GFP_KERNEL);
4364 if (!sit_i->invalid_segmap)
4365 return -ENOMEM;
4366 #endif
4367
4368 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
4369 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
4370 sit_i->written_valid_blocks = 0;
4371 sit_i->bitmap_size = sit_bitmap_size;
4372 sit_i->dirty_sentries = 0;
4373 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
4374 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
4375 sit_i->mounted_time = ktime_get_boottime_seconds();
4376 init_rwsem(&sit_i->sentry_lock);
4377 return 0;
4378 }
4379
build_free_segmap(struct f2fs_sb_info * sbi)4380 static int build_free_segmap(struct f2fs_sb_info *sbi)
4381 {
4382 struct free_segmap_info *free_i;
4383 unsigned int bitmap_size, sec_bitmap_size;
4384
4385 /* allocate memory for free segmap information */
4386 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
4387 if (!free_i)
4388 return -ENOMEM;
4389
4390 SM_I(sbi)->free_info = free_i;
4391
4392 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4393 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
4394 if (!free_i->free_segmap)
4395 return -ENOMEM;
4396
4397 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4398 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
4399 if (!free_i->free_secmap)
4400 return -ENOMEM;
4401
4402 /* set all segments as dirty temporarily */
4403 memset(free_i->free_segmap, 0xff, bitmap_size);
4404 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
4405
4406 /* init free segmap information */
4407 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
4408 free_i->free_segments = 0;
4409 free_i->free_sections = 0;
4410 spin_lock_init(&free_i->segmap_lock);
4411 return 0;
4412 }
4413
build_curseg(struct f2fs_sb_info * sbi)4414 static int build_curseg(struct f2fs_sb_info *sbi)
4415 {
4416 struct curseg_info *array;
4417 int i;
4418
4419 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE,
4420 sizeof(*array)), GFP_KERNEL);
4421 if (!array)
4422 return -ENOMEM;
4423
4424 SM_I(sbi)->curseg_array = array;
4425
4426 for (i = 0; i < NO_CHECK_TYPE; i++) {
4427 mutex_init(&array[i].curseg_mutex);
4428 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
4429 if (!array[i].sum_blk)
4430 return -ENOMEM;
4431 init_rwsem(&array[i].journal_rwsem);
4432 array[i].journal = f2fs_kzalloc(sbi,
4433 sizeof(struct f2fs_journal), GFP_KERNEL);
4434 if (!array[i].journal)
4435 return -ENOMEM;
4436 if (i < NR_PERSISTENT_LOG)
4437 array[i].seg_type = CURSEG_HOT_DATA + i;
4438 else if (i == CURSEG_COLD_DATA_PINNED)
4439 array[i].seg_type = CURSEG_COLD_DATA;
4440 else if (i == CURSEG_ALL_DATA_ATGC)
4441 array[i].seg_type = CURSEG_COLD_DATA;
4442 array[i].segno = NULL_SEGNO;
4443 array[i].next_blkoff = 0;
4444 array[i].inited = false;
4445 }
4446 return restore_curseg_summaries(sbi);
4447 }
4448
build_sit_entries(struct f2fs_sb_info * sbi)4449 static int build_sit_entries(struct f2fs_sb_info *sbi)
4450 {
4451 struct sit_info *sit_i = SIT_I(sbi);
4452 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4453 struct f2fs_journal *journal = curseg->journal;
4454 struct seg_entry *se;
4455 struct f2fs_sit_entry sit;
4456 int sit_blk_cnt = SIT_BLK_CNT(sbi);
4457 unsigned int i, start, end;
4458 unsigned int readed, start_blk = 0;
4459 int err = 0;
4460 block_t sit_valid_blocks[2] = {0, 0};
4461
4462 do {
4463 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
4464 META_SIT, true);
4465
4466 start = start_blk * sit_i->sents_per_block;
4467 end = (start_blk + readed) * sit_i->sents_per_block;
4468
4469 for (; start < end && start < MAIN_SEGS(sbi); start++) {
4470 struct f2fs_sit_block *sit_blk;
4471 struct page *page;
4472
4473 se = &sit_i->sentries[start];
4474 page = get_current_sit_page(sbi, start);
4475 if (IS_ERR(page))
4476 return PTR_ERR(page);
4477 sit_blk = (struct f2fs_sit_block *)page_address(page);
4478 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
4479 f2fs_put_page(page, 1);
4480
4481 err = check_block_count(sbi, start, &sit);
4482 if (err)
4483 return err;
4484 seg_info_from_raw_sit(se, &sit);
4485
4486 if (se->type >= NR_PERSISTENT_LOG) {
4487 f2fs_err(sbi, "Invalid segment type: %u, segno: %u",
4488 se->type, start);
4489 f2fs_handle_error(sbi,
4490 ERROR_INCONSISTENT_SUM_TYPE);
4491 return -EFSCORRUPTED;
4492 }
4493
4494 sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
4495
4496 if (f2fs_block_unit_discard(sbi)) {
4497 /* build discard map only one time */
4498 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4499 memset(se->discard_map, 0xff,
4500 SIT_VBLOCK_MAP_SIZE);
4501 } else {
4502 memcpy(se->discard_map,
4503 se->cur_valid_map,
4504 SIT_VBLOCK_MAP_SIZE);
4505 sbi->discard_blks +=
4506 sbi->blocks_per_seg -
4507 se->valid_blocks;
4508 }
4509 }
4510
4511 if (__is_large_section(sbi))
4512 get_sec_entry(sbi, start)->valid_blocks +=
4513 se->valid_blocks;
4514 }
4515 start_blk += readed;
4516 } while (start_blk < sit_blk_cnt);
4517
4518 down_read(&curseg->journal_rwsem);
4519 for (i = 0; i < sits_in_cursum(journal); i++) {
4520 unsigned int old_valid_blocks;
4521
4522 start = le32_to_cpu(segno_in_journal(journal, i));
4523 if (start >= MAIN_SEGS(sbi)) {
4524 f2fs_err(sbi, "Wrong journal entry on segno %u",
4525 start);
4526 err = -EFSCORRUPTED;
4527 f2fs_handle_error(sbi, ERROR_CORRUPTED_JOURNAL);
4528 break;
4529 }
4530
4531 se = &sit_i->sentries[start];
4532 sit = sit_in_journal(journal, i);
4533
4534 old_valid_blocks = se->valid_blocks;
4535
4536 sit_valid_blocks[SE_PAGETYPE(se)] -= old_valid_blocks;
4537
4538 err = check_block_count(sbi, start, &sit);
4539 if (err)
4540 break;
4541 seg_info_from_raw_sit(se, &sit);
4542
4543 if (se->type >= NR_PERSISTENT_LOG) {
4544 f2fs_err(sbi, "Invalid segment type: %u, segno: %u",
4545 se->type, start);
4546 err = -EFSCORRUPTED;
4547 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE);
4548 break;
4549 }
4550
4551 sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
4552
4553 if (f2fs_block_unit_discard(sbi)) {
4554 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4555 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
4556 } else {
4557 memcpy(se->discard_map, se->cur_valid_map,
4558 SIT_VBLOCK_MAP_SIZE);
4559 sbi->discard_blks += old_valid_blocks;
4560 sbi->discard_blks -= se->valid_blocks;
4561 }
4562 }
4563
4564 if (__is_large_section(sbi)) {
4565 get_sec_entry(sbi, start)->valid_blocks +=
4566 se->valid_blocks;
4567 get_sec_entry(sbi, start)->valid_blocks -=
4568 old_valid_blocks;
4569 }
4570 }
4571 up_read(&curseg->journal_rwsem);
4572
4573 if (err)
4574 return err;
4575
4576 if (sit_valid_blocks[NODE] != valid_node_count(sbi)) {
4577 f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4578 sit_valid_blocks[NODE], valid_node_count(sbi));
4579 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NODE_COUNT);
4580 return -EFSCORRUPTED;
4581 }
4582
4583 if (sit_valid_blocks[DATA] + sit_valid_blocks[NODE] >
4584 valid_user_blocks(sbi)) {
4585 f2fs_err(sbi, "SIT is corrupted data# %u %u vs %u",
4586 sit_valid_blocks[DATA], sit_valid_blocks[NODE],
4587 valid_user_blocks(sbi));
4588 f2fs_handle_error(sbi, ERROR_INCONSISTENT_BLOCK_COUNT);
4589 return -EFSCORRUPTED;
4590 }
4591
4592 return 0;
4593 }
4594
init_free_segmap(struct f2fs_sb_info * sbi)4595 static void init_free_segmap(struct f2fs_sb_info *sbi)
4596 {
4597 unsigned int start;
4598 int type;
4599 struct seg_entry *sentry;
4600
4601 for (start = 0; start < MAIN_SEGS(sbi); start++) {
4602 if (f2fs_usable_blks_in_seg(sbi, start) == 0)
4603 continue;
4604 sentry = get_seg_entry(sbi, start);
4605 if (!sentry->valid_blocks)
4606 __set_free(sbi, start);
4607 else
4608 SIT_I(sbi)->written_valid_blocks +=
4609 sentry->valid_blocks;
4610 }
4611
4612 /* set use the current segments */
4613 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4614 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
4615
4616 __set_test_and_inuse(sbi, curseg_t->segno);
4617 }
4618 }
4619
init_dirty_segmap(struct f2fs_sb_info * sbi)4620 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4621 {
4622 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4623 struct free_segmap_info *free_i = FREE_I(sbi);
4624 unsigned int segno = 0, offset = 0, secno;
4625 block_t valid_blocks, usable_blks_in_seg;
4626
4627 while (1) {
4628 /* find dirty segment based on free segmap */
4629 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4630 if (segno >= MAIN_SEGS(sbi))
4631 break;
4632 offset = segno + 1;
4633 valid_blocks = get_valid_blocks(sbi, segno, false);
4634 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
4635 if (valid_blocks == usable_blks_in_seg || !valid_blocks)
4636 continue;
4637 if (valid_blocks > usable_blks_in_seg) {
4638 f2fs_bug_on(sbi, 1);
4639 continue;
4640 }
4641 mutex_lock(&dirty_i->seglist_lock);
4642 __locate_dirty_segment(sbi, segno, DIRTY);
4643 mutex_unlock(&dirty_i->seglist_lock);
4644 }
4645
4646 if (!__is_large_section(sbi))
4647 return;
4648
4649 mutex_lock(&dirty_i->seglist_lock);
4650 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
4651 valid_blocks = get_valid_blocks(sbi, segno, true);
4652 secno = GET_SEC_FROM_SEG(sbi, segno);
4653
4654 if (!valid_blocks || valid_blocks == CAP_BLKS_PER_SEC(sbi))
4655 continue;
4656 if (IS_CURSEC(sbi, secno))
4657 continue;
4658 set_bit(secno, dirty_i->dirty_secmap);
4659 }
4660 mutex_unlock(&dirty_i->seglist_lock);
4661 }
4662
init_victim_secmap(struct f2fs_sb_info * sbi)4663 static int init_victim_secmap(struct f2fs_sb_info *sbi)
4664 {
4665 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4666 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4667
4668 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4669 if (!dirty_i->victim_secmap)
4670 return -ENOMEM;
4671
4672 dirty_i->pinned_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4673 if (!dirty_i->pinned_secmap)
4674 return -ENOMEM;
4675
4676 dirty_i->pinned_secmap_cnt = 0;
4677 dirty_i->enable_pin_section = true;
4678 return 0;
4679 }
4680
build_dirty_segmap(struct f2fs_sb_info * sbi)4681 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4682 {
4683 struct dirty_seglist_info *dirty_i;
4684 unsigned int bitmap_size, i;
4685
4686 /* allocate memory for dirty segments list information */
4687 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4688 GFP_KERNEL);
4689 if (!dirty_i)
4690 return -ENOMEM;
4691
4692 SM_I(sbi)->dirty_info = dirty_i;
4693 mutex_init(&dirty_i->seglist_lock);
4694
4695 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4696
4697 for (i = 0; i < NR_DIRTY_TYPE; i++) {
4698 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4699 GFP_KERNEL);
4700 if (!dirty_i->dirty_segmap[i])
4701 return -ENOMEM;
4702 }
4703
4704 if (__is_large_section(sbi)) {
4705 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4706 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
4707 bitmap_size, GFP_KERNEL);
4708 if (!dirty_i->dirty_secmap)
4709 return -ENOMEM;
4710 }
4711
4712 init_dirty_segmap(sbi);
4713 return init_victim_secmap(sbi);
4714 }
4715
sanity_check_curseg(struct f2fs_sb_info * sbi)4716 static int sanity_check_curseg(struct f2fs_sb_info *sbi)
4717 {
4718 int i;
4719
4720 /*
4721 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
4722 * In LFS curseg, all blkaddr after .next_blkoff should be unused.
4723 */
4724 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
4725 struct curseg_info *curseg = CURSEG_I(sbi, i);
4726 struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
4727 unsigned int blkofs = curseg->next_blkoff;
4728
4729 if (f2fs_sb_has_readonly(sbi) &&
4730 i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
4731 continue;
4732
4733 sanity_check_seg_type(sbi, curseg->seg_type);
4734
4735 if (curseg->alloc_type != LFS && curseg->alloc_type != SSR) {
4736 f2fs_err(sbi,
4737 "Current segment has invalid alloc_type:%d",
4738 curseg->alloc_type);
4739 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG);
4740 return -EFSCORRUPTED;
4741 }
4742
4743 if (f2fs_test_bit(blkofs, se->cur_valid_map))
4744 goto out;
4745
4746 if (curseg->alloc_type == SSR)
4747 continue;
4748
4749 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
4750 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
4751 continue;
4752 out:
4753 f2fs_err(sbi,
4754 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
4755 i, curseg->segno, curseg->alloc_type,
4756 curseg->next_blkoff, blkofs);
4757 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG);
4758 return -EFSCORRUPTED;
4759 }
4760 }
4761 return 0;
4762 }
4763
4764 #ifdef CONFIG_BLK_DEV_ZONED
4765
check_zone_write_pointer(struct f2fs_sb_info * sbi,struct f2fs_dev_info * fdev,struct blk_zone * zone)4766 static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
4767 struct f2fs_dev_info *fdev,
4768 struct blk_zone *zone)
4769 {
4770 unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno;
4771 block_t zone_block, wp_block, last_valid_block;
4772 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4773 int i, s, b, ret;
4774 struct seg_entry *se;
4775
4776 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4777 return 0;
4778
4779 wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block);
4780 wp_segno = GET_SEGNO(sbi, wp_block);
4781 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4782 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
4783 zone_segno = GET_SEGNO(sbi, zone_block);
4784 zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno);
4785
4786 if (zone_segno >= MAIN_SEGS(sbi))
4787 return 0;
4788
4789 /*
4790 * Skip check of zones cursegs point to, since
4791 * fix_curseg_write_pointer() checks them.
4792 */
4793 for (i = 0; i < NO_CHECK_TYPE; i++)
4794 if (zone_secno == GET_SEC_FROM_SEG(sbi,
4795 CURSEG_I(sbi, i)->segno))
4796 return 0;
4797
4798 /*
4799 * Get last valid block of the zone.
4800 */
4801 last_valid_block = zone_block - 1;
4802 for (s = sbi->segs_per_sec - 1; s >= 0; s--) {
4803 segno = zone_segno + s;
4804 se = get_seg_entry(sbi, segno);
4805 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
4806 if (f2fs_test_bit(b, se->cur_valid_map)) {
4807 last_valid_block = START_BLOCK(sbi, segno) + b;
4808 break;
4809 }
4810 if (last_valid_block >= zone_block)
4811 break;
4812 }
4813
4814 /*
4815 * If last valid block is beyond the write pointer, report the
4816 * inconsistency. This inconsistency does not cause write error
4817 * because the zone will not be selected for write operation until
4818 * it get discarded. Just report it.
4819 */
4820 if (last_valid_block >= wp_block) {
4821 f2fs_notice(sbi, "Valid block beyond write pointer: "
4822 "valid block[0x%x,0x%x] wp[0x%x,0x%x]",
4823 GET_SEGNO(sbi, last_valid_block),
4824 GET_BLKOFF_FROM_SEG0(sbi, last_valid_block),
4825 wp_segno, wp_blkoff);
4826 return 0;
4827 }
4828
4829 /*
4830 * If there is no valid block in the zone and if write pointer is
4831 * not at zone start, reset the write pointer.
4832 */
4833 if (last_valid_block + 1 == zone_block && zone->wp != zone->start) {
4834 f2fs_notice(sbi,
4835 "Zone without valid block has non-zero write "
4836 "pointer. Reset the write pointer: wp[0x%x,0x%x]",
4837 wp_segno, wp_blkoff);
4838 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
4839 zone->len >> log_sectors_per_block);
4840 if (ret) {
4841 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4842 fdev->path, ret);
4843 return ret;
4844 }
4845 }
4846
4847 return 0;
4848 }
4849
get_target_zoned_dev(struct f2fs_sb_info * sbi,block_t zone_blkaddr)4850 static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
4851 block_t zone_blkaddr)
4852 {
4853 int i;
4854
4855 for (i = 0; i < sbi->s_ndevs; i++) {
4856 if (!bdev_is_zoned(FDEV(i).bdev))
4857 continue;
4858 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
4859 zone_blkaddr <= FDEV(i).end_blk))
4860 return &FDEV(i);
4861 }
4862
4863 return NULL;
4864 }
4865
report_one_zone_cb(struct blk_zone * zone,unsigned int idx,void * data)4866 static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
4867 void *data)
4868 {
4869 memcpy(data, zone, sizeof(struct blk_zone));
4870 return 0;
4871 }
4872
fix_curseg_write_pointer(struct f2fs_sb_info * sbi,int type)4873 static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
4874 {
4875 struct curseg_info *cs = CURSEG_I(sbi, type);
4876 struct f2fs_dev_info *zbd;
4877 struct blk_zone zone;
4878 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
4879 block_t cs_zone_block, wp_block;
4880 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4881 sector_t zone_sector;
4882 int err;
4883
4884 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4885 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4886
4887 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4888 if (!zbd)
4889 return 0;
4890
4891 /* report zone for the sector the curseg points to */
4892 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4893 << log_sectors_per_block;
4894 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4895 report_one_zone_cb, &zone);
4896 if (err != 1) {
4897 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4898 zbd->path, err);
4899 return err;
4900 }
4901
4902 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4903 return 0;
4904
4905 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
4906 wp_segno = GET_SEGNO(sbi, wp_block);
4907 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4908 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
4909
4910 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
4911 wp_sector_off == 0)
4912 return 0;
4913
4914 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
4915 "curseg[0x%x,0x%x] wp[0x%x,0x%x]",
4916 type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff);
4917
4918 f2fs_notice(sbi, "Assign new section to curseg[%d]: "
4919 "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff);
4920
4921 f2fs_allocate_new_section(sbi, type, true);
4922
4923 /* check consistency of the zone curseg pointed to */
4924 if (check_zone_write_pointer(sbi, zbd, &zone))
4925 return -EIO;
4926
4927 /* check newly assigned zone */
4928 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4929 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4930
4931 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4932 if (!zbd)
4933 return 0;
4934
4935 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4936 << log_sectors_per_block;
4937 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4938 report_one_zone_cb, &zone);
4939 if (err != 1) {
4940 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4941 zbd->path, err);
4942 return err;
4943 }
4944
4945 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4946 return 0;
4947
4948 if (zone.wp != zone.start) {
4949 f2fs_notice(sbi,
4950 "New zone for curseg[%d] is not yet discarded. "
4951 "Reset the zone: curseg[0x%x,0x%x]",
4952 type, cs->segno, cs->next_blkoff);
4953 err = __f2fs_issue_discard_zone(sbi, zbd->bdev, cs_zone_block,
4954 zone.len >> log_sectors_per_block);
4955 if (err) {
4956 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4957 zbd->path, err);
4958 return err;
4959 }
4960 }
4961
4962 return 0;
4963 }
4964
f2fs_fix_curseg_write_pointer(struct f2fs_sb_info * sbi)4965 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4966 {
4967 int i, ret;
4968
4969 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
4970 ret = fix_curseg_write_pointer(sbi, i);
4971 if (ret)
4972 return ret;
4973 }
4974
4975 return 0;
4976 }
4977
4978 struct check_zone_write_pointer_args {
4979 struct f2fs_sb_info *sbi;
4980 struct f2fs_dev_info *fdev;
4981 };
4982
check_zone_write_pointer_cb(struct blk_zone * zone,unsigned int idx,void * data)4983 static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
4984 void *data)
4985 {
4986 struct check_zone_write_pointer_args *args;
4987
4988 args = (struct check_zone_write_pointer_args *)data;
4989
4990 return check_zone_write_pointer(args->sbi, args->fdev, zone);
4991 }
4992
f2fs_check_write_pointer(struct f2fs_sb_info * sbi)4993 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
4994 {
4995 int i, ret;
4996 struct check_zone_write_pointer_args args;
4997
4998 for (i = 0; i < sbi->s_ndevs; i++) {
4999 if (!bdev_is_zoned(FDEV(i).bdev))
5000 continue;
5001
5002 args.sbi = sbi;
5003 args.fdev = &FDEV(i);
5004 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
5005 check_zone_write_pointer_cb, &args);
5006 if (ret < 0)
5007 return ret;
5008 }
5009
5010 return 0;
5011 }
5012
5013 /*
5014 * Return the number of usable blocks in a segment. The number of blocks
5015 * returned is always equal to the number of blocks in a segment for
5016 * segments fully contained within a sequential zone capacity or a
5017 * conventional zone. For segments partially contained in a sequential
5018 * zone capacity, the number of usable blocks up to the zone capacity
5019 * is returned. 0 is returned in all other cases.
5020 */
f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info * sbi,unsigned int segno)5021 static inline unsigned int f2fs_usable_zone_blks_in_seg(
5022 struct f2fs_sb_info *sbi, unsigned int segno)
5023 {
5024 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
5025 unsigned int secno;
5026
5027 if (!sbi->unusable_blocks_per_sec)
5028 return sbi->blocks_per_seg;
5029
5030 secno = GET_SEC_FROM_SEG(sbi, segno);
5031 seg_start = START_BLOCK(sbi, segno);
5032 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
5033 sec_cap_blkaddr = sec_start_blkaddr + CAP_BLKS_PER_SEC(sbi);
5034
5035 /*
5036 * If segment starts before zone capacity and spans beyond
5037 * zone capacity, then usable blocks are from seg start to
5038 * zone capacity. If the segment starts after the zone capacity,
5039 * then there are no usable blocks.
5040 */
5041 if (seg_start >= sec_cap_blkaddr)
5042 return 0;
5043 if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr)
5044 return sec_cap_blkaddr - seg_start;
5045
5046 return sbi->blocks_per_seg;
5047 }
5048 #else
f2fs_fix_curseg_write_pointer(struct f2fs_sb_info * sbi)5049 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
5050 {
5051 return 0;
5052 }
5053
f2fs_check_write_pointer(struct f2fs_sb_info * sbi)5054 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
5055 {
5056 return 0;
5057 }
5058
f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info * sbi,unsigned int segno)5059 static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
5060 unsigned int segno)
5061 {
5062 return 0;
5063 }
5064
5065 #endif
f2fs_usable_blks_in_seg(struct f2fs_sb_info * sbi,unsigned int segno)5066 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
5067 unsigned int segno)
5068 {
5069 if (f2fs_sb_has_blkzoned(sbi))
5070 return f2fs_usable_zone_blks_in_seg(sbi, segno);
5071
5072 return sbi->blocks_per_seg;
5073 }
5074
f2fs_usable_segs_in_sec(struct f2fs_sb_info * sbi,unsigned int segno)5075 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
5076 unsigned int segno)
5077 {
5078 if (f2fs_sb_has_blkzoned(sbi))
5079 return CAP_SEGS_PER_SEC(sbi);
5080
5081 return sbi->segs_per_sec;
5082 }
5083
5084 /*
5085 * Update min, max modified time for cost-benefit GC algorithm
5086 */
init_min_max_mtime(struct f2fs_sb_info * sbi)5087 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
5088 {
5089 struct sit_info *sit_i = SIT_I(sbi);
5090 unsigned int segno;
5091
5092 down_write(&sit_i->sentry_lock);
5093
5094 sit_i->min_mtime = ULLONG_MAX;
5095
5096 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
5097 unsigned int i;
5098 unsigned long long mtime = 0;
5099
5100 for (i = 0; i < sbi->segs_per_sec; i++)
5101 mtime += get_seg_entry(sbi, segno + i)->mtime;
5102
5103 mtime = div_u64(mtime, sbi->segs_per_sec);
5104
5105 if (sit_i->min_mtime > mtime)
5106 sit_i->min_mtime = mtime;
5107 }
5108 sit_i->max_mtime = get_mtime(sbi, false);
5109 sit_i->dirty_max_mtime = 0;
5110 up_write(&sit_i->sentry_lock);
5111 }
5112
f2fs_build_segment_manager(struct f2fs_sb_info * sbi)5113 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
5114 {
5115 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
5116 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
5117 struct f2fs_sm_info *sm_info;
5118 int err;
5119
5120 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
5121 if (!sm_info)
5122 return -ENOMEM;
5123
5124 /* init sm info */
5125 sbi->sm_info = sm_info;
5126 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
5127 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
5128 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
5129 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
5130 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
5131 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
5132 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
5133 sm_info->rec_prefree_segments = sm_info->main_segments *
5134 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
5135 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
5136 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
5137
5138 if (!f2fs_lfs_mode(sbi))
5139 sm_info->ipu_policy = BIT(F2FS_IPU_FSYNC);
5140 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
5141 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
5142 sm_info->min_seq_blocks = sbi->blocks_per_seg;
5143 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
5144 sm_info->min_ssr_sections = reserved_sections(sbi);
5145
5146 INIT_LIST_HEAD(&sm_info->sit_entry_set);
5147
5148 init_f2fs_rwsem(&sm_info->curseg_lock);
5149
5150 err = f2fs_create_flush_cmd_control(sbi);
5151 if (err)
5152 return err;
5153
5154 err = create_discard_cmd_control(sbi);
5155 if (err)
5156 return err;
5157
5158 err = build_sit_info(sbi);
5159 if (err)
5160 return err;
5161 err = build_free_segmap(sbi);
5162 if (err)
5163 return err;
5164 err = build_curseg(sbi);
5165 if (err)
5166 return err;
5167
5168 /* reinit free segmap based on SIT */
5169 err = build_sit_entries(sbi);
5170 if (err)
5171 return err;
5172
5173 init_free_segmap(sbi);
5174 err = build_dirty_segmap(sbi);
5175 if (err)
5176 return err;
5177
5178 err = sanity_check_curseg(sbi);
5179 if (err)
5180 return err;
5181
5182 init_min_max_mtime(sbi);
5183 return 0;
5184 }
5185
discard_dirty_segmap(struct f2fs_sb_info * sbi,enum dirty_type dirty_type)5186 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
5187 enum dirty_type dirty_type)
5188 {
5189 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5190
5191 mutex_lock(&dirty_i->seglist_lock);
5192 kvfree(dirty_i->dirty_segmap[dirty_type]);
5193 dirty_i->nr_dirty[dirty_type] = 0;
5194 mutex_unlock(&dirty_i->seglist_lock);
5195 }
5196
destroy_victim_secmap(struct f2fs_sb_info * sbi)5197 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
5198 {
5199 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5200
5201 kvfree(dirty_i->pinned_secmap);
5202 kvfree(dirty_i->victim_secmap);
5203 }
5204
destroy_dirty_segmap(struct f2fs_sb_info * sbi)5205 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
5206 {
5207 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5208 int i;
5209
5210 if (!dirty_i)
5211 return;
5212
5213 /* discard pre-free/dirty segments list */
5214 for (i = 0; i < NR_DIRTY_TYPE; i++)
5215 discard_dirty_segmap(sbi, i);
5216
5217 if (__is_large_section(sbi)) {
5218 mutex_lock(&dirty_i->seglist_lock);
5219 kvfree(dirty_i->dirty_secmap);
5220 mutex_unlock(&dirty_i->seglist_lock);
5221 }
5222
5223 destroy_victim_secmap(sbi);
5224 SM_I(sbi)->dirty_info = NULL;
5225 kfree(dirty_i);
5226 }
5227
destroy_curseg(struct f2fs_sb_info * sbi)5228 static void destroy_curseg(struct f2fs_sb_info *sbi)
5229 {
5230 struct curseg_info *array = SM_I(sbi)->curseg_array;
5231 int i;
5232
5233 if (!array)
5234 return;
5235 SM_I(sbi)->curseg_array = NULL;
5236 for (i = 0; i < NR_CURSEG_TYPE; i++) {
5237 kfree(array[i].sum_blk);
5238 kfree(array[i].journal);
5239 }
5240 kfree(array);
5241 }
5242
destroy_free_segmap(struct f2fs_sb_info * sbi)5243 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
5244 {
5245 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
5246
5247 if (!free_i)
5248 return;
5249 SM_I(sbi)->free_info = NULL;
5250 kvfree(free_i->free_segmap);
5251 kvfree(free_i->free_secmap);
5252 kfree(free_i);
5253 }
5254
destroy_sit_info(struct f2fs_sb_info * sbi)5255 static void destroy_sit_info(struct f2fs_sb_info *sbi)
5256 {
5257 struct sit_info *sit_i = SIT_I(sbi);
5258
5259 if (!sit_i)
5260 return;
5261
5262 if (sit_i->sentries)
5263 kvfree(sit_i->bitmap);
5264 kfree(sit_i->tmp_map);
5265
5266 kvfree(sit_i->sentries);
5267 kvfree(sit_i->sec_entries);
5268 kvfree(sit_i->dirty_sentries_bitmap);
5269
5270 SM_I(sbi)->sit_info = NULL;
5271 kvfree(sit_i->sit_bitmap);
5272 #ifdef CONFIG_F2FS_CHECK_FS
5273 kvfree(sit_i->sit_bitmap_mir);
5274 kvfree(sit_i->invalid_segmap);
5275 #endif
5276 kfree(sit_i);
5277 }
5278
f2fs_destroy_segment_manager(struct f2fs_sb_info * sbi)5279 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
5280 {
5281 struct f2fs_sm_info *sm_info = SM_I(sbi);
5282
5283 if (!sm_info)
5284 return;
5285 f2fs_destroy_flush_cmd_control(sbi, true);
5286 destroy_discard_cmd_control(sbi);
5287 destroy_dirty_segmap(sbi);
5288 destroy_curseg(sbi);
5289 destroy_free_segmap(sbi);
5290 destroy_sit_info(sbi);
5291 sbi->sm_info = NULL;
5292 kfree(sm_info);
5293 }
5294
f2fs_create_segment_manager_caches(void)5295 int __init f2fs_create_segment_manager_caches(void)
5296 {
5297 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
5298 sizeof(struct discard_entry));
5299 if (!discard_entry_slab)
5300 goto fail;
5301
5302 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
5303 sizeof(struct discard_cmd));
5304 if (!discard_cmd_slab)
5305 goto destroy_discard_entry;
5306
5307 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
5308 sizeof(struct sit_entry_set));
5309 if (!sit_entry_set_slab)
5310 goto destroy_discard_cmd;
5311
5312 revoke_entry_slab = f2fs_kmem_cache_create("f2fs_revoke_entry",
5313 sizeof(struct revoke_entry));
5314 if (!revoke_entry_slab)
5315 goto destroy_sit_entry_set;
5316 return 0;
5317
5318 destroy_sit_entry_set:
5319 kmem_cache_destroy(sit_entry_set_slab);
5320 destroy_discard_cmd:
5321 kmem_cache_destroy(discard_cmd_slab);
5322 destroy_discard_entry:
5323 kmem_cache_destroy(discard_entry_slab);
5324 fail:
5325 return -ENOMEM;
5326 }
5327
f2fs_destroy_segment_manager_caches(void)5328 void f2fs_destroy_segment_manager_caches(void)
5329 {
5330 kmem_cache_destroy(sit_entry_set_slab);
5331 kmem_cache_destroy(discard_cmd_slab);
5332 kmem_cache_destroy(discard_entry_slab);
5333 kmem_cache_destroy(revoke_entry_slab);
5334 }
5335