11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c66ac9dbSNicholas Bellinger /******************************************************************************* 3c66ac9dbSNicholas Bellinger * Filename: target_core_file.c 4c66ac9dbSNicholas Bellinger * 5c66ac9dbSNicholas Bellinger * This file contains the Storage Engine <-> FILEIO transport specific functions 6c66ac9dbSNicholas Bellinger * 74c76251eSNicholas Bellinger * (c) Copyright 2005-2013 Datera, Inc. 8c66ac9dbSNicholas Bellinger * 9c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org> 10c66ac9dbSNicholas Bellinger * 11c66ac9dbSNicholas Bellinger ******************************************************************************/ 12c66ac9dbSNicholas Bellinger 13c66ac9dbSNicholas Bellinger #include <linux/string.h> 14c66ac9dbSNicholas Bellinger #include <linux/parser.h> 15c66ac9dbSNicholas Bellinger #include <linux/timer.h> 16c66ac9dbSNicholas Bellinger #include <linux/blkdev.h> 17c66ac9dbSNicholas Bellinger #include <linux/slab.h> 18c66ac9dbSNicholas Bellinger #include <linux/spinlock.h> 19827509e3SPaul Gortmaker #include <linux/module.h> 205538d294SDavid S. Miller #include <linux/vmalloc.h> 2170d3ae5cSAsias He #include <linux/falloc.h> 228dcf07beSBart Van Assche #include <linux/uio.h> 23ba929992SBart Van Assche #include <scsi/scsi_proto.h> 2450642762SAsias He #include <asm/unaligned.h> 25c66ac9dbSNicholas Bellinger 26c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 27c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 28c66ac9dbSNicholas Bellinger 29c66ac9dbSNicholas Bellinger #include "target_core_file.h" 30c66ac9dbSNicholas Bellinger 310fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev) 320fd97ccfSChristoph Hellwig { 330fd97ccfSChristoph Hellwig return container_of(dev, struct fd_dev, dev); 340fd97ccfSChristoph Hellwig } 35c66ac9dbSNicholas Bellinger 36c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id) 37c66ac9dbSNicholas Bellinger { 38c66ac9dbSNicholas Bellinger struct fd_host *fd_host; 39c66ac9dbSNicholas Bellinger 40c66ac9dbSNicholas Bellinger fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL); 416708bb27SAndy Grover if (!fd_host) { 426708bb27SAndy Grover pr_err("Unable to allocate memory for struct fd_host\n"); 43e3d6f909SAndy Grover return -ENOMEM; 44c66ac9dbSNicholas Bellinger } 45c66ac9dbSNicholas Bellinger 46c66ac9dbSNicholas Bellinger fd_host->fd_host_id = host_id; 47c66ac9dbSNicholas Bellinger 48e3d6f909SAndy Grover hba->hba_ptr = fd_host; 49c66ac9dbSNicholas Bellinger 506708bb27SAndy Grover pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic" 51c66ac9dbSNicholas Bellinger " Target Core Stack %s\n", hba->hba_id, FD_VERSION, 52ce8dd25dSChristoph Hellwig TARGET_CORE_VERSION); 5395cadaceSNicholas Bellinger pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n", 5495cadaceSNicholas Bellinger hba->hba_id, fd_host->fd_host_id); 55c66ac9dbSNicholas Bellinger 56c66ac9dbSNicholas Bellinger return 0; 57c66ac9dbSNicholas Bellinger } 58c66ac9dbSNicholas Bellinger 59c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba) 60c66ac9dbSNicholas Bellinger { 61c66ac9dbSNicholas Bellinger struct fd_host *fd_host = hba->hba_ptr; 62c66ac9dbSNicholas Bellinger 636708bb27SAndy Grover pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic" 64c66ac9dbSNicholas Bellinger " Target Core\n", hba->hba_id, fd_host->fd_host_id); 65c66ac9dbSNicholas Bellinger 66c66ac9dbSNicholas Bellinger kfree(fd_host); 67c66ac9dbSNicholas Bellinger hba->hba_ptr = NULL; 68c66ac9dbSNicholas Bellinger } 69c66ac9dbSNicholas Bellinger 700fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name) 71c66ac9dbSNicholas Bellinger { 72c66ac9dbSNicholas Bellinger struct fd_dev *fd_dev; 738359cf43SJörn Engel struct fd_host *fd_host = hba->hba_ptr; 74c66ac9dbSNicholas Bellinger 75c66ac9dbSNicholas Bellinger fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL); 766708bb27SAndy Grover if (!fd_dev) { 776708bb27SAndy Grover pr_err("Unable to allocate memory for struct fd_dev\n"); 78c66ac9dbSNicholas Bellinger return NULL; 79c66ac9dbSNicholas Bellinger } 80c66ac9dbSNicholas Bellinger 81c66ac9dbSNicholas Bellinger fd_dev->fd_host = fd_host; 82c66ac9dbSNicholas Bellinger 836708bb27SAndy Grover pr_debug("FILEIO: Allocated fd_dev for %p\n", name); 84c66ac9dbSNicholas Bellinger 850fd97ccfSChristoph Hellwig return &fd_dev->dev; 86c66ac9dbSNicholas Bellinger } 87c66ac9dbSNicholas Bellinger 880fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev) 89c66ac9dbSNicholas Bellinger { 900fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 910fd97ccfSChristoph Hellwig struct fd_host *fd_host = dev->se_hba->hba_ptr; 92c66ac9dbSNicholas Bellinger struct file *file; 93c66ac9dbSNicholas Bellinger struct inode *inode = NULL; 940fd97ccfSChristoph Hellwig int flags, ret = -EINVAL; 95c66ac9dbSNicholas Bellinger 960fd97ccfSChristoph Hellwig if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) { 970fd97ccfSChristoph Hellwig pr_err("Missing fd_dev_name=\n"); 980fd97ccfSChristoph Hellwig return -EINVAL; 990fd97ccfSChristoph Hellwig } 100c66ac9dbSNicholas Bellinger 101c66ac9dbSNicholas Bellinger /* 102a4dff304SNicholas Bellinger * Use O_DSYNC by default instead of O_SYNC to forgo syncing 103a4dff304SNicholas Bellinger * of pure timestamp updates. 104c66ac9dbSNicholas Bellinger */ 105a4dff304SNicholas Bellinger flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 1060fd97ccfSChristoph Hellwig 107b32f4c7eSNicholas Bellinger /* 108b32f4c7eSNicholas Bellinger * Optionally allow fd_buffered_io=1 to be enabled for people 109b32f4c7eSNicholas Bellinger * who want use the fs buffer cache as an WriteCache mechanism. 110b32f4c7eSNicholas Bellinger * 111b32f4c7eSNicholas Bellinger * This means that in event of a hard failure, there is a risk 112b32f4c7eSNicholas Bellinger * of silent data-loss if the SCSI client has *not* performed a 113b32f4c7eSNicholas Bellinger * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE 114b32f4c7eSNicholas Bellinger * to write-out the entire device cache. 115b32f4c7eSNicholas Bellinger */ 116b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 117b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n"); 118b32f4c7eSNicholas Bellinger flags &= ~O_DSYNC; 119b32f4c7eSNicholas Bellinger } 120c66ac9dbSNicholas Bellinger 121dbc6e022SAl Viro file = filp_open(fd_dev->fd_dev_name, flags, 0600); 122613640e4SNicholas Bellinger if (IS_ERR(file)) { 123dbc6e022SAl Viro pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name); 124613640e4SNicholas Bellinger ret = PTR_ERR(file); 125613640e4SNicholas Bellinger goto fail; 126613640e4SNicholas Bellinger } 127c66ac9dbSNicholas Bellinger fd_dev->fd_file = file; 128c66ac9dbSNicholas Bellinger /* 129c66ac9dbSNicholas Bellinger * If using a block backend with this struct file, we extract 130c66ac9dbSNicholas Bellinger * fd_dev->fd_[block,dev]_size from struct block_device. 131c66ac9dbSNicholas Bellinger * 132c66ac9dbSNicholas Bellinger * Otherwise, we use the passed fd_size= from configfs 133c66ac9dbSNicholas Bellinger */ 134c66ac9dbSNicholas Bellinger inode = file->f_mapping->host; 135c66ac9dbSNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 1364e7b5671SChristoph Hellwig struct request_queue *q = bdev_get_queue(I_BDEV(inode)); 137cd9323fdSNicholas Bellinger unsigned long long dev_size; 1380fd97ccfSChristoph Hellwig 1394e7b5671SChristoph Hellwig fd_dev->fd_block_size = bdev_logical_block_size(I_BDEV(inode)); 140c66ac9dbSNicholas Bellinger /* 141c66ac9dbSNicholas Bellinger * Determine the number of bytes from i_size_read() minus 142c66ac9dbSNicholas Bellinger * one (1) logical sector from underlying struct block_device 143c66ac9dbSNicholas Bellinger */ 144cd9323fdSNicholas Bellinger dev_size = (i_size_read(file->f_mapping->host) - 145c66ac9dbSNicholas Bellinger fd_dev->fd_block_size); 146c66ac9dbSNicholas Bellinger 1476708bb27SAndy Grover pr_debug("FILEIO: Using size: %llu bytes from struct" 148c66ac9dbSNicholas Bellinger " block_device blocks: %llu logical_block_size: %d\n", 149cd9323fdSNicholas Bellinger dev_size, div_u64(dev_size, fd_dev->fd_block_size), 150c66ac9dbSNicholas Bellinger fd_dev->fd_block_size); 1518a9ebe71SMike Christie 152ea263c7fSMike Christie if (target_configure_unmap_from_queue(&dev->dev_attrib, q)) 15370d3ae5cSAsias He pr_debug("IFILE: BLOCK Discard support available," 15470d3ae5cSAsias He " disabled by default\n"); 15570d3ae5cSAsias He /* 15670d3ae5cSAsias He * Enable write same emulation for IBLOCK and use 0xFFFF as 15770d3ae5cSAsias He * the smaller WRITE_SAME(10) only has a two-byte block count. 15870d3ae5cSAsias He */ 15970d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0xFFFF; 1600463a3feSAsias He 1610463a3feSAsias He if (blk_queue_nonrot(q)) 1620463a3feSAsias He dev->dev_attrib.is_nonrot = 1; 163c66ac9dbSNicholas Bellinger } else { 164c66ac9dbSNicholas Bellinger if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) { 1656708bb27SAndy Grover pr_err("FILEIO: Missing fd_dev_size=" 166c66ac9dbSNicholas Bellinger " parameter, and no backing struct" 167c66ac9dbSNicholas Bellinger " block_device\n"); 168c66ac9dbSNicholas Bellinger goto fail; 169c66ac9dbSNicholas Bellinger } 170c66ac9dbSNicholas Bellinger 17121363ca8SNicholas Bellinger fd_dev->fd_block_size = FD_BLOCKSIZE; 17270d3ae5cSAsias He /* 17370d3ae5cSAsias He * Limit UNMAP emulation to 8k Number of LBAs (NoLB) 17470d3ae5cSAsias He */ 17570d3ae5cSAsias He dev->dev_attrib.max_unmap_lba_count = 0x2000; 17670d3ae5cSAsias He /* 17770d3ae5cSAsias He * Currently hardcoded to 1 in Linux/SCSI code.. 17870d3ae5cSAsias He */ 17970d3ae5cSAsias He dev->dev_attrib.max_unmap_block_desc_count = 1; 18070d3ae5cSAsias He dev->dev_attrib.unmap_granularity = 1; 18170d3ae5cSAsias He dev->dev_attrib.unmap_granularity_alignment = 0; 18270d3ae5cSAsias He 18370d3ae5cSAsias He /* 18470d3ae5cSAsias He * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB) 18570d3ae5cSAsias He * based upon struct iovec limit for vfs_writev() 18670d3ae5cSAsias He */ 18770d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0x1000; 188c66ac9dbSNicholas Bellinger } 189c66ac9dbSNicholas Bellinger 19021363ca8SNicholas Bellinger dev->dev_attrib.hw_block_size = fd_dev->fd_block_size; 19195cadaceSNicholas Bellinger dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES; 19295cadaceSNicholas Bellinger dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size; 1930fd97ccfSChristoph Hellwig dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; 194c66ac9dbSNicholas Bellinger 195b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 196b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Forcing setting of emulate_write_cache=1" 197b32f4c7eSNicholas Bellinger " with FDBD_HAS_BUFFERED_IO_WCE\n"); 1980fd97ccfSChristoph Hellwig dev->dev_attrib.emulate_write_cache = 1; 199b32f4c7eSNicholas Bellinger } 200b32f4c7eSNicholas Bellinger 201c66ac9dbSNicholas Bellinger fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++; 202c66ac9dbSNicholas Bellinger fd_dev->fd_queue_depth = dev->queue_depth; 203c66ac9dbSNicholas Bellinger 2046708bb27SAndy Grover pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s," 205c66ac9dbSNicholas Bellinger " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id, 206c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size); 207c66ac9dbSNicholas Bellinger 2080fd97ccfSChristoph Hellwig return 0; 209c66ac9dbSNicholas Bellinger fail: 210c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 211c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 212c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 213c66ac9dbSNicholas Bellinger } 2140fd97ccfSChristoph Hellwig return ret; 215c66ac9dbSNicholas Bellinger } 216c66ac9dbSNicholas Bellinger 2174cc987eaSNicholas Bellinger static void fd_dev_call_rcu(struct rcu_head *p) 2184cc987eaSNicholas Bellinger { 2194cc987eaSNicholas Bellinger struct se_device *dev = container_of(p, struct se_device, rcu_head); 2204cc987eaSNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 2214cc987eaSNicholas Bellinger 2224cc987eaSNicholas Bellinger kfree(fd_dev); 2234cc987eaSNicholas Bellinger } 2244cc987eaSNicholas Bellinger 2250fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev) 226c66ac9dbSNicholas Bellinger { 22792634706SMike Christie call_rcu(&dev->rcu_head, fd_dev_call_rcu); 22892634706SMike Christie } 22992634706SMike Christie 23092634706SMike Christie static void fd_destroy_device(struct se_device *dev) 23192634706SMike Christie { 2320fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 233c66ac9dbSNicholas Bellinger 234c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 235c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 236c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 237c66ac9dbSNicholas Bellinger } 238c66ac9dbSNicholas Bellinger } 239c66ac9dbSNicholas Bellinger 240769031c9SAndrei Vagin struct target_core_file_cmd { 241769031c9SAndrei Vagin unsigned long len; 242769031c9SAndrei Vagin struct se_cmd *cmd; 243769031c9SAndrei Vagin struct kiocb iocb; 244769031c9SAndrei Vagin }; 245769031c9SAndrei Vagin 246769031c9SAndrei Vagin static void cmd_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 247769031c9SAndrei Vagin { 248769031c9SAndrei Vagin struct target_core_file_cmd *cmd; 249769031c9SAndrei Vagin 250769031c9SAndrei Vagin cmd = container_of(iocb, struct target_core_file_cmd, iocb); 251769031c9SAndrei Vagin 252769031c9SAndrei Vagin if (ret != cmd->len) 253769031c9SAndrei Vagin target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION); 254769031c9SAndrei Vagin else 255769031c9SAndrei Vagin target_complete_cmd(cmd->cmd, SAM_STAT_GOOD); 256769031c9SAndrei Vagin 257769031c9SAndrei Vagin kfree(cmd); 258769031c9SAndrei Vagin } 259769031c9SAndrei Vagin 260769031c9SAndrei Vagin static sense_reason_t 261769031c9SAndrei Vagin fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 262769031c9SAndrei Vagin enum dma_data_direction data_direction) 263769031c9SAndrei Vagin { 264769031c9SAndrei Vagin int is_write = !(data_direction == DMA_FROM_DEVICE); 265769031c9SAndrei Vagin struct se_device *dev = cmd->se_dev; 266769031c9SAndrei Vagin struct fd_dev *fd_dev = FD_DEV(dev); 267769031c9SAndrei Vagin struct file *file = fd_dev->fd_file; 268769031c9SAndrei Vagin struct target_core_file_cmd *aio_cmd; 269*6b1dba3dSPavel Begunkov struct iov_iter iter; 270769031c9SAndrei Vagin struct scatterlist *sg; 271769031c9SAndrei Vagin struct bio_vec *bvec; 272769031c9SAndrei Vagin ssize_t len = 0; 273769031c9SAndrei Vagin int ret = 0, i; 274769031c9SAndrei Vagin 275769031c9SAndrei Vagin aio_cmd = kmalloc(sizeof(struct target_core_file_cmd), GFP_KERNEL); 276769031c9SAndrei Vagin if (!aio_cmd) 277769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 278769031c9SAndrei Vagin 279769031c9SAndrei Vagin bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL); 280769031c9SAndrei Vagin if (!bvec) { 281769031c9SAndrei Vagin kfree(aio_cmd); 282769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 283769031c9SAndrei Vagin } 284769031c9SAndrei Vagin 285769031c9SAndrei Vagin for_each_sg(sgl, sg, sgl_nents, i) { 286769031c9SAndrei Vagin bvec[i].bv_page = sg_page(sg); 287769031c9SAndrei Vagin bvec[i].bv_len = sg->length; 288769031c9SAndrei Vagin bvec[i].bv_offset = sg->offset; 289769031c9SAndrei Vagin 290769031c9SAndrei Vagin len += sg->length; 291769031c9SAndrei Vagin } 292769031c9SAndrei Vagin 293aa563d7bSDavid Howells iov_iter_bvec(&iter, is_write, bvec, sgl_nents, len); 294769031c9SAndrei Vagin 295769031c9SAndrei Vagin aio_cmd->cmd = cmd; 296769031c9SAndrei Vagin aio_cmd->len = len; 297769031c9SAndrei Vagin aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size; 298769031c9SAndrei Vagin aio_cmd->iocb.ki_filp = file; 299769031c9SAndrei Vagin aio_cmd->iocb.ki_complete = cmd_rw_aio_complete; 300769031c9SAndrei Vagin aio_cmd->iocb.ki_flags = IOCB_DIRECT; 301769031c9SAndrei Vagin 302769031c9SAndrei Vagin if (is_write && (cmd->se_cmd_flags & SCF_FUA)) 303769031c9SAndrei Vagin aio_cmd->iocb.ki_flags |= IOCB_DSYNC; 304769031c9SAndrei Vagin 305769031c9SAndrei Vagin if (is_write) 306769031c9SAndrei Vagin ret = call_write_iter(file, &aio_cmd->iocb, &iter); 307769031c9SAndrei Vagin else 308769031c9SAndrei Vagin ret = call_read_iter(file, &aio_cmd->iocb, &iter); 309769031c9SAndrei Vagin 310769031c9SAndrei Vagin kfree(bvec); 311769031c9SAndrei Vagin 312769031c9SAndrei Vagin if (ret != -EIOCBQUEUED) 313769031c9SAndrei Vagin cmd_rw_aio_complete(&aio_cmd->iocb, ret, 0); 314769031c9SAndrei Vagin 315769031c9SAndrei Vagin return 0; 316769031c9SAndrei Vagin } 317769031c9SAndrei Vagin 3188287fa5fSSagi Grimberg static int fd_do_rw(struct se_cmd *cmd, struct file *fd, 3198287fa5fSSagi Grimberg u32 block_size, struct scatterlist *sgl, 3208287fa5fSSagi Grimberg u32 sgl_nents, u32 data_length, int is_write) 32142201b55SNicholas Bellinger { 3225787cacdSChristoph Hellwig struct scatterlist *sg; 3236b9a44d0SChristoph Hellwig struct iov_iter iter; 3246b9a44d0SChristoph Hellwig struct bio_vec *bvec; 3256b9a44d0SChristoph Hellwig ssize_t len = 0; 3268287fa5fSSagi Grimberg loff_t pos = (cmd->t_task_lba * block_size); 327c66ac9dbSNicholas Bellinger int ret = 0, i; 328c66ac9dbSNicholas Bellinger 3296b9a44d0SChristoph Hellwig bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL); 3306b9a44d0SChristoph Hellwig if (!bvec) { 3316708bb27SAndy Grover pr_err("Unable to allocate fd_do_readv iov[]\n"); 332e3d6f909SAndy Grover return -ENOMEM; 333c66ac9dbSNicholas Bellinger } 334c66ac9dbSNicholas Bellinger 3355787cacdSChristoph Hellwig for_each_sg(sgl, sg, sgl_nents, i) { 3366b9a44d0SChristoph Hellwig bvec[i].bv_page = sg_page(sg); 3376b9a44d0SChristoph Hellwig bvec[i].bv_len = sg->length; 3386b9a44d0SChristoph Hellwig bvec[i].bv_offset = sg->offset; 3396b9a44d0SChristoph Hellwig 3406b9a44d0SChristoph Hellwig len += sg->length; 341c66ac9dbSNicholas Bellinger } 342c66ac9dbSNicholas Bellinger 343aa563d7bSDavid Howells iov_iter_bvec(&iter, READ, bvec, sgl_nents, len); 344778229afSSebastian Andrzej Siewior if (is_write) 345abbb6589SChristoph Hellwig ret = vfs_iter_write(fd, &iter, &pos, 0); 346778229afSSebastian Andrzej Siewior else 34718e9710eSChristoph Hellwig ret = vfs_iter_read(fd, &iter, &pos, 0); 348778229afSSebastian Andrzej Siewior 349778229afSSebastian Andrzej Siewior if (is_write) { 3508287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 351778229afSSebastian Andrzej Siewior pr_err("%s() write returned %d\n", __func__, ret); 352f11b55d1SDmitry Monakhov if (ret >= 0) 353f11b55d1SDmitry Monakhov ret = -EINVAL; 354778229afSSebastian Andrzej Siewior } 355778229afSSebastian Andrzej Siewior } else { 356c66ac9dbSNicholas Bellinger /* 357c66ac9dbSNicholas Bellinger * Return zeros and GOOD status even if the READ did not return 358c66ac9dbSNicholas Bellinger * the expected virt_size for struct file w/o a backing struct 359c66ac9dbSNicholas Bellinger * block_device. 360c66ac9dbSNicholas Bellinger */ 361496ad9aaSAl Viro if (S_ISBLK(file_inode(fd)->i_mode)) { 3628287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 363778229afSSebastian Andrzej Siewior pr_err("%s() returned %d, expecting %u for " 364778229afSSebastian Andrzej Siewior "S_ISBLK\n", __func__, ret, 3658287fa5fSSagi Grimberg data_length); 366f11b55d1SDmitry Monakhov if (ret >= 0) 367f11b55d1SDmitry Monakhov ret = -EINVAL; 368c66ac9dbSNicholas Bellinger } 369c66ac9dbSNicholas Bellinger } else { 370c66ac9dbSNicholas Bellinger if (ret < 0) { 371778229afSSebastian Andrzej Siewior pr_err("%s() returned %d for non S_ISBLK\n", 372778229afSSebastian Andrzej Siewior __func__, ret); 373f11b55d1SDmitry Monakhov } else if (ret != data_length) { 374f11b55d1SDmitry Monakhov /* 375f11b55d1SDmitry Monakhov * Short read case: 376f11b55d1SDmitry Monakhov * Probably some one truncate file under us. 377f11b55d1SDmitry Monakhov * We must explicitly zero sg-pages to prevent 378f11b55d1SDmitry Monakhov * expose uninizialized pages to userspace. 379f11b55d1SDmitry Monakhov */ 380f11b55d1SDmitry Monakhov if (ret < data_length) 381f11b55d1SDmitry Monakhov ret += iov_iter_zero(data_length - ret, &iter); 382f11b55d1SDmitry Monakhov else 383f11b55d1SDmitry Monakhov ret = -EINVAL; 384f11b55d1SDmitry Monakhov } 385f11b55d1SDmitry Monakhov } 386f11b55d1SDmitry Monakhov } 387f11b55d1SDmitry Monakhov kfree(bvec); 388e3d6f909SAndy Grover return ret; 389c66ac9dbSNicholas Bellinger } 390c66ac9dbSNicholas Bellinger 391de103c93SChristoph Hellwig static sense_reason_t 392de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd) 393c66ac9dbSNicholas Bellinger { 394c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 3950fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 396a1d8b49aSAndy Grover int immed = (cmd->t_task_cdb[1] & 0x2); 397c66ac9dbSNicholas Bellinger loff_t start, end; 398c66ac9dbSNicholas Bellinger int ret; 399c66ac9dbSNicholas Bellinger 400c66ac9dbSNicholas Bellinger /* 401c66ac9dbSNicholas Bellinger * If the Immediate bit is set, queue up the GOOD response 402c66ac9dbSNicholas Bellinger * for this SYNCHRONIZE_CACHE op 403c66ac9dbSNicholas Bellinger */ 404c66ac9dbSNicholas Bellinger if (immed) 4055787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 406c66ac9dbSNicholas Bellinger 407c66ac9dbSNicholas Bellinger /* 408c66ac9dbSNicholas Bellinger * Determine if we will be flushing the entire device. 409c66ac9dbSNicholas Bellinger */ 410a1d8b49aSAndy Grover if (cmd->t_task_lba == 0 && cmd->data_length == 0) { 411c66ac9dbSNicholas Bellinger start = 0; 412c66ac9dbSNicholas Bellinger end = LLONG_MAX; 413c66ac9dbSNicholas Bellinger } else { 4140fd97ccfSChristoph Hellwig start = cmd->t_task_lba * dev->dev_attrib.block_size; 415c66ac9dbSNicholas Bellinger if (cmd->data_length) 41662d3ab49SZach Brown end = start + cmd->data_length - 1; 417c66ac9dbSNicholas Bellinger else 418c66ac9dbSNicholas Bellinger end = LLONG_MAX; 419c66ac9dbSNicholas Bellinger } 420c66ac9dbSNicholas Bellinger 421c66ac9dbSNicholas Bellinger ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); 422c66ac9dbSNicholas Bellinger if (ret != 0) 4236708bb27SAndy Grover pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret); 424c66ac9dbSNicholas Bellinger 4255787cacdSChristoph Hellwig if (immed) 426ad67f0d9SChristoph Hellwig return 0; 4275787cacdSChristoph Hellwig 428de103c93SChristoph Hellwig if (ret) 4295787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); 430de103c93SChristoph Hellwig else 4315787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 432ad67f0d9SChristoph Hellwig 433ad67f0d9SChristoph Hellwig return 0; 434c66ac9dbSNicholas Bellinger } 435c66ac9dbSNicholas Bellinger 4367b745c84SNicholas Bellinger static sense_reason_t 4377b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd) 4387b745c84SNicholas Bellinger { 4397b745c84SNicholas Bellinger struct se_device *se_dev = cmd->se_dev; 4407b745c84SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(se_dev); 4417b745c84SNicholas Bellinger loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size; 442d4c5dcacSChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 443d4c5dcacSChristoph Hellwig struct iov_iter iter; 444d4c5dcacSChristoph Hellwig struct bio_vec *bvec; 445d4c5dcacSChristoph Hellwig unsigned int len = 0, i; 446d4c5dcacSChristoph Hellwig ssize_t ret; 4477b745c84SNicholas Bellinger 4487b745c84SNicholas Bellinger if (!nolb) { 4497b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 4507b745c84SNicholas Bellinger return 0; 4517b745c84SNicholas Bellinger } 452afd73f1bSNicholas Bellinger if (cmd->prot_op) { 453afd73f1bSNicholas Bellinger pr_err("WRITE_SAME: Protection information with FILEIO" 454afd73f1bSNicholas Bellinger " backends not supported\n"); 455afd73f1bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 456afd73f1bSNicholas Bellinger } 4577b745c84SNicholas Bellinger 4587b745c84SNicholas Bellinger if (cmd->t_data_nents > 1 || 459d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) { 4607b745c84SNicholas Bellinger pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" 461d4c5dcacSChristoph Hellwig " block_size: %u\n", 462d4c5dcacSChristoph Hellwig cmd->t_data_nents, 463d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length, 4647b745c84SNicholas Bellinger cmd->se_dev->dev_attrib.block_size); 4657b745c84SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 4667b745c84SNicholas Bellinger } 4677b745c84SNicholas Bellinger 468d4c5dcacSChristoph Hellwig bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL); 469d4c5dcacSChristoph Hellwig if (!bvec) 4707b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4717b745c84SNicholas Bellinger 472d4c5dcacSChristoph Hellwig for (i = 0; i < nolb; i++) { 473d4c5dcacSChristoph Hellwig bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]); 474d4c5dcacSChristoph Hellwig bvec[i].bv_len = cmd->t_data_sg[0].length; 475d4c5dcacSChristoph Hellwig bvec[i].bv_offset = cmd->t_data_sg[0].offset; 476d4c5dcacSChristoph Hellwig 477d4c5dcacSChristoph Hellwig len += se_dev->dev_attrib.block_size; 4787b745c84SNicholas Bellinger } 4797b745c84SNicholas Bellinger 480aa563d7bSDavid Howells iov_iter_bvec(&iter, READ, bvec, nolb, len); 481abbb6589SChristoph Hellwig ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos, 0); 4827b745c84SNicholas Bellinger 483d4c5dcacSChristoph Hellwig kfree(bvec); 484d4c5dcacSChristoph Hellwig if (ret < 0 || ret != len) { 485d4c5dcacSChristoph Hellwig pr_err("vfs_iter_write() returned %zd for write same\n", ret); 4867b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4877b745c84SNicholas Bellinger } 4887b745c84SNicholas Bellinger 4897b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 4907b745c84SNicholas Bellinger return 0; 4917b745c84SNicholas Bellinger } 4927b745c84SNicholas Bellinger 49364d240b7SAkinobu Mita static int 49464d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb, 49564d240b7SAkinobu Mita void *buf, size_t bufsize) 49664d240b7SAkinobu Mita { 49764d240b7SAkinobu Mita struct fd_dev *fd_dev = FD_DEV(se_dev); 49864d240b7SAkinobu Mita struct file *prot_fd = fd_dev->fd_prot_file; 49964d240b7SAkinobu Mita sector_t prot_length, prot; 50064d240b7SAkinobu Mita loff_t pos = lba * se_dev->prot_length; 50164d240b7SAkinobu Mita 50264d240b7SAkinobu Mita if (!prot_fd) { 50364d240b7SAkinobu Mita pr_err("Unable to locate fd_dev->fd_prot_file\n"); 50464d240b7SAkinobu Mita return -ENODEV; 50564d240b7SAkinobu Mita } 50664d240b7SAkinobu Mita 50764d240b7SAkinobu Mita prot_length = nolb * se_dev->prot_length; 50864d240b7SAkinobu Mita 50964d240b7SAkinobu Mita for (prot = 0; prot < prot_length;) { 51064d240b7SAkinobu Mita sector_t len = min_t(sector_t, bufsize, prot_length - prot); 511e13ec939SChristoph Hellwig ssize_t ret = kernel_write(prot_fd, buf, len, &pos); 51264d240b7SAkinobu Mita 51364d240b7SAkinobu Mita if (ret != len) { 51464d240b7SAkinobu Mita pr_err("vfs_write to prot file failed: %zd\n", ret); 51564d240b7SAkinobu Mita return ret < 0 ? ret : -ENODEV; 51664d240b7SAkinobu Mita } 51764d240b7SAkinobu Mita prot += ret; 51864d240b7SAkinobu Mita } 51964d240b7SAkinobu Mita 52064d240b7SAkinobu Mita return 0; 52164d240b7SAkinobu Mita } 52264d240b7SAkinobu Mita 52364d240b7SAkinobu Mita static int 52464d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 52564d240b7SAkinobu Mita { 52664d240b7SAkinobu Mita void *buf; 52764d240b7SAkinobu Mita int rc; 52864d240b7SAkinobu Mita 52964d240b7SAkinobu Mita buf = (void *)__get_free_page(GFP_KERNEL); 53064d240b7SAkinobu Mita if (!buf) { 53164d240b7SAkinobu Mita pr_err("Unable to allocate FILEIO prot buf\n"); 53264d240b7SAkinobu Mita return -ENOMEM; 53364d240b7SAkinobu Mita } 53464d240b7SAkinobu Mita memset(buf, 0xff, PAGE_SIZE); 53564d240b7SAkinobu Mita 53664d240b7SAkinobu Mita rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE); 53764d240b7SAkinobu Mita 53864d240b7SAkinobu Mita free_page((unsigned long)buf); 53964d240b7SAkinobu Mita 54064d240b7SAkinobu Mita return rc; 54164d240b7SAkinobu Mita } 54264d240b7SAkinobu Mita 543de103c93SChristoph Hellwig static sense_reason_t 54462e46942SChristoph Hellwig fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 54570d3ae5cSAsias He { 54662e46942SChristoph Hellwig struct file *file = FD_DEV(cmd->se_dev)->fd_file; 54770d3ae5cSAsias He struct inode *inode = file->f_mapping->host; 54870d3ae5cSAsias He int ret; 54970d3ae5cSAsias He 550594e25e7SJiang Yi if (!nolb) { 551594e25e7SJiang Yi return 0; 552594e25e7SJiang Yi } 553594e25e7SJiang Yi 55464d240b7SAkinobu Mita if (cmd->se_dev->dev_attrib.pi_prot_type) { 55564d240b7SAkinobu Mita ret = fd_do_prot_unmap(cmd, lba, nolb); 55664d240b7SAkinobu Mita if (ret) 55764d240b7SAkinobu Mita return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 55864d240b7SAkinobu Mita } 55964d240b7SAkinobu Mita 56070d3ae5cSAsias He if (S_ISBLK(inode->i_mode)) { 56170d3ae5cSAsias He /* The backend is block device, use discard */ 5624e7b5671SChristoph Hellwig struct block_device *bdev = I_BDEV(inode); 5638a9ebe71SMike Christie struct se_device *dev = cmd->se_dev; 56470d3ae5cSAsias He 5658a9ebe71SMike Christie ret = blkdev_issue_discard(bdev, 5668a9ebe71SMike Christie target_to_linux_sector(dev, lba), 5678a9ebe71SMike Christie target_to_linux_sector(dev, nolb), 5688a9ebe71SMike Christie GFP_KERNEL, 0); 56970d3ae5cSAsias He if (ret < 0) { 57070d3ae5cSAsias He pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n", 57170d3ae5cSAsias He ret); 57270d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 57370d3ae5cSAsias He } 57470d3ae5cSAsias He } else { 57570d3ae5cSAsias He /* The backend is normal file, use fallocate */ 57643f55bbbSAsias He struct se_device *se_dev = cmd->se_dev; 57743f55bbbSAsias He loff_t pos = lba * se_dev->dev_attrib.block_size; 57870d3ae5cSAsias He unsigned int len = nolb * se_dev->dev_attrib.block_size; 57970d3ae5cSAsias He int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 58070d3ae5cSAsias He 58170d3ae5cSAsias He if (!file->f_op->fallocate) 58270d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 58370d3ae5cSAsias He 58470d3ae5cSAsias He ret = file->f_op->fallocate(file, mode, pos, len); 58570d3ae5cSAsias He if (ret < 0) { 58670d3ae5cSAsias He pr_warn("FILEIO: fallocate() failed: %d\n", ret); 58770d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 58870d3ae5cSAsias He } 58970d3ae5cSAsias He } 59070d3ae5cSAsias He 59143f55bbbSAsias He return 0; 59243f55bbbSAsias He } 59343f55bbbSAsias He 59443f55bbbSAsias He static sense_reason_t 595769031c9SAndrei Vagin fd_execute_rw_buffered(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 596a82a9538SNicholas Bellinger enum dma_data_direction data_direction) 597c66ac9dbSNicholas Bellinger { 598c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 5998287fa5fSSagi Grimberg struct fd_dev *fd_dev = FD_DEV(dev); 6008287fa5fSSagi Grimberg struct file *file = fd_dev->fd_file; 6018287fa5fSSagi Grimberg struct file *pfile = fd_dev->fd_prot_file; 60242201b55SNicholas Bellinger sense_reason_t rc; 603c66ac9dbSNicholas Bellinger int ret = 0; 604046ba642SNicholas Bellinger /* 605c66ac9dbSNicholas Bellinger * Call vectorized fileio functions to map struct scatterlist 606c66ac9dbSNicholas Bellinger * physical memory addresses to struct iovec virtual memory. 607c66ac9dbSNicholas Bellinger */ 6085787cacdSChristoph Hellwig if (data_direction == DMA_FROM_DEVICE) { 609ee920469SNicholas Bellinger if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 6108287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 6118287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 6128287fa5fSSagi Grimberg cmd->prot_length, 0); 61342201b55SNicholas Bellinger if (ret < 0) 61442201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 61542201b55SNicholas Bellinger } 61642201b55SNicholas Bellinger 6178287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6188287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 0); 61942201b55SNicholas Bellinger 620056e8924SDmitry Monakhov if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type && 621056e8924SDmitry Monakhov dev->dev_attrib.pi_prot_verify) { 6228287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 6238287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 62442201b55SNicholas Bellinger 625f75b6faeSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 626f75b6faeSSagi Grimberg 0, cmd->t_prot_sg, 0); 6278287fa5fSSagi Grimberg if (rc) 62842201b55SNicholas Bellinger return rc; 62942201b55SNicholas Bellinger } 630c66ac9dbSNicholas Bellinger } else { 631056e8924SDmitry Monakhov if (cmd->prot_type && dev->dev_attrib.pi_prot_type && 632056e8924SDmitry Monakhov dev->dev_attrib.pi_prot_verify) { 6338287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 6348287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 63542201b55SNicholas Bellinger 6368287fa5fSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 6378287fa5fSSagi Grimberg 0, cmd->t_prot_sg, 0); 6388287fa5fSSagi Grimberg if (rc) 63942201b55SNicholas Bellinger return rc; 64042201b55SNicholas Bellinger } 64142201b55SNicholas Bellinger 6428287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6438287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 1); 644a4dff304SNicholas Bellinger /* 645125d0119SHannes Reinecke * Perform implicit vfs_fsync_range() for fd_do_writev() ops 646a4dff304SNicholas Bellinger * for SCSI WRITEs with Forced Unit Access (FUA) set. 647a4dff304SNicholas Bellinger * Allow this to happen independent of WCE=0 setting. 648a4dff304SNicholas Bellinger */ 649814e5b45SChristoph Hellwig if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) { 650a4dff304SNicholas Bellinger loff_t start = cmd->t_task_lba * 6510fd97ccfSChristoph Hellwig dev->dev_attrib.block_size; 65262d3ab49SZach Brown loff_t end; 65362d3ab49SZach Brown 65462d3ab49SZach Brown if (cmd->data_length) 65562d3ab49SZach Brown end = start + cmd->data_length - 1; 65662d3ab49SZach Brown else 65762d3ab49SZach Brown end = LLONG_MAX; 658c66ac9dbSNicholas Bellinger 659a4dff304SNicholas Bellinger vfs_fsync_range(fd_dev->fd_file, start, end, 1); 660a4dff304SNicholas Bellinger } 661c66ac9dbSNicholas Bellinger 662ee920469SNicholas Bellinger if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) { 6638287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 6648287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 6658287fa5fSSagi Grimberg cmd->prot_length, 1); 666de103c93SChristoph Hellwig if (ret < 0) 667de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 66842201b55SNicholas Bellinger } 66942201b55SNicholas Bellinger } 67042201b55SNicholas Bellinger 6718287fa5fSSagi Grimberg if (ret < 0) 67242201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 673de103c93SChristoph Hellwig 6745787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 67503e98c9eSNicholas Bellinger return 0; 676c66ac9dbSNicholas Bellinger } 677c66ac9dbSNicholas Bellinger 678769031c9SAndrei Vagin static sense_reason_t 679769031c9SAndrei Vagin fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 680769031c9SAndrei Vagin enum dma_data_direction data_direction) 681769031c9SAndrei Vagin { 682769031c9SAndrei Vagin struct se_device *dev = cmd->se_dev; 683769031c9SAndrei Vagin struct fd_dev *fd_dev = FD_DEV(dev); 684769031c9SAndrei Vagin 685769031c9SAndrei Vagin /* 686769031c9SAndrei Vagin * We are currently limited by the number of iovecs (2048) per 687769031c9SAndrei Vagin * single vfs_[writev,readv] call. 688769031c9SAndrei Vagin */ 689769031c9SAndrei Vagin if (cmd->data_length > FD_MAX_BYTES) { 690769031c9SAndrei Vagin pr_err("FILEIO: Not able to process I/O of %u bytes due to" 691769031c9SAndrei Vagin "FD_MAX_BYTES: %u iovec count limitation\n", 692769031c9SAndrei Vagin cmd->data_length, FD_MAX_BYTES); 693769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 694769031c9SAndrei Vagin } 695769031c9SAndrei Vagin 696769031c9SAndrei Vagin if (fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO) 697769031c9SAndrei Vagin return fd_execute_rw_aio(cmd, sgl, sgl_nents, data_direction); 698769031c9SAndrei Vagin return fd_execute_rw_buffered(cmd, sgl, sgl_nents, data_direction); 699769031c9SAndrei Vagin } 700769031c9SAndrei Vagin 701c66ac9dbSNicholas Bellinger enum { 702769031c9SAndrei Vagin Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, 703769031c9SAndrei Vagin Opt_fd_async_io, Opt_err 704c66ac9dbSNicholas Bellinger }; 705c66ac9dbSNicholas Bellinger 706c66ac9dbSNicholas Bellinger static match_table_t tokens = { 707c66ac9dbSNicholas Bellinger {Opt_fd_dev_name, "fd_dev_name=%s"}, 708c66ac9dbSNicholas Bellinger {Opt_fd_dev_size, "fd_dev_size=%s"}, 709b32f4c7eSNicholas Bellinger {Opt_fd_buffered_io, "fd_buffered_io=%d"}, 710769031c9SAndrei Vagin {Opt_fd_async_io, "fd_async_io=%d"}, 711c66ac9dbSNicholas Bellinger {Opt_err, NULL} 712c66ac9dbSNicholas Bellinger }; 713c66ac9dbSNicholas Bellinger 7140fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev, 715c66ac9dbSNicholas Bellinger const char *page, ssize_t count) 716c66ac9dbSNicholas Bellinger { 7170fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 718c66ac9dbSNicholas Bellinger char *orig, *ptr, *arg_p, *opts; 719c66ac9dbSNicholas Bellinger substring_t args[MAX_OPT_ARGS]; 720b32f4c7eSNicholas Bellinger int ret = 0, arg, token; 721c66ac9dbSNicholas Bellinger 722c66ac9dbSNicholas Bellinger opts = kstrdup(page, GFP_KERNEL); 723c66ac9dbSNicholas Bellinger if (!opts) 724c66ac9dbSNicholas Bellinger return -ENOMEM; 725c66ac9dbSNicholas Bellinger 726c66ac9dbSNicholas Bellinger orig = opts; 727c66ac9dbSNicholas Bellinger 72890c161b6SSebastian Andrzej Siewior while ((ptr = strsep(&opts, ",\n")) != NULL) { 729c66ac9dbSNicholas Bellinger if (!*ptr) 730c66ac9dbSNicholas Bellinger continue; 731c66ac9dbSNicholas Bellinger 732c66ac9dbSNicholas Bellinger token = match_token(ptr, tokens, args); 733c66ac9dbSNicholas Bellinger switch (token) { 734c66ac9dbSNicholas Bellinger case Opt_fd_dev_name: 735dbc6e022SAl Viro if (match_strlcpy(fd_dev->fd_dev_name, &args[0], 736dbc6e022SAl Viro FD_MAX_DEV_NAME) == 0) { 737dbc6e022SAl Viro ret = -EINVAL; 7386d180253SJesper Juhl break; 7396d180253SJesper Juhl } 7406708bb27SAndy Grover pr_debug("FILEIO: Referencing Path: %s\n", 741c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name); 742c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_PATH; 743c66ac9dbSNicholas Bellinger break; 744c66ac9dbSNicholas Bellinger case Opt_fd_dev_size: 745c66ac9dbSNicholas Bellinger arg_p = match_strdup(&args[0]); 7466d180253SJesper Juhl if (!arg_p) { 7476d180253SJesper Juhl ret = -ENOMEM; 7486d180253SJesper Juhl break; 7496d180253SJesper Juhl } 75057103d7fSJingoo Han ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size); 7516d180253SJesper Juhl kfree(arg_p); 752c66ac9dbSNicholas Bellinger if (ret < 0) { 75357103d7fSJingoo Han pr_err("kstrtoull() failed for" 754c66ac9dbSNicholas Bellinger " fd_dev_size=\n"); 755c66ac9dbSNicholas Bellinger goto out; 756c66ac9dbSNicholas Bellinger } 7576708bb27SAndy Grover pr_debug("FILEIO: Referencing Size: %llu" 758c66ac9dbSNicholas Bellinger " bytes\n", fd_dev->fd_dev_size); 759c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_SIZE; 760c66ac9dbSNicholas Bellinger break; 761b32f4c7eSNicholas Bellinger case Opt_fd_buffered_io: 762ce31c1b0SJoern Engel ret = match_int(args, &arg); 763ce31c1b0SJoern Engel if (ret) 764ce31c1b0SJoern Engel goto out; 765b32f4c7eSNicholas Bellinger if (arg != 1) { 766b32f4c7eSNicholas Bellinger pr_err("bogus fd_buffered_io=%d value\n", arg); 767b32f4c7eSNicholas Bellinger ret = -EINVAL; 768b32f4c7eSNicholas Bellinger goto out; 769b32f4c7eSNicholas Bellinger } 770b32f4c7eSNicholas Bellinger 771b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Using buffered I/O" 772b32f4c7eSNicholas Bellinger " operations for struct fd_dev\n"); 773b32f4c7eSNicholas Bellinger 774b32f4c7eSNicholas Bellinger fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE; 775b32f4c7eSNicholas Bellinger break; 776769031c9SAndrei Vagin case Opt_fd_async_io: 777769031c9SAndrei Vagin ret = match_int(args, &arg); 778769031c9SAndrei Vagin if (ret) 779769031c9SAndrei Vagin goto out; 780769031c9SAndrei Vagin if (arg != 1) { 781769031c9SAndrei Vagin pr_err("bogus fd_async_io=%d value\n", arg); 782769031c9SAndrei Vagin ret = -EINVAL; 783769031c9SAndrei Vagin goto out; 784769031c9SAndrei Vagin } 785769031c9SAndrei Vagin 786769031c9SAndrei Vagin pr_debug("FILEIO: Using async I/O" 787769031c9SAndrei Vagin " operations for struct fd_dev\n"); 788769031c9SAndrei Vagin 789769031c9SAndrei Vagin fd_dev->fbd_flags |= FDBD_HAS_ASYNC_IO; 790769031c9SAndrei Vagin break; 791c66ac9dbSNicholas Bellinger default: 792c66ac9dbSNicholas Bellinger break; 793c66ac9dbSNicholas Bellinger } 794c66ac9dbSNicholas Bellinger } 795c66ac9dbSNicholas Bellinger 796c66ac9dbSNicholas Bellinger out: 797c66ac9dbSNicholas Bellinger kfree(orig); 798c66ac9dbSNicholas Bellinger return (!ret) ? count : ret; 799c66ac9dbSNicholas Bellinger } 800c66ac9dbSNicholas Bellinger 8010fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b) 802c66ac9dbSNicholas Bellinger { 8030fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 804c66ac9dbSNicholas Bellinger ssize_t bl = 0; 805c66ac9dbSNicholas Bellinger 806c66ac9dbSNicholas Bellinger bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id); 807769031c9SAndrei Vagin bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s Async: %d\n", 808b32f4c7eSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size, 809b32f4c7eSNicholas Bellinger (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ? 810769031c9SAndrei Vagin "Buffered-WCE" : "O_DSYNC", 811769031c9SAndrei Vagin !!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO)); 812c66ac9dbSNicholas Bellinger return bl; 813c66ac9dbSNicholas Bellinger } 814c66ac9dbSNicholas Bellinger 815c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev) 816c66ac9dbSNicholas Bellinger { 8170fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 818cd9323fdSNicholas Bellinger struct file *f = fd_dev->fd_file; 819cd9323fdSNicholas Bellinger struct inode *i = f->f_mapping->host; 820cd9323fdSNicholas Bellinger unsigned long long dev_size; 821cd9323fdSNicholas Bellinger /* 822cd9323fdSNicholas Bellinger * When using a file that references an underlying struct block_device, 823cd9323fdSNicholas Bellinger * ensure dev_size is always based on the current inode size in order 824cd9323fdSNicholas Bellinger * to handle underlying block_device resize operations. 825cd9323fdSNicholas Bellinger */ 826cd9323fdSNicholas Bellinger if (S_ISBLK(i->i_mode)) 82721363ca8SNicholas Bellinger dev_size = i_size_read(i); 828cd9323fdSNicholas Bellinger else 829cd9323fdSNicholas Bellinger dev_size = fd_dev->fd_dev_size; 830c66ac9dbSNicholas Bellinger 83121363ca8SNicholas Bellinger return div_u64(dev_size - dev->dev_attrib.block_size, 83221363ca8SNicholas Bellinger dev->dev_attrib.block_size); 833c66ac9dbSNicholas Bellinger } 834c66ac9dbSNicholas Bellinger 8350f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev) 8360f5e2ec4SNicholas Bellinger { 8370f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 8380f5e2ec4SNicholas Bellinger struct file *prot_file, *file = fd_dev->fd_file; 8390f5e2ec4SNicholas Bellinger struct inode *inode; 8400f5e2ec4SNicholas Bellinger int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 8410f5e2ec4SNicholas Bellinger char buf[FD_MAX_DEV_PROT_NAME]; 8420f5e2ec4SNicholas Bellinger 8430f5e2ec4SNicholas Bellinger if (!file) { 8440f5e2ec4SNicholas Bellinger pr_err("Unable to locate fd_dev->fd_file\n"); 8450f5e2ec4SNicholas Bellinger return -ENODEV; 8460f5e2ec4SNicholas Bellinger } 8470f5e2ec4SNicholas Bellinger 8480f5e2ec4SNicholas Bellinger inode = file->f_mapping->host; 8490f5e2ec4SNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 8500f5e2ec4SNicholas Bellinger pr_err("FILEIO Protection emulation only supported on" 8510f5e2ec4SNicholas Bellinger " !S_ISBLK\n"); 8520f5e2ec4SNicholas Bellinger return -ENOSYS; 8530f5e2ec4SNicholas Bellinger } 8540f5e2ec4SNicholas Bellinger 8550f5e2ec4SNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) 8560f5e2ec4SNicholas Bellinger flags &= ~O_DSYNC; 8570f5e2ec4SNicholas Bellinger 8580f5e2ec4SNicholas Bellinger snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection", 8590f5e2ec4SNicholas Bellinger fd_dev->fd_dev_name); 8600f5e2ec4SNicholas Bellinger 8610f5e2ec4SNicholas Bellinger prot_file = filp_open(buf, flags, 0600); 8620f5e2ec4SNicholas Bellinger if (IS_ERR(prot_file)) { 8630f5e2ec4SNicholas Bellinger pr_err("filp_open(%s) failed\n", buf); 8640f5e2ec4SNicholas Bellinger ret = PTR_ERR(prot_file); 8650f5e2ec4SNicholas Bellinger return ret; 8660f5e2ec4SNicholas Bellinger } 8670f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = prot_file; 8680f5e2ec4SNicholas Bellinger 8690f5e2ec4SNicholas Bellinger return 0; 8700f5e2ec4SNicholas Bellinger } 8710f5e2ec4SNicholas Bellinger 8720f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev) 8730f5e2ec4SNicholas Bellinger { 8740f5e2ec4SNicholas Bellinger unsigned char *buf; 8750f5e2ec4SNicholas Bellinger int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size; 87664d240b7SAkinobu Mita int ret; 8770f5e2ec4SNicholas Bellinger 8780f5e2ec4SNicholas Bellinger if (!dev->dev_attrib.pi_prot_type) { 8790f5e2ec4SNicholas Bellinger pr_err("Unable to format_prot while pi_prot_type == 0\n"); 8800f5e2ec4SNicholas Bellinger return -ENODEV; 8810f5e2ec4SNicholas Bellinger } 8820f5e2ec4SNicholas Bellinger 8830f5e2ec4SNicholas Bellinger buf = vzalloc(unit_size); 8840f5e2ec4SNicholas Bellinger if (!buf) { 8850f5e2ec4SNicholas Bellinger pr_err("Unable to allocate FILEIO prot buf\n"); 8860f5e2ec4SNicholas Bellinger return -ENOMEM; 8870f5e2ec4SNicholas Bellinger } 8880f5e2ec4SNicholas Bellinger 8890f5e2ec4SNicholas Bellinger pr_debug("Using FILEIO prot_length: %llu\n", 89064d240b7SAkinobu Mita (unsigned long long)(dev->transport->get_blocks(dev) + 1) * 89164d240b7SAkinobu Mita dev->prot_length); 8920f5e2ec4SNicholas Bellinger 89380dcd0c1SSagi Grimberg memset(buf, 0xff, unit_size); 89464d240b7SAkinobu Mita ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1, 89564d240b7SAkinobu Mita buf, unit_size); 8960f5e2ec4SNicholas Bellinger vfree(buf); 8970f5e2ec4SNicholas Bellinger return ret; 8980f5e2ec4SNicholas Bellinger } 8990f5e2ec4SNicholas Bellinger 9000f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev) 9010f5e2ec4SNicholas Bellinger { 9020f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 9030f5e2ec4SNicholas Bellinger 9040f5e2ec4SNicholas Bellinger if (!fd_dev->fd_prot_file) 9050f5e2ec4SNicholas Bellinger return; 9060f5e2ec4SNicholas Bellinger 9070f5e2ec4SNicholas Bellinger filp_close(fd_dev->fd_prot_file, NULL); 9080f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = NULL; 9090f5e2ec4SNicholas Bellinger } 9100f5e2ec4SNicholas Bellinger 9119e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = { 9120c2ad7d1SChristoph Hellwig .execute_rw = fd_execute_rw, 913ad67f0d9SChristoph Hellwig .execute_sync_cache = fd_execute_sync_cache, 9147b745c84SNicholas Bellinger .execute_write_same = fd_execute_write_same, 91550642762SAsias He .execute_unmap = fd_execute_unmap, 9160c2ad7d1SChristoph Hellwig }; 9170c2ad7d1SChristoph Hellwig 918de103c93SChristoph Hellwig static sense_reason_t 919de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd) 9200c2ad7d1SChristoph Hellwig { 9219e999a6cSChristoph Hellwig return sbc_parse_cdb(cmd, &fd_sbc_ops); 9220c2ad7d1SChristoph Hellwig } 9230c2ad7d1SChristoph Hellwig 9240a06d430SChristoph Hellwig static const struct target_backend_ops fileio_ops = { 925c66ac9dbSNicholas Bellinger .name = "fileio", 9260fd97ccfSChristoph Hellwig .inquiry_prod = "FILEIO", 9270fd97ccfSChristoph Hellwig .inquiry_rev = FD_VERSION, 928c66ac9dbSNicholas Bellinger .owner = THIS_MODULE, 929c66ac9dbSNicholas Bellinger .attach_hba = fd_attach_hba, 930c66ac9dbSNicholas Bellinger .detach_hba = fd_detach_hba, 9310fd97ccfSChristoph Hellwig .alloc_device = fd_alloc_device, 9320fd97ccfSChristoph Hellwig .configure_device = fd_configure_device, 93392634706SMike Christie .destroy_device = fd_destroy_device, 934c66ac9dbSNicholas Bellinger .free_device = fd_free_device, 9350c2ad7d1SChristoph Hellwig .parse_cdb = fd_parse_cdb, 936c66ac9dbSNicholas Bellinger .set_configfs_dev_params = fd_set_configfs_dev_params, 937c66ac9dbSNicholas Bellinger .show_configfs_dev_params = fd_show_configfs_dev_params, 9386f23ac8aSChristoph Hellwig .get_device_type = sbc_get_device_type, 939c66ac9dbSNicholas Bellinger .get_blocks = fd_get_blocks, 9400f5e2ec4SNicholas Bellinger .init_prot = fd_init_prot, 9410f5e2ec4SNicholas Bellinger .format_prot = fd_format_prot, 9420f5e2ec4SNicholas Bellinger .free_prot = fd_free_prot, 9435873c4d1SChristoph Hellwig .tb_dev_attrib_attrs = sbc_attrib_attrs, 944c66ac9dbSNicholas Bellinger }; 945c66ac9dbSNicholas Bellinger 946c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void) 947c66ac9dbSNicholas Bellinger { 9480a06d430SChristoph Hellwig return transport_backend_register(&fileio_ops); 949c66ac9dbSNicholas Bellinger } 950c66ac9dbSNicholas Bellinger 95163b91d5aSAsias He static void __exit fileio_module_exit(void) 952c66ac9dbSNicholas Bellinger { 9530a06d430SChristoph Hellwig target_backend_unregister(&fileio_ops); 954c66ac9dbSNicholas Bellinger } 955c66ac9dbSNicholas Bellinger 956c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin"); 957c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 958c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL"); 959c66ac9dbSNicholas Bellinger 960c66ac9dbSNicholas Bellinger module_init(fileio_module_init); 961c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit); 962