blob: 571d3645f58aad3cc33013c9cee92aa8f5bd6c3a [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
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 Bellingerc66ac9d2010-12-17 11:11:26 -080029#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 Gortmaker827509e2011-08-30 14:20:44 -040035#include <linux/module.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38
39#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050040#include <target/target_core_backend.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041
42#include "target_core_file.h"
43
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040044static inline struct fd_dev *FD_DEV(struct se_device *dev)
45{
46 return container_of(dev, struct fd_dev, dev);
47}
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080048
49/* fd_attach_hba(): (Part of se_subsystem_api_t template)
50 *
51 *
52 */
53static int fd_attach_hba(struct se_hba *hba, u32 host_id)
54{
55 struct fd_host *fd_host;
56
57 fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -070058 if (!fd_host) {
59 pr_err("Unable to allocate memory for struct fd_host\n");
Andy Grovere3d6f902011-07-19 08:55:10 +000060 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061 }
62
63 fd_host->fd_host_id = host_id;
64
Andy Grovere3d6f902011-07-19 08:55:10 +000065 hba->hba_ptr = fd_host;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080066
Andy Grover6708bb22011-06-08 10:36:43 -070067 pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068 " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
69 TARGET_CORE_MOD_VERSION);
Andy Grover6708bb22011-06-08 10:36:43 -070070 pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
Andy Grovere3d6f902011-07-19 08:55:10 +000071 " MaxSectors: %u\n",
72 hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080073
74 return 0;
75}
76
77static void fd_detach_hba(struct se_hba *hba)
78{
79 struct fd_host *fd_host = hba->hba_ptr;
80
Andy Grover6708bb22011-06-08 10:36:43 -070081 pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080082 " Target Core\n", hba->hba_id, fd_host->fd_host_id);
83
84 kfree(fd_host);
85 hba->hba_ptr = NULL;
86}
87
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040088static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089{
90 struct fd_dev *fd_dev;
Jörn Engel8359cf42011-11-24 02:05:51 +010091 struct fd_host *fd_host = hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080092
93 fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -070094 if (!fd_dev) {
95 pr_err("Unable to allocate memory for struct fd_dev\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080096 return NULL;
97 }
98
99 fd_dev->fd_host = fd_host;
100
Andy Grover6708bb22011-06-08 10:36:43 -0700101 pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800102
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400103 return &fd_dev->dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104}
105
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400106static int fd_configure_device(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800107{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400108 struct fd_dev *fd_dev = FD_DEV(dev);
109 struct fd_host *fd_host = dev->se_hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800110 struct file *file;
111 struct inode *inode = NULL;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400112 int flags, ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800113
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400114 if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
115 pr_err("Missing fd_dev_name=\n");
116 return -EINVAL;
117 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800118
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 /*
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700120 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
121 * of pure timestamp updates.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 */
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700123 flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400124
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700125 /*
126 * Optionally allow fd_buffered_io=1 to be enabled for people
127 * who want use the fs buffer cache as an WriteCache mechanism.
128 *
129 * This means that in event of a hard failure, there is a risk
130 * of silent data-loss if the SCSI client has *not* performed a
131 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
132 * to write-out the entire device cache.
133 */
134 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
135 pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
136 flags &= ~O_DSYNC;
137 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138
Al Virodbc6e022012-08-01 13:07:04 +0400139 file = filp_open(fd_dev->fd_dev_name, flags, 0600);
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700140 if (IS_ERR(file)) {
Al Virodbc6e022012-08-01 13:07:04 +0400141 pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700142 ret = PTR_ERR(file);
143 goto fail;
144 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800145 fd_dev->fd_file = file;
146 /*
147 * If using a block backend with this struct file, we extract
148 * fd_dev->fd_[block,dev]_size from struct block_device.
149 *
150 * Otherwise, we use the passed fd_size= from configfs
151 */
152 inode = file->f_mapping->host;
153 if (S_ISBLK(inode->i_mode)) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400154 struct request_queue *q = bdev_get_queue(inode->i_bdev);
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700155 unsigned long long dev_size;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400156
157 dev->dev_attrib.hw_block_size =
158 bdev_logical_block_size(inode->i_bdev);
159 dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
160
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161 /*
162 * Determine the number of bytes from i_size_read() minus
163 * one (1) logical sector from underlying struct block_device
164 */
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700165 dev_size = (i_size_read(file->f_mapping->host) -
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166 fd_dev->fd_block_size);
167
Andy Grover6708bb22011-06-08 10:36:43 -0700168 pr_debug("FILEIO: Using size: %llu bytes from struct"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 " block_device blocks: %llu logical_block_size: %d\n",
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700170 dev_size, div_u64(dev_size, fd_dev->fd_block_size),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800171 fd_dev->fd_block_size);
172 } else {
173 if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700174 pr_err("FILEIO: Missing fd_dev_size="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800175 " parameter, and no backing struct"
176 " block_device\n");
177 goto fail;
178 }
179
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400180 dev->dev_attrib.hw_block_size = FD_BLOCKSIZE;
181 dev->dev_attrib.hw_max_sectors = FD_MAX_SECTORS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800182 }
183
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400184 fd_dev->fd_block_size = dev->dev_attrib.hw_block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800185
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400186 dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800187
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700188 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
189 pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
190 " with FDBD_HAS_BUFFERED_IO_WCE\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400191 dev->dev_attrib.emulate_write_cache = 1;
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700192 }
193
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800194 fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
195 fd_dev->fd_queue_depth = dev->queue_depth;
196
Andy Grover6708bb22011-06-08 10:36:43 -0700197 pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800198 " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
199 fd_dev->fd_dev_name, fd_dev->fd_dev_size);
200
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400201 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800202fail:
203 if (fd_dev->fd_file) {
204 filp_close(fd_dev->fd_file, NULL);
205 fd_dev->fd_file = NULL;
206 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400207 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208}
209
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400210static void fd_free_device(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800211{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400212 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213
214 if (fd_dev->fd_file) {
215 filp_close(fd_dev->fd_file, NULL);
216 fd_dev->fd_file = NULL;
217 }
218
219 kfree(fd_dev);
220}
221
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400222static int fd_do_readv(struct se_cmd *cmd, struct scatterlist *sgl,
223 u32 sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800224{
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400225 struct se_device *se_dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400226 struct fd_dev *dev = FD_DEV(se_dev);
Andy Grover6708bb22011-06-08 10:36:43 -0700227 struct file *fd = dev->fd_file;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400228 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 struct iovec *iov;
230 mm_segment_t old_fs;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400231 loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232 int ret = 0, i;
233
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400234 iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700235 if (!iov) {
236 pr_err("Unable to allocate fd_do_readv iov[]\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000237 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238 }
239
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400240 for_each_sg(sgl, sg, sgl_nents, i) {
Sebastian Andrzej Siewior9649fa12011-11-28 12:33:10 +0100241 iov[i].iov_len = sg->length;
242 iov[i].iov_base = sg_virt(sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243 }
244
245 old_fs = get_fs();
246 set_fs(get_ds());
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400247 ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800248 set_fs(old_fs);
249
250 kfree(iov);
251 /*
252 * Return zeros and GOOD status even if the READ did not return
253 * the expected virt_size for struct file w/o a backing struct
254 * block_device.
255 */
256 if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400257 if (ret < 0 || ret != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -0700258 pr_err("vfs_readv() returned %d,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 " expecting %d for S_ISBLK\n", ret,
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400260 (int)cmd->data_length);
Andy Grovere3d6f902011-07-19 08:55:10 +0000261 return (ret < 0 ? ret : -EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800262 }
263 } else {
264 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700265 pr_err("vfs_readv() returned %d for non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800266 " S_ISBLK\n", ret);
Andy Grovere3d6f902011-07-19 08:55:10 +0000267 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800268 }
269 }
270
271 return 1;
272}
273
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400274static int fd_do_writev(struct se_cmd *cmd, struct scatterlist *sgl,
275 u32 sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276{
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400277 struct se_device *se_dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400278 struct fd_dev *dev = FD_DEV(se_dev);
Andy Grover6708bb22011-06-08 10:36:43 -0700279 struct file *fd = dev->fd_file;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400280 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281 struct iovec *iov;
282 mm_segment_t old_fs;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400283 loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 int ret, i = 0;
285
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400286 iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700287 if (!iov) {
288 pr_err("Unable to allocate fd_do_writev iov[]\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000289 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290 }
291
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400292 for_each_sg(sgl, sg, sgl_nents, i) {
Sebastian Andrzej Siewior9649fa12011-11-28 12:33:10 +0100293 iov[i].iov_len = sg->length;
294 iov[i].iov_base = sg_virt(sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 }
296
297 old_fs = get_fs();
298 set_fs(get_ds());
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400299 ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800300 set_fs(old_fs);
301
302 kfree(iov);
303
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400304 if (ret < 0 || ret != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -0700305 pr_err("vfs_writev() returned %d\n", ret);
Andy Grovere3d6f902011-07-19 08:55:10 +0000306 return (ret < 0 ? ret : -EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800307 }
308
309 return 1;
310}
311
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400312static int fd_execute_sync_cache(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400315 struct fd_dev *fd_dev = FD_DEV(dev);
Andy Grovera1d8b492011-05-02 17:12:10 -0700316 int immed = (cmd->t_task_cdb[1] & 0x2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800317 loff_t start, end;
318 int ret;
319
320 /*
321 * If the Immediate bit is set, queue up the GOOD response
322 * for this SYNCHRONIZE_CACHE op
323 */
324 if (immed)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400325 target_complete_cmd(cmd, SAM_STAT_GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326
327 /*
328 * Determine if we will be flushing the entire device.
329 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700330 if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800331 start = 0;
332 end = LLONG_MAX;
333 } else {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400334 start = cmd->t_task_lba * dev->dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800335 if (cmd->data_length)
336 end = start + cmd->data_length;
337 else
338 end = LLONG_MAX;
339 }
340
341 ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
342 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700343 pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800344
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400345 if (immed)
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400346 return 0;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400347
348 if (ret) {
349 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
350 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
351 } else {
352 target_complete_cmd(cmd, SAM_STAT_GOOD);
353 }
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400354
355 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800356}
357
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400358static int fd_execute_rw(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800359{
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400360 struct scatterlist *sgl = cmd->t_data_sg;
361 u32 sgl_nents = cmd->t_data_nents;
362 enum dma_data_direction data_direction = cmd->data_direction;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800363 struct se_device *dev = cmd->se_dev;
364 int ret = 0;
365
366 /*
367 * Call vectorized fileio functions to map struct scatterlist
368 * physical memory addresses to struct iovec virtual memory.
369 */
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400370 if (data_direction == DMA_FROM_DEVICE) {
371 ret = fd_do_readv(cmd, sgl, sgl_nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 } else {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400373 ret = fd_do_writev(cmd, sgl, sgl_nents);
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700374 /*
375 * Perform implict vfs_fsync_range() for fd_do_writev() ops
376 * for SCSI WRITEs with Forced Unit Access (FUA) set.
377 * Allow this to happen independent of WCE=0 setting.
378 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 if (ret > 0 &&
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400380 dev->dev_attrib.emulate_fua_write > 0 &&
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -0500381 (cmd->se_cmd_flags & SCF_FUA)) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400382 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700383 loff_t start = cmd->t_task_lba *
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400384 dev->dev_attrib.block_size;
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700385 loff_t end = start + cmd->data_length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700387 vfs_fsync_range(fd_dev->fd_file, start, end, 1);
388 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389 }
390
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700391 if (ret < 0) {
392 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800393 return ret;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700394 }
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400395 if (ret)
396 target_complete_cmd(cmd, SAM_STAT_GOOD);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700397 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398}
399
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400enum {
401 Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
402};
403
404static match_table_t tokens = {
405 {Opt_fd_dev_name, "fd_dev_name=%s"},
406 {Opt_fd_dev_size, "fd_dev_size=%s"},
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700407 {Opt_fd_buffered_io, "fd_buffered_io=%d"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800408 {Opt_err, NULL}
409};
410
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400411static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
412 const char *page, ssize_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800413{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400414 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 char *orig, *ptr, *arg_p, *opts;
416 substring_t args[MAX_OPT_ARGS];
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700417 int ret = 0, arg, token;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800418
419 opts = kstrdup(page, GFP_KERNEL);
420 if (!opts)
421 return -ENOMEM;
422
423 orig = opts;
424
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +0100425 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800426 if (!*ptr)
427 continue;
428
429 token = match_token(ptr, tokens, args);
430 switch (token) {
431 case Opt_fd_dev_name:
Al Virodbc6e022012-08-01 13:07:04 +0400432 if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
433 FD_MAX_DEV_NAME) == 0) {
434 ret = -EINVAL;
Jesper Juhl6d180252011-03-14 04:05:56 -0700435 break;
436 }
Andy Grover6708bb22011-06-08 10:36:43 -0700437 pr_debug("FILEIO: Referencing Path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438 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 Juhl6d180252011-03-14 04:05:56 -0700443 if (!arg_p) {
444 ret = -ENOMEM;
445 break;
446 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447 ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
Jesper Juhl6d180252011-03-14 04:05:56 -0700448 kfree(arg_p);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700450 pr_err("strict_strtoull() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800451 " fd_dev_size=\n");
452 goto out;
453 }
Andy Grover6708bb22011-06-08 10:36:43 -0700454 pr_debug("FILEIO: Referencing Size: %llu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 " bytes\n", fd_dev->fd_dev_size);
456 fd_dev->fbd_flags |= FBDF_HAS_SIZE;
457 break;
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700458 case Opt_fd_buffered_io:
459 match_int(args, &arg);
460 if (arg != 1) {
461 pr_err("bogus fd_buffered_io=%d value\n", arg);
462 ret = -EINVAL;
463 goto out;
464 }
465
466 pr_debug("FILEIO: Using buffered I/O"
467 " operations for struct fd_dev\n");
468
469 fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
470 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471 default:
472 break;
473 }
474 }
475
476out:
477 kfree(orig);
478 return (!ret) ? count : ret;
479}
480
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400481static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800482{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400483 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484 ssize_t bl = 0;
485
486 bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700487 bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
488 fd_dev->fd_dev_name, fd_dev->fd_dev_size,
489 (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
490 "Buffered-WCE" : "O_DSYNC");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491 return bl;
492}
493
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800494/* fd_get_device_rev(): (Part of se_subsystem_api_t template)
495 *
496 *
497 */
498static 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 */
507static u32 fd_get_device_type(struct se_device *dev)
508{
509 return TYPE_DISK;
510}
511
512static sector_t fd_get_blocks(struct se_device *dev)
513{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400514 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700515 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 Bellingerc66ac9d2010-12-17 11:11:26 -0800527
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400528 return div_u64(dev_size, dev->dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529}
530
Christoph Hellwig9e999a62012-10-07 10:55:50 -0400531static struct sbc_ops fd_sbc_ops = {
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400532 .execute_rw = fd_execute_rw,
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400533 .execute_sync_cache = fd_execute_sync_cache,
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400534};
535
536static int fd_parse_cdb(struct se_cmd *cmd)
537{
Christoph Hellwig9e999a62012-10-07 10:55:50 -0400538 return sbc_parse_cdb(cmd, &fd_sbc_ops);
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400539}
540
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541static struct se_subsystem_api fileio_template = {
542 .name = "fileio",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400543 .inquiry_prod = "FILEIO",
544 .inquiry_rev = FD_VERSION,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545 .owner = THIS_MODULE,
546 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
547 .attach_hba = fd_attach_hba,
548 .detach_hba = fd_detach_hba,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400549 .alloc_device = fd_alloc_device,
550 .configure_device = fd_configure_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551 .free_device = fd_free_device,
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400552 .parse_cdb = fd_parse_cdb,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800553 .set_configfs_dev_params = fd_set_configfs_dev_params,
554 .show_configfs_dev_params = fd_show_configfs_dev_params,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555 .get_device_rev = fd_get_device_rev,
556 .get_device_type = fd_get_device_type,
557 .get_blocks = fd_get_blocks,
558};
559
560static int __init fileio_module_init(void)
561{
562 return transport_subsystem_register(&fileio_template);
563}
564
565static void fileio_module_exit(void)
566{
567 transport_subsystem_release(&fileio_template);
568}
569
570MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
571MODULE_AUTHOR("nab@Linux-iSCSI.org");
572MODULE_LICENSE("GPL");
573
574module_init(fileio_module_init);
575module_exit(fileio_module_exit);