]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/f2fs/segment.c
f2fs: find data segments across all the types
[mirror_ubuntu-artful-kernel.git] / fs / f2fs / segment.c
1 /*
2 * fs/f2fs/segment.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/bio.h>
14 #include <linux/blkdev.h>
15 #include <linux/prefetch.h>
16 #include <linux/kthread.h>
17 #include <linux/swap.h>
18 #include <linux/timer.h>
19
20 #include "f2fs.h"
21 #include "segment.h"
22 #include "node.h"
23 #include "trace.h"
24 #include <trace/events/f2fs.h>
25
26 #define __reverse_ffz(x) __reverse_ffs(~(x))
27
28 static struct kmem_cache *discard_entry_slab;
29 static struct kmem_cache *discard_cmd_slab;
30 static struct kmem_cache *sit_entry_set_slab;
31 static struct kmem_cache *inmem_entry_slab;
32
33 static unsigned long __reverse_ulong(unsigned char *str)
34 {
35 unsigned long tmp = 0;
36 int shift = 24, idx = 0;
37
38 #if BITS_PER_LONG == 64
39 shift = 56;
40 #endif
41 while (shift >= 0) {
42 tmp |= (unsigned long)str[idx++] << shift;
43 shift -= BITS_PER_BYTE;
44 }
45 return tmp;
46 }
47
48 /*
49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
50 * MSB and LSB are reversed in a byte by f2fs_set_bit.
51 */
52 static inline unsigned long __reverse_ffs(unsigned long word)
53 {
54 int num = 0;
55
56 #if BITS_PER_LONG == 64
57 if ((word & 0xffffffff00000000UL) == 0)
58 num += 32;
59 else
60 word >>= 32;
61 #endif
62 if ((word & 0xffff0000) == 0)
63 num += 16;
64 else
65 word >>= 16;
66
67 if ((word & 0xff00) == 0)
68 num += 8;
69 else
70 word >>= 8;
71
72 if ((word & 0xf0) == 0)
73 num += 4;
74 else
75 word >>= 4;
76
77 if ((word & 0xc) == 0)
78 num += 2;
79 else
80 word >>= 2;
81
82 if ((word & 0x2) == 0)
83 num += 1;
84 return num;
85 }
86
87 /*
88 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
89 * f2fs_set_bit makes MSB and LSB reversed in a byte.
90 * @size must be integral times of unsigned long.
91 * Example:
92 * MSB <--> LSB
93 * f2fs_set_bit(0, bitmap) => 1000 0000
94 * f2fs_set_bit(7, bitmap) => 0000 0001
95 */
96 static unsigned long __find_rev_next_bit(const unsigned long *addr,
97 unsigned long size, unsigned long offset)
98 {
99 const unsigned long *p = addr + BIT_WORD(offset);
100 unsigned long result = size;
101 unsigned long tmp;
102
103 if (offset >= size)
104 return size;
105
106 size -= (offset & ~(BITS_PER_LONG - 1));
107 offset %= BITS_PER_LONG;
108
109 while (1) {
110 if (*p == 0)
111 goto pass;
112
113 tmp = __reverse_ulong((unsigned char *)p);
114
115 tmp &= ~0UL >> offset;
116 if (size < BITS_PER_LONG)
117 tmp &= (~0UL << (BITS_PER_LONG - size));
118 if (tmp)
119 goto found;
120 pass:
121 if (size <= BITS_PER_LONG)
122 break;
123 size -= BITS_PER_LONG;
124 offset = 0;
125 p++;
126 }
127 return result;
128 found:
129 return result - size + __reverse_ffs(tmp);
130 }
131
132 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
133 unsigned long size, unsigned long offset)
134 {
135 const unsigned long *p = addr + BIT_WORD(offset);
136 unsigned long result = size;
137 unsigned long tmp;
138
139 if (offset >= size)
140 return size;
141
142 size -= (offset & ~(BITS_PER_LONG - 1));
143 offset %= BITS_PER_LONG;
144
145 while (1) {
146 if (*p == ~0UL)
147 goto pass;
148
149 tmp = __reverse_ulong((unsigned char *)p);
150
151 if (offset)
152 tmp |= ~0UL << (BITS_PER_LONG - offset);
153 if (size < BITS_PER_LONG)
154 tmp |= ~0UL >> size;
155 if (tmp != ~0UL)
156 goto found;
157 pass:
158 if (size <= BITS_PER_LONG)
159 break;
160 size -= BITS_PER_LONG;
161 offset = 0;
162 p++;
163 }
164 return result;
165 found:
166 return result - size + __reverse_ffz(tmp);
167 }
168
169 void register_inmem_page(struct inode *inode, struct page *page)
170 {
171 struct f2fs_inode_info *fi = F2FS_I(inode);
172 struct inmem_pages *new;
173
174 f2fs_trace_pid(page);
175
176 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
177 SetPagePrivate(page);
178
179 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
180
181 /* add atomic page indices to the list */
182 new->page = page;
183 INIT_LIST_HEAD(&new->list);
184
185 /* increase reference count with clean state */
186 mutex_lock(&fi->inmem_lock);
187 get_page(page);
188 list_add_tail(&new->list, &fi->inmem_pages);
189 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
190 mutex_unlock(&fi->inmem_lock);
191
192 trace_f2fs_register_inmem_page(page, INMEM);
193 }
194
195 static int __revoke_inmem_pages(struct inode *inode,
196 struct list_head *head, bool drop, bool recover)
197 {
198 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
199 struct inmem_pages *cur, *tmp;
200 int err = 0;
201
202 list_for_each_entry_safe(cur, tmp, head, list) {
203 struct page *page = cur->page;
204
205 if (drop)
206 trace_f2fs_commit_inmem_page(page, INMEM_DROP);
207
208 lock_page(page);
209
210 if (recover) {
211 struct dnode_of_data dn;
212 struct node_info ni;
213
214 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
215
216 set_new_dnode(&dn, inode, NULL, NULL, 0);
217 if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
218 err = -EAGAIN;
219 goto next;
220 }
221 get_node_info(sbi, dn.nid, &ni);
222 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
223 cur->old_addr, ni.version, true, true);
224 f2fs_put_dnode(&dn);
225 }
226 next:
227 /* we don't need to invalidate this in the sccessful status */
228 if (drop || recover)
229 ClearPageUptodate(page);
230 set_page_private(page, 0);
231 ClearPagePrivate(page);
232 f2fs_put_page(page, 1);
233
234 list_del(&cur->list);
235 kmem_cache_free(inmem_entry_slab, cur);
236 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
237 }
238 return err;
239 }
240
241 void drop_inmem_pages(struct inode *inode)
242 {
243 struct f2fs_inode_info *fi = F2FS_I(inode);
244
245 mutex_lock(&fi->inmem_lock);
246 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
247 mutex_unlock(&fi->inmem_lock);
248
249 clear_inode_flag(inode, FI_ATOMIC_FILE);
250 stat_dec_atomic_write(inode);
251 }
252
253 static int __commit_inmem_pages(struct inode *inode,
254 struct list_head *revoke_list)
255 {
256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
257 struct f2fs_inode_info *fi = F2FS_I(inode);
258 struct inmem_pages *cur, *tmp;
259 struct f2fs_io_info fio = {
260 .sbi = sbi,
261 .type = DATA,
262 .op = REQ_OP_WRITE,
263 .op_flags = REQ_SYNC | REQ_PRIO,
264 .encrypted_page = NULL,
265 };
266 pgoff_t last_idx = ULONG_MAX;
267 int err = 0;
268
269 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
270 struct page *page = cur->page;
271
272 lock_page(page);
273 if (page->mapping == inode->i_mapping) {
274 trace_f2fs_commit_inmem_page(page, INMEM);
275
276 set_page_dirty(page);
277 f2fs_wait_on_page_writeback(page, DATA, true);
278 if (clear_page_dirty_for_io(page)) {
279 inode_dec_dirty_pages(inode);
280 remove_dirty_inode(inode);
281 }
282
283 fio.page = page;
284 err = do_write_data_page(&fio);
285 if (err) {
286 unlock_page(page);
287 break;
288 }
289
290 /* record old blkaddr for revoking */
291 cur->old_addr = fio.old_blkaddr;
292 last_idx = page->index;
293 }
294 unlock_page(page);
295 list_move_tail(&cur->list, revoke_list);
296 }
297
298 if (last_idx != ULONG_MAX)
299 f2fs_submit_merged_bio_cond(sbi, inode, 0, last_idx,
300 DATA, WRITE);
301
302 if (!err)
303 __revoke_inmem_pages(inode, revoke_list, false, false);
304
305 return err;
306 }
307
308 int commit_inmem_pages(struct inode *inode)
309 {
310 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
311 struct f2fs_inode_info *fi = F2FS_I(inode);
312 struct list_head revoke_list;
313 int err;
314
315 INIT_LIST_HEAD(&revoke_list);
316 f2fs_balance_fs(sbi, true);
317 f2fs_lock_op(sbi);
318
319 set_inode_flag(inode, FI_ATOMIC_COMMIT);
320
321 mutex_lock(&fi->inmem_lock);
322 err = __commit_inmem_pages(inode, &revoke_list);
323 if (err) {
324 int ret;
325 /*
326 * try to revoke all committed pages, but still we could fail
327 * due to no memory or other reason, if that happened, EAGAIN
328 * will be returned, which means in such case, transaction is
329 * already not integrity, caller should use journal to do the
330 * recovery or rewrite & commit last transaction. For other
331 * error number, revoking was done by filesystem itself.
332 */
333 ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
334 if (ret)
335 err = ret;
336
337 /* drop all uncommitted pages */
338 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
339 }
340 mutex_unlock(&fi->inmem_lock);
341
342 clear_inode_flag(inode, FI_ATOMIC_COMMIT);
343
344 f2fs_unlock_op(sbi);
345 return err;
346 }
347
348 /*
349 * This function balances dirty node and dentry pages.
350 * In addition, it controls garbage collection.
351 */
352 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
353 {
354 #ifdef CONFIG_F2FS_FAULT_INJECTION
355 if (time_to_inject(sbi, FAULT_CHECKPOINT))
356 f2fs_stop_checkpoint(sbi, false);
357 #endif
358
359 if (!need)
360 return;
361
362 /* balance_fs_bg is able to be pending */
363 if (excess_cached_nats(sbi))
364 f2fs_balance_fs_bg(sbi);
365
366 /*
367 * We should do GC or end up with checkpoint, if there are so many dirty
368 * dir/node pages without enough free segments.
369 */
370 if (has_not_enough_free_secs(sbi, 0, 0)) {
371 mutex_lock(&sbi->gc_mutex);
372 f2fs_gc(sbi, false, false);
373 }
374 }
375
376 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
377 {
378 /* try to shrink extent cache when there is no enough memory */
379 if (!available_free_memory(sbi, EXTENT_CACHE))
380 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
381
382 /* check the # of cached NAT entries */
383 if (!available_free_memory(sbi, NAT_ENTRIES))
384 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
385
386 if (!available_free_memory(sbi, FREE_NIDS))
387 try_to_free_nids(sbi, MAX_FREE_NIDS);
388 else
389 build_free_nids(sbi, false);
390
391 if (!is_idle(sbi))
392 return;
393
394 /* checkpoint is the only way to shrink partial cached entries */
395 if (!available_free_memory(sbi, NAT_ENTRIES) ||
396 !available_free_memory(sbi, INO_ENTRIES) ||
397 excess_prefree_segs(sbi) ||
398 excess_dirty_nats(sbi) ||
399 f2fs_time_over(sbi, CP_TIME)) {
400 if (test_opt(sbi, DATA_FLUSH)) {
401 struct blk_plug plug;
402
403 blk_start_plug(&plug);
404 sync_dirty_inodes(sbi, FILE_INODE);
405 blk_finish_plug(&plug);
406 }
407 f2fs_sync_fs(sbi->sb, true);
408 stat_inc_bg_cp_count(sbi->stat_info);
409 }
410 }
411
412 static int __submit_flush_wait(struct block_device *bdev)
413 {
414 struct bio *bio = f2fs_bio_alloc(0);
415 int ret;
416
417 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
418 bio->bi_bdev = bdev;
419 ret = submit_bio_wait(bio);
420 bio_put(bio);
421 return ret;
422 }
423
424 static int submit_flush_wait(struct f2fs_sb_info *sbi)
425 {
426 int ret = __submit_flush_wait(sbi->sb->s_bdev);
427 int i;
428
429 if (sbi->s_ndevs && !ret) {
430 for (i = 1; i < sbi->s_ndevs; i++) {
431 trace_f2fs_issue_flush(FDEV(i).bdev,
432 test_opt(sbi, NOBARRIER),
433 test_opt(sbi, FLUSH_MERGE));
434 ret = __submit_flush_wait(FDEV(i).bdev);
435 if (ret)
436 break;
437 }
438 }
439 return ret;
440 }
441
442 static int issue_flush_thread(void *data)
443 {
444 struct f2fs_sb_info *sbi = data;
445 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
446 wait_queue_head_t *q = &fcc->flush_wait_queue;
447 repeat:
448 if (kthread_should_stop())
449 return 0;
450
451 if (!llist_empty(&fcc->issue_list)) {
452 struct flush_cmd *cmd, *next;
453 int ret;
454
455 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
456 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
457
458 ret = submit_flush_wait(sbi);
459 llist_for_each_entry_safe(cmd, next,
460 fcc->dispatch_list, llnode) {
461 cmd->ret = ret;
462 complete(&cmd->wait);
463 }
464 fcc->dispatch_list = NULL;
465 }
466
467 wait_event_interruptible(*q,
468 kthread_should_stop() || !llist_empty(&fcc->issue_list));
469 goto repeat;
470 }
471
472 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
473 {
474 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
475 struct flush_cmd cmd;
476
477 if (test_opt(sbi, NOBARRIER))
478 return 0;
479
480 if (!test_opt(sbi, FLUSH_MERGE))
481 return submit_flush_wait(sbi);
482
483 if (!atomic_read(&fcc->submit_flush)) {
484 int ret;
485
486 atomic_inc(&fcc->submit_flush);
487 ret = submit_flush_wait(sbi);
488 atomic_dec(&fcc->submit_flush);
489 return ret;
490 }
491
492 init_completion(&cmd.wait);
493
494 atomic_inc(&fcc->submit_flush);
495 llist_add(&cmd.llnode, &fcc->issue_list);
496
497 if (!fcc->dispatch_list)
498 wake_up(&fcc->flush_wait_queue);
499
500 if (fcc->f2fs_issue_flush) {
501 wait_for_completion(&cmd.wait);
502 atomic_dec(&fcc->submit_flush);
503 } else {
504 llist_del_all(&fcc->issue_list);
505 atomic_set(&fcc->submit_flush, 0);
506 }
507
508 return cmd.ret;
509 }
510
511 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
512 {
513 dev_t dev = sbi->sb->s_bdev->bd_dev;
514 struct flush_cmd_control *fcc;
515 int err = 0;
516
517 if (SM_I(sbi)->fcc_info) {
518 fcc = SM_I(sbi)->fcc_info;
519 goto init_thread;
520 }
521
522 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
523 if (!fcc)
524 return -ENOMEM;
525 atomic_set(&fcc->submit_flush, 0);
526 init_waitqueue_head(&fcc->flush_wait_queue);
527 init_llist_head(&fcc->issue_list);
528 SM_I(sbi)->fcc_info = fcc;
529 init_thread:
530 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
531 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
532 if (IS_ERR(fcc->f2fs_issue_flush)) {
533 err = PTR_ERR(fcc->f2fs_issue_flush);
534 kfree(fcc);
535 SM_I(sbi)->fcc_info = NULL;
536 return err;
537 }
538
539 return err;
540 }
541
542 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
543 {
544 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
545
546 if (fcc && fcc->f2fs_issue_flush) {
547 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
548
549 fcc->f2fs_issue_flush = NULL;
550 kthread_stop(flush_thread);
551 }
552 if (free) {
553 kfree(fcc);
554 SM_I(sbi)->fcc_info = NULL;
555 }
556 }
557
558 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
559 enum dirty_type dirty_type)
560 {
561 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
562
563 /* need not be added */
564 if (IS_CURSEG(sbi, segno))
565 return;
566
567 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
568 dirty_i->nr_dirty[dirty_type]++;
569
570 if (dirty_type == DIRTY) {
571 struct seg_entry *sentry = get_seg_entry(sbi, segno);
572 enum dirty_type t = sentry->type;
573
574 if (unlikely(t >= DIRTY)) {
575 f2fs_bug_on(sbi, 1);
576 return;
577 }
578 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
579 dirty_i->nr_dirty[t]++;
580 }
581 }
582
583 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
584 enum dirty_type dirty_type)
585 {
586 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
587
588 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
589 dirty_i->nr_dirty[dirty_type]--;
590
591 if (dirty_type == DIRTY) {
592 struct seg_entry *sentry = get_seg_entry(sbi, segno);
593 enum dirty_type t = sentry->type;
594
595 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
596 dirty_i->nr_dirty[t]--;
597
598 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
599 clear_bit(GET_SECNO(sbi, segno),
600 dirty_i->victim_secmap);
601 }
602 }
603
604 /*
605 * Should not occur error such as -ENOMEM.
606 * Adding dirty entry into seglist is not critical operation.
607 * If a given segment is one of current working segments, it won't be added.
608 */
609 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
610 {
611 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
612 unsigned short valid_blocks;
613
614 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
615 return;
616
617 mutex_lock(&dirty_i->seglist_lock);
618
619 valid_blocks = get_valid_blocks(sbi, segno, 0);
620
621 if (valid_blocks == 0) {
622 __locate_dirty_segment(sbi, segno, PRE);
623 __remove_dirty_segment(sbi, segno, DIRTY);
624 } else if (valid_blocks < sbi->blocks_per_seg) {
625 __locate_dirty_segment(sbi, segno, DIRTY);
626 } else {
627 /* Recovery routine with SSR needs this */
628 __remove_dirty_segment(sbi, segno, DIRTY);
629 }
630
631 mutex_unlock(&dirty_i->seglist_lock);
632 }
633
634 static void __add_discard_cmd(struct f2fs_sb_info *sbi,
635 struct bio *bio, block_t lstart, block_t len)
636 {
637 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
638 struct list_head *cmd_list = &(dcc->discard_cmd_list);
639 struct discard_cmd *dc;
640
641 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
642 INIT_LIST_HEAD(&dc->list);
643 dc->bio = bio;
644 bio->bi_private = dc;
645 dc->lstart = lstart;
646 dc->len = len;
647 dc->state = D_PREP;
648 init_completion(&dc->wait);
649
650 mutex_lock(&dcc->cmd_lock);
651 list_add_tail(&dc->list, cmd_list);
652 mutex_unlock(&dcc->cmd_lock);
653 }
654
655 static void __remove_discard_cmd(struct f2fs_sb_info *sbi, struct discard_cmd *dc)
656 {
657 int err = dc->bio->bi_error;
658
659 if (dc->state == D_DONE)
660 atomic_dec(&(SM_I(sbi)->dcc_info->submit_discard));
661
662 if (err == -EOPNOTSUPP)
663 err = 0;
664
665 if (err)
666 f2fs_msg(sbi->sb, KERN_INFO,
667 "Issue discard failed, ret: %d", err);
668 bio_put(dc->bio);
669 list_del(&dc->list);
670 kmem_cache_free(discard_cmd_slab, dc);
671 }
672
673 /* This should be covered by global mutex, &sit_i->sentry_lock */
674 void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
675 {
676 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
677 struct list_head *wait_list = &(dcc->discard_cmd_list);
678 struct discard_cmd *dc, *tmp;
679
680 mutex_lock(&dcc->cmd_lock);
681 list_for_each_entry_safe(dc, tmp, wait_list, list) {
682
683 if (blkaddr == NULL_ADDR) {
684 if (dc->state == D_PREP) {
685 dc->state = D_SUBMIT;
686 submit_bio(dc->bio);
687 atomic_inc(&dcc->submit_discard);
688 }
689 wait_for_completion_io(&dc->wait);
690
691 __remove_discard_cmd(sbi, dc);
692 continue;
693 }
694
695 if (dc->lstart <= blkaddr && blkaddr < dc->lstart + dc->len) {
696 if (dc->state == D_SUBMIT)
697 wait_for_completion_io(&dc->wait);
698 else
699 __remove_discard_cmd(sbi, dc);
700 }
701 }
702 mutex_unlock(&dcc->cmd_lock);
703 }
704
705 static void f2fs_submit_discard_endio(struct bio *bio)
706 {
707 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
708
709 complete(&dc->wait);
710 dc->state = D_DONE;
711 }
712
713 static int issue_discard_thread(void *data)
714 {
715 struct f2fs_sb_info *sbi = data;
716 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
717 wait_queue_head_t *q = &dcc->discard_wait_queue;
718 struct list_head *cmd_list = &dcc->discard_cmd_list;
719 struct discard_cmd *dc, *tmp;
720 struct blk_plug plug;
721 int iter = 0;
722 repeat:
723 if (kthread_should_stop())
724 return 0;
725
726 blk_start_plug(&plug);
727
728 mutex_lock(&dcc->cmd_lock);
729 list_for_each_entry_safe(dc, tmp, cmd_list, list) {
730 if (dc->state == D_PREP) {
731 dc->state = D_SUBMIT;
732 submit_bio(dc->bio);
733 atomic_inc(&dcc->submit_discard);
734 if (iter++ > DISCARD_ISSUE_RATE)
735 break;
736 } else if (dc->state == D_DONE) {
737 __remove_discard_cmd(sbi, dc);
738 }
739 }
740 mutex_unlock(&dcc->cmd_lock);
741
742 blk_finish_plug(&plug);
743
744 iter = 0;
745 congestion_wait(BLK_RW_SYNC, HZ/50);
746
747 wait_event_interruptible(*q,
748 kthread_should_stop() || !list_empty(&dcc->discard_cmd_list));
749 goto repeat;
750 }
751
752
753 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
754 static int __f2fs_issue_discard_async(struct f2fs_sb_info *sbi,
755 struct block_device *bdev, block_t blkstart, block_t blklen)
756 {
757 struct bio *bio = NULL;
758 block_t lblkstart = blkstart;
759 int err;
760
761 trace_f2fs_issue_discard(bdev, blkstart, blklen);
762
763 if (sbi->s_ndevs) {
764 int devi = f2fs_target_device_index(sbi, blkstart);
765
766 blkstart -= FDEV(devi).start_blk;
767 }
768 err = __blkdev_issue_discard(bdev,
769 SECTOR_FROM_BLOCK(blkstart),
770 SECTOR_FROM_BLOCK(blklen),
771 GFP_NOFS, 0, &bio);
772 if (!err && bio) {
773 bio->bi_end_io = f2fs_submit_discard_endio;
774 bio->bi_opf |= REQ_SYNC;
775
776 __add_discard_cmd(sbi, bio, lblkstart, blklen);
777 wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue);
778 }
779 return err;
780 }
781
782 #ifdef CONFIG_BLK_DEV_ZONED
783 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
784 struct block_device *bdev, block_t blkstart, block_t blklen)
785 {
786 sector_t nr_sects = SECTOR_FROM_BLOCK(blklen);
787 sector_t sector;
788 int devi = 0;
789
790 if (sbi->s_ndevs) {
791 devi = f2fs_target_device_index(sbi, blkstart);
792 blkstart -= FDEV(devi).start_blk;
793 }
794 sector = SECTOR_FROM_BLOCK(blkstart);
795
796 if (sector & (bdev_zone_sectors(bdev) - 1) ||
797 nr_sects != bdev_zone_sectors(bdev)) {
798 f2fs_msg(sbi->sb, KERN_INFO,
799 "(%d) %s: Unaligned discard attempted (block %x + %x)",
800 devi, sbi->s_ndevs ? FDEV(devi).path: "",
801 blkstart, blklen);
802 return -EIO;
803 }
804
805 /*
806 * We need to know the type of the zone: for conventional zones,
807 * use regular discard if the drive supports it. For sequential
808 * zones, reset the zone write pointer.
809 */
810 switch (get_blkz_type(sbi, bdev, blkstart)) {
811
812 case BLK_ZONE_TYPE_CONVENTIONAL:
813 if (!blk_queue_discard(bdev_get_queue(bdev)))
814 return 0;
815 return __f2fs_issue_discard_async(sbi, bdev, blkstart, blklen);
816 case BLK_ZONE_TYPE_SEQWRITE_REQ:
817 case BLK_ZONE_TYPE_SEQWRITE_PREF:
818 trace_f2fs_issue_reset_zone(bdev, blkstart);
819 return blkdev_reset_zones(bdev, sector,
820 nr_sects, GFP_NOFS);
821 default:
822 /* Unknown zone type: broken device ? */
823 return -EIO;
824 }
825 }
826 #endif
827
828 static int __issue_discard_async(struct f2fs_sb_info *sbi,
829 struct block_device *bdev, block_t blkstart, block_t blklen)
830 {
831 #ifdef CONFIG_BLK_DEV_ZONED
832 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
833 bdev_zoned_model(bdev) != BLK_ZONED_NONE)
834 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
835 #endif
836 return __f2fs_issue_discard_async(sbi, bdev, blkstart, blklen);
837 }
838
839 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
840 block_t blkstart, block_t blklen)
841 {
842 sector_t start = blkstart, len = 0;
843 struct block_device *bdev;
844 struct seg_entry *se;
845 unsigned int offset;
846 block_t i;
847 int err = 0;
848
849 bdev = f2fs_target_device(sbi, blkstart, NULL);
850
851 for (i = blkstart; i < blkstart + blklen; i++, len++) {
852 if (i != start) {
853 struct block_device *bdev2 =
854 f2fs_target_device(sbi, i, NULL);
855
856 if (bdev2 != bdev) {
857 err = __issue_discard_async(sbi, bdev,
858 start, len);
859 if (err)
860 return err;
861 bdev = bdev2;
862 start = i;
863 len = 0;
864 }
865 }
866
867 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
868 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
869
870 if (!f2fs_test_and_set_bit(offset, se->discard_map))
871 sbi->discard_blks--;
872 }
873
874 if (len)
875 err = __issue_discard_async(sbi, bdev, start, len);
876 return err;
877 }
878
879 static void __add_discard_entry(struct f2fs_sb_info *sbi,
880 struct cp_control *cpc, struct seg_entry *se,
881 unsigned int start, unsigned int end)
882 {
883 struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list;
884 struct discard_entry *new, *last;
885
886 if (!list_empty(head)) {
887 last = list_last_entry(head, struct discard_entry, list);
888 if (START_BLOCK(sbi, cpc->trim_start) + start ==
889 last->blkaddr + last->len) {
890 last->len += end - start;
891 goto done;
892 }
893 }
894
895 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
896 INIT_LIST_HEAD(&new->list);
897 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
898 new->len = end - start;
899 list_add_tail(&new->list, head);
900 done:
901 SM_I(sbi)->dcc_info->nr_discards += end - start;
902 }
903
904 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
905 bool check_only)
906 {
907 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
908 int max_blocks = sbi->blocks_per_seg;
909 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
910 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
911 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
912 unsigned long *discard_map = (unsigned long *)se->discard_map;
913 unsigned long *dmap = SIT_I(sbi)->tmp_map;
914 unsigned int start = 0, end = -1;
915 bool force = (cpc->reason == CP_DISCARD);
916 int i;
917
918 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
919 return false;
920
921 if (!force) {
922 if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
923 SM_I(sbi)->dcc_info->nr_discards >=
924 SM_I(sbi)->dcc_info->max_discards)
925 return false;
926 }
927
928 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
929 for (i = 0; i < entries; i++)
930 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
931 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
932
933 while (force || SM_I(sbi)->dcc_info->nr_discards <=
934 SM_I(sbi)->dcc_info->max_discards) {
935 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
936 if (start >= max_blocks)
937 break;
938
939 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
940 if (force && start && end != max_blocks
941 && (end - start) < cpc->trim_minlen)
942 continue;
943
944 if (check_only)
945 return true;
946
947 __add_discard_entry(sbi, cpc, se, start, end);
948 }
949 return false;
950 }
951
952 void release_discard_addrs(struct f2fs_sb_info *sbi)
953 {
954 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
955 struct discard_entry *entry, *this;
956
957 /* drop caches */
958 list_for_each_entry_safe(entry, this, head, list) {
959 list_del(&entry->list);
960 kmem_cache_free(discard_entry_slab, entry);
961 }
962 }
963
964 /*
965 * Should call clear_prefree_segments after checkpoint is done.
966 */
967 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
968 {
969 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
970 unsigned int segno;
971
972 mutex_lock(&dirty_i->seglist_lock);
973 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
974 __set_test_and_free(sbi, segno);
975 mutex_unlock(&dirty_i->seglist_lock);
976 }
977
978 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
979 {
980 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list);
981 struct discard_entry *entry, *this;
982 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
983 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
984 unsigned int start = 0, end = -1;
985 unsigned int secno, start_segno;
986 bool force = (cpc->reason == CP_DISCARD);
987
988 mutex_lock(&dirty_i->seglist_lock);
989
990 while (1) {
991 int i;
992 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
993 if (start >= MAIN_SEGS(sbi))
994 break;
995 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
996 start + 1);
997
998 for (i = start; i < end; i++)
999 clear_bit(i, prefree_map);
1000
1001 dirty_i->nr_dirty[PRE] -= end - start;
1002
1003 if (!test_opt(sbi, DISCARD))
1004 continue;
1005
1006 if (force && start >= cpc->trim_start &&
1007 (end - 1) <= cpc->trim_end)
1008 continue;
1009
1010 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
1011 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
1012 (end - start) << sbi->log_blocks_per_seg);
1013 continue;
1014 }
1015 next:
1016 secno = GET_SECNO(sbi, start);
1017 start_segno = secno * sbi->segs_per_sec;
1018 if (!IS_CURSEC(sbi, secno) &&
1019 !get_valid_blocks(sbi, start, sbi->segs_per_sec))
1020 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1021 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1022
1023 start = start_segno + sbi->segs_per_sec;
1024 if (start < end)
1025 goto next;
1026 }
1027 mutex_unlock(&dirty_i->seglist_lock);
1028
1029 /* send small discards */
1030 list_for_each_entry_safe(entry, this, head, list) {
1031 if (force && entry->len < cpc->trim_minlen)
1032 goto skip;
1033 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
1034 cpc->trimmed += entry->len;
1035 skip:
1036 list_del(&entry->list);
1037 SM_I(sbi)->dcc_info->nr_discards -= entry->len;
1038 kmem_cache_free(discard_entry_slab, entry);
1039 }
1040 }
1041
1042 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
1043 {
1044 dev_t dev = sbi->sb->s_bdev->bd_dev;
1045 struct discard_cmd_control *dcc;
1046 int err = 0;
1047
1048 if (SM_I(sbi)->dcc_info) {
1049 dcc = SM_I(sbi)->dcc_info;
1050 goto init_thread;
1051 }
1052
1053 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL);
1054 if (!dcc)
1055 return -ENOMEM;
1056
1057 INIT_LIST_HEAD(&dcc->discard_entry_list);
1058 INIT_LIST_HEAD(&dcc->discard_cmd_list);
1059 mutex_init(&dcc->cmd_lock);
1060 atomic_set(&dcc->submit_discard, 0);
1061 dcc->nr_discards = 0;
1062 dcc->max_discards = 0;
1063
1064 init_waitqueue_head(&dcc->discard_wait_queue);
1065 SM_I(sbi)->dcc_info = dcc;
1066 init_thread:
1067 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
1068 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
1069 if (IS_ERR(dcc->f2fs_issue_discard)) {
1070 err = PTR_ERR(dcc->f2fs_issue_discard);
1071 kfree(dcc);
1072 SM_I(sbi)->dcc_info = NULL;
1073 return err;
1074 }
1075
1076 return err;
1077 }
1078
1079 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi, bool free)
1080 {
1081 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1082
1083 if (dcc && dcc->f2fs_issue_discard) {
1084 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1085
1086 dcc->f2fs_issue_discard = NULL;
1087 kthread_stop(discard_thread);
1088 }
1089 if (free) {
1090 kfree(dcc);
1091 SM_I(sbi)->dcc_info = NULL;
1092 }
1093 }
1094
1095 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
1096 {
1097 struct sit_info *sit_i = SIT_I(sbi);
1098
1099 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
1100 sit_i->dirty_sentries++;
1101 return false;
1102 }
1103
1104 return true;
1105 }
1106
1107 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
1108 unsigned int segno, int modified)
1109 {
1110 struct seg_entry *se = get_seg_entry(sbi, segno);
1111 se->type = type;
1112 if (modified)
1113 __mark_sit_entry_dirty(sbi, segno);
1114 }
1115
1116 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
1117 {
1118 struct seg_entry *se;
1119 unsigned int segno, offset;
1120 long int new_vblocks;
1121
1122 segno = GET_SEGNO(sbi, blkaddr);
1123
1124 se = get_seg_entry(sbi, segno);
1125 new_vblocks = se->valid_blocks + del;
1126 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1127
1128 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
1129 (new_vblocks > sbi->blocks_per_seg)));
1130
1131 se->valid_blocks = new_vblocks;
1132 se->mtime = get_mtime(sbi);
1133 SIT_I(sbi)->max_mtime = se->mtime;
1134
1135 /* Update valid block bitmap */
1136 if (del > 0) {
1137 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) {
1138 #ifdef CONFIG_F2FS_CHECK_FS
1139 if (f2fs_test_and_set_bit(offset,
1140 se->cur_valid_map_mir))
1141 f2fs_bug_on(sbi, 1);
1142 else
1143 WARN_ON(1);
1144 #else
1145 f2fs_bug_on(sbi, 1);
1146 #endif
1147 }
1148 if (f2fs_discard_en(sbi) &&
1149 !f2fs_test_and_set_bit(offset, se->discard_map))
1150 sbi->discard_blks--;
1151 } else {
1152 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) {
1153 #ifdef CONFIG_F2FS_CHECK_FS
1154 if (!f2fs_test_and_clear_bit(offset,
1155 se->cur_valid_map_mir))
1156 f2fs_bug_on(sbi, 1);
1157 else
1158 WARN_ON(1);
1159 #else
1160 f2fs_bug_on(sbi, 1);
1161 #endif
1162 }
1163 if (f2fs_discard_en(sbi) &&
1164 f2fs_test_and_clear_bit(offset, se->discard_map))
1165 sbi->discard_blks++;
1166 }
1167 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
1168 se->ckpt_valid_blocks += del;
1169
1170 __mark_sit_entry_dirty(sbi, segno);
1171
1172 /* update total number of valid blocks to be written in ckpt area */
1173 SIT_I(sbi)->written_valid_blocks += del;
1174
1175 if (sbi->segs_per_sec > 1)
1176 get_sec_entry(sbi, segno)->valid_blocks += del;
1177 }
1178
1179 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
1180 {
1181 update_sit_entry(sbi, new, 1);
1182 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
1183 update_sit_entry(sbi, old, -1);
1184
1185 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
1186 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
1187 }
1188
1189 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
1190 {
1191 unsigned int segno = GET_SEGNO(sbi, addr);
1192 struct sit_info *sit_i = SIT_I(sbi);
1193
1194 f2fs_bug_on(sbi, addr == NULL_ADDR);
1195 if (addr == NEW_ADDR)
1196 return;
1197
1198 /* add it into sit main buffer */
1199 mutex_lock(&sit_i->sentry_lock);
1200
1201 update_sit_entry(sbi, addr, -1);
1202
1203 /* add it into dirty seglist */
1204 locate_dirty_segment(sbi, segno);
1205
1206 mutex_unlock(&sit_i->sentry_lock);
1207 }
1208
1209 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
1210 {
1211 struct sit_info *sit_i = SIT_I(sbi);
1212 unsigned int segno, offset;
1213 struct seg_entry *se;
1214 bool is_cp = false;
1215
1216 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1217 return true;
1218
1219 mutex_lock(&sit_i->sentry_lock);
1220
1221 segno = GET_SEGNO(sbi, blkaddr);
1222 se = get_seg_entry(sbi, segno);
1223 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
1224
1225 if (f2fs_test_bit(offset, se->ckpt_valid_map))
1226 is_cp = true;
1227
1228 mutex_unlock(&sit_i->sentry_lock);
1229
1230 return is_cp;
1231 }
1232
1233 /*
1234 * This function should be resided under the curseg_mutex lock
1235 */
1236 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
1237 struct f2fs_summary *sum)
1238 {
1239 struct curseg_info *curseg = CURSEG_I(sbi, type);
1240 void *addr = curseg->sum_blk;
1241 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
1242 memcpy(addr, sum, sizeof(struct f2fs_summary));
1243 }
1244
1245 /*
1246 * Calculate the number of current summary pages for writing
1247 */
1248 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
1249 {
1250 int valid_sum_count = 0;
1251 int i, sum_in_page;
1252
1253 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1254 if (sbi->ckpt->alloc_type[i] == SSR)
1255 valid_sum_count += sbi->blocks_per_seg;
1256 else {
1257 if (for_ra)
1258 valid_sum_count += le16_to_cpu(
1259 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
1260 else
1261 valid_sum_count += curseg_blkoff(sbi, i);
1262 }
1263 }
1264
1265 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
1266 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
1267 if (valid_sum_count <= sum_in_page)
1268 return 1;
1269 else if ((valid_sum_count - sum_in_page) <=
1270 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
1271 return 2;
1272 return 3;
1273 }
1274
1275 /*
1276 * Caller should put this summary page
1277 */
1278 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
1279 {
1280 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
1281 }
1282
1283 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
1284 {
1285 struct page *page = grab_meta_page(sbi, blk_addr);
1286 void *dst = page_address(page);
1287
1288 if (src)
1289 memcpy(dst, src, PAGE_SIZE);
1290 else
1291 memset(dst, 0, PAGE_SIZE);
1292 set_page_dirty(page);
1293 f2fs_put_page(page, 1);
1294 }
1295
1296 static void write_sum_page(struct f2fs_sb_info *sbi,
1297 struct f2fs_summary_block *sum_blk, block_t blk_addr)
1298 {
1299 update_meta_page(sbi, (void *)sum_blk, blk_addr);
1300 }
1301
1302 static void write_current_sum_page(struct f2fs_sb_info *sbi,
1303 int type, block_t blk_addr)
1304 {
1305 struct curseg_info *curseg = CURSEG_I(sbi, type);
1306 struct page *page = grab_meta_page(sbi, blk_addr);
1307 struct f2fs_summary_block *src = curseg->sum_blk;
1308 struct f2fs_summary_block *dst;
1309
1310 dst = (struct f2fs_summary_block *)page_address(page);
1311
1312 mutex_lock(&curseg->curseg_mutex);
1313
1314 down_read(&curseg->journal_rwsem);
1315 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
1316 up_read(&curseg->journal_rwsem);
1317
1318 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
1319 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
1320
1321 mutex_unlock(&curseg->curseg_mutex);
1322
1323 set_page_dirty(page);
1324 f2fs_put_page(page, 1);
1325 }
1326
1327 /*
1328 * Find a new segment from the free segments bitmap to right order
1329 * This function should be returned with success, otherwise BUG
1330 */
1331 static void get_new_segment(struct f2fs_sb_info *sbi,
1332 unsigned int *newseg, bool new_sec, int dir)
1333 {
1334 struct free_segmap_info *free_i = FREE_I(sbi);
1335 unsigned int segno, secno, zoneno;
1336 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
1337 unsigned int hint = *newseg / sbi->segs_per_sec;
1338 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
1339 unsigned int left_start = hint;
1340 bool init = true;
1341 int go_left = 0;
1342 int i;
1343
1344 spin_lock(&free_i->segmap_lock);
1345
1346 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
1347 segno = find_next_zero_bit(free_i->free_segmap,
1348 (hint + 1) * sbi->segs_per_sec, *newseg + 1);
1349 if (segno < (hint + 1) * sbi->segs_per_sec)
1350 goto got_it;
1351 }
1352 find_other_zone:
1353 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
1354 if (secno >= MAIN_SECS(sbi)) {
1355 if (dir == ALLOC_RIGHT) {
1356 secno = find_next_zero_bit(free_i->free_secmap,
1357 MAIN_SECS(sbi), 0);
1358 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
1359 } else {
1360 go_left = 1;
1361 left_start = hint - 1;
1362 }
1363 }
1364 if (go_left == 0)
1365 goto skip_left;
1366
1367 while (test_bit(left_start, free_i->free_secmap)) {
1368 if (left_start > 0) {
1369 left_start--;
1370 continue;
1371 }
1372 left_start = find_next_zero_bit(free_i->free_secmap,
1373 MAIN_SECS(sbi), 0);
1374 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
1375 break;
1376 }
1377 secno = left_start;
1378 skip_left:
1379 hint = secno;
1380 segno = secno * sbi->segs_per_sec;
1381 zoneno = secno / sbi->secs_per_zone;
1382
1383 /* give up on finding another zone */
1384 if (!init)
1385 goto got_it;
1386 if (sbi->secs_per_zone == 1)
1387 goto got_it;
1388 if (zoneno == old_zoneno)
1389 goto got_it;
1390 if (dir == ALLOC_LEFT) {
1391 if (!go_left && zoneno + 1 >= total_zones)
1392 goto got_it;
1393 if (go_left && zoneno == 0)
1394 goto got_it;
1395 }
1396 for (i = 0; i < NR_CURSEG_TYPE; i++)
1397 if (CURSEG_I(sbi, i)->zone == zoneno)
1398 break;
1399
1400 if (i < NR_CURSEG_TYPE) {
1401 /* zone is in user, try another */
1402 if (go_left)
1403 hint = zoneno * sbi->secs_per_zone - 1;
1404 else if (zoneno + 1 >= total_zones)
1405 hint = 0;
1406 else
1407 hint = (zoneno + 1) * sbi->secs_per_zone;
1408 init = false;
1409 goto find_other_zone;
1410 }
1411 got_it:
1412 /* set it as dirty segment in free segmap */
1413 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
1414 __set_inuse(sbi, segno);
1415 *newseg = segno;
1416 spin_unlock(&free_i->segmap_lock);
1417 }
1418
1419 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
1420 {
1421 struct curseg_info *curseg = CURSEG_I(sbi, type);
1422 struct summary_footer *sum_footer;
1423
1424 curseg->segno = curseg->next_segno;
1425 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
1426 curseg->next_blkoff = 0;
1427 curseg->next_segno = NULL_SEGNO;
1428
1429 sum_footer = &(curseg->sum_blk->footer);
1430 memset(sum_footer, 0, sizeof(struct summary_footer));
1431 if (IS_DATASEG(type))
1432 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
1433 if (IS_NODESEG(type))
1434 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
1435 __set_sit_entry_type(sbi, type, curseg->segno, modified);
1436 }
1437
1438 /*
1439 * Allocate a current working segment.
1440 * This function always allocates a free segment in LFS manner.
1441 */
1442 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
1443 {
1444 struct curseg_info *curseg = CURSEG_I(sbi, type);
1445 unsigned int segno = curseg->segno;
1446 int dir = ALLOC_LEFT;
1447
1448 write_sum_page(sbi, curseg->sum_blk,
1449 GET_SUM_BLOCK(sbi, segno));
1450 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
1451 dir = ALLOC_RIGHT;
1452
1453 if (test_opt(sbi, NOHEAP))
1454 dir = ALLOC_RIGHT;
1455
1456 get_new_segment(sbi, &segno, new_sec, dir);
1457 curseg->next_segno = segno;
1458 reset_curseg(sbi, type, 1);
1459 curseg->alloc_type = LFS;
1460 }
1461
1462 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
1463 struct curseg_info *seg, block_t start)
1464 {
1465 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
1466 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1467 unsigned long *target_map = SIT_I(sbi)->tmp_map;
1468 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1469 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1470 int i, pos;
1471
1472 for (i = 0; i < entries; i++)
1473 target_map[i] = ckpt_map[i] | cur_map[i];
1474
1475 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
1476
1477 seg->next_blkoff = pos;
1478 }
1479
1480 /*
1481 * If a segment is written by LFS manner, next block offset is just obtained
1482 * by increasing the current block offset. However, if a segment is written by
1483 * SSR manner, next block offset obtained by calling __next_free_blkoff
1484 */
1485 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
1486 struct curseg_info *seg)
1487 {
1488 if (seg->alloc_type == SSR)
1489 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
1490 else
1491 seg->next_blkoff++;
1492 }
1493
1494 /*
1495 * This function always allocates a used segment(from dirty seglist) by SSR
1496 * manner, so it should recover the existing segment information of valid blocks
1497 */
1498 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
1499 {
1500 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1501 struct curseg_info *curseg = CURSEG_I(sbi, type);
1502 unsigned int new_segno = curseg->next_segno;
1503 struct f2fs_summary_block *sum_node;
1504 struct page *sum_page;
1505
1506 write_sum_page(sbi, curseg->sum_blk,
1507 GET_SUM_BLOCK(sbi, curseg->segno));
1508 __set_test_and_inuse(sbi, new_segno);
1509
1510 mutex_lock(&dirty_i->seglist_lock);
1511 __remove_dirty_segment(sbi, new_segno, PRE);
1512 __remove_dirty_segment(sbi, new_segno, DIRTY);
1513 mutex_unlock(&dirty_i->seglist_lock);
1514
1515 reset_curseg(sbi, type, 1);
1516 curseg->alloc_type = SSR;
1517 __next_free_blkoff(sbi, curseg, 0);
1518
1519 if (reuse) {
1520 sum_page = get_sum_page(sbi, new_segno);
1521 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
1522 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
1523 f2fs_put_page(sum_page, 1);
1524 }
1525 }
1526
1527 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
1528 {
1529 struct curseg_info *curseg = CURSEG_I(sbi, type);
1530 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
1531 int i;
1532
1533 /* need_SSR() already forces to do this */
1534 if (v_ops->get_victim(sbi, &(curseg)->next_segno, BG_GC, type, SSR))
1535 return 1;
1536
1537 if (IS_NODESEG(type))
1538 return 0;
1539
1540 /* For data segments, let's do SSR more intensively */
1541 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1542 if (i == type)
1543 continue;
1544 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
1545 BG_GC, i, SSR))
1546 return 1;
1547 }
1548 return 0;
1549 }
1550
1551 /*
1552 * flush out current segment and replace it with new segment
1553 * This function should be returned with success, otherwise BUG
1554 */
1555 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
1556 int type, bool force)
1557 {
1558 if (force)
1559 new_curseg(sbi, type, true);
1560 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
1561 type == CURSEG_WARM_NODE)
1562 new_curseg(sbi, type, false);
1563 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
1564 change_curseg(sbi, type, true);
1565 else
1566 new_curseg(sbi, type, false);
1567
1568 stat_inc_seg_type(sbi, CURSEG_I(sbi, type));
1569 }
1570
1571 void allocate_new_segments(struct f2fs_sb_info *sbi)
1572 {
1573 struct curseg_info *curseg;
1574 unsigned int old_segno;
1575 int i;
1576
1577 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1578 curseg = CURSEG_I(sbi, i);
1579 old_segno = curseg->segno;
1580 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
1581 locate_dirty_segment(sbi, old_segno);
1582 }
1583 }
1584
1585 static const struct segment_allocation default_salloc_ops = {
1586 .allocate_segment = allocate_segment_by_default,
1587 };
1588
1589 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1590 {
1591 __u64 trim_start = cpc->trim_start;
1592 bool has_candidate = false;
1593
1594 mutex_lock(&SIT_I(sbi)->sentry_lock);
1595 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
1596 if (add_discard_addrs(sbi, cpc, true)) {
1597 has_candidate = true;
1598 break;
1599 }
1600 }
1601 mutex_unlock(&SIT_I(sbi)->sentry_lock);
1602
1603 cpc->trim_start = trim_start;
1604 return has_candidate;
1605 }
1606
1607 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
1608 {
1609 __u64 start = F2FS_BYTES_TO_BLK(range->start);
1610 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
1611 unsigned int start_segno, end_segno;
1612 struct cp_control cpc;
1613 int err = 0;
1614
1615 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
1616 return -EINVAL;
1617
1618 cpc.trimmed = 0;
1619 if (end <= MAIN_BLKADDR(sbi))
1620 goto out;
1621
1622 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1623 f2fs_msg(sbi->sb, KERN_WARNING,
1624 "Found FS corruption, run fsck to fix.");
1625 goto out;
1626 }
1627
1628 /* start/end segment number in main_area */
1629 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
1630 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
1631 GET_SEGNO(sbi, end);
1632 cpc.reason = CP_DISCARD;
1633 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
1634
1635 /* do checkpoint to issue discard commands safely */
1636 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
1637 cpc.trim_start = start_segno;
1638
1639 if (sbi->discard_blks == 0)
1640 break;
1641 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
1642 cpc.trim_end = end_segno;
1643 else
1644 cpc.trim_end = min_t(unsigned int,
1645 rounddown(start_segno +
1646 BATCHED_TRIM_SEGMENTS(sbi),
1647 sbi->segs_per_sec) - 1, end_segno);
1648
1649 mutex_lock(&sbi->gc_mutex);
1650 err = write_checkpoint(sbi, &cpc);
1651 mutex_unlock(&sbi->gc_mutex);
1652 if (err)
1653 break;
1654
1655 schedule();
1656 }
1657 out:
1658 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
1659 return err;
1660 }
1661
1662 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
1663 {
1664 struct curseg_info *curseg = CURSEG_I(sbi, type);
1665 if (curseg->next_blkoff < sbi->blocks_per_seg)
1666 return true;
1667 return false;
1668 }
1669
1670 static int __get_segment_type_2(struct page *page, enum page_type p_type)
1671 {
1672 if (p_type == DATA)
1673 return CURSEG_HOT_DATA;
1674 else
1675 return CURSEG_HOT_NODE;
1676 }
1677
1678 static int __get_segment_type_4(struct page *page, enum page_type p_type)
1679 {
1680 if (p_type == DATA) {
1681 struct inode *inode = page->mapping->host;
1682
1683 if (S_ISDIR(inode->i_mode))
1684 return CURSEG_HOT_DATA;
1685 else
1686 return CURSEG_COLD_DATA;
1687 } else {
1688 if (IS_DNODE(page) && is_cold_node(page))
1689 return CURSEG_WARM_NODE;
1690 else
1691 return CURSEG_COLD_NODE;
1692 }
1693 }
1694
1695 static int __get_segment_type_6(struct page *page, enum page_type p_type)
1696 {
1697 if (p_type == DATA) {
1698 struct inode *inode = page->mapping->host;
1699
1700 if (S_ISDIR(inode->i_mode))
1701 return CURSEG_HOT_DATA;
1702 else if (is_cold_data(page) || file_is_cold(inode))
1703 return CURSEG_COLD_DATA;
1704 else
1705 return CURSEG_WARM_DATA;
1706 } else {
1707 if (IS_DNODE(page))
1708 return is_cold_node(page) ? CURSEG_WARM_NODE :
1709 CURSEG_HOT_NODE;
1710 else
1711 return CURSEG_COLD_NODE;
1712 }
1713 }
1714
1715 static int __get_segment_type(struct page *page, enum page_type p_type)
1716 {
1717 switch (F2FS_P_SB(page)->active_logs) {
1718 case 2:
1719 return __get_segment_type_2(page, p_type);
1720 case 4:
1721 return __get_segment_type_4(page, p_type);
1722 }
1723 /* NR_CURSEG_TYPE(6) logs by default */
1724 f2fs_bug_on(F2FS_P_SB(page),
1725 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
1726 return __get_segment_type_6(page, p_type);
1727 }
1728
1729 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
1730 block_t old_blkaddr, block_t *new_blkaddr,
1731 struct f2fs_summary *sum, int type)
1732 {
1733 struct sit_info *sit_i = SIT_I(sbi);
1734 struct curseg_info *curseg = CURSEG_I(sbi, type);
1735
1736 mutex_lock(&curseg->curseg_mutex);
1737 mutex_lock(&sit_i->sentry_lock);
1738
1739 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
1740
1741 f2fs_wait_discard_bio(sbi, *new_blkaddr);
1742
1743 /*
1744 * __add_sum_entry should be resided under the curseg_mutex
1745 * because, this function updates a summary entry in the
1746 * current summary block.
1747 */
1748 __add_sum_entry(sbi, type, sum);
1749
1750 __refresh_next_blkoff(sbi, curseg);
1751
1752 stat_inc_block_count(sbi, curseg);
1753
1754 /*
1755 * SIT information should be updated before segment allocation,
1756 * since SSR needs latest valid block information.
1757 */
1758 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1759
1760 if (!__has_curseg_space(sbi, type))
1761 sit_i->s_ops->allocate_segment(sbi, type, false);
1762
1763 mutex_unlock(&sit_i->sentry_lock);
1764
1765 if (page && IS_NODESEG(type))
1766 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1767
1768 mutex_unlock(&curseg->curseg_mutex);
1769 }
1770
1771 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
1772 {
1773 int type = __get_segment_type(fio->page, fio->type);
1774 int err;
1775
1776 if (fio->type == NODE || fio->type == DATA)
1777 mutex_lock(&fio->sbi->wio_mutex[fio->type]);
1778 reallocate:
1779 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
1780 &fio->new_blkaddr, sum, type);
1781
1782 /* writeout dirty page into bdev */
1783 err = f2fs_submit_page_mbio(fio);
1784 if (err == -EAGAIN) {
1785 fio->old_blkaddr = fio->new_blkaddr;
1786 goto reallocate;
1787 }
1788
1789 if (fio->type == NODE || fio->type == DATA)
1790 mutex_unlock(&fio->sbi->wio_mutex[fio->type]);
1791 }
1792
1793 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1794 {
1795 struct f2fs_io_info fio = {
1796 .sbi = sbi,
1797 .type = META,
1798 .op = REQ_OP_WRITE,
1799 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
1800 .old_blkaddr = page->index,
1801 .new_blkaddr = page->index,
1802 .page = page,
1803 .encrypted_page = NULL,
1804 };
1805
1806 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
1807 fio.op_flags &= ~REQ_META;
1808
1809 set_page_writeback(page);
1810 f2fs_submit_page_mbio(&fio);
1811 }
1812
1813 void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
1814 {
1815 struct f2fs_summary sum;
1816
1817 set_summary(&sum, nid, 0, 0);
1818 do_write_page(&sum, fio);
1819 }
1820
1821 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
1822 {
1823 struct f2fs_sb_info *sbi = fio->sbi;
1824 struct f2fs_summary sum;
1825 struct node_info ni;
1826
1827 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
1828 get_node_info(sbi, dn->nid, &ni);
1829 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1830 do_write_page(&sum, fio);
1831 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
1832 }
1833
1834 void rewrite_data_page(struct f2fs_io_info *fio)
1835 {
1836 fio->new_blkaddr = fio->old_blkaddr;
1837 stat_inc_inplace_blocks(fio->sbi);
1838 f2fs_submit_page_mbio(fio);
1839 }
1840
1841 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
1842 block_t old_blkaddr, block_t new_blkaddr,
1843 bool recover_curseg, bool recover_newaddr)
1844 {
1845 struct sit_info *sit_i = SIT_I(sbi);
1846 struct curseg_info *curseg;
1847 unsigned int segno, old_cursegno;
1848 struct seg_entry *se;
1849 int type;
1850 unsigned short old_blkoff;
1851
1852 segno = GET_SEGNO(sbi, new_blkaddr);
1853 se = get_seg_entry(sbi, segno);
1854 type = se->type;
1855
1856 if (!recover_curseg) {
1857 /* for recovery flow */
1858 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1859 if (old_blkaddr == NULL_ADDR)
1860 type = CURSEG_COLD_DATA;
1861 else
1862 type = CURSEG_WARM_DATA;
1863 }
1864 } else {
1865 if (!IS_CURSEG(sbi, segno))
1866 type = CURSEG_WARM_DATA;
1867 }
1868
1869 curseg = CURSEG_I(sbi, type);
1870
1871 mutex_lock(&curseg->curseg_mutex);
1872 mutex_lock(&sit_i->sentry_lock);
1873
1874 old_cursegno = curseg->segno;
1875 old_blkoff = curseg->next_blkoff;
1876
1877 /* change the current segment */
1878 if (segno != curseg->segno) {
1879 curseg->next_segno = segno;
1880 change_curseg(sbi, type, true);
1881 }
1882
1883 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1884 __add_sum_entry(sbi, type, sum);
1885
1886 if (!recover_curseg || recover_newaddr)
1887 update_sit_entry(sbi, new_blkaddr, 1);
1888 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1889 update_sit_entry(sbi, old_blkaddr, -1);
1890
1891 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1892 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
1893
1894 locate_dirty_segment(sbi, old_cursegno);
1895
1896 if (recover_curseg) {
1897 if (old_cursegno != curseg->segno) {
1898 curseg->next_segno = old_cursegno;
1899 change_curseg(sbi, type, true);
1900 }
1901 curseg->next_blkoff = old_blkoff;
1902 }
1903
1904 mutex_unlock(&sit_i->sentry_lock);
1905 mutex_unlock(&curseg->curseg_mutex);
1906 }
1907
1908 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
1909 block_t old_addr, block_t new_addr,
1910 unsigned char version, bool recover_curseg,
1911 bool recover_newaddr)
1912 {
1913 struct f2fs_summary sum;
1914
1915 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
1916
1917 __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
1918 recover_curseg, recover_newaddr);
1919
1920 f2fs_update_data_blkaddr(dn, new_addr);
1921 }
1922
1923 void f2fs_wait_on_page_writeback(struct page *page,
1924 enum page_type type, bool ordered)
1925 {
1926 if (PageWriteback(page)) {
1927 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1928
1929 f2fs_submit_merged_bio_cond(sbi, page->mapping->host,
1930 0, page->index, type, WRITE);
1931 if (ordered)
1932 wait_on_page_writeback(page);
1933 else
1934 wait_for_stable_page(page);
1935 }
1936 }
1937
1938 void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
1939 block_t blkaddr)
1940 {
1941 struct page *cpage;
1942
1943 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
1944 return;
1945
1946 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
1947 if (cpage) {
1948 f2fs_wait_on_page_writeback(cpage, DATA, true);
1949 f2fs_put_page(cpage, 1);
1950 }
1951 }
1952
1953 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1954 {
1955 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1956 struct curseg_info *seg_i;
1957 unsigned char *kaddr;
1958 struct page *page;
1959 block_t start;
1960 int i, j, offset;
1961
1962 start = start_sum_block(sbi);
1963
1964 page = get_meta_page(sbi, start++);
1965 kaddr = (unsigned char *)page_address(page);
1966
1967 /* Step 1: restore nat cache */
1968 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1969 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
1970
1971 /* Step 2: restore sit cache */
1972 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1973 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
1974 offset = 2 * SUM_JOURNAL_SIZE;
1975
1976 /* Step 3: restore summary entries */
1977 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1978 unsigned short blk_off;
1979 unsigned int segno;
1980
1981 seg_i = CURSEG_I(sbi, i);
1982 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1983 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1984 seg_i->next_segno = segno;
1985 reset_curseg(sbi, i, 0);
1986 seg_i->alloc_type = ckpt->alloc_type[i];
1987 seg_i->next_blkoff = blk_off;
1988
1989 if (seg_i->alloc_type == SSR)
1990 blk_off = sbi->blocks_per_seg;
1991
1992 for (j = 0; j < blk_off; j++) {
1993 struct f2fs_summary *s;
1994 s = (struct f2fs_summary *)(kaddr + offset);
1995 seg_i->sum_blk->entries[j] = *s;
1996 offset += SUMMARY_SIZE;
1997 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
1998 SUM_FOOTER_SIZE)
1999 continue;
2000
2001 f2fs_put_page(page, 1);
2002 page = NULL;
2003
2004 page = get_meta_page(sbi, start++);
2005 kaddr = (unsigned char *)page_address(page);
2006 offset = 0;
2007 }
2008 }
2009 f2fs_put_page(page, 1);
2010 return 0;
2011 }
2012
2013 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
2014 {
2015 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2016 struct f2fs_summary_block *sum;
2017 struct curseg_info *curseg;
2018 struct page *new;
2019 unsigned short blk_off;
2020 unsigned int segno = 0;
2021 block_t blk_addr = 0;
2022
2023 /* get segment number and block addr */
2024 if (IS_DATASEG(type)) {
2025 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
2026 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
2027 CURSEG_HOT_DATA]);
2028 if (__exist_node_summaries(sbi))
2029 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
2030 else
2031 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
2032 } else {
2033 segno = le32_to_cpu(ckpt->cur_node_segno[type -
2034 CURSEG_HOT_NODE]);
2035 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
2036 CURSEG_HOT_NODE]);
2037 if (__exist_node_summaries(sbi))
2038 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
2039 type - CURSEG_HOT_NODE);
2040 else
2041 blk_addr = GET_SUM_BLOCK(sbi, segno);
2042 }
2043
2044 new = get_meta_page(sbi, blk_addr);
2045 sum = (struct f2fs_summary_block *)page_address(new);
2046
2047 if (IS_NODESEG(type)) {
2048 if (__exist_node_summaries(sbi)) {
2049 struct f2fs_summary *ns = &sum->entries[0];
2050 int i;
2051 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
2052 ns->version = 0;
2053 ns->ofs_in_node = 0;
2054 }
2055 } else {
2056 int err;
2057
2058 err = restore_node_summary(sbi, segno, sum);
2059 if (err) {
2060 f2fs_put_page(new, 1);
2061 return err;
2062 }
2063 }
2064 }
2065
2066 /* set uncompleted segment to curseg */
2067 curseg = CURSEG_I(sbi, type);
2068 mutex_lock(&curseg->curseg_mutex);
2069
2070 /* update journal info */
2071 down_write(&curseg->journal_rwsem);
2072 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
2073 up_write(&curseg->journal_rwsem);
2074
2075 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
2076 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
2077 curseg->next_segno = segno;
2078 reset_curseg(sbi, type, 0);
2079 curseg->alloc_type = ckpt->alloc_type[type];
2080 curseg->next_blkoff = blk_off;
2081 mutex_unlock(&curseg->curseg_mutex);
2082 f2fs_put_page(new, 1);
2083 return 0;
2084 }
2085
2086 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
2087 {
2088 int type = CURSEG_HOT_DATA;
2089 int err;
2090
2091 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
2092 int npages = npages_for_summary_flush(sbi, true);
2093
2094 if (npages >= 2)
2095 ra_meta_pages(sbi, start_sum_block(sbi), npages,
2096 META_CP, true);
2097
2098 /* restore for compacted data summary */
2099 if (read_compacted_summaries(sbi))
2100 return -EINVAL;
2101 type = CURSEG_HOT_NODE;
2102 }
2103
2104 if (__exist_node_summaries(sbi))
2105 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
2106 NR_CURSEG_TYPE - type, META_CP, true);
2107
2108 for (; type <= CURSEG_COLD_NODE; type++) {
2109 err = read_normal_summaries(sbi, type);
2110 if (err)
2111 return err;
2112 }
2113
2114 return 0;
2115 }
2116
2117 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
2118 {
2119 struct page *page;
2120 unsigned char *kaddr;
2121 struct f2fs_summary *summary;
2122 struct curseg_info *seg_i;
2123 int written_size = 0;
2124 int i, j;
2125
2126 page = grab_meta_page(sbi, blkaddr++);
2127 kaddr = (unsigned char *)page_address(page);
2128
2129 /* Step 1: write nat cache */
2130 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
2131 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
2132 written_size += SUM_JOURNAL_SIZE;
2133
2134 /* Step 2: write sit cache */
2135 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
2136 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
2137 written_size += SUM_JOURNAL_SIZE;
2138
2139 /* Step 3: write summary entries */
2140 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2141 unsigned short blkoff;
2142 seg_i = CURSEG_I(sbi, i);
2143 if (sbi->ckpt->alloc_type[i] == SSR)
2144 blkoff = sbi->blocks_per_seg;
2145 else
2146 blkoff = curseg_blkoff(sbi, i);
2147
2148 for (j = 0; j < blkoff; j++) {
2149 if (!page) {
2150 page = grab_meta_page(sbi, blkaddr++);
2151 kaddr = (unsigned char *)page_address(page);
2152 written_size = 0;
2153 }
2154 summary = (struct f2fs_summary *)(kaddr + written_size);
2155 *summary = seg_i->sum_blk->entries[j];
2156 written_size += SUMMARY_SIZE;
2157
2158 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
2159 SUM_FOOTER_SIZE)
2160 continue;
2161
2162 set_page_dirty(page);
2163 f2fs_put_page(page, 1);
2164 page = NULL;
2165 }
2166 }
2167 if (page) {
2168 set_page_dirty(page);
2169 f2fs_put_page(page, 1);
2170 }
2171 }
2172
2173 static void write_normal_summaries(struct f2fs_sb_info *sbi,
2174 block_t blkaddr, int type)
2175 {
2176 int i, end;
2177 if (IS_DATASEG(type))
2178 end = type + NR_CURSEG_DATA_TYPE;
2179 else
2180 end = type + NR_CURSEG_NODE_TYPE;
2181
2182 for (i = type; i < end; i++)
2183 write_current_sum_page(sbi, i, blkaddr + (i - type));
2184 }
2185
2186 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2187 {
2188 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
2189 write_compacted_summaries(sbi, start_blk);
2190 else
2191 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
2192 }
2193
2194 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
2195 {
2196 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
2197 }
2198
2199 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2200 unsigned int val, int alloc)
2201 {
2202 int i;
2203
2204 if (type == NAT_JOURNAL) {
2205 for (i = 0; i < nats_in_cursum(journal); i++) {
2206 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
2207 return i;
2208 }
2209 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
2210 return update_nats_in_cursum(journal, 1);
2211 } else if (type == SIT_JOURNAL) {
2212 for (i = 0; i < sits_in_cursum(journal); i++)
2213 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
2214 return i;
2215 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
2216 return update_sits_in_cursum(journal, 1);
2217 }
2218 return -1;
2219 }
2220
2221 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
2222 unsigned int segno)
2223 {
2224 return get_meta_page(sbi, current_sit_addr(sbi, segno));
2225 }
2226
2227 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
2228 unsigned int start)
2229 {
2230 struct sit_info *sit_i = SIT_I(sbi);
2231 struct page *src_page, *dst_page;
2232 pgoff_t src_off, dst_off;
2233 void *src_addr, *dst_addr;
2234
2235 src_off = current_sit_addr(sbi, start);
2236 dst_off = next_sit_addr(sbi, src_off);
2237
2238 /* get current sit block page without lock */
2239 src_page = get_meta_page(sbi, src_off);
2240 dst_page = grab_meta_page(sbi, dst_off);
2241 f2fs_bug_on(sbi, PageDirty(src_page));
2242
2243 src_addr = page_address(src_page);
2244 dst_addr = page_address(dst_page);
2245 memcpy(dst_addr, src_addr, PAGE_SIZE);
2246
2247 set_page_dirty(dst_page);
2248 f2fs_put_page(src_page, 1);
2249
2250 set_to_next_sit(sit_i, start);
2251
2252 return dst_page;
2253 }
2254
2255 static struct sit_entry_set *grab_sit_entry_set(void)
2256 {
2257 struct sit_entry_set *ses =
2258 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
2259
2260 ses->entry_cnt = 0;
2261 INIT_LIST_HEAD(&ses->set_list);
2262 return ses;
2263 }
2264
2265 static void release_sit_entry_set(struct sit_entry_set *ses)
2266 {
2267 list_del(&ses->set_list);
2268 kmem_cache_free(sit_entry_set_slab, ses);
2269 }
2270
2271 static void adjust_sit_entry_set(struct sit_entry_set *ses,
2272 struct list_head *head)
2273 {
2274 struct sit_entry_set *next = ses;
2275
2276 if (list_is_last(&ses->set_list, head))
2277 return;
2278
2279 list_for_each_entry_continue(next, head, set_list)
2280 if (ses->entry_cnt <= next->entry_cnt)
2281 break;
2282
2283 list_move_tail(&ses->set_list, &next->set_list);
2284 }
2285
2286 static void add_sit_entry(unsigned int segno, struct list_head *head)
2287 {
2288 struct sit_entry_set *ses;
2289 unsigned int start_segno = START_SEGNO(segno);
2290
2291 list_for_each_entry(ses, head, set_list) {
2292 if (ses->start_segno == start_segno) {
2293 ses->entry_cnt++;
2294 adjust_sit_entry_set(ses, head);
2295 return;
2296 }
2297 }
2298
2299 ses = grab_sit_entry_set();
2300
2301 ses->start_segno = start_segno;
2302 ses->entry_cnt++;
2303 list_add(&ses->set_list, head);
2304 }
2305
2306 static void add_sits_in_set(struct f2fs_sb_info *sbi)
2307 {
2308 struct f2fs_sm_info *sm_info = SM_I(sbi);
2309 struct list_head *set_list = &sm_info->sit_entry_set;
2310 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
2311 unsigned int segno;
2312
2313 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
2314 add_sit_entry(segno, set_list);
2315 }
2316
2317 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
2318 {
2319 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2320 struct f2fs_journal *journal = curseg->journal;
2321 int i;
2322
2323 down_write(&curseg->journal_rwsem);
2324 for (i = 0; i < sits_in_cursum(journal); i++) {
2325 unsigned int segno;
2326 bool dirtied;
2327
2328 segno = le32_to_cpu(segno_in_journal(journal, i));
2329 dirtied = __mark_sit_entry_dirty(sbi, segno);
2330
2331 if (!dirtied)
2332 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
2333 }
2334 update_sits_in_cursum(journal, -i);
2335 up_write(&curseg->journal_rwsem);
2336 }
2337
2338 /*
2339 * CP calls this function, which flushes SIT entries including sit_journal,
2340 * and moves prefree segs to free segs.
2341 */
2342 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2343 {
2344 struct sit_info *sit_i = SIT_I(sbi);
2345 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
2346 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2347 struct f2fs_journal *journal = curseg->journal;
2348 struct sit_entry_set *ses, *tmp;
2349 struct list_head *head = &SM_I(sbi)->sit_entry_set;
2350 bool to_journal = true;
2351 struct seg_entry *se;
2352
2353 mutex_lock(&sit_i->sentry_lock);
2354
2355 if (!sit_i->dirty_sentries)
2356 goto out;
2357
2358 /*
2359 * add and account sit entries of dirty bitmap in sit entry
2360 * set temporarily
2361 */
2362 add_sits_in_set(sbi);
2363
2364 /*
2365 * if there are no enough space in journal to store dirty sit
2366 * entries, remove all entries from journal and add and account
2367 * them in sit entry set.
2368 */
2369 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
2370 remove_sits_in_journal(sbi);
2371
2372 /*
2373 * there are two steps to flush sit entries:
2374 * #1, flush sit entries to journal in current cold data summary block.
2375 * #2, flush sit entries to sit page.
2376 */
2377 list_for_each_entry_safe(ses, tmp, head, set_list) {
2378 struct page *page = NULL;
2379 struct f2fs_sit_block *raw_sit = NULL;
2380 unsigned int start_segno = ses->start_segno;
2381 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
2382 (unsigned long)MAIN_SEGS(sbi));
2383 unsigned int segno = start_segno;
2384
2385 if (to_journal &&
2386 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
2387 to_journal = false;
2388
2389 if (to_journal) {
2390 down_write(&curseg->journal_rwsem);
2391 } else {
2392 page = get_next_sit_page(sbi, start_segno);
2393 raw_sit = page_address(page);
2394 }
2395
2396 /* flush dirty sit entries in region of current sit set */
2397 for_each_set_bit_from(segno, bitmap, end) {
2398 int offset, sit_offset;
2399
2400 se = get_seg_entry(sbi, segno);
2401
2402 /* add discard candidates */
2403 if (cpc->reason != CP_DISCARD) {
2404 cpc->trim_start = segno;
2405 add_discard_addrs(sbi, cpc, false);
2406 }
2407
2408 if (to_journal) {
2409 offset = lookup_journal_in_cursum(journal,
2410 SIT_JOURNAL, segno, 1);
2411 f2fs_bug_on(sbi, offset < 0);
2412 segno_in_journal(journal, offset) =
2413 cpu_to_le32(segno);
2414 seg_info_to_raw_sit(se,
2415 &sit_in_journal(journal, offset));
2416 } else {
2417 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
2418 seg_info_to_raw_sit(se,
2419 &raw_sit->entries[sit_offset]);
2420 }
2421
2422 __clear_bit(segno, bitmap);
2423 sit_i->dirty_sentries--;
2424 ses->entry_cnt--;
2425 }
2426
2427 if (to_journal)
2428 up_write(&curseg->journal_rwsem);
2429 else
2430 f2fs_put_page(page, 1);
2431
2432 f2fs_bug_on(sbi, ses->entry_cnt);
2433 release_sit_entry_set(ses);
2434 }
2435
2436 f2fs_bug_on(sbi, !list_empty(head));
2437 f2fs_bug_on(sbi, sit_i->dirty_sentries);
2438 out:
2439 if (cpc->reason == CP_DISCARD) {
2440 __u64 trim_start = cpc->trim_start;
2441
2442 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
2443 add_discard_addrs(sbi, cpc, false);
2444
2445 cpc->trim_start = trim_start;
2446 }
2447 mutex_unlock(&sit_i->sentry_lock);
2448
2449 set_prefree_as_free_segments(sbi);
2450 }
2451
2452 static int build_sit_info(struct f2fs_sb_info *sbi)
2453 {
2454 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2455 struct sit_info *sit_i;
2456 unsigned int sit_segs, start;
2457 char *src_bitmap;
2458 unsigned int bitmap_size;
2459
2460 /* allocate memory for SIT information */
2461 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
2462 if (!sit_i)
2463 return -ENOMEM;
2464
2465 SM_I(sbi)->sit_info = sit_i;
2466
2467 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
2468 sizeof(struct seg_entry), GFP_KERNEL);
2469 if (!sit_i->sentries)
2470 return -ENOMEM;
2471
2472 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2473 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2474 if (!sit_i->dirty_sentries_bitmap)
2475 return -ENOMEM;
2476
2477 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2478 sit_i->sentries[start].cur_valid_map
2479 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2480 sit_i->sentries[start].ckpt_valid_map
2481 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2482 if (!sit_i->sentries[start].cur_valid_map ||
2483 !sit_i->sentries[start].ckpt_valid_map)
2484 return -ENOMEM;
2485
2486 #ifdef CONFIG_F2FS_CHECK_FS
2487 sit_i->sentries[start].cur_valid_map_mir
2488 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2489 if (!sit_i->sentries[start].cur_valid_map_mir)
2490 return -ENOMEM;
2491 #endif
2492
2493 if (f2fs_discard_en(sbi)) {
2494 sit_i->sentries[start].discard_map
2495 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2496 if (!sit_i->sentries[start].discard_map)
2497 return -ENOMEM;
2498 }
2499 }
2500
2501 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
2502 if (!sit_i->tmp_map)
2503 return -ENOMEM;
2504
2505 if (sbi->segs_per_sec > 1) {
2506 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
2507 sizeof(struct sec_entry), GFP_KERNEL);
2508 if (!sit_i->sec_entries)
2509 return -ENOMEM;
2510 }
2511
2512 /* get information related with SIT */
2513 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
2514
2515 /* setup SIT bitmap from ckeckpoint pack */
2516 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
2517 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
2518
2519 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2520 if (!sit_i->sit_bitmap)
2521 return -ENOMEM;
2522
2523 #ifdef CONFIG_F2FS_CHECK_FS
2524 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
2525 if (!sit_i->sit_bitmap_mir)
2526 return -ENOMEM;
2527 #endif
2528
2529 /* init SIT information */
2530 sit_i->s_ops = &default_salloc_ops;
2531
2532 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
2533 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
2534 sit_i->written_valid_blocks = 0;
2535 sit_i->bitmap_size = bitmap_size;
2536 sit_i->dirty_sentries = 0;
2537 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
2538 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
2539 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
2540 mutex_init(&sit_i->sentry_lock);
2541 return 0;
2542 }
2543
2544 static int build_free_segmap(struct f2fs_sb_info *sbi)
2545 {
2546 struct free_segmap_info *free_i;
2547 unsigned int bitmap_size, sec_bitmap_size;
2548
2549 /* allocate memory for free segmap information */
2550 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
2551 if (!free_i)
2552 return -ENOMEM;
2553
2554 SM_I(sbi)->free_info = free_i;
2555
2556 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2557 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
2558 if (!free_i->free_segmap)
2559 return -ENOMEM;
2560
2561 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2562 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
2563 if (!free_i->free_secmap)
2564 return -ENOMEM;
2565
2566 /* set all segments as dirty temporarily */
2567 memset(free_i->free_segmap, 0xff, bitmap_size);
2568 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
2569
2570 /* init free segmap information */
2571 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
2572 free_i->free_segments = 0;
2573 free_i->free_sections = 0;
2574 spin_lock_init(&free_i->segmap_lock);
2575 return 0;
2576 }
2577
2578 static int build_curseg(struct f2fs_sb_info *sbi)
2579 {
2580 struct curseg_info *array;
2581 int i;
2582
2583 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
2584 if (!array)
2585 return -ENOMEM;
2586
2587 SM_I(sbi)->curseg_array = array;
2588
2589 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2590 mutex_init(&array[i].curseg_mutex);
2591 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
2592 if (!array[i].sum_blk)
2593 return -ENOMEM;
2594 init_rwsem(&array[i].journal_rwsem);
2595 array[i].journal = kzalloc(sizeof(struct f2fs_journal),
2596 GFP_KERNEL);
2597 if (!array[i].journal)
2598 return -ENOMEM;
2599 array[i].segno = NULL_SEGNO;
2600 array[i].next_blkoff = 0;
2601 }
2602 return restore_curseg_summaries(sbi);
2603 }
2604
2605 static void build_sit_entries(struct f2fs_sb_info *sbi)
2606 {
2607 struct sit_info *sit_i = SIT_I(sbi);
2608 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
2609 struct f2fs_journal *journal = curseg->journal;
2610 struct seg_entry *se;
2611 struct f2fs_sit_entry sit;
2612 int sit_blk_cnt = SIT_BLK_CNT(sbi);
2613 unsigned int i, start, end;
2614 unsigned int readed, start_blk = 0;
2615
2616 do {
2617 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
2618 META_SIT, true);
2619
2620 start = start_blk * sit_i->sents_per_block;
2621 end = (start_blk + readed) * sit_i->sents_per_block;
2622
2623 for (; start < end && start < MAIN_SEGS(sbi); start++) {
2624 struct f2fs_sit_block *sit_blk;
2625 struct page *page;
2626
2627 se = &sit_i->sentries[start];
2628 page = get_current_sit_page(sbi, start);
2629 sit_blk = (struct f2fs_sit_block *)page_address(page);
2630 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
2631 f2fs_put_page(page, 1);
2632
2633 check_block_count(sbi, start, &sit);
2634 seg_info_from_raw_sit(se, &sit);
2635
2636 /* build discard map only one time */
2637 if (f2fs_discard_en(sbi)) {
2638 memcpy(se->discard_map, se->cur_valid_map,
2639 SIT_VBLOCK_MAP_SIZE);
2640 sbi->discard_blks += sbi->blocks_per_seg -
2641 se->valid_blocks;
2642 }
2643
2644 if (sbi->segs_per_sec > 1)
2645 get_sec_entry(sbi, start)->valid_blocks +=
2646 se->valid_blocks;
2647 }
2648 start_blk += readed;
2649 } while (start_blk < sit_blk_cnt);
2650
2651 down_read(&curseg->journal_rwsem);
2652 for (i = 0; i < sits_in_cursum(journal); i++) {
2653 unsigned int old_valid_blocks;
2654
2655 start = le32_to_cpu(segno_in_journal(journal, i));
2656 se = &sit_i->sentries[start];
2657 sit = sit_in_journal(journal, i);
2658
2659 old_valid_blocks = se->valid_blocks;
2660
2661 check_block_count(sbi, start, &sit);
2662 seg_info_from_raw_sit(se, &sit);
2663
2664 if (f2fs_discard_en(sbi)) {
2665 memcpy(se->discard_map, se->cur_valid_map,
2666 SIT_VBLOCK_MAP_SIZE);
2667 sbi->discard_blks += old_valid_blocks -
2668 se->valid_blocks;
2669 }
2670
2671 if (sbi->segs_per_sec > 1)
2672 get_sec_entry(sbi, start)->valid_blocks +=
2673 se->valid_blocks - old_valid_blocks;
2674 }
2675 up_read(&curseg->journal_rwsem);
2676 }
2677
2678 static void init_free_segmap(struct f2fs_sb_info *sbi)
2679 {
2680 unsigned int start;
2681 int type;
2682
2683 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2684 struct seg_entry *sentry = get_seg_entry(sbi, start);
2685 if (!sentry->valid_blocks)
2686 __set_free(sbi, start);
2687 else
2688 SIT_I(sbi)->written_valid_blocks +=
2689 sentry->valid_blocks;
2690 }
2691
2692 /* set use the current segments */
2693 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
2694 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
2695 __set_test_and_inuse(sbi, curseg_t->segno);
2696 }
2697 }
2698
2699 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
2700 {
2701 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2702 struct free_segmap_info *free_i = FREE_I(sbi);
2703 unsigned int segno = 0, offset = 0;
2704 unsigned short valid_blocks;
2705
2706 while (1) {
2707 /* find dirty segment based on free segmap */
2708 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
2709 if (segno >= MAIN_SEGS(sbi))
2710 break;
2711 offset = segno + 1;
2712 valid_blocks = get_valid_blocks(sbi, segno, 0);
2713 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
2714 continue;
2715 if (valid_blocks > sbi->blocks_per_seg) {
2716 f2fs_bug_on(sbi, 1);
2717 continue;
2718 }
2719 mutex_lock(&dirty_i->seglist_lock);
2720 __locate_dirty_segment(sbi, segno, DIRTY);
2721 mutex_unlock(&dirty_i->seglist_lock);
2722 }
2723 }
2724
2725 static int init_victim_secmap(struct f2fs_sb_info *sbi)
2726 {
2727 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2728 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
2729
2730 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2731 if (!dirty_i->victim_secmap)
2732 return -ENOMEM;
2733 return 0;
2734 }
2735
2736 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
2737 {
2738 struct dirty_seglist_info *dirty_i;
2739 unsigned int bitmap_size, i;
2740
2741 /* allocate memory for dirty segments list information */
2742 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
2743 if (!dirty_i)
2744 return -ENOMEM;
2745
2746 SM_I(sbi)->dirty_info = dirty_i;
2747 mutex_init(&dirty_i->seglist_lock);
2748
2749 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
2750
2751 for (i = 0; i < NR_DIRTY_TYPE; i++) {
2752 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
2753 if (!dirty_i->dirty_segmap[i])
2754 return -ENOMEM;
2755 }
2756
2757 init_dirty_segmap(sbi);
2758 return init_victim_secmap(sbi);
2759 }
2760
2761 /*
2762 * Update min, max modified time for cost-benefit GC algorithm
2763 */
2764 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
2765 {
2766 struct sit_info *sit_i = SIT_I(sbi);
2767 unsigned int segno;
2768
2769 mutex_lock(&sit_i->sentry_lock);
2770
2771 sit_i->min_mtime = LLONG_MAX;
2772
2773 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
2774 unsigned int i;
2775 unsigned long long mtime = 0;
2776
2777 for (i = 0; i < sbi->segs_per_sec; i++)
2778 mtime += get_seg_entry(sbi, segno + i)->mtime;
2779
2780 mtime = div_u64(mtime, sbi->segs_per_sec);
2781
2782 if (sit_i->min_mtime > mtime)
2783 sit_i->min_mtime = mtime;
2784 }
2785 sit_i->max_mtime = get_mtime(sbi);
2786 mutex_unlock(&sit_i->sentry_lock);
2787 }
2788
2789 int build_segment_manager(struct f2fs_sb_info *sbi)
2790 {
2791 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
2792 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2793 struct f2fs_sm_info *sm_info;
2794 int err;
2795
2796 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
2797 if (!sm_info)
2798 return -ENOMEM;
2799
2800 /* init sm info */
2801 sbi->sm_info = sm_info;
2802 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
2803 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
2804 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
2805 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
2806 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
2807 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
2808 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
2809 sm_info->rec_prefree_segments = sm_info->main_segments *
2810 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
2811 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
2812 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
2813
2814 if (!test_opt(sbi, LFS))
2815 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
2816 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
2817 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
2818
2819 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
2820
2821 INIT_LIST_HEAD(&sm_info->sit_entry_set);
2822
2823 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
2824 err = create_flush_cmd_control(sbi);
2825 if (err)
2826 return err;
2827 }
2828
2829 err = create_discard_cmd_control(sbi);
2830 if (err)
2831 return err;
2832
2833 err = build_sit_info(sbi);
2834 if (err)
2835 return err;
2836 err = build_free_segmap(sbi);
2837 if (err)
2838 return err;
2839 err = build_curseg(sbi);
2840 if (err)
2841 return err;
2842
2843 /* reinit free segmap based on SIT */
2844 build_sit_entries(sbi);
2845
2846 init_free_segmap(sbi);
2847 err = build_dirty_segmap(sbi);
2848 if (err)
2849 return err;
2850
2851 init_min_max_mtime(sbi);
2852 return 0;
2853 }
2854
2855 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
2856 enum dirty_type dirty_type)
2857 {
2858 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2859
2860 mutex_lock(&dirty_i->seglist_lock);
2861 kvfree(dirty_i->dirty_segmap[dirty_type]);
2862 dirty_i->nr_dirty[dirty_type] = 0;
2863 mutex_unlock(&dirty_i->seglist_lock);
2864 }
2865
2866 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
2867 {
2868 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2869 kvfree(dirty_i->victim_secmap);
2870 }
2871
2872 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
2873 {
2874 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2875 int i;
2876
2877 if (!dirty_i)
2878 return;
2879
2880 /* discard pre-free/dirty segments list */
2881 for (i = 0; i < NR_DIRTY_TYPE; i++)
2882 discard_dirty_segmap(sbi, i);
2883
2884 destroy_victim_secmap(sbi);
2885 SM_I(sbi)->dirty_info = NULL;
2886 kfree(dirty_i);
2887 }
2888
2889 static void destroy_curseg(struct f2fs_sb_info *sbi)
2890 {
2891 struct curseg_info *array = SM_I(sbi)->curseg_array;
2892 int i;
2893
2894 if (!array)
2895 return;
2896 SM_I(sbi)->curseg_array = NULL;
2897 for (i = 0; i < NR_CURSEG_TYPE; i++) {
2898 kfree(array[i].sum_blk);
2899 kfree(array[i].journal);
2900 }
2901 kfree(array);
2902 }
2903
2904 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
2905 {
2906 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
2907 if (!free_i)
2908 return;
2909 SM_I(sbi)->free_info = NULL;
2910 kvfree(free_i->free_segmap);
2911 kvfree(free_i->free_secmap);
2912 kfree(free_i);
2913 }
2914
2915 static void destroy_sit_info(struct f2fs_sb_info *sbi)
2916 {
2917 struct sit_info *sit_i = SIT_I(sbi);
2918 unsigned int start;
2919
2920 if (!sit_i)
2921 return;
2922
2923 if (sit_i->sentries) {
2924 for (start = 0; start < MAIN_SEGS(sbi); start++) {
2925 kfree(sit_i->sentries[start].cur_valid_map);
2926 #ifdef CONFIG_F2FS_CHECK_FS
2927 kfree(sit_i->sentries[start].cur_valid_map_mir);
2928 #endif
2929 kfree(sit_i->sentries[start].ckpt_valid_map);
2930 kfree(sit_i->sentries[start].discard_map);
2931 }
2932 }
2933 kfree(sit_i->tmp_map);
2934
2935 kvfree(sit_i->sentries);
2936 kvfree(sit_i->sec_entries);
2937 kvfree(sit_i->dirty_sentries_bitmap);
2938
2939 SM_I(sbi)->sit_info = NULL;
2940 kfree(sit_i->sit_bitmap);
2941 #ifdef CONFIG_F2FS_CHECK_FS
2942 kfree(sit_i->sit_bitmap_mir);
2943 #endif
2944 kfree(sit_i);
2945 }
2946
2947 void destroy_segment_manager(struct f2fs_sb_info *sbi)
2948 {
2949 struct f2fs_sm_info *sm_info = SM_I(sbi);
2950
2951 if (!sm_info)
2952 return;
2953 destroy_flush_cmd_control(sbi, true);
2954 destroy_discard_cmd_control(sbi, true);
2955 destroy_dirty_segmap(sbi);
2956 destroy_curseg(sbi);
2957 destroy_free_segmap(sbi);
2958 destroy_sit_info(sbi);
2959 sbi->sm_info = NULL;
2960 kfree(sm_info);
2961 }
2962
2963 int __init create_segment_manager_caches(void)
2964 {
2965 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
2966 sizeof(struct discard_entry));
2967 if (!discard_entry_slab)
2968 goto fail;
2969
2970 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
2971 sizeof(struct discard_cmd));
2972 if (!discard_cmd_slab)
2973 goto destroy_discard_entry;
2974
2975 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
2976 sizeof(struct sit_entry_set));
2977 if (!sit_entry_set_slab)
2978 goto destroy_discard_cmd;
2979
2980 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
2981 sizeof(struct inmem_pages));
2982 if (!inmem_entry_slab)
2983 goto destroy_sit_entry_set;
2984 return 0;
2985
2986 destroy_sit_entry_set:
2987 kmem_cache_destroy(sit_entry_set_slab);
2988 destroy_discard_cmd:
2989 kmem_cache_destroy(discard_cmd_slab);
2990 destroy_discard_entry:
2991 kmem_cache_destroy(discard_entry_slab);
2992 fail:
2993 return -ENOMEM;
2994 }
2995
2996 void destroy_segment_manager_caches(void)
2997 {
2998 kmem_cache_destroy(sit_entry_set_slab);
2999 kmem_cache_destroy(discard_cmd_slab);
3000 kmem_cache_destroy(discard_entry_slab);
3001 kmem_cache_destroy(inmem_entry_slab);
3002 }