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  *
64c76251eSNicholas Bellinger  * (c) Copyright 2005-2013 Datera, Inc.
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>
335538d294SDavid S. Miller #include <linux/vmalloc.h>
3470d3ae5cSAsias He #include <linux/falloc.h>
35ba929992SBart Van Assche #include <scsi/scsi_proto.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 static int fd_attach_hba(struct se_hba *hba, u32 host_id)
49c66ac9dbSNicholas Bellinger {
50c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host;
51c66ac9dbSNicholas Bellinger 
52c66ac9dbSNicholas Bellinger 	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
536708bb27SAndy Grover 	if (!fd_host) {
546708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_host\n");
55e3d6f909SAndy Grover 		return -ENOMEM;
56c66ac9dbSNicholas Bellinger 	}
57c66ac9dbSNicholas Bellinger 
58c66ac9dbSNicholas Bellinger 	fd_host->fd_host_id = host_id;
59c66ac9dbSNicholas Bellinger 
60e3d6f909SAndy Grover 	hba->hba_ptr = fd_host;
61c66ac9dbSNicholas Bellinger 
626708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
63c66ac9dbSNicholas Bellinger 		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
64ce8dd25dSChristoph Hellwig 		TARGET_CORE_VERSION);
6595cadaceSNicholas Bellinger 	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
6695cadaceSNicholas Bellinger 		hba->hba_id, fd_host->fd_host_id);
67c66ac9dbSNicholas Bellinger 
68c66ac9dbSNicholas Bellinger 	return 0;
69c66ac9dbSNicholas Bellinger }
70c66ac9dbSNicholas Bellinger 
71c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba)
72c66ac9dbSNicholas Bellinger {
73c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host = hba->hba_ptr;
74c66ac9dbSNicholas Bellinger 
756708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
76c66ac9dbSNicholas Bellinger 		" Target Core\n", hba->hba_id, fd_host->fd_host_id);
77c66ac9dbSNicholas Bellinger 
78c66ac9dbSNicholas Bellinger 	kfree(fd_host);
79c66ac9dbSNicholas Bellinger 	hba->hba_ptr = NULL;
80c66ac9dbSNicholas Bellinger }
81c66ac9dbSNicholas Bellinger 
820fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
83c66ac9dbSNicholas Bellinger {
84c66ac9dbSNicholas Bellinger 	struct fd_dev *fd_dev;
858359cf43SJörn Engel 	struct fd_host *fd_host = hba->hba_ptr;
86c66ac9dbSNicholas Bellinger 
87c66ac9dbSNicholas Bellinger 	fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
886708bb27SAndy Grover 	if (!fd_dev) {
896708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_dev\n");
90c66ac9dbSNicholas Bellinger 		return NULL;
91c66ac9dbSNicholas Bellinger 	}
92c66ac9dbSNicholas Bellinger 
93c66ac9dbSNicholas Bellinger 	fd_dev->fd_host = fd_host;
94c66ac9dbSNicholas Bellinger 
956708bb27SAndy Grover 	pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
96c66ac9dbSNicholas Bellinger 
970fd97ccfSChristoph Hellwig 	return &fd_dev->dev;
98c66ac9dbSNicholas Bellinger }
99c66ac9dbSNicholas Bellinger 
1000fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev)
101c66ac9dbSNicholas Bellinger {
1020fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
1030fd97ccfSChristoph Hellwig 	struct fd_host *fd_host = dev->se_hba->hba_ptr;
104c66ac9dbSNicholas Bellinger 	struct file *file;
105c66ac9dbSNicholas Bellinger 	struct inode *inode = NULL;
1060fd97ccfSChristoph Hellwig 	int flags, ret = -EINVAL;
107c66ac9dbSNicholas Bellinger 
1080fd97ccfSChristoph Hellwig 	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
1090fd97ccfSChristoph Hellwig 		pr_err("Missing fd_dev_name=\n");
1100fd97ccfSChristoph Hellwig 		return -EINVAL;
1110fd97ccfSChristoph Hellwig 	}
112c66ac9dbSNicholas Bellinger 
113c66ac9dbSNicholas Bellinger 	/*
114a4dff304SNicholas Bellinger 	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
115a4dff304SNicholas Bellinger 	 * of pure timestamp updates.
116c66ac9dbSNicholas Bellinger 	 */
117a4dff304SNicholas Bellinger 	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
1180fd97ccfSChristoph Hellwig 
119b32f4c7eSNicholas Bellinger 	/*
120b32f4c7eSNicholas Bellinger 	 * Optionally allow fd_buffered_io=1 to be enabled for people
121b32f4c7eSNicholas Bellinger 	 * who want use the fs buffer cache as an WriteCache mechanism.
122b32f4c7eSNicholas Bellinger 	 *
123b32f4c7eSNicholas Bellinger 	 * This means that in event of a hard failure, there is a risk
124b32f4c7eSNicholas Bellinger 	 * of silent data-loss if the SCSI client has *not* performed a
125b32f4c7eSNicholas Bellinger 	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
126b32f4c7eSNicholas Bellinger 	 * to write-out the entire device cache.
127b32f4c7eSNicholas Bellinger 	 */
128b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
129b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
130b32f4c7eSNicholas Bellinger 		flags &= ~O_DSYNC;
131b32f4c7eSNicholas Bellinger 	}
132c66ac9dbSNicholas Bellinger 
133dbc6e022SAl Viro 	file = filp_open(fd_dev->fd_dev_name, flags, 0600);
134613640e4SNicholas Bellinger 	if (IS_ERR(file)) {
135dbc6e022SAl Viro 		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
136613640e4SNicholas Bellinger 		ret = PTR_ERR(file);
137613640e4SNicholas Bellinger 		goto fail;
138613640e4SNicholas Bellinger 	}
139c66ac9dbSNicholas Bellinger 	fd_dev->fd_file = file;
140c66ac9dbSNicholas Bellinger 	/*
141c66ac9dbSNicholas Bellinger 	 * If using a block backend with this struct file, we extract
142c66ac9dbSNicholas Bellinger 	 * fd_dev->fd_[block,dev]_size from struct block_device.
143c66ac9dbSNicholas Bellinger 	 *
144c66ac9dbSNicholas Bellinger 	 * Otherwise, we use the passed fd_size= from configfs
145c66ac9dbSNicholas Bellinger 	 */
146c66ac9dbSNicholas Bellinger 	inode = file->f_mapping->host;
147c66ac9dbSNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
1480fd97ccfSChristoph Hellwig 		struct request_queue *q = bdev_get_queue(inode->i_bdev);
149cd9323fdSNicholas Bellinger 		unsigned long long dev_size;
1500fd97ccfSChristoph Hellwig 
15121363ca8SNicholas Bellinger 		fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
152c66ac9dbSNicholas Bellinger 		/*
153c66ac9dbSNicholas Bellinger 		 * Determine the number of bytes from i_size_read() minus
154c66ac9dbSNicholas Bellinger 		 * one (1) logical sector from underlying struct block_device
155c66ac9dbSNicholas Bellinger 		 */
156cd9323fdSNicholas Bellinger 		dev_size = (i_size_read(file->f_mapping->host) -
157c66ac9dbSNicholas Bellinger 				       fd_dev->fd_block_size);
158c66ac9dbSNicholas Bellinger 
1596708bb27SAndy Grover 		pr_debug("FILEIO: Using size: %llu bytes from struct"
160c66ac9dbSNicholas Bellinger 			" block_device blocks: %llu logical_block_size: %d\n",
161cd9323fdSNicholas Bellinger 			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
162c66ac9dbSNicholas Bellinger 			fd_dev->fd_block_size);
1638a9ebe71SMike Christie 
1648a9ebe71SMike Christie 		if (target_configure_unmap_from_queue(&dev->dev_attrib, q,
1658a9ebe71SMike Christie 						      fd_dev->fd_block_size))
16670d3ae5cSAsias He 			pr_debug("IFILE: BLOCK Discard support available,"
16770d3ae5cSAsias He 				 " disabled by default\n");
16870d3ae5cSAsias He 		/*
16970d3ae5cSAsias He 		 * Enable write same emulation for IBLOCK and use 0xFFFF as
17070d3ae5cSAsias He 		 * the smaller WRITE_SAME(10) only has a two-byte block count.
17170d3ae5cSAsias He 		 */
17270d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0xFFFF;
1730463a3feSAsias He 
1740463a3feSAsias He 		if (blk_queue_nonrot(q))
1750463a3feSAsias He 			dev->dev_attrib.is_nonrot = 1;
176c66ac9dbSNicholas Bellinger 	} else {
177c66ac9dbSNicholas Bellinger 		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
1786708bb27SAndy Grover 			pr_err("FILEIO: Missing fd_dev_size="
179c66ac9dbSNicholas Bellinger 				" parameter, and no backing struct"
180c66ac9dbSNicholas Bellinger 				" block_device\n");
181c66ac9dbSNicholas Bellinger 			goto fail;
182c66ac9dbSNicholas Bellinger 		}
183c66ac9dbSNicholas Bellinger 
18421363ca8SNicholas Bellinger 		fd_dev->fd_block_size = FD_BLOCKSIZE;
18570d3ae5cSAsias He 		/*
18670d3ae5cSAsias He 		 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
18770d3ae5cSAsias He 		 */
18870d3ae5cSAsias He 		dev->dev_attrib.max_unmap_lba_count = 0x2000;
18970d3ae5cSAsias He 		/*
19070d3ae5cSAsias He 		 * Currently hardcoded to 1 in Linux/SCSI code..
19170d3ae5cSAsias He 		 */
19270d3ae5cSAsias He 		dev->dev_attrib.max_unmap_block_desc_count = 1;
19370d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity = 1;
19470d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity_alignment = 0;
19570d3ae5cSAsias He 
19670d3ae5cSAsias He 		/*
19770d3ae5cSAsias He 		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
19870d3ae5cSAsias He 		 * based upon struct iovec limit for vfs_writev()
19970d3ae5cSAsias He 		 */
20070d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0x1000;
201c66ac9dbSNicholas Bellinger 	}
202c66ac9dbSNicholas Bellinger 
20321363ca8SNicholas Bellinger 	dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
20495cadaceSNicholas Bellinger 	dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
20595cadaceSNicholas Bellinger 	dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
2060fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
207c66ac9dbSNicholas Bellinger 
208b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
209b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
210b32f4c7eSNicholas Bellinger 			" with FDBD_HAS_BUFFERED_IO_WCE\n");
2110fd97ccfSChristoph Hellwig 		dev->dev_attrib.emulate_write_cache = 1;
212b32f4c7eSNicholas Bellinger 	}
213b32f4c7eSNicholas Bellinger 
214c66ac9dbSNicholas Bellinger 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
215c66ac9dbSNicholas Bellinger 	fd_dev->fd_queue_depth = dev->queue_depth;
216c66ac9dbSNicholas Bellinger 
2176708bb27SAndy Grover 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
218c66ac9dbSNicholas Bellinger 		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
219c66ac9dbSNicholas Bellinger 			fd_dev->fd_dev_name, fd_dev->fd_dev_size);
220c66ac9dbSNicholas Bellinger 
2210fd97ccfSChristoph Hellwig 	return 0;
222c66ac9dbSNicholas Bellinger fail:
223c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
224c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
225c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
226c66ac9dbSNicholas Bellinger 	}
2270fd97ccfSChristoph Hellwig 	return ret;
228c66ac9dbSNicholas Bellinger }
229c66ac9dbSNicholas Bellinger 
2304cc987eaSNicholas Bellinger static void fd_dev_call_rcu(struct rcu_head *p)
2314cc987eaSNicholas Bellinger {
2324cc987eaSNicholas Bellinger 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
2334cc987eaSNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
2344cc987eaSNicholas Bellinger 
2354cc987eaSNicholas Bellinger 	kfree(fd_dev);
2364cc987eaSNicholas Bellinger }
2374cc987eaSNicholas Bellinger 
2380fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev)
239c66ac9dbSNicholas Bellinger {
2400fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
241c66ac9dbSNicholas Bellinger 
242c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
243c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
244c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
245c66ac9dbSNicholas Bellinger 	}
2464cc987eaSNicholas Bellinger 	call_rcu(&dev->rcu_head, fd_dev_call_rcu);
247c66ac9dbSNicholas Bellinger }
248c66ac9dbSNicholas Bellinger 
2498287fa5fSSagi Grimberg static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
2508287fa5fSSagi Grimberg 		    u32 block_size, struct scatterlist *sgl,
2518287fa5fSSagi Grimberg 		    u32 sgl_nents, u32 data_length, int is_write)
25242201b55SNicholas Bellinger {
2535787cacdSChristoph Hellwig 	struct scatterlist *sg;
2546b9a44d0SChristoph Hellwig 	struct iov_iter iter;
2556b9a44d0SChristoph Hellwig 	struct bio_vec *bvec;
2566b9a44d0SChristoph Hellwig 	ssize_t len = 0;
2578287fa5fSSagi Grimberg 	loff_t pos = (cmd->t_task_lba * block_size);
258c66ac9dbSNicholas Bellinger 	int ret = 0, i;
259c66ac9dbSNicholas Bellinger 
2606b9a44d0SChristoph Hellwig 	bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
2616b9a44d0SChristoph Hellwig 	if (!bvec) {
2626708bb27SAndy Grover 		pr_err("Unable to allocate fd_do_readv iov[]\n");
263e3d6f909SAndy Grover 		return -ENOMEM;
264c66ac9dbSNicholas Bellinger 	}
265c66ac9dbSNicholas Bellinger 
2665787cacdSChristoph Hellwig 	for_each_sg(sgl, sg, sgl_nents, i) {
2676b9a44d0SChristoph Hellwig 		bvec[i].bv_page = sg_page(sg);
2686b9a44d0SChristoph Hellwig 		bvec[i].bv_len = sg->length;
2696b9a44d0SChristoph Hellwig 		bvec[i].bv_offset = sg->offset;
2706b9a44d0SChristoph Hellwig 
2716b9a44d0SChristoph Hellwig 		len += sg->length;
272c66ac9dbSNicholas Bellinger 	}
273c66ac9dbSNicholas Bellinger 
2746b9a44d0SChristoph Hellwig 	iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len);
275778229afSSebastian Andrzej Siewior 	if (is_write)
2766b9a44d0SChristoph Hellwig 		ret = vfs_iter_write(fd, &iter, &pos);
277778229afSSebastian Andrzej Siewior 	else
2786b9a44d0SChristoph Hellwig 		ret = vfs_iter_read(fd, &iter, &pos);
279778229afSSebastian Andrzej Siewior 
2806b9a44d0SChristoph Hellwig 	kfree(bvec);
281778229afSSebastian Andrzej Siewior 
282778229afSSebastian Andrzej Siewior 	if (is_write) {
2838287fa5fSSagi Grimberg 		if (ret < 0 || ret != data_length) {
284778229afSSebastian Andrzej Siewior 			pr_err("%s() write returned %d\n", __func__, ret);
285778229afSSebastian Andrzej Siewior 			return (ret < 0 ? ret : -EINVAL);
286778229afSSebastian Andrzej Siewior 		}
287778229afSSebastian Andrzej Siewior 	} else {
288c66ac9dbSNicholas Bellinger 		/*
289c66ac9dbSNicholas Bellinger 		 * Return zeros and GOOD status even if the READ did not return
290c66ac9dbSNicholas Bellinger 		 * the expected virt_size for struct file w/o a backing struct
291c66ac9dbSNicholas Bellinger 		 * block_device.
292c66ac9dbSNicholas Bellinger 		 */
293496ad9aaSAl Viro 		if (S_ISBLK(file_inode(fd)->i_mode)) {
2948287fa5fSSagi Grimberg 			if (ret < 0 || ret != data_length) {
295778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d, expecting %u for "
296778229afSSebastian Andrzej Siewior 						"S_ISBLK\n", __func__, ret,
2978287fa5fSSagi Grimberg 						data_length);
298e3d6f909SAndy Grover 				return (ret < 0 ? ret : -EINVAL);
299c66ac9dbSNicholas Bellinger 			}
300c66ac9dbSNicholas Bellinger 		} else {
301c66ac9dbSNicholas Bellinger 			if (ret < 0) {
302778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d for non S_ISBLK\n",
303778229afSSebastian Andrzej Siewior 						__func__, ret);
304e3d6f909SAndy Grover 				return ret;
305c66ac9dbSNicholas Bellinger 			}
306c66ac9dbSNicholas Bellinger 		}
307c66ac9dbSNicholas Bellinger 	}
308c66ac9dbSNicholas Bellinger 	return 1;
309c66ac9dbSNicholas Bellinger }
310c66ac9dbSNicholas Bellinger 
311de103c93SChristoph Hellwig static sense_reason_t
312de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd)
313c66ac9dbSNicholas Bellinger {
314c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3150fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
316a1d8b49aSAndy Grover 	int immed = (cmd->t_task_cdb[1] & 0x2);
317c66ac9dbSNicholas Bellinger 	loff_t start, end;
318c66ac9dbSNicholas Bellinger 	int ret;
319c66ac9dbSNicholas Bellinger 
320c66ac9dbSNicholas Bellinger 	/*
321c66ac9dbSNicholas Bellinger 	 * If the Immediate bit is set, queue up the GOOD response
322c66ac9dbSNicholas Bellinger 	 * for this SYNCHRONIZE_CACHE op
323c66ac9dbSNicholas Bellinger 	 */
324c66ac9dbSNicholas Bellinger 	if (immed)
3255787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
326c66ac9dbSNicholas Bellinger 
327c66ac9dbSNicholas Bellinger 	/*
328c66ac9dbSNicholas Bellinger 	 * Determine if we will be flushing the entire device.
329c66ac9dbSNicholas Bellinger 	 */
330a1d8b49aSAndy Grover 	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
331c66ac9dbSNicholas Bellinger 		start = 0;
332c66ac9dbSNicholas Bellinger 		end = LLONG_MAX;
333c66ac9dbSNicholas Bellinger 	} else {
3340fd97ccfSChristoph Hellwig 		start = cmd->t_task_lba * dev->dev_attrib.block_size;
335c66ac9dbSNicholas Bellinger 		if (cmd->data_length)
33662d3ab49SZach Brown 			end = start + cmd->data_length - 1;
337c66ac9dbSNicholas Bellinger 		else
338c66ac9dbSNicholas Bellinger 			end = LLONG_MAX;
339c66ac9dbSNicholas Bellinger 	}
340c66ac9dbSNicholas Bellinger 
341c66ac9dbSNicholas Bellinger 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
342c66ac9dbSNicholas Bellinger 	if (ret != 0)
3436708bb27SAndy Grover 		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
344c66ac9dbSNicholas Bellinger 
3455787cacdSChristoph Hellwig 	if (immed)
346ad67f0d9SChristoph Hellwig 		return 0;
3475787cacdSChristoph Hellwig 
348de103c93SChristoph Hellwig 	if (ret)
3495787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
350de103c93SChristoph Hellwig 	else
3515787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
352ad67f0d9SChristoph Hellwig 
353ad67f0d9SChristoph Hellwig 	return 0;
354c66ac9dbSNicholas Bellinger }
355c66ac9dbSNicholas Bellinger 
3567b745c84SNicholas Bellinger static sense_reason_t
3577b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd)
3587b745c84SNicholas Bellinger {
3597b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
3607b745c84SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(se_dev);
3617b745c84SNicholas Bellinger 	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
362d4c5dcacSChristoph Hellwig 	sector_t nolb = sbc_get_write_same_sectors(cmd);
363d4c5dcacSChristoph Hellwig 	struct iov_iter iter;
364d4c5dcacSChristoph Hellwig 	struct bio_vec *bvec;
365d4c5dcacSChristoph Hellwig 	unsigned int len = 0, i;
366d4c5dcacSChristoph Hellwig 	ssize_t ret;
3677b745c84SNicholas Bellinger 
3687b745c84SNicholas Bellinger 	if (!nolb) {
3697b745c84SNicholas Bellinger 		target_complete_cmd(cmd, SAM_STAT_GOOD);
3707b745c84SNicholas Bellinger 		return 0;
3717b745c84SNicholas Bellinger 	}
372afd73f1bSNicholas Bellinger 	if (cmd->prot_op) {
373afd73f1bSNicholas Bellinger 		pr_err("WRITE_SAME: Protection information with FILEIO"
374afd73f1bSNicholas Bellinger 		       " backends not supported\n");
375afd73f1bSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
376afd73f1bSNicholas Bellinger 	}
3777b745c84SNicholas Bellinger 
3787b745c84SNicholas Bellinger 	if (cmd->t_data_nents > 1 ||
379d4c5dcacSChristoph Hellwig 	    cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {
3807b745c84SNicholas Bellinger 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
381d4c5dcacSChristoph Hellwig 			" block_size: %u\n",
382d4c5dcacSChristoph Hellwig 			cmd->t_data_nents,
383d4c5dcacSChristoph Hellwig 			cmd->t_data_sg[0].length,
3847b745c84SNicholas Bellinger 			cmd->se_dev->dev_attrib.block_size);
3857b745c84SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
3867b745c84SNicholas Bellinger 	}
3877b745c84SNicholas Bellinger 
388d4c5dcacSChristoph Hellwig 	bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
389d4c5dcacSChristoph Hellwig 	if (!bvec)
3907b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3917b745c84SNicholas Bellinger 
392d4c5dcacSChristoph Hellwig 	for (i = 0; i < nolb; i++) {
393d4c5dcacSChristoph Hellwig 		bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]);
394d4c5dcacSChristoph Hellwig 		bvec[i].bv_len = cmd->t_data_sg[0].length;
395d4c5dcacSChristoph Hellwig 		bvec[i].bv_offset = cmd->t_data_sg[0].offset;
396d4c5dcacSChristoph Hellwig 
397d4c5dcacSChristoph Hellwig 		len += se_dev->dev_attrib.block_size;
3987b745c84SNicholas Bellinger 	}
3997b745c84SNicholas Bellinger 
400d4c5dcacSChristoph Hellwig 	iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len);
401d4c5dcacSChristoph Hellwig 	ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos);
4027b745c84SNicholas Bellinger 
403d4c5dcacSChristoph Hellwig 	kfree(bvec);
404d4c5dcacSChristoph Hellwig 	if (ret < 0 || ret != len) {
405d4c5dcacSChristoph Hellwig 		pr_err("vfs_iter_write() returned %zd for write same\n", ret);
4067b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4077b745c84SNicholas Bellinger 	}
4087b745c84SNicholas Bellinger 
4097b745c84SNicholas Bellinger 	target_complete_cmd(cmd, SAM_STAT_GOOD);
4107b745c84SNicholas Bellinger 	return 0;
4117b745c84SNicholas Bellinger }
4127b745c84SNicholas Bellinger 
41364d240b7SAkinobu Mita static int
41464d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
41564d240b7SAkinobu Mita 		void *buf, size_t bufsize)
41664d240b7SAkinobu Mita {
41764d240b7SAkinobu Mita 	struct fd_dev *fd_dev = FD_DEV(se_dev);
41864d240b7SAkinobu Mita 	struct file *prot_fd = fd_dev->fd_prot_file;
41964d240b7SAkinobu Mita 	sector_t prot_length, prot;
42064d240b7SAkinobu Mita 	loff_t pos = lba * se_dev->prot_length;
42164d240b7SAkinobu Mita 
42264d240b7SAkinobu Mita 	if (!prot_fd) {
42364d240b7SAkinobu Mita 		pr_err("Unable to locate fd_dev->fd_prot_file\n");
42464d240b7SAkinobu Mita 		return -ENODEV;
42564d240b7SAkinobu Mita 	}
42664d240b7SAkinobu Mita 
42764d240b7SAkinobu Mita 	prot_length = nolb * se_dev->prot_length;
42864d240b7SAkinobu Mita 
42964d240b7SAkinobu Mita 	for (prot = 0; prot < prot_length;) {
43064d240b7SAkinobu Mita 		sector_t len = min_t(sector_t, bufsize, prot_length - prot);
43164d240b7SAkinobu Mita 		ssize_t ret = kernel_write(prot_fd, buf, len, pos + prot);
43264d240b7SAkinobu Mita 
43364d240b7SAkinobu Mita 		if (ret != len) {
43464d240b7SAkinobu Mita 			pr_err("vfs_write to prot file failed: %zd\n", ret);
43564d240b7SAkinobu Mita 			return ret < 0 ? ret : -ENODEV;
43664d240b7SAkinobu Mita 		}
43764d240b7SAkinobu Mita 		prot += ret;
43864d240b7SAkinobu Mita 	}
43964d240b7SAkinobu Mita 
44064d240b7SAkinobu Mita 	return 0;
44164d240b7SAkinobu Mita }
44264d240b7SAkinobu Mita 
44364d240b7SAkinobu Mita static int
44464d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
44564d240b7SAkinobu Mita {
44664d240b7SAkinobu Mita 	void *buf;
44764d240b7SAkinobu Mita 	int rc;
44864d240b7SAkinobu Mita 
44964d240b7SAkinobu Mita 	buf = (void *)__get_free_page(GFP_KERNEL);
45064d240b7SAkinobu Mita 	if (!buf) {
45164d240b7SAkinobu Mita 		pr_err("Unable to allocate FILEIO prot buf\n");
45264d240b7SAkinobu Mita 		return -ENOMEM;
45364d240b7SAkinobu Mita 	}
45464d240b7SAkinobu Mita 	memset(buf, 0xff, PAGE_SIZE);
45564d240b7SAkinobu Mita 
45664d240b7SAkinobu Mita 	rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
45764d240b7SAkinobu Mita 
45864d240b7SAkinobu Mita 	free_page((unsigned long)buf);
45964d240b7SAkinobu Mita 
46064d240b7SAkinobu Mita 	return rc;
46164d240b7SAkinobu Mita }
46264d240b7SAkinobu Mita 
463de103c93SChristoph Hellwig static sense_reason_t
46462e46942SChristoph Hellwig fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
46570d3ae5cSAsias He {
46662e46942SChristoph Hellwig 	struct file *file = FD_DEV(cmd->se_dev)->fd_file;
46770d3ae5cSAsias He 	struct inode *inode = file->f_mapping->host;
46870d3ae5cSAsias He 	int ret;
46970d3ae5cSAsias He 
47064d240b7SAkinobu Mita 	if (cmd->se_dev->dev_attrib.pi_prot_type) {
47164d240b7SAkinobu Mita 		ret = fd_do_prot_unmap(cmd, lba, nolb);
47264d240b7SAkinobu Mita 		if (ret)
47364d240b7SAkinobu Mita 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
47464d240b7SAkinobu Mita 	}
47564d240b7SAkinobu Mita 
47670d3ae5cSAsias He 	if (S_ISBLK(inode->i_mode)) {
47770d3ae5cSAsias He 		/* The backend is block device, use discard */
47870d3ae5cSAsias He 		struct block_device *bdev = inode->i_bdev;
4798a9ebe71SMike Christie 		struct se_device *dev = cmd->se_dev;
48070d3ae5cSAsias He 
4818a9ebe71SMike Christie 		ret = blkdev_issue_discard(bdev,
4828a9ebe71SMike Christie 					   target_to_linux_sector(dev, lba),
4838a9ebe71SMike Christie 					   target_to_linux_sector(dev,  nolb),
4848a9ebe71SMike Christie 					   GFP_KERNEL, 0);
48570d3ae5cSAsias He 		if (ret < 0) {
48670d3ae5cSAsias He 			pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
48770d3ae5cSAsias He 				ret);
48870d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
48970d3ae5cSAsias He 		}
49070d3ae5cSAsias He 	} else {
49170d3ae5cSAsias He 		/* The backend is normal file, use fallocate */
49243f55bbbSAsias He 		struct se_device *se_dev = cmd->se_dev;
49343f55bbbSAsias He 		loff_t pos = lba * se_dev->dev_attrib.block_size;
49470d3ae5cSAsias He 		unsigned int len = nolb * se_dev->dev_attrib.block_size;
49570d3ae5cSAsias He 		int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
49670d3ae5cSAsias He 
49770d3ae5cSAsias He 		if (!file->f_op->fallocate)
49870d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
49970d3ae5cSAsias He 
50070d3ae5cSAsias He 		ret = file->f_op->fallocate(file, mode, pos, len);
50170d3ae5cSAsias He 		if (ret < 0) {
50270d3ae5cSAsias He 			pr_warn("FILEIO: fallocate() failed: %d\n", ret);
50370d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
50470d3ae5cSAsias He 		}
50570d3ae5cSAsias He 	}
50670d3ae5cSAsias He 
50743f55bbbSAsias He 	return 0;
50843f55bbbSAsias He }
50943f55bbbSAsias He 
51043f55bbbSAsias He static sense_reason_t
511a82a9538SNicholas Bellinger fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
512a82a9538SNicholas Bellinger 	      enum dma_data_direction data_direction)
513c66ac9dbSNicholas Bellinger {
514c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
5158287fa5fSSagi Grimberg 	struct fd_dev *fd_dev = FD_DEV(dev);
5168287fa5fSSagi Grimberg 	struct file *file = fd_dev->fd_file;
5178287fa5fSSagi Grimberg 	struct file *pfile = fd_dev->fd_prot_file;
51842201b55SNicholas Bellinger 	sense_reason_t rc;
519c66ac9dbSNicholas Bellinger 	int ret = 0;
520046ba642SNicholas Bellinger 	/*
521046ba642SNicholas Bellinger 	 * We are currently limited by the number of iovecs (2048) per
522046ba642SNicholas Bellinger 	 * single vfs_[writev,readv] call.
523046ba642SNicholas Bellinger 	 */
524046ba642SNicholas Bellinger 	if (cmd->data_length > FD_MAX_BYTES) {
525046ba642SNicholas Bellinger 		pr_err("FILEIO: Not able to process I/O of %u bytes due to"
526046ba642SNicholas Bellinger 		       "FD_MAX_BYTES: %u iovec count limitiation\n",
527046ba642SNicholas Bellinger 			cmd->data_length, FD_MAX_BYTES);
528046ba642SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
529046ba642SNicholas Bellinger 	}
530c66ac9dbSNicholas Bellinger 	/*
531c66ac9dbSNicholas Bellinger 	 * Call vectorized fileio functions to map struct scatterlist
532c66ac9dbSNicholas Bellinger 	 * physical memory addresses to struct iovec virtual memory.
533c66ac9dbSNicholas Bellinger 	 */
5345787cacdSChristoph Hellwig 	if (data_direction == DMA_FROM_DEVICE) {
535ee920469SNicholas Bellinger 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5368287fa5fSSagi Grimberg 			ret = fd_do_rw(cmd, pfile, dev->prot_length,
5378287fa5fSSagi Grimberg 				       cmd->t_prot_sg, cmd->t_prot_nents,
5388287fa5fSSagi Grimberg 				       cmd->prot_length, 0);
53942201b55SNicholas Bellinger 			if (ret < 0)
54042201b55SNicholas Bellinger 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
54142201b55SNicholas Bellinger 		}
54242201b55SNicholas Bellinger 
5438287fa5fSSagi Grimberg 		ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
5448287fa5fSSagi Grimberg 			       sgl, sgl_nents, cmd->data_length, 0);
54542201b55SNicholas Bellinger 
546ee920469SNicholas Bellinger 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5478287fa5fSSagi Grimberg 			u32 sectors = cmd->data_length >>
5488287fa5fSSagi Grimberg 					ilog2(dev->dev_attrib.block_size);
54942201b55SNicholas Bellinger 
550f75b6faeSSagi Grimberg 			rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
551f75b6faeSSagi Grimberg 					    0, cmd->t_prot_sg, 0);
5528287fa5fSSagi Grimberg 			if (rc)
55342201b55SNicholas Bellinger 				return rc;
55442201b55SNicholas Bellinger 		}
555c66ac9dbSNicholas Bellinger 	} else {
556ee920469SNicholas Bellinger 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5578287fa5fSSagi Grimberg 			u32 sectors = cmd->data_length >>
5588287fa5fSSagi Grimberg 					ilog2(dev->dev_attrib.block_size);
55942201b55SNicholas Bellinger 
5608287fa5fSSagi Grimberg 			rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
5618287fa5fSSagi Grimberg 					    0, cmd->t_prot_sg, 0);
5628287fa5fSSagi Grimberg 			if (rc)
56342201b55SNicholas Bellinger 				return rc;
56442201b55SNicholas Bellinger 		}
56542201b55SNicholas Bellinger 
5668287fa5fSSagi Grimberg 		ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
5678287fa5fSSagi Grimberg 			       sgl, sgl_nents, cmd->data_length, 1);
568a4dff304SNicholas Bellinger 		/*
569125d0119SHannes Reinecke 		 * Perform implicit vfs_fsync_range() for fd_do_writev() ops
570a4dff304SNicholas Bellinger 		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
571a4dff304SNicholas Bellinger 		 * Allow this to happen independent of WCE=0 setting.
572a4dff304SNicholas Bellinger 		 */
573814e5b45SChristoph Hellwig 		if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) {
574a4dff304SNicholas Bellinger 			loff_t start = cmd->t_task_lba *
5750fd97ccfSChristoph Hellwig 				dev->dev_attrib.block_size;
57662d3ab49SZach Brown 			loff_t end;
57762d3ab49SZach Brown 
57862d3ab49SZach Brown 			if (cmd->data_length)
57962d3ab49SZach Brown 				end = start + cmd->data_length - 1;
58062d3ab49SZach Brown 			else
58162d3ab49SZach Brown 				end = LLONG_MAX;
582c66ac9dbSNicholas Bellinger 
583a4dff304SNicholas Bellinger 			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
584a4dff304SNicholas Bellinger 		}
585c66ac9dbSNicholas Bellinger 
586ee920469SNicholas Bellinger 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5878287fa5fSSagi Grimberg 			ret = fd_do_rw(cmd, pfile, dev->prot_length,
5888287fa5fSSagi Grimberg 				       cmd->t_prot_sg, cmd->t_prot_nents,
5898287fa5fSSagi Grimberg 				       cmd->prot_length, 1);
590de103c93SChristoph Hellwig 			if (ret < 0)
591de103c93SChristoph Hellwig 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
59242201b55SNicholas Bellinger 		}
59342201b55SNicholas Bellinger 	}
59442201b55SNicholas Bellinger 
5958287fa5fSSagi Grimberg 	if (ret < 0)
59642201b55SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
597de103c93SChristoph Hellwig 
5985787cacdSChristoph Hellwig 	if (ret)
5995787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
60003e98c9eSNicholas Bellinger 	return 0;
601c66ac9dbSNicholas Bellinger }
602c66ac9dbSNicholas Bellinger 
603c66ac9dbSNicholas Bellinger enum {
604c66ac9dbSNicholas Bellinger 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
605c66ac9dbSNicholas Bellinger };
606c66ac9dbSNicholas Bellinger 
607c66ac9dbSNicholas Bellinger static match_table_t tokens = {
608c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_name, "fd_dev_name=%s"},
609c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_size, "fd_dev_size=%s"},
610b32f4c7eSNicholas Bellinger 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
611c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
612c66ac9dbSNicholas Bellinger };
613c66ac9dbSNicholas Bellinger 
6140fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
615c66ac9dbSNicholas Bellinger 		const char *page, ssize_t count)
616c66ac9dbSNicholas Bellinger {
6170fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
618c66ac9dbSNicholas Bellinger 	char *orig, *ptr, *arg_p, *opts;
619c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
620b32f4c7eSNicholas Bellinger 	int ret = 0, arg, token;
621c66ac9dbSNicholas Bellinger 
622c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
623c66ac9dbSNicholas Bellinger 	if (!opts)
624c66ac9dbSNicholas Bellinger 		return -ENOMEM;
625c66ac9dbSNicholas Bellinger 
626c66ac9dbSNicholas Bellinger 	orig = opts;
627c66ac9dbSNicholas Bellinger 
62890c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
629c66ac9dbSNicholas Bellinger 		if (!*ptr)
630c66ac9dbSNicholas Bellinger 			continue;
631c66ac9dbSNicholas Bellinger 
632c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
633c66ac9dbSNicholas Bellinger 		switch (token) {
634c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_name:
635dbc6e022SAl Viro 			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
636dbc6e022SAl Viro 				FD_MAX_DEV_NAME) == 0) {
637dbc6e022SAl Viro 				ret = -EINVAL;
6386d180253SJesper Juhl 				break;
6396d180253SJesper Juhl 			}
6406708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Path: %s\n",
641c66ac9dbSNicholas Bellinger 					fd_dev->fd_dev_name);
642c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_PATH;
643c66ac9dbSNicholas Bellinger 			break;
644c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_size:
645c66ac9dbSNicholas Bellinger 			arg_p = match_strdup(&args[0]);
6466d180253SJesper Juhl 			if (!arg_p) {
6476d180253SJesper Juhl 				ret = -ENOMEM;
6486d180253SJesper Juhl 				break;
6496d180253SJesper Juhl 			}
65057103d7fSJingoo Han 			ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
6516d180253SJesper Juhl 			kfree(arg_p);
652c66ac9dbSNicholas Bellinger 			if (ret < 0) {
65357103d7fSJingoo Han 				pr_err("kstrtoull() failed for"
654c66ac9dbSNicholas Bellinger 						" fd_dev_size=\n");
655c66ac9dbSNicholas Bellinger 				goto out;
656c66ac9dbSNicholas Bellinger 			}
6576708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Size: %llu"
658c66ac9dbSNicholas Bellinger 					" bytes\n", fd_dev->fd_dev_size);
659c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
660c66ac9dbSNicholas Bellinger 			break;
661b32f4c7eSNicholas Bellinger 		case Opt_fd_buffered_io:
662ce31c1b0SJoern Engel 			ret = match_int(args, &arg);
663ce31c1b0SJoern Engel 			if (ret)
664ce31c1b0SJoern Engel 				goto out;
665b32f4c7eSNicholas Bellinger 			if (arg != 1) {
666b32f4c7eSNicholas Bellinger 				pr_err("bogus fd_buffered_io=%d value\n", arg);
667b32f4c7eSNicholas Bellinger 				ret = -EINVAL;
668b32f4c7eSNicholas Bellinger 				goto out;
669b32f4c7eSNicholas Bellinger 			}
670b32f4c7eSNicholas Bellinger 
671b32f4c7eSNicholas Bellinger 			pr_debug("FILEIO: Using buffered I/O"
672b32f4c7eSNicholas Bellinger 				" operations for struct fd_dev\n");
673b32f4c7eSNicholas Bellinger 
674b32f4c7eSNicholas Bellinger 			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
675b32f4c7eSNicholas Bellinger 			break;
676c66ac9dbSNicholas Bellinger 		default:
677c66ac9dbSNicholas Bellinger 			break;
678c66ac9dbSNicholas Bellinger 		}
679c66ac9dbSNicholas Bellinger 	}
680c66ac9dbSNicholas Bellinger 
681c66ac9dbSNicholas Bellinger out:
682c66ac9dbSNicholas Bellinger 	kfree(orig);
683c66ac9dbSNicholas Bellinger 	return (!ret) ? count : ret;
684c66ac9dbSNicholas Bellinger }
685c66ac9dbSNicholas Bellinger 
6860fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
687c66ac9dbSNicholas Bellinger {
6880fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
689c66ac9dbSNicholas Bellinger 	ssize_t bl = 0;
690c66ac9dbSNicholas Bellinger 
691c66ac9dbSNicholas Bellinger 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
692b32f4c7eSNicholas Bellinger 	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s\n",
693b32f4c7eSNicholas Bellinger 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
694b32f4c7eSNicholas Bellinger 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
695b32f4c7eSNicholas Bellinger 		"Buffered-WCE" : "O_DSYNC");
696c66ac9dbSNicholas Bellinger 	return bl;
697c66ac9dbSNicholas Bellinger }
698c66ac9dbSNicholas Bellinger 
699c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev)
700c66ac9dbSNicholas Bellinger {
7010fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
702cd9323fdSNicholas Bellinger 	struct file *f = fd_dev->fd_file;
703cd9323fdSNicholas Bellinger 	struct inode *i = f->f_mapping->host;
704cd9323fdSNicholas Bellinger 	unsigned long long dev_size;
705cd9323fdSNicholas Bellinger 	/*
706cd9323fdSNicholas Bellinger 	 * When using a file that references an underlying struct block_device,
707cd9323fdSNicholas Bellinger 	 * ensure dev_size is always based on the current inode size in order
708cd9323fdSNicholas Bellinger 	 * to handle underlying block_device resize operations.
709cd9323fdSNicholas Bellinger 	 */
710cd9323fdSNicholas Bellinger 	if (S_ISBLK(i->i_mode))
71121363ca8SNicholas Bellinger 		dev_size = i_size_read(i);
712cd9323fdSNicholas Bellinger 	else
713cd9323fdSNicholas Bellinger 		dev_size = fd_dev->fd_dev_size;
714c66ac9dbSNicholas Bellinger 
71521363ca8SNicholas Bellinger 	return div_u64(dev_size - dev->dev_attrib.block_size,
71621363ca8SNicholas Bellinger 		       dev->dev_attrib.block_size);
717c66ac9dbSNicholas Bellinger }
718c66ac9dbSNicholas Bellinger 
7190f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev)
7200f5e2ec4SNicholas Bellinger {
7210f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
7220f5e2ec4SNicholas Bellinger 	struct file *prot_file, *file = fd_dev->fd_file;
7230f5e2ec4SNicholas Bellinger 	struct inode *inode;
7240f5e2ec4SNicholas Bellinger 	int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
7250f5e2ec4SNicholas Bellinger 	char buf[FD_MAX_DEV_PROT_NAME];
7260f5e2ec4SNicholas Bellinger 
7270f5e2ec4SNicholas Bellinger 	if (!file) {
7280f5e2ec4SNicholas Bellinger 		pr_err("Unable to locate fd_dev->fd_file\n");
7290f5e2ec4SNicholas Bellinger 		return -ENODEV;
7300f5e2ec4SNicholas Bellinger 	}
7310f5e2ec4SNicholas Bellinger 
7320f5e2ec4SNicholas Bellinger 	inode = file->f_mapping->host;
7330f5e2ec4SNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
7340f5e2ec4SNicholas Bellinger 		pr_err("FILEIO Protection emulation only supported on"
7350f5e2ec4SNicholas Bellinger 		       " !S_ISBLK\n");
7360f5e2ec4SNicholas Bellinger 		return -ENOSYS;
7370f5e2ec4SNicholas Bellinger 	}
7380f5e2ec4SNicholas Bellinger 
7390f5e2ec4SNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
7400f5e2ec4SNicholas Bellinger 		flags &= ~O_DSYNC;
7410f5e2ec4SNicholas Bellinger 
7420f5e2ec4SNicholas Bellinger 	snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
7430f5e2ec4SNicholas Bellinger 		 fd_dev->fd_dev_name);
7440f5e2ec4SNicholas Bellinger 
7450f5e2ec4SNicholas Bellinger 	prot_file = filp_open(buf, flags, 0600);
7460f5e2ec4SNicholas Bellinger 	if (IS_ERR(prot_file)) {
7470f5e2ec4SNicholas Bellinger 		pr_err("filp_open(%s) failed\n", buf);
7480f5e2ec4SNicholas Bellinger 		ret = PTR_ERR(prot_file);
7490f5e2ec4SNicholas Bellinger 		return ret;
7500f5e2ec4SNicholas Bellinger 	}
7510f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = prot_file;
7520f5e2ec4SNicholas Bellinger 
7530f5e2ec4SNicholas Bellinger 	return 0;
7540f5e2ec4SNicholas Bellinger }
7550f5e2ec4SNicholas Bellinger 
7560f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev)
7570f5e2ec4SNicholas Bellinger {
7580f5e2ec4SNicholas Bellinger 	unsigned char *buf;
7590f5e2ec4SNicholas Bellinger 	int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
76064d240b7SAkinobu Mita 	int ret;
7610f5e2ec4SNicholas Bellinger 
7620f5e2ec4SNicholas Bellinger 	if (!dev->dev_attrib.pi_prot_type) {
7630f5e2ec4SNicholas Bellinger 		pr_err("Unable to format_prot while pi_prot_type == 0\n");
7640f5e2ec4SNicholas Bellinger 		return -ENODEV;
7650f5e2ec4SNicholas Bellinger 	}
7660f5e2ec4SNicholas Bellinger 
7670f5e2ec4SNicholas Bellinger 	buf = vzalloc(unit_size);
7680f5e2ec4SNicholas Bellinger 	if (!buf) {
7690f5e2ec4SNicholas Bellinger 		pr_err("Unable to allocate FILEIO prot buf\n");
7700f5e2ec4SNicholas Bellinger 		return -ENOMEM;
7710f5e2ec4SNicholas Bellinger 	}
7720f5e2ec4SNicholas Bellinger 
7730f5e2ec4SNicholas Bellinger 	pr_debug("Using FILEIO prot_length: %llu\n",
77464d240b7SAkinobu Mita 		 (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
77564d240b7SAkinobu Mita 					dev->prot_length);
7760f5e2ec4SNicholas Bellinger 
77780dcd0c1SSagi Grimberg 	memset(buf, 0xff, unit_size);
77864d240b7SAkinobu Mita 	ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
77964d240b7SAkinobu Mita 			      buf, unit_size);
7800f5e2ec4SNicholas Bellinger 	vfree(buf);
7810f5e2ec4SNicholas Bellinger 	return ret;
7820f5e2ec4SNicholas Bellinger }
7830f5e2ec4SNicholas Bellinger 
7840f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev)
7850f5e2ec4SNicholas Bellinger {
7860f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
7870f5e2ec4SNicholas Bellinger 
7880f5e2ec4SNicholas Bellinger 	if (!fd_dev->fd_prot_file)
7890f5e2ec4SNicholas Bellinger 		return;
7900f5e2ec4SNicholas Bellinger 
7910f5e2ec4SNicholas Bellinger 	filp_close(fd_dev->fd_prot_file, NULL);
7920f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = NULL;
7930f5e2ec4SNicholas Bellinger }
7940f5e2ec4SNicholas Bellinger 
7959e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = {
7960c2ad7d1SChristoph Hellwig 	.execute_rw		= fd_execute_rw,
797ad67f0d9SChristoph Hellwig 	.execute_sync_cache	= fd_execute_sync_cache,
7987b745c84SNicholas Bellinger 	.execute_write_same	= fd_execute_write_same,
79950642762SAsias He 	.execute_unmap		= fd_execute_unmap,
8000c2ad7d1SChristoph Hellwig };
8010c2ad7d1SChristoph Hellwig 
802de103c93SChristoph Hellwig static sense_reason_t
803de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd)
8040c2ad7d1SChristoph Hellwig {
8059e999a6cSChristoph Hellwig 	return sbc_parse_cdb(cmd, &fd_sbc_ops);
8060c2ad7d1SChristoph Hellwig }
8070c2ad7d1SChristoph Hellwig 
8080a06d430SChristoph Hellwig static const struct target_backend_ops fileio_ops = {
809c66ac9dbSNicholas Bellinger 	.name			= "fileio",
8100fd97ccfSChristoph Hellwig 	.inquiry_prod		= "FILEIO",
8110fd97ccfSChristoph Hellwig 	.inquiry_rev		= FD_VERSION,
812c66ac9dbSNicholas Bellinger 	.owner			= THIS_MODULE,
813c66ac9dbSNicholas Bellinger 	.attach_hba		= fd_attach_hba,
814c66ac9dbSNicholas Bellinger 	.detach_hba		= fd_detach_hba,
8150fd97ccfSChristoph Hellwig 	.alloc_device		= fd_alloc_device,
8160fd97ccfSChristoph Hellwig 	.configure_device	= fd_configure_device,
817c66ac9dbSNicholas Bellinger 	.free_device		= fd_free_device,
8180c2ad7d1SChristoph Hellwig 	.parse_cdb		= fd_parse_cdb,
819c66ac9dbSNicholas Bellinger 	.set_configfs_dev_params = fd_set_configfs_dev_params,
820c66ac9dbSNicholas Bellinger 	.show_configfs_dev_params = fd_show_configfs_dev_params,
8216f23ac8aSChristoph Hellwig 	.get_device_type	= sbc_get_device_type,
822c66ac9dbSNicholas Bellinger 	.get_blocks		= fd_get_blocks,
8230f5e2ec4SNicholas Bellinger 	.init_prot		= fd_init_prot,
8240f5e2ec4SNicholas Bellinger 	.format_prot		= fd_format_prot,
8250f5e2ec4SNicholas Bellinger 	.free_prot		= fd_free_prot,
8265873c4d1SChristoph Hellwig 	.tb_dev_attrib_attrs	= sbc_attrib_attrs,
827c66ac9dbSNicholas Bellinger };
828c66ac9dbSNicholas Bellinger 
829c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void)
830c66ac9dbSNicholas Bellinger {
8310a06d430SChristoph Hellwig 	return transport_backend_register(&fileio_ops);
832c66ac9dbSNicholas Bellinger }
833c66ac9dbSNicholas Bellinger 
83463b91d5aSAsias He static void __exit fileio_module_exit(void)
835c66ac9dbSNicholas Bellinger {
8360a06d430SChristoph Hellwig 	target_backend_unregister(&fileio_ops);
837c66ac9dbSNicholas Bellinger }
838c66ac9dbSNicholas Bellinger 
839c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
840c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
841c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
842c66ac9dbSNicholas Bellinger 
843c66ac9dbSNicholas Bellinger module_init(fileio_module_init);
844c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit);
845