target_core_file.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*******************************************************************************
 * Filename:  target_core_file.c
 *
 * This file contains the Storage Engine <-> FILEIO transport specific functions
 *
 * Copyright (c) 2005 PyX Technologies, Inc.
 * Copyright (c) 2005-2006 SBE, Inc.  All Rights Reserved.
 * Copyright (c) 2007-2010 Rising Tide Systems
 * Copyright (c) 2008-2010 Linux-iSCSI.org
 *
 * Nicholas A. Bellinger <nab@kernel.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ******************************************************************************/

#include <linux/string.h>
#include <linux/parser.h>
#include <linux/timer.h>
#include <linux/blkdev.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
35
#include <linux/module.h>
36 37 38 39
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>

#include <target/target_core_base.h>
40
#include <target/target_core_backend.h>
41 42 43

#include "target_core_file.h"

44 45 46 47
static inline struct fd_dev *FD_DEV(struct se_device *dev)
{
	return container_of(dev, struct fd_dev, dev);
}
48 49 50 51 52 53 54 55 56 57

/*	fd_attach_hba(): (Part of se_subsystem_api_t template)
 *
 *
 */
static int fd_attach_hba(struct se_hba *hba, u32 host_id)
{
	struct fd_host *fd_host;

	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
58 59
	if (!fd_host) {
		pr_err("Unable to allocate memory for struct fd_host\n");
60
		return -ENOMEM;
61 62 63 64
	}

	fd_host->fd_host_id = host_id;

65
	hba->hba_ptr = fd_host;
66

67
	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
68 69
		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
		TARGET_CORE_MOD_VERSION);
70
	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
71 72
		" MaxSectors: %u\n",
		hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
73 74 75 76 77 78 79 80

	return 0;
}

static void fd_detach_hba(struct se_hba *hba)
{
	struct fd_host *fd_host = hba->hba_ptr;

81
	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
82 83 84 85 86 87
		" Target Core\n", hba->hba_id, fd_host->fd_host_id);

	kfree(fd_host);
	hba->hba_ptr = NULL;
}

88
static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
89 90
{
	struct fd_dev *fd_dev;
J
Jörn Engel 已提交
91
	struct fd_host *fd_host = hba->hba_ptr;
92 93

	fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
94 95
	if (!fd_dev) {
		pr_err("Unable to allocate memory for struct fd_dev\n");
96 97 98 99 100
		return NULL;
	}

	fd_dev->fd_host = fd_host;

101
	pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
102

103
	return &fd_dev->dev;
104 105
}

106
static int fd_configure_device(struct se_device *dev)
107
{
108 109
	struct fd_dev *fd_dev = FD_DEV(dev);
	struct fd_host *fd_host = dev->se_hba->hba_ptr;
110 111
	struct file *file;
	struct inode *inode = NULL;
112
	int flags, ret = -EINVAL;
113

114 115 116 117
	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
		pr_err("Missing fd_dev_name=\n");
		return -EINVAL;
	}
118 119

	/*
120 121
	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
	 * of pure timestamp updates.
122
	 */
123
	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
124

125 126 127 128 129 130 131 132 133 134 135 136 137
	/*
	 * Optionally allow fd_buffered_io=1 to be enabled for people
	 * who want use the fs buffer cache as an WriteCache mechanism.
	 *
	 * This means that in event of a hard failure, there is a risk
	 * of silent data-loss if the SCSI client has *not* performed a
	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
	 * to write-out the entire device cache.
	 */
	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
		flags &= ~O_DSYNC;
	}
138

A
Al Viro 已提交
139
	file = filp_open(fd_dev->fd_dev_name, flags, 0600);
140
	if (IS_ERR(file)) {
A
Al Viro 已提交
141
		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
142 143 144
		ret = PTR_ERR(file);
		goto fail;
	}
145 146 147 148 149 150 151 152 153
	fd_dev->fd_file = file;
	/*
	 * If using a block backend with this struct file, we extract
	 * fd_dev->fd_[block,dev]_size from struct block_device.
	 *
	 * Otherwise, we use the passed fd_size= from configfs
	 */
	inode = file->f_mapping->host;
	if (S_ISBLK(inode->i_mode)) {
154
		struct request_queue *q = bdev_get_queue(inode->i_bdev);
155
		unsigned long long dev_size;
156 157 158 159 160

		dev->dev_attrib.hw_block_size =
			bdev_logical_block_size(inode->i_bdev);
		dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);

161 162 163 164
		/*
		 * Determine the number of bytes from i_size_read() minus
		 * one (1) logical sector from underlying struct block_device
		 */
165
		dev_size = (i_size_read(file->f_mapping->host) -
166 167
				       fd_dev->fd_block_size);

168
		pr_debug("FILEIO: Using size: %llu bytes from struct"
169
			" block_device blocks: %llu logical_block_size: %d\n",
170
			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
171 172 173
			fd_dev->fd_block_size);
	} else {
		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
174
			pr_err("FILEIO: Missing fd_dev_size="
175 176 177 178 179
				" parameter, and no backing struct"
				" block_device\n");
			goto fail;
		}

180 181
		dev->dev_attrib.hw_block_size = FD_BLOCKSIZE;
		dev->dev_attrib.hw_max_sectors = FD_MAX_SECTORS;
182 183
	}

184
	fd_dev->fd_block_size = dev->dev_attrib.hw_block_size;
185

186
	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
187

188 189 190
	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
			" with FDBD_HAS_BUFFERED_IO_WCE\n");
191
		dev->dev_attrib.emulate_write_cache = 1;
192 193
	}

194 195 196
	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
	fd_dev->fd_queue_depth = dev->queue_depth;

197
	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
198 199 200
		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
			fd_dev->fd_dev_name, fd_dev->fd_dev_size);

201
	return 0;
202 203 204 205 206
fail:
	if (fd_dev->fd_file) {
		filp_close(fd_dev->fd_file, NULL);
		fd_dev->fd_file = NULL;
	}
207
	return ret;
208 209
}

210
static void fd_free_device(struct se_device *dev)
211
{
212
	struct fd_dev *fd_dev = FD_DEV(dev);
213 214 215 216 217 218 219 220 221

	if (fd_dev->fd_file) {
		filp_close(fd_dev->fd_file, NULL);
		fd_dev->fd_file = NULL;
	}

	kfree(fd_dev);
}

222 223
static int fd_do_readv(struct se_cmd *cmd, struct scatterlist *sgl,
		u32 sgl_nents)
224
{
225
	struct se_device *se_dev = cmd->se_dev;
226
	struct fd_dev *dev = FD_DEV(se_dev);
227
	struct file *fd = dev->fd_file;
228
	struct scatterlist *sg;
229 230
	struct iovec *iov;
	mm_segment_t old_fs;
231
	loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
232 233
	int ret = 0, i;

234
	iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
235 236
	if (!iov) {
		pr_err("Unable to allocate fd_do_readv iov[]\n");
237
		return -ENOMEM;
238 239
	}

240
	for_each_sg(sgl, sg, sgl_nents, i) {
241 242
		iov[i].iov_len = sg->length;
		iov[i].iov_base = sg_virt(sg);
243 244 245 246
	}

	old_fs = get_fs();
	set_fs(get_ds());
247
	ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
248 249 250 251 252 253 254 255 256
	set_fs(old_fs);

	kfree(iov);
	/*
	 * Return zeros and GOOD status even if the READ did not return
	 * the expected virt_size for struct file w/o a backing struct
	 * block_device.
	 */
	if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
257
		if (ret < 0 || ret != cmd->data_length) {
258
			pr_err("vfs_readv() returned %d,"
259
				" expecting %d for S_ISBLK\n", ret,
260
				(int)cmd->data_length);
261
			return (ret < 0 ? ret : -EINVAL);
262 263 264
		}
	} else {
		if (ret < 0) {
265
			pr_err("vfs_readv() returned %d for non"
266
				" S_ISBLK\n", ret);
267
			return ret;
268 269 270 271 272 273
		}
	}

	return 1;
}

274 275
static int fd_do_writev(struct se_cmd *cmd, struct scatterlist *sgl,
		u32 sgl_nents)
276
{
277
	struct se_device *se_dev = cmd->se_dev;
278
	struct fd_dev *dev = FD_DEV(se_dev);
279
	struct file *fd = dev->fd_file;
280
	struct scatterlist *sg;
281 282
	struct iovec *iov;
	mm_segment_t old_fs;
283
	loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
284 285
	int ret, i = 0;

286
	iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
287 288
	if (!iov) {
		pr_err("Unable to allocate fd_do_writev iov[]\n");
289
		return -ENOMEM;
290 291
	}

292
	for_each_sg(sgl, sg, sgl_nents, i) {
293 294
		iov[i].iov_len = sg->length;
		iov[i].iov_base = sg_virt(sg);
295 296 297 298
	}

	old_fs = get_fs();
	set_fs(get_ds());
299
	ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
300 301 302 303
	set_fs(old_fs);

	kfree(iov);

304
	if (ret < 0 || ret != cmd->data_length) {
305
		pr_err("vfs_writev() returned %d\n", ret);
306
		return (ret < 0 ? ret : -EINVAL);
307 308 309 310 311
	}

	return 1;
}

312
static int fd_execute_sync_cache(struct se_cmd *cmd)
313 314
{
	struct se_device *dev = cmd->se_dev;
315
	struct fd_dev *fd_dev = FD_DEV(dev);
316
	int immed = (cmd->t_task_cdb[1] & 0x2);
317 318 319 320 321 322 323 324
	loff_t start, end;
	int ret;

	/*
	 * If the Immediate bit is set, queue up the GOOD response
	 * for this SYNCHRONIZE_CACHE op
	 */
	if (immed)
325
		target_complete_cmd(cmd, SAM_STAT_GOOD);
326 327 328 329

	/*
	 * Determine if we will be flushing the entire device.
	 */
330
	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
331 332 333
		start = 0;
		end = LLONG_MAX;
	} else {
334
		start = cmd->t_task_lba * dev->dev_attrib.block_size;
335 336 337 338 339 340 341 342
		if (cmd->data_length)
			end = start + cmd->data_length;
		else
			end = LLONG_MAX;
	}

	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
	if (ret != 0)
343
		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
344

345
	if (immed)
346
		return 0;
347 348 349 350 351 352 353

	if (ret) {
		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
	} else {
		target_complete_cmd(cmd, SAM_STAT_GOOD);
	}
354 355

	return 0;
356 357
}

358
static int fd_execute_rw(struct se_cmd *cmd)
359
{
360 361 362
	struct scatterlist *sgl = cmd->t_data_sg;
	u32 sgl_nents = cmd->t_data_nents;
	enum dma_data_direction data_direction = cmd->data_direction;
363 364 365 366 367 368 369
	struct se_device *dev = cmd->se_dev;
	int ret = 0;

	/*
	 * Call vectorized fileio functions to map struct scatterlist
	 * physical memory addresses to struct iovec virtual memory.
	 */
370 371
	if (data_direction == DMA_FROM_DEVICE) {
		ret = fd_do_readv(cmd, sgl, sgl_nents);
372
	} else {
373
		ret = fd_do_writev(cmd, sgl, sgl_nents);
374 375 376 377 378
		/*
		 * Perform implict vfs_fsync_range() for fd_do_writev() ops
		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
		 * Allow this to happen independent of WCE=0 setting.
		 */
379
		if (ret > 0 &&
380
		    dev->dev_attrib.emulate_fua_write > 0 &&
381
		    (cmd->se_cmd_flags & SCF_FUA)) {
382
			struct fd_dev *fd_dev = FD_DEV(dev);
383
			loff_t start = cmd->t_task_lba *
384
				dev->dev_attrib.block_size;
385
			loff_t end = start + cmd->data_length;
386

387 388
			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
		}
389 390
	}

391 392
	if (ret < 0) {
		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
393
		return ret;
394
	}
395 396
	if (ret)
		target_complete_cmd(cmd, SAM_STAT_GOOD);
397
	return 0;
398 399 400 401 402 403 404 405 406
}

enum {
	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
};

static match_table_t tokens = {
	{Opt_fd_dev_name, "fd_dev_name=%s"},
	{Opt_fd_dev_size, "fd_dev_size=%s"},
407
	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
408 409 410
	{Opt_err, NULL}
};

411 412
static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
		const char *page, ssize_t count)
413
{
414
	struct fd_dev *fd_dev = FD_DEV(dev);
415 416
	char *orig, *ptr, *arg_p, *opts;
	substring_t args[MAX_OPT_ARGS];
417
	int ret = 0, arg, token;
418 419 420 421 422 423 424

	opts = kstrdup(page, GFP_KERNEL);
	if (!opts)
		return -ENOMEM;

	orig = opts;

425
	while ((ptr = strsep(&opts, ",\n")) != NULL) {
426 427 428 429 430 431
		if (!*ptr)
			continue;

		token = match_token(ptr, tokens, args);
		switch (token) {
		case Opt_fd_dev_name:
A
Al Viro 已提交
432 433 434
			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
				FD_MAX_DEV_NAME) == 0) {
				ret = -EINVAL;
435 436
				break;
			}
437
			pr_debug("FILEIO: Referencing Path: %s\n",
438 439 440 441 442
					fd_dev->fd_dev_name);
			fd_dev->fbd_flags |= FBDF_HAS_PATH;
			break;
		case Opt_fd_dev_size:
			arg_p = match_strdup(&args[0]);
443 444 445 446
			if (!arg_p) {
				ret = -ENOMEM;
				break;
			}
447
			ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
448
			kfree(arg_p);
449
			if (ret < 0) {
450
				pr_err("strict_strtoull() failed for"
451 452 453
						" fd_dev_size=\n");
				goto out;
			}
454
			pr_debug("FILEIO: Referencing Size: %llu"
455 456 457
					" bytes\n", fd_dev->fd_dev_size);
			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
			break;
458 459 460 461 462 463 464 465 466 467 468 469 470
		case Opt_fd_buffered_io:
			match_int(args, &arg);
			if (arg != 1) {
				pr_err("bogus fd_buffered_io=%d value\n", arg);
				ret = -EINVAL;
				goto out;
			}

			pr_debug("FILEIO: Using buffered I/O"
				" operations for struct fd_dev\n");

			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
			break;
471 472 473 474 475 476 477 478 479 480
		default:
			break;
		}
	}

out:
	kfree(orig);
	return (!ret) ? count : ret;
}

481
static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
482
{
483
	struct fd_dev *fd_dev = FD_DEV(dev);
484 485 486
	ssize_t bl = 0;

	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
487 488 489 490
	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s\n",
		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
		"Buffered-WCE" : "O_DSYNC");
491 492 493 494 495
	return bl;
}

static sector_t fd_get_blocks(struct se_device *dev)
{
496
	struct fd_dev *fd_dev = FD_DEV(dev);
497 498 499 500 501 502 503 504 505 506 507 508
	struct file *f = fd_dev->fd_file;
	struct inode *i = f->f_mapping->host;
	unsigned long long dev_size;
	/*
	 * When using a file that references an underlying struct block_device,
	 * ensure dev_size is always based on the current inode size in order
	 * to handle underlying block_device resize operations.
	 */
	if (S_ISBLK(i->i_mode))
		dev_size = (i_size_read(i) - fd_dev->fd_block_size);
	else
		dev_size = fd_dev->fd_dev_size;
509

510
	return div_u64(dev_size, dev->dev_attrib.block_size);
511 512
}

C
Christoph Hellwig 已提交
513
static struct sbc_ops fd_sbc_ops = {
514
	.execute_rw		= fd_execute_rw,
515
	.execute_sync_cache	= fd_execute_sync_cache,
516 517 518 519
};

static int fd_parse_cdb(struct se_cmd *cmd)
{
C
Christoph Hellwig 已提交
520
	return sbc_parse_cdb(cmd, &fd_sbc_ops);
521 522
}

523 524
static struct se_subsystem_api fileio_template = {
	.name			= "fileio",
525 526
	.inquiry_prod		= "FILEIO",
	.inquiry_rev		= FD_VERSION,
527 528 529 530
	.owner			= THIS_MODULE,
	.transport_type		= TRANSPORT_PLUGIN_VHBA_PDEV,
	.attach_hba		= fd_attach_hba,
	.detach_hba		= fd_detach_hba,
531 532
	.alloc_device		= fd_alloc_device,
	.configure_device	= fd_configure_device,
533
	.free_device		= fd_free_device,
534
	.parse_cdb		= fd_parse_cdb,
535 536
	.set_configfs_dev_params = fd_set_configfs_dev_params,
	.show_configfs_dev_params = fd_show_configfs_dev_params,
537
	.get_device_type	= sbc_get_device_type,
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	.get_blocks		= fd_get_blocks,
};

static int __init fileio_module_init(void)
{
	return transport_subsystem_register(&fileio_template);
}

static void fileio_module_exit(void)
{
	transport_subsystem_release(&fileio_template);
}

MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
MODULE_AUTHOR("nab@Linux-iSCSI.org");
MODULE_LICENSE("GPL");

module_init(fileio_module_init);
module_exit(fileio_module_exit);