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> 2324b83debSChristoph Hellwig #include <linux/scatterlist.h> 24ba929992SBart Van Assche #include <scsi/scsi_proto.h> 2550642762SAsias He #include <asm/unaligned.h> 26c66ac9dbSNicholas Bellinger 27c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 28c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 29c66ac9dbSNicholas Bellinger 30c66ac9dbSNicholas Bellinger #include "target_core_file.h" 31c66ac9dbSNicholas Bellinger 320fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev) 330fd97ccfSChristoph Hellwig { 340fd97ccfSChristoph Hellwig return container_of(dev, struct fd_dev, dev); 350fd97ccfSChristoph Hellwig } 36c66ac9dbSNicholas Bellinger 37c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id) 38c66ac9dbSNicholas Bellinger { 39c66ac9dbSNicholas Bellinger struct fd_host *fd_host; 40c66ac9dbSNicholas Bellinger 41c66ac9dbSNicholas Bellinger fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL); 426708bb27SAndy Grover if (!fd_host) { 436708bb27SAndy Grover pr_err("Unable to allocate memory for struct fd_host\n"); 44e3d6f909SAndy Grover return -ENOMEM; 45c66ac9dbSNicholas Bellinger } 46c66ac9dbSNicholas Bellinger 47c66ac9dbSNicholas Bellinger fd_host->fd_host_id = host_id; 48c66ac9dbSNicholas Bellinger 49e3d6f909SAndy Grover hba->hba_ptr = fd_host; 50c66ac9dbSNicholas Bellinger 516708bb27SAndy Grover pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic" 52c66ac9dbSNicholas Bellinger " Target Core Stack %s\n", hba->hba_id, FD_VERSION, 53ce8dd25dSChristoph Hellwig TARGET_CORE_VERSION); 5495cadaceSNicholas Bellinger pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n", 5595cadaceSNicholas Bellinger hba->hba_id, fd_host->fd_host_id); 56c66ac9dbSNicholas Bellinger 57c66ac9dbSNicholas Bellinger return 0; 58c66ac9dbSNicholas Bellinger } 59c66ac9dbSNicholas Bellinger 60c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba) 61c66ac9dbSNicholas Bellinger { 62c66ac9dbSNicholas Bellinger struct fd_host *fd_host = hba->hba_ptr; 63c66ac9dbSNicholas Bellinger 646708bb27SAndy Grover pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic" 65c66ac9dbSNicholas Bellinger " Target Core\n", hba->hba_id, fd_host->fd_host_id); 66c66ac9dbSNicholas Bellinger 67c66ac9dbSNicholas Bellinger kfree(fd_host); 68c66ac9dbSNicholas Bellinger hba->hba_ptr = NULL; 69c66ac9dbSNicholas Bellinger } 70c66ac9dbSNicholas Bellinger 710fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name) 72c66ac9dbSNicholas Bellinger { 73c66ac9dbSNicholas Bellinger struct fd_dev *fd_dev; 748359cf43SJörn Engel struct fd_host *fd_host = hba->hba_ptr; 75c66ac9dbSNicholas Bellinger 76c66ac9dbSNicholas Bellinger fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL); 776708bb27SAndy Grover if (!fd_dev) { 786708bb27SAndy Grover pr_err("Unable to allocate memory for struct fd_dev\n"); 79c66ac9dbSNicholas Bellinger return NULL; 80c66ac9dbSNicholas Bellinger } 81c66ac9dbSNicholas Bellinger 82c66ac9dbSNicholas Bellinger fd_dev->fd_host = fd_host; 83c66ac9dbSNicholas Bellinger 846708bb27SAndy Grover pr_debug("FILEIO: Allocated fd_dev for %p\n", name); 85c66ac9dbSNicholas Bellinger 860fd97ccfSChristoph Hellwig return &fd_dev->dev; 87c66ac9dbSNicholas Bellinger } 88c66ac9dbSNicholas Bellinger 8933efaaf6SMike Christie static bool fd_configure_unmap(struct se_device *dev) 9033efaaf6SMike Christie { 9133efaaf6SMike Christie struct file *file = FD_DEV(dev)->fd_file; 9233efaaf6SMike Christie struct inode *inode = file->f_mapping->host; 9333efaaf6SMike Christie 9433efaaf6SMike Christie if (S_ISBLK(inode->i_mode)) 9533efaaf6SMike Christie return target_configure_unmap_from_queue(&dev->dev_attrib, 9633efaaf6SMike Christie I_BDEV(inode)); 9733efaaf6SMike Christie 9833efaaf6SMike Christie /* Limit UNMAP emulation to 8k Number of LBAs (NoLB) */ 9933efaaf6SMike Christie dev->dev_attrib.max_unmap_lba_count = 0x2000; 10033efaaf6SMike Christie /* Currently hardcoded to 1 in Linux/SCSI code. */ 10133efaaf6SMike Christie dev->dev_attrib.max_unmap_block_desc_count = 1; 10233efaaf6SMike Christie dev->dev_attrib.unmap_granularity = 1; 10333efaaf6SMike Christie dev->dev_attrib.unmap_granularity_alignment = 0; 10433efaaf6SMike Christie return true; 10533efaaf6SMike Christie } 10633efaaf6SMike Christie 1070fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev) 108c66ac9dbSNicholas Bellinger { 1090fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 1100fd97ccfSChristoph Hellwig struct fd_host *fd_host = dev->se_hba->hba_ptr; 111c66ac9dbSNicholas Bellinger struct file *file; 112c66ac9dbSNicholas Bellinger struct inode *inode = NULL; 1130fd97ccfSChristoph Hellwig int flags, ret = -EINVAL; 114c66ac9dbSNicholas Bellinger 1150fd97ccfSChristoph Hellwig if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) { 1160fd97ccfSChristoph Hellwig pr_err("Missing fd_dev_name=\n"); 1170fd97ccfSChristoph Hellwig return -EINVAL; 1180fd97ccfSChristoph Hellwig } 119c66ac9dbSNicholas Bellinger 120c66ac9dbSNicholas Bellinger /* 121a4dff304SNicholas Bellinger * Use O_DSYNC by default instead of O_SYNC to forgo syncing 122a4dff304SNicholas Bellinger * of pure timestamp updates. 123c66ac9dbSNicholas Bellinger */ 124a4dff304SNicholas Bellinger flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 1250fd97ccfSChristoph Hellwig 126b32f4c7eSNicholas Bellinger /* 127b32f4c7eSNicholas Bellinger * Optionally allow fd_buffered_io=1 to be enabled for people 128b32f4c7eSNicholas Bellinger * who want use the fs buffer cache as an WriteCache mechanism. 129b32f4c7eSNicholas Bellinger * 130b32f4c7eSNicholas Bellinger * This means that in event of a hard failure, there is a risk 131b32f4c7eSNicholas Bellinger * of silent data-loss if the SCSI client has *not* performed a 132b32f4c7eSNicholas Bellinger * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE 133b32f4c7eSNicholas Bellinger * to write-out the entire device cache. 134b32f4c7eSNicholas Bellinger */ 135b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 136b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n"); 137b32f4c7eSNicholas Bellinger flags &= ~O_DSYNC; 138b32f4c7eSNicholas Bellinger } 139c66ac9dbSNicholas Bellinger 140dbc6e022SAl Viro file = filp_open(fd_dev->fd_dev_name, flags, 0600); 141613640e4SNicholas Bellinger if (IS_ERR(file)) { 142dbc6e022SAl Viro pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name); 143613640e4SNicholas Bellinger ret = PTR_ERR(file); 144613640e4SNicholas Bellinger goto fail; 145613640e4SNicholas Bellinger } 146c66ac9dbSNicholas Bellinger fd_dev->fd_file = file; 147c66ac9dbSNicholas Bellinger /* 148c66ac9dbSNicholas Bellinger * If using a block backend with this struct file, we extract 149c66ac9dbSNicholas Bellinger * fd_dev->fd_[block,dev]_size from struct block_device. 150c66ac9dbSNicholas Bellinger * 151c66ac9dbSNicholas Bellinger * Otherwise, we use the passed fd_size= from configfs 152c66ac9dbSNicholas Bellinger */ 153c66ac9dbSNicholas Bellinger inode = file->f_mapping->host; 154c66ac9dbSNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 155817e8b51SChristoph Hellwig struct block_device *bdev = I_BDEV(inode); 156cd9323fdSNicholas Bellinger unsigned long long dev_size; 1570fd97ccfSChristoph Hellwig 158817e8b51SChristoph Hellwig fd_dev->fd_block_size = bdev_logical_block_size(bdev); 159c66ac9dbSNicholas Bellinger /* 160c66ac9dbSNicholas Bellinger * Determine the number of bytes from i_size_read() minus 161c66ac9dbSNicholas Bellinger * one (1) logical sector from underlying struct block_device 162c66ac9dbSNicholas Bellinger */ 163cd9323fdSNicholas Bellinger dev_size = (i_size_read(file->f_mapping->host) - 164c66ac9dbSNicholas Bellinger fd_dev->fd_block_size); 165c66ac9dbSNicholas Bellinger 1666708bb27SAndy Grover pr_debug("FILEIO: Using size: %llu bytes from struct" 167c66ac9dbSNicholas Bellinger " block_device blocks: %llu logical_block_size: %d\n", 168cd9323fdSNicholas Bellinger dev_size, div_u64(dev_size, fd_dev->fd_block_size), 169c66ac9dbSNicholas Bellinger fd_dev->fd_block_size); 17070d3ae5cSAsias He /* 17170d3ae5cSAsias He * Enable write same emulation for IBLOCK and use 0xFFFF as 17270d3ae5cSAsias He * the smaller WRITE_SAME(10) only has a two-byte block count. 17370d3ae5cSAsias He */ 17470d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0xFFFF; 1750463a3feSAsias He 17610f0d2a5SChristoph Hellwig if (bdev_nonrot(bdev)) 1770463a3feSAsias He dev->dev_attrib.is_nonrot = 1; 178c66ac9dbSNicholas Bellinger } else { 179c66ac9dbSNicholas Bellinger if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) { 1806708bb27SAndy Grover pr_err("FILEIO: Missing fd_dev_size=" 181c66ac9dbSNicholas Bellinger " parameter, and no backing struct" 182c66ac9dbSNicholas Bellinger " block_device\n"); 183c66ac9dbSNicholas Bellinger goto fail; 184c66ac9dbSNicholas Bellinger } 185c66ac9dbSNicholas Bellinger 18621363ca8SNicholas Bellinger fd_dev->fd_block_size = FD_BLOCKSIZE; 18770d3ae5cSAsias He 18870d3ae5cSAsias He /* 18970d3ae5cSAsias He * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB) 19070d3ae5cSAsias He * based upon struct iovec limit for vfs_writev() 19170d3ae5cSAsias He */ 19270d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0x1000; 193c66ac9dbSNicholas Bellinger } 194c66ac9dbSNicholas Bellinger 19521363ca8SNicholas Bellinger dev->dev_attrib.hw_block_size = fd_dev->fd_block_size; 19695cadaceSNicholas Bellinger dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES; 19795cadaceSNicholas Bellinger dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size; 1980fd97ccfSChristoph Hellwig dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; 199c66ac9dbSNicholas Bellinger 200b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 201b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Forcing setting of emulate_write_cache=1" 202b32f4c7eSNicholas Bellinger " with FDBD_HAS_BUFFERED_IO_WCE\n"); 2030fd97ccfSChristoph Hellwig dev->dev_attrib.emulate_write_cache = 1; 204b32f4c7eSNicholas Bellinger } 205b32f4c7eSNicholas Bellinger 206c66ac9dbSNicholas Bellinger fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++; 207c66ac9dbSNicholas Bellinger fd_dev->fd_queue_depth = dev->queue_depth; 208c66ac9dbSNicholas Bellinger 2096708bb27SAndy Grover pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s," 210c66ac9dbSNicholas Bellinger " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id, 211c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size); 212c66ac9dbSNicholas Bellinger 2130fd97ccfSChristoph Hellwig return 0; 214c66ac9dbSNicholas Bellinger fail: 215c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 216c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 217c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 218c66ac9dbSNicholas Bellinger } 2190fd97ccfSChristoph Hellwig return ret; 220c66ac9dbSNicholas Bellinger } 221c66ac9dbSNicholas Bellinger 2224cc987eaSNicholas Bellinger static void fd_dev_call_rcu(struct rcu_head *p) 2234cc987eaSNicholas Bellinger { 2244cc987eaSNicholas Bellinger struct se_device *dev = container_of(p, struct se_device, rcu_head); 2254cc987eaSNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 2264cc987eaSNicholas Bellinger 2274cc987eaSNicholas Bellinger kfree(fd_dev); 2284cc987eaSNicholas Bellinger } 2294cc987eaSNicholas Bellinger 2300fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev) 231c66ac9dbSNicholas Bellinger { 23292634706SMike Christie call_rcu(&dev->rcu_head, fd_dev_call_rcu); 23392634706SMike Christie } 23492634706SMike Christie 23592634706SMike Christie static void fd_destroy_device(struct se_device *dev) 23692634706SMike Christie { 2370fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 238c66ac9dbSNicholas Bellinger 239c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 240c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 241c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 242c66ac9dbSNicholas Bellinger } 243c66ac9dbSNicholas Bellinger } 244c66ac9dbSNicholas Bellinger 245769031c9SAndrei Vagin struct target_core_file_cmd { 246769031c9SAndrei Vagin unsigned long len; 247769031c9SAndrei Vagin struct se_cmd *cmd; 248769031c9SAndrei Vagin struct kiocb iocb; 249ecd7fba0SChristoph Hellwig struct bio_vec bvecs[]; 250769031c9SAndrei Vagin }; 251769031c9SAndrei Vagin 2526b19b766SJens Axboe static void cmd_rw_aio_complete(struct kiocb *iocb, long ret) 253769031c9SAndrei Vagin { 254769031c9SAndrei Vagin struct target_core_file_cmd *cmd; 255769031c9SAndrei Vagin 256769031c9SAndrei Vagin cmd = container_of(iocb, struct target_core_file_cmd, iocb); 257769031c9SAndrei Vagin 258769031c9SAndrei Vagin if (ret != cmd->len) 259769031c9SAndrei Vagin target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION); 260769031c9SAndrei Vagin else 261769031c9SAndrei Vagin target_complete_cmd(cmd->cmd, SAM_STAT_GOOD); 262769031c9SAndrei Vagin 263769031c9SAndrei Vagin kfree(cmd); 264769031c9SAndrei Vagin } 265769031c9SAndrei Vagin 266769031c9SAndrei Vagin static sense_reason_t 267769031c9SAndrei Vagin fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 268769031c9SAndrei Vagin enum dma_data_direction data_direction) 269769031c9SAndrei Vagin { 270769031c9SAndrei Vagin int is_write = !(data_direction == DMA_FROM_DEVICE); 271769031c9SAndrei Vagin struct se_device *dev = cmd->se_dev; 272769031c9SAndrei Vagin struct fd_dev *fd_dev = FD_DEV(dev); 273769031c9SAndrei Vagin struct file *file = fd_dev->fd_file; 274769031c9SAndrei Vagin struct target_core_file_cmd *aio_cmd; 2756b1dba3dSPavel Begunkov struct iov_iter iter; 276769031c9SAndrei Vagin struct scatterlist *sg; 277769031c9SAndrei Vagin ssize_t len = 0; 278769031c9SAndrei Vagin int ret = 0, i; 279769031c9SAndrei Vagin 280ecd7fba0SChristoph Hellwig aio_cmd = kmalloc(struct_size(aio_cmd, bvecs, sgl_nents), GFP_KERNEL); 281769031c9SAndrei Vagin if (!aio_cmd) 282769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 283769031c9SAndrei Vagin 284769031c9SAndrei Vagin for_each_sg(sgl, sg, sgl_nents, i) { 285ecd7fba0SChristoph Hellwig aio_cmd->bvecs[i].bv_page = sg_page(sg); 286ecd7fba0SChristoph Hellwig aio_cmd->bvecs[i].bv_len = sg->length; 287ecd7fba0SChristoph Hellwig aio_cmd->bvecs[i].bv_offset = sg->offset; 288769031c9SAndrei Vagin 289769031c9SAndrei Vagin len += sg->length; 290769031c9SAndrei Vagin } 291769031c9SAndrei Vagin 292ecd7fba0SChristoph Hellwig iov_iter_bvec(&iter, is_write, aio_cmd->bvecs, sgl_nents, len); 293769031c9SAndrei Vagin 294769031c9SAndrei Vagin aio_cmd->cmd = cmd; 295769031c9SAndrei Vagin aio_cmd->len = len; 296769031c9SAndrei Vagin aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size; 297769031c9SAndrei Vagin aio_cmd->iocb.ki_filp = file; 298769031c9SAndrei Vagin aio_cmd->iocb.ki_complete = cmd_rw_aio_complete; 299769031c9SAndrei Vagin aio_cmd->iocb.ki_flags = IOCB_DIRECT; 300769031c9SAndrei Vagin 301769031c9SAndrei Vagin if (is_write && (cmd->se_cmd_flags & SCF_FUA)) 302769031c9SAndrei Vagin aio_cmd->iocb.ki_flags |= IOCB_DSYNC; 303769031c9SAndrei Vagin 304769031c9SAndrei Vagin if (is_write) 305769031c9SAndrei Vagin ret = call_write_iter(file, &aio_cmd->iocb, &iter); 306769031c9SAndrei Vagin else 307769031c9SAndrei Vagin ret = call_read_iter(file, &aio_cmd->iocb, &iter); 308769031c9SAndrei Vagin 309769031c9SAndrei Vagin if (ret != -EIOCBQUEUED) 3106b19b766SJens Axboe cmd_rw_aio_complete(&aio_cmd->iocb, ret); 311769031c9SAndrei Vagin 312769031c9SAndrei Vagin return 0; 313769031c9SAndrei Vagin } 314769031c9SAndrei Vagin 3158287fa5fSSagi Grimberg static int fd_do_rw(struct se_cmd *cmd, struct file *fd, 3168287fa5fSSagi Grimberg u32 block_size, struct scatterlist *sgl, 3178287fa5fSSagi Grimberg u32 sgl_nents, u32 data_length, int is_write) 31842201b55SNicholas Bellinger { 3195787cacdSChristoph Hellwig struct scatterlist *sg; 3206b9a44d0SChristoph Hellwig struct iov_iter iter; 3216b9a44d0SChristoph Hellwig struct bio_vec *bvec; 3226b9a44d0SChristoph Hellwig ssize_t len = 0; 3238287fa5fSSagi Grimberg loff_t pos = (cmd->t_task_lba * block_size); 324c66ac9dbSNicholas Bellinger int ret = 0, i; 325c66ac9dbSNicholas Bellinger 3266b9a44d0SChristoph Hellwig bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL); 3276b9a44d0SChristoph Hellwig if (!bvec) { 3286708bb27SAndy Grover pr_err("Unable to allocate fd_do_readv iov[]\n"); 329e3d6f909SAndy Grover return -ENOMEM; 330c66ac9dbSNicholas Bellinger } 331c66ac9dbSNicholas Bellinger 3325787cacdSChristoph Hellwig for_each_sg(sgl, sg, sgl_nents, i) { 3336b9a44d0SChristoph Hellwig bvec[i].bv_page = sg_page(sg); 3346b9a44d0SChristoph Hellwig bvec[i].bv_len = sg->length; 3356b9a44d0SChristoph Hellwig bvec[i].bv_offset = sg->offset; 3366b9a44d0SChristoph Hellwig 3376b9a44d0SChristoph Hellwig len += sg->length; 338c66ac9dbSNicholas Bellinger } 339c66ac9dbSNicholas Bellinger 340b676668dSAl Viro iov_iter_bvec(&iter, is_write, bvec, sgl_nents, len); 341778229afSSebastian Andrzej Siewior if (is_write) 342abbb6589SChristoph Hellwig ret = vfs_iter_write(fd, &iter, &pos, 0); 343778229afSSebastian Andrzej Siewior else 34418e9710eSChristoph Hellwig ret = vfs_iter_read(fd, &iter, &pos, 0); 345778229afSSebastian Andrzej Siewior 346778229afSSebastian Andrzej Siewior if (is_write) { 3478287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 348778229afSSebastian Andrzej Siewior pr_err("%s() write returned %d\n", __func__, ret); 349f11b55d1SDmitry Monakhov if (ret >= 0) 350f11b55d1SDmitry Monakhov ret = -EINVAL; 351778229afSSebastian Andrzej Siewior } 352778229afSSebastian Andrzej Siewior } else { 353c66ac9dbSNicholas Bellinger /* 354c66ac9dbSNicholas Bellinger * Return zeros and GOOD status even if the READ did not return 355c66ac9dbSNicholas Bellinger * the expected virt_size for struct file w/o a backing struct 356c66ac9dbSNicholas Bellinger * block_device. 357c66ac9dbSNicholas Bellinger */ 358496ad9aaSAl Viro if (S_ISBLK(file_inode(fd)->i_mode)) { 3598287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 360778229afSSebastian Andrzej Siewior pr_err("%s() returned %d, expecting %u for " 361778229afSSebastian Andrzej Siewior "S_ISBLK\n", __func__, ret, 3628287fa5fSSagi Grimberg data_length); 363f11b55d1SDmitry Monakhov if (ret >= 0) 364f11b55d1SDmitry Monakhov ret = -EINVAL; 365c66ac9dbSNicholas Bellinger } 366c66ac9dbSNicholas Bellinger } else { 367c66ac9dbSNicholas Bellinger if (ret < 0) { 368778229afSSebastian Andrzej Siewior pr_err("%s() returned %d for non S_ISBLK\n", 369778229afSSebastian Andrzej Siewior __func__, ret); 370f11b55d1SDmitry Monakhov } else if (ret != data_length) { 371f11b55d1SDmitry Monakhov /* 372f11b55d1SDmitry Monakhov * Short read case: 373f11b55d1SDmitry Monakhov * Probably some one truncate file under us. 374f11b55d1SDmitry Monakhov * We must explicitly zero sg-pages to prevent 375f11b55d1SDmitry Monakhov * expose uninizialized pages to userspace. 376f11b55d1SDmitry Monakhov */ 377f11b55d1SDmitry Monakhov if (ret < data_length) 378f11b55d1SDmitry Monakhov ret += iov_iter_zero(data_length - ret, &iter); 379f11b55d1SDmitry Monakhov else 380f11b55d1SDmitry Monakhov ret = -EINVAL; 381f11b55d1SDmitry Monakhov } 382f11b55d1SDmitry Monakhov } 383f11b55d1SDmitry Monakhov } 384f11b55d1SDmitry Monakhov kfree(bvec); 385e3d6f909SAndy Grover return ret; 386c66ac9dbSNicholas Bellinger } 387c66ac9dbSNicholas Bellinger 388de103c93SChristoph Hellwig static sense_reason_t 389de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd) 390c66ac9dbSNicholas Bellinger { 391c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 3920fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 393a1d8b49aSAndy Grover int immed = (cmd->t_task_cdb[1] & 0x2); 394c66ac9dbSNicholas Bellinger loff_t start, end; 395c66ac9dbSNicholas Bellinger int ret; 396c66ac9dbSNicholas Bellinger 397c66ac9dbSNicholas Bellinger /* 398c66ac9dbSNicholas Bellinger * If the Immediate bit is set, queue up the GOOD response 399c66ac9dbSNicholas Bellinger * for this SYNCHRONIZE_CACHE op 400c66ac9dbSNicholas Bellinger */ 401c66ac9dbSNicholas Bellinger if (immed) 4025787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 403c66ac9dbSNicholas Bellinger 404c66ac9dbSNicholas Bellinger /* 405c66ac9dbSNicholas Bellinger * Determine if we will be flushing the entire device. 406c66ac9dbSNicholas Bellinger */ 407a1d8b49aSAndy Grover if (cmd->t_task_lba == 0 && cmd->data_length == 0) { 408c66ac9dbSNicholas Bellinger start = 0; 409c66ac9dbSNicholas Bellinger end = LLONG_MAX; 410c66ac9dbSNicholas Bellinger } else { 4110fd97ccfSChristoph Hellwig start = cmd->t_task_lba * dev->dev_attrib.block_size; 412c66ac9dbSNicholas Bellinger if (cmd->data_length) 41362d3ab49SZach Brown end = start + cmd->data_length - 1; 414c66ac9dbSNicholas Bellinger else 415c66ac9dbSNicholas Bellinger end = LLONG_MAX; 416c66ac9dbSNicholas Bellinger } 417c66ac9dbSNicholas Bellinger 418c66ac9dbSNicholas Bellinger ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); 419c66ac9dbSNicholas Bellinger if (ret != 0) 4206708bb27SAndy Grover pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret); 421c66ac9dbSNicholas Bellinger 4225787cacdSChristoph Hellwig if (immed) 423ad67f0d9SChristoph Hellwig return 0; 4245787cacdSChristoph Hellwig 425de103c93SChristoph Hellwig if (ret) 4265787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); 427de103c93SChristoph Hellwig else 4285787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 429ad67f0d9SChristoph Hellwig 430ad67f0d9SChristoph Hellwig return 0; 431c66ac9dbSNicholas Bellinger } 432c66ac9dbSNicholas Bellinger 4337b745c84SNicholas Bellinger static sense_reason_t 4347b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd) 4357b745c84SNicholas Bellinger { 4367b745c84SNicholas Bellinger struct se_device *se_dev = cmd->se_dev; 4377b745c84SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(se_dev); 4387b745c84SNicholas Bellinger loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size; 439d4c5dcacSChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 440d4c5dcacSChristoph Hellwig struct iov_iter iter; 441d4c5dcacSChristoph Hellwig struct bio_vec *bvec; 442d4c5dcacSChristoph Hellwig unsigned int len = 0, i; 443d4c5dcacSChristoph Hellwig ssize_t ret; 4447b745c84SNicholas Bellinger 445afd73f1bSNicholas Bellinger if (cmd->prot_op) { 446afd73f1bSNicholas Bellinger pr_err("WRITE_SAME: Protection information with FILEIO" 447afd73f1bSNicholas Bellinger " backends not supported\n"); 448afd73f1bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 449afd73f1bSNicholas Bellinger } 4507b745c84SNicholas Bellinger 451ccd3f449SMike Christie if (!cmd->t_data_nents) 452ccd3f449SMike Christie return TCM_INVALID_CDB_FIELD; 453ccd3f449SMike Christie 4547b745c84SNicholas Bellinger if (cmd->t_data_nents > 1 || 455d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) { 4567b745c84SNicholas Bellinger pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" 457d4c5dcacSChristoph Hellwig " block_size: %u\n", 458d4c5dcacSChristoph Hellwig cmd->t_data_nents, 459d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length, 4607b745c84SNicholas Bellinger cmd->se_dev->dev_attrib.block_size); 4617b745c84SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 4627b745c84SNicholas Bellinger } 4637b745c84SNicholas Bellinger 464d4c5dcacSChristoph Hellwig bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL); 465d4c5dcacSChristoph Hellwig if (!bvec) 4667b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4677b745c84SNicholas Bellinger 468d4c5dcacSChristoph Hellwig for (i = 0; i < nolb; i++) { 469d4c5dcacSChristoph Hellwig bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]); 470d4c5dcacSChristoph Hellwig bvec[i].bv_len = cmd->t_data_sg[0].length; 471d4c5dcacSChristoph Hellwig bvec[i].bv_offset = cmd->t_data_sg[0].offset; 472d4c5dcacSChristoph Hellwig 473d4c5dcacSChristoph Hellwig len += se_dev->dev_attrib.block_size; 4747b745c84SNicholas Bellinger } 4757b745c84SNicholas Bellinger 476*de4eda9dSAl Viro iov_iter_bvec(&iter, ITER_SOURCE, bvec, nolb, len); 477abbb6589SChristoph Hellwig ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos, 0); 4787b745c84SNicholas Bellinger 479d4c5dcacSChristoph Hellwig kfree(bvec); 480d4c5dcacSChristoph Hellwig if (ret < 0 || ret != len) { 481d4c5dcacSChristoph Hellwig pr_err("vfs_iter_write() returned %zd for write same\n", ret); 4827b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4837b745c84SNicholas Bellinger } 4847b745c84SNicholas Bellinger 4857b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 4867b745c84SNicholas Bellinger return 0; 4877b745c84SNicholas Bellinger } 4887b745c84SNicholas Bellinger 48964d240b7SAkinobu Mita static int 49064d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb, 49164d240b7SAkinobu Mita void *buf, size_t bufsize) 49264d240b7SAkinobu Mita { 49364d240b7SAkinobu Mita struct fd_dev *fd_dev = FD_DEV(se_dev); 49464d240b7SAkinobu Mita struct file *prot_fd = fd_dev->fd_prot_file; 49564d240b7SAkinobu Mita sector_t prot_length, prot; 49664d240b7SAkinobu Mita loff_t pos = lba * se_dev->prot_length; 49764d240b7SAkinobu Mita 49864d240b7SAkinobu Mita if (!prot_fd) { 49964d240b7SAkinobu Mita pr_err("Unable to locate fd_dev->fd_prot_file\n"); 50064d240b7SAkinobu Mita return -ENODEV; 50164d240b7SAkinobu Mita } 50264d240b7SAkinobu Mita 50364d240b7SAkinobu Mita prot_length = nolb * se_dev->prot_length; 50464d240b7SAkinobu Mita 5054524a0b1SChaitanya Kulkarni memset(buf, 0xff, bufsize); 50664d240b7SAkinobu Mita for (prot = 0; prot < prot_length;) { 50764d240b7SAkinobu Mita sector_t len = min_t(sector_t, bufsize, prot_length - prot); 508e13ec939SChristoph Hellwig ssize_t ret = kernel_write(prot_fd, buf, len, &pos); 50964d240b7SAkinobu Mita 51064d240b7SAkinobu Mita if (ret != len) { 51164d240b7SAkinobu Mita pr_err("vfs_write to prot file failed: %zd\n", ret); 51264d240b7SAkinobu Mita return ret < 0 ? ret : -ENODEV; 51364d240b7SAkinobu Mita } 51464d240b7SAkinobu Mita prot += ret; 51564d240b7SAkinobu Mita } 51664d240b7SAkinobu Mita 51764d240b7SAkinobu Mita return 0; 51864d240b7SAkinobu Mita } 51964d240b7SAkinobu Mita 52064d240b7SAkinobu Mita static int 52164d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 52264d240b7SAkinobu Mita { 52364d240b7SAkinobu Mita void *buf; 52464d240b7SAkinobu Mita int rc; 52564d240b7SAkinobu Mita 52664d240b7SAkinobu Mita buf = (void *)__get_free_page(GFP_KERNEL); 52764d240b7SAkinobu Mita if (!buf) { 52864d240b7SAkinobu Mita pr_err("Unable to allocate FILEIO prot buf\n"); 52964d240b7SAkinobu Mita return -ENOMEM; 53064d240b7SAkinobu Mita } 53164d240b7SAkinobu Mita 53264d240b7SAkinobu Mita rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE); 53364d240b7SAkinobu Mita 53464d240b7SAkinobu Mita free_page((unsigned long)buf); 53564d240b7SAkinobu Mita 53664d240b7SAkinobu Mita return rc; 53764d240b7SAkinobu Mita } 53864d240b7SAkinobu Mita 539de103c93SChristoph Hellwig static sense_reason_t 54062e46942SChristoph Hellwig fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 54170d3ae5cSAsias He { 54262e46942SChristoph Hellwig struct file *file = FD_DEV(cmd->se_dev)->fd_file; 54370d3ae5cSAsias He struct inode *inode = file->f_mapping->host; 54470d3ae5cSAsias He int ret; 54570d3ae5cSAsias He 546594e25e7SJiang Yi if (!nolb) { 547594e25e7SJiang Yi return 0; 548594e25e7SJiang Yi } 549594e25e7SJiang Yi 55064d240b7SAkinobu Mita if (cmd->se_dev->dev_attrib.pi_prot_type) { 55164d240b7SAkinobu Mita ret = fd_do_prot_unmap(cmd, lba, nolb); 55264d240b7SAkinobu Mita if (ret) 55364d240b7SAkinobu Mita return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 55464d240b7SAkinobu Mita } 55564d240b7SAkinobu Mita 55670d3ae5cSAsias He if (S_ISBLK(inode->i_mode)) { 55770d3ae5cSAsias He /* The backend is block device, use discard */ 5584e7b5671SChristoph Hellwig struct block_device *bdev = I_BDEV(inode); 5598a9ebe71SMike Christie struct se_device *dev = cmd->se_dev; 56070d3ae5cSAsias He 5618a9ebe71SMike Christie ret = blkdev_issue_discard(bdev, 5628a9ebe71SMike Christie target_to_linux_sector(dev, lba), 5638a9ebe71SMike Christie target_to_linux_sector(dev, nolb), 56444abff2cSChristoph Hellwig GFP_KERNEL); 56570d3ae5cSAsias He if (ret < 0) { 56670d3ae5cSAsias He pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n", 56770d3ae5cSAsias He ret); 56870d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 56970d3ae5cSAsias He } 57070d3ae5cSAsias He } else { 57170d3ae5cSAsias He /* The backend is normal file, use fallocate */ 57243f55bbbSAsias He struct se_device *se_dev = cmd->se_dev; 57343f55bbbSAsias He loff_t pos = lba * se_dev->dev_attrib.block_size; 57470d3ae5cSAsias He unsigned int len = nolb * se_dev->dev_attrib.block_size; 57570d3ae5cSAsias He int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 57670d3ae5cSAsias He 57770d3ae5cSAsias He if (!file->f_op->fallocate) 57870d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 57970d3ae5cSAsias He 58070d3ae5cSAsias He ret = file->f_op->fallocate(file, mode, pos, len); 58170d3ae5cSAsias He if (ret < 0) { 58270d3ae5cSAsias He pr_warn("FILEIO: fallocate() failed: %d\n", ret); 58370d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 58470d3ae5cSAsias He } 58570d3ae5cSAsias He } 58670d3ae5cSAsias He 58743f55bbbSAsias He return 0; 58843f55bbbSAsias He } 58943f55bbbSAsias He 59043f55bbbSAsias He static sense_reason_t 591769031c9SAndrei Vagin fd_execute_rw_buffered(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 592a82a9538SNicholas Bellinger enum dma_data_direction data_direction) 593c66ac9dbSNicholas Bellinger { 594c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 5958287fa5fSSagi Grimberg struct fd_dev *fd_dev = FD_DEV(dev); 5968287fa5fSSagi Grimberg struct file *file = fd_dev->fd_file; 5978287fa5fSSagi Grimberg struct file *pfile = fd_dev->fd_prot_file; 59842201b55SNicholas Bellinger sense_reason_t rc; 599c66ac9dbSNicholas Bellinger int ret = 0; 600046ba642SNicholas Bellinger /* 601c66ac9dbSNicholas Bellinger * Call vectorized fileio functions to map struct scatterlist 602c66ac9dbSNicholas Bellinger * physical memory addresses to struct iovec virtual memory. 603c66ac9dbSNicholas Bellinger */ 6045787cacdSChristoph Hellwig if (data_direction == DMA_FROM_DEVICE) { 605ee920469SNicholas Bellinger if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 6068287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 6078287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 6088287fa5fSSagi Grimberg cmd->prot_length, 0); 60942201b55SNicholas Bellinger if (ret < 0) 61042201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 61142201b55SNicholas Bellinger } 61242201b55SNicholas Bellinger 6138287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6148287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 0); 61542201b55SNicholas Bellinger 616056e8924SDmitry Monakhov if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type && 617056e8924SDmitry Monakhov dev->dev_attrib.pi_prot_verify) { 6188287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 6198287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 62042201b55SNicholas Bellinger 621f75b6faeSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 622f75b6faeSSagi Grimberg 0, cmd->t_prot_sg, 0); 6238287fa5fSSagi Grimberg if (rc) 62442201b55SNicholas Bellinger return rc; 62542201b55SNicholas Bellinger } 626c66ac9dbSNicholas Bellinger } else { 627056e8924SDmitry Monakhov if (cmd->prot_type && dev->dev_attrib.pi_prot_type && 628056e8924SDmitry Monakhov dev->dev_attrib.pi_prot_verify) { 6298287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 6308287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 63142201b55SNicholas Bellinger 6328287fa5fSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 6338287fa5fSSagi Grimberg 0, cmd->t_prot_sg, 0); 6348287fa5fSSagi Grimberg if (rc) 63542201b55SNicholas Bellinger return rc; 63642201b55SNicholas Bellinger } 63742201b55SNicholas Bellinger 6388287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6398287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 1); 640a4dff304SNicholas Bellinger /* 641125d0119SHannes Reinecke * Perform implicit vfs_fsync_range() for fd_do_writev() ops 642a4dff304SNicholas Bellinger * for SCSI WRITEs with Forced Unit Access (FUA) set. 643a4dff304SNicholas Bellinger * Allow this to happen independent of WCE=0 setting. 644a4dff304SNicholas Bellinger */ 645814e5b45SChristoph Hellwig if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) { 646a4dff304SNicholas Bellinger loff_t start = cmd->t_task_lba * 6470fd97ccfSChristoph Hellwig dev->dev_attrib.block_size; 64862d3ab49SZach Brown loff_t end; 64962d3ab49SZach Brown 65062d3ab49SZach Brown if (cmd->data_length) 65162d3ab49SZach Brown end = start + cmd->data_length - 1; 65262d3ab49SZach Brown else 65362d3ab49SZach Brown end = LLONG_MAX; 654c66ac9dbSNicholas Bellinger 655a4dff304SNicholas Bellinger vfs_fsync_range(fd_dev->fd_file, start, end, 1); 656a4dff304SNicholas Bellinger } 657c66ac9dbSNicholas Bellinger 658ee920469SNicholas Bellinger if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) { 6598287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 6608287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 6618287fa5fSSagi Grimberg cmd->prot_length, 1); 662de103c93SChristoph Hellwig if (ret < 0) 663de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 66442201b55SNicholas Bellinger } 66542201b55SNicholas Bellinger } 66642201b55SNicholas Bellinger 6678287fa5fSSagi Grimberg if (ret < 0) 66842201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 669de103c93SChristoph Hellwig 6705787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 67103e98c9eSNicholas Bellinger return 0; 672c66ac9dbSNicholas Bellinger } 673c66ac9dbSNicholas Bellinger 674769031c9SAndrei Vagin static sense_reason_t 675769031c9SAndrei Vagin fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 676769031c9SAndrei Vagin enum dma_data_direction data_direction) 677769031c9SAndrei Vagin { 678769031c9SAndrei Vagin struct se_device *dev = cmd->se_dev; 679769031c9SAndrei Vagin struct fd_dev *fd_dev = FD_DEV(dev); 680769031c9SAndrei Vagin 681769031c9SAndrei Vagin /* 682769031c9SAndrei Vagin * We are currently limited by the number of iovecs (2048) per 683769031c9SAndrei Vagin * single vfs_[writev,readv] call. 684769031c9SAndrei Vagin */ 685769031c9SAndrei Vagin if (cmd->data_length > FD_MAX_BYTES) { 686769031c9SAndrei Vagin pr_err("FILEIO: Not able to process I/O of %u bytes due to" 687769031c9SAndrei Vagin "FD_MAX_BYTES: %u iovec count limitation\n", 688769031c9SAndrei Vagin cmd->data_length, FD_MAX_BYTES); 689769031c9SAndrei Vagin return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 690769031c9SAndrei Vagin } 691769031c9SAndrei Vagin 692769031c9SAndrei Vagin if (fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO) 693769031c9SAndrei Vagin return fd_execute_rw_aio(cmd, sgl, sgl_nents, data_direction); 694769031c9SAndrei Vagin return fd_execute_rw_buffered(cmd, sgl, sgl_nents, data_direction); 695769031c9SAndrei Vagin } 696769031c9SAndrei Vagin 697c66ac9dbSNicholas Bellinger enum { 698769031c9SAndrei Vagin Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, 699769031c9SAndrei Vagin Opt_fd_async_io, Opt_err 700c66ac9dbSNicholas Bellinger }; 701c66ac9dbSNicholas Bellinger 702c66ac9dbSNicholas Bellinger static match_table_t tokens = { 703c66ac9dbSNicholas Bellinger {Opt_fd_dev_name, "fd_dev_name=%s"}, 704c66ac9dbSNicholas Bellinger {Opt_fd_dev_size, "fd_dev_size=%s"}, 705b32f4c7eSNicholas Bellinger {Opt_fd_buffered_io, "fd_buffered_io=%d"}, 706769031c9SAndrei Vagin {Opt_fd_async_io, "fd_async_io=%d"}, 707c66ac9dbSNicholas Bellinger {Opt_err, NULL} 708c66ac9dbSNicholas Bellinger }; 709c66ac9dbSNicholas Bellinger 7100fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev, 711c66ac9dbSNicholas Bellinger const char *page, ssize_t count) 712c66ac9dbSNicholas Bellinger { 7130fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 714c66ac9dbSNicholas Bellinger char *orig, *ptr, *arg_p, *opts; 715c66ac9dbSNicholas Bellinger substring_t args[MAX_OPT_ARGS]; 716b32f4c7eSNicholas Bellinger int ret = 0, arg, token; 717c66ac9dbSNicholas Bellinger 718c66ac9dbSNicholas Bellinger opts = kstrdup(page, GFP_KERNEL); 719c66ac9dbSNicholas Bellinger if (!opts) 720c66ac9dbSNicholas Bellinger return -ENOMEM; 721c66ac9dbSNicholas Bellinger 722c66ac9dbSNicholas Bellinger orig = opts; 723c66ac9dbSNicholas Bellinger 72490c161b6SSebastian Andrzej Siewior while ((ptr = strsep(&opts, ",\n")) != NULL) { 725c66ac9dbSNicholas Bellinger if (!*ptr) 726c66ac9dbSNicholas Bellinger continue; 727c66ac9dbSNicholas Bellinger 728c66ac9dbSNicholas Bellinger token = match_token(ptr, tokens, args); 729c66ac9dbSNicholas Bellinger switch (token) { 730c66ac9dbSNicholas Bellinger case Opt_fd_dev_name: 731dbc6e022SAl Viro if (match_strlcpy(fd_dev->fd_dev_name, &args[0], 732dbc6e022SAl Viro FD_MAX_DEV_NAME) == 0) { 733dbc6e022SAl Viro ret = -EINVAL; 7346d180253SJesper Juhl break; 7356d180253SJesper Juhl } 7366708bb27SAndy Grover pr_debug("FILEIO: Referencing Path: %s\n", 737c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name); 738c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_PATH; 739c66ac9dbSNicholas Bellinger break; 740c66ac9dbSNicholas Bellinger case Opt_fd_dev_size: 741c66ac9dbSNicholas Bellinger arg_p = match_strdup(&args[0]); 7426d180253SJesper Juhl if (!arg_p) { 7436d180253SJesper Juhl ret = -ENOMEM; 7446d180253SJesper Juhl break; 7456d180253SJesper Juhl } 74657103d7fSJingoo Han ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size); 7476d180253SJesper Juhl kfree(arg_p); 748c66ac9dbSNicholas Bellinger if (ret < 0) { 74957103d7fSJingoo Han pr_err("kstrtoull() failed for" 750c66ac9dbSNicholas Bellinger " fd_dev_size=\n"); 751c66ac9dbSNicholas Bellinger goto out; 752c66ac9dbSNicholas Bellinger } 7536708bb27SAndy Grover pr_debug("FILEIO: Referencing Size: %llu" 754c66ac9dbSNicholas Bellinger " bytes\n", fd_dev->fd_dev_size); 755c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_SIZE; 756c66ac9dbSNicholas Bellinger break; 757b32f4c7eSNicholas Bellinger case Opt_fd_buffered_io: 758ce31c1b0SJoern Engel ret = match_int(args, &arg); 759ce31c1b0SJoern Engel if (ret) 760ce31c1b0SJoern Engel goto out; 761b32f4c7eSNicholas Bellinger if (arg != 1) { 762b32f4c7eSNicholas Bellinger pr_err("bogus fd_buffered_io=%d value\n", arg); 763b32f4c7eSNicholas Bellinger ret = -EINVAL; 764b32f4c7eSNicholas Bellinger goto out; 765b32f4c7eSNicholas Bellinger } 766b32f4c7eSNicholas Bellinger 767b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Using buffered I/O" 768b32f4c7eSNicholas Bellinger " operations for struct fd_dev\n"); 769b32f4c7eSNicholas Bellinger 770b32f4c7eSNicholas Bellinger fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE; 771b32f4c7eSNicholas Bellinger break; 772769031c9SAndrei Vagin case Opt_fd_async_io: 773769031c9SAndrei Vagin ret = match_int(args, &arg); 774769031c9SAndrei Vagin if (ret) 775769031c9SAndrei Vagin goto out; 776769031c9SAndrei Vagin if (arg != 1) { 777769031c9SAndrei Vagin pr_err("bogus fd_async_io=%d value\n", arg); 778769031c9SAndrei Vagin ret = -EINVAL; 779769031c9SAndrei Vagin goto out; 780769031c9SAndrei Vagin } 781769031c9SAndrei Vagin 782769031c9SAndrei Vagin pr_debug("FILEIO: Using async I/O" 783769031c9SAndrei Vagin " operations for struct fd_dev\n"); 784769031c9SAndrei Vagin 785769031c9SAndrei Vagin fd_dev->fbd_flags |= FDBD_HAS_ASYNC_IO; 786769031c9SAndrei Vagin break; 787c66ac9dbSNicholas Bellinger default: 788c66ac9dbSNicholas Bellinger break; 789c66ac9dbSNicholas Bellinger } 790c66ac9dbSNicholas Bellinger } 791c66ac9dbSNicholas Bellinger 792c66ac9dbSNicholas Bellinger out: 793c66ac9dbSNicholas Bellinger kfree(orig); 794c66ac9dbSNicholas Bellinger return (!ret) ? count : ret; 795c66ac9dbSNicholas Bellinger } 796c66ac9dbSNicholas Bellinger 7970fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b) 798c66ac9dbSNicholas Bellinger { 7990fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 800c66ac9dbSNicholas Bellinger ssize_t bl = 0; 801c66ac9dbSNicholas Bellinger 802c66ac9dbSNicholas Bellinger bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id); 803769031c9SAndrei Vagin bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s Async: %d\n", 804b32f4c7eSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size, 805b32f4c7eSNicholas Bellinger (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ? 806769031c9SAndrei Vagin "Buffered-WCE" : "O_DSYNC", 807769031c9SAndrei Vagin !!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO)); 808c66ac9dbSNicholas Bellinger return bl; 809c66ac9dbSNicholas Bellinger } 810c66ac9dbSNicholas Bellinger 811c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev) 812c66ac9dbSNicholas Bellinger { 8130fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 814cd9323fdSNicholas Bellinger struct file *f = fd_dev->fd_file; 815cd9323fdSNicholas Bellinger struct inode *i = f->f_mapping->host; 816cd9323fdSNicholas Bellinger unsigned long long dev_size; 817cd9323fdSNicholas Bellinger /* 818cd9323fdSNicholas Bellinger * When using a file that references an underlying struct block_device, 819cd9323fdSNicholas Bellinger * ensure dev_size is always based on the current inode size in order 820cd9323fdSNicholas Bellinger * to handle underlying block_device resize operations. 821cd9323fdSNicholas Bellinger */ 822cd9323fdSNicholas Bellinger if (S_ISBLK(i->i_mode)) 82321363ca8SNicholas Bellinger dev_size = i_size_read(i); 824cd9323fdSNicholas Bellinger else 825cd9323fdSNicholas Bellinger dev_size = fd_dev->fd_dev_size; 826c66ac9dbSNicholas Bellinger 82721363ca8SNicholas Bellinger return div_u64(dev_size - dev->dev_attrib.block_size, 82821363ca8SNicholas Bellinger dev->dev_attrib.block_size); 829c66ac9dbSNicholas Bellinger } 830c66ac9dbSNicholas Bellinger 8310f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev) 8320f5e2ec4SNicholas Bellinger { 8330f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 8340f5e2ec4SNicholas Bellinger struct file *prot_file, *file = fd_dev->fd_file; 8350f5e2ec4SNicholas Bellinger struct inode *inode; 8360f5e2ec4SNicholas Bellinger int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 8370f5e2ec4SNicholas Bellinger char buf[FD_MAX_DEV_PROT_NAME]; 8380f5e2ec4SNicholas Bellinger 8390f5e2ec4SNicholas Bellinger if (!file) { 8400f5e2ec4SNicholas Bellinger pr_err("Unable to locate fd_dev->fd_file\n"); 8410f5e2ec4SNicholas Bellinger return -ENODEV; 8420f5e2ec4SNicholas Bellinger } 8430f5e2ec4SNicholas Bellinger 8440f5e2ec4SNicholas Bellinger inode = file->f_mapping->host; 8450f5e2ec4SNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 8460f5e2ec4SNicholas Bellinger pr_err("FILEIO Protection emulation only supported on" 8470f5e2ec4SNicholas Bellinger " !S_ISBLK\n"); 8480f5e2ec4SNicholas Bellinger return -ENOSYS; 8490f5e2ec4SNicholas Bellinger } 8500f5e2ec4SNicholas Bellinger 8510f5e2ec4SNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) 8520f5e2ec4SNicholas Bellinger flags &= ~O_DSYNC; 8530f5e2ec4SNicholas Bellinger 8540f5e2ec4SNicholas Bellinger snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection", 8550f5e2ec4SNicholas Bellinger fd_dev->fd_dev_name); 8560f5e2ec4SNicholas Bellinger 8570f5e2ec4SNicholas Bellinger prot_file = filp_open(buf, flags, 0600); 8580f5e2ec4SNicholas Bellinger if (IS_ERR(prot_file)) { 8590f5e2ec4SNicholas Bellinger pr_err("filp_open(%s) failed\n", buf); 8600f5e2ec4SNicholas Bellinger ret = PTR_ERR(prot_file); 8610f5e2ec4SNicholas Bellinger return ret; 8620f5e2ec4SNicholas Bellinger } 8630f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = prot_file; 8640f5e2ec4SNicholas Bellinger 8650f5e2ec4SNicholas Bellinger return 0; 8660f5e2ec4SNicholas Bellinger } 8670f5e2ec4SNicholas Bellinger 8680f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev) 8690f5e2ec4SNicholas Bellinger { 8700f5e2ec4SNicholas Bellinger unsigned char *buf; 8710f5e2ec4SNicholas Bellinger int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size; 87264d240b7SAkinobu Mita int ret; 8730f5e2ec4SNicholas Bellinger 8740f5e2ec4SNicholas Bellinger if (!dev->dev_attrib.pi_prot_type) { 8750f5e2ec4SNicholas Bellinger pr_err("Unable to format_prot while pi_prot_type == 0\n"); 8760f5e2ec4SNicholas Bellinger return -ENODEV; 8770f5e2ec4SNicholas Bellinger } 8780f5e2ec4SNicholas Bellinger 8790f5e2ec4SNicholas Bellinger buf = vzalloc(unit_size); 8800f5e2ec4SNicholas Bellinger if (!buf) { 8810f5e2ec4SNicholas Bellinger pr_err("Unable to allocate FILEIO prot buf\n"); 8820f5e2ec4SNicholas Bellinger return -ENOMEM; 8830f5e2ec4SNicholas Bellinger } 8840f5e2ec4SNicholas Bellinger 8850f5e2ec4SNicholas Bellinger pr_debug("Using FILEIO prot_length: %llu\n", 88664d240b7SAkinobu Mita (unsigned long long)(dev->transport->get_blocks(dev) + 1) * 88764d240b7SAkinobu Mita dev->prot_length); 8880f5e2ec4SNicholas Bellinger 88964d240b7SAkinobu Mita ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1, 89064d240b7SAkinobu Mita buf, unit_size); 8910f5e2ec4SNicholas Bellinger vfree(buf); 8920f5e2ec4SNicholas Bellinger return ret; 8930f5e2ec4SNicholas Bellinger } 8940f5e2ec4SNicholas Bellinger 8950f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev) 8960f5e2ec4SNicholas Bellinger { 8970f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 8980f5e2ec4SNicholas Bellinger 8990f5e2ec4SNicholas Bellinger if (!fd_dev->fd_prot_file) 9000f5e2ec4SNicholas Bellinger return; 9010f5e2ec4SNicholas Bellinger 9020f5e2ec4SNicholas Bellinger filp_close(fd_dev->fd_prot_file, NULL); 9030f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = NULL; 9040f5e2ec4SNicholas Bellinger } 9050f5e2ec4SNicholas Bellinger 9069e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = { 9070c2ad7d1SChristoph Hellwig .execute_rw = fd_execute_rw, 908ad67f0d9SChristoph Hellwig .execute_sync_cache = fd_execute_sync_cache, 9097b745c84SNicholas Bellinger .execute_write_same = fd_execute_write_same, 91050642762SAsias He .execute_unmap = fd_execute_unmap, 9110c2ad7d1SChristoph Hellwig }; 9120c2ad7d1SChristoph Hellwig 913de103c93SChristoph Hellwig static sense_reason_t 914de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd) 9150c2ad7d1SChristoph Hellwig { 9169e999a6cSChristoph Hellwig return sbc_parse_cdb(cmd, &fd_sbc_ops); 9170c2ad7d1SChristoph Hellwig } 9180c2ad7d1SChristoph Hellwig 9190a06d430SChristoph Hellwig static const struct target_backend_ops fileio_ops = { 920c66ac9dbSNicholas Bellinger .name = "fileio", 9210fd97ccfSChristoph Hellwig .inquiry_prod = "FILEIO", 9220fd97ccfSChristoph Hellwig .inquiry_rev = FD_VERSION, 923c66ac9dbSNicholas Bellinger .owner = THIS_MODULE, 924c66ac9dbSNicholas Bellinger .attach_hba = fd_attach_hba, 925c66ac9dbSNicholas Bellinger .detach_hba = fd_detach_hba, 9260fd97ccfSChristoph Hellwig .alloc_device = fd_alloc_device, 9270fd97ccfSChristoph Hellwig .configure_device = fd_configure_device, 92892634706SMike Christie .destroy_device = fd_destroy_device, 929c66ac9dbSNicholas Bellinger .free_device = fd_free_device, 93033efaaf6SMike Christie .configure_unmap = fd_configure_unmap, 9310c2ad7d1SChristoph Hellwig .parse_cdb = fd_parse_cdb, 932c66ac9dbSNicholas Bellinger .set_configfs_dev_params = fd_set_configfs_dev_params, 933c66ac9dbSNicholas Bellinger .show_configfs_dev_params = fd_show_configfs_dev_params, 9346f23ac8aSChristoph Hellwig .get_device_type = sbc_get_device_type, 935c66ac9dbSNicholas Bellinger .get_blocks = fd_get_blocks, 9360f5e2ec4SNicholas Bellinger .init_prot = fd_init_prot, 9370f5e2ec4SNicholas Bellinger .format_prot = fd_format_prot, 9380f5e2ec4SNicholas Bellinger .free_prot = fd_free_prot, 9395873c4d1SChristoph Hellwig .tb_dev_attrib_attrs = sbc_attrib_attrs, 940c66ac9dbSNicholas Bellinger }; 941c66ac9dbSNicholas Bellinger 942c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void) 943c66ac9dbSNicholas Bellinger { 9440a06d430SChristoph Hellwig return transport_backend_register(&fileio_ops); 945c66ac9dbSNicholas Bellinger } 946c66ac9dbSNicholas Bellinger 94763b91d5aSAsias He static void __exit fileio_module_exit(void) 948c66ac9dbSNicholas Bellinger { 9490a06d430SChristoph Hellwig target_backend_unregister(&fileio_ops); 950c66ac9dbSNicholas Bellinger } 951c66ac9dbSNicholas Bellinger 952c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin"); 953c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 954c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL"); 955c66ac9dbSNicholas Bellinger 956c66ac9dbSNicholas Bellinger module_init(fileio_module_init); 957c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit); 958