ima: don't allocate a copy of template_fmt in template_desc_init_fields()
[linux/fpc-iii.git] / fs / f2fs / segment.c
blobd04613df710a7e7b54db01e44ab40c3fabc460e7
1 /*
2 * fs/f2fs/segment.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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/vmalloc.h>
18 #include <linux/swap.h>
20 #include "f2fs.h"
21 #include "segment.h"
22 #include "node.h"
23 #include <trace/events/f2fs.h>
25 #define __reverse_ffz(x) __reverse_ffs(~(x))
27 static struct kmem_cache *discard_entry_slab;
30 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
31 * MSB and LSB are reversed in a byte by f2fs_set_bit.
33 static inline unsigned long __reverse_ffs(unsigned long word)
35 int num = 0;
37 #if BITS_PER_LONG == 64
38 if ((word & 0xffffffff) == 0) {
39 num += 32;
40 word >>= 32;
42 #endif
43 if ((word & 0xffff) == 0) {
44 num += 16;
45 word >>= 16;
47 if ((word & 0xff) == 0) {
48 num += 8;
49 word >>= 8;
51 if ((word & 0xf0) == 0)
52 num += 4;
53 else
54 word >>= 4;
55 if ((word & 0xc) == 0)
56 num += 2;
57 else
58 word >>= 2;
59 if ((word & 0x2) == 0)
60 num += 1;
61 return num;
65 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c becasue
66 * f2fs_set_bit makes MSB and LSB reversed in a byte.
67 * Example:
68 * LSB <--> MSB
69 * f2fs_set_bit(0, bitmap) => 0000 0001
70 * f2fs_set_bit(7, bitmap) => 1000 0000
72 static unsigned long __find_rev_next_bit(const unsigned long *addr,
73 unsigned long size, unsigned long offset)
75 const unsigned long *p = addr + BIT_WORD(offset);
76 unsigned long result = offset & ~(BITS_PER_LONG - 1);
77 unsigned long tmp;
78 unsigned long mask, submask;
79 unsigned long quot, rest;
81 if (offset >= size)
82 return size;
84 size -= result;
85 offset %= BITS_PER_LONG;
86 if (!offset)
87 goto aligned;
89 tmp = *(p++);
90 quot = (offset >> 3) << 3;
91 rest = offset & 0x7;
92 mask = ~0UL << quot;
93 submask = (unsigned char)(0xff << rest) >> rest;
94 submask <<= quot;
95 mask &= submask;
96 tmp &= mask;
97 if (size < BITS_PER_LONG)
98 goto found_first;
99 if (tmp)
100 goto found_middle;
102 size -= BITS_PER_LONG;
103 result += BITS_PER_LONG;
104 aligned:
105 while (size & ~(BITS_PER_LONG-1)) {
106 tmp = *(p++);
107 if (tmp)
108 goto found_middle;
109 result += BITS_PER_LONG;
110 size -= BITS_PER_LONG;
112 if (!size)
113 return result;
114 tmp = *p;
115 found_first:
116 tmp &= (~0UL >> (BITS_PER_LONG - size));
117 if (tmp == 0UL) /* Are any bits set? */
118 return result + size; /* Nope. */
119 found_middle:
120 return result + __reverse_ffs(tmp);
123 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
124 unsigned long size, unsigned long offset)
126 const unsigned long *p = addr + BIT_WORD(offset);
127 unsigned long result = offset & ~(BITS_PER_LONG - 1);
128 unsigned long tmp;
129 unsigned long mask, submask;
130 unsigned long quot, rest;
132 if (offset >= size)
133 return size;
135 size -= result;
136 offset %= BITS_PER_LONG;
137 if (!offset)
138 goto aligned;
140 tmp = *(p++);
141 quot = (offset >> 3) << 3;
142 rest = offset & 0x7;
143 mask = ~(~0UL << quot);
144 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest);
145 submask <<= quot;
146 mask += submask;
147 tmp |= mask;
148 if (size < BITS_PER_LONG)
149 goto found_first;
150 if (~tmp)
151 goto found_middle;
153 size -= BITS_PER_LONG;
154 result += BITS_PER_LONG;
155 aligned:
156 while (size & ~(BITS_PER_LONG - 1)) {
157 tmp = *(p++);
158 if (~tmp)
159 goto found_middle;
160 result += BITS_PER_LONG;
161 size -= BITS_PER_LONG;
163 if (!size)
164 return result;
165 tmp = *p;
167 found_first:
168 tmp |= ~0UL << size;
169 if (tmp == ~0UL) /* Are any bits zero? */
170 return result + size; /* Nope. */
171 found_middle:
172 return result + __reverse_ffz(tmp);
176 * This function balances dirty node and dentry pages.
177 * In addition, it controls garbage collection.
179 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
182 * We should do GC or end up with checkpoint, if there are so many dirty
183 * dir/node pages without enough free segments.
185 if (has_not_enough_free_secs(sbi, 0)) {
186 mutex_lock(&sbi->gc_mutex);
187 f2fs_gc(sbi);
191 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
193 /* check the # of cached NAT entries and prefree segments */
194 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||
195 excess_prefree_segs(sbi))
196 f2fs_sync_fs(sbi->sb, true);
199 static int issue_flush_thread(void *data)
201 struct f2fs_sb_info *sbi = data;
202 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
203 wait_queue_head_t *q = &fcc->flush_wait_queue;
204 repeat:
205 if (kthread_should_stop())
206 return 0;
208 spin_lock(&fcc->issue_lock);
209 if (fcc->issue_list) {
210 fcc->dispatch_list = fcc->issue_list;
211 fcc->issue_list = fcc->issue_tail = NULL;
213 spin_unlock(&fcc->issue_lock);
215 if (fcc->dispatch_list) {
216 struct bio *bio = bio_alloc(GFP_NOIO, 0);
217 struct flush_cmd *cmd, *next;
218 int ret;
220 bio->bi_bdev = sbi->sb->s_bdev;
221 ret = submit_bio_wait(WRITE_FLUSH, bio);
223 for (cmd = fcc->dispatch_list; cmd; cmd = next) {
224 cmd->ret = ret;
225 next = cmd->next;
226 complete(&cmd->wait);
228 bio_put(bio);
229 fcc->dispatch_list = NULL;
232 wait_event_interruptible(*q,
233 kthread_should_stop() || fcc->issue_list);
234 goto repeat;
237 int f2fs_issue_flush(struct f2fs_sb_info *sbi)
239 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
240 struct flush_cmd cmd;
242 if (!test_opt(sbi, FLUSH_MERGE))
243 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL);
245 init_completion(&cmd.wait);
246 cmd.next = NULL;
248 spin_lock(&fcc->issue_lock);
249 if (fcc->issue_list)
250 fcc->issue_tail->next = &cmd;
251 else
252 fcc->issue_list = &cmd;
253 fcc->issue_tail = &cmd;
254 spin_unlock(&fcc->issue_lock);
256 if (!fcc->dispatch_list)
257 wake_up(&fcc->flush_wait_queue);
259 wait_for_completion(&cmd.wait);
261 return cmd.ret;
264 int create_flush_cmd_control(struct f2fs_sb_info *sbi)
266 dev_t dev = sbi->sb->s_bdev->bd_dev;
267 struct flush_cmd_control *fcc;
268 int err = 0;
270 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
271 if (!fcc)
272 return -ENOMEM;
273 spin_lock_init(&fcc->issue_lock);
274 init_waitqueue_head(&fcc->flush_wait_queue);
275 sbi->sm_info->cmd_control_info = fcc;
276 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
277 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
278 if (IS_ERR(fcc->f2fs_issue_flush)) {
279 err = PTR_ERR(fcc->f2fs_issue_flush);
280 kfree(fcc);
281 sbi->sm_info->cmd_control_info = NULL;
282 return err;
285 return err;
288 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
290 struct flush_cmd_control *fcc =
291 sbi->sm_info->cmd_control_info;
293 if (fcc && fcc->f2fs_issue_flush)
294 kthread_stop(fcc->f2fs_issue_flush);
295 kfree(fcc);
296 sbi->sm_info->cmd_control_info = NULL;
299 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
300 enum dirty_type dirty_type)
302 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
304 /* need not be added */
305 if (IS_CURSEG(sbi, segno))
306 return;
308 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
309 dirty_i->nr_dirty[dirty_type]++;
311 if (dirty_type == DIRTY) {
312 struct seg_entry *sentry = get_seg_entry(sbi, segno);
313 enum dirty_type t = sentry->type;
315 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
316 dirty_i->nr_dirty[t]++;
320 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
321 enum dirty_type dirty_type)
323 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
325 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
326 dirty_i->nr_dirty[dirty_type]--;
328 if (dirty_type == DIRTY) {
329 struct seg_entry *sentry = get_seg_entry(sbi, segno);
330 enum dirty_type t = sentry->type;
332 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
333 dirty_i->nr_dirty[t]--;
335 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
336 clear_bit(GET_SECNO(sbi, segno),
337 dirty_i->victim_secmap);
342 * Should not occur error such as -ENOMEM.
343 * Adding dirty entry into seglist is not critical operation.
344 * If a given segment is one of current working segments, it won't be added.
346 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
348 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
349 unsigned short valid_blocks;
351 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
352 return;
354 mutex_lock(&dirty_i->seglist_lock);
356 valid_blocks = get_valid_blocks(sbi, segno, 0);
358 if (valid_blocks == 0) {
359 __locate_dirty_segment(sbi, segno, PRE);
360 __remove_dirty_segment(sbi, segno, DIRTY);
361 } else if (valid_blocks < sbi->blocks_per_seg) {
362 __locate_dirty_segment(sbi, segno, DIRTY);
363 } else {
364 /* Recovery routine with SSR needs this */
365 __remove_dirty_segment(sbi, segno, DIRTY);
368 mutex_unlock(&dirty_i->seglist_lock);
371 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
372 block_t blkstart, block_t blklen)
374 sector_t start = SECTOR_FROM_BLOCK(sbi, blkstart);
375 sector_t len = SECTOR_FROM_BLOCK(sbi, blklen);
376 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
377 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0);
380 void discard_next_dnode(struct f2fs_sb_info *sbi)
382 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
383 block_t blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
385 if (f2fs_issue_discard(sbi, blkaddr, 1)) {
386 struct page *page = grab_meta_page(sbi, blkaddr);
387 /* zero-filled page */
388 set_page_dirty(page);
389 f2fs_put_page(page, 1);
393 static void add_discard_addrs(struct f2fs_sb_info *sbi,
394 unsigned int segno, struct seg_entry *se)
396 struct list_head *head = &SM_I(sbi)->discard_list;
397 struct discard_entry *new;
398 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
399 int max_blocks = sbi->blocks_per_seg;
400 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
401 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
402 unsigned long dmap[entries];
403 unsigned int start = 0, end = -1;
404 int i;
406 if (!test_opt(sbi, DISCARD))
407 return;
409 /* zero block will be discarded through the prefree list */
410 if (!se->valid_blocks || se->valid_blocks == max_blocks)
411 return;
413 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
414 for (i = 0; i < entries; i++)
415 dmap[i] = (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
417 while (SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
418 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
419 if (start >= max_blocks)
420 break;
422 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
424 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
425 INIT_LIST_HEAD(&new->list);
426 new->blkaddr = START_BLOCK(sbi, segno) + start;
427 new->len = end - start;
429 list_add_tail(&new->list, head);
430 SM_I(sbi)->nr_discards += end - start;
435 * Should call clear_prefree_segments after checkpoint is done.
437 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
439 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
440 unsigned int segno = -1;
441 unsigned int total_segs = TOTAL_SEGS(sbi);
443 mutex_lock(&dirty_i->seglist_lock);
444 while (1) {
445 segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
446 segno + 1);
447 if (segno >= total_segs)
448 break;
449 __set_test_and_free(sbi, segno);
451 mutex_unlock(&dirty_i->seglist_lock);
454 void clear_prefree_segments(struct f2fs_sb_info *sbi)
456 struct list_head *head = &(SM_I(sbi)->discard_list);
457 struct discard_entry *entry, *this;
458 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
459 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
460 unsigned int total_segs = TOTAL_SEGS(sbi);
461 unsigned int start = 0, end = -1;
463 mutex_lock(&dirty_i->seglist_lock);
465 while (1) {
466 int i;
467 start = find_next_bit(prefree_map, total_segs, end + 1);
468 if (start >= total_segs)
469 break;
470 end = find_next_zero_bit(prefree_map, total_segs, start + 1);
472 for (i = start; i < end; i++)
473 clear_bit(i, prefree_map);
475 dirty_i->nr_dirty[PRE] -= end - start;
477 if (!test_opt(sbi, DISCARD))
478 continue;
480 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
481 (end - start) << sbi->log_blocks_per_seg);
483 mutex_unlock(&dirty_i->seglist_lock);
485 /* send small discards */
486 list_for_each_entry_safe(entry, this, head, list) {
487 f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
488 list_del(&entry->list);
489 SM_I(sbi)->nr_discards -= entry->len;
490 kmem_cache_free(discard_entry_slab, entry);
494 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
496 struct sit_info *sit_i = SIT_I(sbi);
497 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
498 sit_i->dirty_sentries++;
501 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
502 unsigned int segno, int modified)
504 struct seg_entry *se = get_seg_entry(sbi, segno);
505 se->type = type;
506 if (modified)
507 __mark_sit_entry_dirty(sbi, segno);
510 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
512 struct seg_entry *se;
513 unsigned int segno, offset;
514 long int new_vblocks;
516 segno = GET_SEGNO(sbi, blkaddr);
518 se = get_seg_entry(sbi, segno);
519 new_vblocks = se->valid_blocks + del;
520 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
522 f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
523 (new_vblocks > sbi->blocks_per_seg)));
525 se->valid_blocks = new_vblocks;
526 se->mtime = get_mtime(sbi);
527 SIT_I(sbi)->max_mtime = se->mtime;
529 /* Update valid block bitmap */
530 if (del > 0) {
531 if (f2fs_set_bit(offset, se->cur_valid_map))
532 BUG();
533 } else {
534 if (!f2fs_clear_bit(offset, se->cur_valid_map))
535 BUG();
537 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
538 se->ckpt_valid_blocks += del;
540 __mark_sit_entry_dirty(sbi, segno);
542 /* update total number of valid blocks to be written in ckpt area */
543 SIT_I(sbi)->written_valid_blocks += del;
545 if (sbi->segs_per_sec > 1)
546 get_sec_entry(sbi, segno)->valid_blocks += del;
549 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
551 update_sit_entry(sbi, new, 1);
552 if (GET_SEGNO(sbi, old) != NULL_SEGNO)
553 update_sit_entry(sbi, old, -1);
555 locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
556 locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
559 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
561 unsigned int segno = GET_SEGNO(sbi, addr);
562 struct sit_info *sit_i = SIT_I(sbi);
564 f2fs_bug_on(addr == NULL_ADDR);
565 if (addr == NEW_ADDR)
566 return;
568 /* add it into sit main buffer */
569 mutex_lock(&sit_i->sentry_lock);
571 update_sit_entry(sbi, addr, -1);
573 /* add it into dirty seglist */
574 locate_dirty_segment(sbi, segno);
576 mutex_unlock(&sit_i->sentry_lock);
580 * This function should be resided under the curseg_mutex lock
582 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
583 struct f2fs_summary *sum)
585 struct curseg_info *curseg = CURSEG_I(sbi, type);
586 void *addr = curseg->sum_blk;
587 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
588 memcpy(addr, sum, sizeof(struct f2fs_summary));
592 * Calculate the number of current summary pages for writing
594 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
596 int valid_sum_count = 0;
597 int i, sum_in_page;
599 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
600 if (sbi->ckpt->alloc_type[i] == SSR)
601 valid_sum_count += sbi->blocks_per_seg;
602 else
603 valid_sum_count += curseg_blkoff(sbi, i);
606 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
607 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
608 if (valid_sum_count <= sum_in_page)
609 return 1;
610 else if ((valid_sum_count - sum_in_page) <=
611 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
612 return 2;
613 return 3;
617 * Caller should put this summary page
619 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
621 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
624 static void write_sum_page(struct f2fs_sb_info *sbi,
625 struct f2fs_summary_block *sum_blk, block_t blk_addr)
627 struct page *page = grab_meta_page(sbi, blk_addr);
628 void *kaddr = page_address(page);
629 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
630 set_page_dirty(page);
631 f2fs_put_page(page, 1);
634 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
636 struct curseg_info *curseg = CURSEG_I(sbi, type);
637 unsigned int segno = curseg->segno + 1;
638 struct free_segmap_info *free_i = FREE_I(sbi);
640 if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
641 return !test_bit(segno, free_i->free_segmap);
642 return 0;
646 * Find a new segment from the free segments bitmap to right order
647 * This function should be returned with success, otherwise BUG
649 static void get_new_segment(struct f2fs_sb_info *sbi,
650 unsigned int *newseg, bool new_sec, int dir)
652 struct free_segmap_info *free_i = FREE_I(sbi);
653 unsigned int segno, secno, zoneno;
654 unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
655 unsigned int hint = *newseg / sbi->segs_per_sec;
656 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
657 unsigned int left_start = hint;
658 bool init = true;
659 int go_left = 0;
660 int i;
662 write_lock(&free_i->segmap_lock);
664 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
665 segno = find_next_zero_bit(free_i->free_segmap,
666 TOTAL_SEGS(sbi), *newseg + 1);
667 if (segno - *newseg < sbi->segs_per_sec -
668 (*newseg % sbi->segs_per_sec))
669 goto got_it;
671 find_other_zone:
672 secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
673 if (secno >= TOTAL_SECS(sbi)) {
674 if (dir == ALLOC_RIGHT) {
675 secno = find_next_zero_bit(free_i->free_secmap,
676 TOTAL_SECS(sbi), 0);
677 f2fs_bug_on(secno >= TOTAL_SECS(sbi));
678 } else {
679 go_left = 1;
680 left_start = hint - 1;
683 if (go_left == 0)
684 goto skip_left;
686 while (test_bit(left_start, free_i->free_secmap)) {
687 if (left_start > 0) {
688 left_start--;
689 continue;
691 left_start = find_next_zero_bit(free_i->free_secmap,
692 TOTAL_SECS(sbi), 0);
693 f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
694 break;
696 secno = left_start;
697 skip_left:
698 hint = secno;
699 segno = secno * sbi->segs_per_sec;
700 zoneno = secno / sbi->secs_per_zone;
702 /* give up on finding another zone */
703 if (!init)
704 goto got_it;
705 if (sbi->secs_per_zone == 1)
706 goto got_it;
707 if (zoneno == old_zoneno)
708 goto got_it;
709 if (dir == ALLOC_LEFT) {
710 if (!go_left && zoneno + 1 >= total_zones)
711 goto got_it;
712 if (go_left && zoneno == 0)
713 goto got_it;
715 for (i = 0; i < NR_CURSEG_TYPE; i++)
716 if (CURSEG_I(sbi, i)->zone == zoneno)
717 break;
719 if (i < NR_CURSEG_TYPE) {
720 /* zone is in user, try another */
721 if (go_left)
722 hint = zoneno * sbi->secs_per_zone - 1;
723 else if (zoneno + 1 >= total_zones)
724 hint = 0;
725 else
726 hint = (zoneno + 1) * sbi->secs_per_zone;
727 init = false;
728 goto find_other_zone;
730 got_it:
731 /* set it as dirty segment in free segmap */
732 f2fs_bug_on(test_bit(segno, free_i->free_segmap));
733 __set_inuse(sbi, segno);
734 *newseg = segno;
735 write_unlock(&free_i->segmap_lock);
738 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
740 struct curseg_info *curseg = CURSEG_I(sbi, type);
741 struct summary_footer *sum_footer;
743 curseg->segno = curseg->next_segno;
744 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
745 curseg->next_blkoff = 0;
746 curseg->next_segno = NULL_SEGNO;
748 sum_footer = &(curseg->sum_blk->footer);
749 memset(sum_footer, 0, sizeof(struct summary_footer));
750 if (IS_DATASEG(type))
751 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
752 if (IS_NODESEG(type))
753 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
754 __set_sit_entry_type(sbi, type, curseg->segno, modified);
758 * Allocate a current working segment.
759 * This function always allocates a free segment in LFS manner.
761 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
763 struct curseg_info *curseg = CURSEG_I(sbi, type);
764 unsigned int segno = curseg->segno;
765 int dir = ALLOC_LEFT;
767 write_sum_page(sbi, curseg->sum_blk,
768 GET_SUM_BLOCK(sbi, segno));
769 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
770 dir = ALLOC_RIGHT;
772 if (test_opt(sbi, NOHEAP))
773 dir = ALLOC_RIGHT;
775 get_new_segment(sbi, &segno, new_sec, dir);
776 curseg->next_segno = segno;
777 reset_curseg(sbi, type, 1);
778 curseg->alloc_type = LFS;
781 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
782 struct curseg_info *seg, block_t start)
784 struct seg_entry *se = get_seg_entry(sbi, seg->segno);
785 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
786 unsigned long target_map[entries];
787 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
788 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
789 int i, pos;
791 for (i = 0; i < entries; i++)
792 target_map[i] = ckpt_map[i] | cur_map[i];
794 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
796 seg->next_blkoff = pos;
800 * If a segment is written by LFS manner, next block offset is just obtained
801 * by increasing the current block offset. However, if a segment is written by
802 * SSR manner, next block offset obtained by calling __next_free_blkoff
804 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
805 struct curseg_info *seg)
807 if (seg->alloc_type == SSR)
808 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
809 else
810 seg->next_blkoff++;
814 * This function always allocates a used segment (from dirty seglist) by SSR
815 * manner, so it should recover the existing segment information of valid blocks
817 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
819 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
820 struct curseg_info *curseg = CURSEG_I(sbi, type);
821 unsigned int new_segno = curseg->next_segno;
822 struct f2fs_summary_block *sum_node;
823 struct page *sum_page;
825 write_sum_page(sbi, curseg->sum_blk,
826 GET_SUM_BLOCK(sbi, curseg->segno));
827 __set_test_and_inuse(sbi, new_segno);
829 mutex_lock(&dirty_i->seglist_lock);
830 __remove_dirty_segment(sbi, new_segno, PRE);
831 __remove_dirty_segment(sbi, new_segno, DIRTY);
832 mutex_unlock(&dirty_i->seglist_lock);
834 reset_curseg(sbi, type, 1);
835 curseg->alloc_type = SSR;
836 __next_free_blkoff(sbi, curseg, 0);
838 if (reuse) {
839 sum_page = get_sum_page(sbi, new_segno);
840 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
841 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
842 f2fs_put_page(sum_page, 1);
846 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
848 struct curseg_info *curseg = CURSEG_I(sbi, type);
849 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
851 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
852 return v_ops->get_victim(sbi,
853 &(curseg)->next_segno, BG_GC, type, SSR);
855 /* For data segments, let's do SSR more intensively */
856 for (; type >= CURSEG_HOT_DATA; type--)
857 if (v_ops->get_victim(sbi, &(curseg)->next_segno,
858 BG_GC, type, SSR))
859 return 1;
860 return 0;
864 * flush out current segment and replace it with new segment
865 * This function should be returned with success, otherwise BUG
867 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
868 int type, bool force)
870 struct curseg_info *curseg = CURSEG_I(sbi, type);
872 if (force)
873 new_curseg(sbi, type, true);
874 else if (type == CURSEG_WARM_NODE)
875 new_curseg(sbi, type, false);
876 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
877 new_curseg(sbi, type, false);
878 else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
879 change_curseg(sbi, type, true);
880 else
881 new_curseg(sbi, type, false);
883 stat_inc_seg_type(sbi, curseg);
886 void allocate_new_segments(struct f2fs_sb_info *sbi)
888 struct curseg_info *curseg;
889 unsigned int old_curseg;
890 int i;
892 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
893 curseg = CURSEG_I(sbi, i);
894 old_curseg = curseg->segno;
895 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
896 locate_dirty_segment(sbi, old_curseg);
900 static const struct segment_allocation default_salloc_ops = {
901 .allocate_segment = allocate_segment_by_default,
904 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
906 struct curseg_info *curseg = CURSEG_I(sbi, type);
907 if (curseg->next_blkoff < sbi->blocks_per_seg)
908 return true;
909 return false;
912 static int __get_segment_type_2(struct page *page, enum page_type p_type)
914 if (p_type == DATA)
915 return CURSEG_HOT_DATA;
916 else
917 return CURSEG_HOT_NODE;
920 static int __get_segment_type_4(struct page *page, enum page_type p_type)
922 if (p_type == DATA) {
923 struct inode *inode = page->mapping->host;
925 if (S_ISDIR(inode->i_mode))
926 return CURSEG_HOT_DATA;
927 else
928 return CURSEG_COLD_DATA;
929 } else {
930 if (IS_DNODE(page) && !is_cold_node(page))
931 return CURSEG_HOT_NODE;
932 else
933 return CURSEG_COLD_NODE;
937 static int __get_segment_type_6(struct page *page, enum page_type p_type)
939 if (p_type == DATA) {
940 struct inode *inode = page->mapping->host;
942 if (S_ISDIR(inode->i_mode))
943 return CURSEG_HOT_DATA;
944 else if (is_cold_data(page) || file_is_cold(inode))
945 return CURSEG_COLD_DATA;
946 else
947 return CURSEG_WARM_DATA;
948 } else {
949 if (IS_DNODE(page))
950 return is_cold_node(page) ? CURSEG_WARM_NODE :
951 CURSEG_HOT_NODE;
952 else
953 return CURSEG_COLD_NODE;
957 static int __get_segment_type(struct page *page, enum page_type p_type)
959 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
960 switch (sbi->active_logs) {
961 case 2:
962 return __get_segment_type_2(page, p_type);
963 case 4:
964 return __get_segment_type_4(page, p_type);
966 /* NR_CURSEG_TYPE(6) logs by default */
967 f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
968 return __get_segment_type_6(page, p_type);
971 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
972 block_t old_blkaddr, block_t *new_blkaddr,
973 struct f2fs_summary *sum, int type)
975 struct sit_info *sit_i = SIT_I(sbi);
976 struct curseg_info *curseg;
977 unsigned int old_cursegno;
979 curseg = CURSEG_I(sbi, type);
981 mutex_lock(&curseg->curseg_mutex);
983 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
984 old_cursegno = curseg->segno;
987 * __add_sum_entry should be resided under the curseg_mutex
988 * because, this function updates a summary entry in the
989 * current summary block.
991 __add_sum_entry(sbi, type, sum);
993 mutex_lock(&sit_i->sentry_lock);
994 __refresh_next_blkoff(sbi, curseg);
996 stat_inc_block_count(sbi, curseg);
998 if (!__has_curseg_space(sbi, type))
999 sit_i->s_ops->allocate_segment(sbi, type, false);
1001 * SIT information should be updated before segment allocation,
1002 * since SSR needs latest valid block information.
1004 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
1005 locate_dirty_segment(sbi, old_cursegno);
1007 mutex_unlock(&sit_i->sentry_lock);
1009 if (page && IS_NODESEG(type))
1010 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
1012 mutex_unlock(&curseg->curseg_mutex);
1015 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
1016 block_t old_blkaddr, block_t *new_blkaddr,
1017 struct f2fs_summary *sum, struct f2fs_io_info *fio)
1019 int type = __get_segment_type(page, fio->type);
1021 allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
1023 /* writeout dirty page into bdev */
1024 f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
1027 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
1029 struct f2fs_io_info fio = {
1030 .type = META,
1031 .rw = WRITE_SYNC | REQ_META | REQ_PRIO
1034 set_page_writeback(page);
1035 f2fs_submit_page_mbio(sbi, page, page->index, &fio);
1038 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
1039 struct f2fs_io_info *fio,
1040 unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
1042 struct f2fs_summary sum;
1043 set_summary(&sum, nid, 0, 0);
1044 do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
1047 void write_data_page(struct page *page, struct dnode_of_data *dn,
1048 block_t *new_blkaddr, struct f2fs_io_info *fio)
1050 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
1051 struct f2fs_summary sum;
1052 struct node_info ni;
1054 f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
1055 get_node_info(sbi, dn->nid, &ni);
1056 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1058 do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
1061 void rewrite_data_page(struct page *page, block_t old_blkaddr,
1062 struct f2fs_io_info *fio)
1064 struct inode *inode = page->mapping->host;
1065 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1066 f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
1069 void recover_data_page(struct f2fs_sb_info *sbi,
1070 struct page *page, struct f2fs_summary *sum,
1071 block_t old_blkaddr, block_t new_blkaddr)
1073 struct sit_info *sit_i = SIT_I(sbi);
1074 struct curseg_info *curseg;
1075 unsigned int segno, old_cursegno;
1076 struct seg_entry *se;
1077 int type;
1079 segno = GET_SEGNO(sbi, new_blkaddr);
1080 se = get_seg_entry(sbi, segno);
1081 type = se->type;
1083 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
1084 if (old_blkaddr == NULL_ADDR)
1085 type = CURSEG_COLD_DATA;
1086 else
1087 type = CURSEG_WARM_DATA;
1089 curseg = CURSEG_I(sbi, type);
1091 mutex_lock(&curseg->curseg_mutex);
1092 mutex_lock(&sit_i->sentry_lock);
1094 old_cursegno = curseg->segno;
1096 /* change the current segment */
1097 if (segno != curseg->segno) {
1098 curseg->next_segno = segno;
1099 change_curseg(sbi, type, true);
1102 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1103 __add_sum_entry(sbi, type, sum);
1105 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1106 locate_dirty_segment(sbi, old_cursegno);
1108 mutex_unlock(&sit_i->sentry_lock);
1109 mutex_unlock(&curseg->curseg_mutex);
1112 void rewrite_node_page(struct f2fs_sb_info *sbi,
1113 struct page *page, struct f2fs_summary *sum,
1114 block_t old_blkaddr, block_t new_blkaddr)
1116 struct sit_info *sit_i = SIT_I(sbi);
1117 int type = CURSEG_WARM_NODE;
1118 struct curseg_info *curseg;
1119 unsigned int segno, old_cursegno;
1120 block_t next_blkaddr = next_blkaddr_of_node(page);
1121 unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
1122 struct f2fs_io_info fio = {
1123 .type = NODE,
1124 .rw = WRITE_SYNC,
1127 curseg = CURSEG_I(sbi, type);
1129 mutex_lock(&curseg->curseg_mutex);
1130 mutex_lock(&sit_i->sentry_lock);
1132 segno = GET_SEGNO(sbi, new_blkaddr);
1133 old_cursegno = curseg->segno;
1135 /* change the current segment */
1136 if (segno != curseg->segno) {
1137 curseg->next_segno = segno;
1138 change_curseg(sbi, type, true);
1140 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
1141 __add_sum_entry(sbi, type, sum);
1143 /* change the current log to the next block addr in advance */
1144 if (next_segno != segno) {
1145 curseg->next_segno = next_segno;
1146 change_curseg(sbi, type, true);
1148 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
1150 /* rewrite node page */
1151 set_page_writeback(page);
1152 f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
1153 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1154 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
1155 locate_dirty_segment(sbi, old_cursegno);
1157 mutex_unlock(&sit_i->sentry_lock);
1158 mutex_unlock(&curseg->curseg_mutex);
1161 static inline bool is_merged_page(struct f2fs_sb_info *sbi,
1162 struct page *page, enum page_type type)
1164 enum page_type btype = PAGE_TYPE_OF_BIO(type);
1165 struct f2fs_bio_info *io = &sbi->write_io[btype];
1166 struct bio_vec *bvec;
1167 int i;
1169 down_read(&io->io_rwsem);
1170 if (!io->bio)
1171 goto out;
1173 bio_for_each_segment_all(bvec, io->bio, i) {
1174 if (page == bvec->bv_page) {
1175 up_read(&io->io_rwsem);
1176 return true;
1180 out:
1181 up_read(&io->io_rwsem);
1182 return false;
1185 void f2fs_wait_on_page_writeback(struct page *page,
1186 enum page_type type)
1188 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
1189 if (PageWriteback(page)) {
1190 if (is_merged_page(sbi, page, type))
1191 f2fs_submit_merged_bio(sbi, type, WRITE);
1192 wait_on_page_writeback(page);
1196 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1198 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1199 struct curseg_info *seg_i;
1200 unsigned char *kaddr;
1201 struct page *page;
1202 block_t start;
1203 int i, j, offset;
1205 start = start_sum_block(sbi);
1207 page = get_meta_page(sbi, start++);
1208 kaddr = (unsigned char *)page_address(page);
1210 /* Step 1: restore nat cache */
1211 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1212 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1214 /* Step 2: restore sit cache */
1215 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1216 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1217 SUM_JOURNAL_SIZE);
1218 offset = 2 * SUM_JOURNAL_SIZE;
1220 /* Step 3: restore summary entries */
1221 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1222 unsigned short blk_off;
1223 unsigned int segno;
1225 seg_i = CURSEG_I(sbi, i);
1226 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1227 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1228 seg_i->next_segno = segno;
1229 reset_curseg(sbi, i, 0);
1230 seg_i->alloc_type = ckpt->alloc_type[i];
1231 seg_i->next_blkoff = blk_off;
1233 if (seg_i->alloc_type == SSR)
1234 blk_off = sbi->blocks_per_seg;
1236 for (j = 0; j < blk_off; j++) {
1237 struct f2fs_summary *s;
1238 s = (struct f2fs_summary *)(kaddr + offset);
1239 seg_i->sum_blk->entries[j] = *s;
1240 offset += SUMMARY_SIZE;
1241 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1242 SUM_FOOTER_SIZE)
1243 continue;
1245 f2fs_put_page(page, 1);
1246 page = NULL;
1248 page = get_meta_page(sbi, start++);
1249 kaddr = (unsigned char *)page_address(page);
1250 offset = 0;
1253 f2fs_put_page(page, 1);
1254 return 0;
1257 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1259 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1260 struct f2fs_summary_block *sum;
1261 struct curseg_info *curseg;
1262 struct page *new;
1263 unsigned short blk_off;
1264 unsigned int segno = 0;
1265 block_t blk_addr = 0;
1267 /* get segment number and block addr */
1268 if (IS_DATASEG(type)) {
1269 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1270 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1271 CURSEG_HOT_DATA]);
1272 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1273 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1274 else
1275 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1276 } else {
1277 segno = le32_to_cpu(ckpt->cur_node_segno[type -
1278 CURSEG_HOT_NODE]);
1279 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1280 CURSEG_HOT_NODE]);
1281 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1282 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1283 type - CURSEG_HOT_NODE);
1284 else
1285 blk_addr = GET_SUM_BLOCK(sbi, segno);
1288 new = get_meta_page(sbi, blk_addr);
1289 sum = (struct f2fs_summary_block *)page_address(new);
1291 if (IS_NODESEG(type)) {
1292 if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1293 struct f2fs_summary *ns = &sum->entries[0];
1294 int i;
1295 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1296 ns->version = 0;
1297 ns->ofs_in_node = 0;
1299 } else {
1300 int err;
1302 err = restore_node_summary(sbi, segno, sum);
1303 if (err) {
1304 f2fs_put_page(new, 1);
1305 return err;
1310 /* set uncompleted segment to curseg */
1311 curseg = CURSEG_I(sbi, type);
1312 mutex_lock(&curseg->curseg_mutex);
1313 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1314 curseg->next_segno = segno;
1315 reset_curseg(sbi, type, 0);
1316 curseg->alloc_type = ckpt->alloc_type[type];
1317 curseg->next_blkoff = blk_off;
1318 mutex_unlock(&curseg->curseg_mutex);
1319 f2fs_put_page(new, 1);
1320 return 0;
1323 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1325 int type = CURSEG_HOT_DATA;
1326 int err;
1328 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1329 /* restore for compacted data summary */
1330 if (read_compacted_summaries(sbi))
1331 return -EINVAL;
1332 type = CURSEG_HOT_NODE;
1335 for (; type <= CURSEG_COLD_NODE; type++) {
1336 err = read_normal_summaries(sbi, type);
1337 if (err)
1338 return err;
1341 return 0;
1344 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1346 struct page *page;
1347 unsigned char *kaddr;
1348 struct f2fs_summary *summary;
1349 struct curseg_info *seg_i;
1350 int written_size = 0;
1351 int i, j;
1353 page = grab_meta_page(sbi, blkaddr++);
1354 kaddr = (unsigned char *)page_address(page);
1356 /* Step 1: write nat cache */
1357 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1358 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1359 written_size += SUM_JOURNAL_SIZE;
1361 /* Step 2: write sit cache */
1362 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1363 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1364 SUM_JOURNAL_SIZE);
1365 written_size += SUM_JOURNAL_SIZE;
1367 /* Step 3: write summary entries */
1368 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1369 unsigned short blkoff;
1370 seg_i = CURSEG_I(sbi, i);
1371 if (sbi->ckpt->alloc_type[i] == SSR)
1372 blkoff = sbi->blocks_per_seg;
1373 else
1374 blkoff = curseg_blkoff(sbi, i);
1376 for (j = 0; j < blkoff; j++) {
1377 if (!page) {
1378 page = grab_meta_page(sbi, blkaddr++);
1379 kaddr = (unsigned char *)page_address(page);
1380 written_size = 0;
1382 summary = (struct f2fs_summary *)(kaddr + written_size);
1383 *summary = seg_i->sum_blk->entries[j];
1384 written_size += SUMMARY_SIZE;
1386 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1387 SUM_FOOTER_SIZE)
1388 continue;
1390 set_page_dirty(page);
1391 f2fs_put_page(page, 1);
1392 page = NULL;
1395 if (page) {
1396 set_page_dirty(page);
1397 f2fs_put_page(page, 1);
1401 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1402 block_t blkaddr, int type)
1404 int i, end;
1405 if (IS_DATASEG(type))
1406 end = type + NR_CURSEG_DATA_TYPE;
1407 else
1408 end = type + NR_CURSEG_NODE_TYPE;
1410 for (i = type; i < end; i++) {
1411 struct curseg_info *sum = CURSEG_I(sbi, i);
1412 mutex_lock(&sum->curseg_mutex);
1413 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1414 mutex_unlock(&sum->curseg_mutex);
1418 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1420 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1421 write_compacted_summaries(sbi, start_blk);
1422 else
1423 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1426 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1428 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1429 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1432 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1433 unsigned int val, int alloc)
1435 int i;
1437 if (type == NAT_JOURNAL) {
1438 for (i = 0; i < nats_in_cursum(sum); i++) {
1439 if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1440 return i;
1442 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1443 return update_nats_in_cursum(sum, 1);
1444 } else if (type == SIT_JOURNAL) {
1445 for (i = 0; i < sits_in_cursum(sum); i++)
1446 if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1447 return i;
1448 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1449 return update_sits_in_cursum(sum, 1);
1451 return -1;
1454 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1455 unsigned int segno)
1457 struct sit_info *sit_i = SIT_I(sbi);
1458 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1459 block_t blk_addr = sit_i->sit_base_addr + offset;
1461 check_seg_range(sbi, segno);
1463 /* calculate sit block address */
1464 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1465 blk_addr += sit_i->sit_blocks;
1467 return get_meta_page(sbi, blk_addr);
1470 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1471 unsigned int start)
1473 struct sit_info *sit_i = SIT_I(sbi);
1474 struct page *src_page, *dst_page;
1475 pgoff_t src_off, dst_off;
1476 void *src_addr, *dst_addr;
1478 src_off = current_sit_addr(sbi, start);
1479 dst_off = next_sit_addr(sbi, src_off);
1481 /* get current sit block page without lock */
1482 src_page = get_meta_page(sbi, src_off);
1483 dst_page = grab_meta_page(sbi, dst_off);
1484 f2fs_bug_on(PageDirty(src_page));
1486 src_addr = page_address(src_page);
1487 dst_addr = page_address(dst_page);
1488 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1490 set_page_dirty(dst_page);
1491 f2fs_put_page(src_page, 1);
1493 set_to_next_sit(sit_i, start);
1495 return dst_page;
1498 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1500 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1501 struct f2fs_summary_block *sum = curseg->sum_blk;
1502 int i;
1505 * If the journal area in the current summary is full of sit entries,
1506 * all the sit entries will be flushed. Otherwise the sit entries
1507 * are not able to replace with newly hot sit entries.
1509 if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1510 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1511 unsigned int segno;
1512 segno = le32_to_cpu(segno_in_journal(sum, i));
1513 __mark_sit_entry_dirty(sbi, segno);
1515 update_sits_in_cursum(sum, -sits_in_cursum(sum));
1516 return true;
1518 return false;
1522 * CP calls this function, which flushes SIT entries including sit_journal,
1523 * and moves prefree segs to free segs.
1525 void flush_sit_entries(struct f2fs_sb_info *sbi)
1527 struct sit_info *sit_i = SIT_I(sbi);
1528 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1529 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1530 struct f2fs_summary_block *sum = curseg->sum_blk;
1531 unsigned long nsegs = TOTAL_SEGS(sbi);
1532 struct page *page = NULL;
1533 struct f2fs_sit_block *raw_sit = NULL;
1534 unsigned int start = 0, end = 0;
1535 unsigned int segno = -1;
1536 bool flushed;
1538 mutex_lock(&curseg->curseg_mutex);
1539 mutex_lock(&sit_i->sentry_lock);
1542 * "flushed" indicates whether sit entries in journal are flushed
1543 * to the SIT area or not.
1545 flushed = flush_sits_in_journal(sbi);
1547 while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1548 struct seg_entry *se = get_seg_entry(sbi, segno);
1549 int sit_offset, offset;
1551 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1553 /* add discard candidates */
1554 if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
1555 add_discard_addrs(sbi, segno, se);
1557 if (flushed)
1558 goto to_sit_page;
1560 offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1561 if (offset >= 0) {
1562 segno_in_journal(sum, offset) = cpu_to_le32(segno);
1563 seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1564 goto flush_done;
1566 to_sit_page:
1567 if (!page || (start > segno) || (segno > end)) {
1568 if (page) {
1569 f2fs_put_page(page, 1);
1570 page = NULL;
1573 start = START_SEGNO(sit_i, segno);
1574 end = start + SIT_ENTRY_PER_BLOCK - 1;
1576 /* read sit block that will be updated */
1577 page = get_next_sit_page(sbi, start);
1578 raw_sit = page_address(page);
1581 /* udpate entry in SIT block */
1582 seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1583 flush_done:
1584 __clear_bit(segno, bitmap);
1585 sit_i->dirty_sentries--;
1587 mutex_unlock(&sit_i->sentry_lock);
1588 mutex_unlock(&curseg->curseg_mutex);
1590 /* writeout last modified SIT block */
1591 f2fs_put_page(page, 1);
1593 set_prefree_as_free_segments(sbi);
1596 static int build_sit_info(struct f2fs_sb_info *sbi)
1598 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1599 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1600 struct sit_info *sit_i;
1601 unsigned int sit_segs, start;
1602 char *src_bitmap, *dst_bitmap;
1603 unsigned int bitmap_size;
1605 /* allocate memory for SIT information */
1606 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1607 if (!sit_i)
1608 return -ENOMEM;
1610 SM_I(sbi)->sit_info = sit_i;
1612 sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1613 if (!sit_i->sentries)
1614 return -ENOMEM;
1616 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1617 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1618 if (!sit_i->dirty_sentries_bitmap)
1619 return -ENOMEM;
1621 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1622 sit_i->sentries[start].cur_valid_map
1623 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1624 sit_i->sentries[start].ckpt_valid_map
1625 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1626 if (!sit_i->sentries[start].cur_valid_map
1627 || !sit_i->sentries[start].ckpt_valid_map)
1628 return -ENOMEM;
1631 if (sbi->segs_per_sec > 1) {
1632 sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1633 sizeof(struct sec_entry));
1634 if (!sit_i->sec_entries)
1635 return -ENOMEM;
1638 /* get information related with SIT */
1639 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1641 /* setup SIT bitmap from ckeckpoint pack */
1642 bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1643 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1645 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1646 if (!dst_bitmap)
1647 return -ENOMEM;
1649 /* init SIT information */
1650 sit_i->s_ops = &default_salloc_ops;
1652 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1653 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1654 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1655 sit_i->sit_bitmap = dst_bitmap;
1656 sit_i->bitmap_size = bitmap_size;
1657 sit_i->dirty_sentries = 0;
1658 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1659 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1660 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1661 mutex_init(&sit_i->sentry_lock);
1662 return 0;
1665 static int build_free_segmap(struct f2fs_sb_info *sbi)
1667 struct f2fs_sm_info *sm_info = SM_I(sbi);
1668 struct free_segmap_info *free_i;
1669 unsigned int bitmap_size, sec_bitmap_size;
1671 /* allocate memory for free segmap information */
1672 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1673 if (!free_i)
1674 return -ENOMEM;
1676 SM_I(sbi)->free_info = free_i;
1678 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1679 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1680 if (!free_i->free_segmap)
1681 return -ENOMEM;
1683 sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1684 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1685 if (!free_i->free_secmap)
1686 return -ENOMEM;
1688 /* set all segments as dirty temporarily */
1689 memset(free_i->free_segmap, 0xff, bitmap_size);
1690 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1692 /* init free segmap information */
1693 free_i->start_segno =
1694 (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1695 free_i->free_segments = 0;
1696 free_i->free_sections = 0;
1697 rwlock_init(&free_i->segmap_lock);
1698 return 0;
1701 static int build_curseg(struct f2fs_sb_info *sbi)
1703 struct curseg_info *array;
1704 int i;
1706 array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1707 if (!array)
1708 return -ENOMEM;
1710 SM_I(sbi)->curseg_array = array;
1712 for (i = 0; i < NR_CURSEG_TYPE; i++) {
1713 mutex_init(&array[i].curseg_mutex);
1714 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1715 if (!array[i].sum_blk)
1716 return -ENOMEM;
1717 array[i].segno = NULL_SEGNO;
1718 array[i].next_blkoff = 0;
1720 return restore_curseg_summaries(sbi);
1723 static void build_sit_entries(struct f2fs_sb_info *sbi)
1725 struct sit_info *sit_i = SIT_I(sbi);
1726 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1727 struct f2fs_summary_block *sum = curseg->sum_blk;
1728 int sit_blk_cnt = SIT_BLK_CNT(sbi);
1729 unsigned int i, start, end;
1730 unsigned int readed, start_blk = 0;
1731 int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
1733 do {
1734 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
1736 start = start_blk * sit_i->sents_per_block;
1737 end = (start_blk + readed) * sit_i->sents_per_block;
1739 for (; start < end && start < TOTAL_SEGS(sbi); start++) {
1740 struct seg_entry *se = &sit_i->sentries[start];
1741 struct f2fs_sit_block *sit_blk;
1742 struct f2fs_sit_entry sit;
1743 struct page *page;
1745 mutex_lock(&curseg->curseg_mutex);
1746 for (i = 0; i < sits_in_cursum(sum); i++) {
1747 if (le32_to_cpu(segno_in_journal(sum, i))
1748 == start) {
1749 sit = sit_in_journal(sum, i);
1750 mutex_unlock(&curseg->curseg_mutex);
1751 goto got_it;
1754 mutex_unlock(&curseg->curseg_mutex);
1756 page = get_current_sit_page(sbi, start);
1757 sit_blk = (struct f2fs_sit_block *)page_address(page);
1758 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1759 f2fs_put_page(page, 1);
1760 got_it:
1761 check_block_count(sbi, start, &sit);
1762 seg_info_from_raw_sit(se, &sit);
1763 if (sbi->segs_per_sec > 1) {
1764 struct sec_entry *e = get_sec_entry(sbi, start);
1765 e->valid_blocks += se->valid_blocks;
1768 start_blk += readed;
1769 } while (start_blk < sit_blk_cnt);
1772 static void init_free_segmap(struct f2fs_sb_info *sbi)
1774 unsigned int start;
1775 int type;
1777 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1778 struct seg_entry *sentry = get_seg_entry(sbi, start);
1779 if (!sentry->valid_blocks)
1780 __set_free(sbi, start);
1783 /* set use the current segments */
1784 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1785 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1786 __set_test_and_inuse(sbi, curseg_t->segno);
1790 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1792 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1793 struct free_segmap_info *free_i = FREE_I(sbi);
1794 unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
1795 unsigned short valid_blocks;
1797 while (1) {
1798 /* find dirty segment based on free segmap */
1799 segno = find_next_inuse(free_i, total_segs, offset);
1800 if (segno >= total_segs)
1801 break;
1802 offset = segno + 1;
1803 valid_blocks = get_valid_blocks(sbi, segno, 0);
1804 if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1805 continue;
1806 mutex_lock(&dirty_i->seglist_lock);
1807 __locate_dirty_segment(sbi, segno, DIRTY);
1808 mutex_unlock(&dirty_i->seglist_lock);
1812 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1814 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1815 unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1817 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1818 if (!dirty_i->victim_secmap)
1819 return -ENOMEM;
1820 return 0;
1823 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1825 struct dirty_seglist_info *dirty_i;
1826 unsigned int bitmap_size, i;
1828 /* allocate memory for dirty segments list information */
1829 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1830 if (!dirty_i)
1831 return -ENOMEM;
1833 SM_I(sbi)->dirty_info = dirty_i;
1834 mutex_init(&dirty_i->seglist_lock);
1836 bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1838 for (i = 0; i < NR_DIRTY_TYPE; i++) {
1839 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1840 if (!dirty_i->dirty_segmap[i])
1841 return -ENOMEM;
1844 init_dirty_segmap(sbi);
1845 return init_victim_secmap(sbi);
1849 * Update min, max modified time for cost-benefit GC algorithm
1851 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1853 struct sit_info *sit_i = SIT_I(sbi);
1854 unsigned int segno;
1856 mutex_lock(&sit_i->sentry_lock);
1858 sit_i->min_mtime = LLONG_MAX;
1860 for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1861 unsigned int i;
1862 unsigned long long mtime = 0;
1864 for (i = 0; i < sbi->segs_per_sec; i++)
1865 mtime += get_seg_entry(sbi, segno + i)->mtime;
1867 mtime = div_u64(mtime, sbi->segs_per_sec);
1869 if (sit_i->min_mtime > mtime)
1870 sit_i->min_mtime = mtime;
1872 sit_i->max_mtime = get_mtime(sbi);
1873 mutex_unlock(&sit_i->sentry_lock);
1876 int build_segment_manager(struct f2fs_sb_info *sbi)
1878 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1879 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1880 struct f2fs_sm_info *sm_info;
1881 int err;
1883 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1884 if (!sm_info)
1885 return -ENOMEM;
1887 /* init sm info */
1888 sbi->sm_info = sm_info;
1889 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1890 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1891 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1892 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1893 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1894 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1895 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1896 sm_info->rec_prefree_segments = sm_info->main_segments *
1897 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
1898 sm_info->ipu_policy = F2FS_IPU_DISABLE;
1899 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
1901 INIT_LIST_HEAD(&sm_info->discard_list);
1902 sm_info->nr_discards = 0;
1903 sm_info->max_discards = 0;
1905 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
1906 err = create_flush_cmd_control(sbi);
1907 if (err)
1908 return err;
1911 err = build_sit_info(sbi);
1912 if (err)
1913 return err;
1914 err = build_free_segmap(sbi);
1915 if (err)
1916 return err;
1917 err = build_curseg(sbi);
1918 if (err)
1919 return err;
1921 /* reinit free segmap based on SIT */
1922 build_sit_entries(sbi);
1924 init_free_segmap(sbi);
1925 err = build_dirty_segmap(sbi);
1926 if (err)
1927 return err;
1929 init_min_max_mtime(sbi);
1930 return 0;
1933 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1934 enum dirty_type dirty_type)
1936 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1938 mutex_lock(&dirty_i->seglist_lock);
1939 kfree(dirty_i->dirty_segmap[dirty_type]);
1940 dirty_i->nr_dirty[dirty_type] = 0;
1941 mutex_unlock(&dirty_i->seglist_lock);
1944 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1946 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1947 kfree(dirty_i->victim_secmap);
1950 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1952 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1953 int i;
1955 if (!dirty_i)
1956 return;
1958 /* discard pre-free/dirty segments list */
1959 for (i = 0; i < NR_DIRTY_TYPE; i++)
1960 discard_dirty_segmap(sbi, i);
1962 destroy_victim_secmap(sbi);
1963 SM_I(sbi)->dirty_info = NULL;
1964 kfree(dirty_i);
1967 static void destroy_curseg(struct f2fs_sb_info *sbi)
1969 struct curseg_info *array = SM_I(sbi)->curseg_array;
1970 int i;
1972 if (!array)
1973 return;
1974 SM_I(sbi)->curseg_array = NULL;
1975 for (i = 0; i < NR_CURSEG_TYPE; i++)
1976 kfree(array[i].sum_blk);
1977 kfree(array);
1980 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1982 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1983 if (!free_i)
1984 return;
1985 SM_I(sbi)->free_info = NULL;
1986 kfree(free_i->free_segmap);
1987 kfree(free_i->free_secmap);
1988 kfree(free_i);
1991 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1993 struct sit_info *sit_i = SIT_I(sbi);
1994 unsigned int start;
1996 if (!sit_i)
1997 return;
1999 if (sit_i->sentries) {
2000 for (start = 0; start < TOTAL_SEGS(sbi); start++) {
2001 kfree(sit_i->sentries[start].cur_valid_map);
2002 kfree(sit_i->sentries[start].ckpt_valid_map);
2005 vfree(sit_i->sentries);
2006 vfree(sit_i->sec_entries);
2007 kfree(sit_i->dirty_sentries_bitmap);
2009 SM_I(sbi)->sit_info = NULL;
2010 kfree(sit_i->sit_bitmap);
2011 kfree(sit_i);
2014 void destroy_segment_manager(struct f2fs_sb_info *sbi)
2016 struct f2fs_sm_info *sm_info = SM_I(sbi);
2018 if (!sm_info)
2019 return;
2020 destroy_flush_cmd_control(sbi);
2021 destroy_dirty_segmap(sbi);
2022 destroy_curseg(sbi);
2023 destroy_free_segmap(sbi);
2024 destroy_sit_info(sbi);
2025 sbi->sm_info = NULL;
2026 kfree(sm_info);
2029 int __init create_segment_manager_caches(void)
2031 discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
2032 sizeof(struct discard_entry));
2033 if (!discard_entry_slab)
2034 return -ENOMEM;
2035 return 0;
2038 void destroy_segment_manager_caches(void)
2040 kmem_cache_destroy(discard_entry_slab);