1 /*******************************************************************************
2 * Filename: target_core_file.c
4 * This file contains the Storage Engine <-> FILEIO transport specific functions
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
11 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
27 ******************************************************************************/
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>
35 #include <linux/module.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_device.h>
41 #include <target/target_core_transport.h>
43 #include "target_core_file.h"
45 static struct se_subsystem_api fileio_template;
47 /* fd_attach_hba(): (Part of se_subsystem_api_t template)
51 static int fd_attach_hba(struct se_hba *hba, u32 host_id)
53 struct fd_host *fd_host;
55 fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
57 pr_err("Unable to allocate memory for struct fd_host\n");
61 fd_host->fd_host_id = host_id;
63 hba->hba_ptr = fd_host;
65 pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
66 " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
67 TARGET_CORE_MOD_VERSION);
68 pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
70 hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
75 static void fd_detach_hba(struct se_hba *hba)
77 struct fd_host *fd_host = hba->hba_ptr;
79 pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
80 " Target Core\n", hba->hba_id, fd_host->fd_host_id);
86 static void *fd_allocate_virtdevice(struct se_hba *hba, const char *name)
88 struct fd_dev *fd_dev;
89 struct fd_host *fd_host = (struct fd_host *) hba->hba_ptr;
91 fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
93 pr_err("Unable to allocate memory for struct fd_dev\n");
97 fd_dev->fd_host = fd_host;
99 pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
104 /* fd_create_virtdevice(): (Part of se_subsystem_api_t template)
108 static struct se_device *fd_create_virtdevice(
110 struct se_subsystem_dev *se_dev,
114 struct se_device *dev;
115 struct se_dev_limits dev_limits;
116 struct queue_limits *limits;
117 struct fd_dev *fd_dev = (struct fd_dev *) p;
118 struct fd_host *fd_host = (struct fd_host *) hba->hba_ptr;
121 struct inode *inode = NULL;
122 int dev_flags = 0, flags, ret = -EINVAL;
124 memset(&dev_limits, 0, sizeof(struct se_dev_limits));
128 dev_p = getname(fd_dev->fd_dev_name);
132 pr_err("getname(%s) failed: %lu\n",
133 fd_dev->fd_dev_name, IS_ERR(dev_p));
134 ret = PTR_ERR(dev_p);
138 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
139 * of pure timestamp updates.
141 flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
143 * Optionally allow fd_buffered_io=1 to be enabled for people
144 * who want use the fs buffer cache as an WriteCache mechanism.
146 * This means that in event of a hard failure, there is a risk
147 * of silent data-loss if the SCSI client has *not* performed a
148 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
149 * to write-out the entire device cache.
151 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
152 pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
156 file = filp_open(dev_p, flags, 0600);
158 pr_err("filp_open(%s) failed\n", dev_p);
162 if (!file || !file->f_dentry) {
163 pr_err("filp_open(%s) failed\n", dev_p);
166 fd_dev->fd_file = file;
168 * If using a block backend with this struct file, we extract
169 * fd_dev->fd_[block,dev]_size from struct block_device.
171 * Otherwise, we use the passed fd_size= from configfs
173 inode = file->f_mapping->host;
174 if (S_ISBLK(inode->i_mode)) {
175 struct request_queue *q;
176 unsigned long long dev_size;
178 * Setup the local scope queue_limits from struct request_queue->limits
179 * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
181 q = bdev_get_queue(inode->i_bdev);
182 limits = &dev_limits.limits;
183 limits->logical_block_size = bdev_logical_block_size(inode->i_bdev);
184 limits->max_hw_sectors = queue_max_hw_sectors(q);
185 limits->max_sectors = queue_max_sectors(q);
187 * Determine the number of bytes from i_size_read() minus
188 * one (1) logical sector from underlying struct block_device
190 fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
191 dev_size = (i_size_read(file->f_mapping->host) -
192 fd_dev->fd_block_size);
194 pr_debug("FILEIO: Using size: %llu bytes from struct"
195 " block_device blocks: %llu logical_block_size: %d\n",
196 dev_size, div_u64(dev_size, fd_dev->fd_block_size),
197 fd_dev->fd_block_size);
199 if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
200 pr_err("FILEIO: Missing fd_dev_size="
201 " parameter, and no backing struct"
206 limits = &dev_limits.limits;
207 limits->logical_block_size = FD_BLOCKSIZE;
208 limits->max_hw_sectors = FD_MAX_SECTORS;
209 limits->max_sectors = FD_MAX_SECTORS;
210 fd_dev->fd_block_size = FD_BLOCKSIZE;
213 dev_limits.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
214 dev_limits.queue_depth = FD_DEVICE_QUEUE_DEPTH;
216 dev = transport_add_device_to_core_hba(hba, &fileio_template,
217 se_dev, dev_flags, fd_dev,
218 &dev_limits, "FILEIO", FD_VERSION);
222 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
223 pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
224 " with FDBD_HAS_BUFFERED_IO_WCE\n");
225 dev->se_sub_dev->se_dev_attrib.emulate_write_cache = 1;
228 fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
229 fd_dev->fd_queue_depth = dev->queue_depth;
231 pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
232 " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
233 fd_dev->fd_dev_name, fd_dev->fd_dev_size);
238 if (fd_dev->fd_file) {
239 filp_close(fd_dev->fd_file, NULL);
240 fd_dev->fd_file = NULL;
246 /* fd_free_device(): (Part of se_subsystem_api_t template)
250 static void fd_free_device(void *p)
252 struct fd_dev *fd_dev = (struct fd_dev *) p;
254 if (fd_dev->fd_file) {
255 filp_close(fd_dev->fd_file, NULL);
256 fd_dev->fd_file = NULL;
262 static inline struct fd_request *FILE_REQ(struct se_task *task)
264 return container_of(task, struct fd_request, fd_task);
268 static struct se_task *
269 fd_alloc_task(unsigned char *cdb)
271 struct fd_request *fd_req;
273 fd_req = kzalloc(sizeof(struct fd_request), GFP_KERNEL);
275 pr_err("Unable to allocate struct fd_request\n");
279 return &fd_req->fd_task;
282 static int fd_do_readv(struct se_task *task)
284 struct fd_request *req = FILE_REQ(task);
285 struct se_device *se_dev = req->fd_task.task_se_cmd->se_dev;
286 struct fd_dev *dev = se_dev->dev_ptr;
287 struct file *fd = dev->fd_file;
288 struct scatterlist *sg = task->task_sg;
291 loff_t pos = (task->task_lba *
292 se_dev->se_sub_dev->se_dev_attrib.block_size);
295 iov = kzalloc(sizeof(struct iovec) * task->task_sg_nents, GFP_KERNEL);
297 pr_err("Unable to allocate fd_do_readv iov[]\n");
301 for_each_sg(task->task_sg, sg, task->task_sg_nents, i) {
302 iov[i].iov_len = sg->length;
303 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
308 ret = vfs_readv(fd, &iov[0], task->task_sg_nents, &pos);
311 for_each_sg(task->task_sg, sg, task->task_sg_nents, i)
315 * Return zeros and GOOD status even if the READ did not return
316 * the expected virt_size for struct file w/o a backing struct
319 if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
320 if (ret < 0 || ret != task->task_size) {
321 pr_err("vfs_readv() returned %d,"
322 " expecting %d for S_ISBLK\n", ret,
323 (int)task->task_size);
324 return (ret < 0 ? ret : -EINVAL);
328 pr_err("vfs_readv() returned %d for non"
337 static int fd_do_writev(struct se_task *task)
339 struct fd_request *req = FILE_REQ(task);
340 struct se_device *se_dev = req->fd_task.task_se_cmd->se_dev;
341 struct fd_dev *dev = se_dev->dev_ptr;
342 struct file *fd = dev->fd_file;
343 struct scatterlist *sg = task->task_sg;
346 loff_t pos = (task->task_lba *
347 se_dev->se_sub_dev->se_dev_attrib.block_size);
350 iov = kzalloc(sizeof(struct iovec) * task->task_sg_nents, GFP_KERNEL);
352 pr_err("Unable to allocate fd_do_writev iov[]\n");
356 for_each_sg(task->task_sg, sg, task->task_sg_nents, i) {
357 iov[i].iov_len = sg->length;
358 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
363 ret = vfs_writev(fd, &iov[0], task->task_sg_nents, &pos);
366 for_each_sg(task->task_sg, sg, task->task_sg_nents, i)
371 if (ret < 0 || ret != task->task_size) {
372 pr_err("vfs_writev() returned %d\n", ret);
373 return (ret < 0 ? ret : -EINVAL);
379 static void fd_emulate_sync_cache(struct se_task *task)
381 struct se_cmd *cmd = task->task_se_cmd;
382 struct se_device *dev = cmd->se_dev;
383 struct fd_dev *fd_dev = dev->dev_ptr;
384 int immed = (cmd->t_task_cdb[1] & 0x2);
389 * If the Immediate bit is set, queue up the GOOD response
390 * for this SYNCHRONIZE_CACHE op
393 transport_complete_sync_cache(cmd, 1);
396 * Determine if we will be flushing the entire device.
398 if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
402 start = cmd->t_task_lba * dev->se_sub_dev->se_dev_attrib.block_size;
403 if (cmd->data_length)
404 end = start + cmd->data_length;
409 ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
411 pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
414 transport_complete_sync_cache(cmd, ret == 0);
417 static int fd_do_task(struct se_task *task)
419 struct se_cmd *cmd = task->task_se_cmd;
420 struct se_device *dev = cmd->se_dev;
424 * Call vectorized fileio functions to map struct scatterlist
425 * physical memory addresses to struct iovec virtual memory.
427 if (task->task_data_direction == DMA_FROM_DEVICE) {
428 ret = fd_do_readv(task);
430 ret = fd_do_writev(task);
432 * Perform implict vfs_fsync_range() for fd_do_writev() ops
433 * for SCSI WRITEs with Forced Unit Access (FUA) set.
434 * Allow this to happen independent of WCE=0 setting.
437 dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
438 (cmd->se_cmd_flags & SCF_FUA)) {
439 struct fd_dev *fd_dev = dev->dev_ptr;
440 loff_t start = task->task_lba *
441 dev->se_sub_dev->se_dev_attrib.block_size;
442 loff_t end = start + task->task_size;
444 vfs_fsync_range(fd_dev->fd_file, start, end, 1);
449 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
453 task->task_scsi_status = GOOD;
454 transport_complete_task(task, 1);
459 /* fd_free_task(): (Part of se_subsystem_api_t template)
463 static void fd_free_task(struct se_task *task)
465 struct fd_request *req = FILE_REQ(task);
471 Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
474 static match_table_t tokens = {
475 {Opt_fd_dev_name, "fd_dev_name=%s"},
476 {Opt_fd_dev_size, "fd_dev_size=%s"},
477 {Opt_fd_buffered_io, "fd_buffered_io=%d"},
481 static ssize_t fd_set_configfs_dev_params(
483 struct se_subsystem_dev *se_dev,
484 const char *page, ssize_t count)
486 struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
487 char *orig, *ptr, *arg_p, *opts;
488 substring_t args[MAX_OPT_ARGS];
489 int ret = 0, arg, token;
491 opts = kstrdup(page, GFP_KERNEL);
497 while ((ptr = strsep(&opts, ",")) != NULL) {
501 token = match_token(ptr, tokens, args);
503 case Opt_fd_dev_name:
504 arg_p = match_strdup(&args[0]);
509 snprintf(fd_dev->fd_dev_name, FD_MAX_DEV_NAME,
512 pr_debug("FILEIO: Referencing Path: %s\n",
513 fd_dev->fd_dev_name);
514 fd_dev->fbd_flags |= FBDF_HAS_PATH;
516 case Opt_fd_dev_size:
517 arg_p = match_strdup(&args[0]);
522 ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
525 pr_err("strict_strtoull() failed for"
529 pr_debug("FILEIO: Referencing Size: %llu"
530 " bytes\n", fd_dev->fd_dev_size);
531 fd_dev->fbd_flags |= FBDF_HAS_SIZE;
533 case Opt_fd_buffered_io:
534 match_int(args, &arg);
536 pr_err("bogus fd_buffered_io=%d value\n", arg);
541 pr_debug("FILEIO: Using buffered I/O"
542 " operations for struct fd_dev\n");
544 fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
553 return (!ret) ? count : ret;
556 static ssize_t fd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
558 struct fd_dev *fd_dev = (struct fd_dev *) se_dev->se_dev_su_ptr;
560 if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
561 pr_err("Missing fd_dev_name=\n");
568 static ssize_t fd_show_configfs_dev_params(
570 struct se_subsystem_dev *se_dev,
573 struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
576 bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
577 bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
578 fd_dev->fd_dev_name, fd_dev->fd_dev_size,
579 (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
580 "Buffered-WCE" : "O_DSYNC");
584 /* fd_get_device_rev(): (Part of se_subsystem_api_t template)
588 static u32 fd_get_device_rev(struct se_device *dev)
590 return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
593 /* fd_get_device_type(): (Part of se_subsystem_api_t template)
597 static u32 fd_get_device_type(struct se_device *dev)
602 static sector_t fd_get_blocks(struct se_device *dev)
604 struct fd_dev *fd_dev = dev->dev_ptr;
605 struct file *f = fd_dev->fd_file;
606 struct inode *i = f->f_mapping->host;
607 unsigned long long dev_size;
609 * When using a file that references an underlying struct block_device,
610 * ensure dev_size is always based on the current inode size in order
611 * to handle underlying block_device resize operations.
613 if (S_ISBLK(i->i_mode))
614 dev_size = (i_size_read(i) - fd_dev->fd_block_size);
616 dev_size = fd_dev->fd_dev_size;
618 return div_u64(dev_size, dev->se_sub_dev->se_dev_attrib.block_size);
621 static struct se_subsystem_api fileio_template = {
623 .owner = THIS_MODULE,
624 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
625 .write_cache_emulated = 1,
626 .fua_write_emulated = 1,
627 .attach_hba = fd_attach_hba,
628 .detach_hba = fd_detach_hba,
629 .allocate_virtdevice = fd_allocate_virtdevice,
630 .create_virtdevice = fd_create_virtdevice,
631 .free_device = fd_free_device,
632 .alloc_task = fd_alloc_task,
633 .do_task = fd_do_task,
634 .do_sync_cache = fd_emulate_sync_cache,
635 .free_task = fd_free_task,
636 .check_configfs_dev_params = fd_check_configfs_dev_params,
637 .set_configfs_dev_params = fd_set_configfs_dev_params,
638 .show_configfs_dev_params = fd_show_configfs_dev_params,
639 .get_device_rev = fd_get_device_rev,
640 .get_device_type = fd_get_device_type,
641 .get_blocks = fd_get_blocks,
644 static int __init fileio_module_init(void)
646 return transport_subsystem_register(&fileio_template);
649 static void fileio_module_exit(void)
651 transport_subsystem_release(&fileio_template);
654 MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
655 MODULE_AUTHOR("nab@Linux-iSCSI.org");
656 MODULE_LICENSE("GPL");
658 module_init(fileio_module_init);
659 module_exit(fileio_module_exit);