4271 lines · 103.9 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * fs/f2fs/data.c * * Copyright (c) 2012 Samsung Electronics Co., Ltd. *             http://www.samsung.com/ */#include <linux/fs.h>#include <linux/f2fs_fs.h>#include <linux/sched/mm.h>#include <linux/mpage.h>#include <linux/writeback.h>#include <linux/pagevec.h>#include <linux/blkdev.h>#include <linux/bio.h>#include <linux/blk-crypto.h>#include <linux/swap.h>#include <linux/prefetch.h>#include <linux/uio.h>#include <linux/sched/signal.h>#include <linux/fiemap.h>#include <linux/iomap.h> #include "f2fs.h"#include "node.h"#include "segment.h"#include "iostat.h"#include <trace/events/f2fs.h> #define NUM_PREALLOC_POST_READ_CTXS	128 static struct kmem_cache *bio_post_read_ctx_cache;static struct kmem_cache *bio_entry_slab;static mempool_t *bio_post_read_ctx_pool;static struct bio_set f2fs_bioset; #define	F2FS_BIO_POOL_SIZE	NR_CURSEG_TYPE int __init f2fs_init_bioset(void){	return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,					0, BIOSET_NEED_BVECS);} void f2fs_destroy_bioset(void){	bioset_exit(&f2fs_bioset);} bool f2fs_is_cp_guaranteed(struct page *page){	struct address_space *mapping = page->mapping;	struct inode *inode;	struct f2fs_sb_info *sbi; 	if (!mapping)		return false; 	inode = mapping->host;	sbi = F2FS_I_SB(inode); 	if (inode->i_ino == F2FS_META_INO(sbi) ||			inode->i_ino == F2FS_NODE_INO(sbi) ||			S_ISDIR(inode->i_mode))		return true; 	if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||			page_private_gcing(page))		return true;	return false;} static enum count_type __read_io_type(struct page *page){	struct address_space *mapping = page_file_mapping(page); 	if (mapping) {		struct inode *inode = mapping->host;		struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 		if (inode->i_ino == F2FS_META_INO(sbi))			return F2FS_RD_META; 		if (inode->i_ino == F2FS_NODE_INO(sbi))			return F2FS_RD_NODE;	}	return F2FS_RD_DATA;} /* postprocessing steps for read bios */enum bio_post_read_step {#ifdef CONFIG_FS_ENCRYPTION	STEP_DECRYPT	= BIT(0),#else	STEP_DECRYPT	= 0,	/* compile out the decryption-related code */#endif#ifdef CONFIG_F2FS_FS_COMPRESSION	STEP_DECOMPRESS	= BIT(1),#else	STEP_DECOMPRESS	= 0,	/* compile out the decompression-related code */#endif#ifdef CONFIG_FS_VERITY	STEP_VERITY	= BIT(2),#else	STEP_VERITY	= 0,	/* compile out the verity-related code */#endif}; struct bio_post_read_ctx {	struct bio *bio;	struct f2fs_sb_info *sbi;	struct work_struct work;	unsigned int enabled_steps;	/*	 * decompression_attempted keeps track of whether	 * f2fs_end_read_compressed_page() has been called on the pages in the	 * bio that belong to a compressed cluster yet.	 */	bool decompression_attempted;	block_t fs_blkaddr;}; /* * Update and unlock a bio's pages, and free the bio. * * This marks pages up-to-date only if there was no error in the bio (I/O error, * decryption error, or verity error), as indicated by bio->bi_status. * * "Compressed pages" (pagecache pages backed by a compressed cluster on-disk) * aren't marked up-to-date here, as decompression is done on a per-compression- * cluster basis rather than a per-bio basis.  Instead, we only must do two * things for each compressed page here: call f2fs_end_read_compressed_page() * with failed=true if an error occurred before it would have normally gotten * called (i.e., I/O error or decryption error, but *not* verity error), and * release the bio's reference to the decompress_io_ctx of the page's cluster. */static void f2fs_finish_read_bio(struct bio *bio, bool in_task){	struct bio_vec *bv;	struct bvec_iter_all iter_all;	struct bio_post_read_ctx *ctx = bio->bi_private; 	bio_for_each_segment_all(bv, bio, iter_all) {		struct page *page = bv->bv_page; 		if (f2fs_is_compressed_page(page)) {			if (ctx && !ctx->decompression_attempted)				f2fs_end_read_compressed_page(page, true, 0,							in_task);			f2fs_put_page_dic(page, in_task);			continue;		} 		if (bio->bi_status)			ClearPageUptodate(page);		else			SetPageUptodate(page);		dec_page_count(F2FS_P_SB(page), __read_io_type(page));		unlock_page(page);	} 	if (ctx)		mempool_free(ctx, bio_post_read_ctx_pool);	bio_put(bio);} static void f2fs_verify_bio(struct work_struct *work){	struct bio_post_read_ctx *ctx =		container_of(work, struct bio_post_read_ctx, work);	struct bio *bio = ctx->bio;	bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS); 	/*	 * fsverity_verify_bio() may call readahead() again, and while verity	 * will be disabled for this, decryption and/or decompression may still	 * be needed, resulting in another bio_post_read_ctx being allocated.	 * So to prevent deadlocks we need to release the current ctx to the	 * mempool first.  This assumes that verity is the last post-read step.	 */	mempool_free(ctx, bio_post_read_ctx_pool);	bio->bi_private = NULL; 	/*	 * Verify the bio's pages with fs-verity.  Exclude compressed pages,	 * as those were handled separately by f2fs_end_read_compressed_page().	 */	if (may_have_compressed_pages) {		struct bio_vec *bv;		struct bvec_iter_all iter_all; 		bio_for_each_segment_all(bv, bio, iter_all) {			struct page *page = bv->bv_page; 			if (!f2fs_is_compressed_page(page) &&			    !fsverity_verify_page(page)) {				bio->bi_status = BLK_STS_IOERR;				break;			}		}	} else {		fsverity_verify_bio(bio);	} 	f2fs_finish_read_bio(bio, true);} /* * If the bio's data needs to be verified with fs-verity, then enqueue the * verity work for the bio.  Otherwise finish the bio now. * * Note that to avoid deadlocks, the verity work can't be done on the * decryption/decompression workqueue.  This is because verifying the data pages * can involve reading verity metadata pages from the file, and these verity * metadata pages may be encrypted and/or compressed. */static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task){	struct bio_post_read_ctx *ctx = bio->bi_private; 	if (ctx && (ctx->enabled_steps & STEP_VERITY)) {		INIT_WORK(&ctx->work, f2fs_verify_bio);		fsverity_enqueue_verify_work(&ctx->work);	} else {		f2fs_finish_read_bio(bio, in_task);	}} /* * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last * remaining page was read by @ctx->bio. * * Note that a bio may span clusters (even a mix of compressed and uncompressed * clusters) or be for just part of a cluster.  STEP_DECOMPRESS just indicates * that the bio includes at least one compressed page.  The actual decompression * is done on a per-cluster basis, not a per-bio basis. */static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,		bool in_task){	struct bio_vec *bv;	struct bvec_iter_all iter_all;	bool all_compressed = true;	block_t blkaddr = ctx->fs_blkaddr; 	bio_for_each_segment_all(bv, ctx->bio, iter_all) {		struct page *page = bv->bv_page; 		if (f2fs_is_compressed_page(page))			f2fs_end_read_compressed_page(page, false, blkaddr,						      in_task);		else			all_compressed = false; 		blkaddr++;	} 	ctx->decompression_attempted = true; 	/*	 * Optimization: if all the bio's pages are compressed, then scheduling	 * the per-bio verity work is unnecessary, as verity will be fully	 * handled at the compression cluster level.	 */	if (all_compressed)		ctx->enabled_steps &= ~STEP_VERITY;} static void f2fs_post_read_work(struct work_struct *work){	struct bio_post_read_ctx *ctx =		container_of(work, struct bio_post_read_ctx, work);	struct bio *bio = ctx->bio; 	if ((ctx->enabled_steps & STEP_DECRYPT) && !fscrypt_decrypt_bio(bio)) {		f2fs_finish_read_bio(bio, true);		return;	} 	if (ctx->enabled_steps & STEP_DECOMPRESS)		f2fs_handle_step_decompress(ctx, true); 	f2fs_verify_and_finish_bio(bio, true);} static void f2fs_read_end_io(struct bio *bio){	struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));	struct bio_post_read_ctx *ctx;	bool intask = in_task(); 	iostat_update_and_unbind_ctx(bio);	ctx = bio->bi_private; 	if (time_to_inject(sbi, FAULT_READ_IO))		bio->bi_status = BLK_STS_IOERR; 	if (bio->bi_status) {		f2fs_finish_read_bio(bio, intask);		return;	} 	if (ctx) {		unsigned int enabled_steps = ctx->enabled_steps &					(STEP_DECRYPT | STEP_DECOMPRESS); 		/*		 * If we have only decompression step between decompression and		 * decrypt, we don't need post processing for this.		 */		if (enabled_steps == STEP_DECOMPRESS &&				!f2fs_low_mem_mode(sbi)) {			f2fs_handle_step_decompress(ctx, intask);		} else if (enabled_steps) {			INIT_WORK(&ctx->work, f2fs_post_read_work);			queue_work(ctx->sbi->post_read_wq, &ctx->work);			return;		}	} 	f2fs_verify_and_finish_bio(bio, intask);} static void f2fs_write_end_io(struct bio *bio){	struct f2fs_sb_info *sbi;	struct bio_vec *bvec;	struct bvec_iter_all iter_all; 	iostat_update_and_unbind_ctx(bio);	sbi = bio->bi_private; 	if (time_to_inject(sbi, FAULT_WRITE_IO))		bio->bi_status = BLK_STS_IOERR; 	bio_for_each_segment_all(bvec, bio, iter_all) {		struct page *page = bvec->bv_page;		enum count_type type = WB_DATA_TYPE(page, false); 		fscrypt_finalize_bounce_page(&page); #ifdef CONFIG_F2FS_FS_COMPRESSION		if (f2fs_is_compressed_page(page)) {			f2fs_compress_write_end_io(bio, page);			continue;		}#endif 		if (unlikely(bio->bi_status)) {			mapping_set_error(page->mapping, -EIO);			if (type == F2FS_WB_CP_DATA)				f2fs_stop_checkpoint(sbi, true,						STOP_CP_REASON_WRITE_FAIL);		} 		f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&				page_folio(page)->index != nid_of_node(page)); 		dec_page_count(sbi, type);		if (f2fs_in_warm_node_list(sbi, page))			f2fs_del_fsync_node_entry(sbi, page);		clear_page_private_gcing(page);		end_page_writeback(page);	}	if (!get_pages(sbi, F2FS_WB_CP_DATA) &&				wq_has_sleeper(&sbi->cp_wait))		wake_up(&sbi->cp_wait); 	bio_put(bio);} #ifdef CONFIG_BLK_DEV_ZONEDstatic void f2fs_zone_write_end_io(struct bio *bio){	struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private; 	bio->bi_private = io->bi_private;	complete(&io->zone_wait);	f2fs_write_end_io(bio);}#endif struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,		block_t blk_addr, sector_t *sector){	struct block_device *bdev = sbi->sb->s_bdev;	int i; 	if (f2fs_is_multi_device(sbi)) {		for (i = 0; i < sbi->s_ndevs; i++) {			if (FDEV(i).start_blk <= blk_addr &&			    FDEV(i).end_blk >= blk_addr) {				blk_addr -= FDEV(i).start_blk;				bdev = FDEV(i).bdev;				break;			}		}	} 	if (sector)		*sector = SECTOR_FROM_BLOCK(blk_addr);	return bdev;} int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr){	int i; 	if (!f2fs_is_multi_device(sbi))		return 0; 	for (i = 0; i < sbi->s_ndevs; i++)		if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)			return i;	return 0;} static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio){	unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0);	unsigned int fua_flag, meta_flag, io_flag;	blk_opf_t op_flags = 0; 	if (fio->op != REQ_OP_WRITE)		return 0;	if (fio->type == DATA)		io_flag = fio->sbi->data_io_flag;	else if (fio->type == NODE)		io_flag = fio->sbi->node_io_flag;	else		return 0; 	fua_flag = io_flag & temp_mask;	meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask; 	/*	 * data/node io flag bits per temp:	 *      REQ_META     |      REQ_FUA      |	 *    5 |    4 |   3 |    2 |    1 |   0 |	 * Cold | Warm | Hot | Cold | Warm | Hot |	 */	if (BIT(fio->temp) & meta_flag)		op_flags |= REQ_META;	if (BIT(fio->temp) & fua_flag)		op_flags |= REQ_FUA;	return op_flags;} static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages){	struct f2fs_sb_info *sbi = fio->sbi;	struct block_device *bdev;	sector_t sector;	struct bio *bio; 	bdev = f2fs_target_device(sbi, fio->new_blkaddr, &sector);	bio = bio_alloc_bioset(bdev, npages,				fio->op | fio->op_flags | f2fs_io_flags(fio),				GFP_NOIO, &f2fs_bioset);	bio->bi_iter.bi_sector = sector;	if (is_read_io(fio->op)) {		bio->bi_end_io = f2fs_read_end_io;		bio->bi_private = NULL;	} else {		bio->bi_end_io = f2fs_write_end_io;		bio->bi_private = sbi;		bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,						fio->type, fio->temp);	}	iostat_alloc_and_bind_ctx(sbi, bio, NULL); 	if (fio->io_wbc)		wbc_init_bio(fio->io_wbc, bio); 	return bio;} static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,				  pgoff_t first_idx,				  const struct f2fs_io_info *fio,				  gfp_t gfp_mask){	/*	 * The f2fs garbage collector sets ->encrypted_page when it wants to	 * read/write raw data without encryption.	 */	if (!fio || !fio->encrypted_page)		fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);} static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,				     pgoff_t next_idx,				     const struct f2fs_io_info *fio){	/*	 * The f2fs garbage collector sets ->encrypted_page when it wants to	 * read/write raw data without encryption.	 */	if (fio && fio->encrypted_page)		return !bio_has_crypt_ctx(bio);

Lines 1–500 of 4271.