blob: f84cbff83bcd4b09a038c841416b49bf5e2419a2 [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
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044static struct se_subsystem_api fileio_template;
45
46/* fd_attach_hba(): (Part of se_subsystem_api_t template)
47 *
48 *
49 */
50static int fd_attach_hba(struct se_hba *hba, u32 host_id)
51{
52 struct fd_host *fd_host;
53
54 fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -070055 if (!fd_host) {
56 pr_err("Unable to allocate memory for struct fd_host\n");
Andy Grovere3d6f902011-07-19 08:55:10 +000057 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080058 }
59
60 fd_host->fd_host_id = host_id;
61
Andy Grovere3d6f902011-07-19 08:55:10 +000062 hba->hba_ptr = fd_host;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063
Andy Grover6708bb22011-06-08 10:36:43 -070064 pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080065 " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
66 TARGET_CORE_MOD_VERSION);
Andy Grover6708bb22011-06-08 10:36:43 -070067 pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
Andy Grovere3d6f902011-07-19 08:55:10 +000068 " MaxSectors: %u\n",
69 hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080070
71 return 0;
72}
73
74static void fd_detach_hba(struct se_hba *hba)
75{
76 struct fd_host *fd_host = hba->hba_ptr;
77
Andy Grover6708bb22011-06-08 10:36:43 -070078 pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080079 " Target Core\n", hba->hba_id, fd_host->fd_host_id);
80
81 kfree(fd_host);
82 hba->hba_ptr = NULL;
83}
84
85static void *fd_allocate_virtdevice(struct se_hba *hba, const char *name)
86{
87 struct fd_dev *fd_dev;
Jörn Engel8359cf42011-11-24 02:05:51 +010088 struct fd_host *fd_host = hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089
90 fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -070091 if (!fd_dev) {
92 pr_err("Unable to allocate memory for struct fd_dev\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080093 return NULL;
94 }
95
96 fd_dev->fd_host = fd_host;
97
Andy Grover6708bb22011-06-08 10:36:43 -070098 pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080099
100 return fd_dev;
101}
102
103/* fd_create_virtdevice(): (Part of se_subsystem_api_t template)
104 *
105 *
106 */
107static struct se_device *fd_create_virtdevice(
108 struct se_hba *hba,
109 struct se_subsystem_dev *se_dev,
110 void *p)
111{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800112 struct se_device *dev;
113 struct se_dev_limits dev_limits;
114 struct queue_limits *limits;
Jörn Engel8359cf42011-11-24 02:05:51 +0100115 struct fd_dev *fd_dev = p;
116 struct fd_host *fd_host = hba->hba_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800117 struct file *file;
118 struct inode *inode = NULL;
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700119 int dev_flags = 0, flags, ret = -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120
121 memset(&dev_limits, 0, sizeof(struct se_dev_limits));
122
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800123 /*
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700124 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
125 * of pure timestamp updates.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800126 */
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700127 flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800128
Al Virodbc6e022012-08-01 13:07:04 +0400129 file = filp_open(fd_dev->fd_dev_name, flags, 0600);
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700130 if (IS_ERR(file)) {
Al Virodbc6e022012-08-01 13:07:04 +0400131 pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700132 ret = PTR_ERR(file);
133 goto fail;
134 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800135 fd_dev->fd_file = file;
136 /*
137 * If using a block backend with this struct file, we extract
138 * fd_dev->fd_[block,dev]_size from struct block_device.
139 *
140 * Otherwise, we use the passed fd_size= from configfs
141 */
142 inode = file->f_mapping->host;
143 if (S_ISBLK(inode->i_mode)) {
144 struct request_queue *q;
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700145 unsigned long long dev_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800146 /*
147 * Setup the local scope queue_limits from struct request_queue->limits
148 * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
149 */
150 q = bdev_get_queue(inode->i_bdev);
151 limits = &dev_limits.limits;
152 limits->logical_block_size = bdev_logical_block_size(inode->i_bdev);
153 limits->max_hw_sectors = queue_max_hw_sectors(q);
154 limits->max_sectors = queue_max_sectors(q);
155 /*
156 * Determine the number of bytes from i_size_read() minus
157 * one (1) logical sector from underlying struct block_device
158 */
159 fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700160 dev_size = (i_size_read(file->f_mapping->host) -
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161 fd_dev->fd_block_size);
162
Andy Grover6708bb22011-06-08 10:36:43 -0700163 pr_debug("FILEIO: Using size: %llu bytes from struct"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164 " block_device blocks: %llu logical_block_size: %d\n",
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700165 dev_size, div_u64(dev_size, fd_dev->fd_block_size),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166 fd_dev->fd_block_size);
167 } else {
168 if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700169 pr_err("FILEIO: Missing fd_dev_size="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 " parameter, and no backing struct"
171 " block_device\n");
172 goto fail;
173 }
174
175 limits = &dev_limits.limits;
176 limits->logical_block_size = FD_BLOCKSIZE;
177 limits->max_hw_sectors = FD_MAX_SECTORS;
178 limits->max_sectors = FD_MAX_SECTORS;
179 fd_dev->fd_block_size = FD_BLOCKSIZE;
180 }
181
182 dev_limits.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
183 dev_limits.queue_depth = FD_DEVICE_QUEUE_DEPTH;
184
185 dev = transport_add_device_to_core_hba(hba, &fileio_template,
Andy Grover5951146d2011-07-19 10:26:37 +0000186 se_dev, dev_flags, fd_dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800187 &dev_limits, "FILEIO", FD_VERSION);
Andy Grover6708bb22011-06-08 10:36:43 -0700188 if (!dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800189 goto fail;
190
191 fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
192 fd_dev->fd_queue_depth = dev->queue_depth;
193
Andy Grover6708bb22011-06-08 10:36:43 -0700194 pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800195 " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
196 fd_dev->fd_dev_name, fd_dev->fd_dev_size);
197
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800198 return dev;
199fail:
200 if (fd_dev->fd_file) {
201 filp_close(fd_dev->fd_file, NULL);
202 fd_dev->fd_file = NULL;
203 }
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700204 return ERR_PTR(ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800205}
206
207/* fd_free_device(): (Part of se_subsystem_api_t template)
208 *
209 *
210 */
211static void fd_free_device(void *p)
212{
Jörn Engel8359cf42011-11-24 02:05:51 +0100213 struct fd_dev *fd_dev = p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214
215 if (fd_dev->fd_file) {
216 filp_close(fd_dev->fd_file, NULL);
217 fd_dev->fd_file = NULL;
218 }
219
220 kfree(fd_dev);
221}
222
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400223static int fd_do_readv(struct se_cmd *cmd, struct scatterlist *sgl,
224 u32 sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800225{
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400226 struct se_device *se_dev = cmd->se_dev;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400227 struct fd_dev *dev = se_dev->dev_ptr;
Andy Grover6708bb22011-06-08 10:36:43 -0700228 struct file *fd = dev->fd_file;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400229 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800230 struct iovec *iov;
231 mm_segment_t old_fs;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400232 loff_t pos = (cmd->t_task_lba *
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400233 se_dev->se_sub_dev->se_dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800234 int ret = 0, i;
235
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400236 iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700237 if (!iov) {
238 pr_err("Unable to allocate fd_do_readv iov[]\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000239 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800240 }
241
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400242 for_each_sg(sgl, sg, sgl_nents, i) {
Sebastian Andrzej Siewior9649fa12011-11-28 12:33:10 +0100243 iov[i].iov_len = sg->length;
244 iov[i].iov_base = sg_virt(sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800245 }
246
247 old_fs = get_fs();
248 set_fs(get_ds());
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400249 ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800250 set_fs(old_fs);
251
252 kfree(iov);
253 /*
254 * Return zeros and GOOD status even if the READ did not return
255 * the expected virt_size for struct file w/o a backing struct
256 * block_device.
257 */
258 if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400259 if (ret < 0 || ret != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -0700260 pr_err("vfs_readv() returned %d,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800261 " expecting %d for S_ISBLK\n", ret,
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400262 (int)cmd->data_length);
Andy Grovere3d6f902011-07-19 08:55:10 +0000263 return (ret < 0 ? ret : -EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800264 }
265 } else {
266 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700267 pr_err("vfs_readv() returned %d for non"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800268 " S_ISBLK\n", ret);
Andy Grovere3d6f902011-07-19 08:55:10 +0000269 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800270 }
271 }
272
273 return 1;
274}
275
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400276static int fd_do_writev(struct se_cmd *cmd, struct scatterlist *sgl,
277 u32 sgl_nents)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278{
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400279 struct se_device *se_dev = cmd->se_dev;
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400280 struct fd_dev *dev = se_dev->dev_ptr;
Andy Grover6708bb22011-06-08 10:36:43 -0700281 struct file *fd = dev->fd_file;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400282 struct scatterlist *sg;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800283 struct iovec *iov;
284 mm_segment_t old_fs;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400285 loff_t pos = (cmd->t_task_lba *
Christoph Hellwig42bf8292011-10-12 11:07:00 -0400286 se_dev->se_sub_dev->se_dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 int ret, i = 0;
288
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400289 iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700290 if (!iov) {
291 pr_err("Unable to allocate fd_do_writev iov[]\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000292 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800293 }
294
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400295 for_each_sg(sgl, sg, sgl_nents, i) {
Sebastian Andrzej Siewior9649fa12011-11-28 12:33:10 +0100296 iov[i].iov_len = sg->length;
297 iov[i].iov_base = sg_virt(sg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 }
299
300 old_fs = get_fs();
301 set_fs(get_ds());
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400302 ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 set_fs(old_fs);
304
305 kfree(iov);
306
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400307 if (ret < 0 || ret != cmd->data_length) {
Andy Grover6708bb22011-06-08 10:36:43 -0700308 pr_err("vfs_writev() returned %d\n", ret);
Andy Grovere3d6f902011-07-19 08:55:10 +0000309 return (ret < 0 ? ret : -EINVAL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310 }
311
312 return 1;
313}
314
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400315static void fd_emulate_sync_cache(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800317 struct se_device *dev = cmd->se_dev;
318 struct fd_dev *fd_dev = dev->dev_ptr;
Andy Grovera1d8b492011-05-02 17:12:10 -0700319 int immed = (cmd->t_task_cdb[1] & 0x2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800320 loff_t start, end;
321 int ret;
322
323 /*
324 * If the Immediate bit is set, queue up the GOOD response
325 * for this SYNCHRONIZE_CACHE op
326 */
327 if (immed)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400328 target_complete_cmd(cmd, SAM_STAT_GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800329
330 /*
331 * Determine if we will be flushing the entire device.
332 */
Andy Grovera1d8b492011-05-02 17:12:10 -0700333 if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800334 start = 0;
335 end = LLONG_MAX;
336 } else {
Andy Grovera1d8b492011-05-02 17:12:10 -0700337 start = cmd->t_task_lba * dev->se_sub_dev->se_dev_attrib.block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 if (cmd->data_length)
339 end = start + cmd->data_length;
340 else
341 end = LLONG_MAX;
342 }
343
344 ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
345 if (ret != 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700346 pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400348 if (immed)
349 return;
350
351 if (ret) {
352 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
353 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
354 } else {
355 target_complete_cmd(cmd, SAM_STAT_GOOD);
356 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800357}
358
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400359static int fd_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
360 u32 sgl_nents, enum dma_data_direction data_direction)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800361{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800362 struct se_device *dev = cmd->se_dev;
363 int ret = 0;
364
365 /*
366 * Call vectorized fileio functions to map struct scatterlist
367 * physical memory addresses to struct iovec virtual memory.
368 */
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400369 if (data_direction == DMA_FROM_DEVICE) {
370 ret = fd_do_readv(cmd, sgl, sgl_nents);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 } else {
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400372 ret = fd_do_writev(cmd, sgl, sgl_nents);
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700373 /*
374 * Perform implict vfs_fsync_range() for fd_do_writev() ops
375 * for SCSI WRITEs with Forced Unit Access (FUA) set.
376 * Allow this to happen independent of WCE=0 setting.
377 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800378 if (ret > 0 &&
Andy Grovere3d6f902011-07-19 08:55:10 +0000379 dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
Christoph Hellwig2d3a4b52011-11-14 11:36:29 -0500380 (cmd->se_cmd_flags & SCF_FUA)) {
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700381 struct fd_dev *fd_dev = dev->dev_ptr;
382 loff_t start = cmd->t_task_lba *
383 dev->se_sub_dev->se_dev_attrib.block_size;
384 loff_t end = start + cmd->data_length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800385
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700386 vfs_fsync_range(fd_dev->fd_file, start, end, 1);
387 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800388 }
389
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700390 if (ret < 0) {
391 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 return ret;
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700393 }
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 Bellingerc66ac9d2010-12-17 11:11:26 -0800406 {Opt_err, NULL}
407};
408
409static ssize_t fd_set_configfs_dev_params(
410 struct se_hba *hba,
411 struct se_subsystem_dev *se_dev,
412 const char *page, ssize_t count)
413{
414 struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
415 char *orig, *ptr, *arg_p, *opts;
416 substring_t args[MAX_OPT_ARGS];
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700417 int ret = 0, 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 Bellingerc66ac9d2010-12-17 11:11:26 -0800458 default:
459 break;
460 }
461 }
462
463out:
464 kfree(orig);
465 return (!ret) ? count : ret;
466}
467
468static ssize_t fd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
469{
Jörn Engel8359cf42011-11-24 02:05:51 +0100470 struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471
472 if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700473 pr_err("Missing fd_dev_name=\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000474 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800475 }
476
477 return 0;
478}
479
480static ssize_t fd_show_configfs_dev_params(
481 struct se_hba *hba,
482 struct se_subsystem_dev *se_dev,
483 char *b)
484{
485 struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
486 ssize_t bl = 0;
487
488 bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
Nicholas Bellingera4dff302012-05-30 16:25:41 -0700489 bl += sprintf(b + bl, " File: %s Size: %llu Mode: O_DSYNC\n",
490 fd_dev->fd_dev_name, fd_dev->fd_dev_size);
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{
514 struct fd_dev *fd_dev = dev->dev_ptr;
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
Nicholas Bellingercd9323f2012-05-16 16:05:26 -0700528 return div_u64(dev_size, dev->se_sub_dev->se_dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529}
530
531static struct se_subsystem_api fileio_template = {
532 .name = "fileio",
533 .owner = THIS_MODULE,
534 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400535 .write_cache_emulated = 1,
536 .fua_write_emulated = 1,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537 .attach_hba = fd_attach_hba,
538 .detach_hba = fd_detach_hba,
539 .allocate_virtdevice = fd_allocate_virtdevice,
540 .create_virtdevice = fd_create_virtdevice,
541 .free_device = fd_free_device,
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400542 .execute_cmd = fd_execute_cmd,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543 .do_sync_cache = fd_emulate_sync_cache,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 .check_configfs_dev_params = fd_check_configfs_dev_params,
545 .set_configfs_dev_params = fd_set_configfs_dev_params,
546 .show_configfs_dev_params = fd_show_configfs_dev_params,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 .get_device_rev = fd_get_device_rev,
548 .get_device_type = fd_get_device_type,
549 .get_blocks = fd_get_blocks,
550};
551
552static int __init fileio_module_init(void)
553{
554 return transport_subsystem_register(&fileio_template);
555}
556
557static void fileio_module_exit(void)
558{
559 transport_subsystem_release(&fileio_template);
560}
561
562MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
563MODULE_AUTHOR("nab@Linux-iSCSI.org");
564MODULE_LICENSE("GPL");
565
566module_init(fileio_module_init);
567module_exit(fileio_module_exit);