950 lines · 24.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/******************************************************************************* * Filename:  target_core_file.c * * This file contains the Storage Engine <-> FILEIO transport specific functions * * (c) Copyright 2005-2013 Datera, Inc. * * Nicholas A. Bellinger <nab@kernel.org> * ******************************************************************************/ #include <linux/string.h>#include <linux/parser.h>#include <linux/timer.h>#include <linux/blkdev.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/module.h>#include <linux/vmalloc.h>#include <linux/falloc.h>#include <linux/uio.h>#include <linux/scatterlist.h>#include <scsi/scsi_proto.h>#include <linux/unaligned.h> #include <target/target_core_base.h>#include <target/target_core_backend.h> #include "target_core_file.h" static inline struct fd_dev *FD_DEV(struct se_device *dev){	return container_of(dev, struct fd_dev, dev);} static int fd_attach_hba(struct se_hba *hba, u32 host_id){	struct fd_host *fd_host; 	fd_host = kzalloc_obj(struct fd_host);	if (!fd_host) {		pr_err("Unable to allocate memory for struct fd_host\n");		return -ENOMEM;	} 	fd_host->fd_host_id = host_id; 	hba->hba_ptr = fd_host; 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,		TARGET_CORE_VERSION);	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",		hba->hba_id, fd_host->fd_host_id); 	return 0;} static void fd_detach_hba(struct se_hba *hba){	struct fd_host *fd_host = hba->hba_ptr; 	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"		" Target Core\n", hba->hba_id, fd_host->fd_host_id); 	kfree(fd_host);	hba->hba_ptr = NULL;} static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name){	struct fd_dev *fd_dev;	struct fd_host *fd_host = hba->hba_ptr; 	fd_dev = kzalloc_obj(struct fd_dev);	if (!fd_dev) {		pr_err("Unable to allocate memory for struct fd_dev\n");		return NULL;	} 	fd_dev->fd_host = fd_host; 	pr_debug("FILEIO: Allocated fd_dev for %p\n", name); 	return &fd_dev->dev;} static bool fd_configure_unmap(struct se_device *dev){	struct file *file = FD_DEV(dev)->fd_file;	struct inode *inode = file->f_mapping->host; 	if (S_ISBLK(inode->i_mode))		return target_configure_unmap_from_bdev(&dev->dev_attrib,							I_BDEV(inode)); 	/* Limit UNMAP emulation to 8k Number of LBAs (NoLB) */	dev->dev_attrib.max_unmap_lba_count = 0x2000;	/* Currently hardcoded to 1 in Linux/SCSI code. */	dev->dev_attrib.max_unmap_block_desc_count = 1;	dev->dev_attrib.unmap_granularity = 1;	dev->dev_attrib.unmap_granularity_alignment = 0;	return true;} static int fd_configure_device(struct se_device *dev){	struct fd_dev *fd_dev = FD_DEV(dev);	struct fd_host *fd_host = dev->se_hba->hba_ptr;	struct file *file;	struct inode *inode = NULL;	int flags, ret = -EINVAL; 	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {		pr_err("Missing fd_dev_name=\n");		return -EINVAL;	} 	/*	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing	 * of pure timestamp updates.	 */	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 	/*	 * Optionally allow fd_buffered_io=1 to be enabled for people	 * who want use the fs buffer cache as an WriteCache mechanism.	 *	 * This means that in event of a hard failure, there is a risk	 * of silent data-loss if the SCSI client has *not* performed a	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE	 * to write-out the entire device cache.	 */	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");		flags &= ~O_DSYNC;	} 	file = filp_open(fd_dev->fd_dev_name, flags, 0600);	if (IS_ERR(file)) {		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);		ret = PTR_ERR(file);		goto fail;	}	fd_dev->fd_file = file;	/*	 * If using a block backend with this struct file, we extract	 * fd_dev->fd_[block,dev]_size from struct block_device.	 *	 * Otherwise, we use the passed fd_size= from configfs	 */	inode = file->f_mapping->host;	if (S_ISBLK(inode->i_mode)) {		struct block_device *bdev = I_BDEV(inode);		unsigned long long dev_size; 		fd_dev->fd_block_size = bdev_logical_block_size(bdev);		/*		 * Determine the number of bytes from i_size_read() minus		 * one (1) logical sector from underlying struct block_device		 */		dev_size = (i_size_read(file->f_mapping->host) -				       fd_dev->fd_block_size); 		pr_debug("FILEIO: Using size: %llu bytes from struct"			" block_device blocks: %llu logical_block_size: %d\n",			dev_size, div_u64(dev_size, fd_dev->fd_block_size),			fd_dev->fd_block_size);		/*		 * Enable write same emulation for IBLOCK and use 0xFFFF as		 * the smaller WRITE_SAME(10) only has a two-byte block count.		 */		dev->dev_attrib.max_write_same_len = 0xFFFF; 		if (!bdev_rot(bdev))			dev->dev_attrib.is_nonrot = 1;	} else {		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {			pr_err("FILEIO: Missing fd_dev_size="				" parameter, and no backing struct"				" block_device\n");			goto fail;		} 		fd_dev->fd_block_size = FD_BLOCKSIZE; 		/*		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)		 * based upon struct iovec limit for vfs_writev()		 */		dev->dev_attrib.max_write_same_len = 0x1000;	} 	dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;	dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"			" with FDBD_HAS_BUFFERED_IO_WCE\n");		dev->dev_attrib.emulate_write_cache = 1;	} 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;	fd_dev->fd_queue_depth = dev->queue_depth; 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,			fd_dev->fd_dev_name, fd_dev->fd_dev_size); 	return 0;fail:	if (fd_dev->fd_file) {		filp_close(fd_dev->fd_file, NULL);		fd_dev->fd_file = NULL;	}	return ret;} static void fd_dev_call_rcu(struct rcu_head *p){	struct se_device *dev = container_of(p, struct se_device, rcu_head);	struct fd_dev *fd_dev = FD_DEV(dev); 	kfree(fd_dev);} static void fd_free_device(struct se_device *dev){	call_rcu(&dev->rcu_head, fd_dev_call_rcu);} static void fd_destroy_device(struct se_device *dev){	struct fd_dev *fd_dev = FD_DEV(dev); 	if (fd_dev->fd_file) {		filp_close(fd_dev->fd_file, NULL);		fd_dev->fd_file = NULL;	}} struct target_core_file_cmd {	unsigned long	len;	struct se_cmd	*cmd;	struct kiocb	iocb;	struct bio_vec	bvecs[];}; static void cmd_rw_aio_complete(struct kiocb *iocb, long ret){	struct target_core_file_cmd *cmd; 	cmd = container_of(iocb, struct target_core_file_cmd, iocb); 	if (ret != cmd->len)		target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION);	else		target_complete_cmd(cmd->cmd, SAM_STAT_GOOD); 	kfree(cmd);} static sense_reason_tfd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,	      enum dma_data_direction data_direction){	int is_write = !(data_direction == DMA_FROM_DEVICE);	struct se_device *dev = cmd->se_dev;	struct fd_dev *fd_dev = FD_DEV(dev);	struct file *file = fd_dev->fd_file;	struct target_core_file_cmd *aio_cmd;	struct iov_iter iter;	struct scatterlist *sg;	ssize_t len = 0;	int ret = 0, i; 	aio_cmd = kzalloc_flex(*aio_cmd, bvecs, sgl_nents);	if (!aio_cmd)		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 	for_each_sg(sgl, sg, sgl_nents, i) {		bvec_set_page(&aio_cmd->bvecs[i], sg_page(sg), sg->length,			      sg->offset);		len += sg->length;	} 	iov_iter_bvec(&iter, is_write, aio_cmd->bvecs, sgl_nents, len); 	aio_cmd->cmd = cmd;	aio_cmd->len = len;	aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size;	aio_cmd->iocb.ki_filp = file;	aio_cmd->iocb.ki_complete = cmd_rw_aio_complete;	aio_cmd->iocb.ki_flags = IOCB_DIRECT; 	if (is_write && (cmd->se_cmd_flags & SCF_FUA))		aio_cmd->iocb.ki_flags |= IOCB_DSYNC; 	if (is_write)		ret = file->f_op->write_iter(&aio_cmd->iocb, &iter);	else		ret = file->f_op->read_iter(&aio_cmd->iocb, &iter); 	if (ret != -EIOCBQUEUED)		cmd_rw_aio_complete(&aio_cmd->iocb, ret); 	return 0;} static int fd_do_rw(struct se_cmd *cmd, struct file *fd,		    u32 block_size, struct scatterlist *sgl,		    u32 sgl_nents, u32 data_length, int is_write){	struct scatterlist *sg;	struct iov_iter iter;	struct bio_vec *bvec;	ssize_t len = 0;	loff_t pos = (cmd->t_task_lba * block_size);	int ret = 0, i; 	bvec = kzalloc_objs(struct bio_vec, sgl_nents);	if (!bvec) {		pr_err("Unable to allocate fd_do_readv iov[]\n");		return -ENOMEM;	} 	for_each_sg(sgl, sg, sgl_nents, i) {		bvec_set_page(&bvec[i], sg_page(sg), sg->length, sg->offset);		len += sg->length;	} 	iov_iter_bvec(&iter, is_write, bvec, sgl_nents, len);	if (is_write)		ret = vfs_iter_write(fd, &iter, &pos, 0);	else		ret = vfs_iter_read(fd, &iter, &pos, 0); 	if (is_write) {		if (ret < 0 || ret != data_length) {			pr_err("%s() write returned %d\n", __func__, ret);			if (ret >= 0)				ret = -EINVAL;		}	} else {		/*		 * Return zeros and GOOD status even if the READ did not return		 * the expected virt_size for struct file w/o a backing struct		 * block_device.		 */		if (S_ISBLK(file_inode(fd)->i_mode)) {			if (ret < 0 || ret != data_length) {				pr_err("%s() returned %d, expecting %u for "						"S_ISBLK\n", __func__, ret,						data_length);				if (ret >= 0)					ret = -EINVAL;			}		} else {			if (ret < 0) {				pr_err("%s() returned %d for non S_ISBLK\n",						__func__, ret);			} else if (ret != data_length) {				/*				 * Short read case:				 * Probably some one truncate file under us.				 * We must explicitly zero sg-pages to prevent				 * expose uninizialized pages to userspace.				 */				if (ret < data_length)					ret += iov_iter_zero(data_length - ret, &iter);				else					ret = -EINVAL;			}		}	}	kfree(bvec);	return ret;} static sense_reason_tfd_execute_sync_cache(struct se_cmd *cmd){	struct se_device *dev = cmd->se_dev;	struct fd_dev *fd_dev = FD_DEV(dev);	int immed = (cmd->t_task_cdb[1] & 0x2);	loff_t start, end;	int ret; 	/*	 * If the Immediate bit is set, queue up the GOOD response	 * for this SYNCHRONIZE_CACHE op	 */	if (immed)		target_complete_cmd(cmd, SAM_STAT_GOOD); 	/*	 * Determine if we will be flushing the entire device.	 */	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {		start = 0;		end = LLONG_MAX;	} else {		start = cmd->t_task_lba * dev->dev_attrib.block_size;		if (cmd->data_length)			end = start + cmd->data_length - 1;		else			end = LLONG_MAX;	} 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);	if (ret != 0)		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret); 	if (immed)		return 0; 	if (ret)		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);	else		target_complete_cmd(cmd, SAM_STAT_GOOD); 	return 0;} static sense_reason_tfd_execute_write_same(struct se_cmd *cmd){	struct se_device *se_dev = cmd->se_dev;	struct fd_dev *fd_dev = FD_DEV(se_dev);	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;	sector_t nolb = sbc_get_write_same_sectors(cmd);	struct iov_iter iter;	struct bio_vec *bvec;	unsigned int len = 0, i;	ssize_t ret; 	if (cmd->prot_op) {		pr_err("WRITE_SAME: Protection information with FILEIO"		       " backends not supported\n");		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;	} 	if (!cmd->t_data_nents)		return TCM_INVALID_CDB_FIELD; 	if (cmd->t_data_nents > 1 ||	    cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"			" block_size: %u\n",			cmd->t_data_nents,			cmd->t_data_sg[0].length,			cmd->se_dev->dev_attrib.block_size);		return TCM_INVALID_CDB_FIELD;	} 	bvec = kzalloc_objs(struct bio_vec, nolb);	if (!bvec)		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 	for (i = 0; i < nolb; i++) {		bvec_set_page(&bvec[i], sg_page(&cmd->t_data_sg[0]),			      cmd->t_data_sg[0].length,			      cmd->t_data_sg[0].offset);		len += se_dev->dev_attrib.block_size;	} 	iov_iter_bvec(&iter, ITER_SOURCE, bvec, nolb, len);	ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos, 0); 	kfree(bvec);	if (ret < 0 || ret != len) {		pr_err("vfs_iter_write() returned %zd for write same\n", ret);		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;	} 	target_complete_cmd(cmd, SAM_STAT_GOOD);	return 0;} static intfd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,		void *buf, size_t bufsize){	struct fd_dev *fd_dev = FD_DEV(se_dev);	struct file *prot_fd = fd_dev->fd_prot_file;	sector_t prot_length, prot;	loff_t pos = lba * se_dev->prot_length; 	if (!prot_fd) {		pr_err("Unable to locate fd_dev->fd_prot_file\n");		return -ENODEV;	} 	prot_length = nolb * se_dev->prot_length; 	memset(buf, 0xff, bufsize);	for (prot = 0; prot < prot_length;) {		sector_t len = min_t(sector_t, bufsize, prot_length - prot);

Lines 1–500 of 950.

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