blob: 3ccef831a641906a71f4094f313f6d5e062f558a [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 Hellwigde103c92012-11-06 12:24:09 -0800312static sense_reason_t
313fd_execute_sync_cache(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315 struct se_device *dev = cmd->se_dev;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400316 struct fd_dev *fd_dev = FD_DEV(dev);
Andy Grovera1d8b492011-05-02 17:12:10 -0700317 int immed = (cmd->t_task_cdb[1] & 0x2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318 loff_t start, end;
319 int ret;
320
321 /*
322 * If the Immediate bit is set, queue up the GOOD response
323 * for this SYNCHRONIZE_CACHE op
324 */
325 if (immed)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400326 target_complete_cmd(cmd, SAM_STAT_GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800327
328 /*
329 * Determine if we will be flushing the entire device.
330 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700331 if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800332 start = 0;
333 end = LLONG_MAX;
334 } else {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400335 start = cmd->t_task_lba * dev->dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800336 if (cmd->data_length)
337 end = start + cmd->data_length;
338 else
339 end = LLONG_MAX;
340 }
341
342 ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
343 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700344 pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800345
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400346 if (immed)
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400347 return 0;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400348
Christoph Hellwigde103c92012-11-06 12:24:09 -0800349 if (ret)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400350 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800351 else
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400352 target_complete_cmd(cmd, SAM_STAT_GOOD);
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400353
354 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800355}
356
Christoph Hellwigde103c92012-11-06 12:24:09 -0800357static sense_reason_t
358fd_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
Christoph Hellwigde103c92012-11-06 12:24:09 -0800391 if (ret < 0)
392 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
393
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400394 if (ret)
395 target_complete_cmd(cmd, SAM_STAT_GOOD);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700396 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397}
398
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399enum {
400 Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
401};
402
403static match_table_t tokens = {
404 {Opt_fd_dev_name, "fd_dev_name=%s"},
405 {Opt_fd_dev_size, "fd_dev_size=%s"},
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700406 {Opt_fd_buffered_io, "fd_buffered_io=%d"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800407 {Opt_err, NULL}
408};
409
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400410static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
411 const char *page, ssize_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400413 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 char *orig, *ptr, *arg_p, *opts;
415 substring_t args[MAX_OPT_ARGS];
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700416 int ret = 0, arg, token;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800417
418 opts = kstrdup(page, GFP_KERNEL);
419 if (!opts)
420 return -ENOMEM;
421
422 orig = opts;
423
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +0100424 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800425 if (!*ptr)
426 continue;
427
428 token = match_token(ptr, tokens, args);
429 switch (token) {
430 case Opt_fd_dev_name:
Al Virodbc6e022012-08-01 13:07:04 +0400431 if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
432 FD_MAX_DEV_NAME) == 0) {
433 ret = -EINVAL;
Jesper Juhl6d180252011-03-14 04:05:56 -0700434 break;
435 }
Andy Grover6708bb22011-06-08 10:36:43 -0700436 pr_debug("FILEIO: Referencing Path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800437 fd_dev->fd_dev_name);
438 fd_dev->fbd_flags |= FBDF_HAS_PATH;
439 break;
440 case Opt_fd_dev_size:
441 arg_p = match_strdup(&args[0]);
Jesper Juhl6d180252011-03-14 04:05:56 -0700442 if (!arg_p) {
443 ret = -ENOMEM;
444 break;
445 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800446 ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
Jesper Juhl6d180252011-03-14 04:05:56 -0700447 kfree(arg_p);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800448 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700449 pr_err("strict_strtoull() failed for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 " fd_dev_size=\n");
451 goto out;
452 }
Andy Grover6708bb22011-06-08 10:36:43 -0700453 pr_debug("FILEIO: Referencing Size: %llu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800454 " bytes\n", fd_dev->fd_dev_size);
455 fd_dev->fbd_flags |= FBDF_HAS_SIZE;
456 break;
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700457 case Opt_fd_buffered_io:
458 match_int(args, &arg);
459 if (arg != 1) {
460 pr_err("bogus fd_buffered_io=%d value\n", arg);
461 ret = -EINVAL;
462 goto out;
463 }
464
465 pr_debug("FILEIO: Using buffered I/O"
466 " operations for struct fd_dev\n");
467
468 fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
469 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 default:
471 break;
472 }
473 }
474
475out:
476 kfree(orig);
477 return (!ret) ? count : ret;
478}
479
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400480static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800481{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400482 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 ssize_t bl = 0;
484
485 bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
Nicholas Bellingerb32f4c72012-09-29 17:15:37 -0700486 bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
487 fd_dev->fd_dev_name, fd_dev->fd_dev_size,
488 (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
489 "Buffered-WCE" : "O_DSYNC");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800490 return bl;
491}
492
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493static sector_t fd_get_blocks(struct se_device *dev)
494{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400495 struct fd_dev *fd_dev = FD_DEV(dev);
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700496 struct file *f = fd_dev->fd_file;
497 struct inode *i = f->f_mapping->host;
498 unsigned long long dev_size;
499 /*
500 * When using a file that references an underlying struct block_device,
501 * ensure dev_size is always based on the current inode size in order
502 * to handle underlying block_device resize operations.
503 */
504 if (S_ISBLK(i->i_mode))
505 dev_size = (i_size_read(i) - fd_dev->fd_block_size);
506 else
507 dev_size = fd_dev->fd_dev_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800508
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400509 return div_u64(dev_size, dev->dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800510}
511
Christoph Hellwig9e999a62012-10-07 10:55:50 -0400512static struct sbc_ops fd_sbc_ops = {
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400513 .execute_rw = fd_execute_rw,
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400514 .execute_sync_cache = fd_execute_sync_cache,
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400515};
516
Christoph Hellwigde103c92012-11-06 12:24:09 -0800517static sense_reason_t
518fd_parse_cdb(struct se_cmd *cmd)
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400519{
Christoph Hellwig9e999a62012-10-07 10:55:50 -0400520 return sbc_parse_cdb(cmd, &fd_sbc_ops);
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400521}
522
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800523static struct se_subsystem_api fileio_template = {
524 .name = "fileio",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400525 .inquiry_prod = "FILEIO",
526 .inquiry_rev = FD_VERSION,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800527 .owner = THIS_MODULE,
528 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
529 .attach_hba = fd_attach_hba,
530 .detach_hba = fd_detach_hba,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400531 .alloc_device = fd_alloc_device,
532 .configure_device = fd_configure_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800533 .free_device = fd_free_device,
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400534 .parse_cdb = fd_parse_cdb,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535 .set_configfs_dev_params = fd_set_configfs_dev_params,
536 .show_configfs_dev_params = fd_show_configfs_dev_params,
Christoph Hellwig6f23ac82012-10-07 10:55:53 -0400537 .get_device_type = sbc_get_device_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 .get_blocks = fd_get_blocks,
539};
540
541static int __init fileio_module_init(void)
542{
543 return transport_subsystem_register(&fileio_template);
544}
545
546static void fileio_module_exit(void)
547{
548 transport_subsystem_release(&fileio_template);
549}
550
551MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
552MODULE_AUTHOR("nab@Linux-iSCSI.org");
553MODULE_LICENSE("GPL");
554
555module_init(fileio_module_init);
556module_exit(fileio_module_exit);