xref: /openbmc/linux/fs/f2fs/segment.c (revision f35e839a)
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/vmalloc.h>
17 
18 #include "f2fs.h"
19 #include "segment.h"
20 #include "node.h"
21 #include <trace/events/f2fs.h>
22 
23 /*
24  * This function balances dirty node and dentry pages.
25  * In addition, it controls garbage collection.
26  */
27 void f2fs_balance_fs(struct f2fs_sb_info *sbi)
28 {
29 	/*
30 	 * We should do GC or end up with checkpoint, if there are so many dirty
31 	 * dir/node pages without enough free segments.
32 	 */
33 	if (has_not_enough_free_secs(sbi, 0)) {
34 		mutex_lock(&sbi->gc_mutex);
35 		f2fs_gc(sbi);
36 	}
37 }
38 
39 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
40 		enum dirty_type dirty_type)
41 {
42 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
43 
44 	/* need not be added */
45 	if (IS_CURSEG(sbi, segno))
46 		return;
47 
48 	if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
49 		dirty_i->nr_dirty[dirty_type]++;
50 
51 	if (dirty_type == DIRTY) {
52 		struct seg_entry *sentry = get_seg_entry(sbi, segno);
53 		enum dirty_type t = DIRTY_HOT_DATA;
54 
55 		dirty_type = sentry->type;
56 
57 		if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
58 			dirty_i->nr_dirty[dirty_type]++;
59 
60 		/* Only one bitmap should be set */
61 		for (; t <= DIRTY_COLD_NODE; t++) {
62 			if (t == dirty_type)
63 				continue;
64 			if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
65 				dirty_i->nr_dirty[t]--;
66 		}
67 	}
68 }
69 
70 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
71 		enum dirty_type dirty_type)
72 {
73 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
74 
75 	if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
76 		dirty_i->nr_dirty[dirty_type]--;
77 
78 	if (dirty_type == DIRTY) {
79 		enum dirty_type t = DIRTY_HOT_DATA;
80 
81 		/* clear all the bitmaps */
82 		for (; t <= DIRTY_COLD_NODE; t++)
83 			if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
84 				dirty_i->nr_dirty[t]--;
85 
86 		if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
87 			clear_bit(GET_SECNO(sbi, segno),
88 						dirty_i->victim_secmap);
89 	}
90 }
91 
92 /*
93  * Should not occur error such as -ENOMEM.
94  * Adding dirty entry into seglist is not critical operation.
95  * If a given segment is one of current working segments, it won't be added.
96  */
97 void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
98 {
99 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
100 	unsigned short valid_blocks;
101 
102 	if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
103 		return;
104 
105 	mutex_lock(&dirty_i->seglist_lock);
106 
107 	valid_blocks = get_valid_blocks(sbi, segno, 0);
108 
109 	if (valid_blocks == 0) {
110 		__locate_dirty_segment(sbi, segno, PRE);
111 		__remove_dirty_segment(sbi, segno, DIRTY);
112 	} else if (valid_blocks < sbi->blocks_per_seg) {
113 		__locate_dirty_segment(sbi, segno, DIRTY);
114 	} else {
115 		/* Recovery routine with SSR needs this */
116 		__remove_dirty_segment(sbi, segno, DIRTY);
117 	}
118 
119 	mutex_unlock(&dirty_i->seglist_lock);
120 	return;
121 }
122 
123 /*
124  * Should call clear_prefree_segments after checkpoint is done.
125  */
126 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
127 {
128 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
129 	unsigned int segno, offset = 0;
130 	unsigned int total_segs = TOTAL_SEGS(sbi);
131 
132 	mutex_lock(&dirty_i->seglist_lock);
133 	while (1) {
134 		segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
135 				offset);
136 		if (segno >= total_segs)
137 			break;
138 		__set_test_and_free(sbi, segno);
139 		offset = segno + 1;
140 	}
141 	mutex_unlock(&dirty_i->seglist_lock);
142 }
143 
144 void clear_prefree_segments(struct f2fs_sb_info *sbi)
145 {
146 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
147 	unsigned int segno, offset = 0;
148 	unsigned int total_segs = TOTAL_SEGS(sbi);
149 
150 	mutex_lock(&dirty_i->seglist_lock);
151 	while (1) {
152 		segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
153 				offset);
154 		if (segno >= total_segs)
155 			break;
156 
157 		offset = segno + 1;
158 		if (test_and_clear_bit(segno, dirty_i->dirty_segmap[PRE]))
159 			dirty_i->nr_dirty[PRE]--;
160 
161 		/* Let's use trim */
162 		if (test_opt(sbi, DISCARD))
163 			blkdev_issue_discard(sbi->sb->s_bdev,
164 					START_BLOCK(sbi, segno) <<
165 					sbi->log_sectors_per_block,
166 					1 << (sbi->log_sectors_per_block +
167 						sbi->log_blocks_per_seg),
168 					GFP_NOFS, 0);
169 	}
170 	mutex_unlock(&dirty_i->seglist_lock);
171 }
172 
173 static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
174 {
175 	struct sit_info *sit_i = SIT_I(sbi);
176 	if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
177 		sit_i->dirty_sentries++;
178 }
179 
180 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
181 					unsigned int segno, int modified)
182 {
183 	struct seg_entry *se = get_seg_entry(sbi, segno);
184 	se->type = type;
185 	if (modified)
186 		__mark_sit_entry_dirty(sbi, segno);
187 }
188 
189 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
190 {
191 	struct seg_entry *se;
192 	unsigned int segno, offset;
193 	long int new_vblocks;
194 
195 	segno = GET_SEGNO(sbi, blkaddr);
196 
197 	se = get_seg_entry(sbi, segno);
198 	new_vblocks = se->valid_blocks + del;
199 	offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1);
200 
201 	BUG_ON((new_vblocks >> (sizeof(unsigned short) << 3) ||
202 				(new_vblocks > sbi->blocks_per_seg)));
203 
204 	se->valid_blocks = new_vblocks;
205 	se->mtime = get_mtime(sbi);
206 	SIT_I(sbi)->max_mtime = se->mtime;
207 
208 	/* Update valid block bitmap */
209 	if (del > 0) {
210 		if (f2fs_set_bit(offset, se->cur_valid_map))
211 			BUG();
212 	} else {
213 		if (!f2fs_clear_bit(offset, se->cur_valid_map))
214 			BUG();
215 	}
216 	if (!f2fs_test_bit(offset, se->ckpt_valid_map))
217 		se->ckpt_valid_blocks += del;
218 
219 	__mark_sit_entry_dirty(sbi, segno);
220 
221 	/* update total number of valid blocks to be written in ckpt area */
222 	SIT_I(sbi)->written_valid_blocks += del;
223 
224 	if (sbi->segs_per_sec > 1)
225 		get_sec_entry(sbi, segno)->valid_blocks += del;
226 }
227 
228 static void refresh_sit_entry(struct f2fs_sb_info *sbi,
229 			block_t old_blkaddr, block_t new_blkaddr)
230 {
231 	update_sit_entry(sbi, new_blkaddr, 1);
232 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
233 		update_sit_entry(sbi, old_blkaddr, -1);
234 }
235 
236 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
237 {
238 	unsigned int segno = GET_SEGNO(sbi, addr);
239 	struct sit_info *sit_i = SIT_I(sbi);
240 
241 	BUG_ON(addr == NULL_ADDR);
242 	if (addr == NEW_ADDR)
243 		return;
244 
245 	/* add it into sit main buffer */
246 	mutex_lock(&sit_i->sentry_lock);
247 
248 	update_sit_entry(sbi, addr, -1);
249 
250 	/* add it into dirty seglist */
251 	locate_dirty_segment(sbi, segno);
252 
253 	mutex_unlock(&sit_i->sentry_lock);
254 }
255 
256 /*
257  * This function should be resided under the curseg_mutex lock
258  */
259 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
260 		struct f2fs_summary *sum, unsigned short offset)
261 {
262 	struct curseg_info *curseg = CURSEG_I(sbi, type);
263 	void *addr = curseg->sum_blk;
264 	addr += offset * sizeof(struct f2fs_summary);
265 	memcpy(addr, sum, sizeof(struct f2fs_summary));
266 	return;
267 }
268 
269 /*
270  * Calculate the number of current summary pages for writing
271  */
272 int npages_for_summary_flush(struct f2fs_sb_info *sbi)
273 {
274 	int total_size_bytes = 0;
275 	int valid_sum_count = 0;
276 	int i, sum_space;
277 
278 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
279 		if (sbi->ckpt->alloc_type[i] == SSR)
280 			valid_sum_count += sbi->blocks_per_seg;
281 		else
282 			valid_sum_count += curseg_blkoff(sbi, i);
283 	}
284 
285 	total_size_bytes = valid_sum_count * (SUMMARY_SIZE + 1)
286 			+ sizeof(struct nat_journal) + 2
287 			+ sizeof(struct sit_journal) + 2;
288 	sum_space = PAGE_CACHE_SIZE - SUM_FOOTER_SIZE;
289 	if (total_size_bytes < sum_space)
290 		return 1;
291 	else if (total_size_bytes < 2 * sum_space)
292 		return 2;
293 	return 3;
294 }
295 
296 /*
297  * Caller should put this summary page
298  */
299 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
300 {
301 	return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
302 }
303 
304 static void write_sum_page(struct f2fs_sb_info *sbi,
305 			struct f2fs_summary_block *sum_blk, block_t blk_addr)
306 {
307 	struct page *page = grab_meta_page(sbi, blk_addr);
308 	void *kaddr = page_address(page);
309 	memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
310 	set_page_dirty(page);
311 	f2fs_put_page(page, 1);
312 }
313 
314 static unsigned int check_prefree_segments(struct f2fs_sb_info *sbi, int type)
315 {
316 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
317 	unsigned long *prefree_segmap = dirty_i->dirty_segmap[PRE];
318 	unsigned int segno;
319 	unsigned int ofs = 0;
320 
321 	/*
322 	 * If there is not enough reserved sections,
323 	 * we should not reuse prefree segments.
324 	 */
325 	if (has_not_enough_free_secs(sbi, 0))
326 		return NULL_SEGNO;
327 
328 	/*
329 	 * NODE page should not reuse prefree segment,
330 	 * since those information is used for SPOR.
331 	 */
332 	if (IS_NODESEG(type))
333 		return NULL_SEGNO;
334 next:
335 	segno = find_next_bit(prefree_segmap, TOTAL_SEGS(sbi), ofs);
336 	ofs += sbi->segs_per_sec;
337 
338 	if (segno < TOTAL_SEGS(sbi)) {
339 		int i;
340 
341 		/* skip intermediate segments in a section */
342 		if (segno % sbi->segs_per_sec)
343 			goto next;
344 
345 		/* skip if the section is currently used */
346 		if (sec_usage_check(sbi, GET_SECNO(sbi, segno)))
347 			goto next;
348 
349 		/* skip if whole section is not prefree */
350 		for (i = 1; i < sbi->segs_per_sec; i++)
351 			if (!test_bit(segno + i, prefree_segmap))
352 				goto next;
353 
354 		/* skip if whole section was not free at the last checkpoint */
355 		for (i = 0; i < sbi->segs_per_sec; i++)
356 			if (get_seg_entry(sbi, segno + i)->ckpt_valid_blocks)
357 				goto next;
358 
359 		return segno;
360 	}
361 	return NULL_SEGNO;
362 }
363 
364 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
365 {
366 	struct curseg_info *curseg = CURSEG_I(sbi, type);
367 	unsigned int segno = curseg->segno;
368 	struct free_segmap_info *free_i = FREE_I(sbi);
369 
370 	if (segno + 1 < TOTAL_SEGS(sbi) && (segno + 1) % sbi->segs_per_sec)
371 		return !test_bit(segno + 1, free_i->free_segmap);
372 	return 0;
373 }
374 
375 /*
376  * Find a new segment from the free segments bitmap to right order
377  * This function should be returned with success, otherwise BUG
378  */
379 static void get_new_segment(struct f2fs_sb_info *sbi,
380 			unsigned int *newseg, bool new_sec, int dir)
381 {
382 	struct free_segmap_info *free_i = FREE_I(sbi);
383 	unsigned int segno, secno, zoneno;
384 	unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
385 	unsigned int hint = *newseg / sbi->segs_per_sec;
386 	unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
387 	unsigned int left_start = hint;
388 	bool init = true;
389 	int go_left = 0;
390 	int i;
391 
392 	write_lock(&free_i->segmap_lock);
393 
394 	if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
395 		segno = find_next_zero_bit(free_i->free_segmap,
396 					TOTAL_SEGS(sbi), *newseg + 1);
397 		if (segno - *newseg < sbi->segs_per_sec -
398 					(*newseg % sbi->segs_per_sec))
399 			goto got_it;
400 	}
401 find_other_zone:
402 	secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
403 	if (secno >= TOTAL_SECS(sbi)) {
404 		if (dir == ALLOC_RIGHT) {
405 			secno = find_next_zero_bit(free_i->free_secmap,
406 							TOTAL_SECS(sbi), 0);
407 			BUG_ON(secno >= TOTAL_SECS(sbi));
408 		} else {
409 			go_left = 1;
410 			left_start = hint - 1;
411 		}
412 	}
413 	if (go_left == 0)
414 		goto skip_left;
415 
416 	while (test_bit(left_start, free_i->free_secmap)) {
417 		if (left_start > 0) {
418 			left_start--;
419 			continue;
420 		}
421 		left_start = find_next_zero_bit(free_i->free_secmap,
422 							TOTAL_SECS(sbi), 0);
423 		BUG_ON(left_start >= TOTAL_SECS(sbi));
424 		break;
425 	}
426 	secno = left_start;
427 skip_left:
428 	hint = secno;
429 	segno = secno * sbi->segs_per_sec;
430 	zoneno = secno / sbi->secs_per_zone;
431 
432 	/* give up on finding another zone */
433 	if (!init)
434 		goto got_it;
435 	if (sbi->secs_per_zone == 1)
436 		goto got_it;
437 	if (zoneno == old_zoneno)
438 		goto got_it;
439 	if (dir == ALLOC_LEFT) {
440 		if (!go_left && zoneno + 1 >= total_zones)
441 			goto got_it;
442 		if (go_left && zoneno == 0)
443 			goto got_it;
444 	}
445 	for (i = 0; i < NR_CURSEG_TYPE; i++)
446 		if (CURSEG_I(sbi, i)->zone == zoneno)
447 			break;
448 
449 	if (i < NR_CURSEG_TYPE) {
450 		/* zone is in user, try another */
451 		if (go_left)
452 			hint = zoneno * sbi->secs_per_zone - 1;
453 		else if (zoneno + 1 >= total_zones)
454 			hint = 0;
455 		else
456 			hint = (zoneno + 1) * sbi->secs_per_zone;
457 		init = false;
458 		goto find_other_zone;
459 	}
460 got_it:
461 	/* set it as dirty segment in free segmap */
462 	BUG_ON(test_bit(segno, free_i->free_segmap));
463 	__set_inuse(sbi, segno);
464 	*newseg = segno;
465 	write_unlock(&free_i->segmap_lock);
466 }
467 
468 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
469 {
470 	struct curseg_info *curseg = CURSEG_I(sbi, type);
471 	struct summary_footer *sum_footer;
472 
473 	curseg->segno = curseg->next_segno;
474 	curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
475 	curseg->next_blkoff = 0;
476 	curseg->next_segno = NULL_SEGNO;
477 
478 	sum_footer = &(curseg->sum_blk->footer);
479 	memset(sum_footer, 0, sizeof(struct summary_footer));
480 	if (IS_DATASEG(type))
481 		SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
482 	if (IS_NODESEG(type))
483 		SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
484 	__set_sit_entry_type(sbi, type, curseg->segno, modified);
485 }
486 
487 /*
488  * Allocate a current working segment.
489  * This function always allocates a free segment in LFS manner.
490  */
491 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
492 {
493 	struct curseg_info *curseg = CURSEG_I(sbi, type);
494 	unsigned int segno = curseg->segno;
495 	int dir = ALLOC_LEFT;
496 
497 	write_sum_page(sbi, curseg->sum_blk,
498 				GET_SUM_BLOCK(sbi, curseg->segno));
499 	if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
500 		dir = ALLOC_RIGHT;
501 
502 	if (test_opt(sbi, NOHEAP))
503 		dir = ALLOC_RIGHT;
504 
505 	get_new_segment(sbi, &segno, new_sec, dir);
506 	curseg->next_segno = segno;
507 	reset_curseg(sbi, type, 1);
508 	curseg->alloc_type = LFS;
509 }
510 
511 static void __next_free_blkoff(struct f2fs_sb_info *sbi,
512 			struct curseg_info *seg, block_t start)
513 {
514 	struct seg_entry *se = get_seg_entry(sbi, seg->segno);
515 	block_t ofs;
516 	for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {
517 		if (!f2fs_test_bit(ofs, se->ckpt_valid_map)
518 			&& !f2fs_test_bit(ofs, se->cur_valid_map))
519 			break;
520 	}
521 	seg->next_blkoff = ofs;
522 }
523 
524 /*
525  * If a segment is written by LFS manner, next block offset is just obtained
526  * by increasing the current block offset. However, if a segment is written by
527  * SSR manner, next block offset obtained by calling __next_free_blkoff
528  */
529 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
530 				struct curseg_info *seg)
531 {
532 	if (seg->alloc_type == SSR)
533 		__next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
534 	else
535 		seg->next_blkoff++;
536 }
537 
538 /*
539  * This function always allocates a used segment (from dirty seglist) by SSR
540  * manner, so it should recover the existing segment information of valid blocks
541  */
542 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
543 {
544 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
545 	struct curseg_info *curseg = CURSEG_I(sbi, type);
546 	unsigned int new_segno = curseg->next_segno;
547 	struct f2fs_summary_block *sum_node;
548 	struct page *sum_page;
549 
550 	write_sum_page(sbi, curseg->sum_blk,
551 				GET_SUM_BLOCK(sbi, curseg->segno));
552 	__set_test_and_inuse(sbi, new_segno);
553 
554 	mutex_lock(&dirty_i->seglist_lock);
555 	__remove_dirty_segment(sbi, new_segno, PRE);
556 	__remove_dirty_segment(sbi, new_segno, DIRTY);
557 	mutex_unlock(&dirty_i->seglist_lock);
558 
559 	reset_curseg(sbi, type, 1);
560 	curseg->alloc_type = SSR;
561 	__next_free_blkoff(sbi, curseg, 0);
562 
563 	if (reuse) {
564 		sum_page = get_sum_page(sbi, new_segno);
565 		sum_node = (struct f2fs_summary_block *)page_address(sum_page);
566 		memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
567 		f2fs_put_page(sum_page, 1);
568 	}
569 }
570 
571 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
572 {
573 	struct curseg_info *curseg = CURSEG_I(sbi, type);
574 	const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
575 
576 	if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
577 		return v_ops->get_victim(sbi,
578 				&(curseg)->next_segno, BG_GC, type, SSR);
579 
580 	/* For data segments, let's do SSR more intensively */
581 	for (; type >= CURSEG_HOT_DATA; type--)
582 		if (v_ops->get_victim(sbi, &(curseg)->next_segno,
583 						BG_GC, type, SSR))
584 			return 1;
585 	return 0;
586 }
587 
588 /*
589  * flush out current segment and replace it with new segment
590  * This function should be returned with success, otherwise BUG
591  */
592 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
593 						int type, bool force)
594 {
595 	struct curseg_info *curseg = CURSEG_I(sbi, type);
596 
597 	if (force) {
598 		new_curseg(sbi, type, true);
599 		goto out;
600 	}
601 
602 	curseg->next_segno = check_prefree_segments(sbi, type);
603 
604 	if (curseg->next_segno != NULL_SEGNO)
605 		change_curseg(sbi, type, false);
606 	else if (type == CURSEG_WARM_NODE)
607 		new_curseg(sbi, type, false);
608 	else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
609 		new_curseg(sbi, type, false);
610 	else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
611 		change_curseg(sbi, type, true);
612 	else
613 		new_curseg(sbi, type, false);
614 out:
615 	sbi->segment_count[curseg->alloc_type]++;
616 }
617 
618 void allocate_new_segments(struct f2fs_sb_info *sbi)
619 {
620 	struct curseg_info *curseg;
621 	unsigned int old_curseg;
622 	int i;
623 
624 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
625 		curseg = CURSEG_I(sbi, i);
626 		old_curseg = curseg->segno;
627 		SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
628 		locate_dirty_segment(sbi, old_curseg);
629 	}
630 }
631 
632 static const struct segment_allocation default_salloc_ops = {
633 	.allocate_segment = allocate_segment_by_default,
634 };
635 
636 static void f2fs_end_io_write(struct bio *bio, int err)
637 {
638 	const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
639 	struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
640 	struct bio_private *p = bio->bi_private;
641 
642 	do {
643 		struct page *page = bvec->bv_page;
644 
645 		if (--bvec >= bio->bi_io_vec)
646 			prefetchw(&bvec->bv_page->flags);
647 		if (!uptodate) {
648 			SetPageError(page);
649 			if (page->mapping)
650 				set_bit(AS_EIO, &page->mapping->flags);
651 			set_ckpt_flags(p->sbi->ckpt, CP_ERROR_FLAG);
652 			p->sbi->sb->s_flags |= MS_RDONLY;
653 		}
654 		end_page_writeback(page);
655 		dec_page_count(p->sbi, F2FS_WRITEBACK);
656 	} while (bvec >= bio->bi_io_vec);
657 
658 	if (p->is_sync)
659 		complete(p->wait);
660 	kfree(p);
661 	bio_put(bio);
662 }
663 
664 struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
665 {
666 	struct bio *bio;
667 	struct bio_private *priv;
668 retry:
669 	priv = kmalloc(sizeof(struct bio_private), GFP_NOFS);
670 	if (!priv) {
671 		cond_resched();
672 		goto retry;
673 	}
674 
675 	/* No failure on bio allocation */
676 	bio = bio_alloc(GFP_NOIO, npages);
677 	bio->bi_bdev = bdev;
678 	bio->bi_private = priv;
679 	return bio;
680 }
681 
682 static void do_submit_bio(struct f2fs_sb_info *sbi,
683 				enum page_type type, bool sync)
684 {
685 	int rw = sync ? WRITE_SYNC : WRITE;
686 	enum page_type btype = type > META ? META : type;
687 
688 	if (type >= META_FLUSH)
689 		rw = WRITE_FLUSH_FUA;
690 
691 	if (btype == META)
692 		rw |= REQ_META;
693 
694 	if (sbi->bio[btype]) {
695 		struct bio_private *p = sbi->bio[btype]->bi_private;
696 		p->sbi = sbi;
697 		sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
698 
699 		trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
700 
701 		if (type == META_FLUSH) {
702 			DECLARE_COMPLETION_ONSTACK(wait);
703 			p->is_sync = true;
704 			p->wait = &wait;
705 			submit_bio(rw, sbi->bio[btype]);
706 			wait_for_completion(&wait);
707 		} else {
708 			p->is_sync = false;
709 			submit_bio(rw, sbi->bio[btype]);
710 		}
711 		sbi->bio[btype] = NULL;
712 	}
713 }
714 
715 void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
716 {
717 	down_write(&sbi->bio_sem);
718 	do_submit_bio(sbi, type, sync);
719 	up_write(&sbi->bio_sem);
720 }
721 
722 static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
723 				block_t blk_addr, enum page_type type)
724 {
725 	struct block_device *bdev = sbi->sb->s_bdev;
726 
727 	verify_block_addr(sbi, blk_addr);
728 
729 	down_write(&sbi->bio_sem);
730 
731 	inc_page_count(sbi, F2FS_WRITEBACK);
732 
733 	if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
734 		do_submit_bio(sbi, type, false);
735 alloc_new:
736 	if (sbi->bio[type] == NULL) {
737 		sbi->bio[type] = f2fs_bio_alloc(bdev, max_hw_blocks(sbi));
738 		sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
739 		/*
740 		 * The end_io will be assigned at the sumbission phase.
741 		 * Until then, let bio_add_page() merge consecutive IOs as much
742 		 * as possible.
743 		 */
744 	}
745 
746 	if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
747 							PAGE_CACHE_SIZE) {
748 		do_submit_bio(sbi, type, false);
749 		goto alloc_new;
750 	}
751 
752 	sbi->last_block_in_bio[type] = blk_addr;
753 
754 	up_write(&sbi->bio_sem);
755 	trace_f2fs_submit_write_page(page, blk_addr, type);
756 }
757 
758 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
759 {
760 	struct curseg_info *curseg = CURSEG_I(sbi, type);
761 	if (curseg->next_blkoff < sbi->blocks_per_seg)
762 		return true;
763 	return false;
764 }
765 
766 static int __get_segment_type_2(struct page *page, enum page_type p_type)
767 {
768 	if (p_type == DATA)
769 		return CURSEG_HOT_DATA;
770 	else
771 		return CURSEG_HOT_NODE;
772 }
773 
774 static int __get_segment_type_4(struct page *page, enum page_type p_type)
775 {
776 	if (p_type == DATA) {
777 		struct inode *inode = page->mapping->host;
778 
779 		if (S_ISDIR(inode->i_mode))
780 			return CURSEG_HOT_DATA;
781 		else
782 			return CURSEG_COLD_DATA;
783 	} else {
784 		if (IS_DNODE(page) && !is_cold_node(page))
785 			return CURSEG_HOT_NODE;
786 		else
787 			return CURSEG_COLD_NODE;
788 	}
789 }
790 
791 static int __get_segment_type_6(struct page *page, enum page_type p_type)
792 {
793 	if (p_type == DATA) {
794 		struct inode *inode = page->mapping->host;
795 
796 		if (S_ISDIR(inode->i_mode))
797 			return CURSEG_HOT_DATA;
798 		else if (is_cold_data(page) || is_cold_file(inode))
799 			return CURSEG_COLD_DATA;
800 		else
801 			return CURSEG_WARM_DATA;
802 	} else {
803 		if (IS_DNODE(page))
804 			return is_cold_node(page) ? CURSEG_WARM_NODE :
805 						CURSEG_HOT_NODE;
806 		else
807 			return CURSEG_COLD_NODE;
808 	}
809 }
810 
811 static int __get_segment_type(struct page *page, enum page_type p_type)
812 {
813 	struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
814 	switch (sbi->active_logs) {
815 	case 2:
816 		return __get_segment_type_2(page, p_type);
817 	case 4:
818 		return __get_segment_type_4(page, p_type);
819 	}
820 	/* NR_CURSEG_TYPE(6) logs by default */
821 	BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
822 	return __get_segment_type_6(page, p_type);
823 }
824 
825 static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
826 			block_t old_blkaddr, block_t *new_blkaddr,
827 			struct f2fs_summary *sum, enum page_type p_type)
828 {
829 	struct sit_info *sit_i = SIT_I(sbi);
830 	struct curseg_info *curseg;
831 	unsigned int old_cursegno;
832 	int type;
833 
834 	type = __get_segment_type(page, p_type);
835 	curseg = CURSEG_I(sbi, type);
836 
837 	mutex_lock(&curseg->curseg_mutex);
838 
839 	*new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
840 	old_cursegno = curseg->segno;
841 
842 	/*
843 	 * __add_sum_entry should be resided under the curseg_mutex
844 	 * because, this function updates a summary entry in the
845 	 * current summary block.
846 	 */
847 	__add_sum_entry(sbi, type, sum, curseg->next_blkoff);
848 
849 	mutex_lock(&sit_i->sentry_lock);
850 	__refresh_next_blkoff(sbi, curseg);
851 	sbi->block_count[curseg->alloc_type]++;
852 
853 	/*
854 	 * SIT information should be updated before segment allocation,
855 	 * since SSR needs latest valid block information.
856 	 */
857 	refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
858 
859 	if (!__has_curseg_space(sbi, type))
860 		sit_i->s_ops->allocate_segment(sbi, type, false);
861 
862 	locate_dirty_segment(sbi, old_cursegno);
863 	locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
864 	mutex_unlock(&sit_i->sentry_lock);
865 
866 	if (p_type == NODE)
867 		fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
868 
869 	/* writeout dirty page into bdev */
870 	submit_write_page(sbi, page, *new_blkaddr, p_type);
871 
872 	mutex_unlock(&curseg->curseg_mutex);
873 }
874 
875 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
876 {
877 	set_page_writeback(page);
878 	submit_write_page(sbi, page, page->index, META);
879 }
880 
881 void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
882 		unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
883 {
884 	struct f2fs_summary sum;
885 	set_summary(&sum, nid, 0, 0);
886 	do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
887 }
888 
889 void write_data_page(struct inode *inode, struct page *page,
890 		struct dnode_of_data *dn, block_t old_blkaddr,
891 		block_t *new_blkaddr)
892 {
893 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
894 	struct f2fs_summary sum;
895 	struct node_info ni;
896 
897 	BUG_ON(old_blkaddr == NULL_ADDR);
898 	get_node_info(sbi, dn->nid, &ni);
899 	set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
900 
901 	do_write_page(sbi, page, old_blkaddr,
902 			new_blkaddr, &sum, DATA);
903 }
904 
905 void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
906 					block_t old_blk_addr)
907 {
908 	submit_write_page(sbi, page, old_blk_addr, DATA);
909 }
910 
911 void recover_data_page(struct f2fs_sb_info *sbi,
912 			struct page *page, struct f2fs_summary *sum,
913 			block_t old_blkaddr, block_t new_blkaddr)
914 {
915 	struct sit_info *sit_i = SIT_I(sbi);
916 	struct curseg_info *curseg;
917 	unsigned int segno, old_cursegno;
918 	struct seg_entry *se;
919 	int type;
920 
921 	segno = GET_SEGNO(sbi, new_blkaddr);
922 	se = get_seg_entry(sbi, segno);
923 	type = se->type;
924 
925 	if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
926 		if (old_blkaddr == NULL_ADDR)
927 			type = CURSEG_COLD_DATA;
928 		else
929 			type = CURSEG_WARM_DATA;
930 	}
931 	curseg = CURSEG_I(sbi, type);
932 
933 	mutex_lock(&curseg->curseg_mutex);
934 	mutex_lock(&sit_i->sentry_lock);
935 
936 	old_cursegno = curseg->segno;
937 
938 	/* change the current segment */
939 	if (segno != curseg->segno) {
940 		curseg->next_segno = segno;
941 		change_curseg(sbi, type, true);
942 	}
943 
944 	curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
945 					(sbi->blocks_per_seg - 1);
946 	__add_sum_entry(sbi, type, sum, curseg->next_blkoff);
947 
948 	refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
949 
950 	locate_dirty_segment(sbi, old_cursegno);
951 	locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
952 
953 	mutex_unlock(&sit_i->sentry_lock);
954 	mutex_unlock(&curseg->curseg_mutex);
955 }
956 
957 void rewrite_node_page(struct f2fs_sb_info *sbi,
958 			struct page *page, struct f2fs_summary *sum,
959 			block_t old_blkaddr, block_t new_blkaddr)
960 {
961 	struct sit_info *sit_i = SIT_I(sbi);
962 	int type = CURSEG_WARM_NODE;
963 	struct curseg_info *curseg;
964 	unsigned int segno, old_cursegno;
965 	block_t next_blkaddr = next_blkaddr_of_node(page);
966 	unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
967 
968 	curseg = CURSEG_I(sbi, type);
969 
970 	mutex_lock(&curseg->curseg_mutex);
971 	mutex_lock(&sit_i->sentry_lock);
972 
973 	segno = GET_SEGNO(sbi, new_blkaddr);
974 	old_cursegno = curseg->segno;
975 
976 	/* change the current segment */
977 	if (segno != curseg->segno) {
978 		curseg->next_segno = segno;
979 		change_curseg(sbi, type, true);
980 	}
981 	curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
982 					(sbi->blocks_per_seg - 1);
983 	__add_sum_entry(sbi, type, sum, curseg->next_blkoff);
984 
985 	/* change the current log to the next block addr in advance */
986 	if (next_segno != segno) {
987 		curseg->next_segno = next_segno;
988 		change_curseg(sbi, type, true);
989 	}
990 	curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
991 					(sbi->blocks_per_seg - 1);
992 
993 	/* rewrite node page */
994 	set_page_writeback(page);
995 	submit_write_page(sbi, page, new_blkaddr, NODE);
996 	f2fs_submit_bio(sbi, NODE, true);
997 	refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
998 
999 	locate_dirty_segment(sbi, old_cursegno);
1000 	locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
1001 
1002 	mutex_unlock(&sit_i->sentry_lock);
1003 	mutex_unlock(&curseg->curseg_mutex);
1004 }
1005 
1006 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1007 {
1008 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1009 	struct curseg_info *seg_i;
1010 	unsigned char *kaddr;
1011 	struct page *page;
1012 	block_t start;
1013 	int i, j, offset;
1014 
1015 	start = start_sum_block(sbi);
1016 
1017 	page = get_meta_page(sbi, start++);
1018 	kaddr = (unsigned char *)page_address(page);
1019 
1020 	/* Step 1: restore nat cache */
1021 	seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1022 	memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1023 
1024 	/* Step 2: restore sit cache */
1025 	seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1026 	memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1027 						SUM_JOURNAL_SIZE);
1028 	offset = 2 * SUM_JOURNAL_SIZE;
1029 
1030 	/* Step 3: restore summary entries */
1031 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1032 		unsigned short blk_off;
1033 		unsigned int segno;
1034 
1035 		seg_i = CURSEG_I(sbi, i);
1036 		segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1037 		blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1038 		seg_i->next_segno = segno;
1039 		reset_curseg(sbi, i, 0);
1040 		seg_i->alloc_type = ckpt->alloc_type[i];
1041 		seg_i->next_blkoff = blk_off;
1042 
1043 		if (seg_i->alloc_type == SSR)
1044 			blk_off = sbi->blocks_per_seg;
1045 
1046 		for (j = 0; j < blk_off; j++) {
1047 			struct f2fs_summary *s;
1048 			s = (struct f2fs_summary *)(kaddr + offset);
1049 			seg_i->sum_blk->entries[j] = *s;
1050 			offset += SUMMARY_SIZE;
1051 			if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1052 						SUM_FOOTER_SIZE)
1053 				continue;
1054 
1055 			f2fs_put_page(page, 1);
1056 			page = NULL;
1057 
1058 			page = get_meta_page(sbi, start++);
1059 			kaddr = (unsigned char *)page_address(page);
1060 			offset = 0;
1061 		}
1062 	}
1063 	f2fs_put_page(page, 1);
1064 	return 0;
1065 }
1066 
1067 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1068 {
1069 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1070 	struct f2fs_summary_block *sum;
1071 	struct curseg_info *curseg;
1072 	struct page *new;
1073 	unsigned short blk_off;
1074 	unsigned int segno = 0;
1075 	block_t blk_addr = 0;
1076 
1077 	/* get segment number and block addr */
1078 	if (IS_DATASEG(type)) {
1079 		segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1080 		blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1081 							CURSEG_HOT_DATA]);
1082 		if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1083 			blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1084 		else
1085 			blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1086 	} else {
1087 		segno = le32_to_cpu(ckpt->cur_node_segno[type -
1088 							CURSEG_HOT_NODE]);
1089 		blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1090 							CURSEG_HOT_NODE]);
1091 		if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1092 			blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1093 							type - CURSEG_HOT_NODE);
1094 		else
1095 			blk_addr = GET_SUM_BLOCK(sbi, segno);
1096 	}
1097 
1098 	new = get_meta_page(sbi, blk_addr);
1099 	sum = (struct f2fs_summary_block *)page_address(new);
1100 
1101 	if (IS_NODESEG(type)) {
1102 		if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1103 			struct f2fs_summary *ns = &sum->entries[0];
1104 			int i;
1105 			for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1106 				ns->version = 0;
1107 				ns->ofs_in_node = 0;
1108 			}
1109 		} else {
1110 			if (restore_node_summary(sbi, segno, sum)) {
1111 				f2fs_put_page(new, 1);
1112 				return -EINVAL;
1113 			}
1114 		}
1115 	}
1116 
1117 	/* set uncompleted segment to curseg */
1118 	curseg = CURSEG_I(sbi, type);
1119 	mutex_lock(&curseg->curseg_mutex);
1120 	memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1121 	curseg->next_segno = segno;
1122 	reset_curseg(sbi, type, 0);
1123 	curseg->alloc_type = ckpt->alloc_type[type];
1124 	curseg->next_blkoff = blk_off;
1125 	mutex_unlock(&curseg->curseg_mutex);
1126 	f2fs_put_page(new, 1);
1127 	return 0;
1128 }
1129 
1130 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1131 {
1132 	int type = CURSEG_HOT_DATA;
1133 
1134 	if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1135 		/* restore for compacted data summary */
1136 		if (read_compacted_summaries(sbi))
1137 			return -EINVAL;
1138 		type = CURSEG_HOT_NODE;
1139 	}
1140 
1141 	for (; type <= CURSEG_COLD_NODE; type++)
1142 		if (read_normal_summaries(sbi, type))
1143 			return -EINVAL;
1144 	return 0;
1145 }
1146 
1147 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1148 {
1149 	struct page *page;
1150 	unsigned char *kaddr;
1151 	struct f2fs_summary *summary;
1152 	struct curseg_info *seg_i;
1153 	int written_size = 0;
1154 	int i, j;
1155 
1156 	page = grab_meta_page(sbi, blkaddr++);
1157 	kaddr = (unsigned char *)page_address(page);
1158 
1159 	/* Step 1: write nat cache */
1160 	seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1161 	memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1162 	written_size += SUM_JOURNAL_SIZE;
1163 
1164 	/* Step 2: write sit cache */
1165 	seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1166 	memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1167 						SUM_JOURNAL_SIZE);
1168 	written_size += SUM_JOURNAL_SIZE;
1169 
1170 	set_page_dirty(page);
1171 
1172 	/* Step 3: write summary entries */
1173 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1174 		unsigned short blkoff;
1175 		seg_i = CURSEG_I(sbi, i);
1176 		if (sbi->ckpt->alloc_type[i] == SSR)
1177 			blkoff = sbi->blocks_per_seg;
1178 		else
1179 			blkoff = curseg_blkoff(sbi, i);
1180 
1181 		for (j = 0; j < blkoff; j++) {
1182 			if (!page) {
1183 				page = grab_meta_page(sbi, blkaddr++);
1184 				kaddr = (unsigned char *)page_address(page);
1185 				written_size = 0;
1186 			}
1187 			summary = (struct f2fs_summary *)(kaddr + written_size);
1188 			*summary = seg_i->sum_blk->entries[j];
1189 			written_size += SUMMARY_SIZE;
1190 			set_page_dirty(page);
1191 
1192 			if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1193 							SUM_FOOTER_SIZE)
1194 				continue;
1195 
1196 			f2fs_put_page(page, 1);
1197 			page = NULL;
1198 		}
1199 	}
1200 	if (page)
1201 		f2fs_put_page(page, 1);
1202 }
1203 
1204 static void write_normal_summaries(struct f2fs_sb_info *sbi,
1205 					block_t blkaddr, int type)
1206 {
1207 	int i, end;
1208 	if (IS_DATASEG(type))
1209 		end = type + NR_CURSEG_DATA_TYPE;
1210 	else
1211 		end = type + NR_CURSEG_NODE_TYPE;
1212 
1213 	for (i = type; i < end; i++) {
1214 		struct curseg_info *sum = CURSEG_I(sbi, i);
1215 		mutex_lock(&sum->curseg_mutex);
1216 		write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1217 		mutex_unlock(&sum->curseg_mutex);
1218 	}
1219 }
1220 
1221 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1222 {
1223 	if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1224 		write_compacted_summaries(sbi, start_blk);
1225 	else
1226 		write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1227 }
1228 
1229 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1230 {
1231 	if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1232 		write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1233 	return;
1234 }
1235 
1236 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1237 					unsigned int val, int alloc)
1238 {
1239 	int i;
1240 
1241 	if (type == NAT_JOURNAL) {
1242 		for (i = 0; i < nats_in_cursum(sum); i++) {
1243 			if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1244 				return i;
1245 		}
1246 		if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1247 			return update_nats_in_cursum(sum, 1);
1248 	} else if (type == SIT_JOURNAL) {
1249 		for (i = 0; i < sits_in_cursum(sum); i++)
1250 			if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1251 				return i;
1252 		if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1253 			return update_sits_in_cursum(sum, 1);
1254 	}
1255 	return -1;
1256 }
1257 
1258 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1259 					unsigned int segno)
1260 {
1261 	struct sit_info *sit_i = SIT_I(sbi);
1262 	unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1263 	block_t blk_addr = sit_i->sit_base_addr + offset;
1264 
1265 	check_seg_range(sbi, segno);
1266 
1267 	/* calculate sit block address */
1268 	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1269 		blk_addr += sit_i->sit_blocks;
1270 
1271 	return get_meta_page(sbi, blk_addr);
1272 }
1273 
1274 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1275 					unsigned int start)
1276 {
1277 	struct sit_info *sit_i = SIT_I(sbi);
1278 	struct page *src_page, *dst_page;
1279 	pgoff_t src_off, dst_off;
1280 	void *src_addr, *dst_addr;
1281 
1282 	src_off = current_sit_addr(sbi, start);
1283 	dst_off = next_sit_addr(sbi, src_off);
1284 
1285 	/* get current sit block page without lock */
1286 	src_page = get_meta_page(sbi, src_off);
1287 	dst_page = grab_meta_page(sbi, dst_off);
1288 	BUG_ON(PageDirty(src_page));
1289 
1290 	src_addr = page_address(src_page);
1291 	dst_addr = page_address(dst_page);
1292 	memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1293 
1294 	set_page_dirty(dst_page);
1295 	f2fs_put_page(src_page, 1);
1296 
1297 	set_to_next_sit(sit_i, start);
1298 
1299 	return dst_page;
1300 }
1301 
1302 static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1303 {
1304 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1305 	struct f2fs_summary_block *sum = curseg->sum_blk;
1306 	int i;
1307 
1308 	/*
1309 	 * If the journal area in the current summary is full of sit entries,
1310 	 * all the sit entries will be flushed. Otherwise the sit entries
1311 	 * are not able to replace with newly hot sit entries.
1312 	 */
1313 	if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1314 		for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1315 			unsigned int segno;
1316 			segno = le32_to_cpu(segno_in_journal(sum, i));
1317 			__mark_sit_entry_dirty(sbi, segno);
1318 		}
1319 		update_sits_in_cursum(sum, -sits_in_cursum(sum));
1320 		return 1;
1321 	}
1322 	return 0;
1323 }
1324 
1325 /*
1326  * CP calls this function, which flushes SIT entries including sit_journal,
1327  * and moves prefree segs to free segs.
1328  */
1329 void flush_sit_entries(struct f2fs_sb_info *sbi)
1330 {
1331 	struct sit_info *sit_i = SIT_I(sbi);
1332 	unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1333 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1334 	struct f2fs_summary_block *sum = curseg->sum_blk;
1335 	unsigned long nsegs = TOTAL_SEGS(sbi);
1336 	struct page *page = NULL;
1337 	struct f2fs_sit_block *raw_sit = NULL;
1338 	unsigned int start = 0, end = 0;
1339 	unsigned int segno = -1;
1340 	bool flushed;
1341 
1342 	mutex_lock(&curseg->curseg_mutex);
1343 	mutex_lock(&sit_i->sentry_lock);
1344 
1345 	/*
1346 	 * "flushed" indicates whether sit entries in journal are flushed
1347 	 * to the SIT area or not.
1348 	 */
1349 	flushed = flush_sits_in_journal(sbi);
1350 
1351 	while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1352 		struct seg_entry *se = get_seg_entry(sbi, segno);
1353 		int sit_offset, offset;
1354 
1355 		sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1356 
1357 		if (flushed)
1358 			goto to_sit_page;
1359 
1360 		offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1361 		if (offset >= 0) {
1362 			segno_in_journal(sum, offset) = cpu_to_le32(segno);
1363 			seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1364 			goto flush_done;
1365 		}
1366 to_sit_page:
1367 		if (!page || (start > segno) || (segno > end)) {
1368 			if (page) {
1369 				f2fs_put_page(page, 1);
1370 				page = NULL;
1371 			}
1372 
1373 			start = START_SEGNO(sit_i, segno);
1374 			end = start + SIT_ENTRY_PER_BLOCK - 1;
1375 
1376 			/* read sit block that will be updated */
1377 			page = get_next_sit_page(sbi, start);
1378 			raw_sit = page_address(page);
1379 		}
1380 
1381 		/* udpate entry in SIT block */
1382 		seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1383 flush_done:
1384 		__clear_bit(segno, bitmap);
1385 		sit_i->dirty_sentries--;
1386 	}
1387 	mutex_unlock(&sit_i->sentry_lock);
1388 	mutex_unlock(&curseg->curseg_mutex);
1389 
1390 	/* writeout last modified SIT block */
1391 	f2fs_put_page(page, 1);
1392 
1393 	set_prefree_as_free_segments(sbi);
1394 }
1395 
1396 static int build_sit_info(struct f2fs_sb_info *sbi)
1397 {
1398 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1399 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1400 	struct sit_info *sit_i;
1401 	unsigned int sit_segs, start;
1402 	char *src_bitmap, *dst_bitmap;
1403 	unsigned int bitmap_size;
1404 
1405 	/* allocate memory for SIT information */
1406 	sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1407 	if (!sit_i)
1408 		return -ENOMEM;
1409 
1410 	SM_I(sbi)->sit_info = sit_i;
1411 
1412 	sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1413 	if (!sit_i->sentries)
1414 		return -ENOMEM;
1415 
1416 	bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1417 	sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1418 	if (!sit_i->dirty_sentries_bitmap)
1419 		return -ENOMEM;
1420 
1421 	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1422 		sit_i->sentries[start].cur_valid_map
1423 			= kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1424 		sit_i->sentries[start].ckpt_valid_map
1425 			= kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1426 		if (!sit_i->sentries[start].cur_valid_map
1427 				|| !sit_i->sentries[start].ckpt_valid_map)
1428 			return -ENOMEM;
1429 	}
1430 
1431 	if (sbi->segs_per_sec > 1) {
1432 		sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1433 					sizeof(struct sec_entry));
1434 		if (!sit_i->sec_entries)
1435 			return -ENOMEM;
1436 	}
1437 
1438 	/* get information related with SIT */
1439 	sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1440 
1441 	/* setup SIT bitmap from ckeckpoint pack */
1442 	bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1443 	src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1444 
1445 	dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1446 	if (!dst_bitmap)
1447 		return -ENOMEM;
1448 
1449 	/* init SIT information */
1450 	sit_i->s_ops = &default_salloc_ops;
1451 
1452 	sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1453 	sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1454 	sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1455 	sit_i->sit_bitmap = dst_bitmap;
1456 	sit_i->bitmap_size = bitmap_size;
1457 	sit_i->dirty_sentries = 0;
1458 	sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1459 	sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1460 	sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1461 	mutex_init(&sit_i->sentry_lock);
1462 	return 0;
1463 }
1464 
1465 static int build_free_segmap(struct f2fs_sb_info *sbi)
1466 {
1467 	struct f2fs_sm_info *sm_info = SM_I(sbi);
1468 	struct free_segmap_info *free_i;
1469 	unsigned int bitmap_size, sec_bitmap_size;
1470 
1471 	/* allocate memory for free segmap information */
1472 	free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1473 	if (!free_i)
1474 		return -ENOMEM;
1475 
1476 	SM_I(sbi)->free_info = free_i;
1477 
1478 	bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1479 	free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1480 	if (!free_i->free_segmap)
1481 		return -ENOMEM;
1482 
1483 	sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1484 	free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1485 	if (!free_i->free_secmap)
1486 		return -ENOMEM;
1487 
1488 	/* set all segments as dirty temporarily */
1489 	memset(free_i->free_segmap, 0xff, bitmap_size);
1490 	memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1491 
1492 	/* init free segmap information */
1493 	free_i->start_segno =
1494 		(unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1495 	free_i->free_segments = 0;
1496 	free_i->free_sections = 0;
1497 	rwlock_init(&free_i->segmap_lock);
1498 	return 0;
1499 }
1500 
1501 static int build_curseg(struct f2fs_sb_info *sbi)
1502 {
1503 	struct curseg_info *array;
1504 	int i;
1505 
1506 	array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1507 	if (!array)
1508 		return -ENOMEM;
1509 
1510 	SM_I(sbi)->curseg_array = array;
1511 
1512 	for (i = 0; i < NR_CURSEG_TYPE; i++) {
1513 		mutex_init(&array[i].curseg_mutex);
1514 		array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1515 		if (!array[i].sum_blk)
1516 			return -ENOMEM;
1517 		array[i].segno = NULL_SEGNO;
1518 		array[i].next_blkoff = 0;
1519 	}
1520 	return restore_curseg_summaries(sbi);
1521 }
1522 
1523 static void build_sit_entries(struct f2fs_sb_info *sbi)
1524 {
1525 	struct sit_info *sit_i = SIT_I(sbi);
1526 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1527 	struct f2fs_summary_block *sum = curseg->sum_blk;
1528 	unsigned int start;
1529 
1530 	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1531 		struct seg_entry *se = &sit_i->sentries[start];
1532 		struct f2fs_sit_block *sit_blk;
1533 		struct f2fs_sit_entry sit;
1534 		struct page *page;
1535 		int i;
1536 
1537 		mutex_lock(&curseg->curseg_mutex);
1538 		for (i = 0; i < sits_in_cursum(sum); i++) {
1539 			if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1540 				sit = sit_in_journal(sum, i);
1541 				mutex_unlock(&curseg->curseg_mutex);
1542 				goto got_it;
1543 			}
1544 		}
1545 		mutex_unlock(&curseg->curseg_mutex);
1546 		page = get_current_sit_page(sbi, start);
1547 		sit_blk = (struct f2fs_sit_block *)page_address(page);
1548 		sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1549 		f2fs_put_page(page, 1);
1550 got_it:
1551 		check_block_count(sbi, start, &sit);
1552 		seg_info_from_raw_sit(se, &sit);
1553 		if (sbi->segs_per_sec > 1) {
1554 			struct sec_entry *e = get_sec_entry(sbi, start);
1555 			e->valid_blocks += se->valid_blocks;
1556 		}
1557 	}
1558 }
1559 
1560 static void init_free_segmap(struct f2fs_sb_info *sbi)
1561 {
1562 	unsigned int start;
1563 	int type;
1564 
1565 	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1566 		struct seg_entry *sentry = get_seg_entry(sbi, start);
1567 		if (!sentry->valid_blocks)
1568 			__set_free(sbi, start);
1569 	}
1570 
1571 	/* set use the current segments */
1572 	for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1573 		struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1574 		__set_test_and_inuse(sbi, curseg_t->segno);
1575 	}
1576 }
1577 
1578 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1579 {
1580 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1581 	struct free_segmap_info *free_i = FREE_I(sbi);
1582 	unsigned int segno = 0, offset = 0;
1583 	unsigned short valid_blocks;
1584 
1585 	while (segno < TOTAL_SEGS(sbi)) {
1586 		/* find dirty segment based on free segmap */
1587 		segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1588 		if (segno >= TOTAL_SEGS(sbi))
1589 			break;
1590 		offset = segno + 1;
1591 		valid_blocks = get_valid_blocks(sbi, segno, 0);
1592 		if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1593 			continue;
1594 		mutex_lock(&dirty_i->seglist_lock);
1595 		__locate_dirty_segment(sbi, segno, DIRTY);
1596 		mutex_unlock(&dirty_i->seglist_lock);
1597 	}
1598 }
1599 
1600 static int init_victim_secmap(struct f2fs_sb_info *sbi)
1601 {
1602 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1603 	unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1604 
1605 	dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1606 	if (!dirty_i->victim_secmap)
1607 		return -ENOMEM;
1608 	return 0;
1609 }
1610 
1611 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1612 {
1613 	struct dirty_seglist_info *dirty_i;
1614 	unsigned int bitmap_size, i;
1615 
1616 	/* allocate memory for dirty segments list information */
1617 	dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1618 	if (!dirty_i)
1619 		return -ENOMEM;
1620 
1621 	SM_I(sbi)->dirty_info = dirty_i;
1622 	mutex_init(&dirty_i->seglist_lock);
1623 
1624 	bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1625 
1626 	for (i = 0; i < NR_DIRTY_TYPE; i++) {
1627 		dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1628 		if (!dirty_i->dirty_segmap[i])
1629 			return -ENOMEM;
1630 	}
1631 
1632 	init_dirty_segmap(sbi);
1633 	return init_victim_secmap(sbi);
1634 }
1635 
1636 /*
1637  * Update min, max modified time for cost-benefit GC algorithm
1638  */
1639 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1640 {
1641 	struct sit_info *sit_i = SIT_I(sbi);
1642 	unsigned int segno;
1643 
1644 	mutex_lock(&sit_i->sentry_lock);
1645 
1646 	sit_i->min_mtime = LLONG_MAX;
1647 
1648 	for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1649 		unsigned int i;
1650 		unsigned long long mtime = 0;
1651 
1652 		for (i = 0; i < sbi->segs_per_sec; i++)
1653 			mtime += get_seg_entry(sbi, segno + i)->mtime;
1654 
1655 		mtime = div_u64(mtime, sbi->segs_per_sec);
1656 
1657 		if (sit_i->min_mtime > mtime)
1658 			sit_i->min_mtime = mtime;
1659 	}
1660 	sit_i->max_mtime = get_mtime(sbi);
1661 	mutex_unlock(&sit_i->sentry_lock);
1662 }
1663 
1664 int build_segment_manager(struct f2fs_sb_info *sbi)
1665 {
1666 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1667 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1668 	struct f2fs_sm_info *sm_info;
1669 	int err;
1670 
1671 	sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1672 	if (!sm_info)
1673 		return -ENOMEM;
1674 
1675 	/* init sm info */
1676 	sbi->sm_info = sm_info;
1677 	INIT_LIST_HEAD(&sm_info->wblist_head);
1678 	spin_lock_init(&sm_info->wblist_lock);
1679 	sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1680 	sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1681 	sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1682 	sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1683 	sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1684 	sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1685 	sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1686 
1687 	err = build_sit_info(sbi);
1688 	if (err)
1689 		return err;
1690 	err = build_free_segmap(sbi);
1691 	if (err)
1692 		return err;
1693 	err = build_curseg(sbi);
1694 	if (err)
1695 		return err;
1696 
1697 	/* reinit free segmap based on SIT */
1698 	build_sit_entries(sbi);
1699 
1700 	init_free_segmap(sbi);
1701 	err = build_dirty_segmap(sbi);
1702 	if (err)
1703 		return err;
1704 
1705 	init_min_max_mtime(sbi);
1706 	return 0;
1707 }
1708 
1709 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1710 		enum dirty_type dirty_type)
1711 {
1712 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1713 
1714 	mutex_lock(&dirty_i->seglist_lock);
1715 	kfree(dirty_i->dirty_segmap[dirty_type]);
1716 	dirty_i->nr_dirty[dirty_type] = 0;
1717 	mutex_unlock(&dirty_i->seglist_lock);
1718 }
1719 
1720 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1721 {
1722 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1723 	kfree(dirty_i->victim_secmap);
1724 }
1725 
1726 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1727 {
1728 	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1729 	int i;
1730 
1731 	if (!dirty_i)
1732 		return;
1733 
1734 	/* discard pre-free/dirty segments list */
1735 	for (i = 0; i < NR_DIRTY_TYPE; i++)
1736 		discard_dirty_segmap(sbi, i);
1737 
1738 	destroy_victim_secmap(sbi);
1739 	SM_I(sbi)->dirty_info = NULL;
1740 	kfree(dirty_i);
1741 }
1742 
1743 static void destroy_curseg(struct f2fs_sb_info *sbi)
1744 {
1745 	struct curseg_info *array = SM_I(sbi)->curseg_array;
1746 	int i;
1747 
1748 	if (!array)
1749 		return;
1750 	SM_I(sbi)->curseg_array = NULL;
1751 	for (i = 0; i < NR_CURSEG_TYPE; i++)
1752 		kfree(array[i].sum_blk);
1753 	kfree(array);
1754 }
1755 
1756 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1757 {
1758 	struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1759 	if (!free_i)
1760 		return;
1761 	SM_I(sbi)->free_info = NULL;
1762 	kfree(free_i->free_segmap);
1763 	kfree(free_i->free_secmap);
1764 	kfree(free_i);
1765 }
1766 
1767 static void destroy_sit_info(struct f2fs_sb_info *sbi)
1768 {
1769 	struct sit_info *sit_i = SIT_I(sbi);
1770 	unsigned int start;
1771 
1772 	if (!sit_i)
1773 		return;
1774 
1775 	if (sit_i->sentries) {
1776 		for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1777 			kfree(sit_i->sentries[start].cur_valid_map);
1778 			kfree(sit_i->sentries[start].ckpt_valid_map);
1779 		}
1780 	}
1781 	vfree(sit_i->sentries);
1782 	vfree(sit_i->sec_entries);
1783 	kfree(sit_i->dirty_sentries_bitmap);
1784 
1785 	SM_I(sbi)->sit_info = NULL;
1786 	kfree(sit_i->sit_bitmap);
1787 	kfree(sit_i);
1788 }
1789 
1790 void destroy_segment_manager(struct f2fs_sb_info *sbi)
1791 {
1792 	struct f2fs_sm_info *sm_info = SM_I(sbi);
1793 	destroy_dirty_segmap(sbi);
1794 	destroy_curseg(sbi);
1795 	destroy_free_segmap(sbi);
1796 	destroy_sit_info(sbi);
1797 	sbi->sm_info = NULL;
1798 	kfree(sm_info);
1799 }
1800