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>
35c66ac9dbSNicholas Bellinger #include <scsi/scsi.h>
36c66ac9dbSNicholas Bellinger #include <scsi/scsi_host.h>
3750642762SAsias He #include <asm/unaligned.h>
38c66ac9dbSNicholas Bellinger 
39c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
40c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
41b2320497SNicholas Bellinger #include <target/target_core_backend_configfs.h>
42c66ac9dbSNicholas Bellinger 
43c66ac9dbSNicholas Bellinger #include "target_core_file.h"
44c66ac9dbSNicholas Bellinger 
450fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev)
460fd97ccfSChristoph Hellwig {
470fd97ccfSChristoph Hellwig 	return container_of(dev, struct fd_dev, dev);
480fd97ccfSChristoph Hellwig }
49c66ac9dbSNicholas Bellinger 
50c66ac9dbSNicholas Bellinger /*	fd_attach_hba(): (Part of se_subsystem_api_t template)
51c66ac9dbSNicholas Bellinger  *
52c66ac9dbSNicholas Bellinger  *
53c66ac9dbSNicholas Bellinger  */
54c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id)
55c66ac9dbSNicholas Bellinger {
56c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host;
57c66ac9dbSNicholas Bellinger 
58c66ac9dbSNicholas Bellinger 	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
596708bb27SAndy Grover 	if (!fd_host) {
606708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_host\n");
61e3d6f909SAndy Grover 		return -ENOMEM;
62c66ac9dbSNicholas Bellinger 	}
63c66ac9dbSNicholas Bellinger 
64c66ac9dbSNicholas Bellinger 	fd_host->fd_host_id = host_id;
65c66ac9dbSNicholas Bellinger 
66e3d6f909SAndy Grover 	hba->hba_ptr = fd_host;
67c66ac9dbSNicholas Bellinger 
686708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
69c66ac9dbSNicholas Bellinger 		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
70c66ac9dbSNicholas Bellinger 		TARGET_CORE_MOD_VERSION);
7195cadaceSNicholas Bellinger 	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
7295cadaceSNicholas Bellinger 		hba->hba_id, fd_host->fd_host_id);
73c66ac9dbSNicholas Bellinger 
74c66ac9dbSNicholas Bellinger 	return 0;
75c66ac9dbSNicholas Bellinger }
76c66ac9dbSNicholas Bellinger 
77c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba)
78c66ac9dbSNicholas Bellinger {
79c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host = hba->hba_ptr;
80c66ac9dbSNicholas Bellinger 
816708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
82c66ac9dbSNicholas Bellinger 		" Target Core\n", hba->hba_id, fd_host->fd_host_id);
83c66ac9dbSNicholas Bellinger 
84c66ac9dbSNicholas Bellinger 	kfree(fd_host);
85c66ac9dbSNicholas Bellinger 	hba->hba_ptr = NULL;
86c66ac9dbSNicholas Bellinger }
87c66ac9dbSNicholas Bellinger 
880fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
89c66ac9dbSNicholas Bellinger {
90c66ac9dbSNicholas Bellinger 	struct fd_dev *fd_dev;
918359cf43SJörn Engel 	struct fd_host *fd_host = hba->hba_ptr;
92c66ac9dbSNicholas Bellinger 
93c66ac9dbSNicholas Bellinger 	fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
946708bb27SAndy Grover 	if (!fd_dev) {
956708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_dev\n");
96c66ac9dbSNicholas Bellinger 		return NULL;
97c66ac9dbSNicholas Bellinger 	}
98c66ac9dbSNicholas Bellinger 
99c66ac9dbSNicholas Bellinger 	fd_dev->fd_host = fd_host;
100c66ac9dbSNicholas Bellinger 
1016708bb27SAndy Grover 	pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
102c66ac9dbSNicholas Bellinger 
1030fd97ccfSChristoph Hellwig 	return &fd_dev->dev;
104c66ac9dbSNicholas Bellinger }
105c66ac9dbSNicholas Bellinger 
1060fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev)
107c66ac9dbSNicholas Bellinger {
1080fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
1090fd97ccfSChristoph Hellwig 	struct fd_host *fd_host = dev->se_hba->hba_ptr;
110c66ac9dbSNicholas Bellinger 	struct file *file;
111c66ac9dbSNicholas Bellinger 	struct inode *inode = NULL;
1120fd97ccfSChristoph Hellwig 	int flags, ret = -EINVAL;
113c66ac9dbSNicholas Bellinger 
1140fd97ccfSChristoph Hellwig 	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
1150fd97ccfSChristoph Hellwig 		pr_err("Missing fd_dev_name=\n");
1160fd97ccfSChristoph Hellwig 		return -EINVAL;
1170fd97ccfSChristoph Hellwig 	}
118c66ac9dbSNicholas Bellinger 
119c66ac9dbSNicholas Bellinger 	/*
120a4dff304SNicholas Bellinger 	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
121a4dff304SNicholas Bellinger 	 * of pure timestamp updates.
122c66ac9dbSNicholas Bellinger 	 */
123a4dff304SNicholas Bellinger 	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
1240fd97ccfSChristoph Hellwig 
125b32f4c7eSNicholas Bellinger 	/*
126b32f4c7eSNicholas Bellinger 	 * Optionally allow fd_buffered_io=1 to be enabled for people
127b32f4c7eSNicholas Bellinger 	 * who want use the fs buffer cache as an WriteCache mechanism.
128b32f4c7eSNicholas Bellinger 	 *
129b32f4c7eSNicholas Bellinger 	 * This means that in event of a hard failure, there is a risk
130b32f4c7eSNicholas Bellinger 	 * of silent data-loss if the SCSI client has *not* performed a
131b32f4c7eSNicholas Bellinger 	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
132b32f4c7eSNicholas Bellinger 	 * to write-out the entire device cache.
133b32f4c7eSNicholas Bellinger 	 */
134b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
135b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
136b32f4c7eSNicholas Bellinger 		flags &= ~O_DSYNC;
137b32f4c7eSNicholas Bellinger 	}
138c66ac9dbSNicholas Bellinger 
139dbc6e022SAl Viro 	file = filp_open(fd_dev->fd_dev_name, flags, 0600);
140613640e4SNicholas Bellinger 	if (IS_ERR(file)) {
141dbc6e022SAl Viro 		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
142613640e4SNicholas Bellinger 		ret = PTR_ERR(file);
143613640e4SNicholas Bellinger 		goto fail;
144613640e4SNicholas Bellinger 	}
145c66ac9dbSNicholas Bellinger 	fd_dev->fd_file = file;
146c66ac9dbSNicholas Bellinger 	/*
147c66ac9dbSNicholas Bellinger 	 * If using a block backend with this struct file, we extract
148c66ac9dbSNicholas Bellinger 	 * fd_dev->fd_[block,dev]_size from struct block_device.
149c66ac9dbSNicholas Bellinger 	 *
150c66ac9dbSNicholas Bellinger 	 * Otherwise, we use the passed fd_size= from configfs
151c66ac9dbSNicholas Bellinger 	 */
152c66ac9dbSNicholas Bellinger 	inode = file->f_mapping->host;
153c66ac9dbSNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
1540fd97ccfSChristoph Hellwig 		struct request_queue *q = bdev_get_queue(inode->i_bdev);
155cd9323fdSNicholas Bellinger 		unsigned long long dev_size;
1560fd97ccfSChristoph Hellwig 
15721363ca8SNicholas Bellinger 		fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
158c66ac9dbSNicholas Bellinger 		/*
159c66ac9dbSNicholas Bellinger 		 * Determine the number of bytes from i_size_read() minus
160c66ac9dbSNicholas Bellinger 		 * one (1) logical sector from underlying struct block_device
161c66ac9dbSNicholas Bellinger 		 */
162cd9323fdSNicholas Bellinger 		dev_size = (i_size_read(file->f_mapping->host) -
163c66ac9dbSNicholas Bellinger 				       fd_dev->fd_block_size);
164c66ac9dbSNicholas Bellinger 
1656708bb27SAndy Grover 		pr_debug("FILEIO: Using size: %llu bytes from struct"
166c66ac9dbSNicholas Bellinger 			" block_device blocks: %llu logical_block_size: %d\n",
167cd9323fdSNicholas Bellinger 			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
168c66ac9dbSNicholas Bellinger 			fd_dev->fd_block_size);
16970d3ae5cSAsias He 		/*
17070d3ae5cSAsias He 		 * Check if the underlying struct block_device request_queue supports
17170d3ae5cSAsias He 		 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
17270d3ae5cSAsias He 		 * in ATA and we need to set TPE=1
17370d3ae5cSAsias He 		 */
17470d3ae5cSAsias He 		if (blk_queue_discard(q)) {
17570d3ae5cSAsias He 			dev->dev_attrib.max_unmap_lba_count =
17670d3ae5cSAsias He 				q->limits.max_discard_sectors;
17770d3ae5cSAsias He 			/*
17870d3ae5cSAsias He 			 * Currently hardcoded to 1 in Linux/SCSI code..
17970d3ae5cSAsias He 			 */
18070d3ae5cSAsias He 			dev->dev_attrib.max_unmap_block_desc_count = 1;
18170d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity =
18270d3ae5cSAsias He 				q->limits.discard_granularity >> 9;
18370d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity_alignment =
18470d3ae5cSAsias He 				q->limits.discard_alignment;
18570d3ae5cSAsias He 			pr_debug("IFILE: BLOCK Discard support available,"
18670d3ae5cSAsias He 					" disabled by default\n");
18770d3ae5cSAsias He 		}
18870d3ae5cSAsias He 		/*
18970d3ae5cSAsias He 		 * Enable write same emulation for IBLOCK and use 0xFFFF as
19070d3ae5cSAsias He 		 * the smaller WRITE_SAME(10) only has a two-byte block count.
19170d3ae5cSAsias He 		 */
19270d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0xFFFF;
1930463a3feSAsias He 
1940463a3feSAsias He 		if (blk_queue_nonrot(q))
1950463a3feSAsias He 			dev->dev_attrib.is_nonrot = 1;
196c66ac9dbSNicholas Bellinger 	} else {
197c66ac9dbSNicholas Bellinger 		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
1986708bb27SAndy Grover 			pr_err("FILEIO: Missing fd_dev_size="
199c66ac9dbSNicholas Bellinger 				" parameter, and no backing struct"
200c66ac9dbSNicholas Bellinger 				" block_device\n");
201c66ac9dbSNicholas Bellinger 			goto fail;
202c66ac9dbSNicholas Bellinger 		}
203c66ac9dbSNicholas Bellinger 
20421363ca8SNicholas Bellinger 		fd_dev->fd_block_size = FD_BLOCKSIZE;
20570d3ae5cSAsias He 		/*
20670d3ae5cSAsias He 		 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
20770d3ae5cSAsias He 		 */
20870d3ae5cSAsias He 		dev->dev_attrib.max_unmap_lba_count = 0x2000;
20970d3ae5cSAsias He 		/*
21070d3ae5cSAsias He 		 * Currently hardcoded to 1 in Linux/SCSI code..
21170d3ae5cSAsias He 		 */
21270d3ae5cSAsias He 		dev->dev_attrib.max_unmap_block_desc_count = 1;
21370d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity = 1;
21470d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity_alignment = 0;
21570d3ae5cSAsias He 
21670d3ae5cSAsias He 		/*
21770d3ae5cSAsias He 		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
21870d3ae5cSAsias He 		 * based upon struct iovec limit for vfs_writev()
21970d3ae5cSAsias He 		 */
22070d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0x1000;
221c66ac9dbSNicholas Bellinger 	}
222c66ac9dbSNicholas Bellinger 
22321363ca8SNicholas Bellinger 	dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
22495cadaceSNicholas Bellinger 	dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
22595cadaceSNicholas Bellinger 	dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
2260fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
227c66ac9dbSNicholas Bellinger 
228b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
229b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
230b32f4c7eSNicholas Bellinger 			" with FDBD_HAS_BUFFERED_IO_WCE\n");
2310fd97ccfSChristoph Hellwig 		dev->dev_attrib.emulate_write_cache = 1;
232b32f4c7eSNicholas Bellinger 	}
233b32f4c7eSNicholas Bellinger 
234c66ac9dbSNicholas Bellinger 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
235c66ac9dbSNicholas Bellinger 	fd_dev->fd_queue_depth = dev->queue_depth;
236c66ac9dbSNicholas Bellinger 
2376708bb27SAndy Grover 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
238c66ac9dbSNicholas Bellinger 		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
239c66ac9dbSNicholas Bellinger 			fd_dev->fd_dev_name, fd_dev->fd_dev_size);
240c66ac9dbSNicholas Bellinger 
2410fd97ccfSChristoph Hellwig 	return 0;
242c66ac9dbSNicholas Bellinger fail:
243c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
244c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
245c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
246c66ac9dbSNicholas Bellinger 	}
2470fd97ccfSChristoph Hellwig 	return ret;
248c66ac9dbSNicholas Bellinger }
249c66ac9dbSNicholas Bellinger 
2500fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev)
251c66ac9dbSNicholas Bellinger {
2520fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
253c66ac9dbSNicholas Bellinger 
254c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
255c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
256c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
257c66ac9dbSNicholas Bellinger 	}
258c66ac9dbSNicholas Bellinger 
259c66ac9dbSNicholas Bellinger 	kfree(fd_dev);
260c66ac9dbSNicholas Bellinger }
261c66ac9dbSNicholas Bellinger 
26242201b55SNicholas Bellinger static int fd_do_prot_rw(struct se_cmd *cmd, struct fd_prot *fd_prot,
26342201b55SNicholas Bellinger 			 int is_write)
26442201b55SNicholas Bellinger {
26542201b55SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
26642201b55SNicholas Bellinger 	struct fd_dev *dev = FD_DEV(se_dev);
26742201b55SNicholas Bellinger 	struct file *prot_fd = dev->fd_prot_file;
26842201b55SNicholas Bellinger 	loff_t pos = (cmd->t_task_lba * se_dev->prot_length);
26942201b55SNicholas Bellinger 	unsigned char *buf;
270c8367778SAkinobu Mita 	u32 prot_size;
271c8367778SAkinobu Mita 	int rc, ret = 1;
27242201b55SNicholas Bellinger 
27342201b55SNicholas Bellinger 	prot_size = (cmd->data_length / se_dev->dev_attrib.block_size) *
27442201b55SNicholas Bellinger 		     se_dev->prot_length;
27542201b55SNicholas Bellinger 
27642201b55SNicholas Bellinger 	if (!is_write) {
27738da0f49SAkinobu Mita 		fd_prot->prot_buf = kzalloc(prot_size, GFP_KERNEL);
27842201b55SNicholas Bellinger 		if (!fd_prot->prot_buf) {
27942201b55SNicholas Bellinger 			pr_err("Unable to allocate fd_prot->prot_buf\n");
28042201b55SNicholas Bellinger 			return -ENOMEM;
28142201b55SNicholas Bellinger 		}
28242201b55SNicholas Bellinger 		buf = fd_prot->prot_buf;
28342201b55SNicholas Bellinger 
284c8367778SAkinobu Mita 		fd_prot->prot_sg_nents = 1;
285c8367778SAkinobu Mita 		fd_prot->prot_sg = kzalloc(sizeof(struct scatterlist),
286c8367778SAkinobu Mita 					   GFP_KERNEL);
28742201b55SNicholas Bellinger 		if (!fd_prot->prot_sg) {
28842201b55SNicholas Bellinger 			pr_err("Unable to allocate fd_prot->prot_sg\n");
28938da0f49SAkinobu Mita 			kfree(fd_prot->prot_buf);
29042201b55SNicholas Bellinger 			return -ENOMEM;
29142201b55SNicholas Bellinger 		}
29238da0f49SAkinobu Mita 		sg_init_table(fd_prot->prot_sg, fd_prot->prot_sg_nents);
293c8367778SAkinobu Mita 		sg_set_buf(fd_prot->prot_sg, buf, prot_size);
29442201b55SNicholas Bellinger 	}
29542201b55SNicholas Bellinger 
29642201b55SNicholas Bellinger 	if (is_write) {
29742201b55SNicholas Bellinger 		rc = kernel_write(prot_fd, fd_prot->prot_buf, prot_size, pos);
29842201b55SNicholas Bellinger 		if (rc < 0 || prot_size != rc) {
29942201b55SNicholas Bellinger 			pr_err("kernel_write() for fd_do_prot_rw failed:"
30042201b55SNicholas Bellinger 			       " %d\n", rc);
30142201b55SNicholas Bellinger 			ret = -EINVAL;
30242201b55SNicholas Bellinger 		}
30342201b55SNicholas Bellinger 	} else {
30442201b55SNicholas Bellinger 		rc = kernel_read(prot_fd, pos, fd_prot->prot_buf, prot_size);
30542201b55SNicholas Bellinger 		if (rc < 0) {
30642201b55SNicholas Bellinger 			pr_err("kernel_read() for fd_do_prot_rw failed:"
30742201b55SNicholas Bellinger 			       " %d\n", rc);
30842201b55SNicholas Bellinger 			ret = -EINVAL;
30942201b55SNicholas Bellinger 		}
31042201b55SNicholas Bellinger 	}
31142201b55SNicholas Bellinger 
31242201b55SNicholas Bellinger 	if (is_write || ret < 0) {
31342201b55SNicholas Bellinger 		kfree(fd_prot->prot_sg);
31438da0f49SAkinobu Mita 		kfree(fd_prot->prot_buf);
31542201b55SNicholas Bellinger 	}
31642201b55SNicholas Bellinger 
31742201b55SNicholas Bellinger 	return ret;
31842201b55SNicholas Bellinger }
31942201b55SNicholas Bellinger 
320778229afSSebastian Andrzej Siewior static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
321778229afSSebastian Andrzej Siewior 		u32 sgl_nents, int is_write)
322c66ac9dbSNicholas Bellinger {
3235787cacdSChristoph Hellwig 	struct se_device *se_dev = cmd->se_dev;
3240fd97ccfSChristoph Hellwig 	struct fd_dev *dev = FD_DEV(se_dev);
3256708bb27SAndy Grover 	struct file *fd = dev->fd_file;
3265787cacdSChristoph Hellwig 	struct scatterlist *sg;
3276b9a44d0SChristoph Hellwig 	struct iov_iter iter;
3286b9a44d0SChristoph Hellwig 	struct bio_vec *bvec;
3296b9a44d0SChristoph Hellwig 	ssize_t len = 0;
3300fd97ccfSChristoph Hellwig 	loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
331c66ac9dbSNicholas Bellinger 	int ret = 0, i;
332c66ac9dbSNicholas Bellinger 
3336b9a44d0SChristoph Hellwig 	bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
3346b9a44d0SChristoph Hellwig 	if (!bvec) {
3356708bb27SAndy Grover 		pr_err("Unable to allocate fd_do_readv iov[]\n");
336e3d6f909SAndy Grover 		return -ENOMEM;
337c66ac9dbSNicholas Bellinger 	}
338c66ac9dbSNicholas Bellinger 
3395787cacdSChristoph Hellwig 	for_each_sg(sgl, sg, sgl_nents, i) {
3406b9a44d0SChristoph Hellwig 		bvec[i].bv_page = sg_page(sg);
3416b9a44d0SChristoph Hellwig 		bvec[i].bv_len = sg->length;
3426b9a44d0SChristoph Hellwig 		bvec[i].bv_offset = sg->offset;
3436b9a44d0SChristoph Hellwig 
3446b9a44d0SChristoph Hellwig 		len += sg->length;
345c66ac9dbSNicholas Bellinger 	}
346c66ac9dbSNicholas Bellinger 
3476b9a44d0SChristoph Hellwig 	iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len);
348778229afSSebastian Andrzej Siewior 	if (is_write)
3496b9a44d0SChristoph Hellwig 		ret = vfs_iter_write(fd, &iter, &pos);
350778229afSSebastian Andrzej Siewior 	else
3516b9a44d0SChristoph Hellwig 		ret = vfs_iter_read(fd, &iter, &pos);
352778229afSSebastian Andrzej Siewior 
3536b9a44d0SChristoph Hellwig 	kfree(bvec);
354778229afSSebastian Andrzej Siewior 
355778229afSSebastian Andrzej Siewior 	if (is_write) {
356778229afSSebastian Andrzej Siewior 		if (ret < 0 || ret != cmd->data_length) {
357778229afSSebastian Andrzej Siewior 			pr_err("%s() write returned %d\n", __func__, ret);
358778229afSSebastian Andrzej Siewior 			return (ret < 0 ? ret : -EINVAL);
359778229afSSebastian Andrzej Siewior 		}
360778229afSSebastian Andrzej Siewior 	} else {
361c66ac9dbSNicholas Bellinger 		/*
362c66ac9dbSNicholas Bellinger 		 * Return zeros and GOOD status even if the READ did not return
363c66ac9dbSNicholas Bellinger 		 * the expected virt_size for struct file w/o a backing struct
364c66ac9dbSNicholas Bellinger 		 * block_device.
365c66ac9dbSNicholas Bellinger 		 */
366496ad9aaSAl Viro 		if (S_ISBLK(file_inode(fd)->i_mode)) {
3675787cacdSChristoph Hellwig 			if (ret < 0 || ret != cmd->data_length) {
368778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d, expecting %u for "
369778229afSSebastian Andrzej Siewior 						"S_ISBLK\n", __func__, ret,
370778229afSSebastian Andrzej Siewior 						cmd->data_length);
371e3d6f909SAndy Grover 				return (ret < 0 ? ret : -EINVAL);
372c66ac9dbSNicholas Bellinger 			}
373c66ac9dbSNicholas Bellinger 		} else {
374c66ac9dbSNicholas Bellinger 			if (ret < 0) {
375778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d for non S_ISBLK\n",
376778229afSSebastian Andrzej Siewior 						__func__, ret);
377e3d6f909SAndy Grover 				return ret;
378c66ac9dbSNicholas Bellinger 			}
379c66ac9dbSNicholas Bellinger 		}
380c66ac9dbSNicholas Bellinger 	}
381c66ac9dbSNicholas Bellinger 	return 1;
382c66ac9dbSNicholas Bellinger }
383c66ac9dbSNicholas Bellinger 
384de103c93SChristoph Hellwig static sense_reason_t
385de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd)
386c66ac9dbSNicholas Bellinger {
387c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3880fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
389a1d8b49aSAndy Grover 	int immed = (cmd->t_task_cdb[1] & 0x2);
390c66ac9dbSNicholas Bellinger 	loff_t start, end;
391c66ac9dbSNicholas Bellinger 	int ret;
392c66ac9dbSNicholas Bellinger 
393c66ac9dbSNicholas Bellinger 	/*
394c66ac9dbSNicholas Bellinger 	 * If the Immediate bit is set, queue up the GOOD response
395c66ac9dbSNicholas Bellinger 	 * for this SYNCHRONIZE_CACHE op
396c66ac9dbSNicholas Bellinger 	 */
397c66ac9dbSNicholas Bellinger 	if (immed)
3985787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
399c66ac9dbSNicholas Bellinger 
400c66ac9dbSNicholas Bellinger 	/*
401c66ac9dbSNicholas Bellinger 	 * Determine if we will be flushing the entire device.
402c66ac9dbSNicholas Bellinger 	 */
403a1d8b49aSAndy Grover 	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
404c66ac9dbSNicholas Bellinger 		start = 0;
405c66ac9dbSNicholas Bellinger 		end = LLONG_MAX;
406c66ac9dbSNicholas Bellinger 	} else {
4070fd97ccfSChristoph Hellwig 		start = cmd->t_task_lba * dev->dev_attrib.block_size;
408c66ac9dbSNicholas Bellinger 		if (cmd->data_length)
40962d3ab49SZach Brown 			end = start + cmd->data_length - 1;
410c66ac9dbSNicholas Bellinger 		else
411c66ac9dbSNicholas Bellinger 			end = LLONG_MAX;
412c66ac9dbSNicholas Bellinger 	}
413c66ac9dbSNicholas Bellinger 
414c66ac9dbSNicholas Bellinger 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
415c66ac9dbSNicholas Bellinger 	if (ret != 0)
4166708bb27SAndy Grover 		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
417c66ac9dbSNicholas Bellinger 
4185787cacdSChristoph Hellwig 	if (immed)
419ad67f0d9SChristoph Hellwig 		return 0;
4205787cacdSChristoph Hellwig 
421de103c93SChristoph Hellwig 	if (ret)
4225787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
423de103c93SChristoph Hellwig 	else
4245787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
425ad67f0d9SChristoph Hellwig 
426ad67f0d9SChristoph Hellwig 	return 0;
427c66ac9dbSNicholas Bellinger }
428c66ac9dbSNicholas Bellinger 
4297b745c84SNicholas Bellinger static sense_reason_t
4307b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd)
4317b745c84SNicholas Bellinger {
4327b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
4337b745c84SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(se_dev);
4347b745c84SNicholas Bellinger 	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
435d4c5dcacSChristoph Hellwig 	sector_t nolb = sbc_get_write_same_sectors(cmd);
436d4c5dcacSChristoph Hellwig 	struct iov_iter iter;
437d4c5dcacSChristoph Hellwig 	struct bio_vec *bvec;
438d4c5dcacSChristoph Hellwig 	unsigned int len = 0, i;
439d4c5dcacSChristoph Hellwig 	ssize_t ret;
4407b745c84SNicholas Bellinger 
4417b745c84SNicholas Bellinger 	if (!nolb) {
4427b745c84SNicholas Bellinger 		target_complete_cmd(cmd, SAM_STAT_GOOD);
4437b745c84SNicholas Bellinger 		return 0;
4447b745c84SNicholas Bellinger 	}
445afd73f1bSNicholas Bellinger 	if (cmd->prot_op) {
446afd73f1bSNicholas Bellinger 		pr_err("WRITE_SAME: Protection information with FILEIO"
447afd73f1bSNicholas Bellinger 		       " backends not supported\n");
448afd73f1bSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
449afd73f1bSNicholas Bellinger 	}
4507b745c84SNicholas Bellinger 
4517b745c84SNicholas Bellinger 	if (cmd->t_data_nents > 1 ||
452d4c5dcacSChristoph Hellwig 	    cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {
4537b745c84SNicholas Bellinger 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
454d4c5dcacSChristoph Hellwig 			" block_size: %u\n",
455d4c5dcacSChristoph Hellwig 			cmd->t_data_nents,
456d4c5dcacSChristoph Hellwig 			cmd->t_data_sg[0].length,
4577b745c84SNicholas Bellinger 			cmd->se_dev->dev_attrib.block_size);
4587b745c84SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
4597b745c84SNicholas Bellinger 	}
4607b745c84SNicholas Bellinger 
461d4c5dcacSChristoph Hellwig 	bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
462d4c5dcacSChristoph Hellwig 	if (!bvec)
4637b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4647b745c84SNicholas Bellinger 
465d4c5dcacSChristoph Hellwig 	for (i = 0; i < nolb; i++) {
466d4c5dcacSChristoph Hellwig 		bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]);
467d4c5dcacSChristoph Hellwig 		bvec[i].bv_len = cmd->t_data_sg[0].length;
468d4c5dcacSChristoph Hellwig 		bvec[i].bv_offset = cmd->t_data_sg[0].offset;
469d4c5dcacSChristoph Hellwig 
470d4c5dcacSChristoph Hellwig 		len += se_dev->dev_attrib.block_size;
4717b745c84SNicholas Bellinger 	}
4727b745c84SNicholas Bellinger 
473d4c5dcacSChristoph Hellwig 	iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len);
474d4c5dcacSChristoph Hellwig 	ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos);
4757b745c84SNicholas Bellinger 
476d4c5dcacSChristoph Hellwig 	kfree(bvec);
477d4c5dcacSChristoph Hellwig 	if (ret < 0 || ret != len) {
478d4c5dcacSChristoph Hellwig 		pr_err("vfs_iter_write() returned %zd for write same\n", ret);
4797b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4807b745c84SNicholas Bellinger 	}
4817b745c84SNicholas Bellinger 
4827b745c84SNicholas Bellinger 	target_complete_cmd(cmd, SAM_STAT_GOOD);
4837b745c84SNicholas Bellinger 	return 0;
4847b745c84SNicholas Bellinger }
4857b745c84SNicholas Bellinger 
48664d240b7SAkinobu Mita static int
48764d240b7SAkinobu Mita fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
48864d240b7SAkinobu Mita 		void *buf, size_t bufsize)
48964d240b7SAkinobu Mita {
49064d240b7SAkinobu Mita 	struct fd_dev *fd_dev = FD_DEV(se_dev);
49164d240b7SAkinobu Mita 	struct file *prot_fd = fd_dev->fd_prot_file;
49264d240b7SAkinobu Mita 	sector_t prot_length, prot;
49364d240b7SAkinobu Mita 	loff_t pos = lba * se_dev->prot_length;
49464d240b7SAkinobu Mita 
49564d240b7SAkinobu Mita 	if (!prot_fd) {
49664d240b7SAkinobu Mita 		pr_err("Unable to locate fd_dev->fd_prot_file\n");
49764d240b7SAkinobu Mita 		return -ENODEV;
49864d240b7SAkinobu Mita 	}
49964d240b7SAkinobu Mita 
50064d240b7SAkinobu Mita 	prot_length = nolb * se_dev->prot_length;
50164d240b7SAkinobu Mita 
50264d240b7SAkinobu Mita 	for (prot = 0; prot < prot_length;) {
50364d240b7SAkinobu Mita 		sector_t len = min_t(sector_t, bufsize, prot_length - prot);
50464d240b7SAkinobu Mita 		ssize_t ret = kernel_write(prot_fd, buf, len, pos + prot);
50564d240b7SAkinobu Mita 
50664d240b7SAkinobu Mita 		if (ret != len) {
50764d240b7SAkinobu Mita 			pr_err("vfs_write to prot file failed: %zd\n", ret);
50864d240b7SAkinobu Mita 			return ret < 0 ? ret : -ENODEV;
50964d240b7SAkinobu Mita 		}
51064d240b7SAkinobu Mita 		prot += ret;
51164d240b7SAkinobu Mita 	}
51264d240b7SAkinobu Mita 
51364d240b7SAkinobu Mita 	return 0;
51464d240b7SAkinobu Mita }
51564d240b7SAkinobu Mita 
51664d240b7SAkinobu Mita static int
51764d240b7SAkinobu Mita fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
51864d240b7SAkinobu Mita {
51964d240b7SAkinobu Mita 	void *buf;
52064d240b7SAkinobu Mita 	int rc;
52164d240b7SAkinobu Mita 
52264d240b7SAkinobu Mita 	buf = (void *)__get_free_page(GFP_KERNEL);
52364d240b7SAkinobu Mita 	if (!buf) {
52464d240b7SAkinobu Mita 		pr_err("Unable to allocate FILEIO prot buf\n");
52564d240b7SAkinobu Mita 		return -ENOMEM;
52664d240b7SAkinobu Mita 	}
52764d240b7SAkinobu Mita 	memset(buf, 0xff, PAGE_SIZE);
52864d240b7SAkinobu Mita 
52964d240b7SAkinobu Mita 	rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
53064d240b7SAkinobu Mita 
53164d240b7SAkinobu Mita 	free_page((unsigned long)buf);
53264d240b7SAkinobu Mita 
53364d240b7SAkinobu Mita 	return rc;
53464d240b7SAkinobu Mita }
53564d240b7SAkinobu Mita 
536de103c93SChristoph Hellwig static sense_reason_t
53786d71829SAsias He fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
53870d3ae5cSAsias He {
53986d71829SAsias He 	struct file *file = priv;
54070d3ae5cSAsias He 	struct inode *inode = file->f_mapping->host;
54170d3ae5cSAsias He 	int ret;
54270d3ae5cSAsias He 
54364d240b7SAkinobu Mita 	if (cmd->se_dev->dev_attrib.pi_prot_type) {
54464d240b7SAkinobu Mita 		ret = fd_do_prot_unmap(cmd, lba, nolb);
54564d240b7SAkinobu Mita 		if (ret)
54664d240b7SAkinobu Mita 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
54764d240b7SAkinobu Mita 	}
54864d240b7SAkinobu Mita 
54970d3ae5cSAsias He 	if (S_ISBLK(inode->i_mode)) {
55070d3ae5cSAsias He 		/* The backend is block device, use discard */
55170d3ae5cSAsias He 		struct block_device *bdev = inode->i_bdev;
55270d3ae5cSAsias He 
55343f55bbbSAsias He 		ret = blkdev_issue_discard(bdev, lba,
55470d3ae5cSAsias He 				nolb, GFP_KERNEL, 0);
55570d3ae5cSAsias He 		if (ret < 0) {
55670d3ae5cSAsias He 			pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
55770d3ae5cSAsias He 				ret);
55870d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
55970d3ae5cSAsias He 		}
56070d3ae5cSAsias He 	} else {
56170d3ae5cSAsias He 		/* The backend is normal file, use fallocate */
56243f55bbbSAsias He 		struct se_device *se_dev = cmd->se_dev;
56343f55bbbSAsias He 		loff_t pos = lba * se_dev->dev_attrib.block_size;
56470d3ae5cSAsias He 		unsigned int len = nolb * se_dev->dev_attrib.block_size;
56570d3ae5cSAsias He 		int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
56670d3ae5cSAsias He 
56770d3ae5cSAsias He 		if (!file->f_op->fallocate)
56870d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
56970d3ae5cSAsias He 
57070d3ae5cSAsias He 		ret = file->f_op->fallocate(file, mode, pos, len);
57170d3ae5cSAsias He 		if (ret < 0) {
57270d3ae5cSAsias He 			pr_warn("FILEIO: fallocate() failed: %d\n", ret);
57370d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
57470d3ae5cSAsias He 		}
57570d3ae5cSAsias He 	}
57670d3ae5cSAsias He 
57743f55bbbSAsias He 	return 0;
57843f55bbbSAsias He }
57943f55bbbSAsias He 
58043f55bbbSAsias He static sense_reason_t
58143f55bbbSAsias He fd_execute_write_same_unmap(struct se_cmd *cmd)
58243f55bbbSAsias He {
58343f55bbbSAsias He 	struct se_device *se_dev = cmd->se_dev;
58443f55bbbSAsias He 	struct fd_dev *fd_dev = FD_DEV(se_dev);
58543f55bbbSAsias He 	struct file *file = fd_dev->fd_file;
58643f55bbbSAsias He 	sector_t lba = cmd->t_task_lba;
58743f55bbbSAsias He 	sector_t nolb = sbc_get_write_same_sectors(cmd);
5883abff1e5SChristoph Hellwig 	sense_reason_t ret;
58943f55bbbSAsias He 
59043f55bbbSAsias He 	if (!nolb) {
59143f55bbbSAsias He 		target_complete_cmd(cmd, SAM_STAT_GOOD);
59243f55bbbSAsias He 		return 0;
59343f55bbbSAsias He 	}
59443f55bbbSAsias He 
59543f55bbbSAsias He 	ret = fd_do_unmap(cmd, file, lba, nolb);
59643f55bbbSAsias He 	if (ret)
59743f55bbbSAsias He 		return ret;
59843f55bbbSAsias He 
59970d3ae5cSAsias He 	target_complete_cmd(cmd, GOOD);
60070d3ae5cSAsias He 	return 0;
60170d3ae5cSAsias He }
60270d3ae5cSAsias He 
60370d3ae5cSAsias He static sense_reason_t
60450642762SAsias He fd_execute_unmap(struct se_cmd *cmd)
60550642762SAsias He {
60686d71829SAsias He 	struct file *file = FD_DEV(cmd->se_dev)->fd_file;
60750642762SAsias He 
60886d71829SAsias He 	return sbc_execute_unmap(cmd, fd_do_unmap, file);
60950642762SAsias He }
61050642762SAsias He 
61150642762SAsias He static sense_reason_t
612a82a9538SNicholas Bellinger fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
613a82a9538SNicholas Bellinger 	      enum dma_data_direction data_direction)
614c66ac9dbSNicholas Bellinger {
615c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
61642201b55SNicholas Bellinger 	struct fd_prot fd_prot;
61742201b55SNicholas Bellinger 	sense_reason_t rc;
618c66ac9dbSNicholas Bellinger 	int ret = 0;
619046ba642SNicholas Bellinger 	/*
620046ba642SNicholas Bellinger 	 * We are currently limited by the number of iovecs (2048) per
621046ba642SNicholas Bellinger 	 * single vfs_[writev,readv] call.
622046ba642SNicholas Bellinger 	 */
623046ba642SNicholas Bellinger 	if (cmd->data_length > FD_MAX_BYTES) {
624046ba642SNicholas Bellinger 		pr_err("FILEIO: Not able to process I/O of %u bytes due to"
625046ba642SNicholas Bellinger 		       "FD_MAX_BYTES: %u iovec count limitiation\n",
626046ba642SNicholas Bellinger 			cmd->data_length, FD_MAX_BYTES);
627046ba642SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
628046ba642SNicholas Bellinger 	}
629c66ac9dbSNicholas Bellinger 	/*
630c66ac9dbSNicholas Bellinger 	 * Call vectorized fileio functions to map struct scatterlist
631c66ac9dbSNicholas Bellinger 	 * physical memory addresses to struct iovec virtual memory.
632c66ac9dbSNicholas Bellinger 	 */
6335787cacdSChristoph Hellwig 	if (data_direction == DMA_FROM_DEVICE) {
63442201b55SNicholas Bellinger 		memset(&fd_prot, 0, sizeof(struct fd_prot));
63542201b55SNicholas Bellinger 
636ee920469SNicholas Bellinger 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
63742201b55SNicholas Bellinger 			ret = fd_do_prot_rw(cmd, &fd_prot, false);
63842201b55SNicholas Bellinger 			if (ret < 0)
63942201b55SNicholas Bellinger 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
64042201b55SNicholas Bellinger 		}
64142201b55SNicholas Bellinger 
642778229afSSebastian Andrzej Siewior 		ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
64342201b55SNicholas Bellinger 
644ee920469SNicholas Bellinger 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
64542201b55SNicholas Bellinger 			u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
64642201b55SNicholas Bellinger 
64742201b55SNicholas Bellinger 			rc = sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors,
64842201b55SNicholas Bellinger 						 0, fd_prot.prot_sg, 0);
64942201b55SNicholas Bellinger 			if (rc) {
65042201b55SNicholas Bellinger 				kfree(fd_prot.prot_sg);
65138da0f49SAkinobu Mita 				kfree(fd_prot.prot_buf);
65242201b55SNicholas Bellinger 				return rc;
65342201b55SNicholas Bellinger 			}
65442201b55SNicholas Bellinger 			kfree(fd_prot.prot_sg);
65538da0f49SAkinobu Mita 			kfree(fd_prot.prot_buf);
65642201b55SNicholas Bellinger 		}
657c66ac9dbSNicholas Bellinger 	} else {
65842201b55SNicholas Bellinger 		memset(&fd_prot, 0, sizeof(struct fd_prot));
65942201b55SNicholas Bellinger 
660ee920469SNicholas Bellinger 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
66142201b55SNicholas Bellinger 			u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
66242201b55SNicholas Bellinger 
66342201b55SNicholas Bellinger 			ret = fd_do_prot_rw(cmd, &fd_prot, false);
66442201b55SNicholas Bellinger 			if (ret < 0)
66542201b55SNicholas Bellinger 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
66642201b55SNicholas Bellinger 
66742201b55SNicholas Bellinger 			rc = sbc_dif_verify_write(cmd, cmd->t_task_lba, sectors,
66842201b55SNicholas Bellinger 						  0, fd_prot.prot_sg, 0);
66942201b55SNicholas Bellinger 			if (rc) {
67042201b55SNicholas Bellinger 				kfree(fd_prot.prot_sg);
67138da0f49SAkinobu Mita 				kfree(fd_prot.prot_buf);
67242201b55SNicholas Bellinger 				return rc;
67342201b55SNicholas Bellinger 			}
67442201b55SNicholas Bellinger 		}
67542201b55SNicholas Bellinger 
676778229afSSebastian Andrzej Siewior 		ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
677a4dff304SNicholas Bellinger 		/*
678125d0119SHannes Reinecke 		 * Perform implicit vfs_fsync_range() for fd_do_writev() ops
679a4dff304SNicholas Bellinger 		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
680a4dff304SNicholas Bellinger 		 * Allow this to happen independent of WCE=0 setting.
681a4dff304SNicholas Bellinger 		 */
682c66ac9dbSNicholas Bellinger 		if (ret > 0 &&
6830fd97ccfSChristoph Hellwig 		    dev->dev_attrib.emulate_fua_write > 0 &&
6842d3a4b51SChristoph Hellwig 		    (cmd->se_cmd_flags & SCF_FUA)) {
6850fd97ccfSChristoph Hellwig 			struct fd_dev *fd_dev = FD_DEV(dev);
686a4dff304SNicholas Bellinger 			loff_t start = cmd->t_task_lba *
6870fd97ccfSChristoph Hellwig 				dev->dev_attrib.block_size;
68862d3ab49SZach Brown 			loff_t end;
68962d3ab49SZach Brown 
69062d3ab49SZach Brown 			if (cmd->data_length)
69162d3ab49SZach Brown 				end = start + cmd->data_length - 1;
69262d3ab49SZach Brown 			else
69362d3ab49SZach Brown 				end = LLONG_MAX;
694c66ac9dbSNicholas Bellinger 
695a4dff304SNicholas Bellinger 			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
696a4dff304SNicholas Bellinger 		}
697c66ac9dbSNicholas Bellinger 
698ee920469SNicholas Bellinger 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
69942201b55SNicholas Bellinger 			ret = fd_do_prot_rw(cmd, &fd_prot, true);
700de103c93SChristoph Hellwig 			if (ret < 0)
701de103c93SChristoph Hellwig 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
70242201b55SNicholas Bellinger 		}
70342201b55SNicholas Bellinger 	}
70442201b55SNicholas Bellinger 
70542201b55SNicholas Bellinger 	if (ret < 0) {
70642201b55SNicholas Bellinger 		kfree(fd_prot.prot_sg);
70738da0f49SAkinobu Mita 		kfree(fd_prot.prot_buf);
70842201b55SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
70942201b55SNicholas Bellinger 	}
710de103c93SChristoph Hellwig 
7115787cacdSChristoph Hellwig 	if (ret)
7125787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
71303e98c9eSNicholas Bellinger 	return 0;
714c66ac9dbSNicholas Bellinger }
715c66ac9dbSNicholas Bellinger 
716c66ac9dbSNicholas Bellinger enum {
717c66ac9dbSNicholas Bellinger 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
718c66ac9dbSNicholas Bellinger };
719c66ac9dbSNicholas Bellinger 
720c66ac9dbSNicholas Bellinger static match_table_t tokens = {
721c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_name, "fd_dev_name=%s"},
722c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_size, "fd_dev_size=%s"},
723b32f4c7eSNicholas Bellinger 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
724c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
725c66ac9dbSNicholas Bellinger };
726c66ac9dbSNicholas Bellinger 
7270fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
728c66ac9dbSNicholas Bellinger 		const char *page, ssize_t count)
729c66ac9dbSNicholas Bellinger {
7300fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
731c66ac9dbSNicholas Bellinger 	char *orig, *ptr, *arg_p, *opts;
732c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
733b32f4c7eSNicholas Bellinger 	int ret = 0, arg, token;
734c66ac9dbSNicholas Bellinger 
735c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
736c66ac9dbSNicholas Bellinger 	if (!opts)
737c66ac9dbSNicholas Bellinger 		return -ENOMEM;
738c66ac9dbSNicholas Bellinger 
739c66ac9dbSNicholas Bellinger 	orig = opts;
740c66ac9dbSNicholas Bellinger 
74190c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
742c66ac9dbSNicholas Bellinger 		if (!*ptr)
743c66ac9dbSNicholas Bellinger 			continue;
744c66ac9dbSNicholas Bellinger 
745c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
746c66ac9dbSNicholas Bellinger 		switch (token) {
747c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_name:
748dbc6e022SAl Viro 			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
749dbc6e022SAl Viro 				FD_MAX_DEV_NAME) == 0) {
750dbc6e022SAl Viro 				ret = -EINVAL;
7516d180253SJesper Juhl 				break;
7526d180253SJesper Juhl 			}
7536708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Path: %s\n",
754c66ac9dbSNicholas Bellinger 					fd_dev->fd_dev_name);
755c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_PATH;
756c66ac9dbSNicholas Bellinger 			break;
757c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_size:
758c66ac9dbSNicholas Bellinger 			arg_p = match_strdup(&args[0]);
7596d180253SJesper Juhl 			if (!arg_p) {
7606d180253SJesper Juhl 				ret = -ENOMEM;
7616d180253SJesper Juhl 				break;
7626d180253SJesper Juhl 			}
76357103d7fSJingoo Han 			ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
7646d180253SJesper Juhl 			kfree(arg_p);
765c66ac9dbSNicholas Bellinger 			if (ret < 0) {
76657103d7fSJingoo Han 				pr_err("kstrtoull() failed for"
767c66ac9dbSNicholas Bellinger 						" fd_dev_size=\n");
768c66ac9dbSNicholas Bellinger 				goto out;
769c66ac9dbSNicholas Bellinger 			}
7706708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Size: %llu"
771c66ac9dbSNicholas Bellinger 					" bytes\n", fd_dev->fd_dev_size);
772c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
773c66ac9dbSNicholas Bellinger 			break;
774b32f4c7eSNicholas Bellinger 		case Opt_fd_buffered_io:
775ce31c1b0SJoern Engel 			ret = match_int(args, &arg);
776ce31c1b0SJoern Engel 			if (ret)
777ce31c1b0SJoern Engel 				goto out;
778b32f4c7eSNicholas Bellinger 			if (arg != 1) {
779b32f4c7eSNicholas Bellinger 				pr_err("bogus fd_buffered_io=%d value\n", arg);
780b32f4c7eSNicholas Bellinger 				ret = -EINVAL;
781b32f4c7eSNicholas Bellinger 				goto out;
782b32f4c7eSNicholas Bellinger 			}
783b32f4c7eSNicholas Bellinger 
784b32f4c7eSNicholas Bellinger 			pr_debug("FILEIO: Using buffered I/O"
785b32f4c7eSNicholas Bellinger 				" operations for struct fd_dev\n");
786b32f4c7eSNicholas Bellinger 
787b32f4c7eSNicholas Bellinger 			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
788b32f4c7eSNicholas Bellinger 			break;
789c66ac9dbSNicholas Bellinger 		default:
790c66ac9dbSNicholas Bellinger 			break;
791c66ac9dbSNicholas Bellinger 		}
792c66ac9dbSNicholas Bellinger 	}
793c66ac9dbSNicholas Bellinger 
794c66ac9dbSNicholas Bellinger out:
795c66ac9dbSNicholas Bellinger 	kfree(orig);
796c66ac9dbSNicholas Bellinger 	return (!ret) ? count : ret;
797c66ac9dbSNicholas Bellinger }
798c66ac9dbSNicholas Bellinger 
7990fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
800c66ac9dbSNicholas Bellinger {
8010fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
802c66ac9dbSNicholas Bellinger 	ssize_t bl = 0;
803c66ac9dbSNicholas Bellinger 
804c66ac9dbSNicholas Bellinger 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
805b32f4c7eSNicholas Bellinger 	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s\n",
806b32f4c7eSNicholas Bellinger 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
807b32f4c7eSNicholas Bellinger 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
808b32f4c7eSNicholas Bellinger 		"Buffered-WCE" : "O_DSYNC");
809c66ac9dbSNicholas Bellinger 	return bl;
810c66ac9dbSNicholas Bellinger }
811c66ac9dbSNicholas Bellinger 
812c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev)
813c66ac9dbSNicholas Bellinger {
8140fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
815cd9323fdSNicholas Bellinger 	struct file *f = fd_dev->fd_file;
816cd9323fdSNicholas Bellinger 	struct inode *i = f->f_mapping->host;
817cd9323fdSNicholas Bellinger 	unsigned long long dev_size;
818cd9323fdSNicholas Bellinger 	/*
819cd9323fdSNicholas Bellinger 	 * When using a file that references an underlying struct block_device,
820cd9323fdSNicholas Bellinger 	 * ensure dev_size is always based on the current inode size in order
821cd9323fdSNicholas Bellinger 	 * to handle underlying block_device resize operations.
822cd9323fdSNicholas Bellinger 	 */
823cd9323fdSNicholas Bellinger 	if (S_ISBLK(i->i_mode))
82421363ca8SNicholas Bellinger 		dev_size = i_size_read(i);
825cd9323fdSNicholas Bellinger 	else
826cd9323fdSNicholas Bellinger 		dev_size = fd_dev->fd_dev_size;
827c66ac9dbSNicholas Bellinger 
82821363ca8SNicholas Bellinger 	return div_u64(dev_size - dev->dev_attrib.block_size,
82921363ca8SNicholas Bellinger 		       dev->dev_attrib.block_size);
830c66ac9dbSNicholas Bellinger }
831c66ac9dbSNicholas Bellinger 
8320f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev)
8330f5e2ec4SNicholas Bellinger {
8340f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
8350f5e2ec4SNicholas Bellinger 	struct file *prot_file, *file = fd_dev->fd_file;
8360f5e2ec4SNicholas Bellinger 	struct inode *inode;
8370f5e2ec4SNicholas Bellinger 	int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
8380f5e2ec4SNicholas Bellinger 	char buf[FD_MAX_DEV_PROT_NAME];
8390f5e2ec4SNicholas Bellinger 
8400f5e2ec4SNicholas Bellinger 	if (!file) {
8410f5e2ec4SNicholas Bellinger 		pr_err("Unable to locate fd_dev->fd_file\n");
8420f5e2ec4SNicholas Bellinger 		return -ENODEV;
8430f5e2ec4SNicholas Bellinger 	}
8440f5e2ec4SNicholas Bellinger 
8450f5e2ec4SNicholas Bellinger 	inode = file->f_mapping->host;
8460f5e2ec4SNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
8470f5e2ec4SNicholas Bellinger 		pr_err("FILEIO Protection emulation only supported on"
8480f5e2ec4SNicholas Bellinger 		       " !S_ISBLK\n");
8490f5e2ec4SNicholas Bellinger 		return -ENOSYS;
8500f5e2ec4SNicholas Bellinger 	}
8510f5e2ec4SNicholas Bellinger 
8520f5e2ec4SNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
8530f5e2ec4SNicholas Bellinger 		flags &= ~O_DSYNC;
8540f5e2ec4SNicholas Bellinger 
8550f5e2ec4SNicholas Bellinger 	snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
8560f5e2ec4SNicholas Bellinger 		 fd_dev->fd_dev_name);
8570f5e2ec4SNicholas Bellinger 
8580f5e2ec4SNicholas Bellinger 	prot_file = filp_open(buf, flags, 0600);
8590f5e2ec4SNicholas Bellinger 	if (IS_ERR(prot_file)) {
8600f5e2ec4SNicholas Bellinger 		pr_err("filp_open(%s) failed\n", buf);
8610f5e2ec4SNicholas Bellinger 		ret = PTR_ERR(prot_file);
8620f5e2ec4SNicholas Bellinger 		return ret;
8630f5e2ec4SNicholas Bellinger 	}
8640f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = prot_file;
8650f5e2ec4SNicholas Bellinger 
8660f5e2ec4SNicholas Bellinger 	return 0;
8670f5e2ec4SNicholas Bellinger }
8680f5e2ec4SNicholas Bellinger 
8690f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev)
8700f5e2ec4SNicholas Bellinger {
8710f5e2ec4SNicholas Bellinger 	unsigned char *buf;
8720f5e2ec4SNicholas Bellinger 	int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
87364d240b7SAkinobu Mita 	int ret;
8740f5e2ec4SNicholas Bellinger 
8750f5e2ec4SNicholas Bellinger 	if (!dev->dev_attrib.pi_prot_type) {
8760f5e2ec4SNicholas Bellinger 		pr_err("Unable to format_prot while pi_prot_type == 0\n");
8770f5e2ec4SNicholas Bellinger 		return -ENODEV;
8780f5e2ec4SNicholas Bellinger 	}
8790f5e2ec4SNicholas Bellinger 
8800f5e2ec4SNicholas Bellinger 	buf = vzalloc(unit_size);
8810f5e2ec4SNicholas Bellinger 	if (!buf) {
8820f5e2ec4SNicholas Bellinger 		pr_err("Unable to allocate FILEIO prot buf\n");
8830f5e2ec4SNicholas Bellinger 		return -ENOMEM;
8840f5e2ec4SNicholas Bellinger 	}
8850f5e2ec4SNicholas Bellinger 
8860f5e2ec4SNicholas Bellinger 	pr_debug("Using FILEIO prot_length: %llu\n",
88764d240b7SAkinobu Mita 		 (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
88864d240b7SAkinobu Mita 					dev->prot_length);
8890f5e2ec4SNicholas Bellinger 
89080dcd0c1SSagi Grimberg 	memset(buf, 0xff, unit_size);
89164d240b7SAkinobu Mita 	ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
89264d240b7SAkinobu Mita 			      buf, unit_size);
8930f5e2ec4SNicholas Bellinger 	vfree(buf);
8940f5e2ec4SNicholas Bellinger 	return ret;
8950f5e2ec4SNicholas Bellinger }
8960f5e2ec4SNicholas Bellinger 
8970f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev)
8980f5e2ec4SNicholas Bellinger {
8990f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
9000f5e2ec4SNicholas Bellinger 
9010f5e2ec4SNicholas Bellinger 	if (!fd_dev->fd_prot_file)
9020f5e2ec4SNicholas Bellinger 		return;
9030f5e2ec4SNicholas Bellinger 
9040f5e2ec4SNicholas Bellinger 	filp_close(fd_dev->fd_prot_file, NULL);
9050f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = NULL;
9060f5e2ec4SNicholas Bellinger }
9070f5e2ec4SNicholas Bellinger 
9089e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = {
9090c2ad7d1SChristoph Hellwig 	.execute_rw		= fd_execute_rw,
910ad67f0d9SChristoph Hellwig 	.execute_sync_cache	= fd_execute_sync_cache,
9117b745c84SNicholas Bellinger 	.execute_write_same	= fd_execute_write_same,
91270d3ae5cSAsias He 	.execute_write_same_unmap = fd_execute_write_same_unmap,
91350642762SAsias He 	.execute_unmap		= fd_execute_unmap,
9140c2ad7d1SChristoph Hellwig };
9150c2ad7d1SChristoph Hellwig 
916de103c93SChristoph Hellwig static sense_reason_t
917de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd)
9180c2ad7d1SChristoph Hellwig {
9199e999a6cSChristoph Hellwig 	return sbc_parse_cdb(cmd, &fd_sbc_ops);
9200c2ad7d1SChristoph Hellwig }
9210c2ad7d1SChristoph Hellwig 
922b2320497SNicholas Bellinger DEF_TB_DEFAULT_ATTRIBS(fileio);
923b2320497SNicholas Bellinger 
924b2320497SNicholas Bellinger static struct configfs_attribute *fileio_backend_dev_attrs[] = {
925b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_model_alias.attr,
926b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_dpo.attr,
927b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_fua_write.attr,
928b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_fua_read.attr,
929b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_write_cache.attr,
930b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_ua_intlck_ctrl.attr,
931b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_tas.attr,
932b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_tpu.attr,
933b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_tpws.attr,
934b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_caw.attr,
935b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_3pc.attr,
936b2320497SNicholas Bellinger 	&fileio_dev_attrib_pi_prot_type.attr,
937b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_pi_prot_type.attr,
938b2320497SNicholas Bellinger 	&fileio_dev_attrib_pi_prot_format.attr,
939b2320497SNicholas Bellinger 	&fileio_dev_attrib_enforce_pr_isids.attr,
940b2320497SNicholas Bellinger 	&fileio_dev_attrib_is_nonrot.attr,
941b2320497SNicholas Bellinger 	&fileio_dev_attrib_emulate_rest_reord.attr,
942b2320497SNicholas Bellinger 	&fileio_dev_attrib_force_pr_aptpl.attr,
943b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_block_size.attr,
944b2320497SNicholas Bellinger 	&fileio_dev_attrib_block_size.attr,
945b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_max_sectors.attr,
946b2320497SNicholas Bellinger 	&fileio_dev_attrib_optimal_sectors.attr,
947b2320497SNicholas Bellinger 	&fileio_dev_attrib_hw_queue_depth.attr,
948b2320497SNicholas Bellinger 	&fileio_dev_attrib_queue_depth.attr,
949b2320497SNicholas Bellinger 	&fileio_dev_attrib_max_unmap_lba_count.attr,
950b2320497SNicholas Bellinger 	&fileio_dev_attrib_max_unmap_block_desc_count.attr,
951b2320497SNicholas Bellinger 	&fileio_dev_attrib_unmap_granularity.attr,
952b2320497SNicholas Bellinger 	&fileio_dev_attrib_unmap_granularity_alignment.attr,
953b2320497SNicholas Bellinger 	&fileio_dev_attrib_max_write_same_len.attr,
954b2320497SNicholas Bellinger 	NULL,
955b2320497SNicholas Bellinger };
956b2320497SNicholas Bellinger 
957c66ac9dbSNicholas Bellinger static struct se_subsystem_api fileio_template = {
958c66ac9dbSNicholas Bellinger 	.name			= "fileio",
9590fd97ccfSChristoph Hellwig 	.inquiry_prod		= "FILEIO",
9600fd97ccfSChristoph Hellwig 	.inquiry_rev		= FD_VERSION,
961c66ac9dbSNicholas Bellinger 	.owner			= THIS_MODULE,
962c66ac9dbSNicholas Bellinger 	.transport_type		= TRANSPORT_PLUGIN_VHBA_PDEV,
963c66ac9dbSNicholas Bellinger 	.attach_hba		= fd_attach_hba,
964c66ac9dbSNicholas Bellinger 	.detach_hba		= fd_detach_hba,
9650fd97ccfSChristoph Hellwig 	.alloc_device		= fd_alloc_device,
9660fd97ccfSChristoph Hellwig 	.configure_device	= fd_configure_device,
967c66ac9dbSNicholas Bellinger 	.free_device		= fd_free_device,
9680c2ad7d1SChristoph Hellwig 	.parse_cdb		= fd_parse_cdb,
969c66ac9dbSNicholas Bellinger 	.set_configfs_dev_params = fd_set_configfs_dev_params,
970c66ac9dbSNicholas Bellinger 	.show_configfs_dev_params = fd_show_configfs_dev_params,
9716f23ac8aSChristoph Hellwig 	.get_device_type	= sbc_get_device_type,
972c66ac9dbSNicholas Bellinger 	.get_blocks		= fd_get_blocks,
9730f5e2ec4SNicholas Bellinger 	.init_prot		= fd_init_prot,
9740f5e2ec4SNicholas Bellinger 	.format_prot		= fd_format_prot,
9750f5e2ec4SNicholas Bellinger 	.free_prot		= fd_free_prot,
976c66ac9dbSNicholas Bellinger };
977c66ac9dbSNicholas Bellinger 
978c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void)
979c66ac9dbSNicholas Bellinger {
980b2320497SNicholas Bellinger 	struct target_backend_cits *tbc = &fileio_template.tb_cits;
981b2320497SNicholas Bellinger 
982b2320497SNicholas Bellinger 	target_core_setup_sub_cits(&fileio_template);
983b2320497SNicholas Bellinger 	tbc->tb_dev_attrib_cit.ct_attrs = fileio_backend_dev_attrs;
984b2320497SNicholas Bellinger 
985c66ac9dbSNicholas Bellinger 	return transport_subsystem_register(&fileio_template);
986c66ac9dbSNicholas Bellinger }
987c66ac9dbSNicholas Bellinger 
98863b91d5aSAsias He static void __exit fileio_module_exit(void)
989c66ac9dbSNicholas Bellinger {
990c66ac9dbSNicholas Bellinger 	transport_subsystem_release(&fileio_template);
991c66ac9dbSNicholas Bellinger }
992c66ac9dbSNicholas Bellinger 
993c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
994c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
995c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
996c66ac9dbSNicholas Bellinger 
997c66ac9dbSNicholas Bellinger module_init(fileio_module_init);
998c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit);
999