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>
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>
40b2320497SNicholas Bellinger #include <target/target_core_backend_configfs.h>
41c66ac9dbSNicholas Bellinger 
42c66ac9dbSNicholas Bellinger #include "target_core_file.h"
43c66ac9dbSNicholas Bellinger 
440fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev)
450fd97ccfSChristoph Hellwig {
460fd97ccfSChristoph Hellwig 	return container_of(dev, struct fd_dev, dev);
470fd97ccfSChristoph Hellwig }
48c66ac9dbSNicholas Bellinger 
49c66ac9dbSNicholas Bellinger /*	fd_attach_hba(): (Part of se_subsystem_api_t template)
50c66ac9dbSNicholas Bellinger  *
51c66ac9dbSNicholas Bellinger  *
52c66ac9dbSNicholas Bellinger  */
53c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id)
54c66ac9dbSNicholas Bellinger {
55c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host;
56c66ac9dbSNicholas Bellinger 
57c66ac9dbSNicholas Bellinger 	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
586708bb27SAndy Grover 	if (!fd_host) {
596708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_host\n");
60e3d6f909SAndy Grover 		return -ENOMEM;
61c66ac9dbSNicholas Bellinger 	}
62c66ac9dbSNicholas Bellinger 
63c66ac9dbSNicholas Bellinger 	fd_host->fd_host_id = host_id;
64c66ac9dbSNicholas Bellinger 
65e3d6f909SAndy Grover 	hba->hba_ptr = fd_host;
66c66ac9dbSNicholas Bellinger 
676708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
68c66ac9dbSNicholas Bellinger 		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
69c66ac9dbSNicholas Bellinger 		TARGET_CORE_MOD_VERSION);
7095cadaceSNicholas Bellinger 	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
7195cadaceSNicholas Bellinger 		hba->hba_id, fd_host->fd_host_id);
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 
15621363ca8SNicholas Bellinger 		fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
157c66ac9dbSNicholas Bellinger 		/*
158c66ac9dbSNicholas Bellinger 		 * Determine the number of bytes from i_size_read() minus
159c66ac9dbSNicholas Bellinger 		 * one (1) logical sector from underlying struct block_device
160c66ac9dbSNicholas Bellinger 		 */
161cd9323fdSNicholas Bellinger 		dev_size = (i_size_read(file->f_mapping->host) -
162c66ac9dbSNicholas Bellinger 				       fd_dev->fd_block_size);
163c66ac9dbSNicholas Bellinger 
1646708bb27SAndy Grover 		pr_debug("FILEIO: Using size: %llu bytes from struct"
165c66ac9dbSNicholas Bellinger 			" block_device blocks: %llu logical_block_size: %d\n",
166cd9323fdSNicholas Bellinger 			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
167c66ac9dbSNicholas Bellinger 			fd_dev->fd_block_size);
16870d3ae5cSAsias He 		/*
16970d3ae5cSAsias He 		 * Check if the underlying struct block_device request_queue supports
17070d3ae5cSAsias He 		 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
17170d3ae5cSAsias He 		 * in ATA and we need to set TPE=1
17270d3ae5cSAsias He 		 */
17370d3ae5cSAsias He 		if (blk_queue_discard(q)) {
17470d3ae5cSAsias He 			dev->dev_attrib.max_unmap_lba_count =
17570d3ae5cSAsias He 				q->limits.max_discard_sectors;
17670d3ae5cSAsias He 			/*
17770d3ae5cSAsias He 			 * Currently hardcoded to 1 in Linux/SCSI code..
17870d3ae5cSAsias He 			 */
17970d3ae5cSAsias He 			dev->dev_attrib.max_unmap_block_desc_count = 1;
18070d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity =
18170d3ae5cSAsias He 				q->limits.discard_granularity >> 9;
18270d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity_alignment =
18370d3ae5cSAsias He 				q->limits.discard_alignment;
18470d3ae5cSAsias He 			pr_debug("IFILE: BLOCK Discard support available,"
18570d3ae5cSAsias He 					" disabled by default\n");
18670d3ae5cSAsias He 		}
18770d3ae5cSAsias He 		/*
18870d3ae5cSAsias He 		 * Enable write same emulation for IBLOCK and use 0xFFFF as
18970d3ae5cSAsias He 		 * the smaller WRITE_SAME(10) only has a two-byte block count.
19070d3ae5cSAsias He 		 */
19170d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0xFFFF;
1920463a3feSAsias He 
1930463a3feSAsias He 		if (blk_queue_nonrot(q))
1940463a3feSAsias He 			dev->dev_attrib.is_nonrot = 1;
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 
20321363ca8SNicholas Bellinger 		fd_dev->fd_block_size = FD_BLOCKSIZE;
20470d3ae5cSAsias He 		/*
20570d3ae5cSAsias He 		 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
20670d3ae5cSAsias He 		 */
20770d3ae5cSAsias He 		dev->dev_attrib.max_unmap_lba_count = 0x2000;
20870d3ae5cSAsias He 		/*
20970d3ae5cSAsias He 		 * Currently hardcoded to 1 in Linux/SCSI code..
21070d3ae5cSAsias He 		 */
21170d3ae5cSAsias He 		dev->dev_attrib.max_unmap_block_desc_count = 1;
21270d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity = 1;
21370d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity_alignment = 0;
21470d3ae5cSAsias He 
21570d3ae5cSAsias He 		/*
21670d3ae5cSAsias He 		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
21770d3ae5cSAsias He 		 * based upon struct iovec limit for vfs_writev()
21870d3ae5cSAsias He 		 */
21970d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0x1000;
220c66ac9dbSNicholas Bellinger 	}
221c66ac9dbSNicholas Bellinger 
22221363ca8SNicholas Bellinger 	dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
22395cadaceSNicholas Bellinger 	dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
22495cadaceSNicholas Bellinger 	dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
2250fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
226c66ac9dbSNicholas Bellinger 
227b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
228b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
229b32f4c7eSNicholas Bellinger 			" with FDBD_HAS_BUFFERED_IO_WCE\n");
2300fd97ccfSChristoph Hellwig 		dev->dev_attrib.emulate_write_cache = 1;
231b32f4c7eSNicholas Bellinger 	}
232b32f4c7eSNicholas Bellinger 
233c66ac9dbSNicholas Bellinger 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
234c66ac9dbSNicholas Bellinger 	fd_dev->fd_queue_depth = dev->queue_depth;
235c66ac9dbSNicholas Bellinger 
2366708bb27SAndy Grover 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
237c66ac9dbSNicholas Bellinger 		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
238c66ac9dbSNicholas Bellinger 			fd_dev->fd_dev_name, fd_dev->fd_dev_size);
239c66ac9dbSNicholas Bellinger 
2400fd97ccfSChristoph Hellwig 	return 0;
241c66ac9dbSNicholas Bellinger fail:
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 	}
2460fd97ccfSChristoph Hellwig 	return ret;
247c66ac9dbSNicholas Bellinger }
248c66ac9dbSNicholas Bellinger 
2490fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev)
250c66ac9dbSNicholas Bellinger {
2510fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
252c66ac9dbSNicholas Bellinger 
253c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
254c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
255c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
256c66ac9dbSNicholas Bellinger 	}
257c66ac9dbSNicholas Bellinger 
258c66ac9dbSNicholas Bellinger 	kfree(fd_dev);
259c66ac9dbSNicholas Bellinger }
260c66ac9dbSNicholas Bellinger 
2618287fa5fSSagi Grimberg static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
2628287fa5fSSagi Grimberg 		    u32 block_size, struct scatterlist *sgl,
2638287fa5fSSagi Grimberg 		    u32 sgl_nents, u32 data_length, int is_write)
26442201b55SNicholas Bellinger {
2655787cacdSChristoph Hellwig 	struct scatterlist *sg;
2666b9a44d0SChristoph Hellwig 	struct iov_iter iter;
2676b9a44d0SChristoph Hellwig 	struct bio_vec *bvec;
2686b9a44d0SChristoph Hellwig 	ssize_t len = 0;
2698287fa5fSSagi Grimberg 	loff_t pos = (cmd->t_task_lba * block_size);
270c66ac9dbSNicholas Bellinger 	int ret = 0, i;
271c66ac9dbSNicholas Bellinger 
2726b9a44d0SChristoph Hellwig 	bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
2736b9a44d0SChristoph Hellwig 	if (!bvec) {
2746708bb27SAndy Grover 		pr_err("Unable to allocate fd_do_readv iov[]\n");
275e3d6f909SAndy Grover 		return -ENOMEM;
276c66ac9dbSNicholas Bellinger 	}
277c66ac9dbSNicholas Bellinger 
2785787cacdSChristoph Hellwig 	for_each_sg(sgl, sg, sgl_nents, i) {
2796b9a44d0SChristoph Hellwig 		bvec[i].bv_page = sg_page(sg);
2806b9a44d0SChristoph Hellwig 		bvec[i].bv_len = sg->length;
2816b9a44d0SChristoph Hellwig 		bvec[i].bv_offset = sg->offset;
2826b9a44d0SChristoph Hellwig 
2836b9a44d0SChristoph Hellwig 		len += sg->length;
284c66ac9dbSNicholas Bellinger 	}
285c66ac9dbSNicholas Bellinger 
2866b9a44d0SChristoph Hellwig 	iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len);
287778229afSSebastian Andrzej Siewior 	if (is_write)
2886b9a44d0SChristoph Hellwig 		ret = vfs_iter_write(fd, &iter, &pos);
289778229afSSebastian Andrzej Siewior 	else
2906b9a44d0SChristoph Hellwig 		ret = vfs_iter_read(fd, &iter, &pos);
291778229afSSebastian Andrzej Siewior 
2926b9a44d0SChristoph Hellwig 	kfree(bvec);
293778229afSSebastian Andrzej Siewior 
294778229afSSebastian Andrzej Siewior 	if (is_write) {
2958287fa5fSSagi Grimberg 		if (ret < 0 || ret != data_length) {
296778229afSSebastian Andrzej Siewior 			pr_err("%s() write returned %d\n", __func__, ret);
297778229afSSebastian Andrzej Siewior 			return (ret < 0 ? ret : -EINVAL);
298778229afSSebastian Andrzej Siewior 		}
299778229afSSebastian Andrzej Siewior 	} else {
300c66ac9dbSNicholas Bellinger 		/*
301c66ac9dbSNicholas Bellinger 		 * Return zeros and GOOD status even if the READ did not return
302c66ac9dbSNicholas Bellinger 		 * the expected virt_size for struct file w/o a backing struct
303c66ac9dbSNicholas Bellinger 		 * block_device.
304c66ac9dbSNicholas Bellinger 		 */
305496ad9aaSAl Viro 		if (S_ISBLK(file_inode(fd)->i_mode)) {
3068287fa5fSSagi Grimberg 			if (ret < 0 || ret != data_length) {
307778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d, expecting %u for "
308778229afSSebastian Andrzej Siewior 						"S_ISBLK\n", __func__, ret,
3098287fa5fSSagi Grimberg 						data_length);
310e3d6f909SAndy Grover 				return (ret < 0 ? ret : -EINVAL);
311c66ac9dbSNicholas Bellinger 			}
312c66ac9dbSNicholas Bellinger 		} else {
313c66ac9dbSNicholas Bellinger 			if (ret < 0) {
314778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d for non S_ISBLK\n",
315778229afSSebastian Andrzej Siewior 						__func__, ret);
316e3d6f909SAndy Grover 				return ret;
317c66ac9dbSNicholas Bellinger 			}
318c66ac9dbSNicholas Bellinger 		}
319c66ac9dbSNicholas Bellinger 	}
320c66ac9dbSNicholas Bellinger 	return 1;
321c66ac9dbSNicholas Bellinger }
322c66ac9dbSNicholas Bellinger 
323de103c93SChristoph Hellwig static sense_reason_t
324de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd)
325c66ac9dbSNicholas Bellinger {
326c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3270fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
328a1d8b49aSAndy Grover 	int immed = (cmd->t_task_cdb[1] & 0x2);
329c66ac9dbSNicholas Bellinger 	loff_t start, end;
330c66ac9dbSNicholas Bellinger 	int ret;
331c66ac9dbSNicholas Bellinger 
332c66ac9dbSNicholas Bellinger 	/*
333c66ac9dbSNicholas Bellinger 	 * If the Immediate bit is set, queue up the GOOD response
334c66ac9dbSNicholas Bellinger 	 * for this SYNCHRONIZE_CACHE op
335c66ac9dbSNicholas Bellinger 	 */
336c66ac9dbSNicholas Bellinger 	if (immed)
3375787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
338c66ac9dbSNicholas Bellinger 
339c66ac9dbSNicholas Bellinger 	/*
340c66ac9dbSNicholas Bellinger 	 * Determine if we will be flushing the entire device.
341c66ac9dbSNicholas Bellinger 	 */
342a1d8b49aSAndy Grover 	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
343c66ac9dbSNicholas Bellinger 		start = 0;
344c66ac9dbSNicholas Bellinger 		end = LLONG_MAX;
345c66ac9dbSNicholas Bellinger 	} else {
3460fd97ccfSChristoph Hellwig 		start = cmd->t_task_lba * dev->dev_attrib.block_size;
347c66ac9dbSNicholas Bellinger 		if (cmd->data_length)
34862d3ab49SZach Brown 			end = start + cmd->data_length - 1;
349c66ac9dbSNicholas Bellinger 		else
350c66ac9dbSNicholas Bellinger 			end = LLONG_MAX;
351c66ac9dbSNicholas Bellinger 	}
352c66ac9dbSNicholas Bellinger 
353c66ac9dbSNicholas Bellinger 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
354c66ac9dbSNicholas Bellinger 	if (ret != 0)
3556708bb27SAndy Grover 		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
356c66ac9dbSNicholas Bellinger 
3575787cacdSChristoph Hellwig 	if (immed)
358ad67f0d9SChristoph Hellwig 		return 0;
3595787cacdSChristoph Hellwig 
360de103c93SChristoph Hellwig 	if (ret)
3615787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
362de103c93SChristoph Hellwig 	else
3635787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
364ad67f0d9SChristoph Hellwig 
365ad67f0d9SChristoph Hellwig 	return 0;
366c66ac9dbSNicholas Bellinger }
367c66ac9dbSNicholas Bellinger 
3687b745c84SNicholas Bellinger static sense_reason_t
3697b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd)
3707b745c84SNicholas Bellinger {
3717b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
3727b745c84SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(se_dev);
3737b745c84SNicholas Bellinger 	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
374d4c5dcacSChristoph Hellwig 	sector_t nolb = sbc_get_write_same_sectors(cmd);
375d4c5dcacSChristoph Hellwig 	struct iov_iter iter;
376d4c5dcacSChristoph Hellwig 	struct bio_vec *bvec;
377d4c5dcacSChristoph Hellwig 	unsigned int len = 0, i;
378d4c5dcacSChristoph Hellwig 	ssize_t ret;
3797b745c84SNicholas Bellinger 
3807b745c84SNicholas Bellinger 	if (!nolb) {
3817b745c84SNicholas Bellinger 		target_complete_cmd(cmd, SAM_STAT_GOOD);
3827b745c84SNicholas Bellinger 		return 0;
3837b745c84SNicholas Bellinger 	}
384afd73f1bSNicholas Bellinger 	if (cmd->prot_op) {
385afd73f1bSNicholas Bellinger 		pr_err("WRITE_SAME: Protection information with FILEIO"
386afd73f1bSNicholas Bellinger 		       " backends not supported\n");
387afd73f1bSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
388afd73f1bSNicholas Bellinger 	}
3897b745c84SNicholas Bellinger 
3907b745c84SNicholas Bellinger 	if (cmd->t_data_nents > 1 ||
391d4c5dcacSChristoph Hellwig 	    cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {
3927b745c84SNicholas Bellinger 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
393d4c5dcacSChristoph Hellwig 			" block_size: %u\n",
394d4c5dcacSChristoph Hellwig 			cmd->t_data_nents,
395d4c5dcacSChristoph Hellwig 			cmd->t_data_sg[0].length,
3967b745c84SNicholas Bellinger 			cmd->se_dev->dev_attrib.block_size);
3977b745c84SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
3987b745c84SNicholas Bellinger 	}
3997b745c84SNicholas Bellinger 
400d4c5dcacSChristoph Hellwig 	bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
401d4c5dcacSChristoph Hellwig 	if (!bvec)
4027b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4037b745c84SNicholas Bellinger 
404d4c5dcacSChristoph Hellwig 	for (i = 0; i < nolb; i++) {
405d4c5dcacSChristoph Hellwig 		bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]);
406d4c5dcacSChristoph Hellwig 		bvec[i].bv_len = cmd->t_data_sg[0].length;
407d4c5dcacSChristoph Hellwig 		bvec[i].bv_offset = cmd->t_data_sg[0].offset;
408d4c5dcacSChristoph Hellwig 
409d4c5dcacSChristoph Hellwig 		len += se_dev->dev_attrib.block_size;
4107b745c84SNicholas Bellinger 	}
4117b745c84SNicholas Bellinger 
412d4c5dcacSChristoph Hellwig 	iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len);
413d4c5dcacSChristoph Hellwig 	ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos);
4147b745c84SNicholas Bellinger 
415d4c5dcacSChristoph Hellwig 	kfree(bvec);
416d4c5dcacSChristoph Hellwig 	if (ret < 0 || ret != len) {
417d4c5dcacSChristoph Hellwig 		pr_err("vfs_iter_write() returned %zd for write same\n", ret);
4187b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4197b745c84SNicholas Bellinger 	}
4207b745c84SNicholas Bellinger 
4217b745c84SNicholas Bellinger 	target_complete_cmd(cmd, SAM_STAT_GOOD);
4227b745c84SNicholas Bellinger 	return 0;
4237b745c84SNicholas Bellinger }
4247b745c84SNicholas Bellinger 
42564d240b7SAkinobu Mita static int
42664d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
42764d240b7SAkinobu Mita 		void *buf, size_t bufsize)
42864d240b7SAkinobu Mita {
42964d240b7SAkinobu Mita 	struct fd_dev *fd_dev = FD_DEV(se_dev);
43064d240b7SAkinobu Mita 	struct file *prot_fd = fd_dev->fd_prot_file;
43164d240b7SAkinobu Mita 	sector_t prot_length, prot;
43264d240b7SAkinobu Mita 	loff_t pos = lba * se_dev->prot_length;
43364d240b7SAkinobu Mita 
43464d240b7SAkinobu Mita 	if (!prot_fd) {
43564d240b7SAkinobu Mita 		pr_err("Unable to locate fd_dev->fd_prot_file\n");
43664d240b7SAkinobu Mita 		return -ENODEV;
43764d240b7SAkinobu Mita 	}
43864d240b7SAkinobu Mita 
43964d240b7SAkinobu Mita 	prot_length = nolb * se_dev->prot_length;
44064d240b7SAkinobu Mita 
44164d240b7SAkinobu Mita 	for (prot = 0; prot < prot_length;) {
44264d240b7SAkinobu Mita 		sector_t len = min_t(sector_t, bufsize, prot_length - prot);
44364d240b7SAkinobu Mita 		ssize_t ret = kernel_write(prot_fd, buf, len, pos + prot);
44464d240b7SAkinobu Mita 
44564d240b7SAkinobu Mita 		if (ret != len) {
44664d240b7SAkinobu Mita 			pr_err("vfs_write to prot file failed: %zd\n", ret);
44764d240b7SAkinobu Mita 			return ret < 0 ? ret : -ENODEV;
44864d240b7SAkinobu Mita 		}
44964d240b7SAkinobu Mita 		prot += ret;
45064d240b7SAkinobu Mita 	}
45164d240b7SAkinobu Mita 
45264d240b7SAkinobu Mita 	return 0;
45364d240b7SAkinobu Mita }
45464d240b7SAkinobu Mita 
45564d240b7SAkinobu Mita static int
45664d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
45764d240b7SAkinobu Mita {
45864d240b7SAkinobu Mita 	void *buf;
45964d240b7SAkinobu Mita 	int rc;
46064d240b7SAkinobu Mita 
46164d240b7SAkinobu Mita 	buf = (void *)__get_free_page(GFP_KERNEL);
46264d240b7SAkinobu Mita 	if (!buf) {
46364d240b7SAkinobu Mita 		pr_err("Unable to allocate FILEIO prot buf\n");
46464d240b7SAkinobu Mita 		return -ENOMEM;
46564d240b7SAkinobu Mita 	}
46664d240b7SAkinobu Mita 	memset(buf, 0xff, PAGE_SIZE);
46764d240b7SAkinobu Mita 
46864d240b7SAkinobu Mita 	rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
46964d240b7SAkinobu Mita 
47064d240b7SAkinobu Mita 	free_page((unsigned long)buf);
47164d240b7SAkinobu Mita 
47264d240b7SAkinobu Mita 	return rc;
47364d240b7SAkinobu Mita }
47464d240b7SAkinobu Mita 
475de103c93SChristoph Hellwig static sense_reason_t
47686d71829SAsias He fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
47770d3ae5cSAsias He {
47886d71829SAsias He 	struct file *file = priv;
47970d3ae5cSAsias He 	struct inode *inode = file->f_mapping->host;
48070d3ae5cSAsias He 	int ret;
48170d3ae5cSAsias He 
48264d240b7SAkinobu Mita 	if (cmd->se_dev->dev_attrib.pi_prot_type) {
48364d240b7SAkinobu Mita 		ret = fd_do_prot_unmap(cmd, lba, nolb);
48464d240b7SAkinobu Mita 		if (ret)
48564d240b7SAkinobu Mita 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
48664d240b7SAkinobu Mita 	}
48764d240b7SAkinobu Mita 
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);
5273abff1e5SChristoph Hellwig 	sense_reason_t 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 {
54586d71829SAsias He 	struct file *file = FD_DEV(cmd->se_dev)->fd_file;
54650642762SAsias He 
54786d71829SAsias He 	return sbc_execute_unmap(cmd, fd_do_unmap, file);
54850642762SAsias He }
54950642762SAsias He 
55050642762SAsias He static sense_reason_t
551a82a9538SNicholas Bellinger fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
552a82a9538SNicholas Bellinger 	      enum dma_data_direction data_direction)
553c66ac9dbSNicholas Bellinger {
554c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
5558287fa5fSSagi Grimberg 	struct fd_dev *fd_dev = FD_DEV(dev);
5568287fa5fSSagi Grimberg 	struct file *file = fd_dev->fd_file;
5578287fa5fSSagi Grimberg 	struct file *pfile = fd_dev->fd_prot_file;
55842201b55SNicholas Bellinger 	sense_reason_t rc;
559c66ac9dbSNicholas Bellinger 	int ret = 0;
560046ba642SNicholas Bellinger 	/*
561046ba642SNicholas Bellinger 	 * We are currently limited by the number of iovecs (2048) per
562046ba642SNicholas Bellinger 	 * single vfs_[writev,readv] call.
563046ba642SNicholas Bellinger 	 */
564046ba642SNicholas Bellinger 	if (cmd->data_length > FD_MAX_BYTES) {
565046ba642SNicholas Bellinger 		pr_err("FILEIO: Not able to process I/O of %u bytes due to"
566046ba642SNicholas Bellinger 		       "FD_MAX_BYTES: %u iovec count limitiation\n",
567046ba642SNicholas Bellinger 			cmd->data_length, FD_MAX_BYTES);
568046ba642SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
569046ba642SNicholas Bellinger 	}
570c66ac9dbSNicholas Bellinger 	/*
571c66ac9dbSNicholas Bellinger 	 * Call vectorized fileio functions to map struct scatterlist
572c66ac9dbSNicholas Bellinger 	 * physical memory addresses to struct iovec virtual memory.
573c66ac9dbSNicholas Bellinger 	 */
5745787cacdSChristoph Hellwig 	if (data_direction == DMA_FROM_DEVICE) {
575ee920469SNicholas Bellinger 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5768287fa5fSSagi Grimberg 			ret = fd_do_rw(cmd, pfile, dev->prot_length,
5778287fa5fSSagi Grimberg 				       cmd->t_prot_sg, cmd->t_prot_nents,
5788287fa5fSSagi Grimberg 				       cmd->prot_length, 0);
57942201b55SNicholas Bellinger 			if (ret < 0)
58042201b55SNicholas Bellinger 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
58142201b55SNicholas Bellinger 		}
58242201b55SNicholas Bellinger 
5838287fa5fSSagi Grimberg 		ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
5848287fa5fSSagi Grimberg 			       sgl, sgl_nents, cmd->data_length, 0);
58542201b55SNicholas Bellinger 
586ee920469SNicholas Bellinger 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5878287fa5fSSagi Grimberg 			u32 sectors = cmd->data_length >>
5888287fa5fSSagi Grimberg 					ilog2(dev->dev_attrib.block_size);
58942201b55SNicholas Bellinger 
590f75b6faeSSagi Grimberg 			rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
591f75b6faeSSagi Grimberg 					    0, cmd->t_prot_sg, 0);
5928287fa5fSSagi Grimberg 			if (rc)
59342201b55SNicholas Bellinger 				return rc;
59442201b55SNicholas Bellinger 		}
5958287fa5fSSagi Grimberg 	} else {
5968287fa5fSSagi Grimberg 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
5978287fa5fSSagi Grimberg 			u32 sectors = cmd->data_length >>
5988287fa5fSSagi Grimberg 					ilog2(dev->dev_attrib.block_size);
5998287fa5fSSagi Grimberg 
6008287fa5fSSagi Grimberg 			rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
6018287fa5fSSagi Grimberg 					    0, cmd->t_prot_sg, 0);
6028287fa5fSSagi Grimberg 			if (rc)
6038287fa5fSSagi Grimberg 				return rc;
60442201b55SNicholas Bellinger 		}
60542201b55SNicholas Bellinger 
6068287fa5fSSagi Grimberg 		ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
6078287fa5fSSagi Grimberg 			       sgl, sgl_nents, cmd->data_length, 1);
608a4dff304SNicholas Bellinger 		/*
609125d0119SHannes Reinecke 		 * Perform implicit vfs_fsync_range() for fd_do_writev() ops
610a4dff304SNicholas Bellinger 		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
611a4dff304SNicholas Bellinger 		 * Allow this to happen independent of WCE=0 setting.
612a4dff304SNicholas Bellinger 		 */
613c66ac9dbSNicholas Bellinger 		if (ret > 0 &&
6140fd97ccfSChristoph Hellwig 		    dev->dev_attrib.emulate_fua_write > 0 &&
6152d3a4b51SChristoph Hellwig 		    (cmd->se_cmd_flags & SCF_FUA)) {
616a4dff304SNicholas Bellinger 			loff_t start = cmd->t_task_lba *
6170fd97ccfSChristoph Hellwig 				dev->dev_attrib.block_size;
61862d3ab49SZach Brown 			loff_t end;
61962d3ab49SZach Brown 
62062d3ab49SZach Brown 			if (cmd->data_length)
62162d3ab49SZach Brown 				end = start + cmd->data_length - 1;
62262d3ab49SZach Brown 			else
62362d3ab49SZach Brown 				end = LLONG_MAX;
624c66ac9dbSNicholas Bellinger 
625a4dff304SNicholas Bellinger 			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
626a4dff304SNicholas Bellinger 		}
627c66ac9dbSNicholas Bellinger 
628ee920469SNicholas Bellinger 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
6298287fa5fSSagi Grimberg 			ret = fd_do_rw(cmd, pfile, dev->prot_length,
6308287fa5fSSagi Grimberg 				       cmd->t_prot_sg, cmd->t_prot_nents,
6318287fa5fSSagi Grimberg 				       cmd->prot_length, 1);
632de103c93SChristoph Hellwig 			if (ret < 0)
633de103c93SChristoph Hellwig 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
63442201b55SNicholas Bellinger 		}
63542201b55SNicholas Bellinger 	}
63642201b55SNicholas Bellinger 
6378287fa5fSSagi Grimberg 	if (ret < 0)
63842201b55SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
639de103c93SChristoph Hellwig 
6405787cacdSChristoph Hellwig 	if (ret)
6415787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
64203e98c9eSNicholas Bellinger 	return 0;
643c66ac9dbSNicholas Bellinger }
644c66ac9dbSNicholas Bellinger 
645c66ac9dbSNicholas Bellinger enum {
646c66ac9dbSNicholas Bellinger 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
647c66ac9dbSNicholas Bellinger };
648c66ac9dbSNicholas Bellinger 
649c66ac9dbSNicholas Bellinger static match_table_t tokens = {
650c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_name, "fd_dev_name=%s"},
651c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_size, "fd_dev_size=%s"},
652b32f4c7eSNicholas Bellinger 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
653c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
654c66ac9dbSNicholas Bellinger };
655c66ac9dbSNicholas Bellinger 
6560fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
657c66ac9dbSNicholas Bellinger 		const char *page, ssize_t count)
658c66ac9dbSNicholas Bellinger {
6590fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
660c66ac9dbSNicholas Bellinger 	char *orig, *ptr, *arg_p, *opts;
661c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
662b32f4c7eSNicholas Bellinger 	int ret = 0, arg, token;
663c66ac9dbSNicholas Bellinger 
664c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
665c66ac9dbSNicholas Bellinger 	if (!opts)
666c66ac9dbSNicholas Bellinger 		return -ENOMEM;
667c66ac9dbSNicholas Bellinger 
668c66ac9dbSNicholas Bellinger 	orig = opts;
669c66ac9dbSNicholas Bellinger 
67090c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
671c66ac9dbSNicholas Bellinger 		if (!*ptr)
672c66ac9dbSNicholas Bellinger 			continue;
673c66ac9dbSNicholas Bellinger 
674c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
675c66ac9dbSNicholas Bellinger 		switch (token) {
676c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_name:
677dbc6e022SAl Viro 			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
678dbc6e022SAl Viro 				FD_MAX_DEV_NAME) == 0) {
679dbc6e022SAl Viro 				ret = -EINVAL;
6806d180253SJesper Juhl 				break;
6816d180253SJesper Juhl 			}
6826708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Path: %s\n",
683c66ac9dbSNicholas Bellinger 					fd_dev->fd_dev_name);
684c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_PATH;
685c66ac9dbSNicholas Bellinger 			break;
686c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_size:
687c66ac9dbSNicholas Bellinger 			arg_p = match_strdup(&args[0]);
6886d180253SJesper Juhl 			if (!arg_p) {
6896d180253SJesper Juhl 				ret = -ENOMEM;
6906d180253SJesper Juhl 				break;
6916d180253SJesper Juhl 			}
69257103d7fSJingoo Han 			ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
6936d180253SJesper Juhl 			kfree(arg_p);
694c66ac9dbSNicholas Bellinger 			if (ret < 0) {
69557103d7fSJingoo Han 				pr_err("kstrtoull() failed for"
696c66ac9dbSNicholas Bellinger 						" fd_dev_size=\n");
697c66ac9dbSNicholas Bellinger 				goto out;
698c66ac9dbSNicholas Bellinger 			}
6996708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Size: %llu"
700c66ac9dbSNicholas Bellinger 					" bytes\n", fd_dev->fd_dev_size);
701c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
702c66ac9dbSNicholas Bellinger 			break;
703b32f4c7eSNicholas Bellinger 		case Opt_fd_buffered_io:
704ce31c1b0SJoern Engel 			ret = match_int(args, &arg);
705ce31c1b0SJoern Engel 			if (ret)
706ce31c1b0SJoern Engel 				goto out;
707b32f4c7eSNicholas Bellinger 			if (arg != 1) {
708b32f4c7eSNicholas Bellinger 				pr_err("bogus fd_buffered_io=%d value\n", arg);
709b32f4c7eSNicholas Bellinger 				ret = -EINVAL;
710b32f4c7eSNicholas Bellinger 				goto out;
711b32f4c7eSNicholas Bellinger 			}
712b32f4c7eSNicholas Bellinger 
713b32f4c7eSNicholas Bellinger 			pr_debug("FILEIO: Using buffered I/O"
714b32f4c7eSNicholas Bellinger 				" operations for struct fd_dev\n");
715b32f4c7eSNicholas Bellinger 
716b32f4c7eSNicholas Bellinger 			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
717b32f4c7eSNicholas Bellinger 			break;
718c66ac9dbSNicholas Bellinger 		default:
719c66ac9dbSNicholas Bellinger 			break;
720c66ac9dbSNicholas Bellinger 		}
721c66ac9dbSNicholas Bellinger 	}
722c66ac9dbSNicholas Bellinger 
723c66ac9dbSNicholas Bellinger out:
724c66ac9dbSNicholas Bellinger 	kfree(orig);
725c66ac9dbSNicholas Bellinger 	return (!ret) ? count : ret;
726c66ac9dbSNicholas Bellinger }
727c66ac9dbSNicholas Bellinger 
7280fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
729c66ac9dbSNicholas Bellinger {
7300fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
731c66ac9dbSNicholas Bellinger 	ssize_t bl = 0;
732c66ac9dbSNicholas Bellinger 
733c66ac9dbSNicholas Bellinger 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
734b32f4c7eSNicholas Bellinger 	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s\n",
735b32f4c7eSNicholas Bellinger 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
736b32f4c7eSNicholas Bellinger 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
737b32f4c7eSNicholas Bellinger 		"Buffered-WCE" : "O_DSYNC");
738c66ac9dbSNicholas Bellinger 	return bl;
739c66ac9dbSNicholas Bellinger }
740c66ac9dbSNicholas Bellinger 
741c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev)
742c66ac9dbSNicholas Bellinger {
7430fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
744cd9323fdSNicholas Bellinger 	struct file *f = fd_dev->fd_file;
745cd9323fdSNicholas Bellinger 	struct inode *i = f->f_mapping->host;
746cd9323fdSNicholas Bellinger 	unsigned long long dev_size;
747cd9323fdSNicholas Bellinger 	/*
748cd9323fdSNicholas Bellinger 	 * When using a file that references an underlying struct block_device,
749cd9323fdSNicholas Bellinger 	 * ensure dev_size is always based on the current inode size in order
750cd9323fdSNicholas Bellinger 	 * to handle underlying block_device resize operations.
751cd9323fdSNicholas Bellinger 	 */
752cd9323fdSNicholas Bellinger 	if (S_ISBLK(i->i_mode))
75321363ca8SNicholas Bellinger 		dev_size = i_size_read(i);
754cd9323fdSNicholas Bellinger 	else
755cd9323fdSNicholas Bellinger 		dev_size = fd_dev->fd_dev_size;
756c66ac9dbSNicholas Bellinger 
75721363ca8SNicholas Bellinger 	return div_u64(dev_size - dev->dev_attrib.block_size,
75821363ca8SNicholas Bellinger 		       dev->dev_attrib.block_size);
759c66ac9dbSNicholas Bellinger }
760c66ac9dbSNicholas Bellinger 
7610f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev)
7620f5e2ec4SNicholas Bellinger {
7630f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
7640f5e2ec4SNicholas Bellinger 	struct file *prot_file, *file = fd_dev->fd_file;
7650f5e2ec4SNicholas Bellinger 	struct inode *inode;
7660f5e2ec4SNicholas Bellinger 	int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
7670f5e2ec4SNicholas Bellinger 	char buf[FD_MAX_DEV_PROT_NAME];
7680f5e2ec4SNicholas Bellinger 
7690f5e2ec4SNicholas Bellinger 	if (!file) {
7700f5e2ec4SNicholas Bellinger 		pr_err("Unable to locate fd_dev->fd_file\n");
7710f5e2ec4SNicholas Bellinger 		return -ENODEV;
7720f5e2ec4SNicholas Bellinger 	}
7730f5e2ec4SNicholas Bellinger 
7740f5e2ec4SNicholas Bellinger 	inode = file->f_mapping->host;
7750f5e2ec4SNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
7760f5e2ec4SNicholas Bellinger 		pr_err("FILEIO Protection emulation only supported on"
7770f5e2ec4SNicholas Bellinger 		       " !S_ISBLK\n");
7780f5e2ec4SNicholas Bellinger 		return -ENOSYS;
7790f5e2ec4SNicholas Bellinger 	}
7800f5e2ec4SNicholas Bellinger 
7810f5e2ec4SNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
7820f5e2ec4SNicholas Bellinger 		flags &= ~O_DSYNC;
7830f5e2ec4SNicholas Bellinger 
7840f5e2ec4SNicholas Bellinger 	snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
7850f5e2ec4SNicholas Bellinger 		 fd_dev->fd_dev_name);
7860f5e2ec4SNicholas Bellinger 
7870f5e2ec4SNicholas Bellinger 	prot_file = filp_open(buf, flags, 0600);
7880f5e2ec4SNicholas Bellinger 	if (IS_ERR(prot_file)) {
7890f5e2ec4SNicholas Bellinger 		pr_err("filp_open(%s) failed\n", buf);
7900f5e2ec4SNicholas Bellinger 		ret = PTR_ERR(prot_file);
7910f5e2ec4SNicholas Bellinger 		return ret;
7920f5e2ec4SNicholas Bellinger 	}
7930f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = prot_file;
7940f5e2ec4SNicholas Bellinger 
7950f5e2ec4SNicholas Bellinger 	return 0;
7960f5e2ec4SNicholas Bellinger }
7970f5e2ec4SNicholas Bellinger 
7980f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev)
7990f5e2ec4SNicholas Bellinger {
8000f5e2ec4SNicholas Bellinger 	unsigned char *buf;
8010f5e2ec4SNicholas Bellinger 	int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
80264d240b7SAkinobu Mita 	int ret;
8030f5e2ec4SNicholas Bellinger 
8040f5e2ec4SNicholas Bellinger 	if (!dev->dev_attrib.pi_prot_type) {
8050f5e2ec4SNicholas Bellinger 		pr_err("Unable to format_prot while pi_prot_type == 0\n");
8060f5e2ec4SNicholas Bellinger 		return -ENODEV;
8070f5e2ec4SNicholas Bellinger 	}
8080f5e2ec4SNicholas Bellinger 
8090f5e2ec4SNicholas Bellinger 	buf = vzalloc(unit_size);
8100f5e2ec4SNicholas Bellinger 	if (!buf) {
8110f5e2ec4SNicholas Bellinger 		pr_err("Unable to allocate FILEIO prot buf\n");
8120f5e2ec4SNicholas Bellinger 		return -ENOMEM;
8130f5e2ec4SNicholas Bellinger 	}
8140f5e2ec4SNicholas Bellinger 
8150f5e2ec4SNicholas Bellinger 	pr_debug("Using FILEIO prot_length: %llu\n",
81664d240b7SAkinobu Mita 		 (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
81764d240b7SAkinobu Mita 					dev->prot_length);
8180f5e2ec4SNicholas Bellinger 
81980dcd0c1SSagi Grimberg 	memset(buf, 0xff, unit_size);
82064d240b7SAkinobu Mita 	ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
82164d240b7SAkinobu Mita 			      buf, unit_size);
8220f5e2ec4SNicholas Bellinger 	vfree(buf);
8230f5e2ec4SNicholas Bellinger 	return ret;
8240f5e2ec4SNicholas Bellinger }
8250f5e2ec4SNicholas Bellinger 
8260f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev)
8270f5e2ec4SNicholas Bellinger {
8280f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
8290f5e2ec4SNicholas Bellinger 
8300f5e2ec4SNicholas Bellinger 	if (!fd_dev->fd_prot_file)
8310f5e2ec4SNicholas Bellinger 		return;
8320f5e2ec4SNicholas Bellinger 
8330f5e2ec4SNicholas Bellinger 	filp_close(fd_dev->fd_prot_file, NULL);
8340f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = NULL;
8350f5e2ec4SNicholas Bellinger }
8360f5e2ec4SNicholas Bellinger 
8379e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = {
8380c2ad7d1SChristoph Hellwig 	.execute_rw		= fd_execute_rw,
839ad67f0d9SChristoph Hellwig 	.execute_sync_cache	= fd_execute_sync_cache,
8407b745c84SNicholas Bellinger 	.execute_write_same	= fd_execute_write_same,
84170d3ae5cSAsias He 	.execute_write_same_unmap = fd_execute_write_same_unmap,
84250642762SAsias He 	.execute_unmap		= fd_execute_unmap,
8430c2ad7d1SChristoph Hellwig };
8440c2ad7d1SChristoph Hellwig 
845de103c93SChristoph Hellwig static sense_reason_t
846de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd)
8470c2ad7d1SChristoph Hellwig {
8489e999a6cSChristoph Hellwig 	return sbc_parse_cdb(cmd, &fd_sbc_ops);
8490c2ad7d1SChristoph Hellwig }
8500c2ad7d1SChristoph Hellwig 
851b2320497SNicholas Bellinger DEF_TB_DEFAULT_ATTRIBS(fileio);
852b2320497SNicholas Bellinger 
853b2320497SNicholas Bellinger static struct configfs_attribute *fileio_backend_dev_attrs[] = {
854b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_model_alias.attr,
855b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_dpo.attr,
856b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_fua_write.attr,
857b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_fua_read.attr,
858b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_write_cache.attr,
859b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_ua_intlck_ctrl.attr,
860b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_tas.attr,
861b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_tpu.attr,
862b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_tpws.attr,
863b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_caw.attr,
864b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_3pc.attr,
865b2320497SNicholas Bellinger 	&fileio_dev_attrib_pi_prot_type.attr,
866b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_pi_prot_type.attr,
867b2320497SNicholas Bellinger 	&fileio_dev_attrib_pi_prot_format.attr,
868b2320497SNicholas Bellinger 	&fileio_dev_attrib_enforce_pr_isids.attr,
869b2320497SNicholas Bellinger 	&fileio_dev_attrib_is_nonrot.attr,
870b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_rest_reord.attr,
871b2320497SNicholas Bellinger 	&fileio_dev_attrib_force_pr_aptpl.attr,
872b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_block_size.attr,
873b2320497SNicholas Bellinger 	&fileio_dev_attrib_block_size.attr,
874b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_max_sectors.attr,
875b2320497SNicholas Bellinger 	&fileio_dev_attrib_optimal_sectors.attr,
876b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_queue_depth.attr,
877b2320497SNicholas Bellinger 	&fileio_dev_attrib_queue_depth.attr,
878b2320497SNicholas Bellinger 	&fileio_dev_attrib_max_unmap_lba_count.attr,
879b2320497SNicholas Bellinger 	&fileio_dev_attrib_max_unmap_block_desc_count.attr,
880b2320497SNicholas Bellinger 	&fileio_dev_attrib_unmap_granularity.attr,
881b2320497SNicholas Bellinger 	&fileio_dev_attrib_unmap_granularity_alignment.attr,
882b2320497SNicholas Bellinger 	&fileio_dev_attrib_max_write_same_len.attr,
883b2320497SNicholas Bellinger 	NULL,
884b2320497SNicholas Bellinger };
885b2320497SNicholas Bellinger 
886c66ac9dbSNicholas Bellinger static struct se_subsystem_api fileio_template = {
887c66ac9dbSNicholas Bellinger 	.name			= "fileio",
8880fd97ccfSChristoph Hellwig 	.inquiry_prod		= "FILEIO",
8890fd97ccfSChristoph Hellwig 	.inquiry_rev		= FD_VERSION,
890c66ac9dbSNicholas Bellinger 	.owner			= THIS_MODULE,
891c66ac9dbSNicholas Bellinger 	.attach_hba		= fd_attach_hba,
892c66ac9dbSNicholas Bellinger 	.detach_hba		= fd_detach_hba,
8930fd97ccfSChristoph Hellwig 	.alloc_device		= fd_alloc_device,
8940fd97ccfSChristoph Hellwig 	.configure_device	= fd_configure_device,
895c66ac9dbSNicholas Bellinger 	.free_device		= fd_free_device,
8960c2ad7d1SChristoph Hellwig 	.parse_cdb		= fd_parse_cdb,
897c66ac9dbSNicholas Bellinger 	.set_configfs_dev_params = fd_set_configfs_dev_params,
898c66ac9dbSNicholas Bellinger 	.show_configfs_dev_params = fd_show_configfs_dev_params,
8996f23ac8aSChristoph Hellwig 	.get_device_type	= sbc_get_device_type,
900c66ac9dbSNicholas Bellinger 	.get_blocks		= fd_get_blocks,
9010f5e2ec4SNicholas Bellinger 	.init_prot		= fd_init_prot,
9020f5e2ec4SNicholas Bellinger 	.format_prot		= fd_format_prot,
9030f5e2ec4SNicholas Bellinger 	.free_prot		= fd_free_prot,
904c66ac9dbSNicholas Bellinger };
905c66ac9dbSNicholas Bellinger 
906c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void)
907c66ac9dbSNicholas Bellinger {
908b2320497SNicholas Bellinger 	struct target_backend_cits *tbc = &fileio_template.tb_cits;
909b2320497SNicholas Bellinger 
910b2320497SNicholas Bellinger 	target_core_setup_sub_cits(&fileio_template);
911b2320497SNicholas Bellinger 	tbc->tb_dev_attrib_cit.ct_attrs = fileio_backend_dev_attrs;
912b2320497SNicholas Bellinger 
913c66ac9dbSNicholas Bellinger 	return transport_subsystem_register(&fileio_template);
914c66ac9dbSNicholas Bellinger }
915c66ac9dbSNicholas Bellinger 
91663b91d5aSAsias He static void __exit fileio_module_exit(void)
917c66ac9dbSNicholas Bellinger {
918c66ac9dbSNicholas Bellinger 	transport_subsystem_release(&fileio_template);
919c66ac9dbSNicholas Bellinger }
920c66ac9dbSNicholas Bellinger 
921c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
922c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
923c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
924c66ac9dbSNicholas Bellinger 
925c66ac9dbSNicholas Bellinger module_init(fileio_module_init);
926c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit);
927