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> 3370d3ae5cSAsias He #include <linux/falloc.h> 34c66ac9dbSNicholas Bellinger #include <scsi/scsi.h> 35c66ac9dbSNicholas Bellinger #include <scsi/scsi_host.h> 3650642762SAsias He #include <asm/unaligned.h> 37c66ac9dbSNicholas Bellinger 38c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 39c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 40b2320497SNicholas Bellinger #include <target/target_core_backend_configfs.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, 65c66ac9dbSNicholas Bellinger TARGET_CORE_MOD_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); 16470d3ae5cSAsias He /* 16570d3ae5cSAsias He * Check if the underlying struct block_device request_queue supports 16670d3ae5cSAsias He * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM 16770d3ae5cSAsias He * in ATA and we need to set TPE=1 16870d3ae5cSAsias He */ 16970d3ae5cSAsias He if (blk_queue_discard(q)) { 17070d3ae5cSAsias He dev->dev_attrib.max_unmap_lba_count = 17170d3ae5cSAsias He q->limits.max_discard_sectors; 17270d3ae5cSAsias He /* 17370d3ae5cSAsias He * Currently hardcoded to 1 in Linux/SCSI code.. 17470d3ae5cSAsias He */ 17570d3ae5cSAsias He dev->dev_attrib.max_unmap_block_desc_count = 1; 17670d3ae5cSAsias He dev->dev_attrib.unmap_granularity = 17770d3ae5cSAsias He q->limits.discard_granularity >> 9; 17870d3ae5cSAsias He dev->dev_attrib.unmap_granularity_alignment = 17970d3ae5cSAsias He q->limits.discard_alignment; 18070d3ae5cSAsias He pr_debug("IFILE: BLOCK Discard support available," 18170d3ae5cSAsias He " disabled by default\n"); 18270d3ae5cSAsias He } 18370d3ae5cSAsias He /* 18470d3ae5cSAsias He * Enable write same emulation for IBLOCK and use 0xFFFF as 18570d3ae5cSAsias He * the smaller WRITE_SAME(10) only has a two-byte block count. 18670d3ae5cSAsias He */ 18770d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0xFFFF; 1880463a3feSAsias He 1890463a3feSAsias He if (blk_queue_nonrot(q)) 1900463a3feSAsias He dev->dev_attrib.is_nonrot = 1; 191c66ac9dbSNicholas Bellinger } else { 192c66ac9dbSNicholas Bellinger if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) { 1936708bb27SAndy Grover pr_err("FILEIO: Missing fd_dev_size=" 194c66ac9dbSNicholas Bellinger " parameter, and no backing struct" 195c66ac9dbSNicholas Bellinger " block_device\n"); 196c66ac9dbSNicholas Bellinger goto fail; 197c66ac9dbSNicholas Bellinger } 198c66ac9dbSNicholas Bellinger 19921363ca8SNicholas Bellinger fd_dev->fd_block_size = FD_BLOCKSIZE; 20070d3ae5cSAsias He /* 20170d3ae5cSAsias He * Limit UNMAP emulation to 8k Number of LBAs (NoLB) 20270d3ae5cSAsias He */ 20370d3ae5cSAsias He dev->dev_attrib.max_unmap_lba_count = 0x2000; 20470d3ae5cSAsias He /* 20570d3ae5cSAsias He * Currently hardcoded to 1 in Linux/SCSI code.. 20670d3ae5cSAsias He */ 20770d3ae5cSAsias He dev->dev_attrib.max_unmap_block_desc_count = 1; 20870d3ae5cSAsias He dev->dev_attrib.unmap_granularity = 1; 20970d3ae5cSAsias He dev->dev_attrib.unmap_granularity_alignment = 0; 21070d3ae5cSAsias He 21170d3ae5cSAsias He /* 21270d3ae5cSAsias He * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB) 21370d3ae5cSAsias He * based upon struct iovec limit for vfs_writev() 21470d3ae5cSAsias He */ 21570d3ae5cSAsias He dev->dev_attrib.max_write_same_len = 0x1000; 216c66ac9dbSNicholas Bellinger } 217c66ac9dbSNicholas Bellinger 21821363ca8SNicholas Bellinger dev->dev_attrib.hw_block_size = fd_dev->fd_block_size; 21995cadaceSNicholas Bellinger dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES; 22095cadaceSNicholas Bellinger dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size; 2210fd97ccfSChristoph Hellwig dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; 222c66ac9dbSNicholas Bellinger 223b32f4c7eSNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) { 224b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Forcing setting of emulate_write_cache=1" 225b32f4c7eSNicholas Bellinger " with FDBD_HAS_BUFFERED_IO_WCE\n"); 2260fd97ccfSChristoph Hellwig dev->dev_attrib.emulate_write_cache = 1; 227b32f4c7eSNicholas Bellinger } 228b32f4c7eSNicholas Bellinger 229c66ac9dbSNicholas Bellinger fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++; 230c66ac9dbSNicholas Bellinger fd_dev->fd_queue_depth = dev->queue_depth; 231c66ac9dbSNicholas Bellinger 2326708bb27SAndy Grover pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s," 233c66ac9dbSNicholas Bellinger " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id, 234c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size); 235c66ac9dbSNicholas Bellinger 2360fd97ccfSChristoph Hellwig return 0; 237c66ac9dbSNicholas Bellinger fail: 238c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 239c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 240c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 241c66ac9dbSNicholas Bellinger } 2420fd97ccfSChristoph Hellwig return ret; 243c66ac9dbSNicholas Bellinger } 244c66ac9dbSNicholas Bellinger 2450fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev) 246c66ac9dbSNicholas Bellinger { 2470fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 248c66ac9dbSNicholas Bellinger 249c66ac9dbSNicholas Bellinger if (fd_dev->fd_file) { 250c66ac9dbSNicholas Bellinger filp_close(fd_dev->fd_file, NULL); 251c66ac9dbSNicholas Bellinger fd_dev->fd_file = NULL; 252c66ac9dbSNicholas Bellinger } 253c66ac9dbSNicholas Bellinger 254c66ac9dbSNicholas Bellinger kfree(fd_dev); 255c66ac9dbSNicholas Bellinger } 256c66ac9dbSNicholas Bellinger 2578287fa5fSSagi Grimberg static int fd_do_rw(struct se_cmd *cmd, struct file *fd, 2588287fa5fSSagi Grimberg u32 block_size, struct scatterlist *sgl, 2598287fa5fSSagi Grimberg u32 sgl_nents, u32 data_length, int is_write) 26042201b55SNicholas Bellinger { 2615787cacdSChristoph Hellwig struct scatterlist *sg; 2626b9a44d0SChristoph Hellwig struct iov_iter iter; 2636b9a44d0SChristoph Hellwig struct bio_vec *bvec; 2646b9a44d0SChristoph Hellwig ssize_t len = 0; 2658287fa5fSSagi Grimberg loff_t pos = (cmd->t_task_lba * block_size); 266c66ac9dbSNicholas Bellinger int ret = 0, i; 267c66ac9dbSNicholas Bellinger 2686b9a44d0SChristoph Hellwig bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL); 2696b9a44d0SChristoph Hellwig if (!bvec) { 2706708bb27SAndy Grover pr_err("Unable to allocate fd_do_readv iov[]\n"); 271e3d6f909SAndy Grover return -ENOMEM; 272c66ac9dbSNicholas Bellinger } 273c66ac9dbSNicholas Bellinger 2745787cacdSChristoph Hellwig for_each_sg(sgl, sg, sgl_nents, i) { 2756b9a44d0SChristoph Hellwig bvec[i].bv_page = sg_page(sg); 2766b9a44d0SChristoph Hellwig bvec[i].bv_len = sg->length; 2776b9a44d0SChristoph Hellwig bvec[i].bv_offset = sg->offset; 2786b9a44d0SChristoph Hellwig 2796b9a44d0SChristoph Hellwig len += sg->length; 280c66ac9dbSNicholas Bellinger } 281c66ac9dbSNicholas Bellinger 2826b9a44d0SChristoph Hellwig iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len); 283778229afSSebastian Andrzej Siewior if (is_write) 2846b9a44d0SChristoph Hellwig ret = vfs_iter_write(fd, &iter, &pos); 285778229afSSebastian Andrzej Siewior else 2866b9a44d0SChristoph Hellwig ret = vfs_iter_read(fd, &iter, &pos); 287778229afSSebastian Andrzej Siewior 2886b9a44d0SChristoph Hellwig kfree(bvec); 289778229afSSebastian Andrzej Siewior 290778229afSSebastian Andrzej Siewior if (is_write) { 2918287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 292778229afSSebastian Andrzej Siewior pr_err("%s() write returned %d\n", __func__, ret); 293778229afSSebastian Andrzej Siewior return (ret < 0 ? ret : -EINVAL); 294778229afSSebastian Andrzej Siewior } 295778229afSSebastian Andrzej Siewior } else { 296c66ac9dbSNicholas Bellinger /* 297c66ac9dbSNicholas Bellinger * Return zeros and GOOD status even if the READ did not return 298c66ac9dbSNicholas Bellinger * the expected virt_size for struct file w/o a backing struct 299c66ac9dbSNicholas Bellinger * block_device. 300c66ac9dbSNicholas Bellinger */ 301496ad9aaSAl Viro if (S_ISBLK(file_inode(fd)->i_mode)) { 3028287fa5fSSagi Grimberg if (ret < 0 || ret != data_length) { 303778229afSSebastian Andrzej Siewior pr_err("%s() returned %d, expecting %u for " 304778229afSSebastian Andrzej Siewior "S_ISBLK\n", __func__, ret, 3058287fa5fSSagi Grimberg data_length); 306e3d6f909SAndy Grover return (ret < 0 ? ret : -EINVAL); 307c66ac9dbSNicholas Bellinger } 308c66ac9dbSNicholas Bellinger } else { 309c66ac9dbSNicholas Bellinger if (ret < 0) { 310778229afSSebastian Andrzej Siewior pr_err("%s() returned %d for non S_ISBLK\n", 311778229afSSebastian Andrzej Siewior __func__, ret); 312e3d6f909SAndy Grover return ret; 313c66ac9dbSNicholas Bellinger } 314c66ac9dbSNicholas Bellinger } 315c66ac9dbSNicholas Bellinger } 316c66ac9dbSNicholas Bellinger return 1; 317c66ac9dbSNicholas Bellinger } 318c66ac9dbSNicholas Bellinger 319de103c93SChristoph Hellwig static sense_reason_t 320de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd) 321c66ac9dbSNicholas Bellinger { 322c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 3230fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 324a1d8b49aSAndy Grover int immed = (cmd->t_task_cdb[1] & 0x2); 325c66ac9dbSNicholas Bellinger loff_t start, end; 326c66ac9dbSNicholas Bellinger int ret; 327c66ac9dbSNicholas Bellinger 328c66ac9dbSNicholas Bellinger /* 329c66ac9dbSNicholas Bellinger * If the Immediate bit is set, queue up the GOOD response 330c66ac9dbSNicholas Bellinger * for this SYNCHRONIZE_CACHE op 331c66ac9dbSNicholas Bellinger */ 332c66ac9dbSNicholas Bellinger if (immed) 3335787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 334c66ac9dbSNicholas Bellinger 335c66ac9dbSNicholas Bellinger /* 336c66ac9dbSNicholas Bellinger * Determine if we will be flushing the entire device. 337c66ac9dbSNicholas Bellinger */ 338a1d8b49aSAndy Grover if (cmd->t_task_lba == 0 && cmd->data_length == 0) { 339c66ac9dbSNicholas Bellinger start = 0; 340c66ac9dbSNicholas Bellinger end = LLONG_MAX; 341c66ac9dbSNicholas Bellinger } else { 3420fd97ccfSChristoph Hellwig start = cmd->t_task_lba * dev->dev_attrib.block_size; 343c66ac9dbSNicholas Bellinger if (cmd->data_length) 34462d3ab49SZach Brown end = start + cmd->data_length - 1; 345c66ac9dbSNicholas Bellinger else 346c66ac9dbSNicholas Bellinger end = LLONG_MAX; 347c66ac9dbSNicholas Bellinger } 348c66ac9dbSNicholas Bellinger 349c66ac9dbSNicholas Bellinger ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); 350c66ac9dbSNicholas Bellinger if (ret != 0) 3516708bb27SAndy Grover pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret); 352c66ac9dbSNicholas Bellinger 3535787cacdSChristoph Hellwig if (immed) 354ad67f0d9SChristoph Hellwig return 0; 3555787cacdSChristoph Hellwig 356de103c93SChristoph Hellwig if (ret) 3575787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); 358de103c93SChristoph Hellwig else 3595787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 360ad67f0d9SChristoph Hellwig 361ad67f0d9SChristoph Hellwig return 0; 362c66ac9dbSNicholas Bellinger } 363c66ac9dbSNicholas Bellinger 3647b745c84SNicholas Bellinger static sense_reason_t 3657b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd) 3667b745c84SNicholas Bellinger { 3677b745c84SNicholas Bellinger struct se_device *se_dev = cmd->se_dev; 3687b745c84SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(se_dev); 3697b745c84SNicholas Bellinger loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size; 370d4c5dcacSChristoph Hellwig sector_t nolb = sbc_get_write_same_sectors(cmd); 371d4c5dcacSChristoph Hellwig struct iov_iter iter; 372d4c5dcacSChristoph Hellwig struct bio_vec *bvec; 373d4c5dcacSChristoph Hellwig unsigned int len = 0, i; 374d4c5dcacSChristoph Hellwig ssize_t ret; 3757b745c84SNicholas Bellinger 3767b745c84SNicholas Bellinger if (!nolb) { 3777b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 3787b745c84SNicholas Bellinger return 0; 3797b745c84SNicholas Bellinger } 380afd73f1bSNicholas Bellinger if (cmd->prot_op) { 381afd73f1bSNicholas Bellinger pr_err("WRITE_SAME: Protection information with FILEIO" 382afd73f1bSNicholas Bellinger " backends not supported\n"); 383afd73f1bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 384afd73f1bSNicholas Bellinger } 3857b745c84SNicholas Bellinger 3867b745c84SNicholas Bellinger if (cmd->t_data_nents > 1 || 387d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) { 3887b745c84SNicholas Bellinger pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" 389d4c5dcacSChristoph Hellwig " block_size: %u\n", 390d4c5dcacSChristoph Hellwig cmd->t_data_nents, 391d4c5dcacSChristoph Hellwig cmd->t_data_sg[0].length, 3927b745c84SNicholas Bellinger cmd->se_dev->dev_attrib.block_size); 3937b745c84SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 3947b745c84SNicholas Bellinger } 3957b745c84SNicholas Bellinger 396d4c5dcacSChristoph Hellwig bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL); 397d4c5dcacSChristoph Hellwig if (!bvec) 3987b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 3997b745c84SNicholas Bellinger 400d4c5dcacSChristoph Hellwig for (i = 0; i < nolb; i++) { 401d4c5dcacSChristoph Hellwig bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]); 402d4c5dcacSChristoph Hellwig bvec[i].bv_len = cmd->t_data_sg[0].length; 403d4c5dcacSChristoph Hellwig bvec[i].bv_offset = cmd->t_data_sg[0].offset; 404d4c5dcacSChristoph Hellwig 405d4c5dcacSChristoph Hellwig len += se_dev->dev_attrib.block_size; 4067b745c84SNicholas Bellinger } 4077b745c84SNicholas Bellinger 408d4c5dcacSChristoph Hellwig iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len); 409d4c5dcacSChristoph Hellwig ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos); 4107b745c84SNicholas Bellinger 411d4c5dcacSChristoph Hellwig kfree(bvec); 412d4c5dcacSChristoph Hellwig if (ret < 0 || ret != len) { 413d4c5dcacSChristoph Hellwig pr_err("vfs_iter_write() returned %zd for write same\n", ret); 4147b745c84SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4157b745c84SNicholas Bellinger } 4167b745c84SNicholas Bellinger 4177b745c84SNicholas Bellinger target_complete_cmd(cmd, SAM_STAT_GOOD); 4187b745c84SNicholas Bellinger return 0; 4197b745c84SNicholas Bellinger } 4207b745c84SNicholas Bellinger 42164d240b7SAkinobu Mita static int 42264d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb, 42364d240b7SAkinobu Mita void *buf, size_t bufsize) 42464d240b7SAkinobu Mita { 42564d240b7SAkinobu Mita struct fd_dev *fd_dev = FD_DEV(se_dev); 42664d240b7SAkinobu Mita struct file *prot_fd = fd_dev->fd_prot_file; 42764d240b7SAkinobu Mita sector_t prot_length, prot; 42864d240b7SAkinobu Mita loff_t pos = lba * se_dev->prot_length; 42964d240b7SAkinobu Mita 43064d240b7SAkinobu Mita if (!prot_fd) { 43164d240b7SAkinobu Mita pr_err("Unable to locate fd_dev->fd_prot_file\n"); 43264d240b7SAkinobu Mita return -ENODEV; 43364d240b7SAkinobu Mita } 43464d240b7SAkinobu Mita 43564d240b7SAkinobu Mita prot_length = nolb * se_dev->prot_length; 43664d240b7SAkinobu Mita 43764d240b7SAkinobu Mita for (prot = 0; prot < prot_length;) { 43864d240b7SAkinobu Mita sector_t len = min_t(sector_t, bufsize, prot_length - prot); 43964d240b7SAkinobu Mita ssize_t ret = kernel_write(prot_fd, buf, len, pos + prot); 44064d240b7SAkinobu Mita 44164d240b7SAkinobu Mita if (ret != len) { 44264d240b7SAkinobu Mita pr_err("vfs_write to prot file failed: %zd\n", ret); 44364d240b7SAkinobu Mita return ret < 0 ? ret : -ENODEV; 44464d240b7SAkinobu Mita } 44564d240b7SAkinobu Mita prot += ret; 44664d240b7SAkinobu Mita } 44764d240b7SAkinobu Mita 44864d240b7SAkinobu Mita return 0; 44964d240b7SAkinobu Mita } 45064d240b7SAkinobu Mita 45164d240b7SAkinobu Mita static int 45264d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 45364d240b7SAkinobu Mita { 45464d240b7SAkinobu Mita void *buf; 45564d240b7SAkinobu Mita int rc; 45664d240b7SAkinobu Mita 45764d240b7SAkinobu Mita buf = (void *)__get_free_page(GFP_KERNEL); 45864d240b7SAkinobu Mita if (!buf) { 45964d240b7SAkinobu Mita pr_err("Unable to allocate FILEIO prot buf\n"); 46064d240b7SAkinobu Mita return -ENOMEM; 46164d240b7SAkinobu Mita } 46264d240b7SAkinobu Mita memset(buf, 0xff, PAGE_SIZE); 46364d240b7SAkinobu Mita 46464d240b7SAkinobu Mita rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE); 46564d240b7SAkinobu Mita 46664d240b7SAkinobu Mita free_page((unsigned long)buf); 46764d240b7SAkinobu Mita 46864d240b7SAkinobu Mita return rc; 46964d240b7SAkinobu Mita } 47064d240b7SAkinobu Mita 471de103c93SChristoph Hellwig static sense_reason_t 47286d71829SAsias He fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb) 47370d3ae5cSAsias He { 47486d71829SAsias He struct file *file = priv; 47570d3ae5cSAsias He struct inode *inode = file->f_mapping->host; 47670d3ae5cSAsias He int ret; 47770d3ae5cSAsias He 47864d240b7SAkinobu Mita if (cmd->se_dev->dev_attrib.pi_prot_type) { 47964d240b7SAkinobu Mita ret = fd_do_prot_unmap(cmd, lba, nolb); 48064d240b7SAkinobu Mita if (ret) 48164d240b7SAkinobu Mita return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 48264d240b7SAkinobu Mita } 48364d240b7SAkinobu Mita 48470d3ae5cSAsias He if (S_ISBLK(inode->i_mode)) { 48570d3ae5cSAsias He /* The backend is block device, use discard */ 48670d3ae5cSAsias He struct block_device *bdev = inode->i_bdev; 48770d3ae5cSAsias He 48843f55bbbSAsias He ret = blkdev_issue_discard(bdev, lba, 48970d3ae5cSAsias He nolb, GFP_KERNEL, 0); 49070d3ae5cSAsias He if (ret < 0) { 49170d3ae5cSAsias He pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n", 49270d3ae5cSAsias He ret); 49370d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 49470d3ae5cSAsias He } 49570d3ae5cSAsias He } else { 49670d3ae5cSAsias He /* The backend is normal file, use fallocate */ 49743f55bbbSAsias He struct se_device *se_dev = cmd->se_dev; 49843f55bbbSAsias He loff_t pos = lba * se_dev->dev_attrib.block_size; 49970d3ae5cSAsias He unsigned int len = nolb * se_dev->dev_attrib.block_size; 50070d3ae5cSAsias He int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 50170d3ae5cSAsias He 50270d3ae5cSAsias He if (!file->f_op->fallocate) 50370d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 50470d3ae5cSAsias He 50570d3ae5cSAsias He ret = file->f_op->fallocate(file, mode, pos, len); 50670d3ae5cSAsias He if (ret < 0) { 50770d3ae5cSAsias He pr_warn("FILEIO: fallocate() failed: %d\n", ret); 50870d3ae5cSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 50970d3ae5cSAsias He } 51070d3ae5cSAsias He } 51170d3ae5cSAsias He 51243f55bbbSAsias He return 0; 51343f55bbbSAsias He } 51443f55bbbSAsias He 51543f55bbbSAsias He static sense_reason_t 51643f55bbbSAsias He fd_execute_write_same_unmap(struct se_cmd *cmd) 51743f55bbbSAsias He { 51843f55bbbSAsias He struct se_device *se_dev = cmd->se_dev; 51943f55bbbSAsias He struct fd_dev *fd_dev = FD_DEV(se_dev); 52043f55bbbSAsias He struct file *file = fd_dev->fd_file; 52143f55bbbSAsias He sector_t lba = cmd->t_task_lba; 52243f55bbbSAsias He sector_t nolb = sbc_get_write_same_sectors(cmd); 5233abff1e5SChristoph Hellwig sense_reason_t ret; 52443f55bbbSAsias He 52543f55bbbSAsias He if (!nolb) { 52643f55bbbSAsias He target_complete_cmd(cmd, SAM_STAT_GOOD); 52743f55bbbSAsias He return 0; 52843f55bbbSAsias He } 52943f55bbbSAsias He 53043f55bbbSAsias He ret = fd_do_unmap(cmd, file, lba, nolb); 53143f55bbbSAsias He if (ret) 53243f55bbbSAsias He return ret; 53343f55bbbSAsias He 53470d3ae5cSAsias He target_complete_cmd(cmd, GOOD); 53570d3ae5cSAsias He return 0; 53670d3ae5cSAsias He } 53770d3ae5cSAsias He 53870d3ae5cSAsias He static sense_reason_t 53950642762SAsias He fd_execute_unmap(struct se_cmd *cmd) 54050642762SAsias He { 54186d71829SAsias He struct file *file = FD_DEV(cmd->se_dev)->fd_file; 54250642762SAsias He 54386d71829SAsias He return sbc_execute_unmap(cmd, fd_do_unmap, file); 54450642762SAsias He } 54550642762SAsias He 54650642762SAsias He static sense_reason_t 547a82a9538SNicholas Bellinger fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 548a82a9538SNicholas Bellinger enum dma_data_direction data_direction) 549c66ac9dbSNicholas Bellinger { 550c66ac9dbSNicholas Bellinger struct se_device *dev = cmd->se_dev; 5518287fa5fSSagi Grimberg struct fd_dev *fd_dev = FD_DEV(dev); 5528287fa5fSSagi Grimberg struct file *file = fd_dev->fd_file; 5538287fa5fSSagi Grimberg struct file *pfile = fd_dev->fd_prot_file; 55442201b55SNicholas Bellinger sense_reason_t rc; 555c66ac9dbSNicholas Bellinger int ret = 0; 556046ba642SNicholas Bellinger /* 557046ba642SNicholas Bellinger * We are currently limited by the number of iovecs (2048) per 558046ba642SNicholas Bellinger * single vfs_[writev,readv] call. 559046ba642SNicholas Bellinger */ 560046ba642SNicholas Bellinger if (cmd->data_length > FD_MAX_BYTES) { 561046ba642SNicholas Bellinger pr_err("FILEIO: Not able to process I/O of %u bytes due to" 562046ba642SNicholas Bellinger "FD_MAX_BYTES: %u iovec count limitiation\n", 563046ba642SNicholas Bellinger cmd->data_length, FD_MAX_BYTES); 564046ba642SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 565046ba642SNicholas Bellinger } 566c66ac9dbSNicholas Bellinger /* 567c66ac9dbSNicholas Bellinger * Call vectorized fileio functions to map struct scatterlist 568c66ac9dbSNicholas Bellinger * physical memory addresses to struct iovec virtual memory. 569c66ac9dbSNicholas Bellinger */ 5705787cacdSChristoph Hellwig if (data_direction == DMA_FROM_DEVICE) { 571ee920469SNicholas Bellinger if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 5728287fa5fSSagi Grimberg ret = fd_do_rw(cmd, pfile, dev->prot_length, 5738287fa5fSSagi Grimberg cmd->t_prot_sg, cmd->t_prot_nents, 5748287fa5fSSagi Grimberg cmd->prot_length, 0); 57542201b55SNicholas Bellinger if (ret < 0) 57642201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 57742201b55SNicholas Bellinger } 57842201b55SNicholas Bellinger 5798287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 5808287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 0); 58142201b55SNicholas Bellinger 582ee920469SNicholas Bellinger if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) { 5838287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 5848287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 58542201b55SNicholas Bellinger 586f75b6faeSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 587f75b6faeSSagi Grimberg 0, cmd->t_prot_sg, 0); 5888287fa5fSSagi Grimberg if (rc) 58942201b55SNicholas Bellinger return rc; 59042201b55SNicholas Bellinger } 5918287fa5fSSagi Grimberg } else { 5928287fa5fSSagi Grimberg if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 5938287fa5fSSagi Grimberg u32 sectors = cmd->data_length >> 5948287fa5fSSagi Grimberg ilog2(dev->dev_attrib.block_size); 5958287fa5fSSagi Grimberg 5968287fa5fSSagi Grimberg rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 5978287fa5fSSagi Grimberg 0, cmd->t_prot_sg, 0); 5988287fa5fSSagi Grimberg if (rc) 5998287fa5fSSagi Grimberg return rc; 60042201b55SNicholas Bellinger } 60142201b55SNicholas Bellinger 6028287fa5fSSagi Grimberg ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size, 6038287fa5fSSagi Grimberg sgl, sgl_nents, cmd->data_length, 1); 604a4dff304SNicholas Bellinger /* 605125d0119SHannes Reinecke * Perform implicit vfs_fsync_range() for fd_do_writev() ops 606a4dff304SNicholas Bellinger * for SCSI WRITEs with Forced Unit Access (FUA) set. 607a4dff304SNicholas Bellinger * Allow this to happen independent of WCE=0 setting. 608a4dff304SNicholas Bellinger */ 609814e5b45SChristoph Hellwig if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) { 610a4dff304SNicholas Bellinger loff_t start = cmd->t_task_lba * 6110fd97ccfSChristoph Hellwig dev->dev_attrib.block_size; 61262d3ab49SZach Brown loff_t end; 61362d3ab49SZach Brown 61462d3ab49SZach Brown if (cmd->data_length) 61562d3ab49SZach Brown end = start + cmd->data_length - 1; 61662d3ab49SZach Brown else 61762d3ab49SZach Brown end = LLONG_MAX; 618c66ac9dbSNicholas Bellinger 619a4dff304SNicholas Bellinger vfs_fsync_range(fd_dev->fd_file, start, end, 1); 620a4dff304SNicholas Bellinger } 621c66ac9dbSNicholas Bellinger 622ee920469SNicholas Bellinger if (ret > 0 && 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, 1); 626de103c93SChristoph Hellwig if (ret < 0) 627de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 62842201b55SNicholas Bellinger } 62942201b55SNicholas Bellinger } 63042201b55SNicholas Bellinger 6318287fa5fSSagi Grimberg if (ret < 0) 63242201b55SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 633de103c93SChristoph Hellwig 6345787cacdSChristoph Hellwig if (ret) 6355787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 63603e98c9eSNicholas Bellinger return 0; 637c66ac9dbSNicholas Bellinger } 638c66ac9dbSNicholas Bellinger 639c66ac9dbSNicholas Bellinger enum { 640c66ac9dbSNicholas Bellinger Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err 641c66ac9dbSNicholas Bellinger }; 642c66ac9dbSNicholas Bellinger 643c66ac9dbSNicholas Bellinger static match_table_t tokens = { 644c66ac9dbSNicholas Bellinger {Opt_fd_dev_name, "fd_dev_name=%s"}, 645c66ac9dbSNicholas Bellinger {Opt_fd_dev_size, "fd_dev_size=%s"}, 646b32f4c7eSNicholas Bellinger {Opt_fd_buffered_io, "fd_buffered_io=%d"}, 647c66ac9dbSNicholas Bellinger {Opt_err, NULL} 648c66ac9dbSNicholas Bellinger }; 649c66ac9dbSNicholas Bellinger 6500fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev, 651c66ac9dbSNicholas Bellinger const char *page, ssize_t count) 652c66ac9dbSNicholas Bellinger { 6530fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 654c66ac9dbSNicholas Bellinger char *orig, *ptr, *arg_p, *opts; 655c66ac9dbSNicholas Bellinger substring_t args[MAX_OPT_ARGS]; 656b32f4c7eSNicholas Bellinger int ret = 0, arg, token; 657c66ac9dbSNicholas Bellinger 658c66ac9dbSNicholas Bellinger opts = kstrdup(page, GFP_KERNEL); 659c66ac9dbSNicholas Bellinger if (!opts) 660c66ac9dbSNicholas Bellinger return -ENOMEM; 661c66ac9dbSNicholas Bellinger 662c66ac9dbSNicholas Bellinger orig = opts; 663c66ac9dbSNicholas Bellinger 66490c161b6SSebastian Andrzej Siewior while ((ptr = strsep(&opts, ",\n")) != NULL) { 665c66ac9dbSNicholas Bellinger if (!*ptr) 666c66ac9dbSNicholas Bellinger continue; 667c66ac9dbSNicholas Bellinger 668c66ac9dbSNicholas Bellinger token = match_token(ptr, tokens, args); 669c66ac9dbSNicholas Bellinger switch (token) { 670c66ac9dbSNicholas Bellinger case Opt_fd_dev_name: 671dbc6e022SAl Viro if (match_strlcpy(fd_dev->fd_dev_name, &args[0], 672dbc6e022SAl Viro FD_MAX_DEV_NAME) == 0) { 673dbc6e022SAl Viro ret = -EINVAL; 6746d180253SJesper Juhl break; 6756d180253SJesper Juhl } 6766708bb27SAndy Grover pr_debug("FILEIO: Referencing Path: %s\n", 677c66ac9dbSNicholas Bellinger fd_dev->fd_dev_name); 678c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_PATH; 679c66ac9dbSNicholas Bellinger break; 680c66ac9dbSNicholas Bellinger case Opt_fd_dev_size: 681c66ac9dbSNicholas Bellinger arg_p = match_strdup(&args[0]); 6826d180253SJesper Juhl if (!arg_p) { 6836d180253SJesper Juhl ret = -ENOMEM; 6846d180253SJesper Juhl break; 6856d180253SJesper Juhl } 68657103d7fSJingoo Han ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size); 6876d180253SJesper Juhl kfree(arg_p); 688c66ac9dbSNicholas Bellinger if (ret < 0) { 68957103d7fSJingoo Han pr_err("kstrtoull() failed for" 690c66ac9dbSNicholas Bellinger " fd_dev_size=\n"); 691c66ac9dbSNicholas Bellinger goto out; 692c66ac9dbSNicholas Bellinger } 6936708bb27SAndy Grover pr_debug("FILEIO: Referencing Size: %llu" 694c66ac9dbSNicholas Bellinger " bytes\n", fd_dev->fd_dev_size); 695c66ac9dbSNicholas Bellinger fd_dev->fbd_flags |= FBDF_HAS_SIZE; 696c66ac9dbSNicholas Bellinger break; 697b32f4c7eSNicholas Bellinger case Opt_fd_buffered_io: 698ce31c1b0SJoern Engel ret = match_int(args, &arg); 699ce31c1b0SJoern Engel if (ret) 700ce31c1b0SJoern Engel goto out; 701b32f4c7eSNicholas Bellinger if (arg != 1) { 702b32f4c7eSNicholas Bellinger pr_err("bogus fd_buffered_io=%d value\n", arg); 703b32f4c7eSNicholas Bellinger ret = -EINVAL; 704b32f4c7eSNicholas Bellinger goto out; 705b32f4c7eSNicholas Bellinger } 706b32f4c7eSNicholas Bellinger 707b32f4c7eSNicholas Bellinger pr_debug("FILEIO: Using buffered I/O" 708b32f4c7eSNicholas Bellinger " operations for struct fd_dev\n"); 709b32f4c7eSNicholas Bellinger 710b32f4c7eSNicholas Bellinger fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE; 711b32f4c7eSNicholas Bellinger break; 712c66ac9dbSNicholas Bellinger default: 713c66ac9dbSNicholas Bellinger break; 714c66ac9dbSNicholas Bellinger } 715c66ac9dbSNicholas Bellinger } 716c66ac9dbSNicholas Bellinger 717c66ac9dbSNicholas Bellinger out: 718c66ac9dbSNicholas Bellinger kfree(orig); 719c66ac9dbSNicholas Bellinger return (!ret) ? count : ret; 720c66ac9dbSNicholas Bellinger } 721c66ac9dbSNicholas Bellinger 7220fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b) 723c66ac9dbSNicholas Bellinger { 7240fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 725c66ac9dbSNicholas Bellinger ssize_t bl = 0; 726c66ac9dbSNicholas Bellinger 727c66ac9dbSNicholas Bellinger bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id); 728b32f4c7eSNicholas Bellinger bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n", 729b32f4c7eSNicholas Bellinger fd_dev->fd_dev_name, fd_dev->fd_dev_size, 730b32f4c7eSNicholas Bellinger (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ? 731b32f4c7eSNicholas Bellinger "Buffered-WCE" : "O_DSYNC"); 732c66ac9dbSNicholas Bellinger return bl; 733c66ac9dbSNicholas Bellinger } 734c66ac9dbSNicholas Bellinger 735c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev) 736c66ac9dbSNicholas Bellinger { 7370fd97ccfSChristoph Hellwig struct fd_dev *fd_dev = FD_DEV(dev); 738cd9323fdSNicholas Bellinger struct file *f = fd_dev->fd_file; 739cd9323fdSNicholas Bellinger struct inode *i = f->f_mapping->host; 740cd9323fdSNicholas Bellinger unsigned long long dev_size; 741cd9323fdSNicholas Bellinger /* 742cd9323fdSNicholas Bellinger * When using a file that references an underlying struct block_device, 743cd9323fdSNicholas Bellinger * ensure dev_size is always based on the current inode size in order 744cd9323fdSNicholas Bellinger * to handle underlying block_device resize operations. 745cd9323fdSNicholas Bellinger */ 746cd9323fdSNicholas Bellinger if (S_ISBLK(i->i_mode)) 74721363ca8SNicholas Bellinger dev_size = i_size_read(i); 748cd9323fdSNicholas Bellinger else 749cd9323fdSNicholas Bellinger dev_size = fd_dev->fd_dev_size; 750c66ac9dbSNicholas Bellinger 75121363ca8SNicholas Bellinger return div_u64(dev_size - dev->dev_attrib.block_size, 75221363ca8SNicholas Bellinger dev->dev_attrib.block_size); 753c66ac9dbSNicholas Bellinger } 754c66ac9dbSNicholas Bellinger 7550f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev) 7560f5e2ec4SNicholas Bellinger { 7570f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 7580f5e2ec4SNicholas Bellinger struct file *prot_file, *file = fd_dev->fd_file; 7590f5e2ec4SNicholas Bellinger struct inode *inode; 7600f5e2ec4SNicholas Bellinger int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; 7610f5e2ec4SNicholas Bellinger char buf[FD_MAX_DEV_PROT_NAME]; 7620f5e2ec4SNicholas Bellinger 7630f5e2ec4SNicholas Bellinger if (!file) { 7640f5e2ec4SNicholas Bellinger pr_err("Unable to locate fd_dev->fd_file\n"); 7650f5e2ec4SNicholas Bellinger return -ENODEV; 7660f5e2ec4SNicholas Bellinger } 7670f5e2ec4SNicholas Bellinger 7680f5e2ec4SNicholas Bellinger inode = file->f_mapping->host; 7690f5e2ec4SNicholas Bellinger if (S_ISBLK(inode->i_mode)) { 7700f5e2ec4SNicholas Bellinger pr_err("FILEIO Protection emulation only supported on" 7710f5e2ec4SNicholas Bellinger " !S_ISBLK\n"); 7720f5e2ec4SNicholas Bellinger return -ENOSYS; 7730f5e2ec4SNicholas Bellinger } 7740f5e2ec4SNicholas Bellinger 7750f5e2ec4SNicholas Bellinger if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) 7760f5e2ec4SNicholas Bellinger flags &= ~O_DSYNC; 7770f5e2ec4SNicholas Bellinger 7780f5e2ec4SNicholas Bellinger snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection", 7790f5e2ec4SNicholas Bellinger fd_dev->fd_dev_name); 7800f5e2ec4SNicholas Bellinger 7810f5e2ec4SNicholas Bellinger prot_file = filp_open(buf, flags, 0600); 7820f5e2ec4SNicholas Bellinger if (IS_ERR(prot_file)) { 7830f5e2ec4SNicholas Bellinger pr_err("filp_open(%s) failed\n", buf); 7840f5e2ec4SNicholas Bellinger ret = PTR_ERR(prot_file); 7850f5e2ec4SNicholas Bellinger return ret; 7860f5e2ec4SNicholas Bellinger } 7870f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = prot_file; 7880f5e2ec4SNicholas Bellinger 7890f5e2ec4SNicholas Bellinger return 0; 7900f5e2ec4SNicholas Bellinger } 7910f5e2ec4SNicholas Bellinger 7920f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev) 7930f5e2ec4SNicholas Bellinger { 7940f5e2ec4SNicholas Bellinger unsigned char *buf; 7950f5e2ec4SNicholas Bellinger int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size; 79664d240b7SAkinobu Mita int ret; 7970f5e2ec4SNicholas Bellinger 7980f5e2ec4SNicholas Bellinger if (!dev->dev_attrib.pi_prot_type) { 7990f5e2ec4SNicholas Bellinger pr_err("Unable to format_prot while pi_prot_type == 0\n"); 8000f5e2ec4SNicholas Bellinger return -ENODEV; 8010f5e2ec4SNicholas Bellinger } 8020f5e2ec4SNicholas Bellinger 8030f5e2ec4SNicholas Bellinger buf = vzalloc(unit_size); 8040f5e2ec4SNicholas Bellinger if (!buf) { 8050f5e2ec4SNicholas Bellinger pr_err("Unable to allocate FILEIO prot buf\n"); 8060f5e2ec4SNicholas Bellinger return -ENOMEM; 8070f5e2ec4SNicholas Bellinger } 8080f5e2ec4SNicholas Bellinger 8090f5e2ec4SNicholas Bellinger pr_debug("Using FILEIO prot_length: %llu\n", 81064d240b7SAkinobu Mita (unsigned long long)(dev->transport->get_blocks(dev) + 1) * 81164d240b7SAkinobu Mita dev->prot_length); 8120f5e2ec4SNicholas Bellinger 81380dcd0c1SSagi Grimberg memset(buf, 0xff, unit_size); 81464d240b7SAkinobu Mita ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1, 81564d240b7SAkinobu Mita buf, unit_size); 8160f5e2ec4SNicholas Bellinger vfree(buf); 8170f5e2ec4SNicholas Bellinger return ret; 8180f5e2ec4SNicholas Bellinger } 8190f5e2ec4SNicholas Bellinger 8200f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev) 8210f5e2ec4SNicholas Bellinger { 8220f5e2ec4SNicholas Bellinger struct fd_dev *fd_dev = FD_DEV(dev); 8230f5e2ec4SNicholas Bellinger 8240f5e2ec4SNicholas Bellinger if (!fd_dev->fd_prot_file) 8250f5e2ec4SNicholas Bellinger return; 8260f5e2ec4SNicholas Bellinger 8270f5e2ec4SNicholas Bellinger filp_close(fd_dev->fd_prot_file, NULL); 8280f5e2ec4SNicholas Bellinger fd_dev->fd_prot_file = NULL; 8290f5e2ec4SNicholas Bellinger } 8300f5e2ec4SNicholas Bellinger 8319e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = { 8320c2ad7d1SChristoph Hellwig .execute_rw = fd_execute_rw, 833ad67f0d9SChristoph Hellwig .execute_sync_cache = fd_execute_sync_cache, 8347b745c84SNicholas Bellinger .execute_write_same = fd_execute_write_same, 83570d3ae5cSAsias He .execute_write_same_unmap = fd_execute_write_same_unmap, 83650642762SAsias He .execute_unmap = fd_execute_unmap, 8370c2ad7d1SChristoph Hellwig }; 8380c2ad7d1SChristoph Hellwig 839de103c93SChristoph Hellwig static sense_reason_t 840de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd) 8410c2ad7d1SChristoph Hellwig { 8429e999a6cSChristoph Hellwig return sbc_parse_cdb(cmd, &fd_sbc_ops); 8430c2ad7d1SChristoph Hellwig } 8440c2ad7d1SChristoph Hellwig 845b2320497SNicholas Bellinger DEF_TB_DEFAULT_ATTRIBS(fileio); 846b2320497SNicholas Bellinger 847b2320497SNicholas Bellinger static struct configfs_attribute *fileio_backend_dev_attrs[] = { 848b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_model_alias.attr, 849b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_dpo.attr, 850b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_fua_write.attr, 851b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_fua_read.attr, 852b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_write_cache.attr, 853b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_ua_intlck_ctrl.attr, 854b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_tas.attr, 855b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_tpu.attr, 856b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_tpws.attr, 857b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_caw.attr, 858b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_3pc.attr, 859b2320497SNicholas Bellinger &fileio_dev_attrib_pi_prot_type.attr, 860b2320497SNicholas Bellinger &fileio_dev_attrib_hw_pi_prot_type.attr, 861b2320497SNicholas Bellinger &fileio_dev_attrib_pi_prot_format.attr, 862b2320497SNicholas Bellinger &fileio_dev_attrib_enforce_pr_isids.attr, 863b2320497SNicholas Bellinger &fileio_dev_attrib_is_nonrot.attr, 864b2320497SNicholas Bellinger &fileio_dev_attrib_emulate_rest_reord.attr, 865b2320497SNicholas Bellinger &fileio_dev_attrib_force_pr_aptpl.attr, 866b2320497SNicholas Bellinger &fileio_dev_attrib_hw_block_size.attr, 867b2320497SNicholas Bellinger &fileio_dev_attrib_block_size.attr, 868b2320497SNicholas Bellinger &fileio_dev_attrib_hw_max_sectors.attr, 869b2320497SNicholas Bellinger &fileio_dev_attrib_optimal_sectors.attr, 870b2320497SNicholas Bellinger &fileio_dev_attrib_hw_queue_depth.attr, 871b2320497SNicholas Bellinger &fileio_dev_attrib_queue_depth.attr, 872b2320497SNicholas Bellinger &fileio_dev_attrib_max_unmap_lba_count.attr, 873b2320497SNicholas Bellinger &fileio_dev_attrib_max_unmap_block_desc_count.attr, 874b2320497SNicholas Bellinger &fileio_dev_attrib_unmap_granularity.attr, 875b2320497SNicholas Bellinger &fileio_dev_attrib_unmap_granularity_alignment.attr, 876b2320497SNicholas Bellinger &fileio_dev_attrib_max_write_same_len.attr, 877b2320497SNicholas Bellinger NULL, 878b2320497SNicholas Bellinger }; 879b2320497SNicholas Bellinger 8800a06d430SChristoph Hellwig static const struct target_backend_ops fileio_ops = { 881c66ac9dbSNicholas Bellinger .name = "fileio", 8820fd97ccfSChristoph Hellwig .inquiry_prod = "FILEIO", 8830fd97ccfSChristoph Hellwig .inquiry_rev = FD_VERSION, 884c66ac9dbSNicholas Bellinger .owner = THIS_MODULE, 885c66ac9dbSNicholas Bellinger .attach_hba = fd_attach_hba, 886c66ac9dbSNicholas Bellinger .detach_hba = fd_detach_hba, 8870fd97ccfSChristoph Hellwig .alloc_device = fd_alloc_device, 8880fd97ccfSChristoph Hellwig .configure_device = fd_configure_device, 889c66ac9dbSNicholas Bellinger .free_device = fd_free_device, 8900c2ad7d1SChristoph Hellwig .parse_cdb = fd_parse_cdb, 891c66ac9dbSNicholas Bellinger .set_configfs_dev_params = fd_set_configfs_dev_params, 892c66ac9dbSNicholas Bellinger .show_configfs_dev_params = fd_show_configfs_dev_params, 8936f23ac8aSChristoph Hellwig .get_device_type = sbc_get_device_type, 894c66ac9dbSNicholas Bellinger .get_blocks = fd_get_blocks, 8950f5e2ec4SNicholas Bellinger .init_prot = fd_init_prot, 8960f5e2ec4SNicholas Bellinger .format_prot = fd_format_prot, 8970f5e2ec4SNicholas Bellinger .free_prot = fd_free_prot, 8980a06d430SChristoph Hellwig .tb_dev_attrib_attrs = fileio_backend_dev_attrs, 899c66ac9dbSNicholas Bellinger }; 900c66ac9dbSNicholas Bellinger 901c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void) 902c66ac9dbSNicholas Bellinger { 9030a06d430SChristoph Hellwig return transport_backend_register(&fileio_ops); 904c66ac9dbSNicholas Bellinger } 905c66ac9dbSNicholas Bellinger 90663b91d5aSAsias He static void __exit fileio_module_exit(void) 907c66ac9dbSNicholas Bellinger { 9080a06d430SChristoph Hellwig target_backend_unregister(&fileio_ops); 909c66ac9dbSNicholas Bellinger } 910c66ac9dbSNicholas Bellinger 911c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin"); 912c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 913c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL"); 914c66ac9dbSNicholas Bellinger 915c66ac9dbSNicholas Bellinger module_init(fileio_module_init); 916c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit); 917