segment.c revision 6ec178dac6768204a6edf70f4a53d40b691c12b4
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 */
27void 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
39static 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
70static 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 */
97void 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 */
126static 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
144void 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
173static 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
180static 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
189static 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
228static 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
236void 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 */
259static 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 */
272int 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 */
299struct 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
304static 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
314static 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;
334next:
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
364static 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 */
379static 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	}
401find_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;
427skip_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	}
460got_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
468static 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 */
491static 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
511static 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 */
529static 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 */
542static 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
571static 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 */
592static 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);
614out:
615	sbi->segment_count[curseg->alloc_type]++;
616}
617
618void 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
632static const struct segment_allocation default_salloc_ops = {
633	.allocate_segment = allocate_segment_by_default,
634};
635
636static 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
664struct bio *f2fs_bio_alloc(struct block_device *bdev, int npages)
665{
666	struct bio *bio;
667	struct bio_private *priv;
668retry:
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
682static 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 (sbi->bio[btype]) {
692		struct bio_private *p = sbi->bio[btype]->bi_private;
693		p->sbi = sbi;
694		sbi->bio[btype]->bi_end_io = f2fs_end_io_write;
695
696		trace_f2fs_do_submit_bio(sbi->sb, btype, sync, sbi->bio[btype]);
697
698		if (type == META_FLUSH) {
699			DECLARE_COMPLETION_ONSTACK(wait);
700			p->is_sync = true;
701			p->wait = &wait;
702			submit_bio(rw, sbi->bio[btype]);
703			wait_for_completion(&wait);
704		} else {
705			p->is_sync = false;
706			submit_bio(rw, sbi->bio[btype]);
707		}
708		sbi->bio[btype] = NULL;
709	}
710}
711
712void f2fs_submit_bio(struct f2fs_sb_info *sbi, enum page_type type, bool sync)
713{
714	down_write(&sbi->bio_sem);
715	do_submit_bio(sbi, type, sync);
716	up_write(&sbi->bio_sem);
717}
718
719static void submit_write_page(struct f2fs_sb_info *sbi, struct page *page,
720				block_t blk_addr, enum page_type type)
721{
722	struct block_device *bdev = sbi->sb->s_bdev;
723
724	verify_block_addr(sbi, blk_addr);
725
726	down_write(&sbi->bio_sem);
727
728	inc_page_count(sbi, F2FS_WRITEBACK);
729
730	if (sbi->bio[type] && sbi->last_block_in_bio[type] != blk_addr - 1)
731		do_submit_bio(sbi, type, false);
732alloc_new:
733	if (sbi->bio[type] == NULL) {
734		sbi->bio[type] = f2fs_bio_alloc(bdev, bio_get_nr_vecs(bdev));
735		sbi->bio[type]->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
736		/*
737		 * The end_io will be assigned at the sumbission phase.
738		 * Until then, let bio_add_page() merge consecutive IOs as much
739		 * as possible.
740		 */
741	}
742
743	if (bio_add_page(sbi->bio[type], page, PAGE_CACHE_SIZE, 0) <
744							PAGE_CACHE_SIZE) {
745		do_submit_bio(sbi, type, false);
746		goto alloc_new;
747	}
748
749	sbi->last_block_in_bio[type] = blk_addr;
750
751	up_write(&sbi->bio_sem);
752	trace_f2fs_submit_write_page(page, blk_addr, type);
753}
754
755static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
756{
757	struct curseg_info *curseg = CURSEG_I(sbi, type);
758	if (curseg->next_blkoff < sbi->blocks_per_seg)
759		return true;
760	return false;
761}
762
763static int __get_segment_type_2(struct page *page, enum page_type p_type)
764{
765	if (p_type == DATA)
766		return CURSEG_HOT_DATA;
767	else
768		return CURSEG_HOT_NODE;
769}
770
771static int __get_segment_type_4(struct page *page, enum page_type p_type)
772{
773	if (p_type == DATA) {
774		struct inode *inode = page->mapping->host;
775
776		if (S_ISDIR(inode->i_mode))
777			return CURSEG_HOT_DATA;
778		else
779			return CURSEG_COLD_DATA;
780	} else {
781		if (IS_DNODE(page) && !is_cold_node(page))
782			return CURSEG_HOT_NODE;
783		else
784			return CURSEG_COLD_NODE;
785	}
786}
787
788static int __get_segment_type_6(struct page *page, enum page_type p_type)
789{
790	if (p_type == DATA) {
791		struct inode *inode = page->mapping->host;
792
793		if (S_ISDIR(inode->i_mode))
794			return CURSEG_HOT_DATA;
795		else if (is_cold_data(page) || is_cold_file(inode))
796			return CURSEG_COLD_DATA;
797		else
798			return CURSEG_WARM_DATA;
799	} else {
800		if (IS_DNODE(page))
801			return is_cold_node(page) ? CURSEG_WARM_NODE :
802						CURSEG_HOT_NODE;
803		else
804			return CURSEG_COLD_NODE;
805	}
806}
807
808static int __get_segment_type(struct page *page, enum page_type p_type)
809{
810	struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
811	switch (sbi->active_logs) {
812	case 2:
813		return __get_segment_type_2(page, p_type);
814	case 4:
815		return __get_segment_type_4(page, p_type);
816	}
817	/* NR_CURSEG_TYPE(6) logs by default */
818	BUG_ON(sbi->active_logs != NR_CURSEG_TYPE);
819	return __get_segment_type_6(page, p_type);
820}
821
822static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
823			block_t old_blkaddr, block_t *new_blkaddr,
824			struct f2fs_summary *sum, enum page_type p_type)
825{
826	struct sit_info *sit_i = SIT_I(sbi);
827	struct curseg_info *curseg;
828	unsigned int old_cursegno;
829	int type;
830
831	type = __get_segment_type(page, p_type);
832	curseg = CURSEG_I(sbi, type);
833
834	mutex_lock(&curseg->curseg_mutex);
835
836	*new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
837	old_cursegno = curseg->segno;
838
839	/*
840	 * __add_sum_entry should be resided under the curseg_mutex
841	 * because, this function updates a summary entry in the
842	 * current summary block.
843	 */
844	__add_sum_entry(sbi, type, sum, curseg->next_blkoff);
845
846	mutex_lock(&sit_i->sentry_lock);
847	__refresh_next_blkoff(sbi, curseg);
848	sbi->block_count[curseg->alloc_type]++;
849
850	/*
851	 * SIT information should be updated before segment allocation,
852	 * since SSR needs latest valid block information.
853	 */
854	refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
855
856	if (!__has_curseg_space(sbi, type))
857		sit_i->s_ops->allocate_segment(sbi, type, false);
858
859	locate_dirty_segment(sbi, old_cursegno);
860	locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
861	mutex_unlock(&sit_i->sentry_lock);
862
863	if (p_type == NODE)
864		fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
865
866	/* writeout dirty page into bdev */
867	submit_write_page(sbi, page, *new_blkaddr, p_type);
868
869	mutex_unlock(&curseg->curseg_mutex);
870}
871
872void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
873{
874	set_page_writeback(page);
875	submit_write_page(sbi, page, page->index, META);
876}
877
878void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
879		unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
880{
881	struct f2fs_summary sum;
882	set_summary(&sum, nid, 0, 0);
883	do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, NODE);
884}
885
886void write_data_page(struct inode *inode, struct page *page,
887		struct dnode_of_data *dn, block_t old_blkaddr,
888		block_t *new_blkaddr)
889{
890	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
891	struct f2fs_summary sum;
892	struct node_info ni;
893
894	BUG_ON(old_blkaddr == NULL_ADDR);
895	get_node_info(sbi, dn->nid, &ni);
896	set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
897
898	do_write_page(sbi, page, old_blkaddr,
899			new_blkaddr, &sum, DATA);
900}
901
902void rewrite_data_page(struct f2fs_sb_info *sbi, struct page *page,
903					block_t old_blk_addr)
904{
905	submit_write_page(sbi, page, old_blk_addr, DATA);
906}
907
908void recover_data_page(struct f2fs_sb_info *sbi,
909			struct page *page, struct f2fs_summary *sum,
910			block_t old_blkaddr, block_t new_blkaddr)
911{
912	struct sit_info *sit_i = SIT_I(sbi);
913	struct curseg_info *curseg;
914	unsigned int segno, old_cursegno;
915	struct seg_entry *se;
916	int type;
917
918	segno = GET_SEGNO(sbi, new_blkaddr);
919	se = get_seg_entry(sbi, segno);
920	type = se->type;
921
922	if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
923		if (old_blkaddr == NULL_ADDR)
924			type = CURSEG_COLD_DATA;
925		else
926			type = CURSEG_WARM_DATA;
927	}
928	curseg = CURSEG_I(sbi, type);
929
930	mutex_lock(&curseg->curseg_mutex);
931	mutex_lock(&sit_i->sentry_lock);
932
933	old_cursegno = curseg->segno;
934
935	/* change the current segment */
936	if (segno != curseg->segno) {
937		curseg->next_segno = segno;
938		change_curseg(sbi, type, true);
939	}
940
941	curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
942					(sbi->blocks_per_seg - 1);
943	__add_sum_entry(sbi, type, sum, curseg->next_blkoff);
944
945	refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
946
947	locate_dirty_segment(sbi, old_cursegno);
948	locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
949
950	mutex_unlock(&sit_i->sentry_lock);
951	mutex_unlock(&curseg->curseg_mutex);
952}
953
954void rewrite_node_page(struct f2fs_sb_info *sbi,
955			struct page *page, struct f2fs_summary *sum,
956			block_t old_blkaddr, block_t new_blkaddr)
957{
958	struct sit_info *sit_i = SIT_I(sbi);
959	int type = CURSEG_WARM_NODE;
960	struct curseg_info *curseg;
961	unsigned int segno, old_cursegno;
962	block_t next_blkaddr = next_blkaddr_of_node(page);
963	unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
964
965	curseg = CURSEG_I(sbi, type);
966
967	mutex_lock(&curseg->curseg_mutex);
968	mutex_lock(&sit_i->sentry_lock);
969
970	segno = GET_SEGNO(sbi, new_blkaddr);
971	old_cursegno = curseg->segno;
972
973	/* change the current segment */
974	if (segno != curseg->segno) {
975		curseg->next_segno = segno;
976		change_curseg(sbi, type, true);
977	}
978	curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, new_blkaddr) &
979					(sbi->blocks_per_seg - 1);
980	__add_sum_entry(sbi, type, sum, curseg->next_blkoff);
981
982	/* change the current log to the next block addr in advance */
983	if (next_segno != segno) {
984		curseg->next_segno = next_segno;
985		change_curseg(sbi, type, true);
986	}
987	curseg->next_blkoff = GET_SEGOFF_FROM_SEG0(sbi, next_blkaddr) &
988					(sbi->blocks_per_seg - 1);
989
990	/* rewrite node page */
991	set_page_writeback(page);
992	submit_write_page(sbi, page, new_blkaddr, NODE);
993	f2fs_submit_bio(sbi, NODE, true);
994	refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
995
996	locate_dirty_segment(sbi, old_cursegno);
997	locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
998
999	mutex_unlock(&sit_i->sentry_lock);
1000	mutex_unlock(&curseg->curseg_mutex);
1001}
1002
1003static int read_compacted_summaries(struct f2fs_sb_info *sbi)
1004{
1005	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1006	struct curseg_info *seg_i;
1007	unsigned char *kaddr;
1008	struct page *page;
1009	block_t start;
1010	int i, j, offset;
1011
1012	start = start_sum_block(sbi);
1013
1014	page = get_meta_page(sbi, start++);
1015	kaddr = (unsigned char *)page_address(page);
1016
1017	/* Step 1: restore nat cache */
1018	seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1019	memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
1020
1021	/* Step 2: restore sit cache */
1022	seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1023	memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
1024						SUM_JOURNAL_SIZE);
1025	offset = 2 * SUM_JOURNAL_SIZE;
1026
1027	/* Step 3: restore summary entries */
1028	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1029		unsigned short blk_off;
1030		unsigned int segno;
1031
1032		seg_i = CURSEG_I(sbi, i);
1033		segno = le32_to_cpu(ckpt->cur_data_segno[i]);
1034		blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
1035		seg_i->next_segno = segno;
1036		reset_curseg(sbi, i, 0);
1037		seg_i->alloc_type = ckpt->alloc_type[i];
1038		seg_i->next_blkoff = blk_off;
1039
1040		if (seg_i->alloc_type == SSR)
1041			blk_off = sbi->blocks_per_seg;
1042
1043		for (j = 0; j < blk_off; j++) {
1044			struct f2fs_summary *s;
1045			s = (struct f2fs_summary *)(kaddr + offset);
1046			seg_i->sum_blk->entries[j] = *s;
1047			offset += SUMMARY_SIZE;
1048			if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1049						SUM_FOOTER_SIZE)
1050				continue;
1051
1052			f2fs_put_page(page, 1);
1053			page = NULL;
1054
1055			page = get_meta_page(sbi, start++);
1056			kaddr = (unsigned char *)page_address(page);
1057			offset = 0;
1058		}
1059	}
1060	f2fs_put_page(page, 1);
1061	return 0;
1062}
1063
1064static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
1065{
1066	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1067	struct f2fs_summary_block *sum;
1068	struct curseg_info *curseg;
1069	struct page *new;
1070	unsigned short blk_off;
1071	unsigned int segno = 0;
1072	block_t blk_addr = 0;
1073
1074	/* get segment number and block addr */
1075	if (IS_DATASEG(type)) {
1076		segno = le32_to_cpu(ckpt->cur_data_segno[type]);
1077		blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
1078							CURSEG_HOT_DATA]);
1079		if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1080			blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
1081		else
1082			blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
1083	} else {
1084		segno = le32_to_cpu(ckpt->cur_node_segno[type -
1085							CURSEG_HOT_NODE]);
1086		blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
1087							CURSEG_HOT_NODE]);
1088		if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
1089			blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
1090							type - CURSEG_HOT_NODE);
1091		else
1092			blk_addr = GET_SUM_BLOCK(sbi, segno);
1093	}
1094
1095	new = get_meta_page(sbi, blk_addr);
1096	sum = (struct f2fs_summary_block *)page_address(new);
1097
1098	if (IS_NODESEG(type)) {
1099		if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
1100			struct f2fs_summary *ns = &sum->entries[0];
1101			int i;
1102			for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
1103				ns->version = 0;
1104				ns->ofs_in_node = 0;
1105			}
1106		} else {
1107			if (restore_node_summary(sbi, segno, sum)) {
1108				f2fs_put_page(new, 1);
1109				return -EINVAL;
1110			}
1111		}
1112	}
1113
1114	/* set uncompleted segment to curseg */
1115	curseg = CURSEG_I(sbi, type);
1116	mutex_lock(&curseg->curseg_mutex);
1117	memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
1118	curseg->next_segno = segno;
1119	reset_curseg(sbi, type, 0);
1120	curseg->alloc_type = ckpt->alloc_type[type];
1121	curseg->next_blkoff = blk_off;
1122	mutex_unlock(&curseg->curseg_mutex);
1123	f2fs_put_page(new, 1);
1124	return 0;
1125}
1126
1127static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
1128{
1129	int type = CURSEG_HOT_DATA;
1130
1131	if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
1132		/* restore for compacted data summary */
1133		if (read_compacted_summaries(sbi))
1134			return -EINVAL;
1135		type = CURSEG_HOT_NODE;
1136	}
1137
1138	for (; type <= CURSEG_COLD_NODE; type++)
1139		if (read_normal_summaries(sbi, type))
1140			return -EINVAL;
1141	return 0;
1142}
1143
1144static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
1145{
1146	struct page *page;
1147	unsigned char *kaddr;
1148	struct f2fs_summary *summary;
1149	struct curseg_info *seg_i;
1150	int written_size = 0;
1151	int i, j;
1152
1153	page = grab_meta_page(sbi, blkaddr++);
1154	kaddr = (unsigned char *)page_address(page);
1155
1156	/* Step 1: write nat cache */
1157	seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
1158	memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
1159	written_size += SUM_JOURNAL_SIZE;
1160
1161	/* Step 2: write sit cache */
1162	seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
1163	memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
1164						SUM_JOURNAL_SIZE);
1165	written_size += SUM_JOURNAL_SIZE;
1166
1167	set_page_dirty(page);
1168
1169	/* Step 3: write summary entries */
1170	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
1171		unsigned short blkoff;
1172		seg_i = CURSEG_I(sbi, i);
1173		if (sbi->ckpt->alloc_type[i] == SSR)
1174			blkoff = sbi->blocks_per_seg;
1175		else
1176			blkoff = curseg_blkoff(sbi, i);
1177
1178		for (j = 0; j < blkoff; j++) {
1179			if (!page) {
1180				page = grab_meta_page(sbi, blkaddr++);
1181				kaddr = (unsigned char *)page_address(page);
1182				written_size = 0;
1183			}
1184			summary = (struct f2fs_summary *)(kaddr + written_size);
1185			*summary = seg_i->sum_blk->entries[j];
1186			written_size += SUMMARY_SIZE;
1187			set_page_dirty(page);
1188
1189			if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
1190							SUM_FOOTER_SIZE)
1191				continue;
1192
1193			f2fs_put_page(page, 1);
1194			page = NULL;
1195		}
1196	}
1197	if (page)
1198		f2fs_put_page(page, 1);
1199}
1200
1201static void write_normal_summaries(struct f2fs_sb_info *sbi,
1202					block_t blkaddr, int type)
1203{
1204	int i, end;
1205	if (IS_DATASEG(type))
1206		end = type + NR_CURSEG_DATA_TYPE;
1207	else
1208		end = type + NR_CURSEG_NODE_TYPE;
1209
1210	for (i = type; i < end; i++) {
1211		struct curseg_info *sum = CURSEG_I(sbi, i);
1212		mutex_lock(&sum->curseg_mutex);
1213		write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
1214		mutex_unlock(&sum->curseg_mutex);
1215	}
1216}
1217
1218void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1219{
1220	if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
1221		write_compacted_summaries(sbi, start_blk);
1222	else
1223		write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
1224}
1225
1226void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
1227{
1228	if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
1229		write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
1230	return;
1231}
1232
1233int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
1234					unsigned int val, int alloc)
1235{
1236	int i;
1237
1238	if (type == NAT_JOURNAL) {
1239		for (i = 0; i < nats_in_cursum(sum); i++) {
1240			if (le32_to_cpu(nid_in_journal(sum, i)) == val)
1241				return i;
1242		}
1243		if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
1244			return update_nats_in_cursum(sum, 1);
1245	} else if (type == SIT_JOURNAL) {
1246		for (i = 0; i < sits_in_cursum(sum); i++)
1247			if (le32_to_cpu(segno_in_journal(sum, i)) == val)
1248				return i;
1249		if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
1250			return update_sits_in_cursum(sum, 1);
1251	}
1252	return -1;
1253}
1254
1255static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
1256					unsigned int segno)
1257{
1258	struct sit_info *sit_i = SIT_I(sbi);
1259	unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
1260	block_t blk_addr = sit_i->sit_base_addr + offset;
1261
1262	check_seg_range(sbi, segno);
1263
1264	/* calculate sit block address */
1265	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
1266		blk_addr += sit_i->sit_blocks;
1267
1268	return get_meta_page(sbi, blk_addr);
1269}
1270
1271static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
1272					unsigned int start)
1273{
1274	struct sit_info *sit_i = SIT_I(sbi);
1275	struct page *src_page, *dst_page;
1276	pgoff_t src_off, dst_off;
1277	void *src_addr, *dst_addr;
1278
1279	src_off = current_sit_addr(sbi, start);
1280	dst_off = next_sit_addr(sbi, src_off);
1281
1282	/* get current sit block page without lock */
1283	src_page = get_meta_page(sbi, src_off);
1284	dst_page = grab_meta_page(sbi, dst_off);
1285	BUG_ON(PageDirty(src_page));
1286
1287	src_addr = page_address(src_page);
1288	dst_addr = page_address(dst_page);
1289	memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
1290
1291	set_page_dirty(dst_page);
1292	f2fs_put_page(src_page, 1);
1293
1294	set_to_next_sit(sit_i, start);
1295
1296	return dst_page;
1297}
1298
1299static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
1300{
1301	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1302	struct f2fs_summary_block *sum = curseg->sum_blk;
1303	int i;
1304
1305	/*
1306	 * If the journal area in the current summary is full of sit entries,
1307	 * all the sit entries will be flushed. Otherwise the sit entries
1308	 * are not able to replace with newly hot sit entries.
1309	 */
1310	if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
1311		for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
1312			unsigned int segno;
1313			segno = le32_to_cpu(segno_in_journal(sum, i));
1314			__mark_sit_entry_dirty(sbi, segno);
1315		}
1316		update_sits_in_cursum(sum, -sits_in_cursum(sum));
1317		return 1;
1318	}
1319	return 0;
1320}
1321
1322/*
1323 * CP calls this function, which flushes SIT entries including sit_journal,
1324 * and moves prefree segs to free segs.
1325 */
1326void flush_sit_entries(struct f2fs_sb_info *sbi)
1327{
1328	struct sit_info *sit_i = SIT_I(sbi);
1329	unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
1330	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1331	struct f2fs_summary_block *sum = curseg->sum_blk;
1332	unsigned long nsegs = TOTAL_SEGS(sbi);
1333	struct page *page = NULL;
1334	struct f2fs_sit_block *raw_sit = NULL;
1335	unsigned int start = 0, end = 0;
1336	unsigned int segno = -1;
1337	bool flushed;
1338
1339	mutex_lock(&curseg->curseg_mutex);
1340	mutex_lock(&sit_i->sentry_lock);
1341
1342	/*
1343	 * "flushed" indicates whether sit entries in journal are flushed
1344	 * to the SIT area or not.
1345	 */
1346	flushed = flush_sits_in_journal(sbi);
1347
1348	while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
1349		struct seg_entry *se = get_seg_entry(sbi, segno);
1350		int sit_offset, offset;
1351
1352		sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
1353
1354		if (flushed)
1355			goto to_sit_page;
1356
1357		offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
1358		if (offset >= 0) {
1359			segno_in_journal(sum, offset) = cpu_to_le32(segno);
1360			seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
1361			goto flush_done;
1362		}
1363to_sit_page:
1364		if (!page || (start > segno) || (segno > end)) {
1365			if (page) {
1366				f2fs_put_page(page, 1);
1367				page = NULL;
1368			}
1369
1370			start = START_SEGNO(sit_i, segno);
1371			end = start + SIT_ENTRY_PER_BLOCK - 1;
1372
1373			/* read sit block that will be updated */
1374			page = get_next_sit_page(sbi, start);
1375			raw_sit = page_address(page);
1376		}
1377
1378		/* udpate entry in SIT block */
1379		seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
1380flush_done:
1381		__clear_bit(segno, bitmap);
1382		sit_i->dirty_sentries--;
1383	}
1384	mutex_unlock(&sit_i->sentry_lock);
1385	mutex_unlock(&curseg->curseg_mutex);
1386
1387	/* writeout last modified SIT block */
1388	f2fs_put_page(page, 1);
1389
1390	set_prefree_as_free_segments(sbi);
1391}
1392
1393static int build_sit_info(struct f2fs_sb_info *sbi)
1394{
1395	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1396	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1397	struct sit_info *sit_i;
1398	unsigned int sit_segs, start;
1399	char *src_bitmap, *dst_bitmap;
1400	unsigned int bitmap_size;
1401
1402	/* allocate memory for SIT information */
1403	sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
1404	if (!sit_i)
1405		return -ENOMEM;
1406
1407	SM_I(sbi)->sit_info = sit_i;
1408
1409	sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
1410	if (!sit_i->sentries)
1411		return -ENOMEM;
1412
1413	bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1414	sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1415	if (!sit_i->dirty_sentries_bitmap)
1416		return -ENOMEM;
1417
1418	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1419		sit_i->sentries[start].cur_valid_map
1420			= kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1421		sit_i->sentries[start].ckpt_valid_map
1422			= kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
1423		if (!sit_i->sentries[start].cur_valid_map
1424				|| !sit_i->sentries[start].ckpt_valid_map)
1425			return -ENOMEM;
1426	}
1427
1428	if (sbi->segs_per_sec > 1) {
1429		sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
1430					sizeof(struct sec_entry));
1431		if (!sit_i->sec_entries)
1432			return -ENOMEM;
1433	}
1434
1435	/* get information related with SIT */
1436	sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
1437
1438	/* setup SIT bitmap from ckeckpoint pack */
1439	bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
1440	src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
1441
1442	dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
1443	if (!dst_bitmap)
1444		return -ENOMEM;
1445
1446	/* init SIT information */
1447	sit_i->s_ops = &default_salloc_ops;
1448
1449	sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
1450	sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
1451	sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
1452	sit_i->sit_bitmap = dst_bitmap;
1453	sit_i->bitmap_size = bitmap_size;
1454	sit_i->dirty_sentries = 0;
1455	sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
1456	sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
1457	sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
1458	mutex_init(&sit_i->sentry_lock);
1459	return 0;
1460}
1461
1462static int build_free_segmap(struct f2fs_sb_info *sbi)
1463{
1464	struct f2fs_sm_info *sm_info = SM_I(sbi);
1465	struct free_segmap_info *free_i;
1466	unsigned int bitmap_size, sec_bitmap_size;
1467
1468	/* allocate memory for free segmap information */
1469	free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
1470	if (!free_i)
1471		return -ENOMEM;
1472
1473	SM_I(sbi)->free_info = free_i;
1474
1475	bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1476	free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
1477	if (!free_i->free_segmap)
1478		return -ENOMEM;
1479
1480	sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1481	free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
1482	if (!free_i->free_secmap)
1483		return -ENOMEM;
1484
1485	/* set all segments as dirty temporarily */
1486	memset(free_i->free_segmap, 0xff, bitmap_size);
1487	memset(free_i->free_secmap, 0xff, sec_bitmap_size);
1488
1489	/* init free segmap information */
1490	free_i->start_segno =
1491		(unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
1492	free_i->free_segments = 0;
1493	free_i->free_sections = 0;
1494	rwlock_init(&free_i->segmap_lock);
1495	return 0;
1496}
1497
1498static int build_curseg(struct f2fs_sb_info *sbi)
1499{
1500	struct curseg_info *array;
1501	int i;
1502
1503	array = kzalloc(sizeof(*array) * NR_CURSEG_TYPE, GFP_KERNEL);
1504	if (!array)
1505		return -ENOMEM;
1506
1507	SM_I(sbi)->curseg_array = array;
1508
1509	for (i = 0; i < NR_CURSEG_TYPE; i++) {
1510		mutex_init(&array[i].curseg_mutex);
1511		array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
1512		if (!array[i].sum_blk)
1513			return -ENOMEM;
1514		array[i].segno = NULL_SEGNO;
1515		array[i].next_blkoff = 0;
1516	}
1517	return restore_curseg_summaries(sbi);
1518}
1519
1520static void build_sit_entries(struct f2fs_sb_info *sbi)
1521{
1522	struct sit_info *sit_i = SIT_I(sbi);
1523	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
1524	struct f2fs_summary_block *sum = curseg->sum_blk;
1525	unsigned int start;
1526
1527	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1528		struct seg_entry *se = &sit_i->sentries[start];
1529		struct f2fs_sit_block *sit_blk;
1530		struct f2fs_sit_entry sit;
1531		struct page *page;
1532		int i;
1533
1534		mutex_lock(&curseg->curseg_mutex);
1535		for (i = 0; i < sits_in_cursum(sum); i++) {
1536			if (le32_to_cpu(segno_in_journal(sum, i)) == start) {
1537				sit = sit_in_journal(sum, i);
1538				mutex_unlock(&curseg->curseg_mutex);
1539				goto got_it;
1540			}
1541		}
1542		mutex_unlock(&curseg->curseg_mutex);
1543		page = get_current_sit_page(sbi, start);
1544		sit_blk = (struct f2fs_sit_block *)page_address(page);
1545		sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
1546		f2fs_put_page(page, 1);
1547got_it:
1548		check_block_count(sbi, start, &sit);
1549		seg_info_from_raw_sit(se, &sit);
1550		if (sbi->segs_per_sec > 1) {
1551			struct sec_entry *e = get_sec_entry(sbi, start);
1552			e->valid_blocks += se->valid_blocks;
1553		}
1554	}
1555}
1556
1557static void init_free_segmap(struct f2fs_sb_info *sbi)
1558{
1559	unsigned int start;
1560	int type;
1561
1562	for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1563		struct seg_entry *sentry = get_seg_entry(sbi, start);
1564		if (!sentry->valid_blocks)
1565			__set_free(sbi, start);
1566	}
1567
1568	/* set use the current segments */
1569	for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
1570		struct curseg_info *curseg_t = CURSEG_I(sbi, type);
1571		__set_test_and_inuse(sbi, curseg_t->segno);
1572	}
1573}
1574
1575static void init_dirty_segmap(struct f2fs_sb_info *sbi)
1576{
1577	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1578	struct free_segmap_info *free_i = FREE_I(sbi);
1579	unsigned int segno = 0, offset = 0;
1580	unsigned short valid_blocks;
1581
1582	while (segno < TOTAL_SEGS(sbi)) {
1583		/* find dirty segment based on free segmap */
1584		segno = find_next_inuse(free_i, TOTAL_SEGS(sbi), offset);
1585		if (segno >= TOTAL_SEGS(sbi))
1586			break;
1587		offset = segno + 1;
1588		valid_blocks = get_valid_blocks(sbi, segno, 0);
1589		if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
1590			continue;
1591		mutex_lock(&dirty_i->seglist_lock);
1592		__locate_dirty_segment(sbi, segno, DIRTY);
1593		mutex_unlock(&dirty_i->seglist_lock);
1594	}
1595}
1596
1597static int init_victim_secmap(struct f2fs_sb_info *sbi)
1598{
1599	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1600	unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
1601
1602	dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
1603	if (!dirty_i->victim_secmap)
1604		return -ENOMEM;
1605	return 0;
1606}
1607
1608static int build_dirty_segmap(struct f2fs_sb_info *sbi)
1609{
1610	struct dirty_seglist_info *dirty_i;
1611	unsigned int bitmap_size, i;
1612
1613	/* allocate memory for dirty segments list information */
1614	dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
1615	if (!dirty_i)
1616		return -ENOMEM;
1617
1618	SM_I(sbi)->dirty_info = dirty_i;
1619	mutex_init(&dirty_i->seglist_lock);
1620
1621	bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
1622
1623	for (i = 0; i < NR_DIRTY_TYPE; i++) {
1624		dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
1625		if (!dirty_i->dirty_segmap[i])
1626			return -ENOMEM;
1627	}
1628
1629	init_dirty_segmap(sbi);
1630	return init_victim_secmap(sbi);
1631}
1632
1633/*
1634 * Update min, max modified time for cost-benefit GC algorithm
1635 */
1636static void init_min_max_mtime(struct f2fs_sb_info *sbi)
1637{
1638	struct sit_info *sit_i = SIT_I(sbi);
1639	unsigned int segno;
1640
1641	mutex_lock(&sit_i->sentry_lock);
1642
1643	sit_i->min_mtime = LLONG_MAX;
1644
1645	for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
1646		unsigned int i;
1647		unsigned long long mtime = 0;
1648
1649		for (i = 0; i < sbi->segs_per_sec; i++)
1650			mtime += get_seg_entry(sbi, segno + i)->mtime;
1651
1652		mtime = div_u64(mtime, sbi->segs_per_sec);
1653
1654		if (sit_i->min_mtime > mtime)
1655			sit_i->min_mtime = mtime;
1656	}
1657	sit_i->max_mtime = get_mtime(sbi);
1658	mutex_unlock(&sit_i->sentry_lock);
1659}
1660
1661int build_segment_manager(struct f2fs_sb_info *sbi)
1662{
1663	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
1664	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1665	struct f2fs_sm_info *sm_info;
1666	int err;
1667
1668	sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
1669	if (!sm_info)
1670		return -ENOMEM;
1671
1672	/* init sm info */
1673	sbi->sm_info = sm_info;
1674	INIT_LIST_HEAD(&sm_info->wblist_head);
1675	spin_lock_init(&sm_info->wblist_lock);
1676	sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
1677	sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
1678	sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
1679	sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
1680	sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
1681	sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
1682	sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
1683
1684	err = build_sit_info(sbi);
1685	if (err)
1686		return err;
1687	err = build_free_segmap(sbi);
1688	if (err)
1689		return err;
1690	err = build_curseg(sbi);
1691	if (err)
1692		return err;
1693
1694	/* reinit free segmap based on SIT */
1695	build_sit_entries(sbi);
1696
1697	init_free_segmap(sbi);
1698	err = build_dirty_segmap(sbi);
1699	if (err)
1700		return err;
1701
1702	init_min_max_mtime(sbi);
1703	return 0;
1704}
1705
1706static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
1707		enum dirty_type dirty_type)
1708{
1709	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1710
1711	mutex_lock(&dirty_i->seglist_lock);
1712	kfree(dirty_i->dirty_segmap[dirty_type]);
1713	dirty_i->nr_dirty[dirty_type] = 0;
1714	mutex_unlock(&dirty_i->seglist_lock);
1715}
1716
1717static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
1718{
1719	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1720	kfree(dirty_i->victim_secmap);
1721}
1722
1723static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
1724{
1725	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1726	int i;
1727
1728	if (!dirty_i)
1729		return;
1730
1731	/* discard pre-free/dirty segments list */
1732	for (i = 0; i < NR_DIRTY_TYPE; i++)
1733		discard_dirty_segmap(sbi, i);
1734
1735	destroy_victim_secmap(sbi);
1736	SM_I(sbi)->dirty_info = NULL;
1737	kfree(dirty_i);
1738}
1739
1740static void destroy_curseg(struct f2fs_sb_info *sbi)
1741{
1742	struct curseg_info *array = SM_I(sbi)->curseg_array;
1743	int i;
1744
1745	if (!array)
1746		return;
1747	SM_I(sbi)->curseg_array = NULL;
1748	for (i = 0; i < NR_CURSEG_TYPE; i++)
1749		kfree(array[i].sum_blk);
1750	kfree(array);
1751}
1752
1753static void destroy_free_segmap(struct f2fs_sb_info *sbi)
1754{
1755	struct free_segmap_info *free_i = SM_I(sbi)->free_info;
1756	if (!free_i)
1757		return;
1758	SM_I(sbi)->free_info = NULL;
1759	kfree(free_i->free_segmap);
1760	kfree(free_i->free_secmap);
1761	kfree(free_i);
1762}
1763
1764static void destroy_sit_info(struct f2fs_sb_info *sbi)
1765{
1766	struct sit_info *sit_i = SIT_I(sbi);
1767	unsigned int start;
1768
1769	if (!sit_i)
1770		return;
1771
1772	if (sit_i->sentries) {
1773		for (start = 0; start < TOTAL_SEGS(sbi); start++) {
1774			kfree(sit_i->sentries[start].cur_valid_map);
1775			kfree(sit_i->sentries[start].ckpt_valid_map);
1776		}
1777	}
1778	vfree(sit_i->sentries);
1779	vfree(sit_i->sec_entries);
1780	kfree(sit_i->dirty_sentries_bitmap);
1781
1782	SM_I(sbi)->sit_info = NULL;
1783	kfree(sit_i->sit_bitmap);
1784	kfree(sit_i);
1785}
1786
1787void destroy_segment_manager(struct f2fs_sb_info *sbi)
1788{
1789	struct f2fs_sm_info *sm_info = SM_I(sbi);
1790	destroy_dirty_segmap(sbi);
1791	destroy_curseg(sbi);
1792	destroy_free_segmap(sbi);
1793	destroy_sit_info(sbi);
1794	sbi->sm_info = NULL;
1795	kfree(sm_info);
1796}
1797