1762 lines · 45.9 KB · C
RawDownload
/* * fs/f2fs/segment.c * * Copyright (c) 2012 Samsung Electronics Co., Ltd. *             http://www.samsung.com/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/fs.h>#include <linux/f2fs_fs.h>#include <linux/bio.h>#include <linux/blkdev.h>#include <linux/prefetch.h>#include <linux/vmalloc.h> #include "f2fs.h"#include "segment.h"#include "node.h"#include <trace/events/f2fs.h> /* * This function balances dirty node and dentry pages. * In addition, it controls garbage collection. */void f2fs_balance_fs(struct f2fs_sb_info *sbi){	/*	 * We should do GC or end up with checkpoint, if there are so many dirty	 * dir/node pages without enough free segments.	 */	if (has_not_enough_free_secs(sbi, 0)) {		mutex_lock(&sbi->gc_mutex);		f2fs_gc(sbi);	}} void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi){	/* check the # of cached NAT entries and prefree segments */	if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) ||				excess_prefree_segs(sbi))		f2fs_sync_fs(sbi->sb, true);} static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,		enum dirty_type dirty_type){	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 	/* need not be added */	if (IS_CURSEG(sbi, segno))		return; 	if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))		dirty_i->nr_dirty[dirty_type]++; 	if (dirty_type == DIRTY) {		struct seg_entry *sentry = get_seg_entry(sbi, segno);		enum dirty_type t = sentry->type; 		if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))			dirty_i->nr_dirty[t]++;	}} static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,		enum dirty_type dirty_type){	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 	if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))		dirty_i->nr_dirty[dirty_type]--; 	if (dirty_type == DIRTY) {		struct seg_entry *sentry = get_seg_entry(sbi, segno);		enum dirty_type t = sentry->type; 		if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))			dirty_i->nr_dirty[t]--; 		if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)			clear_bit(GET_SECNO(sbi, segno),						dirty_i->victim_secmap);	}} /* * Should not occur error such as -ENOMEM. * Adding dirty entry into seglist is not critical operation. * If a given segment is one of current working segments, it won't be added. */static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno){	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);	unsigned short valid_blocks; 	if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))		return; 	mutex_lock(&dirty_i->seglist_lock); 	valid_blocks = get_valid_blocks(sbi, segno, 0); 	if (valid_blocks == 0) {		__locate_dirty_segment(sbi, segno, PRE);		__remove_dirty_segment(sbi, segno, DIRTY);	} else if (valid_blocks < sbi->blocks_per_seg) {		__locate_dirty_segment(sbi, segno, DIRTY);	} else {		/* Recovery routine with SSR needs this */		__remove_dirty_segment(sbi, segno, DIRTY);	} 	mutex_unlock(&dirty_i->seglist_lock);} /* * Should call clear_prefree_segments after checkpoint is done. */static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi){	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);	unsigned int segno = -1;	unsigned int total_segs = TOTAL_SEGS(sbi); 	mutex_lock(&dirty_i->seglist_lock);	while (1) {		segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,				segno + 1);		if (segno >= total_segs)			break;		__set_test_and_free(sbi, segno);	}	mutex_unlock(&dirty_i->seglist_lock);} void clear_prefree_segments(struct f2fs_sb_info *sbi){	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);	unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];	unsigned int total_segs = TOTAL_SEGS(sbi);	unsigned int start = 0, end = -1; 	mutex_lock(&dirty_i->seglist_lock); 	while (1) {		int i;		start = find_next_bit(prefree_map, total_segs, end + 1);		if (start >= total_segs)			break;		end = find_next_zero_bit(prefree_map, total_segs, start + 1); 		for (i = start; i < end; i++)			clear_bit(i, prefree_map); 		dirty_i->nr_dirty[PRE] -= end - start; 		if (!test_opt(sbi, DISCARD))			continue; 		blkdev_issue_discard(sbi->sb->s_bdev,				START_BLOCK(sbi, start) <<				sbi->log_sectors_per_block,				(1 << (sbi->log_sectors_per_block +				sbi->log_blocks_per_seg)) * (end - start),				GFP_NOFS, 0);	}	mutex_unlock(&dirty_i->seglist_lock);} static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno){	struct sit_info *sit_i = SIT_I(sbi);	if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))		sit_i->dirty_sentries++;} static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,					unsigned int segno, int modified){	struct seg_entry *se = get_seg_entry(sbi, segno);	se->type = type;	if (modified)		__mark_sit_entry_dirty(sbi, segno);} static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del){	struct seg_entry *se;	unsigned int segno, offset;	long int new_vblocks; 	segno = GET_SEGNO(sbi, blkaddr); 	se = get_seg_entry(sbi, segno);	new_vblocks = se->valid_blocks + del;	offset = GET_SEGOFF_FROM_SEG0(sbi, blkaddr) & (sbi->blocks_per_seg - 1); 	f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||				(new_vblocks > sbi->blocks_per_seg))); 	se->valid_blocks = new_vblocks;	se->mtime = get_mtime(sbi);	SIT_I(sbi)->max_mtime = se->mtime; 	/* Update valid block bitmap */	if (del > 0) {		if (f2fs_set_bit(offset, se->cur_valid_map))			BUG();	} else {		if (!f2fs_clear_bit(offset, se->cur_valid_map))			BUG();	}	if (!f2fs_test_bit(offset, se->ckpt_valid_map))		se->ckpt_valid_blocks += del; 	__mark_sit_entry_dirty(sbi, segno); 	/* update total number of valid blocks to be written in ckpt area */	SIT_I(sbi)->written_valid_blocks += del; 	if (sbi->segs_per_sec > 1)		get_sec_entry(sbi, segno)->valid_blocks += del;} static void refresh_sit_entry(struct f2fs_sb_info *sbi,			block_t old_blkaddr, block_t new_blkaddr){	update_sit_entry(sbi, new_blkaddr, 1);	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)		update_sit_entry(sbi, old_blkaddr, -1);} void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr){	unsigned int segno = GET_SEGNO(sbi, addr);	struct sit_info *sit_i = SIT_I(sbi); 	f2fs_bug_on(addr == NULL_ADDR);	if (addr == NEW_ADDR)		return; 	/* add it into sit main buffer */	mutex_lock(&sit_i->sentry_lock); 	update_sit_entry(sbi, addr, -1); 	/* add it into dirty seglist */	locate_dirty_segment(sbi, segno); 	mutex_unlock(&sit_i->sentry_lock);} /* * This function should be resided under the curseg_mutex lock */static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,					struct f2fs_summary *sum){	struct curseg_info *curseg = CURSEG_I(sbi, type);	void *addr = curseg->sum_blk;	addr += curseg->next_blkoff * sizeof(struct f2fs_summary);	memcpy(addr, sum, sizeof(struct f2fs_summary));} /* * Calculate the number of current summary pages for writing */int npages_for_summary_flush(struct f2fs_sb_info *sbi){	int valid_sum_count = 0;	int i, sum_in_page; 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {		if (sbi->ckpt->alloc_type[i] == SSR)			valid_sum_count += sbi->blocks_per_seg;		else			valid_sum_count += curseg_blkoff(sbi, i);	} 	sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -			SUM_FOOTER_SIZE) / SUMMARY_SIZE;	if (valid_sum_count <= sum_in_page)		return 1;	else if ((valid_sum_count - sum_in_page) <=		(PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)		return 2;	return 3;} /* * Caller should put this summary page */struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno){	return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));} static void write_sum_page(struct f2fs_sb_info *sbi,			struct f2fs_summary_block *sum_blk, block_t blk_addr){	struct page *page = grab_meta_page(sbi, blk_addr);	void *kaddr = page_address(page);	memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);	set_page_dirty(page);	f2fs_put_page(page, 1);} static int is_next_segment_free(struct f2fs_sb_info *sbi, int type){	struct curseg_info *curseg = CURSEG_I(sbi, type);	unsigned int segno = curseg->segno + 1;	struct free_segmap_info *free_i = FREE_I(sbi); 	if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)		return !test_bit(segno, free_i->free_segmap);	return 0;} /* * Find a new segment from the free segments bitmap to right order * This function should be returned with success, otherwise BUG */static void get_new_segment(struct f2fs_sb_info *sbi,			unsigned int *newseg, bool new_sec, int dir){	struct free_segmap_info *free_i = FREE_I(sbi);	unsigned int segno, secno, zoneno;	unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;	unsigned int hint = *newseg / sbi->segs_per_sec;	unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);	unsigned int left_start = hint;	bool init = true;	int go_left = 0;	int i; 	write_lock(&free_i->segmap_lock); 	if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {		segno = find_next_zero_bit(free_i->free_segmap,					TOTAL_SEGS(sbi), *newseg + 1);		if (segno - *newseg < sbi->segs_per_sec -					(*newseg % sbi->segs_per_sec))			goto got_it;	}find_other_zone:	secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);	if (secno >= TOTAL_SECS(sbi)) {		if (dir == ALLOC_RIGHT) {			secno = find_next_zero_bit(free_i->free_secmap,							TOTAL_SECS(sbi), 0);			f2fs_bug_on(secno >= TOTAL_SECS(sbi));		} else {			go_left = 1;			left_start = hint - 1;		}	}	if (go_left == 0)		goto skip_left; 	while (test_bit(left_start, free_i->free_secmap)) {		if (left_start > 0) {			left_start--;			continue;		}		left_start = find_next_zero_bit(free_i->free_secmap,							TOTAL_SECS(sbi), 0);		f2fs_bug_on(left_start >= TOTAL_SECS(sbi));		break;	}	secno = left_start;skip_left:	hint = secno;	segno = secno * sbi->segs_per_sec;	zoneno = secno / sbi->secs_per_zone; 	/* give up on finding another zone */	if (!init)		goto got_it;	if (sbi->secs_per_zone == 1)		goto got_it;	if (zoneno == old_zoneno)		goto got_it;	if (dir == ALLOC_LEFT) {		if (!go_left && zoneno + 1 >= total_zones)			goto got_it;		if (go_left && zoneno == 0)			goto got_it;	}	for (i = 0; i < NR_CURSEG_TYPE; i++)		if (CURSEG_I(sbi, i)->zone == zoneno)			break; 	if (i < NR_CURSEG_TYPE) {		/* zone is in user, try another */		if (go_left)			hint = zoneno * sbi->secs_per_zone - 1;		else if (zoneno + 1 >= total_zones)			hint = 0;		else			hint = (zoneno + 1) * sbi->secs_per_zone;		init = false;		goto find_other_zone;	}got_it:	/* set it as dirty segment in free segmap */	f2fs_bug_on(test_bit(segno, free_i->free_segmap));	__set_inuse(sbi, segno);	*newseg = segno;	write_unlock(&free_i->segmap_lock);} static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified){	struct curseg_info *curseg = CURSEG_I(sbi, type);	struct summary_footer *sum_footer; 	curseg->segno = curseg->next_segno;	curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);	curseg->next_blkoff = 0;	curseg->next_segno = NULL_SEGNO; 	sum_footer = &(curseg->sum_blk->footer);	memset(sum_footer, 0, sizeof(struct summary_footer));	if (IS_DATASEG(type))		SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);	if (IS_NODESEG(type))		SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);	__set_sit_entry_type(sbi, type, curseg->segno, modified);} /* * Allocate a current working segment. * This function always allocates a free segment in LFS manner. */static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec){	struct curseg_info *curseg = CURSEG_I(sbi, type);	unsigned int segno = curseg->segno;	int dir = ALLOC_LEFT; 	write_sum_page(sbi, curseg->sum_blk,				GET_SUM_BLOCK(sbi, segno));	if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)		dir = ALLOC_RIGHT; 	if (test_opt(sbi, NOHEAP))		dir = ALLOC_RIGHT; 	get_new_segment(sbi, &segno, new_sec, dir);	curseg->next_segno = segno;	reset_curseg(sbi, type, 1);	curseg->alloc_type = LFS;} static void __next_free_blkoff(struct f2fs_sb_info *sbi,			struct curseg_info *seg, block_t start){	struct seg_entry *se = get_seg_entry(sbi, seg->segno);	block_t ofs;	for (ofs = start; ofs < sbi->blocks_per_seg; ofs++) {		if (!f2fs_test_bit(ofs, se->ckpt_valid_map)			&& !f2fs_test_bit(ofs, se->cur_valid_map))			break;	}	seg->next_blkoff = ofs;} /* * If a segment is written by LFS manner, next block offset is just obtained * by increasing the current block offset. However, if a segment is written by * SSR manner, next block offset obtained by calling __next_free_blkoff */static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,				struct curseg_info *seg){	if (seg->alloc_type == SSR)		__next_free_blkoff(sbi, seg, seg->next_blkoff + 1);	else		seg->next_blkoff++;} /* * This function always allocates a used segment (from dirty seglist) by SSR * manner, so it should recover the existing segment information of valid blocks */static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse){	struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);	struct curseg_info *curseg = CURSEG_I(sbi, type);	unsigned int new_segno = curseg->next_segno;	struct f2fs_summary_block *sum_node;	struct page *sum_page; 	write_sum_page(sbi, curseg->sum_blk,				GET_SUM_BLOCK(sbi, curseg->segno));	__set_test_and_inuse(sbi, new_segno); 

Lines 1–500 of 1762.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t