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 | |
| 29 | #include <linux/version.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/parser.h> |
| 32 | #include <linux/timer.h> |
| 33 | #include <linux/blkdev.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/spinlock.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> |
| 40 | #include <target/target_core_device.h> |
| 41 | #include <target/target_core_transport.h> |
| 42 | |
| 43 | #include "target_core_file.h" |
| 44 | |
| 45 | #if 1 |
| 46 | #define DEBUG_FD_CACHE(x...) printk(x) |
| 47 | #else |
| 48 | #define DEBUG_FD_CACHE(x...) |
| 49 | #endif |
| 50 | |
| 51 | #if 1 |
| 52 | #define DEBUG_FD_FUA(x...) printk(x) |
| 53 | #else |
| 54 | #define DEBUG_FD_FUA(x...) |
| 55 | #endif |
| 56 | |
| 57 | static struct se_subsystem_api fileio_template; |
| 58 | |
| 59 | /* fd_attach_hba(): (Part of se_subsystem_api_t template) |
| 60 | * |
| 61 | * |
| 62 | */ |
| 63 | static int fd_attach_hba(struct se_hba *hba, u32 host_id) |
| 64 | { |
| 65 | struct fd_host *fd_host; |
| 66 | |
| 67 | fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL); |
| 68 | if (!(fd_host)) { |
| 69 | printk(KERN_ERR "Unable to allocate memory for struct fd_host\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 70 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | fd_host->fd_host_id = host_id; |
| 74 | |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 75 | hba->hba_ptr = fd_host; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 76 | |
| 77 | printk(KERN_INFO "CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic" |
| 78 | " Target Core Stack %s\n", hba->hba_id, FD_VERSION, |
| 79 | TARGET_CORE_MOD_VERSION); |
| 80 | printk(KERN_INFO "CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic" |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 81 | " MaxSectors: %u\n", |
| 82 | hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static void fd_detach_hba(struct se_hba *hba) |
| 88 | { |
| 89 | struct fd_host *fd_host = hba->hba_ptr; |
| 90 | |
| 91 | printk(KERN_INFO "CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic" |
| 92 | " Target Core\n", hba->hba_id, fd_host->fd_host_id); |
| 93 | |
| 94 | kfree(fd_host); |
| 95 | hba->hba_ptr = NULL; |
| 96 | } |
| 97 | |
| 98 | static void *fd_allocate_virtdevice(struct se_hba *hba, const char *name) |
| 99 | { |
| 100 | struct fd_dev *fd_dev; |
| 101 | struct fd_host *fd_host = (struct fd_host *) hba->hba_ptr; |
| 102 | |
| 103 | fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL); |
| 104 | if (!(fd_dev)) { |
| 105 | printk(KERN_ERR "Unable to allocate memory for struct fd_dev\n"); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | fd_dev->fd_host = fd_host; |
| 110 | |
| 111 | printk(KERN_INFO "FILEIO: Allocated fd_dev for %p\n", name); |
| 112 | |
| 113 | return fd_dev; |
| 114 | } |
| 115 | |
| 116 | /* fd_create_virtdevice(): (Part of se_subsystem_api_t template) |
| 117 | * |
| 118 | * |
| 119 | */ |
| 120 | static struct se_device *fd_create_virtdevice( |
| 121 | struct se_hba *hba, |
| 122 | struct se_subsystem_dev *se_dev, |
| 123 | void *p) |
| 124 | { |
| 125 | char *dev_p = NULL; |
| 126 | struct se_device *dev; |
| 127 | struct se_dev_limits dev_limits; |
| 128 | struct queue_limits *limits; |
| 129 | struct fd_dev *fd_dev = (struct fd_dev *) p; |
| 130 | struct fd_host *fd_host = (struct fd_host *) hba->hba_ptr; |
| 131 | mm_segment_t old_fs; |
| 132 | struct file *file; |
| 133 | struct inode *inode = NULL; |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 134 | int dev_flags = 0, flags, ret = -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 135 | |
| 136 | memset(&dev_limits, 0, sizeof(struct se_dev_limits)); |
| 137 | |
| 138 | old_fs = get_fs(); |
| 139 | set_fs(get_ds()); |
| 140 | dev_p = getname(fd_dev->fd_dev_name); |
| 141 | set_fs(old_fs); |
| 142 | |
| 143 | if (IS_ERR(dev_p)) { |
| 144 | printk(KERN_ERR "getname(%s) failed: %lu\n", |
| 145 | fd_dev->fd_dev_name, IS_ERR(dev_p)); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 146 | ret = PTR_ERR(dev_p); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 147 | goto fail; |
| 148 | } |
| 149 | #if 0 |
| 150 | if (di->no_create_file) |
| 151 | flags = O_RDWR | O_LARGEFILE; |
| 152 | else |
| 153 | flags = O_RDWR | O_CREAT | O_LARGEFILE; |
| 154 | #else |
| 155 | flags = O_RDWR | O_CREAT | O_LARGEFILE; |
| 156 | #endif |
| 157 | /* flags |= O_DIRECT; */ |
| 158 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 159 | * If fd_buffered_io=1 has not been set explicitly (the default), |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 160 | * use O_SYNC to force FILEIO writes to disk. |
| 161 | */ |
| 162 | if (!(fd_dev->fbd_flags & FDBD_USE_BUFFERED_IO)) |
| 163 | flags |= O_SYNC; |
| 164 | |
| 165 | file = filp_open(dev_p, flags, 0600); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 166 | if (IS_ERR(file)) { |
| 167 | printk(KERN_ERR "filp_open(%s) failed\n", dev_p); |
| 168 | ret = PTR_ERR(file); |
| 169 | goto fail; |
| 170 | } |
| 171 | if (!file || !file->f_dentry) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 172 | printk(KERN_ERR "filp_open(%s) failed\n", dev_p); |
| 173 | goto fail; |
| 174 | } |
| 175 | fd_dev->fd_file = file; |
| 176 | /* |
| 177 | * If using a block backend with this struct file, we extract |
| 178 | * fd_dev->fd_[block,dev]_size from struct block_device. |
| 179 | * |
| 180 | * Otherwise, we use the passed fd_size= from configfs |
| 181 | */ |
| 182 | inode = file->f_mapping->host; |
| 183 | if (S_ISBLK(inode->i_mode)) { |
| 184 | struct request_queue *q; |
| 185 | /* |
| 186 | * Setup the local scope queue_limits from struct request_queue->limits |
| 187 | * to pass into transport_add_device_to_core_hba() as struct se_dev_limits. |
| 188 | */ |
| 189 | q = bdev_get_queue(inode->i_bdev); |
| 190 | limits = &dev_limits.limits; |
| 191 | limits->logical_block_size = bdev_logical_block_size(inode->i_bdev); |
| 192 | limits->max_hw_sectors = queue_max_hw_sectors(q); |
| 193 | limits->max_sectors = queue_max_sectors(q); |
| 194 | /* |
| 195 | * Determine the number of bytes from i_size_read() minus |
| 196 | * one (1) logical sector from underlying struct block_device |
| 197 | */ |
| 198 | fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev); |
| 199 | fd_dev->fd_dev_size = (i_size_read(file->f_mapping->host) - |
| 200 | fd_dev->fd_block_size); |
| 201 | |
| 202 | printk(KERN_INFO "FILEIO: Using size: %llu bytes from struct" |
| 203 | " block_device blocks: %llu logical_block_size: %d\n", |
| 204 | fd_dev->fd_dev_size, |
| 205 | div_u64(fd_dev->fd_dev_size, fd_dev->fd_block_size), |
| 206 | fd_dev->fd_block_size); |
| 207 | } else { |
| 208 | if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) { |
| 209 | printk(KERN_ERR "FILEIO: Missing fd_dev_size=" |
| 210 | " parameter, and no backing struct" |
| 211 | " block_device\n"); |
| 212 | goto fail; |
| 213 | } |
| 214 | |
| 215 | limits = &dev_limits.limits; |
| 216 | limits->logical_block_size = FD_BLOCKSIZE; |
| 217 | limits->max_hw_sectors = FD_MAX_SECTORS; |
| 218 | limits->max_sectors = FD_MAX_SECTORS; |
| 219 | fd_dev->fd_block_size = FD_BLOCKSIZE; |
| 220 | } |
| 221 | |
| 222 | dev_limits.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH; |
| 223 | dev_limits.queue_depth = FD_DEVICE_QUEUE_DEPTH; |
| 224 | |
| 225 | dev = transport_add_device_to_core_hba(hba, &fileio_template, |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 226 | se_dev, dev_flags, fd_dev, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 227 | &dev_limits, "FILEIO", FD_VERSION); |
| 228 | if (!(dev)) |
| 229 | goto fail; |
| 230 | |
| 231 | fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++; |
| 232 | fd_dev->fd_queue_depth = dev->queue_depth; |
| 233 | |
| 234 | printk(KERN_INFO "CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s," |
| 235 | " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id, |
| 236 | fd_dev->fd_dev_name, fd_dev->fd_dev_size); |
| 237 | |
| 238 | putname(dev_p); |
| 239 | return dev; |
| 240 | fail: |
| 241 | if (fd_dev->fd_file) { |
| 242 | filp_close(fd_dev->fd_file, NULL); |
| 243 | fd_dev->fd_file = NULL; |
| 244 | } |
| 245 | putname(dev_p); |
Nicholas Bellinger | 613640e | 2011-03-14 04:05:59 -0700 | [diff] [blame] | 246 | return ERR_PTR(ret); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* fd_free_device(): (Part of se_subsystem_api_t template) |
| 250 | * |
| 251 | * |
| 252 | */ |
| 253 | static void fd_free_device(void *p) |
| 254 | { |
| 255 | struct fd_dev *fd_dev = (struct fd_dev *) p; |
| 256 | |
| 257 | if (fd_dev->fd_file) { |
| 258 | filp_close(fd_dev->fd_file, NULL); |
| 259 | fd_dev->fd_file = NULL; |
| 260 | } |
| 261 | |
| 262 | kfree(fd_dev); |
| 263 | } |
| 264 | |
| 265 | static inline struct fd_request *FILE_REQ(struct se_task *task) |
| 266 | { |
| 267 | return container_of(task, struct fd_request, fd_task); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static struct se_task * |
| 272 | fd_alloc_task(struct se_cmd *cmd) |
| 273 | { |
| 274 | struct fd_request *fd_req; |
| 275 | |
| 276 | fd_req = kzalloc(sizeof(struct fd_request), GFP_KERNEL); |
| 277 | if (!(fd_req)) { |
| 278 | printk(KERN_ERR "Unable to allocate struct fd_request\n"); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Andy Grover | 5951146d | 2011-07-19 10:26:37 +0000 | [diff] [blame] | 282 | fd_req->fd_dev = cmd->se_dev->dev_ptr; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 283 | |
| 284 | return &fd_req->fd_task; |
| 285 | } |
| 286 | |
| 287 | static int fd_do_readv(struct se_task *task) |
| 288 | { |
| 289 | struct fd_request *req = FILE_REQ(task); |
| 290 | struct file *fd = req->fd_dev->fd_file; |
| 291 | struct scatterlist *sg = task->task_sg; |
| 292 | struct iovec *iov; |
| 293 | mm_segment_t old_fs; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 294 | loff_t pos = (task->task_lba * |
| 295 | task->se_dev->se_sub_dev->se_dev_attrib.block_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 296 | int ret = 0, i; |
| 297 | |
| 298 | iov = kzalloc(sizeof(struct iovec) * task->task_sg_num, GFP_KERNEL); |
| 299 | if (!(iov)) { |
| 300 | printk(KERN_ERR "Unable to allocate fd_do_readv iov[]\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 301 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | for (i = 0; i < task->task_sg_num; i++) { |
| 305 | iov[i].iov_len = sg[i].length; |
| 306 | iov[i].iov_base = sg_virt(&sg[i]); |
| 307 | } |
| 308 | |
| 309 | old_fs = get_fs(); |
| 310 | set_fs(get_ds()); |
| 311 | ret = vfs_readv(fd, &iov[0], task->task_sg_num, &pos); |
| 312 | set_fs(old_fs); |
| 313 | |
| 314 | kfree(iov); |
| 315 | /* |
| 316 | * Return zeros and GOOD status even if the READ did not return |
| 317 | * the expected virt_size for struct file w/o a backing struct |
| 318 | * block_device. |
| 319 | */ |
| 320 | if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) { |
| 321 | if (ret < 0 || ret != task->task_size) { |
| 322 | printk(KERN_ERR "vfs_readv() returned %d," |
| 323 | " expecting %d for S_ISBLK\n", ret, |
| 324 | (int)task->task_size); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 325 | return (ret < 0 ? ret : -EINVAL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 326 | } |
| 327 | } else { |
| 328 | if (ret < 0) { |
| 329 | printk(KERN_ERR "vfs_readv() returned %d for non" |
| 330 | " S_ISBLK\n", ret); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 331 | return ret; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | return 1; |
| 336 | } |
| 337 | |
| 338 | static int fd_do_writev(struct se_task *task) |
| 339 | { |
| 340 | struct fd_request *req = FILE_REQ(task); |
| 341 | struct file *fd = req->fd_dev->fd_file; |
| 342 | struct scatterlist *sg = task->task_sg; |
| 343 | struct iovec *iov; |
| 344 | mm_segment_t old_fs; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 345 | loff_t pos = (task->task_lba * |
| 346 | task->se_dev->se_sub_dev->se_dev_attrib.block_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 347 | int ret, i = 0; |
| 348 | |
| 349 | iov = kzalloc(sizeof(struct iovec) * task->task_sg_num, GFP_KERNEL); |
| 350 | if (!(iov)) { |
| 351 | printk(KERN_ERR "Unable to allocate fd_do_writev iov[]\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 352 | return -ENOMEM; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | for (i = 0; i < task->task_sg_num; i++) { |
| 356 | iov[i].iov_len = sg[i].length; |
| 357 | iov[i].iov_base = sg_virt(&sg[i]); |
| 358 | } |
| 359 | |
| 360 | old_fs = get_fs(); |
| 361 | set_fs(get_ds()); |
| 362 | ret = vfs_writev(fd, &iov[0], task->task_sg_num, &pos); |
| 363 | set_fs(old_fs); |
| 364 | |
| 365 | kfree(iov); |
| 366 | |
| 367 | if (ret < 0 || ret != task->task_size) { |
| 368 | printk(KERN_ERR "vfs_writev() returned %d\n", ret); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 369 | return (ret < 0 ? ret : -EINVAL); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return 1; |
| 373 | } |
| 374 | |
| 375 | static void fd_emulate_sync_cache(struct se_task *task) |
| 376 | { |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 377 | struct se_cmd *cmd = task->task_se_cmd; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 378 | struct se_device *dev = cmd->se_dev; |
| 379 | struct fd_dev *fd_dev = dev->dev_ptr; |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame^] | 380 | int immed = (cmd->t_task_cdb[1] & 0x2); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 381 | loff_t start, end; |
| 382 | int ret; |
| 383 | |
| 384 | /* |
| 385 | * If the Immediate bit is set, queue up the GOOD response |
| 386 | * for this SYNCHRONIZE_CACHE op |
| 387 | */ |
| 388 | if (immed) |
| 389 | transport_complete_sync_cache(cmd, 1); |
| 390 | |
| 391 | /* |
| 392 | * Determine if we will be flushing the entire device. |
| 393 | */ |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame^] | 394 | if (cmd->t_task_lba == 0 && cmd->data_length == 0) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 395 | start = 0; |
| 396 | end = LLONG_MAX; |
| 397 | } else { |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame^] | 398 | 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] | 399 | if (cmd->data_length) |
| 400 | end = start + cmd->data_length; |
| 401 | else |
| 402 | end = LLONG_MAX; |
| 403 | } |
| 404 | |
| 405 | ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); |
| 406 | if (ret != 0) |
| 407 | printk(KERN_ERR "FILEIO: vfs_fsync_range() failed: %d\n", ret); |
| 408 | |
| 409 | if (!immed) |
| 410 | transport_complete_sync_cache(cmd, ret == 0); |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Tell TCM Core that we are capable of WriteCache emulation for |
| 415 | * an underlying struct se_device. |
| 416 | */ |
| 417 | static int fd_emulated_write_cache(struct se_device *dev) |
| 418 | { |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | static int fd_emulated_dpo(struct se_device *dev) |
| 423 | { |
| 424 | return 0; |
| 425 | } |
| 426 | /* |
| 427 | * Tell TCM Core that we will be emulating Forced Unit Access (FUA) for WRITEs |
| 428 | * for TYPE_DISK. |
| 429 | */ |
| 430 | static int fd_emulated_fua_write(struct se_device *dev) |
| 431 | { |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | static int fd_emulated_fua_read(struct se_device *dev) |
| 436 | { |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * WRITE Force Unit Access (FUA) emulation on a per struct se_task |
| 442 | * LBA range basis.. |
| 443 | */ |
| 444 | static void fd_emulate_write_fua(struct se_cmd *cmd, struct se_task *task) |
| 445 | { |
| 446 | struct se_device *dev = cmd->se_dev; |
| 447 | struct fd_dev *fd_dev = dev->dev_ptr; |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 448 | loff_t start = task->task_lba * dev->se_sub_dev->se_dev_attrib.block_size; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 449 | loff_t end = start + task->task_size; |
| 450 | int ret; |
| 451 | |
| 452 | DEBUG_FD_CACHE("FILEIO: FUA WRITE LBA: %llu, bytes: %u\n", |
| 453 | task->task_lba, task->task_size); |
| 454 | |
| 455 | ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1); |
| 456 | if (ret != 0) |
| 457 | printk(KERN_ERR "FILEIO: vfs_fsync_range() failed: %d\n", ret); |
| 458 | } |
| 459 | |
| 460 | static int fd_do_task(struct se_task *task) |
| 461 | { |
| 462 | struct se_cmd *cmd = task->task_se_cmd; |
| 463 | struct se_device *dev = cmd->se_dev; |
| 464 | int ret = 0; |
| 465 | |
| 466 | /* |
| 467 | * Call vectorized fileio functions to map struct scatterlist |
| 468 | * physical memory addresses to struct iovec virtual memory. |
| 469 | */ |
| 470 | if (task->task_data_direction == DMA_FROM_DEVICE) { |
| 471 | ret = fd_do_readv(task); |
| 472 | } else { |
| 473 | ret = fd_do_writev(task); |
| 474 | |
| 475 | if (ret > 0 && |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 476 | dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0 && |
| 477 | dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 && |
Andy Grover | a1d8b49 | 2011-05-02 17:12:10 -0700 | [diff] [blame^] | 478 | cmd->t_tasks_fua) { |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 479 | /* |
| 480 | * We might need to be a bit smarter here |
| 481 | * and return some sense data to let the initiator |
| 482 | * know the FUA WRITE cache sync failed..? |
| 483 | */ |
| 484 | fd_emulate_write_fua(cmd, task); |
| 485 | } |
| 486 | |
| 487 | } |
| 488 | |
| 489 | if (ret < 0) |
| 490 | return ret; |
| 491 | if (ret) { |
| 492 | task->task_scsi_status = GOOD; |
| 493 | transport_complete_task(task, 1); |
| 494 | } |
| 495 | return PYX_TRANSPORT_SENT_TO_TRANSPORT; |
| 496 | } |
| 497 | |
| 498 | /* fd_free_task(): (Part of se_subsystem_api_t template) |
| 499 | * |
| 500 | * |
| 501 | */ |
| 502 | static void fd_free_task(struct se_task *task) |
| 503 | { |
| 504 | struct fd_request *req = FILE_REQ(task); |
| 505 | |
| 506 | kfree(req); |
| 507 | } |
| 508 | |
| 509 | enum { |
| 510 | Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err |
| 511 | }; |
| 512 | |
| 513 | static match_table_t tokens = { |
| 514 | {Opt_fd_dev_name, "fd_dev_name=%s"}, |
| 515 | {Opt_fd_dev_size, "fd_dev_size=%s"}, |
Nicholas Bellinger | 58c3e64 | 2011-03-14 04:06:08 -0700 | [diff] [blame] | 516 | {Opt_fd_buffered_io, "fd_buffered_io=%d"}, |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 517 | {Opt_err, NULL} |
| 518 | }; |
| 519 | |
| 520 | static ssize_t fd_set_configfs_dev_params( |
| 521 | struct se_hba *hba, |
| 522 | struct se_subsystem_dev *se_dev, |
| 523 | const char *page, ssize_t count) |
| 524 | { |
| 525 | struct fd_dev *fd_dev = se_dev->se_dev_su_ptr; |
| 526 | char *orig, *ptr, *arg_p, *opts; |
| 527 | substring_t args[MAX_OPT_ARGS]; |
| 528 | int ret = 0, arg, token; |
| 529 | |
| 530 | opts = kstrdup(page, GFP_KERNEL); |
| 531 | if (!opts) |
| 532 | return -ENOMEM; |
| 533 | |
| 534 | orig = opts; |
| 535 | |
| 536 | while ((ptr = strsep(&opts, ",")) != NULL) { |
| 537 | if (!*ptr) |
| 538 | continue; |
| 539 | |
| 540 | token = match_token(ptr, tokens, args); |
| 541 | switch (token) { |
| 542 | case Opt_fd_dev_name: |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 543 | arg_p = match_strdup(&args[0]); |
| 544 | if (!arg_p) { |
| 545 | ret = -ENOMEM; |
| 546 | break; |
| 547 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 548 | snprintf(fd_dev->fd_dev_name, FD_MAX_DEV_NAME, |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 549 | "%s", arg_p); |
| 550 | kfree(arg_p); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 551 | printk(KERN_INFO "FILEIO: Referencing Path: %s\n", |
| 552 | fd_dev->fd_dev_name); |
| 553 | fd_dev->fbd_flags |= FBDF_HAS_PATH; |
| 554 | break; |
| 555 | case Opt_fd_dev_size: |
| 556 | arg_p = match_strdup(&args[0]); |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 557 | if (!arg_p) { |
| 558 | ret = -ENOMEM; |
| 559 | break; |
| 560 | } |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 561 | ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size); |
Jesper Juhl | 6d18025 | 2011-03-14 04:05:56 -0700 | [diff] [blame] | 562 | kfree(arg_p); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 563 | if (ret < 0) { |
| 564 | printk(KERN_ERR "strict_strtoull() failed for" |
| 565 | " fd_dev_size=\n"); |
| 566 | goto out; |
| 567 | } |
| 568 | printk(KERN_INFO "FILEIO: Referencing Size: %llu" |
| 569 | " bytes\n", fd_dev->fd_dev_size); |
| 570 | fd_dev->fbd_flags |= FBDF_HAS_SIZE; |
| 571 | break; |
| 572 | case Opt_fd_buffered_io: |
| 573 | match_int(args, &arg); |
| 574 | if (arg != 1) { |
| 575 | printk(KERN_ERR "bogus fd_buffered_io=%d value\n", arg); |
| 576 | ret = -EINVAL; |
| 577 | goto out; |
| 578 | } |
| 579 | |
| 580 | printk(KERN_INFO "FILEIO: Using buffered I/O" |
| 581 | " operations for struct fd_dev\n"); |
| 582 | |
| 583 | fd_dev->fbd_flags |= FDBD_USE_BUFFERED_IO; |
| 584 | break; |
| 585 | default: |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | out: |
| 591 | kfree(orig); |
| 592 | return (!ret) ? count : ret; |
| 593 | } |
| 594 | |
| 595 | static ssize_t fd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev) |
| 596 | { |
| 597 | struct fd_dev *fd_dev = (struct fd_dev *) se_dev->se_dev_su_ptr; |
| 598 | |
| 599 | if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) { |
| 600 | printk(KERN_ERR "Missing fd_dev_name=\n"); |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 601 | return -EINVAL; |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static ssize_t fd_show_configfs_dev_params( |
| 608 | struct se_hba *hba, |
| 609 | struct se_subsystem_dev *se_dev, |
| 610 | char *b) |
| 611 | { |
| 612 | struct fd_dev *fd_dev = se_dev->se_dev_su_ptr; |
| 613 | ssize_t bl = 0; |
| 614 | |
| 615 | bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id); |
| 616 | bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n", |
| 617 | fd_dev->fd_dev_name, fd_dev->fd_dev_size, |
| 618 | (fd_dev->fbd_flags & FDBD_USE_BUFFERED_IO) ? |
| 619 | "Buffered" : "Synchronous"); |
| 620 | return bl; |
| 621 | } |
| 622 | |
| 623 | /* fd_get_cdb(): (Part of se_subsystem_api_t template) |
| 624 | * |
| 625 | * |
| 626 | */ |
| 627 | static unsigned char *fd_get_cdb(struct se_task *task) |
| 628 | { |
| 629 | struct fd_request *req = FILE_REQ(task); |
| 630 | |
| 631 | return req->fd_scsi_cdb; |
| 632 | } |
| 633 | |
| 634 | /* fd_get_device_rev(): (Part of se_subsystem_api_t template) |
| 635 | * |
| 636 | * |
| 637 | */ |
| 638 | static u32 fd_get_device_rev(struct se_device *dev) |
| 639 | { |
| 640 | return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */ |
| 641 | } |
| 642 | |
| 643 | /* fd_get_device_type(): (Part of se_subsystem_api_t template) |
| 644 | * |
| 645 | * |
| 646 | */ |
| 647 | static u32 fd_get_device_type(struct se_device *dev) |
| 648 | { |
| 649 | return TYPE_DISK; |
| 650 | } |
| 651 | |
| 652 | static sector_t fd_get_blocks(struct se_device *dev) |
| 653 | { |
| 654 | struct fd_dev *fd_dev = dev->dev_ptr; |
| 655 | unsigned long long blocks_long = div_u64(fd_dev->fd_dev_size, |
Andy Grover | e3d6f90 | 2011-07-19 08:55:10 +0000 | [diff] [blame] | 656 | dev->se_sub_dev->se_dev_attrib.block_size); |
Nicholas Bellinger | c66ac9d | 2010-12-17 11:11:26 -0800 | [diff] [blame] | 657 | |
| 658 | return blocks_long; |
| 659 | } |
| 660 | |
| 661 | static struct se_subsystem_api fileio_template = { |
| 662 | .name = "fileio", |
| 663 | .owner = THIS_MODULE, |
| 664 | .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV, |
| 665 | .attach_hba = fd_attach_hba, |
| 666 | .detach_hba = fd_detach_hba, |
| 667 | .allocate_virtdevice = fd_allocate_virtdevice, |
| 668 | .create_virtdevice = fd_create_virtdevice, |
| 669 | .free_device = fd_free_device, |
| 670 | .dpo_emulated = fd_emulated_dpo, |
| 671 | .fua_write_emulated = fd_emulated_fua_write, |
| 672 | .fua_read_emulated = fd_emulated_fua_read, |
| 673 | .write_cache_emulated = fd_emulated_write_cache, |
| 674 | .alloc_task = fd_alloc_task, |
| 675 | .do_task = fd_do_task, |
| 676 | .do_sync_cache = fd_emulate_sync_cache, |
| 677 | .free_task = fd_free_task, |
| 678 | .check_configfs_dev_params = fd_check_configfs_dev_params, |
| 679 | .set_configfs_dev_params = fd_set_configfs_dev_params, |
| 680 | .show_configfs_dev_params = fd_show_configfs_dev_params, |
| 681 | .get_cdb = fd_get_cdb, |
| 682 | .get_device_rev = fd_get_device_rev, |
| 683 | .get_device_type = fd_get_device_type, |
| 684 | .get_blocks = fd_get_blocks, |
| 685 | }; |
| 686 | |
| 687 | static int __init fileio_module_init(void) |
| 688 | { |
| 689 | return transport_subsystem_register(&fileio_template); |
| 690 | } |
| 691 | |
| 692 | static void fileio_module_exit(void) |
| 693 | { |
| 694 | transport_subsystem_release(&fileio_template); |
| 695 | } |
| 696 | |
| 697 | MODULE_DESCRIPTION("TCM FILEIO subsystem plugin"); |
| 698 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); |
| 699 | MODULE_LICENSE("GPL"); |
| 700 | |
| 701 | module_init(fileio_module_init); |
| 702 | module_exit(fileio_module_exit); |