Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Filename: target_core_file.c |
| 3 | * |
| 4 | * This file contains the Storage Engine <-> FILEIO transport specific functions |
| 5 | * |
| 6 | * Copyright (c) 2005 PyX Technologies, Inc. |
| 7 | * Copyright (c) 2005-2006 SBE, Inc. All Rights Reserved. |
| 8 | * Copyright (c) 2007-2010 Rising Tide Systems |
| 9 | * Copyright (c) 2008-2010 Linux-iSCSI.org |
| 10 | * |
| 11 | * Nicholas A. Bellinger <nab@kernel.org> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 26 | * |
| 27 | ******************************************************************************/ |
| 28 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 29 | #include <linux/string.h> |
| 30 | #include <linux/parser.h> |
| 31 | #include <linux/timer.h> |
| 32 | #include <linux/blkdev.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/spinlock.h> |
Paul Gortmaker | 827509e | 2011-08-30 14:20:44 -0400 | [diff] [blame] | 35 | #include <linux/module.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 36 | #include <scsi/scsi.h> |
| 37 | #include <scsi/scsi_host.h> |
| 38 | |
| 39 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 40 | #include <target/target_core_backend.h> |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 41 | |
| 42 | #include "target_core_file.h" |
| 43 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 44 | static struct se_subsystem_api fileio_template; |
| 45 | |
| 46 | /* fd_attach_hba(): (Part of se_subsystem_api_t template) |
| 47 | * |
| 48 | * |
| 49 | */ |
| 50 | static int fd_attach_hba(struct se_hba *hba, u32 host_id) |
| 51 | { |
| 52 | struct fd_host *fd_host; |
| 53 | |
| 54 | fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 55 | if (!fd_host) { |
| 56 | pr_err("Unable to allocate memory for struct fd_host\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 57 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | fd_host->fd_host_id = host_id; |
| 61 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 62 | hba->hba_ptr = fd_host; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 63 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 64 | pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 65 | " Target Core Stack %s\n", hba->hba_id, FD_VERSION, |
| 66 | TARGET_CORE_MOD_VERSION); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 67 | pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 68 | " MaxSectors: %u\n", |
| 69 | hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static void fd_detach_hba(struct se_hba *hba) |
| 75 | { |
| 76 | struct fd_host *fd_host = hba->hba_ptr; |
| 77 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 78 | pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 79 | " Target Core\n", hba->hba_id, fd_host->fd_host_id); |
| 80 | |
| 81 | kfree(fd_host); |
| 82 | hba->hba_ptr = NULL; |
| 83 | } |
| 84 | |
| 85 | static void *fd_allocate_virtdevice(struct se_hba *hba, const char *name) |
| 86 | { |
| 87 | struct fd_dev *fd_dev; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 88 | struct fd_host *fd_host = hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 89 | |
| 90 | fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 91 | if (!fd_dev) { |
| 92 | pr_err("Unable to allocate memory for struct fd_dev\n"); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | fd_dev->fd_host = fd_host; |
| 97 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 98 | pr_debug("FILEIO: Allocated fd_dev for %p\n", name); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 99 | |
| 100 | return fd_dev; |
| 101 | } |
| 102 | |
| 103 | /* fd_create_virtdevice(): (Part of se_subsystem_api_t template) |
| 104 | * |
| 105 | * |
| 106 | */ |
| 107 | static struct se_device *fd_create_virtdevice( |
| 108 | struct se_hba *hba, |
| 109 | struct se_subsystem_dev *se_dev, |
| 110 | void *p) |
| 111 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 112 | struct se_device *dev; |
| 113 | struct se_dev_limits dev_limits; |
| 114 | struct queue_limits *limits; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 115 | struct fd_dev *fd_dev = p; |
| 116 | struct fd_host *fd_host = hba->hba_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 117 | struct file *file; |
| 118 | struct inode *inode = NULL; |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 119 | int dev_flags = 0, flags, ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 120 | |
| 121 | memset(&dev_limits, 0, sizeof(struct se_dev_limits)); |
| 122 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 123 | /* |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 124 | * Use O_DSYNC by default instead of O_SYNC to forgo syncing |
| 125 | * of pure timestamp updates. |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 126 | */ |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 127 | flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 128 | |
Al Viro | dbc6e02 | 2012-08-01 13:07:04 +0400 | [diff] [blame^] | 129 | file = filp_open(fd_dev->fd_dev_name, flags, 0600); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 130 | if (IS_ERR(file)) { |
Al Viro | dbc6e02 | 2012-08-01 13:07:04 +0400 | [diff] [blame^] | 131 | pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 132 | ret = PTR_ERR(file); |
| 133 | goto fail; |
| 134 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 135 | fd_dev->fd_file = file; |
| 136 | /* |
| 137 | * If using a block backend with this struct file, we extract |
| 138 | * fd_dev->fd_[block,dev]_size from struct block_device. |
| 139 | * |
| 140 | * Otherwise, we use the passed fd_size= from configfs |
| 141 | */ |
| 142 | inode = file->f_mapping->host; |
| 143 | if (S_ISBLK(inode->i_mode)) { |
| 144 | struct request_queue *q; |
Nicholas Bellinger | cd9323f | 2012-05-16 16:05:26 -0700 | [diff] [blame] | 145 | unsigned long long dev_size; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 146 | /* |
| 147 | * Setup the local scope queue_limits from struct request_queue->limits |
| 148 | * to pass into transport_add_device_to_core_hba() as struct se_dev_limits. |
| 149 | */ |
| 150 | q = bdev_get_queue(inode->i_bdev); |
| 151 | limits = &dev_limits.limits; |
| 152 | limits->logical_block_size = bdev_logical_block_size(inode->i_bdev); |
| 153 | limits->max_hw_sectors = queue_max_hw_sectors(q); |
| 154 | limits->max_sectors = queue_max_sectors(q); |
| 155 | /* |
| 156 | * Determine the number of bytes from i_size_read() minus |
| 157 | * one (1) logical sector from underlying struct block_device |
| 158 | */ |
| 159 | fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev); |
Nicholas Bellinger | cd9323f | 2012-05-16 16:05:26 -0700 | [diff] [blame] | 160 | dev_size = (i_size_read(file->f_mapping->host) - |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 161 | fd_dev->fd_block_size); |
| 162 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 163 | pr_debug("FILEIO: Using size: %llu bytes from struct" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 164 | " block_device blocks: %llu logical_block_size: %d\n", |
Nicholas Bellinger | cd9323f | 2012-05-16 16:05:26 -0700 | [diff] [blame] | 165 | dev_size, div_u64(dev_size, fd_dev->fd_block_size), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 166 | fd_dev->fd_block_size); |
| 167 | } else { |
| 168 | if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 169 | pr_err("FILEIO: Missing fd_dev_size=" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 170 | " parameter, and no backing struct" |
| 171 | " block_device\n"); |
| 172 | goto fail; |
| 173 | } |
| 174 | |
| 175 | limits = &dev_limits.limits; |
| 176 | limits->logical_block_size = FD_BLOCKSIZE; |
| 177 | limits->max_hw_sectors = FD_MAX_SECTORS; |
| 178 | limits->max_sectors = FD_MAX_SECTORS; |
| 179 | fd_dev->fd_block_size = FD_BLOCKSIZE; |
| 180 | } |
| 181 | |
| 182 | dev_limits.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; |
| 183 | dev_limits.queue_depth = FD_DEVICE_QUEUE_DEPTH; |
| 184 | |
| 185 | dev = transport_add_device_to_core_hba(hba, &fileio_template, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 186 | se_dev, dev_flags, fd_dev, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 187 | &dev_limits, "FILEIO", FD_VERSION); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 188 | if (!dev) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 189 | goto fail; |
| 190 | |
| 191 | fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++; |
| 192 | fd_dev->fd_queue_depth = dev->queue_depth; |
| 193 | |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 194 | pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 195 | " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id, |
| 196 | fd_dev->fd_dev_name, fd_dev->fd_dev_size); |
| 197 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 198 | return dev; |
| 199 | fail: |
| 200 | if (fd_dev->fd_file) { |
| 201 | filp_close(fd_dev->fd_file, NULL); |
| 202 | fd_dev->fd_file = NULL; |
| 203 | } |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 204 | return ERR_PTR(ret); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* fd_free_device(): (Part of se_subsystem_api_t template) |
| 208 | * |
| 209 | * |
| 210 | */ |
| 211 | static void fd_free_device(void *p) |
| 212 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 213 | struct fd_dev *fd_dev = p; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 214 | |
| 215 | if (fd_dev->fd_file) { |
| 216 | filp_close(fd_dev->fd_file, NULL); |
| 217 | fd_dev->fd_file = NULL; |
| 218 | } |
| 219 | |
| 220 | kfree(fd_dev); |
| 221 | } |
| 222 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 223 | static int fd_do_readv(struct se_cmd *cmd, struct scatterlist *sgl, |
| 224 | u32 sgl_nents) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 225 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 226 | struct se_device *se_dev = cmd->se_dev; |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 227 | struct fd_dev *dev = se_dev->dev_ptr; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 228 | struct file *fd = dev->fd_file; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 229 | struct scatterlist *sg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 230 | struct iovec *iov; |
| 231 | mm_segment_t old_fs; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 232 | loff_t pos = (cmd->t_task_lba * |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 233 | se_dev->se_sub_dev->se_dev_attrib.block_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 234 | int ret = 0, i; |
| 235 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 236 | iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 237 | if (!iov) { |
| 238 | pr_err("Unable to allocate fd_do_readv iov[]\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 239 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 242 | for_each_sg(sgl, sg, sgl_nents, i) { |
Sebastian Andrzej Siewior | 9649fa1 | 2011-11-28 12:33:10 +0100 | [diff] [blame] | 243 | iov[i].iov_len = sg->length; |
| 244 | iov[i].iov_base = sg_virt(sg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | old_fs = get_fs(); |
| 248 | set_fs(get_ds()); |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 249 | ret = vfs_readv(fd, &iov[0], sgl_nents, &pos); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 250 | set_fs(old_fs); |
| 251 | |
| 252 | kfree(iov); |
| 253 | /* |
| 254 | * Return zeros and GOOD status even if the READ did not return |
| 255 | * the expected virt_size for struct file w/o a backing struct |
| 256 | * block_device. |
| 257 | */ |
| 258 | if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 259 | if (ret < 0 || ret != cmd->data_length) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 260 | pr_err("vfs_readv() returned %d," |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 261 | " expecting %d for S_ISBLK\n", ret, |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 262 | (int)cmd->data_length); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 263 | return (ret < 0 ? ret : -EINVAL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 264 | } |
| 265 | } else { |
| 266 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 267 | pr_err("vfs_readv() returned %d for non" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 268 | " S_ISBLK\n", ret); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 269 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | return 1; |
| 274 | } |
| 275 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 276 | static int fd_do_writev(struct se_cmd *cmd, struct scatterlist *sgl, |
| 277 | u32 sgl_nents) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 278 | { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 279 | struct se_device *se_dev = cmd->se_dev; |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 280 | struct fd_dev *dev = se_dev->dev_ptr; |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 281 | struct file *fd = dev->fd_file; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 282 | struct scatterlist *sg; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 283 | struct iovec *iov; |
| 284 | mm_segment_t old_fs; |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 285 | loff_t pos = (cmd->t_task_lba * |
Christoph Hellwig | 42bf829 | 2011-10-12 11:07:00 -0400 | [diff] [blame] | 286 | se_dev->se_sub_dev->se_dev_attrib.block_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 287 | int ret, i = 0; |
| 288 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 289 | iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL); |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 290 | if (!iov) { |
| 291 | pr_err("Unable to allocate fd_do_writev iov[]\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 292 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 295 | for_each_sg(sgl, sg, sgl_nents, i) { |
Sebastian Andrzej Siewior | 9649fa1 | 2011-11-28 12:33:10 +0100 | [diff] [blame] | 296 | iov[i].iov_len = sg->length; |
| 297 | iov[i].iov_base = sg_virt(sg); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | old_fs = get_fs(); |
| 301 | set_fs(get_ds()); |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 302 | ret = vfs_writev(fd, &iov[0], sgl_nents, &pos); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 303 | set_fs(old_fs); |
| 304 | |
| 305 | kfree(iov); |
| 306 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 307 | if (ret < 0 || ret != cmd->data_length) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 308 | pr_err("vfs_writev() returned %d\n", ret); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 309 | return (ret < 0 ? ret : -EINVAL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return 1; |
| 313 | } |
| 314 | |
Christoph Hellwig | 6bb35e0 | 2012-04-23 11:35:33 -0400 | [diff] [blame] | 315 | static void fd_emulate_sync_cache(struct se_cmd *cmd) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 316 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 317 | struct se_device *dev = cmd->se_dev; |
| 318 | struct fd_dev *fd_dev = dev->dev_ptr; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 319 | int immed = (cmd->t_task_cdb[1] & 0x2); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 320 | loff_t start, end; |
| 321 | int ret; |
| 322 | |
| 323 | /* |
| 324 | * If the Immediate bit is set, queue up the GOOD response |
| 325 | * for this SYNCHRONIZE_CACHE op |
| 326 | */ |
| 327 | if (immed) |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 328 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Determine if we will be flushing the entire device. |
| 332 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 333 | if (cmd->t_task_lba == 0 && cmd->data_length == 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 334 | start = 0; |
| 335 | end = LLONG_MAX; |
| 336 | } else { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame] | 337 | start = cmd->t_task_lba * dev->se_sub_dev->se_dev_attrib.block_size; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 338 | if (cmd->data_length) |
| 339 | end = start + cmd->data_length; |
| 340 | else |
| 341 | end = LLONG_MAX; |
| 342 | } |
| 343 | |
| 344 | ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); |
| 345 | if (ret != 0) |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 346 | pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 347 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 348 | if (immed) |
| 349 | return; |
| 350 | |
| 351 | if (ret) { |
| 352 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
| 353 | target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); |
| 354 | } else { |
| 355 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
| 356 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 359 | static int fd_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl, |
| 360 | u32 sgl_nents, enum dma_data_direction data_direction) |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 361 | { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 362 | struct se_device *dev = cmd->se_dev; |
| 363 | int ret = 0; |
| 364 | |
| 365 | /* |
| 366 | * Call vectorized fileio functions to map struct scatterlist |
| 367 | * physical memory addresses to struct iovec virtual memory. |
| 368 | */ |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 369 | if (data_direction == DMA_FROM_DEVICE) { |
| 370 | ret = fd_do_readv(cmd, sgl, sgl_nents); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 371 | } else { |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 372 | ret = fd_do_writev(cmd, sgl, sgl_nents); |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 373 | /* |
| 374 | * Perform implict vfs_fsync_range() for fd_do_writev() ops |
| 375 | * for SCSI WRITEs with Forced Unit Access (FUA) set. |
| 376 | * Allow this to happen independent of WCE=0 setting. |
| 377 | */ |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 378 | if (ret > 0 && |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 379 | dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 && |
Christoph Hellwig | 2d3a4b5 | 2011-11-14 11:36:29 -0500 | [diff] [blame] | 380 | (cmd->se_cmd_flags & SCF_FUA)) { |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 381 | struct fd_dev *fd_dev = dev->dev_ptr; |
| 382 | loff_t start = cmd->t_task_lba * |
| 383 | dev->se_sub_dev->se_dev_attrib.block_size; |
| 384 | loff_t end = start + cmd->data_length; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 385 | |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 386 | vfs_fsync_range(fd_dev->fd_file, start, end, 1); |
| 387 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 390 | if (ret < 0) { |
| 391 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 392 | return ret; |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 393 | } |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 394 | if (ret) |
| 395 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
Nicholas Bellinger | 03e98c9 | 2011-11-04 02:36:16 -0700 | [diff] [blame] | 396 | return 0; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 399 | enum { |
| 400 | Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err |
| 401 | }; |
| 402 | |
| 403 | static match_table_t tokens = { |
| 404 | {Opt_fd_dev_name, "fd_dev_name=%s"}, |
| 405 | {Opt_fd_dev_size, "fd_dev_size=%s"}, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 406 | {Opt_err, NULL} |
| 407 | }; |
| 408 | |
| 409 | static ssize_t fd_set_configfs_dev_params( |
| 410 | struct se_hba *hba, |
| 411 | struct se_subsystem_dev *se_dev, |
| 412 | const char *page, ssize_t count) |
| 413 | { |
| 414 | struct fd_dev *fd_dev = se_dev->se_dev_su_ptr; |
| 415 | char *orig, *ptr, *arg_p, *opts; |
| 416 | substring_t args[MAX_OPT_ARGS]; |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 417 | int ret = 0, token; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 418 | |
| 419 | opts = kstrdup(page, GFP_KERNEL); |
| 420 | if (!opts) |
| 421 | return -ENOMEM; |
| 422 | |
| 423 | orig = opts; |
| 424 | |
Sebastian Andrzej Siewior | 90c161b | 2011-11-23 20:53:17 +0100 | [diff] [blame] | 425 | while ((ptr = strsep(&opts, ",\n")) != NULL) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 426 | if (!*ptr) |
| 427 | continue; |
| 428 | |
| 429 | token = match_token(ptr, tokens, args); |
| 430 | switch (token) { |
| 431 | case Opt_fd_dev_name: |
Al Viro | dbc6e02 | 2012-08-01 13:07:04 +0400 | [diff] [blame^] | 432 | if (match_strlcpy(fd_dev->fd_dev_name, &args[0], |
| 433 | FD_MAX_DEV_NAME) == 0) { |
| 434 | ret = -EINVAL; |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 435 | break; |
| 436 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 437 | pr_debug("FILEIO: Referencing Path: %s\n", |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 438 | fd_dev->fd_dev_name); |
| 439 | fd_dev->fbd_flags |= FBDF_HAS_PATH; |
| 440 | break; |
| 441 | case Opt_fd_dev_size: |
| 442 | arg_p = match_strdup(&args[0]); |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 443 | if (!arg_p) { |
| 444 | ret = -ENOMEM; |
| 445 | break; |
| 446 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 447 | ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size); |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 448 | kfree(arg_p); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 449 | if (ret < 0) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 450 | pr_err("strict_strtoull() failed for" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 451 | " fd_dev_size=\n"); |
| 452 | goto out; |
| 453 | } |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 454 | pr_debug("FILEIO: Referencing Size: %llu" |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 455 | " bytes\n", fd_dev->fd_dev_size); |
| 456 | fd_dev->fbd_flags |= FBDF_HAS_SIZE; |
| 457 | break; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 458 | default: |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | out: |
| 464 | kfree(orig); |
| 465 | return (!ret) ? count : ret; |
| 466 | } |
| 467 | |
| 468 | static ssize_t fd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev) |
| 469 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 470 | struct fd_dev *fd_dev = se_dev->se_dev_su_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 471 | |
| 472 | if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) { |
Andy Grover | 6708bb2 | 2011-06-08 10:36:43 -0700 | [diff] [blame] | 473 | pr_err("Missing fd_dev_name=\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 474 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static ssize_t fd_show_configfs_dev_params( |
| 481 | struct se_hba *hba, |
| 482 | struct se_subsystem_dev *se_dev, |
| 483 | char *b) |
| 484 | { |
| 485 | struct fd_dev *fd_dev = se_dev->se_dev_su_ptr; |
| 486 | ssize_t bl = 0; |
| 487 | |
| 488 | bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id); |
Nicholas Bellinger | a4dff30 | 2012-05-30 16:25:41 -0700 | [diff] [blame] | 489 | bl += sprintf(b + bl, " File: %s Size: %llu Mode: O_DSYNC\n", |
| 490 | fd_dev->fd_dev_name, fd_dev->fd_dev_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 491 | return bl; |
| 492 | } |
| 493 | |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 494 | /* fd_get_device_rev(): (Part of se_subsystem_api_t template) |
| 495 | * |
| 496 | * |
| 497 | */ |
| 498 | static u32 fd_get_device_rev(struct se_device *dev) |
| 499 | { |
| 500 | return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */ |
| 501 | } |
| 502 | |
| 503 | /* fd_get_device_type(): (Part of se_subsystem_api_t template) |
| 504 | * |
| 505 | * |
| 506 | */ |
| 507 | static u32 fd_get_device_type(struct se_device *dev) |
| 508 | { |
| 509 | return TYPE_DISK; |
| 510 | } |
| 511 | |
| 512 | static sector_t fd_get_blocks(struct se_device *dev) |
| 513 | { |
| 514 | struct fd_dev *fd_dev = dev->dev_ptr; |
Nicholas Bellinger | cd9323f | 2012-05-16 16:05:26 -0700 | [diff] [blame] | 515 | struct file *f = fd_dev->fd_file; |
| 516 | struct inode *i = f->f_mapping->host; |
| 517 | unsigned long long dev_size; |
| 518 | /* |
| 519 | * When using a file that references an underlying struct block_device, |
| 520 | * ensure dev_size is always based on the current inode size in order |
| 521 | * to handle underlying block_device resize operations. |
| 522 | */ |
| 523 | if (S_ISBLK(i->i_mode)) |
| 524 | dev_size = (i_size_read(i) - fd_dev->fd_block_size); |
| 525 | else |
| 526 | dev_size = fd_dev->fd_dev_size; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 527 | |
Nicholas Bellinger | cd9323f | 2012-05-16 16:05:26 -0700 | [diff] [blame] | 528 | return div_u64(dev_size, dev->se_sub_dev->se_dev_attrib.block_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static struct se_subsystem_api fileio_template = { |
| 532 | .name = "fileio", |
| 533 | .owner = THIS_MODULE, |
| 534 | .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV, |
Christoph Hellwig | f55918f | 2011-10-14 07:30:17 -0400 | [diff] [blame] | 535 | .write_cache_emulated = 1, |
| 536 | .fua_write_emulated = 1, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 537 | .attach_hba = fd_attach_hba, |
| 538 | .detach_hba = fd_detach_hba, |
| 539 | .allocate_virtdevice = fd_allocate_virtdevice, |
| 540 | .create_virtdevice = fd_create_virtdevice, |
| 541 | .free_device = fd_free_device, |
Christoph Hellwig | 5787cac | 2012-04-24 00:25:06 -0400 | [diff] [blame] | 542 | .execute_cmd = fd_execute_cmd, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 543 | .do_sync_cache = fd_emulate_sync_cache, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 544 | .check_configfs_dev_params = fd_check_configfs_dev_params, |
| 545 | .set_configfs_dev_params = fd_set_configfs_dev_params, |
| 546 | .show_configfs_dev_params = fd_show_configfs_dev_params, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 547 | .get_device_rev = fd_get_device_rev, |
| 548 | .get_device_type = fd_get_device_type, |
| 549 | .get_blocks = fd_get_blocks, |
| 550 | }; |
| 551 | |
| 552 | static int __init fileio_module_init(void) |
| 553 | { |
| 554 | return transport_subsystem_register(&fileio_template); |
| 555 | } |
| 556 | |
| 557 | static void fileio_module_exit(void) |
| 558 | { |
| 559 | transport_subsystem_release(&fileio_template); |
| 560 | } |
| 561 | |
| 562 | MODULE_DESCRIPTION("TCM FILEIO subsystem plugin"); |
| 563 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); |
| 564 | MODULE_LICENSE("GPL"); |
| 565 | |
| 566 | module_init(fileio_module_init); |
| 567 | module_exit(fileio_module_exit); |