1c66ac9dbSNicholas Bellinger /******************************************************************************* 2c66ac9dbSNicholas Bellinger * Filename: target_core_file.c 3c66ac9dbSNicholas Bellinger * 4c66ac9dbSNicholas Bellinger * This file contains the Storage Engine <-> FILEIO transport specific functions 5c66ac9dbSNicholas Bellinger * 64c76251eSNicholas Bellinger * (c) Copyright 2005-2013 Datera, Inc. 7c66ac9dbSNicholas Bellinger * 8c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org> 9c66ac9dbSNicholas Bellinger * 10c66ac9dbSNicholas Bellinger * This program is free software; you can redistribute it and/or modify 11c66ac9dbSNicholas Bellinger * it under the terms of the GNU General Public License as published by 12c66ac9dbSNicholas Bellinger * the Free Software Foundation; either version 2 of the License, or 13c66ac9dbSNicholas Bellinger * (at your option) any later version. 14c66ac9dbSNicholas Bellinger * 15c66ac9dbSNicholas Bellinger * This program is distributed in the hope that it will be useful, 16c66ac9dbSNicholas Bellinger * but WITHOUT ANY WARRANTY; without even the implied warranty of 17c66ac9dbSNicholas Bellinger * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18c66ac9dbSNicholas Bellinger * GNU General Public License for more details. 19c66ac9dbSNicholas Bellinger * 20c66ac9dbSNicholas Bellinger * You should have received a copy of the GNU General Public License 21c66ac9dbSNicholas Bellinger * along with this program; if not, write to the Free Software 22c66ac9dbSNicholas Bellinger * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23c66ac9dbSNicholas Bellinger * 24c66ac9dbSNicholas Bellinger ******************************************************************************/ 25c66ac9dbSNicholas Bellinger 26c66ac9dbSNicholas Bellinger #include <linux/string.h> 27c66ac9dbSNicholas Bellinger #include <linux/parser.h> 28c66ac9dbSNicholas Bellinger #include <linux/timer.h> 29c66ac9dbSNicholas Bellinger #include <linux/blkdev.h> 30c66ac9dbSNicholas Bellinger #include <linux/slab.h> 31c66ac9dbSNicholas Bellinger #include <linux/spinlock.h> 32827509e3SPaul Gortmaker #include <linux/module.h> 335538d294SDavid S. Miller #include <linux/vmalloc.h> 3470d3ae5cSAsias He #include <linux/falloc.h> 358dcf07beSBart Van Assche #include <linux/uio.h> 36ba929992SBart Van Assche #include <scsi/scsi_proto.h> 3750642762SAsias He #include <asm/unaligned.h> 38c66ac9dbSNicholas Bellinger 39c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 40c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 41c66ac9dbSNicholas Bellinger 42c66ac9dbSNicholas Bellinger #include "target_core_file.h" 43c66ac9dbSNicholas Bellinger 440fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev) 450fd97ccfSChristoph Hellwig { 460fd97ccfSChristoph Hellwig return container_of(dev, struct fd_dev, dev); 470fd97ccfSChristoph Hellwig } 48c66ac9dbSNicholas Bellinger 49c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id) 50c66ac9dbSNicholas Bellinger { 51c66ac9dbSNicholas Bellinger struct fd_host *fd_host; 52c66ac9dbSNicholas Bellinger 53c66ac9dbSNicholas Bellinger fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL); 546708bb27SAndy Grover if (!fd_host) { 556708bb27SAndy Grover pr_err("Unable to allocate memory for struct fd_host\n"); 56e3d6f909SAndy Grover return -ENOMEM; 57c66ac9dbSNicholas Bellinger } 58c66ac9dbSNicholas Bellinger 59c66ac9dbSNicholas Bellinger fd_host->fd_host_id = host_id; 60c66ac9dbSNicholas Bellinger 61e3d6f909SAndy Grover hba->hba_ptr = fd_host; 62c66ac9dbSNicholas Bellinger 636708bb27SAndy Grover pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic" 64c66ac9dbSNicholas Bellinger " Target Core Stack %s\n", hba->hba_id, FD_VERSION, 65ce8dd25dSChristoph Hellwig TARGET_CORE_VERSION); 6695cadaceSNicholas Bellinger pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n", 6795cadaceSNicholas Bellinger hba->hba_id, fd_host->fd_host_id); 68c66ac9dbSNicholas Bellinger 69c66ac9dbSNicholas Bellinger return 0; 70c66ac9dbSNicholas Bellinger } 71c66ac9dbSNicholas Bellinger 72c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba) 73c66ac9dbSNicholas Bellinger { 74c66ac9dbSNicholas Bellinger struct fd_host *fd_host = hba->hba_ptr; 75c66ac9dbSNicholas Bellinger 766708bb27SAndy Grover pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic" 77c66ac9dbSNicholas Bellinger " Target Core\n", hba->hba_id, fd_host->fd_host_id); 78c66ac9dbSNicholas Bellinger 79c66ac9dbSNicholas Bellinger kfree(fd_host); 80c66ac9dbSNicholas Bellinger hba->hba_ptr = NULL; 81c66ac9dbSNicholas Bellinger } 82c66ac9dbSNicholas Bellinger 830fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name) 84c66ac9dbSNicholas Bellinger { 85c66ac9dbSNicholas Bellinger struct fd_dev *fd_dev; 868359cf43SJörn Engel struct fd_host *fd_host = hba->hba_ptr; 87c66ac9dbSNicholas Bellinger 88c66ac9dbSNicholas Bellinger fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL); 896708bb27SAndy Grover if (!fd_dev) { 906708bb27SAndy Grover pr_err("Unable to allocate memory for struct fd_dev\n"); 91c66ac9dbSNicholas Bellinger return NULL; 92c66ac9dbSNicholas Bellinger } 93c66ac9dbSNicholas Bellinger 94c66ac9dbSNicholas Bellinger fd_dev->fd_host = fd_host; 95c66ac9dbSNicholas Bellinger 966708bb27SAndy Grover pr_debug("FILEIO: Allocated fd_dev for %p\n", name); 97c66ac9dbSNicholas Bellinger 980fd97ccfSChristoph Hellwig return &fd_dev->dev; 99c66ac9dbSNicholas Bellinger } 100c66ac9dbSNicholas Bellinger 1010fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev) 102c66ac9dbSNicholas Bellinger { 1030fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 1040fd97ccfSChristoph Hellwig struct fd_host *fd_host = dev->se_hba->hba_ptr; 105c66ac9dbSNicholas Bellinger struct file *file; 106c66ac9dbSNicholas Bellinger struct inode *inode = NULL; 1070fd97ccfSChristoph Hellwig int flags, ret = -EINVAL; 108c66ac9dbSNicholas Bellinger 1090fd97ccfSChristoph Hellwig if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) { 1100fd97ccfSChristoph Hellwig pr_err("Missing fd_dev_name=\n"); 1110fd97ccfSChristoph Hellwig return -EINVAL; 1120fd97ccfSChristoph Hellwig } 113c66ac9dbSNicholas Bellinger 114c66ac9dbSNicholas Bellinger /* 115a4dff304SNicholas Bellinger * Use O_DSYNC by default instead of O_SYNC to forgo syncing 116a4dff304SNicholas Bellinger * of pure timestamp updates. 117c66ac9dbSNicholas Bellinger */ 118a4dff304SNicholas Bellinger flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 1190fd97ccfSChristoph Hellwig 120b32f4c7eSNicholas Bellinger /* 121b32f4c7eSNicholas Bellinger * Optionally allow fd_buffered_io=1 to be enabled for people 122b32f4c7eSNicholas Bellinger * who want use the fs buffer cache as an WriteCache mechanism. 123b32f4c7eSNicholas Bellinger * 124b32f4c7eSNicholas Bellinger * This means that in event of a hard failure, there is a risk 125b32f4c7eSNicholas Bellinger * of silent data-loss if the SCSI client has *not* performed a 126b32f4c7eSNicholas Bellinger * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE 127b32f4c7eSNicholas Bellinger * to write-out the entire device cache. 128b32f4c7eSNicholas Bellinger */ 129b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 130b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n"); 131b32f4c7eSNicholas Bellinger flags &= ~O_DSYNC; 132b32f4c7eSNicholas Bellinger } 133c66ac9dbSNicholas Bellinger 134dbc6e022SAl Viro file = filp_open(fd_dev->fd_dev_name, flags, 0600); 135613640e4SNicholas Bellinger if (IS_ERR(file)) { 136dbc6e022SAl Viro pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name); 137613640e4SNicholas Bellinger ret = PTR_ERR(file); 138613640e4SNicholas Bellinger goto fail; 139613640e4SNicholas Bellinger } 140c66ac9dbSNicholas Bellinger fd_dev->fd_file = file; 141c66ac9dbSNicholas Bellinger /* 142c66ac9dbSNicholas Bellinger * If using a block backend with this struct file, we extract 143c66ac9dbSNicholas Bellinger * fd_dev->fd_[block,dev]_size from struct block_device. 144c66ac9dbSNicholas Bellinger * 145c66ac9dbSNicholas Bellinger * Otherwise, we use the passed fd_size= from configfs 146c66ac9dbSNicholas Bellinger */ 147c66ac9dbSNicholas Bellinger inode = file->f_mapping->host; 148c66ac9dbSNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 1490fd97ccfSChristoph Hellwig struct request_queue *q = bdev_get_queue(inode->i_bdev); 150cd9323fdSNicholas Bellinger unsigned long long dev_size; 1510fd97ccfSChristoph Hellwig 15221363ca8SNicholas Bellinger fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev); 153c66ac9dbSNicholas Bellinger /* 154c66ac9dbSNicholas Bellinger * Determine the number of bytes from i_size_read() minus 155c66ac9dbSNicholas Bellinger * one (1) logical sector from underlying struct block_device 156c66ac9dbSNicholas Bellinger */ 157cd9323fdSNicholas Bellinger dev_size = (i_size_read(file->f_mapping->host) - 158c66ac9dbSNicholas Bellinger fd_dev->fd_block_size); 159c66ac9dbSNicholas Bellinger 1606708bb27SAndy Grover pr_debug("FILEIO: Using size: %llu bytes from struct" 161c66ac9dbSNicholas Bellinger " block_device blocks: %llu logical_block_size: %d\n", 162cd9323fdSNicholas Bellinger dev_size, div_u64(dev_size, fd_dev->fd_block_size), 163c66ac9dbSNicholas Bellinger fd_dev->fd_block_size); 1648a9ebe71SMike Christie 165ea263c7fSMike Christie if (target_configure_unmap_from_queue(&dev->dev_attrib, q)) 16670d3ae5cSAsias He pr_debug("IFILE: BLOCK Discard support available," 16770d3ae5cSAsias He " disabled by default\n"); 16870d3ae5cSAsias He /* 16970d3ae5cSAsias He * Enable write same emulation for IBLOCK and use 0xFFFF as 17070d3ae5cSAsias He * the smaller WRITE_SAME(10) only has a two-byte block count. 17170d3ae5cSAsias He */ 17270d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0xFFFF; 1730463a3feSAsias He 1740463a3feSAsias He if (blk_queue_nonrot(q)) 1750463a3feSAsias He dev->dev_attrib.is_nonrot = 1; 176c66ac9dbSNicholas Bellinger } else { 177c66ac9dbSNicholas Bellinger if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) { 1786708bb27SAndy Grover pr_err("FILEIO: Missing fd_dev_size=" 179c66ac9dbSNicholas Bellinger " parameter, and no backing struct" 180c66ac9dbSNicholas Bellinger " block_device\n"); 181c66ac9dbSNicholas Bellinger goto fail; 182c66ac9dbSNicholas Bellinger } 183c66ac9dbSNicholas Bellinger 18421363ca8SNicholas Bellinger fd_dev->fd_block_size = FD_BLOCKSIZE; 18570d3ae5cSAsias He /* 18670d3ae5cSAsias He * Limit UNMAP emulation to 8k Number of LBAs (NoLB) 18770d3ae5cSAsias He */ 18870d3ae5cSAsias He dev->dev_attrib.max_unmap_lba_count = 0x2000; 18970d3ae5cSAsias He /* 19070d3ae5cSAsias He * Currently hardcoded to 1 in Linux/SCSI code.. 19170d3ae5cSAsias He */ 19270d3ae5cSAsias He dev->dev_attrib.max_unmap_block_desc_count = 1; 19370d3ae5cSAsias He dev->dev_attrib.unmap_granularity = 1; 19470d3ae5cSAsias He dev->dev_attrib.unmap_granularity_alignment = 0; 19570d3ae5cSAsias He 19670d3ae5cSAsias He /* 19770d3ae5cSAsias He * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB) 19870d3ae5cSAsias He * based upon struct iovec limit for vfs_writev() 19970d3ae5cSAsias He */ 20070d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0x1000; 201c66ac9dbSNicholas Bellinger } 202c66ac9dbSNicholas Bellinger 20321363ca8SNicholas Bellinger dev->dev_attrib.hw_block_size = fd_dev->fd_block_size; 20495cadaceSNicholas Bellinger dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES; 20595cadaceSNicholas Bellinger dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size; 2060fd97ccfSChristoph Hellwig dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; 207c66ac9dbSNicholas Bellinger 208b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 209b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Forcing setting of emulate_write_cache=1" 210b32f4c7eSNicholas Bellinger " with FDBD_HAS_BUFFERED_IO_WCE\n"); 2110fd97ccfSChristoph Hellwig dev->dev_attrib.emulate_write_cache = 1; 212b32f4c7eSNicholas Bellinger } 213b32f4c7eSNicholas Bellinger 214c66ac9dbSNicholas Bellinger fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++; 215c66ac9dbSNicholas Bellinger fd_dev->fd_queue_depth = dev->queue_depth; 216c66ac9dbSNicholas Bellinger 2176708bb27SAndy Grover pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s," 218c66ac9dbSNicholas Bellinger " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id, 219c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size); 220c66ac9dbSNicholas Bellinger 2210fd97ccfSChristoph Hellwig return 0; 222c66ac9dbSNicholas Bellinger fail: 223c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 224c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 225c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 226c66ac9dbSNicholas Bellinger } 2270fd97ccfSChristoph Hellwig return ret; 228c66ac9dbSNicholas Bellinger } 229c66ac9dbSNicholas Bellinger 2304cc987eaSNicholas Bellinger static void fd_dev_call_rcu(struct rcu_head *p) 2314cc987eaSNicholas Bellinger { 2324cc987eaSNicholas Bellinger struct se_device *dev = container_of(p, struct se_device, rcu_head); 2334cc987eaSNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 2344cc987eaSNicholas Bellinger 2354cc987eaSNicholas Bellinger kfree(fd_dev); 2364cc987eaSNicholas Bellinger } 2374cc987eaSNicholas Bellinger 2380fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev) 239c66ac9dbSNicholas Bellinger { 24092634706SMike Christie call_rcu(&dev->rcu_head, fd_dev_call_rcu); 24192634706SMike Christie } 24292634706SMike Christie 24392634706SMike Christie static void fd_destroy_device(struct se_device *dev) 24492634706SMike Christie { 2450fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 246c66ac9dbSNicholas Bellinger 247c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 248c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 249c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 250c66ac9dbSNicholas Bellinger } 251c66ac9dbSNicholas Bellinger } 252c66ac9dbSNicholas Bellinger 253769031c9SAndrei Vagin struct target_core_file_cmd { 254769031c9SAndrei Vagin unsigned long len; 255769031c9SAndrei Vagin struct se_cmd *cmd; 256769031c9SAndrei Vagin struct kiocb iocb; 257769031c9SAndrei Vagin }; 258769031c9SAndrei Vagin 259769031c9SAndrei Vagin static void cmd_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) 260769031c9SAndrei Vagin { 261769031c9SAndrei Vagin struct target_core_file_cmd *cmd; 262769031c9SAndrei Vagin 263769031c9SAndrei Vagin cmd = container_of(iocb, struct target_core_file_cmd, iocb); 264769031c9SAndrei Vagin 265769031c9SAndrei Vagin if (ret != cmd->len) 266769031c9SAndrei Vagin target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION); 267769031c9SAndrei Vagin else 268769031c9SAndrei Vagin target_complete_cmd(cmd->cmd, SAM_STAT_GOOD); 269769031c9SAndrei Vagin 270769031c9SAndrei Vagin kfree(cmd); 271769031c9SAndrei Vagin } 272769031c9SAndrei Vagin 273769031c9SAndrei Vagin static sense_reason_t 274769031c9SAndrei Vagin fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 275769031c9SAndrei Vagin enum dma_data_direction data_direction) 276769031c9SAndrei Vagin { 277769031c9SAndrei Vagin int is_write = !(data_direction == DMA_FROM_DEVICE); 278769031c9SAndrei Vagin struct se_device *dev = cmd->se_dev; 279769031c9SAndrei Vagin struct fd_dev *fd_dev = FD_DEV(dev); 280769031c9SAndrei Vagin struct file *file = fd_dev->fd_file; 281769031c9SAndrei Vagin struct target_core_file_cmd *aio_cmd; 282769031c9SAndrei Vagin struct iov_iter iter = {}; 283769031c9SAndrei Vagin struct scatterlist *sg; 284769031c9SAndrei Vagin struct bio_vec *bvec; 285769031c9SAndrei Vagin ssize_t len = 0; 286769031c9SAndrei Vagin int ret = 0, i; 287769031c9SAndrei Vagin 288769031c9SAndrei Vagin aio_cmd = kmalloc(sizeof(struct target_core_file_cmd), GFP_KERNEL); 289769031c9SAndrei Vagin if (!aio_cmd) 290769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 291769031c9SAndrei Vagin 292769031c9SAndrei Vagin bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL); 293769031c9SAndrei Vagin if (!bvec) { 294769031c9SAndrei Vagin kfree(aio_cmd); 295769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 296769031c9SAndrei Vagin } 297769031c9SAndrei Vagin 298769031c9SAndrei Vagin for_each_sg(sgl, sg, sgl_nents, i) { 299769031c9SAndrei Vagin bvec[i].bv_page = sg_page(sg); 300769031c9SAndrei Vagin bvec[i].bv_len = sg->length; 301769031c9SAndrei Vagin bvec[i].bv_offset = sg->offset; 302769031c9SAndrei Vagin 303769031c9SAndrei Vagin len += sg->length; 304769031c9SAndrei Vagin } 305769031c9SAndrei Vagin 306769031c9SAndrei Vagin iov_iter_bvec(&iter, ITER_BVEC | is_write, bvec, sgl_nents, len); 307769031c9SAndrei Vagin 308769031c9SAndrei Vagin aio_cmd->cmd = cmd; 309769031c9SAndrei Vagin aio_cmd->len = len; 310769031c9SAndrei Vagin aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size; 311769031c9SAndrei Vagin aio_cmd->iocb.ki_filp = file; 312769031c9SAndrei Vagin aio_cmd->iocb.ki_complete = cmd_rw_aio_complete; 313769031c9SAndrei Vagin aio_cmd->iocb.ki_flags = IOCB_DIRECT; 314769031c9SAndrei Vagin 315769031c9SAndrei Vagin if (is_write && (cmd->se_cmd_flags & SCF_FUA)) 316769031c9SAndrei Vagin aio_cmd->iocb.ki_flags |= IOCB_DSYNC; 317769031c9SAndrei Vagin 318769031c9SAndrei Vagin if (is_write) 319769031c9SAndrei Vagin ret = call_write_iter(file, &aio_cmd->iocb, &iter); 320769031c9SAndrei Vagin else 321769031c9SAndrei Vagin ret = call_read_iter(file, &aio_cmd->iocb, &iter); 322769031c9SAndrei Vagin 323769031c9SAndrei Vagin kfree(bvec); 324769031c9SAndrei Vagin 325769031c9SAndrei Vagin if (ret != -EIOCBQUEUED) 326769031c9SAndrei Vagin cmd_rw_aio_complete(&aio_cmd->iocb, ret, 0); 327769031c9SAndrei Vagin 328769031c9SAndrei Vagin return 0; 329769031c9SAndrei Vagin } 330769031c9SAndrei Vagin 3318287fa5fSSagi Grimberg static int fd_do_rw(struct se_cmd *cmd, struct file *fd, 3328287fa5fSSagi Grimberg u32 block_size, struct scatterlist *sgl, 3338287fa5fSSagi Grimberg u32 sgl_nents, u32 data_length, int is_write) 33442201b55SNicholas Bellinger { 3355787cacdSChristoph Hellwig struct scatterlist *sg; 3366b9a44d0SChristoph Hellwig struct iov_iter iter; 3376b9a44d0SChristoph Hellwig struct bio_vec *bvec; 3386b9a44d0SChristoph Hellwig ssize_t len = 0; 3398287fa5fSSagi Grimberg loff_t pos = (cmd->t_task_lba * block_size); 340c66ac9dbSNicholas Bellinger int ret = 0, i; 341c66ac9dbSNicholas Bellinger 3426b9a44d0SChristoph Hellwig bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL); 3436b9a44d0SChristoph Hellwig if (!bvec) { 3446708bb27SAndy Grover pr_err("Unable to allocate fd_do_readv iov[]\n"); 345e3d6f909SAndy Grover return -ENOMEM; 346c66ac9dbSNicholas Bellinger } 347c66ac9dbSNicholas Bellinger 3485787cacdSChristoph Hellwig for_each_sg(sgl, sg, sgl_nents, i) { 3496b9a44d0SChristoph Hellwig bvec[i].bv_page = sg_page(sg); 3506b9a44d0SChristoph Hellwig bvec[i].bv_len = sg->length; 3516b9a44d0SChristoph Hellwig bvec[i].bv_offset = sg->offset; 3526b9a44d0SChristoph Hellwig 3536b9a44d0SChristoph Hellwig len += sg->length; 354c66ac9dbSNicholas Bellinger } 355c66ac9dbSNicholas Bellinger 3566b9a44d0SChristoph Hellwig iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len); 357778229afSSebastian Andrzej Siewior if (is_write) 358abbb6589SChristoph Hellwig ret = vfs_iter_write(fd, &iter, &pos, 0); 359778229afSSebastian Andrzej Siewior else 36018e9710eSChristoph Hellwig ret = vfs_iter_read(fd, &iter, &pos, 0); 361778229afSSebastian Andrzej Siewior 362778229afSSebastian Andrzej Siewior if (is_write) { 3638287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 364778229afSSebastian Andrzej Siewior pr_err("%s() write returned %d\n", __func__, ret); 365f11b55d1SDmitry Monakhov if (ret >= 0) 366f11b55d1SDmitry Monakhov ret = -EINVAL; 367778229afSSebastian Andrzej Siewior } 368778229afSSebastian Andrzej Siewior } else { 369c66ac9dbSNicholas Bellinger /* 370c66ac9dbSNicholas Bellinger * Return zeros and GOOD status even if the READ did not return 371c66ac9dbSNicholas Bellinger * the expected virt_size for struct file w/o a backing struct 372c66ac9dbSNicholas Bellinger * block_device. 373c66ac9dbSNicholas Bellinger */ 374496ad9aaSAl Viro if (S_ISBLK(file_inode(fd)->i_mode)) { 3758287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 376778229afSSebastian Andrzej Siewior pr_err("%s() returned %d, expecting %u for " 377778229afSSebastian Andrzej Siewior "S_ISBLK\n", __func__, ret, 3788287fa5fSSagi Grimberg data_length); 379f11b55d1SDmitry Monakhov if (ret >= 0) 380f11b55d1SDmitry Monakhov ret = -EINVAL; 381c66ac9dbSNicholas Bellinger } 382c66ac9dbSNicholas Bellinger } else { 383c66ac9dbSNicholas Bellinger if (ret < 0) { 384778229afSSebastian Andrzej Siewior pr_err("%s() returned %d for non S_ISBLK\n", 385778229afSSebastian Andrzej Siewior __func__, ret); 386f11b55d1SDmitry Monakhov } else if (ret != data_length) { 387f11b55d1SDmitry Monakhov /* 388f11b55d1SDmitry Monakhov * Short read case: 389f11b55d1SDmitry Monakhov * Probably some one truncate file under us. 390f11b55d1SDmitry Monakhov * We must explicitly zero sg-pages to prevent 391f11b55d1SDmitry Monakhov * expose uninizialized pages to userspace. 392f11b55d1SDmitry Monakhov */ 393f11b55d1SDmitry Monakhov if (ret < data_length) 394f11b55d1SDmitry Monakhov ret += iov_iter_zero(data_length - ret, &iter); 395f11b55d1SDmitry Monakhov else 396f11b55d1SDmitry Monakhov ret = -EINVAL; 397f11b55d1SDmitry Monakhov } 398f11b55d1SDmitry Monakhov } 399f11b55d1SDmitry Monakhov } 400f11b55d1SDmitry Monakhov kfree(bvec); 401e3d6f909SAndy Grover return ret; 402c66ac9dbSNicholas Bellinger } 403c66ac9dbSNicholas Bellinger 404de103c93SChristoph Hellwig static sense_reason_t 405de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd) 406c66ac9dbSNicholas Bellinger { 407c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 4080fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 409a1d8b49aSAndy Grover int immed = (cmd->t_task_cdb[1] & 0x2); 410c66ac9dbSNicholas Bellinger loff_t start, end; 411c66ac9dbSNicholas Bellinger int ret; 412c66ac9dbSNicholas Bellinger 413c66ac9dbSNicholas Bellinger /* 414c66ac9dbSNicholas Bellinger * If the Immediate bit is set, queue up the GOOD response 415c66ac9dbSNicholas Bellinger * for this SYNCHRONIZE_CACHE op 416c66ac9dbSNicholas Bellinger */ 417c66ac9dbSNicholas Bellinger if (immed) 4185787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 419c66ac9dbSNicholas Bellinger 420c66ac9dbSNicholas Bellinger /* 421c66ac9dbSNicholas Bellinger * Determine if we will be flushing the entire device. 422c66ac9dbSNicholas Bellinger */ 423a1d8b49aSAndy Grover if (cmd->t_task_lba == 0 && cmd->data_length == 0) { 424c66ac9dbSNicholas Bellinger start = 0; 425c66ac9dbSNicholas Bellinger end = LLONG_MAX; 426c66ac9dbSNicholas Bellinger } else { 4270fd97ccfSChristoph Hellwig start = cmd->t_task_lba * dev->dev_attrib.block_size; 428c66ac9dbSNicholas Bellinger if (cmd->data_length) 42962d3ab49SZach Brown end = start + cmd->data_length - 1; 430c66ac9dbSNicholas Bellinger else 431c66ac9dbSNicholas Bellinger end = LLONG_MAX; 432c66ac9dbSNicholas Bellinger } 433c66ac9dbSNicholas Bellinger 434c66ac9dbSNicholas Bellinger ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); 435c66ac9dbSNicholas Bellinger if (ret != 0) 4366708bb27SAndy Grover pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret); 437c66ac9dbSNicholas Bellinger 4385787cacdSChristoph Hellwig if (immed) 439ad67f0d9SChristoph Hellwig return 0; 4405787cacdSChristoph Hellwig 441de103c93SChristoph Hellwig if (ret) 4425787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); 443de103c93SChristoph Hellwig else 4445787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 445ad67f0d9SChristoph Hellwig 446ad67f0d9SChristoph Hellwig return 0; 447c66ac9dbSNicholas Bellinger } 448c66ac9dbSNicholas Bellinger 4497b745c84SNicholas Bellinger static sense_reason_t 4507b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd) 4517b745c84SNicholas Bellinger { 4527b745c84SNicholas Bellinger struct se_device *se_dev = cmd->se_dev; 4537b745c84SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(se_dev); 4547b745c84SNicholas Bellinger loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size; 455d4c5dcacSChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 456d4c5dcacSChristoph Hellwig struct iov_iter iter; 457d4c5dcacSChristoph Hellwig struct bio_vec *bvec; 458d4c5dcacSChristoph Hellwig unsigned int len = 0, i; 459d4c5dcacSChristoph Hellwig ssize_t ret; 4607b745c84SNicholas Bellinger 4617b745c84SNicholas Bellinger if (!nolb) { 4627b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 4637b745c84SNicholas Bellinger return 0; 4647b745c84SNicholas Bellinger } 465afd73f1bSNicholas Bellinger if (cmd->prot_op) { 466afd73f1bSNicholas Bellinger pr_err("WRITE_SAME: Protection information with FILEIO" 467afd73f1bSNicholas Bellinger " backends not supported\n"); 468afd73f1bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 469afd73f1bSNicholas Bellinger } 4707b745c84SNicholas Bellinger 4717b745c84SNicholas Bellinger if (cmd->t_data_nents > 1 || 472d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) { 4737b745c84SNicholas Bellinger pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" 474d4c5dcacSChristoph Hellwig " block_size: %u\n", 475d4c5dcacSChristoph Hellwig cmd->t_data_nents, 476d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length, 4777b745c84SNicholas Bellinger cmd->se_dev->dev_attrib.block_size); 4787b745c84SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 4797b745c84SNicholas Bellinger } 4807b745c84SNicholas Bellinger 481d4c5dcacSChristoph Hellwig bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL); 482d4c5dcacSChristoph Hellwig if (!bvec) 4837b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4847b745c84SNicholas Bellinger 485d4c5dcacSChristoph Hellwig for (i = 0; i < nolb; i++) { 486d4c5dcacSChristoph Hellwig bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]); 487d4c5dcacSChristoph Hellwig bvec[i].bv_len = cmd->t_data_sg[0].length; 488d4c5dcacSChristoph Hellwig bvec[i].bv_offset = cmd->t_data_sg[0].offset; 489d4c5dcacSChristoph Hellwig 490d4c5dcacSChristoph Hellwig len += se_dev->dev_attrib.block_size; 4917b745c84SNicholas Bellinger } 4927b745c84SNicholas Bellinger 493d4c5dcacSChristoph Hellwig iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len); 494abbb6589SChristoph Hellwig ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos, 0); 4957b745c84SNicholas Bellinger 496d4c5dcacSChristoph Hellwig kfree(bvec); 497d4c5dcacSChristoph Hellwig if (ret < 0 || ret != len) { 498d4c5dcacSChristoph Hellwig pr_err("vfs_iter_write() returned %zd for write same\n", ret); 4997b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 5007b745c84SNicholas Bellinger } 5017b745c84SNicholas Bellinger 5027b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 5037b745c84SNicholas Bellinger return 0; 5047b745c84SNicholas Bellinger } 5057b745c84SNicholas Bellinger 50664d240b7SAkinobu Mita static int 50764d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb, 50864d240b7SAkinobu Mita void *buf, size_t bufsize) 50964d240b7SAkinobu Mita { 51064d240b7SAkinobu Mita struct fd_dev *fd_dev = FD_DEV(se_dev); 51164d240b7SAkinobu Mita struct file *prot_fd = fd_dev->fd_prot_file; 51264d240b7SAkinobu Mita sector_t prot_length, prot; 51364d240b7SAkinobu Mita loff_t pos = lba * se_dev->prot_length; 51464d240b7SAkinobu Mita 51564d240b7SAkinobu Mita if (!prot_fd) { 51664d240b7SAkinobu Mita pr_err("Unable to locate fd_dev->fd_prot_file\n"); 51764d240b7SAkinobu Mita return -ENODEV; 51864d240b7SAkinobu Mita } 51964d240b7SAkinobu Mita 52064d240b7SAkinobu Mita prot_length = nolb * se_dev->prot_length; 52164d240b7SAkinobu Mita 52264d240b7SAkinobu Mita for (prot = 0; prot < prot_length;) { 52364d240b7SAkinobu Mita sector_t len = min_t(sector_t, bufsize, prot_length - prot); 524e13ec939SChristoph Hellwig ssize_t ret = kernel_write(prot_fd, buf, len, &pos); 52564d240b7SAkinobu Mita 52664d240b7SAkinobu Mita if (ret != len) { 52764d240b7SAkinobu Mita pr_err("vfs_write to prot file failed: %zd\n", ret); 52864d240b7SAkinobu Mita return ret < 0 ? ret : -ENODEV; 52964d240b7SAkinobu Mita } 53064d240b7SAkinobu Mita prot += ret; 53164d240b7SAkinobu Mita } 53264d240b7SAkinobu Mita 53364d240b7SAkinobu Mita return 0; 53464d240b7SAkinobu Mita } 53564d240b7SAkinobu Mita 53664d240b7SAkinobu Mita static int 53764d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 53864d240b7SAkinobu Mita { 53964d240b7SAkinobu Mita void *buf; 54064d240b7SAkinobu Mita int rc; 54164d240b7SAkinobu Mita 54264d240b7SAkinobu Mita buf = (void *)__get_free_page(GFP_KERNEL); 54364d240b7SAkinobu Mita if (!buf) { 54464d240b7SAkinobu Mita pr_err("Unable to allocate FILEIO prot buf\n"); 54564d240b7SAkinobu Mita return -ENOMEM; 54664d240b7SAkinobu Mita } 54764d240b7SAkinobu Mita memset(buf, 0xff, PAGE_SIZE); 54864d240b7SAkinobu Mita 54964d240b7SAkinobu Mita rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE); 55064d240b7SAkinobu Mita 55164d240b7SAkinobu Mita free_page((unsigned long)buf); 55264d240b7SAkinobu Mita 55364d240b7SAkinobu Mita return rc; 55464d240b7SAkinobu Mita } 55564d240b7SAkinobu Mita 556de103c93SChristoph Hellwig static sense_reason_t 55762e46942SChristoph Hellwig fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 55870d3ae5cSAsias He { 55962e46942SChristoph Hellwig struct file *file = FD_DEV(cmd->se_dev)->fd_file; 56070d3ae5cSAsias He struct inode *inode = file->f_mapping->host; 56170d3ae5cSAsias He int ret; 56270d3ae5cSAsias He 563594e25e7SJiang Yi if (!nolb) { 564594e25e7SJiang Yi return 0; 565594e25e7SJiang Yi } 566594e25e7SJiang Yi 56764d240b7SAkinobu Mita if (cmd->se_dev->dev_attrib.pi_prot_type) { 56864d240b7SAkinobu Mita ret = fd_do_prot_unmap(cmd, lba, nolb); 56964d240b7SAkinobu Mita if (ret) 57064d240b7SAkinobu Mita return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 57164d240b7SAkinobu Mita } 57264d240b7SAkinobu Mita 57370d3ae5cSAsias He if (S_ISBLK(inode->i_mode)) { 57470d3ae5cSAsias He /* The backend is block device, use discard */ 57570d3ae5cSAsias He struct block_device *bdev = inode->i_bdev; 5768a9ebe71SMike Christie struct se_device *dev = cmd->se_dev; 57770d3ae5cSAsias He 5788a9ebe71SMike Christie ret = blkdev_issue_discard(bdev, 5798a9ebe71SMike Christie target_to_linux_sector(dev, lba), 5808a9ebe71SMike Christie target_to_linux_sector(dev, nolb), 5818a9ebe71SMike Christie GFP_KERNEL, 0); 58270d3ae5cSAsias He if (ret < 0) { 58370d3ae5cSAsias He pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n", 58470d3ae5cSAsias He ret); 58570d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 58670d3ae5cSAsias He } 58770d3ae5cSAsias He } else { 58870d3ae5cSAsias He /* The backend is normal file, use fallocate */ 58943f55bbbSAsias He struct se_device *se_dev = cmd->se_dev; 59043f55bbbSAsias He loff_t pos = lba * se_dev->dev_attrib.block_size; 59170d3ae5cSAsias He unsigned int len = nolb * se_dev->dev_attrib.block_size; 59270d3ae5cSAsias He int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 59370d3ae5cSAsias He 59470d3ae5cSAsias He if (!file->f_op->fallocate) 59570d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 59670d3ae5cSAsias He 59770d3ae5cSAsias He ret = file->f_op->fallocate(file, mode, pos, len); 59870d3ae5cSAsias He if (ret < 0) { 59970d3ae5cSAsias He pr_warn("FILEIO: fallocate() failed: %d\n", ret); 60070d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 60170d3ae5cSAsias He } 60270d3ae5cSAsias He } 60370d3ae5cSAsias He 60443f55bbbSAsias He return 0; 60543f55bbbSAsias He } 60643f55bbbSAsias He 60743f55bbbSAsias He static sense_reason_t 608769031c9SAndrei Vagin fd_execute_rw_buffered(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 609a82a9538SNicholas Bellinger enum dma_data_direction data_direction) 610c66ac9dbSNicholas Bellinger { 611c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 6128287fa5fSSagi Grimberg struct fd_dev *fd_dev = FD_DEV(dev); 6138287fa5fSSagi Grimberg struct file *file = fd_dev->fd_file; 6148287fa5fSSagi Grimberg struct file *pfile = fd_dev->fd_prot_file; 61542201b55SNicholas Bellinger sense_reason_t rc; 616c66ac9dbSNicholas Bellinger int ret = 0; 617046ba642SNicholas Bellinger /* 618c66ac9dbSNicholas Bellinger * Call vectorized fileio functions to map struct scatterlist 619c66ac9dbSNicholas Bellinger * physical memory addresses to struct iovec virtual memory. 620c66ac9dbSNicholas Bellinger */ 6215787cacdSChristoph Hellwig if (data_direction == DMA_FROM_DEVICE) { 622ee920469SNicholas Bellinger if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 6238287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 6248287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 6258287fa5fSSagi Grimberg cmd->prot_length, 0); 62642201b55SNicholas Bellinger if (ret < 0) 62742201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 62842201b55SNicholas Bellinger } 62942201b55SNicholas Bellinger 6308287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6318287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 0); 63242201b55SNicholas Bellinger 633056e8924SDmitry Monakhov if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type && 634056e8924SDmitry Monakhov dev->dev_attrib.pi_prot_verify) { 6358287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 6368287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 63742201b55SNicholas Bellinger 638f75b6faeSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 639f75b6faeSSagi Grimberg 0, cmd->t_prot_sg, 0); 6408287fa5fSSagi Grimberg if (rc) 64142201b55SNicholas Bellinger return rc; 64242201b55SNicholas Bellinger } 643c66ac9dbSNicholas Bellinger } else { 644056e8924SDmitry Monakhov if (cmd->prot_type && dev->dev_attrib.pi_prot_type && 645056e8924SDmitry Monakhov dev->dev_attrib.pi_prot_verify) { 6468287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 6478287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 64842201b55SNicholas Bellinger 6498287fa5fSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 6508287fa5fSSagi Grimberg 0, cmd->t_prot_sg, 0); 6518287fa5fSSagi Grimberg if (rc) 65242201b55SNicholas Bellinger return rc; 65342201b55SNicholas Bellinger } 65442201b55SNicholas Bellinger 6558287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6568287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 1); 657a4dff304SNicholas Bellinger /* 658125d0119SHannes Reinecke * Perform implicit vfs_fsync_range() for fd_do_writev() ops 659a4dff304SNicholas Bellinger * for SCSI WRITEs with Forced Unit Access (FUA) set. 660a4dff304SNicholas Bellinger * Allow this to happen independent of WCE=0 setting. 661a4dff304SNicholas Bellinger */ 662814e5b45SChristoph Hellwig if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) { 663a4dff304SNicholas Bellinger loff_t start = cmd->t_task_lba * 6640fd97ccfSChristoph Hellwig dev->dev_attrib.block_size; 66562d3ab49SZach Brown loff_t end; 66662d3ab49SZach Brown 66762d3ab49SZach Brown if (cmd->data_length) 66862d3ab49SZach Brown end = start + cmd->data_length - 1; 66962d3ab49SZach Brown else 67062d3ab49SZach Brown end = LLONG_MAX; 671c66ac9dbSNicholas Bellinger 672a4dff304SNicholas Bellinger vfs_fsync_range(fd_dev->fd_file, start, end, 1); 673a4dff304SNicholas Bellinger } 674c66ac9dbSNicholas Bellinger 675ee920469SNicholas Bellinger if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) { 6768287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 6778287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 6788287fa5fSSagi Grimberg cmd->prot_length, 1); 679de103c93SChristoph Hellwig if (ret < 0) 680de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 68142201b55SNicholas Bellinger } 68242201b55SNicholas Bellinger } 68342201b55SNicholas Bellinger 6848287fa5fSSagi Grimberg if (ret < 0) 68542201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 686de103c93SChristoph Hellwig 6875787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 68803e98c9eSNicholas Bellinger return 0; 689c66ac9dbSNicholas Bellinger } 690c66ac9dbSNicholas Bellinger 691769031c9SAndrei Vagin static sense_reason_t 692769031c9SAndrei Vagin fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 693769031c9SAndrei Vagin enum dma_data_direction data_direction) 694769031c9SAndrei Vagin { 695769031c9SAndrei Vagin struct se_device *dev = cmd->se_dev; 696769031c9SAndrei Vagin struct fd_dev *fd_dev = FD_DEV(dev); 697769031c9SAndrei Vagin 698769031c9SAndrei Vagin /* 699769031c9SAndrei Vagin * We are currently limited by the number of iovecs (2048) per 700769031c9SAndrei Vagin * single vfs_[writev,readv] call. 701769031c9SAndrei Vagin */ 702769031c9SAndrei Vagin if (cmd->data_length > FD_MAX_BYTES) { 703769031c9SAndrei Vagin pr_err("FILEIO: Not able to process I/O of %u bytes due to" 704769031c9SAndrei Vagin "FD_MAX_BYTES: %u iovec count limitation\n", 705769031c9SAndrei Vagin cmd->data_length, FD_MAX_BYTES); 706769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 707769031c9SAndrei Vagin } 708769031c9SAndrei Vagin 709769031c9SAndrei Vagin if (fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO) 710769031c9SAndrei Vagin return fd_execute_rw_aio(cmd, sgl, sgl_nents, data_direction); 711769031c9SAndrei Vagin return fd_execute_rw_buffered(cmd, sgl, sgl_nents, data_direction); 712769031c9SAndrei Vagin } 713769031c9SAndrei Vagin 714c66ac9dbSNicholas Bellinger enum { 715769031c9SAndrei Vagin Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, 716769031c9SAndrei Vagin Opt_fd_async_io, Opt_err 717c66ac9dbSNicholas Bellinger }; 718c66ac9dbSNicholas Bellinger 719c66ac9dbSNicholas Bellinger static match_table_t tokens = { 720c66ac9dbSNicholas Bellinger {Opt_fd_dev_name, "fd_dev_name=%s"}, 721c66ac9dbSNicholas Bellinger {Opt_fd_dev_size, "fd_dev_size=%s"}, 722b32f4c7eSNicholas Bellinger {Opt_fd_buffered_io, "fd_buffered_io=%d"}, 723769031c9SAndrei Vagin {Opt_fd_async_io, "fd_async_io=%d"}, 724c66ac9dbSNicholas Bellinger {Opt_err, NULL} 725c66ac9dbSNicholas Bellinger }; 726c66ac9dbSNicholas Bellinger 7270fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev, 728c66ac9dbSNicholas Bellinger const char *page, ssize_t count) 729c66ac9dbSNicholas Bellinger { 7300fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 731c66ac9dbSNicholas Bellinger char *orig, *ptr, *arg_p, *opts; 732c66ac9dbSNicholas Bellinger substring_t args[MAX_OPT_ARGS]; 733b32f4c7eSNicholas Bellinger int ret = 0, arg, token; 734c66ac9dbSNicholas Bellinger 735c66ac9dbSNicholas Bellinger opts = kstrdup(page, GFP_KERNEL); 736c66ac9dbSNicholas Bellinger if (!opts) 737c66ac9dbSNicholas Bellinger return -ENOMEM; 738c66ac9dbSNicholas Bellinger 739c66ac9dbSNicholas Bellinger orig = opts; 740c66ac9dbSNicholas Bellinger 74190c161b6SSebastian Andrzej Siewior while ((ptr = strsep(&opts, ",\n")) != NULL) { 742c66ac9dbSNicholas Bellinger if (!*ptr) 743c66ac9dbSNicholas Bellinger continue; 744c66ac9dbSNicholas Bellinger 745c66ac9dbSNicholas Bellinger token = match_token(ptr, tokens, args); 746c66ac9dbSNicholas Bellinger switch (token) { 747c66ac9dbSNicholas Bellinger case Opt_fd_dev_name: 748dbc6e022SAl Viro if (match_strlcpy(fd_dev->fd_dev_name, &args[0], 749dbc6e022SAl Viro FD_MAX_DEV_NAME) == 0) { 750dbc6e022SAl Viro ret = -EINVAL; 7516d180253SJesper Juhl break; 7526d180253SJesper Juhl } 7536708bb27SAndy Grover pr_debug("FILEIO: Referencing Path: %s\n", 754c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name); 755c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_PATH; 756c66ac9dbSNicholas Bellinger break; 757c66ac9dbSNicholas Bellinger case Opt_fd_dev_size: 758c66ac9dbSNicholas Bellinger arg_p = match_strdup(&args[0]); 7596d180253SJesper Juhl if (!arg_p) { 7606d180253SJesper Juhl ret = -ENOMEM; 7616d180253SJesper Juhl break; 7626d180253SJesper Juhl } 76357103d7fSJingoo Han ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size); 7646d180253SJesper Juhl kfree(arg_p); 765c66ac9dbSNicholas Bellinger if (ret < 0) { 76657103d7fSJingoo Han pr_err("kstrtoull() failed for" 767c66ac9dbSNicholas Bellinger " fd_dev_size=\n"); 768c66ac9dbSNicholas Bellinger goto out; 769c66ac9dbSNicholas Bellinger } 7706708bb27SAndy Grover pr_debug("FILEIO: Referencing Size: %llu" 771c66ac9dbSNicholas Bellinger " bytes\n", fd_dev->fd_dev_size); 772c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_SIZE; 773c66ac9dbSNicholas Bellinger break; 774b32f4c7eSNicholas Bellinger case Opt_fd_buffered_io: 775ce31c1b0SJoern Engel ret = match_int(args, &arg); 776ce31c1b0SJoern Engel if (ret) 777ce31c1b0SJoern Engel goto out; 778b32f4c7eSNicholas Bellinger if (arg != 1) { 779b32f4c7eSNicholas Bellinger pr_err("bogus fd_buffered_io=%d value\n", arg); 780b32f4c7eSNicholas Bellinger ret = -EINVAL; 781b32f4c7eSNicholas Bellinger goto out; 782b32f4c7eSNicholas Bellinger } 783b32f4c7eSNicholas Bellinger 784b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Using buffered I/O" 785b32f4c7eSNicholas Bellinger " operations for struct fd_dev\n"); 786b32f4c7eSNicholas Bellinger 787b32f4c7eSNicholas Bellinger fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE; 788b32f4c7eSNicholas Bellinger break; 789769031c9SAndrei Vagin case Opt_fd_async_io: 790769031c9SAndrei Vagin ret = match_int(args, &arg); 791769031c9SAndrei Vagin if (ret) 792769031c9SAndrei Vagin goto out; 793769031c9SAndrei Vagin if (arg != 1) { 794769031c9SAndrei Vagin pr_err("bogus fd_async_io=%d value\n", arg); 795769031c9SAndrei Vagin ret = -EINVAL; 796769031c9SAndrei Vagin goto out; 797769031c9SAndrei Vagin } 798769031c9SAndrei Vagin 799769031c9SAndrei Vagin pr_debug("FILEIO: Using async I/O" 800769031c9SAndrei Vagin " operations for struct fd_dev\n"); 801769031c9SAndrei Vagin 802769031c9SAndrei Vagin fd_dev->fbd_flags |= FDBD_HAS_ASYNC_IO; 803769031c9SAndrei Vagin break; 804c66ac9dbSNicholas Bellinger default: 805c66ac9dbSNicholas Bellinger break; 806c66ac9dbSNicholas Bellinger } 807c66ac9dbSNicholas Bellinger } 808c66ac9dbSNicholas Bellinger 809c66ac9dbSNicholas Bellinger out: 810c66ac9dbSNicholas Bellinger kfree(orig); 811c66ac9dbSNicholas Bellinger return (!ret) ? count : ret; 812c66ac9dbSNicholas Bellinger } 813c66ac9dbSNicholas Bellinger 8140fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b) 815c66ac9dbSNicholas Bellinger { 8160fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 817c66ac9dbSNicholas Bellinger ssize_t bl = 0; 818c66ac9dbSNicholas Bellinger 819c66ac9dbSNicholas Bellinger bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id); 820769031c9SAndrei Vagin bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s Async: %d\n", 821b32f4c7eSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size, 822b32f4c7eSNicholas Bellinger (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ? 823769031c9SAndrei Vagin "Buffered-WCE" : "O_DSYNC", 824769031c9SAndrei Vagin !!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO)); 825c66ac9dbSNicholas Bellinger return bl; 826c66ac9dbSNicholas Bellinger } 827c66ac9dbSNicholas Bellinger 828c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev) 829c66ac9dbSNicholas Bellinger { 8300fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 831cd9323fdSNicholas Bellinger struct file *f = fd_dev->fd_file; 832cd9323fdSNicholas Bellinger struct inode *i = f->f_mapping->host; 833cd9323fdSNicholas Bellinger unsigned long long dev_size; 834cd9323fdSNicholas Bellinger /* 835cd9323fdSNicholas Bellinger * When using a file that references an underlying struct block_device, 836cd9323fdSNicholas Bellinger * ensure dev_size is always based on the current inode size in order 837cd9323fdSNicholas Bellinger * to handle underlying block_device resize operations. 838cd9323fdSNicholas Bellinger */ 839cd9323fdSNicholas Bellinger if (S_ISBLK(i->i_mode)) 84021363ca8SNicholas Bellinger dev_size = i_size_read(i); 841cd9323fdSNicholas Bellinger else 842cd9323fdSNicholas Bellinger dev_size = fd_dev->fd_dev_size; 843c66ac9dbSNicholas Bellinger 84421363ca8SNicholas Bellinger return div_u64(dev_size - dev->dev_attrib.block_size, 84521363ca8SNicholas Bellinger dev->dev_attrib.block_size); 846c66ac9dbSNicholas Bellinger } 847c66ac9dbSNicholas Bellinger 8480f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev) 8490f5e2ec4SNicholas Bellinger { 8500f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 8510f5e2ec4SNicholas Bellinger struct file *prot_file, *file = fd_dev->fd_file; 8520f5e2ec4SNicholas Bellinger struct inode *inode; 8530f5e2ec4SNicholas Bellinger int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 8540f5e2ec4SNicholas Bellinger char buf[FD_MAX_DEV_PROT_NAME]; 8550f5e2ec4SNicholas Bellinger 8560f5e2ec4SNicholas Bellinger if (!file) { 8570f5e2ec4SNicholas Bellinger pr_err("Unable to locate fd_dev->fd_file\n"); 8580f5e2ec4SNicholas Bellinger return -ENODEV; 8590f5e2ec4SNicholas Bellinger } 8600f5e2ec4SNicholas Bellinger 8610f5e2ec4SNicholas Bellinger inode = file->f_mapping->host; 8620f5e2ec4SNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 8630f5e2ec4SNicholas Bellinger pr_err("FILEIO Protection emulation only supported on" 8640f5e2ec4SNicholas Bellinger " !S_ISBLK\n"); 8650f5e2ec4SNicholas Bellinger return -ENOSYS; 8660f5e2ec4SNicholas Bellinger } 8670f5e2ec4SNicholas Bellinger 8680f5e2ec4SNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) 8690f5e2ec4SNicholas Bellinger flags &= ~O_DSYNC; 8700f5e2ec4SNicholas Bellinger 8710f5e2ec4SNicholas Bellinger snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection", 8720f5e2ec4SNicholas Bellinger fd_dev->fd_dev_name); 8730f5e2ec4SNicholas Bellinger 8740f5e2ec4SNicholas Bellinger prot_file = filp_open(buf, flags, 0600); 8750f5e2ec4SNicholas Bellinger if (IS_ERR(prot_file)) { 8760f5e2ec4SNicholas Bellinger pr_err("filp_open(%s) failed\n", buf); 8770f5e2ec4SNicholas Bellinger ret = PTR_ERR(prot_file); 8780f5e2ec4SNicholas Bellinger return ret; 8790f5e2ec4SNicholas Bellinger } 8800f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = prot_file; 8810f5e2ec4SNicholas Bellinger 8820f5e2ec4SNicholas Bellinger return 0; 8830f5e2ec4SNicholas Bellinger } 8840f5e2ec4SNicholas Bellinger 8850f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev) 8860f5e2ec4SNicholas Bellinger { 8870f5e2ec4SNicholas Bellinger unsigned char *buf; 8880f5e2ec4SNicholas Bellinger int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size; 88964d240b7SAkinobu Mita int ret; 8900f5e2ec4SNicholas Bellinger 8910f5e2ec4SNicholas Bellinger if (!dev->dev_attrib.pi_prot_type) { 8920f5e2ec4SNicholas Bellinger pr_err("Unable to format_prot while pi_prot_type == 0\n"); 8930f5e2ec4SNicholas Bellinger return -ENODEV; 8940f5e2ec4SNicholas Bellinger } 8950f5e2ec4SNicholas Bellinger 8960f5e2ec4SNicholas Bellinger buf = vzalloc(unit_size); 8970f5e2ec4SNicholas Bellinger if (!buf) { 8980f5e2ec4SNicholas Bellinger pr_err("Unable to allocate FILEIO prot buf\n"); 8990f5e2ec4SNicholas Bellinger return -ENOMEM; 9000f5e2ec4SNicholas Bellinger } 9010f5e2ec4SNicholas Bellinger 9020f5e2ec4SNicholas Bellinger pr_debug("Using FILEIO prot_length: %llu\n", 90364d240b7SAkinobu Mita (unsigned long long)(dev->transport->get_blocks(dev) + 1) * 90464d240b7SAkinobu Mita dev->prot_length); 9050f5e2ec4SNicholas Bellinger 90680dcd0c1SSagi Grimberg memset(buf, 0xff, unit_size); 90764d240b7SAkinobu Mita ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1, 90864d240b7SAkinobu Mita buf, unit_size); 9090f5e2ec4SNicholas Bellinger vfree(buf); 9100f5e2ec4SNicholas Bellinger return ret; 9110f5e2ec4SNicholas Bellinger } 9120f5e2ec4SNicholas Bellinger 9130f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev) 9140f5e2ec4SNicholas Bellinger { 9150f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 9160f5e2ec4SNicholas Bellinger 9170f5e2ec4SNicholas Bellinger if (!fd_dev->fd_prot_file) 9180f5e2ec4SNicholas Bellinger return; 9190f5e2ec4SNicholas Bellinger 9200f5e2ec4SNicholas Bellinger filp_close(fd_dev->fd_prot_file, NULL); 9210f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = NULL; 9220f5e2ec4SNicholas Bellinger } 9230f5e2ec4SNicholas Bellinger 9249e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = { 9250c2ad7d1SChristoph Hellwig .execute_rw = fd_execute_rw, 926ad67f0d9SChristoph Hellwig .execute_sync_cache = fd_execute_sync_cache, 9277b745c84SNicholas Bellinger .execute_write_same = fd_execute_write_same, 92850642762SAsias He .execute_unmap = fd_execute_unmap, 9290c2ad7d1SChristoph Hellwig }; 9300c2ad7d1SChristoph Hellwig 931de103c93SChristoph Hellwig static sense_reason_t 932de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd) 9330c2ad7d1SChristoph Hellwig { 9349e999a6cSChristoph Hellwig return sbc_parse_cdb(cmd, &fd_sbc_ops); 9350c2ad7d1SChristoph Hellwig } 9360c2ad7d1SChristoph Hellwig 9370a06d430SChristoph Hellwig static const struct target_backend_ops fileio_ops = { 938c66ac9dbSNicholas Bellinger .name = "fileio", 9390fd97ccfSChristoph Hellwig .inquiry_prod = "FILEIO", 9400fd97ccfSChristoph Hellwig .inquiry_rev = FD_VERSION, 941c66ac9dbSNicholas Bellinger .owner = THIS_MODULE, 942c66ac9dbSNicholas Bellinger .attach_hba = fd_attach_hba, 943c66ac9dbSNicholas Bellinger .detach_hba = fd_detach_hba, 9440fd97ccfSChristoph Hellwig .alloc_device = fd_alloc_device, 9450fd97ccfSChristoph Hellwig .configure_device = fd_configure_device, 94692634706SMike Christie .destroy_device = fd_destroy_device, 947c66ac9dbSNicholas Bellinger .free_device = fd_free_device, 9480c2ad7d1SChristoph Hellwig .parse_cdb = fd_parse_cdb, 949c66ac9dbSNicholas Bellinger .set_configfs_dev_params = fd_set_configfs_dev_params, 950c66ac9dbSNicholas Bellinger .show_configfs_dev_params = fd_show_configfs_dev_params, 9516f23ac8aSChristoph Hellwig .get_device_type = sbc_get_device_type, 952c66ac9dbSNicholas Bellinger .get_blocks = fd_get_blocks, 9530f5e2ec4SNicholas Bellinger .init_prot = fd_init_prot, 9540f5e2ec4SNicholas Bellinger .format_prot = fd_format_prot, 9550f5e2ec4SNicholas Bellinger .free_prot = fd_free_prot, 9565873c4d1SChristoph Hellwig .tb_dev_attrib_attrs = sbc_attrib_attrs, 957c66ac9dbSNicholas Bellinger }; 958c66ac9dbSNicholas Bellinger 959c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void) 960c66ac9dbSNicholas Bellinger { 9610a06d430SChristoph Hellwig return transport_backend_register(&fileio_ops); 962c66ac9dbSNicholas Bellinger } 963c66ac9dbSNicholas Bellinger 96463b91d5aSAsias He static void __exit fileio_module_exit(void) 965c66ac9dbSNicholas Bellinger { 9660a06d430SChristoph Hellwig target_backend_unregister(&fileio_ops); 967c66ac9dbSNicholas Bellinger } 968c66ac9dbSNicholas Bellinger 969c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin"); 970c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 971c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL"); 972c66ac9dbSNicholas Bellinger 973c66ac9dbSNicholas Bellinger module_init(fileio_module_init); 974c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit); 975