1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_file.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains the Storage Engine <-> FILEIO transport specific functions
5c66ac9dbSNicholas Bellinger  *
6fd9a11d7SNicholas Bellinger  * (c) Copyright 2005-2012 RisingTide Systems LLC.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
11c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
12c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
13c66ac9dbSNicholas Bellinger  * (at your option) any later version.
14c66ac9dbSNicholas Bellinger  *
15c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
16c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
19c66ac9dbSNicholas Bellinger  *
20c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
21c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
22c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23c66ac9dbSNicholas Bellinger  *
24c66ac9dbSNicholas Bellinger  ******************************************************************************/
25c66ac9dbSNicholas Bellinger 
26c66ac9dbSNicholas Bellinger #include <linux/string.h>
27c66ac9dbSNicholas Bellinger #include <linux/parser.h>
28c66ac9dbSNicholas Bellinger #include <linux/timer.h>
29c66ac9dbSNicholas Bellinger #include <linux/blkdev.h>
30c66ac9dbSNicholas Bellinger #include <linux/slab.h>
31c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
32827509e3SPaul Gortmaker #include <linux/module.h>
3370d3ae5cSAsias He #include <linux/falloc.h>
34c66ac9dbSNicholas Bellinger #include <scsi/scsi.h>
35c66ac9dbSNicholas Bellinger #include <scsi/scsi_host.h>
3650642762SAsias He #include <asm/unaligned.h>
37c66ac9dbSNicholas Bellinger 
38c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
39c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
40c66ac9dbSNicholas Bellinger 
41c66ac9dbSNicholas Bellinger #include "target_core_file.h"
42c66ac9dbSNicholas Bellinger 
430fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev)
440fd97ccfSChristoph Hellwig {
450fd97ccfSChristoph Hellwig 	return container_of(dev, struct fd_dev, dev);
460fd97ccfSChristoph Hellwig }
47c66ac9dbSNicholas Bellinger 
48c66ac9dbSNicholas Bellinger /*	fd_attach_hba(): (Part of se_subsystem_api_t template)
49c66ac9dbSNicholas Bellinger  *
50c66ac9dbSNicholas Bellinger  *
51c66ac9dbSNicholas Bellinger  */
52c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id)
53c66ac9dbSNicholas Bellinger {
54c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host;
55c66ac9dbSNicholas Bellinger 
56c66ac9dbSNicholas Bellinger 	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
576708bb27SAndy Grover 	if (!fd_host) {
586708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_host\n");
59e3d6f909SAndy Grover 		return -ENOMEM;
60c66ac9dbSNicholas Bellinger 	}
61c66ac9dbSNicholas Bellinger 
62c66ac9dbSNicholas Bellinger 	fd_host->fd_host_id = host_id;
63c66ac9dbSNicholas Bellinger 
64e3d6f909SAndy Grover 	hba->hba_ptr = fd_host;
65c66ac9dbSNicholas Bellinger 
666708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
67c66ac9dbSNicholas Bellinger 		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
68c66ac9dbSNicholas Bellinger 		TARGET_CORE_MOD_VERSION);
696708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
70e3d6f909SAndy Grover 		" MaxSectors: %u\n",
71e3d6f909SAndy Grover 		hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
72c66ac9dbSNicholas Bellinger 
73c66ac9dbSNicholas Bellinger 	return 0;
74c66ac9dbSNicholas Bellinger }
75c66ac9dbSNicholas Bellinger 
76c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba)
77c66ac9dbSNicholas Bellinger {
78c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host = hba->hba_ptr;
79c66ac9dbSNicholas Bellinger 
806708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
81c66ac9dbSNicholas Bellinger 		" Target Core\n", hba->hba_id, fd_host->fd_host_id);
82c66ac9dbSNicholas Bellinger 
83c66ac9dbSNicholas Bellinger 	kfree(fd_host);
84c66ac9dbSNicholas Bellinger 	hba->hba_ptr = NULL;
85c66ac9dbSNicholas Bellinger }
86c66ac9dbSNicholas Bellinger 
870fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
88c66ac9dbSNicholas Bellinger {
89c66ac9dbSNicholas Bellinger 	struct fd_dev *fd_dev;
908359cf43SJörn Engel 	struct fd_host *fd_host = hba->hba_ptr;
91c66ac9dbSNicholas Bellinger 
92c66ac9dbSNicholas Bellinger 	fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
936708bb27SAndy Grover 	if (!fd_dev) {
946708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_dev\n");
95c66ac9dbSNicholas Bellinger 		return NULL;
96c66ac9dbSNicholas Bellinger 	}
97c66ac9dbSNicholas Bellinger 
98c66ac9dbSNicholas Bellinger 	fd_dev->fd_host = fd_host;
99c66ac9dbSNicholas Bellinger 
1006708bb27SAndy Grover 	pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
101c66ac9dbSNicholas Bellinger 
1020fd97ccfSChristoph Hellwig 	return &fd_dev->dev;
103c66ac9dbSNicholas Bellinger }
104c66ac9dbSNicholas Bellinger 
1050fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev)
106c66ac9dbSNicholas Bellinger {
1070fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
1080fd97ccfSChristoph Hellwig 	struct fd_host *fd_host = dev->se_hba->hba_ptr;
109c66ac9dbSNicholas Bellinger 	struct file *file;
110c66ac9dbSNicholas Bellinger 	struct inode *inode = NULL;
1110fd97ccfSChristoph Hellwig 	int flags, ret = -EINVAL;
112c66ac9dbSNicholas Bellinger 
1130fd97ccfSChristoph Hellwig 	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
1140fd97ccfSChristoph Hellwig 		pr_err("Missing fd_dev_name=\n");
1150fd97ccfSChristoph Hellwig 		return -EINVAL;
1160fd97ccfSChristoph Hellwig 	}
117c66ac9dbSNicholas Bellinger 
118c66ac9dbSNicholas Bellinger 	/*
119a4dff304SNicholas Bellinger 	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
120a4dff304SNicholas Bellinger 	 * of pure timestamp updates.
121c66ac9dbSNicholas Bellinger 	 */
122a4dff304SNicholas Bellinger 	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
1230fd97ccfSChristoph Hellwig 
124b32f4c7eSNicholas Bellinger 	/*
125b32f4c7eSNicholas Bellinger 	 * Optionally allow fd_buffered_io=1 to be enabled for people
126b32f4c7eSNicholas Bellinger 	 * who want use the fs buffer cache as an WriteCache mechanism.
127b32f4c7eSNicholas Bellinger 	 *
128b32f4c7eSNicholas Bellinger 	 * This means that in event of a hard failure, there is a risk
129b32f4c7eSNicholas Bellinger 	 * of silent data-loss if the SCSI client has *not* performed a
130b32f4c7eSNicholas Bellinger 	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
131b32f4c7eSNicholas Bellinger 	 * to write-out the entire device cache.
132b32f4c7eSNicholas Bellinger 	 */
133b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
134b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
135b32f4c7eSNicholas Bellinger 		flags &= ~O_DSYNC;
136b32f4c7eSNicholas Bellinger 	}
137c66ac9dbSNicholas Bellinger 
138dbc6e022SAl Viro 	file = filp_open(fd_dev->fd_dev_name, flags, 0600);
139613640e4SNicholas Bellinger 	if (IS_ERR(file)) {
140dbc6e022SAl Viro 		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
141613640e4SNicholas Bellinger 		ret = PTR_ERR(file);
142613640e4SNicholas Bellinger 		goto fail;
143613640e4SNicholas Bellinger 	}
144c66ac9dbSNicholas Bellinger 	fd_dev->fd_file = file;
145c66ac9dbSNicholas Bellinger 	/*
146c66ac9dbSNicholas Bellinger 	 * If using a block backend with this struct file, we extract
147c66ac9dbSNicholas Bellinger 	 * fd_dev->fd_[block,dev]_size from struct block_device.
148c66ac9dbSNicholas Bellinger 	 *
149c66ac9dbSNicholas Bellinger 	 * Otherwise, we use the passed fd_size= from configfs
150c66ac9dbSNicholas Bellinger 	 */
151c66ac9dbSNicholas Bellinger 	inode = file->f_mapping->host;
152c66ac9dbSNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
1530fd97ccfSChristoph Hellwig 		struct request_queue *q = bdev_get_queue(inode->i_bdev);
154cd9323fdSNicholas Bellinger 		unsigned long long dev_size;
1550fd97ccfSChristoph Hellwig 
1560fd97ccfSChristoph Hellwig 		dev->dev_attrib.hw_block_size =
1570fd97ccfSChristoph Hellwig 			bdev_logical_block_size(inode->i_bdev);
1580fd97ccfSChristoph Hellwig 		dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
1590fd97ccfSChristoph Hellwig 
160c66ac9dbSNicholas Bellinger 		/*
161c66ac9dbSNicholas Bellinger 		 * Determine the number of bytes from i_size_read() minus
162c66ac9dbSNicholas Bellinger 		 * one (1) logical sector from underlying struct block_device
163c66ac9dbSNicholas Bellinger 		 */
164cd9323fdSNicholas Bellinger 		dev_size = (i_size_read(file->f_mapping->host) -
165c66ac9dbSNicholas Bellinger 				       fd_dev->fd_block_size);
166c66ac9dbSNicholas Bellinger 
1676708bb27SAndy Grover 		pr_debug("FILEIO: Using size: %llu bytes from struct"
168c66ac9dbSNicholas Bellinger 			" block_device blocks: %llu logical_block_size: %d\n",
169cd9323fdSNicholas Bellinger 			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
170c66ac9dbSNicholas Bellinger 			fd_dev->fd_block_size);
17170d3ae5cSAsias He 		/*
17270d3ae5cSAsias He 		 * Check if the underlying struct block_device request_queue supports
17370d3ae5cSAsias He 		 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
17470d3ae5cSAsias He 		 * in ATA and we need to set TPE=1
17570d3ae5cSAsias He 		 */
17670d3ae5cSAsias He 		if (blk_queue_discard(q)) {
17770d3ae5cSAsias He 			dev->dev_attrib.max_unmap_lba_count =
17870d3ae5cSAsias He 				q->limits.max_discard_sectors;
17970d3ae5cSAsias He 			/*
18070d3ae5cSAsias He 			 * Currently hardcoded to 1 in Linux/SCSI code..
18170d3ae5cSAsias He 			 */
18270d3ae5cSAsias He 			dev->dev_attrib.max_unmap_block_desc_count = 1;
18370d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity =
18470d3ae5cSAsias He 				q->limits.discard_granularity >> 9;
18570d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity_alignment =
18670d3ae5cSAsias He 				q->limits.discard_alignment;
18770d3ae5cSAsias He 			pr_debug("IFILE: BLOCK Discard support available,"
18870d3ae5cSAsias He 					" disabled by default\n");
18970d3ae5cSAsias He 		}
19070d3ae5cSAsias He 		/*
19170d3ae5cSAsias He 		 * Enable write same emulation for IBLOCK and use 0xFFFF as
19270d3ae5cSAsias He 		 * the smaller WRITE_SAME(10) only has a two-byte block count.
19370d3ae5cSAsias He 		 */
19470d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0xFFFF;
195c66ac9dbSNicholas Bellinger 	} else {
196c66ac9dbSNicholas Bellinger 		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
1976708bb27SAndy Grover 			pr_err("FILEIO: Missing fd_dev_size="
198c66ac9dbSNicholas Bellinger 				" parameter, and no backing struct"
199c66ac9dbSNicholas Bellinger 				" block_device\n");
200c66ac9dbSNicholas Bellinger 			goto fail;
201c66ac9dbSNicholas Bellinger 		}
202c66ac9dbSNicholas Bellinger 
2030fd97ccfSChristoph Hellwig 		dev->dev_attrib.hw_block_size = FD_BLOCKSIZE;
2040fd97ccfSChristoph Hellwig 		dev->dev_attrib.hw_max_sectors = FD_MAX_SECTORS;
20570d3ae5cSAsias He 
20670d3ae5cSAsias He 		/*
20770d3ae5cSAsias He 		 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
20870d3ae5cSAsias He 		 */
20970d3ae5cSAsias He 		dev->dev_attrib.max_unmap_lba_count = 0x2000;
21070d3ae5cSAsias He 		/*
21170d3ae5cSAsias He 		 * Currently hardcoded to 1 in Linux/SCSI code..
21270d3ae5cSAsias He 		 */
21370d3ae5cSAsias He 		dev->dev_attrib.max_unmap_block_desc_count = 1;
21470d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity = 1;
21570d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity_alignment = 0;
21670d3ae5cSAsias He 
21770d3ae5cSAsias He 		/*
21870d3ae5cSAsias He 		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
21970d3ae5cSAsias He 		 * based upon struct iovec limit for vfs_writev()
22070d3ae5cSAsias He 		 */
22170d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0x1000;
222c66ac9dbSNicholas Bellinger 	}
223c66ac9dbSNicholas Bellinger 
2240fd97ccfSChristoph Hellwig 	fd_dev->fd_block_size = dev->dev_attrib.hw_block_size;
225c66ac9dbSNicholas Bellinger 
2260fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
227c66ac9dbSNicholas Bellinger 
228b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
229b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
230b32f4c7eSNicholas Bellinger 			" with FDBD_HAS_BUFFERED_IO_WCE\n");
2310fd97ccfSChristoph Hellwig 		dev->dev_attrib.emulate_write_cache = 1;
232b32f4c7eSNicholas Bellinger 	}
233b32f4c7eSNicholas Bellinger 
234c66ac9dbSNicholas Bellinger 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
235c66ac9dbSNicholas Bellinger 	fd_dev->fd_queue_depth = dev->queue_depth;
236c66ac9dbSNicholas Bellinger 
2376708bb27SAndy Grover 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
238c66ac9dbSNicholas Bellinger 		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
239c66ac9dbSNicholas Bellinger 			fd_dev->fd_dev_name, fd_dev->fd_dev_size);
240c66ac9dbSNicholas Bellinger 
2410fd97ccfSChristoph Hellwig 	return 0;
242c66ac9dbSNicholas Bellinger fail:
243c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
244c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
245c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
246c66ac9dbSNicholas Bellinger 	}
2470fd97ccfSChristoph Hellwig 	return ret;
248c66ac9dbSNicholas Bellinger }
249c66ac9dbSNicholas Bellinger 
2500fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev)
251c66ac9dbSNicholas Bellinger {
2520fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
253c66ac9dbSNicholas Bellinger 
254c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
255c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
256c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
257c66ac9dbSNicholas Bellinger 	}
258c66ac9dbSNicholas Bellinger 
259c66ac9dbSNicholas Bellinger 	kfree(fd_dev);
260c66ac9dbSNicholas Bellinger }
261c66ac9dbSNicholas Bellinger 
262778229afSSebastian Andrzej Siewior static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
263778229afSSebastian Andrzej Siewior 		u32 sgl_nents, int is_write)
264c66ac9dbSNicholas Bellinger {
2655787cacdSChristoph Hellwig 	struct se_device *se_dev = cmd->se_dev;
2660fd97ccfSChristoph Hellwig 	struct fd_dev *dev = FD_DEV(se_dev);
2676708bb27SAndy Grover 	struct file *fd = dev->fd_file;
2685787cacdSChristoph Hellwig 	struct scatterlist *sg;
269c66ac9dbSNicholas Bellinger 	struct iovec *iov;
270c66ac9dbSNicholas Bellinger 	mm_segment_t old_fs;
2710fd97ccfSChristoph Hellwig 	loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
272c66ac9dbSNicholas Bellinger 	int ret = 0, i;
273c66ac9dbSNicholas Bellinger 
2745787cacdSChristoph Hellwig 	iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
2756708bb27SAndy Grover 	if (!iov) {
2766708bb27SAndy Grover 		pr_err("Unable to allocate fd_do_readv iov[]\n");
277e3d6f909SAndy Grover 		return -ENOMEM;
278c66ac9dbSNicholas Bellinger 	}
279c66ac9dbSNicholas Bellinger 
2805787cacdSChristoph Hellwig 	for_each_sg(sgl, sg, sgl_nents, i) {
2819649fa1bSSebastian Andrzej Siewior 		iov[i].iov_len = sg->length;
28240ff2c3bSSebastian Andrzej Siewior 		iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
283c66ac9dbSNicholas Bellinger 	}
284c66ac9dbSNicholas Bellinger 
285c66ac9dbSNicholas Bellinger 	old_fs = get_fs();
286c66ac9dbSNicholas Bellinger 	set_fs(get_ds());
287778229afSSebastian Andrzej Siewior 
288778229afSSebastian Andrzej Siewior 	if (is_write)
289778229afSSebastian Andrzej Siewior 		ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
290778229afSSebastian Andrzej Siewior 	else
2915787cacdSChristoph Hellwig 		ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
292778229afSSebastian Andrzej Siewior 
293c66ac9dbSNicholas Bellinger 	set_fs(old_fs);
294c66ac9dbSNicholas Bellinger 
29540ff2c3bSSebastian Andrzej Siewior 	for_each_sg(sgl, sg, sgl_nents, i)
29640ff2c3bSSebastian Andrzej Siewior 		kunmap(sg_page(sg));
297778229afSSebastian Andrzej Siewior 
298c66ac9dbSNicholas Bellinger 	kfree(iov);
299778229afSSebastian Andrzej Siewior 
300778229afSSebastian Andrzej Siewior 	if (is_write) {
301778229afSSebastian Andrzej Siewior 		if (ret < 0 || ret != cmd->data_length) {
302778229afSSebastian Andrzej Siewior 			pr_err("%s() write returned %d\n", __func__, ret);
303778229afSSebastian Andrzej Siewior 			return (ret < 0 ? ret : -EINVAL);
304778229afSSebastian Andrzej Siewior 		}
305778229afSSebastian Andrzej Siewior 	} else {
306c66ac9dbSNicholas Bellinger 		/*
307c66ac9dbSNicholas Bellinger 		 * Return zeros and GOOD status even if the READ did not return
308c66ac9dbSNicholas Bellinger 		 * the expected virt_size for struct file w/o a backing struct
309c66ac9dbSNicholas Bellinger 		 * block_device.
310c66ac9dbSNicholas Bellinger 		 */
311496ad9aaSAl Viro 		if (S_ISBLK(file_inode(fd)->i_mode)) {
3125787cacdSChristoph Hellwig 			if (ret < 0 || ret != cmd->data_length) {
313778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d, expecting %u for "
314778229afSSebastian Andrzej Siewior 						"S_ISBLK\n", __func__, ret,
315778229afSSebastian Andrzej Siewior 						cmd->data_length);
316e3d6f909SAndy Grover 				return (ret < 0 ? ret : -EINVAL);
317c66ac9dbSNicholas Bellinger 			}
318c66ac9dbSNicholas Bellinger 		} else {
319c66ac9dbSNicholas Bellinger 			if (ret < 0) {
320778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d for non S_ISBLK\n",
321778229afSSebastian Andrzej Siewior 						__func__, ret);
322e3d6f909SAndy Grover 				return ret;
323c66ac9dbSNicholas Bellinger 			}
324c66ac9dbSNicholas Bellinger 		}
325c66ac9dbSNicholas Bellinger 	}
326c66ac9dbSNicholas Bellinger 	return 1;
327c66ac9dbSNicholas Bellinger }
328c66ac9dbSNicholas Bellinger 
329de103c93SChristoph Hellwig static sense_reason_t
330de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd)
331c66ac9dbSNicholas Bellinger {
332c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3330fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
334a1d8b49aSAndy Grover 	int immed = (cmd->t_task_cdb[1] & 0x2);
335c66ac9dbSNicholas Bellinger 	loff_t start, end;
336c66ac9dbSNicholas Bellinger 	int ret;
337c66ac9dbSNicholas Bellinger 
338c66ac9dbSNicholas Bellinger 	/*
339c66ac9dbSNicholas Bellinger 	 * If the Immediate bit is set, queue up the GOOD response
340c66ac9dbSNicholas Bellinger 	 * for this SYNCHRONIZE_CACHE op
341c66ac9dbSNicholas Bellinger 	 */
342c66ac9dbSNicholas Bellinger 	if (immed)
3435787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
344c66ac9dbSNicholas Bellinger 
345c66ac9dbSNicholas Bellinger 	/*
346c66ac9dbSNicholas Bellinger 	 * Determine if we will be flushing the entire device.
347c66ac9dbSNicholas Bellinger 	 */
348a1d8b49aSAndy Grover 	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
349c66ac9dbSNicholas Bellinger 		start = 0;
350c66ac9dbSNicholas Bellinger 		end = LLONG_MAX;
351c66ac9dbSNicholas Bellinger 	} else {
3520fd97ccfSChristoph Hellwig 		start = cmd->t_task_lba * dev->dev_attrib.block_size;
353c66ac9dbSNicholas Bellinger 		if (cmd->data_length)
354c66ac9dbSNicholas Bellinger 			end = start + cmd->data_length;
355c66ac9dbSNicholas Bellinger 		else
356c66ac9dbSNicholas Bellinger 			end = LLONG_MAX;
357c66ac9dbSNicholas Bellinger 	}
358c66ac9dbSNicholas Bellinger 
359c66ac9dbSNicholas Bellinger 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
360c66ac9dbSNicholas Bellinger 	if (ret != 0)
3616708bb27SAndy Grover 		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
362c66ac9dbSNicholas Bellinger 
3635787cacdSChristoph Hellwig 	if (immed)
364ad67f0d9SChristoph Hellwig 		return 0;
3655787cacdSChristoph Hellwig 
366de103c93SChristoph Hellwig 	if (ret)
3675787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
368de103c93SChristoph Hellwig 	else
3695787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
370ad67f0d9SChristoph Hellwig 
371ad67f0d9SChristoph Hellwig 	return 0;
372c66ac9dbSNicholas Bellinger }
373c66ac9dbSNicholas Bellinger 
3747b745c84SNicholas Bellinger static unsigned char *
3757b745c84SNicholas Bellinger fd_setup_write_same_buf(struct se_cmd *cmd, struct scatterlist *sg,
3767b745c84SNicholas Bellinger 		    unsigned int len)
3777b745c84SNicholas Bellinger {
3787b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
3797b745c84SNicholas Bellinger 	unsigned int block_size = se_dev->dev_attrib.block_size;
3807b745c84SNicholas Bellinger 	unsigned int i = 0, end;
3817b745c84SNicholas Bellinger 	unsigned char *buf, *p, *kmap_buf;
3827b745c84SNicholas Bellinger 
3837b745c84SNicholas Bellinger 	buf = kzalloc(min_t(unsigned int, len, PAGE_SIZE), GFP_KERNEL);
3847b745c84SNicholas Bellinger 	if (!buf) {
3857b745c84SNicholas Bellinger 		pr_err("Unable to allocate fd_execute_write_same buf\n");
3867b745c84SNicholas Bellinger 		return NULL;
3877b745c84SNicholas Bellinger 	}
3887b745c84SNicholas Bellinger 
3897b745c84SNicholas Bellinger 	kmap_buf = kmap(sg_page(sg)) + sg->offset;
3907b745c84SNicholas Bellinger 	if (!kmap_buf) {
3917b745c84SNicholas Bellinger 		pr_err("kmap() failed in fd_setup_write_same\n");
3927b745c84SNicholas Bellinger 		kfree(buf);
3937b745c84SNicholas Bellinger 		return NULL;
3947b745c84SNicholas Bellinger 	}
3957b745c84SNicholas Bellinger 	/*
3967b745c84SNicholas Bellinger 	 * Fill local *buf to contain multiple WRITE_SAME blocks up to
3977b745c84SNicholas Bellinger 	 * min(len, PAGE_SIZE)
3987b745c84SNicholas Bellinger 	 */
3997b745c84SNicholas Bellinger 	p = buf;
4007b745c84SNicholas Bellinger 	end = min_t(unsigned int, len, PAGE_SIZE);
4017b745c84SNicholas Bellinger 
4027b745c84SNicholas Bellinger 	while (i < end) {
4037b745c84SNicholas Bellinger 		memcpy(p, kmap_buf, block_size);
4047b745c84SNicholas Bellinger 
4057b745c84SNicholas Bellinger 		i += block_size;
4067b745c84SNicholas Bellinger 		p += block_size;
4077b745c84SNicholas Bellinger 	}
4087b745c84SNicholas Bellinger 	kunmap(sg_page(sg));
4097b745c84SNicholas Bellinger 
4107b745c84SNicholas Bellinger 	return buf;
4117b745c84SNicholas Bellinger }
4127b745c84SNicholas Bellinger 
4137b745c84SNicholas Bellinger static sense_reason_t
4147b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd)
4157b745c84SNicholas Bellinger {
4167b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
4177b745c84SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(se_dev);
4187b745c84SNicholas Bellinger 	struct file *f = fd_dev->fd_file;
4197b745c84SNicholas Bellinger 	struct scatterlist *sg;
4207b745c84SNicholas Bellinger 	struct iovec *iov;
4217b745c84SNicholas Bellinger 	mm_segment_t old_fs;
422972b29c8SRoland Dreier 	sector_t nolb = sbc_get_write_same_sectors(cmd);
4237b745c84SNicholas Bellinger 	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
4247b745c84SNicholas Bellinger 	unsigned int len, len_tmp, iov_num;
4257b745c84SNicholas Bellinger 	int i, rc;
4267b745c84SNicholas Bellinger 	unsigned char *buf;
4277b745c84SNicholas Bellinger 
4287b745c84SNicholas Bellinger 	if (!nolb) {
4297b745c84SNicholas Bellinger 		target_complete_cmd(cmd, SAM_STAT_GOOD);
4307b745c84SNicholas Bellinger 		return 0;
4317b745c84SNicholas Bellinger 	}
4327b745c84SNicholas Bellinger 	sg = &cmd->t_data_sg[0];
4337b745c84SNicholas Bellinger 
4347b745c84SNicholas Bellinger 	if (cmd->t_data_nents > 1 ||
4357b745c84SNicholas Bellinger 	    sg->length != cmd->se_dev->dev_attrib.block_size) {
4367b745c84SNicholas Bellinger 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
4377b745c84SNicholas Bellinger 			" block_size: %u\n", cmd->t_data_nents, sg->length,
4387b745c84SNicholas Bellinger 			cmd->se_dev->dev_attrib.block_size);
4397b745c84SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
4407b745c84SNicholas Bellinger 	}
4417b745c84SNicholas Bellinger 
4427b745c84SNicholas Bellinger 	len = len_tmp = nolb * se_dev->dev_attrib.block_size;
4437b745c84SNicholas Bellinger 	iov_num = DIV_ROUND_UP(len, PAGE_SIZE);
4447b745c84SNicholas Bellinger 
4457b745c84SNicholas Bellinger 	buf = fd_setup_write_same_buf(cmd, sg, len);
4467b745c84SNicholas Bellinger 	if (!buf)
4477b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4487b745c84SNicholas Bellinger 
4497b745c84SNicholas Bellinger 	iov = vzalloc(sizeof(struct iovec) * iov_num);
4507b745c84SNicholas Bellinger 	if (!iov) {
4517b745c84SNicholas Bellinger 		pr_err("Unable to allocate fd_execute_write_same iovecs\n");
4527b745c84SNicholas Bellinger 		kfree(buf);
4537b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4547b745c84SNicholas Bellinger 	}
4557b745c84SNicholas Bellinger 	/*
4567b745c84SNicholas Bellinger 	 * Map the single fabric received scatterlist block now populated
4577b745c84SNicholas Bellinger 	 * in *buf into each iovec for I/O submission.
4587b745c84SNicholas Bellinger 	 */
4597b745c84SNicholas Bellinger 	for (i = 0; i < iov_num; i++) {
4607b745c84SNicholas Bellinger 		iov[i].iov_base = buf;
4617b745c84SNicholas Bellinger 		iov[i].iov_len = min_t(unsigned int, len_tmp, PAGE_SIZE);
4627b745c84SNicholas Bellinger 		len_tmp -= iov[i].iov_len;
4637b745c84SNicholas Bellinger 	}
4647b745c84SNicholas Bellinger 
4657b745c84SNicholas Bellinger 	old_fs = get_fs();
4667b745c84SNicholas Bellinger 	set_fs(get_ds());
4677b745c84SNicholas Bellinger 	rc = vfs_writev(f, &iov[0], iov_num, &pos);
4687b745c84SNicholas Bellinger 	set_fs(old_fs);
4697b745c84SNicholas Bellinger 
4707b745c84SNicholas Bellinger 	vfree(iov);
4717b745c84SNicholas Bellinger 	kfree(buf);
4727b745c84SNicholas Bellinger 
4737b745c84SNicholas Bellinger 	if (rc < 0 || rc != len) {
4747b745c84SNicholas Bellinger 		pr_err("vfs_writev() returned %d for write same\n", rc);
4757b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4767b745c84SNicholas Bellinger 	}
4777b745c84SNicholas Bellinger 
4787b745c84SNicholas Bellinger 	target_complete_cmd(cmd, SAM_STAT_GOOD);
4797b745c84SNicholas Bellinger 	return 0;
4807b745c84SNicholas Bellinger }
4817b745c84SNicholas Bellinger 
482de103c93SChristoph Hellwig static sense_reason_t
48343f55bbbSAsias He fd_do_unmap(struct se_cmd *cmd, struct file *file, sector_t lba, sector_t nolb)
48470d3ae5cSAsias He {
48570d3ae5cSAsias He 	struct inode *inode = file->f_mapping->host;
48670d3ae5cSAsias He 	int ret;
48770d3ae5cSAsias He 
48870d3ae5cSAsias He 	if (S_ISBLK(inode->i_mode)) {
48970d3ae5cSAsias He 		/* The backend is block device, use discard */
49070d3ae5cSAsias He 		struct block_device *bdev = inode->i_bdev;
49170d3ae5cSAsias He 
49243f55bbbSAsias He 		ret = blkdev_issue_discard(bdev, lba,
49370d3ae5cSAsias He 				nolb, GFP_KERNEL, 0);
49470d3ae5cSAsias He 		if (ret < 0) {
49570d3ae5cSAsias He 			pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
49670d3ae5cSAsias He 				ret);
49770d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
49870d3ae5cSAsias He 		}
49970d3ae5cSAsias He 	} else {
50070d3ae5cSAsias He 		/* The backend is normal file, use fallocate */
50143f55bbbSAsias He 		struct se_device *se_dev = cmd->se_dev;
50243f55bbbSAsias He 		loff_t pos = lba * se_dev->dev_attrib.block_size;
50370d3ae5cSAsias He 		unsigned int len = nolb * se_dev->dev_attrib.block_size;
50470d3ae5cSAsias He 		int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
50570d3ae5cSAsias He 
50670d3ae5cSAsias He 		if (!file->f_op->fallocate)
50770d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
50870d3ae5cSAsias He 
50970d3ae5cSAsias He 		ret = file->f_op->fallocate(file, mode, pos, len);
51070d3ae5cSAsias He 		if (ret < 0) {
51170d3ae5cSAsias He 			pr_warn("FILEIO: fallocate() failed: %d\n", ret);
51270d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
51370d3ae5cSAsias He 		}
51470d3ae5cSAsias He 	}
51570d3ae5cSAsias He 
51643f55bbbSAsias He 	return 0;
51743f55bbbSAsias He }
51843f55bbbSAsias He 
51943f55bbbSAsias He static sense_reason_t
52043f55bbbSAsias He fd_execute_write_same_unmap(struct se_cmd *cmd)
52143f55bbbSAsias He {
52243f55bbbSAsias He 	struct se_device *se_dev = cmd->se_dev;
52343f55bbbSAsias He 	struct fd_dev *fd_dev = FD_DEV(se_dev);
52443f55bbbSAsias He 	struct file *file = fd_dev->fd_file;
52543f55bbbSAsias He 	sector_t lba = cmd->t_task_lba;
52643f55bbbSAsias He 	sector_t nolb = sbc_get_write_same_sectors(cmd);
52743f55bbbSAsias He 	int ret;
52843f55bbbSAsias He 
52943f55bbbSAsias He 	if (!nolb) {
53043f55bbbSAsias He 		target_complete_cmd(cmd, SAM_STAT_GOOD);
53143f55bbbSAsias He 		return 0;
53243f55bbbSAsias He 	}
53343f55bbbSAsias He 
53443f55bbbSAsias He 	ret = fd_do_unmap(cmd, file, lba, nolb);
53543f55bbbSAsias He 	if (ret)
53643f55bbbSAsias He 		return ret;
53743f55bbbSAsias He 
53870d3ae5cSAsias He 	target_complete_cmd(cmd, GOOD);
53970d3ae5cSAsias He 	return 0;
54070d3ae5cSAsias He }
54170d3ae5cSAsias He 
54270d3ae5cSAsias He static sense_reason_t
54350642762SAsias He fd_execute_unmap(struct se_cmd *cmd)
54450642762SAsias He {
54550642762SAsias He 	struct se_device *dev = cmd->se_dev;
54650642762SAsias He 	struct fd_dev *fd_dev = FD_DEV(dev);
54750642762SAsias He 	struct file *file = fd_dev->fd_file;
54850642762SAsias He 	unsigned char *buf, *ptr = NULL;
54950642762SAsias He 	sector_t lba;
55050642762SAsias He 	int size;
55150642762SAsias He 	u32 range;
55250642762SAsias He 	sense_reason_t ret = 0;
55343f55bbbSAsias He 	int dl, bd_dl;
55450642762SAsias He 
55550642762SAsias He 	/* We never set ANC_SUP */
55650642762SAsias He 	if (cmd->t_task_cdb[1])
55750642762SAsias He 		return TCM_INVALID_CDB_FIELD;
55850642762SAsias He 
55950642762SAsias He 	if (cmd->data_length == 0) {
56050642762SAsias He 		target_complete_cmd(cmd, SAM_STAT_GOOD);
56150642762SAsias He 		return 0;
56250642762SAsias He 	}
56350642762SAsias He 
56450642762SAsias He 	if (cmd->data_length < 8) {
56550642762SAsias He 		pr_warn("UNMAP parameter list length %u too small\n",
56650642762SAsias He 			cmd->data_length);
56750642762SAsias He 		return TCM_PARAMETER_LIST_LENGTH_ERROR;
56850642762SAsias He 	}
56950642762SAsias He 
57050642762SAsias He 	buf = transport_kmap_data_sg(cmd);
57150642762SAsias He 	if (!buf)
57250642762SAsias He 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
57350642762SAsias He 
57450642762SAsias He 	dl = get_unaligned_be16(&buf[0]);
57550642762SAsias He 	bd_dl = get_unaligned_be16(&buf[2]);
57650642762SAsias He 
57750642762SAsias He 	size = cmd->data_length - 8;
57850642762SAsias He 	if (bd_dl > size)
57950642762SAsias He 		pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
58050642762SAsias He 			cmd->data_length, bd_dl);
58150642762SAsias He 	else
58250642762SAsias He 		size = bd_dl;
58350642762SAsias He 
58450642762SAsias He 	if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
58550642762SAsias He 		ret = TCM_INVALID_PARAMETER_LIST;
58650642762SAsias He 		goto err;
58750642762SAsias He 	}
58850642762SAsias He 
58950642762SAsias He 	/* First UNMAP block descriptor starts at 8 byte offset */
59050642762SAsias He 	ptr = &buf[8];
59150642762SAsias He 	pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
59250642762SAsias He 		" ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
59350642762SAsias He 
59450642762SAsias He 	while (size >= 16) {
59550642762SAsias He 		lba = get_unaligned_be64(&ptr[0]);
59650642762SAsias He 		range = get_unaligned_be32(&ptr[8]);
59750642762SAsias He 		pr_debug("UNMAP: Using lba: %llu and range: %u\n",
59850642762SAsias He 				 (unsigned long long)lba, range);
59950642762SAsias He 
60050642762SAsias He 		if (range > dev->dev_attrib.max_unmap_lba_count) {
60150642762SAsias He 			ret = TCM_INVALID_PARAMETER_LIST;
60250642762SAsias He 			goto err;
60350642762SAsias He 		}
60450642762SAsias He 
60550642762SAsias He 		if (lba + range > dev->transport->get_blocks(dev) + 1) {
60650642762SAsias He 			ret = TCM_ADDRESS_OUT_OF_RANGE;
60750642762SAsias He 			goto err;
60850642762SAsias He 		}
60950642762SAsias He 
61043f55bbbSAsias He 		ret = fd_do_unmap(cmd, file, lba, range);
61143f55bbbSAsias He 		if (ret)
61250642762SAsias He 			goto err;
61350642762SAsias He 
61450642762SAsias He 		ptr += 16;
61550642762SAsias He 		size -= 16;
61650642762SAsias He 	}
61750642762SAsias He 
61850642762SAsias He err:
61950642762SAsias He 	transport_kunmap_data_sg(cmd);
62050642762SAsias He 	if (!ret)
62150642762SAsias He 		target_complete_cmd(cmd, GOOD);
62250642762SAsias He 	return ret;
62350642762SAsias He }
62450642762SAsias He 
62550642762SAsias He static sense_reason_t
626de103c93SChristoph Hellwig fd_execute_rw(struct se_cmd *cmd)
627c66ac9dbSNicholas Bellinger {
6280c2ad7d1SChristoph Hellwig 	struct scatterlist *sgl = cmd->t_data_sg;
6290c2ad7d1SChristoph Hellwig 	u32 sgl_nents = cmd->t_data_nents;
6300c2ad7d1SChristoph Hellwig 	enum dma_data_direction data_direction = cmd->data_direction;
631c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
632c66ac9dbSNicholas Bellinger 	int ret = 0;
633c66ac9dbSNicholas Bellinger 
634c66ac9dbSNicholas Bellinger 	/*
635c66ac9dbSNicholas Bellinger 	 * Call vectorized fileio functions to map struct scatterlist
636c66ac9dbSNicholas Bellinger 	 * physical memory addresses to struct iovec virtual memory.
637c66ac9dbSNicholas Bellinger 	 */
6385787cacdSChristoph Hellwig 	if (data_direction == DMA_FROM_DEVICE) {
639778229afSSebastian Andrzej Siewior 		ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
640c66ac9dbSNicholas Bellinger 	} else {
641778229afSSebastian Andrzej Siewior 		ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
642a4dff304SNicholas Bellinger 		/*
643a4dff304SNicholas Bellinger 		 * Perform implict vfs_fsync_range() for fd_do_writev() ops
644a4dff304SNicholas Bellinger 		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
645a4dff304SNicholas Bellinger 		 * Allow this to happen independent of WCE=0 setting.
646a4dff304SNicholas Bellinger 		 */
647c66ac9dbSNicholas Bellinger 		if (ret > 0 &&
6480fd97ccfSChristoph Hellwig 		    dev->dev_attrib.emulate_fua_write > 0 &&
6492d3a4b51SChristoph Hellwig 		    (cmd->se_cmd_flags & SCF_FUA)) {
6500fd97ccfSChristoph Hellwig 			struct fd_dev *fd_dev = FD_DEV(dev);
651a4dff304SNicholas Bellinger 			loff_t start = cmd->t_task_lba *
6520fd97ccfSChristoph Hellwig 				dev->dev_attrib.block_size;
653a4dff304SNicholas Bellinger 			loff_t end = start + cmd->data_length;
654c66ac9dbSNicholas Bellinger 
655a4dff304SNicholas Bellinger 			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
656a4dff304SNicholas Bellinger 		}
657c66ac9dbSNicholas Bellinger 	}
658c66ac9dbSNicholas Bellinger 
659de103c93SChristoph Hellwig 	if (ret < 0)
660de103c93SChristoph Hellwig 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
661de103c93SChristoph Hellwig 
6625787cacdSChristoph Hellwig 	if (ret)
6635787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
66403e98c9eSNicholas Bellinger 	return 0;
665c66ac9dbSNicholas Bellinger }
666c66ac9dbSNicholas Bellinger 
667c66ac9dbSNicholas Bellinger enum {
668c66ac9dbSNicholas Bellinger 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
669c66ac9dbSNicholas Bellinger };
670c66ac9dbSNicholas Bellinger 
671c66ac9dbSNicholas Bellinger static match_table_t tokens = {
672c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_name, "fd_dev_name=%s"},
673c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_size, "fd_dev_size=%s"},
674b32f4c7eSNicholas Bellinger 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
675c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
676c66ac9dbSNicholas Bellinger };
677c66ac9dbSNicholas Bellinger 
6780fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
679c66ac9dbSNicholas Bellinger 		const char *page, ssize_t count)
680c66ac9dbSNicholas Bellinger {
6810fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
682c66ac9dbSNicholas Bellinger 	char *orig, *ptr, *arg_p, *opts;
683c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
684b32f4c7eSNicholas Bellinger 	int ret = 0, arg, token;
685c66ac9dbSNicholas Bellinger 
686c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
687c66ac9dbSNicholas Bellinger 	if (!opts)
688c66ac9dbSNicholas Bellinger 		return -ENOMEM;
689c66ac9dbSNicholas Bellinger 
690c66ac9dbSNicholas Bellinger 	orig = opts;
691c66ac9dbSNicholas Bellinger 
69290c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
693c66ac9dbSNicholas Bellinger 		if (!*ptr)
694c66ac9dbSNicholas Bellinger 			continue;
695c66ac9dbSNicholas Bellinger 
696c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
697c66ac9dbSNicholas Bellinger 		switch (token) {
698c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_name:
699dbc6e022SAl Viro 			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
700dbc6e022SAl Viro 				FD_MAX_DEV_NAME) == 0) {
701dbc6e022SAl Viro 				ret = -EINVAL;
7026d180253SJesper Juhl 				break;
7036d180253SJesper Juhl 			}
7046708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Path: %s\n",
705c66ac9dbSNicholas Bellinger 					fd_dev->fd_dev_name);
706c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_PATH;
707c66ac9dbSNicholas Bellinger 			break;
708c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_size:
709c66ac9dbSNicholas Bellinger 			arg_p = match_strdup(&args[0]);
7106d180253SJesper Juhl 			if (!arg_p) {
7116d180253SJesper Juhl 				ret = -ENOMEM;
7126d180253SJesper Juhl 				break;
7136d180253SJesper Juhl 			}
714c66ac9dbSNicholas Bellinger 			ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
7156d180253SJesper Juhl 			kfree(arg_p);
716c66ac9dbSNicholas Bellinger 			if (ret < 0) {
7176708bb27SAndy Grover 				pr_err("strict_strtoull() failed for"
718c66ac9dbSNicholas Bellinger 						" fd_dev_size=\n");
719c66ac9dbSNicholas Bellinger 				goto out;
720c66ac9dbSNicholas Bellinger 			}
7216708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Size: %llu"
722c66ac9dbSNicholas Bellinger 					" bytes\n", fd_dev->fd_dev_size);
723c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
724c66ac9dbSNicholas Bellinger 			break;
725b32f4c7eSNicholas Bellinger 		case Opt_fd_buffered_io:
726b32f4c7eSNicholas Bellinger 			match_int(args, &arg);
727b32f4c7eSNicholas Bellinger 			if (arg != 1) {
728b32f4c7eSNicholas Bellinger 				pr_err("bogus fd_buffered_io=%d value\n", arg);
729b32f4c7eSNicholas Bellinger 				ret = -EINVAL;
730b32f4c7eSNicholas Bellinger 				goto out;
731b32f4c7eSNicholas Bellinger 			}
732b32f4c7eSNicholas Bellinger 
733b32f4c7eSNicholas Bellinger 			pr_debug("FILEIO: Using buffered I/O"
734b32f4c7eSNicholas Bellinger 				" operations for struct fd_dev\n");
735b32f4c7eSNicholas Bellinger 
736b32f4c7eSNicholas Bellinger 			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
737b32f4c7eSNicholas Bellinger 			break;
738c66ac9dbSNicholas Bellinger 		default:
739c66ac9dbSNicholas Bellinger 			break;
740c66ac9dbSNicholas Bellinger 		}
741c66ac9dbSNicholas Bellinger 	}
742c66ac9dbSNicholas Bellinger 
743c66ac9dbSNicholas Bellinger out:
744c66ac9dbSNicholas Bellinger 	kfree(orig);
745c66ac9dbSNicholas Bellinger 	return (!ret) ? count : ret;
746c66ac9dbSNicholas Bellinger }
747c66ac9dbSNicholas Bellinger 
7480fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
749c66ac9dbSNicholas Bellinger {
7500fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
751c66ac9dbSNicholas Bellinger 	ssize_t bl = 0;
752c66ac9dbSNicholas Bellinger 
753c66ac9dbSNicholas Bellinger 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
754b32f4c7eSNicholas Bellinger 	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s\n",
755b32f4c7eSNicholas Bellinger 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
756b32f4c7eSNicholas Bellinger 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
757b32f4c7eSNicholas Bellinger 		"Buffered-WCE" : "O_DSYNC");
758c66ac9dbSNicholas Bellinger 	return bl;
759c66ac9dbSNicholas Bellinger }
760c66ac9dbSNicholas Bellinger 
761c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev)
762c66ac9dbSNicholas Bellinger {
7630fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
764cd9323fdSNicholas Bellinger 	struct file *f = fd_dev->fd_file;
765cd9323fdSNicholas Bellinger 	struct inode *i = f->f_mapping->host;
766cd9323fdSNicholas Bellinger 	unsigned long long dev_size;
767cd9323fdSNicholas Bellinger 	/*
768cd9323fdSNicholas Bellinger 	 * When using a file that references an underlying struct block_device,
769cd9323fdSNicholas Bellinger 	 * ensure dev_size is always based on the current inode size in order
770cd9323fdSNicholas Bellinger 	 * to handle underlying block_device resize operations.
771cd9323fdSNicholas Bellinger 	 */
772cd9323fdSNicholas Bellinger 	if (S_ISBLK(i->i_mode))
773cd9323fdSNicholas Bellinger 		dev_size = (i_size_read(i) - fd_dev->fd_block_size);
774cd9323fdSNicholas Bellinger 	else
775cd9323fdSNicholas Bellinger 		dev_size = fd_dev->fd_dev_size;
776c66ac9dbSNicholas Bellinger 
7770fd97ccfSChristoph Hellwig 	return div_u64(dev_size, dev->dev_attrib.block_size);
778c66ac9dbSNicholas Bellinger }
779c66ac9dbSNicholas Bellinger 
7809e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = {
7810c2ad7d1SChristoph Hellwig 	.execute_rw		= fd_execute_rw,
782ad67f0d9SChristoph Hellwig 	.execute_sync_cache	= fd_execute_sync_cache,
7837b745c84SNicholas Bellinger 	.execute_write_same	= fd_execute_write_same,
78470d3ae5cSAsias He 	.execute_write_same_unmap = fd_execute_write_same_unmap,
78550642762SAsias He 	.execute_unmap		= fd_execute_unmap,
7860c2ad7d1SChristoph Hellwig };
7870c2ad7d1SChristoph Hellwig 
788de103c93SChristoph Hellwig static sense_reason_t
789de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd)
7900c2ad7d1SChristoph Hellwig {
7919e999a6cSChristoph Hellwig 	return sbc_parse_cdb(cmd, &fd_sbc_ops);
7920c2ad7d1SChristoph Hellwig }
7930c2ad7d1SChristoph Hellwig 
794c66ac9dbSNicholas Bellinger static struct se_subsystem_api fileio_template = {
795c66ac9dbSNicholas Bellinger 	.name			= "fileio",
7960fd97ccfSChristoph Hellwig 	.inquiry_prod		= "FILEIO",
7970fd97ccfSChristoph Hellwig 	.inquiry_rev		= FD_VERSION,
798c66ac9dbSNicholas Bellinger 	.owner			= THIS_MODULE,
799c66ac9dbSNicholas Bellinger 	.transport_type		= TRANSPORT_PLUGIN_VHBA_PDEV,
800c66ac9dbSNicholas Bellinger 	.attach_hba		= fd_attach_hba,
801c66ac9dbSNicholas Bellinger 	.detach_hba		= fd_detach_hba,
8020fd97ccfSChristoph Hellwig 	.alloc_device		= fd_alloc_device,
8030fd97ccfSChristoph Hellwig 	.configure_device	= fd_configure_device,
804c66ac9dbSNicholas Bellinger 	.free_device		= fd_free_device,
8050c2ad7d1SChristoph Hellwig 	.parse_cdb		= fd_parse_cdb,
806c66ac9dbSNicholas Bellinger 	.set_configfs_dev_params = fd_set_configfs_dev_params,
807c66ac9dbSNicholas Bellinger 	.show_configfs_dev_params = fd_show_configfs_dev_params,
8086f23ac8aSChristoph Hellwig 	.get_device_type	= sbc_get_device_type,
809c66ac9dbSNicholas Bellinger 	.get_blocks		= fd_get_blocks,
810c66ac9dbSNicholas Bellinger };
811c66ac9dbSNicholas Bellinger 
812c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void)
813c66ac9dbSNicholas Bellinger {
814c66ac9dbSNicholas Bellinger 	return transport_subsystem_register(&fileio_template);
815c66ac9dbSNicholas Bellinger }
816c66ac9dbSNicholas Bellinger 
81763b91d5aSAsias He static void __exit fileio_module_exit(void)
818c66ac9dbSNicholas Bellinger {
819c66ac9dbSNicholas Bellinger 	transport_subsystem_release(&fileio_template);
820c66ac9dbSNicholas Bellinger }
821c66ac9dbSNicholas Bellinger 
822c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
823c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
824c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
825c66ac9dbSNicholas Bellinger 
826c66ac9dbSNicholas Bellinger module_init(fileio_module_init);
827c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit);
828