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>
40c66ac9dbSNicholas Bellinger 
41c66ac9dbSNicholas Bellinger #include "target_core_file.h"
42c66ac9dbSNicholas Bellinger 
430fd97ccfSChristoph Hellwig static inline struct fd_dev *FD_DEV(struct se_device *dev)
440fd97ccfSChristoph Hellwig {
450fd97ccfSChristoph Hellwig 	return container_of(dev, struct fd_dev, dev);
460fd97ccfSChristoph Hellwig }
47c66ac9dbSNicholas Bellinger 
48c66ac9dbSNicholas Bellinger /*	fd_attach_hba(): (Part of se_subsystem_api_t template)
49c66ac9dbSNicholas Bellinger  *
50c66ac9dbSNicholas Bellinger  *
51c66ac9dbSNicholas Bellinger  */
52c66ac9dbSNicholas Bellinger static int fd_attach_hba(struct se_hba *hba, u32 host_id)
53c66ac9dbSNicholas Bellinger {
54c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host;
55c66ac9dbSNicholas Bellinger 
56c66ac9dbSNicholas Bellinger 	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
576708bb27SAndy Grover 	if (!fd_host) {
586708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_host\n");
59e3d6f909SAndy Grover 		return -ENOMEM;
60c66ac9dbSNicholas Bellinger 	}
61c66ac9dbSNicholas Bellinger 
62c66ac9dbSNicholas Bellinger 	fd_host->fd_host_id = host_id;
63c66ac9dbSNicholas Bellinger 
64e3d6f909SAndy Grover 	hba->hba_ptr = fd_host;
65c66ac9dbSNicholas Bellinger 
666708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
67c66ac9dbSNicholas Bellinger 		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
68c66ac9dbSNicholas Bellinger 		TARGET_CORE_MOD_VERSION);
6995cadaceSNicholas Bellinger 	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
7095cadaceSNicholas Bellinger 		hba->hba_id, fd_host->fd_host_id);
71c66ac9dbSNicholas Bellinger 
72c66ac9dbSNicholas Bellinger 	return 0;
73c66ac9dbSNicholas Bellinger }
74c66ac9dbSNicholas Bellinger 
75c66ac9dbSNicholas Bellinger static void fd_detach_hba(struct se_hba *hba)
76c66ac9dbSNicholas Bellinger {
77c66ac9dbSNicholas Bellinger 	struct fd_host *fd_host = hba->hba_ptr;
78c66ac9dbSNicholas Bellinger 
796708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
80c66ac9dbSNicholas Bellinger 		" Target Core\n", hba->hba_id, fd_host->fd_host_id);
81c66ac9dbSNicholas Bellinger 
82c66ac9dbSNicholas Bellinger 	kfree(fd_host);
83c66ac9dbSNicholas Bellinger 	hba->hba_ptr = NULL;
84c66ac9dbSNicholas Bellinger }
85c66ac9dbSNicholas Bellinger 
860fd97ccfSChristoph Hellwig static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
87c66ac9dbSNicholas Bellinger {
88c66ac9dbSNicholas Bellinger 	struct fd_dev *fd_dev;
898359cf43SJörn Engel 	struct fd_host *fd_host = hba->hba_ptr;
90c66ac9dbSNicholas Bellinger 
91c66ac9dbSNicholas Bellinger 	fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
926708bb27SAndy Grover 	if (!fd_dev) {
936708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct fd_dev\n");
94c66ac9dbSNicholas Bellinger 		return NULL;
95c66ac9dbSNicholas Bellinger 	}
96c66ac9dbSNicholas Bellinger 
97c66ac9dbSNicholas Bellinger 	fd_dev->fd_host = fd_host;
98c66ac9dbSNicholas Bellinger 
996708bb27SAndy Grover 	pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
100c66ac9dbSNicholas Bellinger 
1010fd97ccfSChristoph Hellwig 	return &fd_dev->dev;
102c66ac9dbSNicholas Bellinger }
103c66ac9dbSNicholas Bellinger 
1040fd97ccfSChristoph Hellwig static int fd_configure_device(struct se_device *dev)
105c66ac9dbSNicholas Bellinger {
1060fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
1070fd97ccfSChristoph Hellwig 	struct fd_host *fd_host = dev->se_hba->hba_ptr;
108c66ac9dbSNicholas Bellinger 	struct file *file;
109c66ac9dbSNicholas Bellinger 	struct inode *inode = NULL;
1100fd97ccfSChristoph Hellwig 	int flags, ret = -EINVAL;
111c66ac9dbSNicholas Bellinger 
1120fd97ccfSChristoph Hellwig 	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
1130fd97ccfSChristoph Hellwig 		pr_err("Missing fd_dev_name=\n");
1140fd97ccfSChristoph Hellwig 		return -EINVAL;
1150fd97ccfSChristoph Hellwig 	}
116c66ac9dbSNicholas Bellinger 
117c66ac9dbSNicholas Bellinger 	/*
118a4dff304SNicholas Bellinger 	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
119a4dff304SNicholas Bellinger 	 * of pure timestamp updates.
120c66ac9dbSNicholas Bellinger 	 */
121a4dff304SNicholas Bellinger 	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
1220fd97ccfSChristoph Hellwig 
123b32f4c7eSNicholas Bellinger 	/*
124b32f4c7eSNicholas Bellinger 	 * Optionally allow fd_buffered_io=1 to be enabled for people
125b32f4c7eSNicholas Bellinger 	 * who want use the fs buffer cache as an WriteCache mechanism.
126b32f4c7eSNicholas Bellinger 	 *
127b32f4c7eSNicholas Bellinger 	 * This means that in event of a hard failure, there is a risk
128b32f4c7eSNicholas Bellinger 	 * of silent data-loss if the SCSI client has *not* performed a
129b32f4c7eSNicholas Bellinger 	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
130b32f4c7eSNicholas Bellinger 	 * to write-out the entire device cache.
131b32f4c7eSNicholas Bellinger 	 */
132b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
133b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
134b32f4c7eSNicholas Bellinger 		flags &= ~O_DSYNC;
135b32f4c7eSNicholas Bellinger 	}
136c66ac9dbSNicholas Bellinger 
137dbc6e022SAl Viro 	file = filp_open(fd_dev->fd_dev_name, flags, 0600);
138613640e4SNicholas Bellinger 	if (IS_ERR(file)) {
139dbc6e022SAl Viro 		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
140613640e4SNicholas Bellinger 		ret = PTR_ERR(file);
141613640e4SNicholas Bellinger 		goto fail;
142613640e4SNicholas Bellinger 	}
143c66ac9dbSNicholas Bellinger 	fd_dev->fd_file = file;
144c66ac9dbSNicholas Bellinger 	/*
145c66ac9dbSNicholas Bellinger 	 * If using a block backend with this struct file, we extract
146c66ac9dbSNicholas Bellinger 	 * fd_dev->fd_[block,dev]_size from struct block_device.
147c66ac9dbSNicholas Bellinger 	 *
148c66ac9dbSNicholas Bellinger 	 * Otherwise, we use the passed fd_size= from configfs
149c66ac9dbSNicholas Bellinger 	 */
150c66ac9dbSNicholas Bellinger 	inode = file->f_mapping->host;
151c66ac9dbSNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
1520fd97ccfSChristoph Hellwig 		struct request_queue *q = bdev_get_queue(inode->i_bdev);
153cd9323fdSNicholas Bellinger 		unsigned long long dev_size;
1540fd97ccfSChristoph Hellwig 
15521363ca8SNicholas Bellinger 		fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
156c66ac9dbSNicholas Bellinger 		/*
157c66ac9dbSNicholas Bellinger 		 * Determine the number of bytes from i_size_read() minus
158c66ac9dbSNicholas Bellinger 		 * one (1) logical sector from underlying struct block_device
159c66ac9dbSNicholas Bellinger 		 */
160cd9323fdSNicholas Bellinger 		dev_size = (i_size_read(file->f_mapping->host) -
161c66ac9dbSNicholas Bellinger 				       fd_dev->fd_block_size);
162c66ac9dbSNicholas Bellinger 
1636708bb27SAndy Grover 		pr_debug("FILEIO: Using size: %llu bytes from struct"
164c66ac9dbSNicholas Bellinger 			" block_device blocks: %llu logical_block_size: %d\n",
165cd9323fdSNicholas Bellinger 			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
166c66ac9dbSNicholas Bellinger 			fd_dev->fd_block_size);
16770d3ae5cSAsias He 		/*
16870d3ae5cSAsias He 		 * Check if the underlying struct block_device request_queue supports
16970d3ae5cSAsias He 		 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
17070d3ae5cSAsias He 		 * in ATA and we need to set TPE=1
17170d3ae5cSAsias He 		 */
17270d3ae5cSAsias He 		if (blk_queue_discard(q)) {
17370d3ae5cSAsias He 			dev->dev_attrib.max_unmap_lba_count =
17470d3ae5cSAsias He 				q->limits.max_discard_sectors;
17570d3ae5cSAsias He 			/*
17670d3ae5cSAsias He 			 * Currently hardcoded to 1 in Linux/SCSI code..
17770d3ae5cSAsias He 			 */
17870d3ae5cSAsias He 			dev->dev_attrib.max_unmap_block_desc_count = 1;
17970d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity =
18070d3ae5cSAsias He 				q->limits.discard_granularity >> 9;
18170d3ae5cSAsias He 			dev->dev_attrib.unmap_granularity_alignment =
18270d3ae5cSAsias He 				q->limits.discard_alignment;
18370d3ae5cSAsias He 			pr_debug("IFILE: BLOCK Discard support available,"
18470d3ae5cSAsias He 					" disabled by default\n");
18570d3ae5cSAsias He 		}
18670d3ae5cSAsias He 		/*
18770d3ae5cSAsias He 		 * Enable write same emulation for IBLOCK and use 0xFFFF as
18870d3ae5cSAsias He 		 * the smaller WRITE_SAME(10) only has a two-byte block count.
18970d3ae5cSAsias He 		 */
19070d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0xFFFF;
1910463a3feSAsias He 
1920463a3feSAsias He 		if (blk_queue_nonrot(q))
1930463a3feSAsias He 			dev->dev_attrib.is_nonrot = 1;
194c66ac9dbSNicholas Bellinger 	} else {
195c66ac9dbSNicholas Bellinger 		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
1966708bb27SAndy Grover 			pr_err("FILEIO: Missing fd_dev_size="
197c66ac9dbSNicholas Bellinger 				" parameter, and no backing struct"
198c66ac9dbSNicholas Bellinger 				" block_device\n");
199c66ac9dbSNicholas Bellinger 			goto fail;
200c66ac9dbSNicholas Bellinger 		}
201c66ac9dbSNicholas Bellinger 
20221363ca8SNicholas Bellinger 		fd_dev->fd_block_size = FD_BLOCKSIZE;
20370d3ae5cSAsias He 		/*
20470d3ae5cSAsias He 		 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
20570d3ae5cSAsias He 		 */
20670d3ae5cSAsias He 		dev->dev_attrib.max_unmap_lba_count = 0x2000;
20770d3ae5cSAsias He 		/*
20870d3ae5cSAsias He 		 * Currently hardcoded to 1 in Linux/SCSI code..
20970d3ae5cSAsias He 		 */
21070d3ae5cSAsias He 		dev->dev_attrib.max_unmap_block_desc_count = 1;
21170d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity = 1;
21270d3ae5cSAsias He 		dev->dev_attrib.unmap_granularity_alignment = 0;
21370d3ae5cSAsias He 
21470d3ae5cSAsias He 		/*
21570d3ae5cSAsias He 		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
21670d3ae5cSAsias He 		 * based upon struct iovec limit for vfs_writev()
21770d3ae5cSAsias He 		 */
21870d3ae5cSAsias He 		dev->dev_attrib.max_write_same_len = 0x1000;
219c66ac9dbSNicholas Bellinger 	}
220c66ac9dbSNicholas Bellinger 
22121363ca8SNicholas Bellinger 	dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
22295cadaceSNicholas Bellinger 	dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
22395cadaceSNicholas Bellinger 	dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
2240fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
225c66ac9dbSNicholas Bellinger 
226b32f4c7eSNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
227b32f4c7eSNicholas Bellinger 		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
228b32f4c7eSNicholas Bellinger 			" with FDBD_HAS_BUFFERED_IO_WCE\n");
2290fd97ccfSChristoph Hellwig 		dev->dev_attrib.emulate_write_cache = 1;
230b32f4c7eSNicholas Bellinger 	}
231b32f4c7eSNicholas Bellinger 
232c66ac9dbSNicholas Bellinger 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
233c66ac9dbSNicholas Bellinger 	fd_dev->fd_queue_depth = dev->queue_depth;
234c66ac9dbSNicholas Bellinger 
2356708bb27SAndy Grover 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
236c66ac9dbSNicholas Bellinger 		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
237c66ac9dbSNicholas Bellinger 			fd_dev->fd_dev_name, fd_dev->fd_dev_size);
238c66ac9dbSNicholas Bellinger 
2390fd97ccfSChristoph Hellwig 	return 0;
240c66ac9dbSNicholas Bellinger fail:
241c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
242c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
243c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
244c66ac9dbSNicholas Bellinger 	}
2450fd97ccfSChristoph Hellwig 	return ret;
246c66ac9dbSNicholas Bellinger }
247c66ac9dbSNicholas Bellinger 
2480fd97ccfSChristoph Hellwig static void fd_free_device(struct se_device *dev)
249c66ac9dbSNicholas Bellinger {
2500fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
251c66ac9dbSNicholas Bellinger 
252c66ac9dbSNicholas Bellinger 	if (fd_dev->fd_file) {
253c66ac9dbSNicholas Bellinger 		filp_close(fd_dev->fd_file, NULL);
254c66ac9dbSNicholas Bellinger 		fd_dev->fd_file = NULL;
255c66ac9dbSNicholas Bellinger 	}
256c66ac9dbSNicholas Bellinger 
257c66ac9dbSNicholas Bellinger 	kfree(fd_dev);
258c66ac9dbSNicholas Bellinger }
259c66ac9dbSNicholas Bellinger 
26042201b55SNicholas Bellinger static int fd_do_prot_rw(struct se_cmd *cmd, struct fd_prot *fd_prot,
26142201b55SNicholas Bellinger 			 int is_write)
26242201b55SNicholas Bellinger {
26342201b55SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
26442201b55SNicholas Bellinger 	struct fd_dev *dev = FD_DEV(se_dev);
26542201b55SNicholas Bellinger 	struct file *prot_fd = dev->fd_prot_file;
26642201b55SNicholas Bellinger 	struct scatterlist *sg;
26742201b55SNicholas Bellinger 	loff_t pos = (cmd->t_task_lba * se_dev->prot_length);
26842201b55SNicholas Bellinger 	unsigned char *buf;
26942201b55SNicholas Bellinger 	u32 prot_size, len, size;
27042201b55SNicholas Bellinger 	int rc, ret = 1, i;
27142201b55SNicholas Bellinger 
27242201b55SNicholas Bellinger 	prot_size = (cmd->data_length / se_dev->dev_attrib.block_size) *
27342201b55SNicholas Bellinger 		     se_dev->prot_length;
27442201b55SNicholas Bellinger 
27542201b55SNicholas Bellinger 	if (!is_write) {
27642201b55SNicholas Bellinger 		fd_prot->prot_buf = vzalloc(prot_size);
27742201b55SNicholas Bellinger 		if (!fd_prot->prot_buf) {
27842201b55SNicholas Bellinger 			pr_err("Unable to allocate fd_prot->prot_buf\n");
27942201b55SNicholas Bellinger 			return -ENOMEM;
28042201b55SNicholas Bellinger 		}
28142201b55SNicholas Bellinger 		buf = fd_prot->prot_buf;
28242201b55SNicholas Bellinger 
28342201b55SNicholas Bellinger 		fd_prot->prot_sg_nents = cmd->t_prot_nents;
28442201b55SNicholas Bellinger 		fd_prot->prot_sg = kzalloc(sizeof(struct scatterlist) *
28542201b55SNicholas Bellinger 					   fd_prot->prot_sg_nents, GFP_KERNEL);
28642201b55SNicholas Bellinger 		if (!fd_prot->prot_sg) {
28742201b55SNicholas Bellinger 			pr_err("Unable to allocate fd_prot->prot_sg\n");
28842201b55SNicholas Bellinger 			vfree(fd_prot->prot_buf);
28942201b55SNicholas Bellinger 			return -ENOMEM;
29042201b55SNicholas Bellinger 		}
29142201b55SNicholas Bellinger 		size = prot_size;
29242201b55SNicholas Bellinger 
29342201b55SNicholas Bellinger 		for_each_sg(fd_prot->prot_sg, sg, fd_prot->prot_sg_nents, i) {
29442201b55SNicholas Bellinger 
29542201b55SNicholas Bellinger 			len = min_t(u32, PAGE_SIZE, size);
29642201b55SNicholas Bellinger 			sg_set_buf(sg, buf, len);
29742201b55SNicholas Bellinger 			size -= len;
29842201b55SNicholas Bellinger 			buf += len;
29942201b55SNicholas Bellinger 		}
30042201b55SNicholas Bellinger 	}
30142201b55SNicholas Bellinger 
30242201b55SNicholas Bellinger 	if (is_write) {
30342201b55SNicholas Bellinger 		rc = kernel_write(prot_fd, fd_prot->prot_buf, prot_size, pos);
30442201b55SNicholas Bellinger 		if (rc < 0 || prot_size != rc) {
30542201b55SNicholas Bellinger 			pr_err("kernel_write() for fd_do_prot_rw failed:"
30642201b55SNicholas Bellinger 			       " %d\n", rc);
30742201b55SNicholas Bellinger 			ret = -EINVAL;
30842201b55SNicholas Bellinger 		}
30942201b55SNicholas Bellinger 	} else {
31042201b55SNicholas Bellinger 		rc = kernel_read(prot_fd, pos, fd_prot->prot_buf, prot_size);
31142201b55SNicholas Bellinger 		if (rc < 0) {
31242201b55SNicholas Bellinger 			pr_err("kernel_read() for fd_do_prot_rw failed:"
31342201b55SNicholas Bellinger 			       " %d\n", rc);
31442201b55SNicholas Bellinger 			ret = -EINVAL;
31542201b55SNicholas Bellinger 		}
31642201b55SNicholas Bellinger 	}
31742201b55SNicholas Bellinger 
31842201b55SNicholas Bellinger 	if (is_write || ret < 0) {
31942201b55SNicholas Bellinger 		kfree(fd_prot->prot_sg);
32042201b55SNicholas Bellinger 		vfree(fd_prot->prot_buf);
32142201b55SNicholas Bellinger 	}
32242201b55SNicholas Bellinger 
32342201b55SNicholas Bellinger 	return ret;
32442201b55SNicholas Bellinger }
32542201b55SNicholas Bellinger 
326778229afSSebastian Andrzej Siewior static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
327778229afSSebastian Andrzej Siewior 		u32 sgl_nents, int is_write)
328c66ac9dbSNicholas Bellinger {
3295787cacdSChristoph Hellwig 	struct se_device *se_dev = cmd->se_dev;
3300fd97ccfSChristoph Hellwig 	struct fd_dev *dev = FD_DEV(se_dev);
3316708bb27SAndy Grover 	struct file *fd = dev->fd_file;
3325787cacdSChristoph Hellwig 	struct scatterlist *sg;
333c66ac9dbSNicholas Bellinger 	struct iovec *iov;
334c66ac9dbSNicholas Bellinger 	mm_segment_t old_fs;
3350fd97ccfSChristoph Hellwig 	loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
336c66ac9dbSNicholas Bellinger 	int ret = 0, i;
337c66ac9dbSNicholas Bellinger 
3385787cacdSChristoph Hellwig 	iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
3396708bb27SAndy Grover 	if (!iov) {
3406708bb27SAndy Grover 		pr_err("Unable to allocate fd_do_readv iov[]\n");
341e3d6f909SAndy Grover 		return -ENOMEM;
342c66ac9dbSNicholas Bellinger 	}
343c66ac9dbSNicholas Bellinger 
3445787cacdSChristoph Hellwig 	for_each_sg(sgl, sg, sgl_nents, i) {
3459649fa1bSSebastian Andrzej Siewior 		iov[i].iov_len = sg->length;
34640ff2c3bSSebastian Andrzej Siewior 		iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
347c66ac9dbSNicholas Bellinger 	}
348c66ac9dbSNicholas Bellinger 
349c66ac9dbSNicholas Bellinger 	old_fs = get_fs();
350c66ac9dbSNicholas Bellinger 	set_fs(get_ds());
351778229afSSebastian Andrzej Siewior 
352778229afSSebastian Andrzej Siewior 	if (is_write)
353778229afSSebastian Andrzej Siewior 		ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
354778229afSSebastian Andrzej Siewior 	else
3555787cacdSChristoph Hellwig 		ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
356778229afSSebastian Andrzej Siewior 
357c66ac9dbSNicholas Bellinger 	set_fs(old_fs);
358c66ac9dbSNicholas Bellinger 
35940ff2c3bSSebastian Andrzej Siewior 	for_each_sg(sgl, sg, sgl_nents, i)
36040ff2c3bSSebastian Andrzej Siewior 		kunmap(sg_page(sg));
361778229afSSebastian Andrzej Siewior 
362c66ac9dbSNicholas Bellinger 	kfree(iov);
363778229afSSebastian Andrzej Siewior 
364778229afSSebastian Andrzej Siewior 	if (is_write) {
365778229afSSebastian Andrzej Siewior 		if (ret < 0 || ret != cmd->data_length) {
366778229afSSebastian Andrzej Siewior 			pr_err("%s() write returned %d\n", __func__, ret);
367778229afSSebastian Andrzej Siewior 			return (ret < 0 ? ret : -EINVAL);
368778229afSSebastian Andrzej Siewior 		}
369778229afSSebastian Andrzej Siewior 	} else {
370c66ac9dbSNicholas Bellinger 		/*
371c66ac9dbSNicholas Bellinger 		 * Return zeros and GOOD status even if the READ did not return
372c66ac9dbSNicholas Bellinger 		 * the expected virt_size for struct file w/o a backing struct
373c66ac9dbSNicholas Bellinger 		 * block_device.
374c66ac9dbSNicholas Bellinger 		 */
375496ad9aaSAl Viro 		if (S_ISBLK(file_inode(fd)->i_mode)) {
3765787cacdSChristoph Hellwig 			if (ret < 0 || ret != cmd->data_length) {
377778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d, expecting %u for "
378778229afSSebastian Andrzej Siewior 						"S_ISBLK\n", __func__, ret,
379778229afSSebastian Andrzej Siewior 						cmd->data_length);
380e3d6f909SAndy Grover 				return (ret < 0 ? ret : -EINVAL);
381c66ac9dbSNicholas Bellinger 			}
382c66ac9dbSNicholas Bellinger 		} else {
383c66ac9dbSNicholas Bellinger 			if (ret < 0) {
384778229afSSebastian Andrzej Siewior 				pr_err("%s() returned %d for non S_ISBLK\n",
385778229afSSebastian Andrzej Siewior 						__func__, ret);
386e3d6f909SAndy Grover 				return ret;
387c66ac9dbSNicholas Bellinger 			}
388c66ac9dbSNicholas Bellinger 		}
389c66ac9dbSNicholas Bellinger 	}
390c66ac9dbSNicholas Bellinger 	return 1;
391c66ac9dbSNicholas Bellinger }
392c66ac9dbSNicholas Bellinger 
393de103c93SChristoph Hellwig static sense_reason_t
394de103c93SChristoph Hellwig fd_execute_sync_cache(struct se_cmd *cmd)
395c66ac9dbSNicholas Bellinger {
396c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
3970fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
398a1d8b49aSAndy Grover 	int immed = (cmd->t_task_cdb[1] & 0x2);
399c66ac9dbSNicholas Bellinger 	loff_t start, end;
400c66ac9dbSNicholas Bellinger 	int ret;
401c66ac9dbSNicholas Bellinger 
402c66ac9dbSNicholas Bellinger 	/*
403c66ac9dbSNicholas Bellinger 	 * If the Immediate bit is set, queue up the GOOD response
404c66ac9dbSNicholas Bellinger 	 * for this SYNCHRONIZE_CACHE op
405c66ac9dbSNicholas Bellinger 	 */
406c66ac9dbSNicholas Bellinger 	if (immed)
4075787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
408c66ac9dbSNicholas Bellinger 
409c66ac9dbSNicholas Bellinger 	/*
410c66ac9dbSNicholas Bellinger 	 * Determine if we will be flushing the entire device.
411c66ac9dbSNicholas Bellinger 	 */
412a1d8b49aSAndy Grover 	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
413c66ac9dbSNicholas Bellinger 		start = 0;
414c66ac9dbSNicholas Bellinger 		end = LLONG_MAX;
415c66ac9dbSNicholas Bellinger 	} else {
4160fd97ccfSChristoph Hellwig 		start = cmd->t_task_lba * dev->dev_attrib.block_size;
417c66ac9dbSNicholas Bellinger 		if (cmd->data_length)
418c66ac9dbSNicholas Bellinger 			end = start + cmd->data_length;
419c66ac9dbSNicholas Bellinger 		else
420c66ac9dbSNicholas Bellinger 			end = LLONG_MAX;
421c66ac9dbSNicholas Bellinger 	}
422c66ac9dbSNicholas Bellinger 
423c66ac9dbSNicholas Bellinger 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
424c66ac9dbSNicholas Bellinger 	if (ret != 0)
4256708bb27SAndy Grover 		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
426c66ac9dbSNicholas Bellinger 
4275787cacdSChristoph Hellwig 	if (immed)
428ad67f0d9SChristoph Hellwig 		return 0;
4295787cacdSChristoph Hellwig 
430de103c93SChristoph Hellwig 	if (ret)
4315787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
432de103c93SChristoph Hellwig 	else
4335787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
434ad67f0d9SChristoph Hellwig 
435ad67f0d9SChristoph Hellwig 	return 0;
436c66ac9dbSNicholas Bellinger }
437c66ac9dbSNicholas Bellinger 
4387b745c84SNicholas Bellinger static unsigned char *
4397b745c84SNicholas Bellinger fd_setup_write_same_buf(struct se_cmd *cmd, struct scatterlist *sg,
4407b745c84SNicholas Bellinger 		    unsigned int len)
4417b745c84SNicholas Bellinger {
4427b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
4437b745c84SNicholas Bellinger 	unsigned int block_size = se_dev->dev_attrib.block_size;
4447b745c84SNicholas Bellinger 	unsigned int i = 0, end;
4457b745c84SNicholas Bellinger 	unsigned char *buf, *p, *kmap_buf;
4467b745c84SNicholas Bellinger 
4477b745c84SNicholas Bellinger 	buf = kzalloc(min_t(unsigned int, len, PAGE_SIZE), GFP_KERNEL);
4487b745c84SNicholas Bellinger 	if (!buf) {
4497b745c84SNicholas Bellinger 		pr_err("Unable to allocate fd_execute_write_same buf\n");
4507b745c84SNicholas Bellinger 		return NULL;
4517b745c84SNicholas Bellinger 	}
4527b745c84SNicholas Bellinger 
4537b745c84SNicholas Bellinger 	kmap_buf = kmap(sg_page(sg)) + sg->offset;
4547b745c84SNicholas Bellinger 	if (!kmap_buf) {
4557b745c84SNicholas Bellinger 		pr_err("kmap() failed in fd_setup_write_same\n");
4567b745c84SNicholas Bellinger 		kfree(buf);
4577b745c84SNicholas Bellinger 		return NULL;
4587b745c84SNicholas Bellinger 	}
4597b745c84SNicholas Bellinger 	/*
4607b745c84SNicholas Bellinger 	 * Fill local *buf to contain multiple WRITE_SAME blocks up to
4617b745c84SNicholas Bellinger 	 * min(len, PAGE_SIZE)
4627b745c84SNicholas Bellinger 	 */
4637b745c84SNicholas Bellinger 	p = buf;
4647b745c84SNicholas Bellinger 	end = min_t(unsigned int, len, PAGE_SIZE);
4657b745c84SNicholas Bellinger 
4667b745c84SNicholas Bellinger 	while (i < end) {
4677b745c84SNicholas Bellinger 		memcpy(p, kmap_buf, block_size);
4687b745c84SNicholas Bellinger 
4697b745c84SNicholas Bellinger 		i += block_size;
4707b745c84SNicholas Bellinger 		p += block_size;
4717b745c84SNicholas Bellinger 	}
4727b745c84SNicholas Bellinger 	kunmap(sg_page(sg));
4737b745c84SNicholas Bellinger 
4747b745c84SNicholas Bellinger 	return buf;
4757b745c84SNicholas Bellinger }
4767b745c84SNicholas Bellinger 
4777b745c84SNicholas Bellinger static sense_reason_t
4787b745c84SNicholas Bellinger fd_execute_write_same(struct se_cmd *cmd)
4797b745c84SNicholas Bellinger {
4807b745c84SNicholas Bellinger 	struct se_device *se_dev = cmd->se_dev;
4817b745c84SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(se_dev);
4827b745c84SNicholas Bellinger 	struct file *f = fd_dev->fd_file;
4837b745c84SNicholas Bellinger 	struct scatterlist *sg;
4847b745c84SNicholas Bellinger 	struct iovec *iov;
4857b745c84SNicholas Bellinger 	mm_segment_t old_fs;
486972b29c8SRoland Dreier 	sector_t nolb = sbc_get_write_same_sectors(cmd);
4877b745c84SNicholas Bellinger 	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
4887b745c84SNicholas Bellinger 	unsigned int len, len_tmp, iov_num;
4897b745c84SNicholas Bellinger 	int i, rc;
4907b745c84SNicholas Bellinger 	unsigned char *buf;
4917b745c84SNicholas Bellinger 
4927b745c84SNicholas Bellinger 	if (!nolb) {
4937b745c84SNicholas Bellinger 		target_complete_cmd(cmd, SAM_STAT_GOOD);
4947b745c84SNicholas Bellinger 		return 0;
4957b745c84SNicholas Bellinger 	}
4967b745c84SNicholas Bellinger 	sg = &cmd->t_data_sg[0];
4977b745c84SNicholas Bellinger 
4987b745c84SNicholas Bellinger 	if (cmd->t_data_nents > 1 ||
4997b745c84SNicholas Bellinger 	    sg->length != cmd->se_dev->dev_attrib.block_size) {
5007b745c84SNicholas Bellinger 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
5017b745c84SNicholas Bellinger 			" block_size: %u\n", cmd->t_data_nents, sg->length,
5027b745c84SNicholas Bellinger 			cmd->se_dev->dev_attrib.block_size);
5037b745c84SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
5047b745c84SNicholas Bellinger 	}
5057b745c84SNicholas Bellinger 
5067b745c84SNicholas Bellinger 	len = len_tmp = nolb * se_dev->dev_attrib.block_size;
5077b745c84SNicholas Bellinger 	iov_num = DIV_ROUND_UP(len, PAGE_SIZE);
5087b745c84SNicholas Bellinger 
5097b745c84SNicholas Bellinger 	buf = fd_setup_write_same_buf(cmd, sg, len);
5107b745c84SNicholas Bellinger 	if (!buf)
5117b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
5127b745c84SNicholas Bellinger 
5137b745c84SNicholas Bellinger 	iov = vzalloc(sizeof(struct iovec) * iov_num);
5147b745c84SNicholas Bellinger 	if (!iov) {
5157b745c84SNicholas Bellinger 		pr_err("Unable to allocate fd_execute_write_same iovecs\n");
5167b745c84SNicholas Bellinger 		kfree(buf);
5177b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
5187b745c84SNicholas Bellinger 	}
5197b745c84SNicholas Bellinger 	/*
5207b745c84SNicholas Bellinger 	 * Map the single fabric received scatterlist block now populated
5217b745c84SNicholas Bellinger 	 * in *buf into each iovec for I/O submission.
5227b745c84SNicholas Bellinger 	 */
5237b745c84SNicholas Bellinger 	for (i = 0; i < iov_num; i++) {
5247b745c84SNicholas Bellinger 		iov[i].iov_base = buf;
5257b745c84SNicholas Bellinger 		iov[i].iov_len = min_t(unsigned int, len_tmp, PAGE_SIZE);
5267b745c84SNicholas Bellinger 		len_tmp -= iov[i].iov_len;
5277b745c84SNicholas Bellinger 	}
5287b745c84SNicholas Bellinger 
5297b745c84SNicholas Bellinger 	old_fs = get_fs();
5307b745c84SNicholas Bellinger 	set_fs(get_ds());
5317b745c84SNicholas Bellinger 	rc = vfs_writev(f, &iov[0], iov_num, &pos);
5327b745c84SNicholas Bellinger 	set_fs(old_fs);
5337b745c84SNicholas Bellinger 
5347b745c84SNicholas Bellinger 	vfree(iov);
5357b745c84SNicholas Bellinger 	kfree(buf);
5367b745c84SNicholas Bellinger 
5377b745c84SNicholas Bellinger 	if (rc < 0 || rc != len) {
5387b745c84SNicholas Bellinger 		pr_err("vfs_writev() returned %d for write same\n", rc);
5397b745c84SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
5407b745c84SNicholas Bellinger 	}
5417b745c84SNicholas Bellinger 
5427b745c84SNicholas Bellinger 	target_complete_cmd(cmd, SAM_STAT_GOOD);
5437b745c84SNicholas Bellinger 	return 0;
5447b745c84SNicholas Bellinger }
5457b745c84SNicholas Bellinger 
546de103c93SChristoph Hellwig static sense_reason_t
54786d71829SAsias He fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
54870d3ae5cSAsias He {
54986d71829SAsias He 	struct file *file = priv;
55070d3ae5cSAsias He 	struct inode *inode = file->f_mapping->host;
55170d3ae5cSAsias He 	int ret;
55270d3ae5cSAsias He 
55370d3ae5cSAsias He 	if (S_ISBLK(inode->i_mode)) {
55470d3ae5cSAsias He 		/* The backend is block device, use discard */
55570d3ae5cSAsias He 		struct block_device *bdev = inode->i_bdev;
55670d3ae5cSAsias He 
55743f55bbbSAsias He 		ret = blkdev_issue_discard(bdev, lba,
55870d3ae5cSAsias He 				nolb, GFP_KERNEL, 0);
55970d3ae5cSAsias He 		if (ret < 0) {
56070d3ae5cSAsias He 			pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
56170d3ae5cSAsias He 				ret);
56270d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
56370d3ae5cSAsias He 		}
56470d3ae5cSAsias He 	} else {
56570d3ae5cSAsias He 		/* The backend is normal file, use fallocate */
56643f55bbbSAsias He 		struct se_device *se_dev = cmd->se_dev;
56743f55bbbSAsias He 		loff_t pos = lba * se_dev->dev_attrib.block_size;
56870d3ae5cSAsias He 		unsigned int len = nolb * se_dev->dev_attrib.block_size;
56970d3ae5cSAsias He 		int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
57070d3ae5cSAsias He 
57170d3ae5cSAsias He 		if (!file->f_op->fallocate)
57270d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
57370d3ae5cSAsias He 
57470d3ae5cSAsias He 		ret = file->f_op->fallocate(file, mode, pos, len);
57570d3ae5cSAsias He 		if (ret < 0) {
57670d3ae5cSAsias He 			pr_warn("FILEIO: fallocate() failed: %d\n", ret);
57770d3ae5cSAsias He 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
57870d3ae5cSAsias He 		}
57970d3ae5cSAsias He 	}
58070d3ae5cSAsias He 
58143f55bbbSAsias He 	return 0;
58243f55bbbSAsias He }
58343f55bbbSAsias He 
58443f55bbbSAsias He static sense_reason_t
58543f55bbbSAsias He fd_execute_write_same_unmap(struct se_cmd *cmd)
58643f55bbbSAsias He {
58743f55bbbSAsias He 	struct se_device *se_dev = cmd->se_dev;
58843f55bbbSAsias He 	struct fd_dev *fd_dev = FD_DEV(se_dev);
58943f55bbbSAsias He 	struct file *file = fd_dev->fd_file;
59043f55bbbSAsias He 	sector_t lba = cmd->t_task_lba;
59143f55bbbSAsias He 	sector_t nolb = sbc_get_write_same_sectors(cmd);
59243f55bbbSAsias He 	int ret;
59343f55bbbSAsias He 
59443f55bbbSAsias He 	if (!nolb) {
59543f55bbbSAsias He 		target_complete_cmd(cmd, SAM_STAT_GOOD);
59643f55bbbSAsias He 		return 0;
59743f55bbbSAsias He 	}
59843f55bbbSAsias He 
59943f55bbbSAsias He 	ret = fd_do_unmap(cmd, file, lba, nolb);
60043f55bbbSAsias He 	if (ret)
60143f55bbbSAsias He 		return ret;
60243f55bbbSAsias He 
60370d3ae5cSAsias He 	target_complete_cmd(cmd, GOOD);
60470d3ae5cSAsias He 	return 0;
60570d3ae5cSAsias He }
60670d3ae5cSAsias He 
60770d3ae5cSAsias He static sense_reason_t
60850642762SAsias He fd_execute_unmap(struct se_cmd *cmd)
60950642762SAsias He {
61086d71829SAsias He 	struct file *file = FD_DEV(cmd->se_dev)->fd_file;
61150642762SAsias He 
61286d71829SAsias He 	return sbc_execute_unmap(cmd, fd_do_unmap, file);
61350642762SAsias He }
61450642762SAsias He 
61550642762SAsias He static sense_reason_t
616a82a9538SNicholas Bellinger fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
617a82a9538SNicholas Bellinger 	      enum dma_data_direction data_direction)
618c66ac9dbSNicholas Bellinger {
619c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
62042201b55SNicholas Bellinger 	struct fd_prot fd_prot;
62142201b55SNicholas Bellinger 	sense_reason_t rc;
622c66ac9dbSNicholas Bellinger 	int ret = 0;
623c66ac9dbSNicholas Bellinger 
624c66ac9dbSNicholas Bellinger 	/*
625c66ac9dbSNicholas Bellinger 	 * Call vectorized fileio functions to map struct scatterlist
626c66ac9dbSNicholas Bellinger 	 * physical memory addresses to struct iovec virtual memory.
627c66ac9dbSNicholas Bellinger 	 */
6285787cacdSChristoph Hellwig 	if (data_direction == DMA_FROM_DEVICE) {
62942201b55SNicholas Bellinger 		memset(&fd_prot, 0, sizeof(struct fd_prot));
63042201b55SNicholas Bellinger 
63142201b55SNicholas Bellinger 		if (cmd->prot_type) {
63242201b55SNicholas Bellinger 			ret = fd_do_prot_rw(cmd, &fd_prot, false);
63342201b55SNicholas Bellinger 			if (ret < 0)
63442201b55SNicholas Bellinger 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
63542201b55SNicholas Bellinger 		}
63642201b55SNicholas Bellinger 
637778229afSSebastian Andrzej Siewior 		ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
63842201b55SNicholas Bellinger 
63942201b55SNicholas Bellinger 		if (ret > 0 && cmd->prot_type) {
64042201b55SNicholas Bellinger 			u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
64142201b55SNicholas Bellinger 
64242201b55SNicholas Bellinger 			rc = sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors,
64342201b55SNicholas Bellinger 						 0, fd_prot.prot_sg, 0);
64442201b55SNicholas Bellinger 			if (rc) {
64542201b55SNicholas Bellinger 				kfree(fd_prot.prot_sg);
64642201b55SNicholas Bellinger 				vfree(fd_prot.prot_buf);
64742201b55SNicholas Bellinger 				return rc;
64842201b55SNicholas Bellinger 			}
64942201b55SNicholas Bellinger 			kfree(fd_prot.prot_sg);
65042201b55SNicholas Bellinger 			vfree(fd_prot.prot_buf);
65142201b55SNicholas Bellinger 		}
652c66ac9dbSNicholas Bellinger 	} else {
65342201b55SNicholas Bellinger 		memset(&fd_prot, 0, sizeof(struct fd_prot));
65442201b55SNicholas Bellinger 
65542201b55SNicholas Bellinger 		if (cmd->prot_type) {
65642201b55SNicholas Bellinger 			u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
65742201b55SNicholas Bellinger 
65842201b55SNicholas Bellinger 			ret = fd_do_prot_rw(cmd, &fd_prot, false);
65942201b55SNicholas Bellinger 			if (ret < 0)
66042201b55SNicholas Bellinger 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
66142201b55SNicholas Bellinger 
66242201b55SNicholas Bellinger 			rc = sbc_dif_verify_write(cmd, cmd->t_task_lba, sectors,
66342201b55SNicholas Bellinger 						  0, fd_prot.prot_sg, 0);
66442201b55SNicholas Bellinger 			if (rc) {
66542201b55SNicholas Bellinger 				kfree(fd_prot.prot_sg);
66642201b55SNicholas Bellinger 				vfree(fd_prot.prot_buf);
66742201b55SNicholas Bellinger 				return rc;
66842201b55SNicholas Bellinger 			}
66942201b55SNicholas Bellinger 		}
67042201b55SNicholas Bellinger 
671778229afSSebastian Andrzej Siewior 		ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
672a4dff304SNicholas Bellinger 		/*
673125d0119SHannes Reinecke 		 * Perform implicit vfs_fsync_range() for fd_do_writev() ops
674a4dff304SNicholas Bellinger 		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
675a4dff304SNicholas Bellinger 		 * Allow this to happen independent of WCE=0 setting.
676a4dff304SNicholas Bellinger 		 */
677c66ac9dbSNicholas Bellinger 		if (ret > 0 &&
6780fd97ccfSChristoph Hellwig 		    dev->dev_attrib.emulate_fua_write > 0 &&
6792d3a4b51SChristoph Hellwig 		    (cmd->se_cmd_flags & SCF_FUA)) {
6800fd97ccfSChristoph Hellwig 			struct fd_dev *fd_dev = FD_DEV(dev);
681a4dff304SNicholas Bellinger 			loff_t start = cmd->t_task_lba *
6820fd97ccfSChristoph Hellwig 				dev->dev_attrib.block_size;
683a4dff304SNicholas Bellinger 			loff_t end = start + cmd->data_length;
684c66ac9dbSNicholas Bellinger 
685a4dff304SNicholas Bellinger 			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
686a4dff304SNicholas Bellinger 		}
687c66ac9dbSNicholas Bellinger 
68842201b55SNicholas Bellinger 		if (ret > 0 && cmd->prot_type) {
68942201b55SNicholas Bellinger 			ret = fd_do_prot_rw(cmd, &fd_prot, true);
690de103c93SChristoph Hellwig 			if (ret < 0)
691de103c93SChristoph Hellwig 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
69242201b55SNicholas Bellinger 		}
69342201b55SNicholas Bellinger 	}
69442201b55SNicholas Bellinger 
69542201b55SNicholas Bellinger 	if (ret < 0) {
69642201b55SNicholas Bellinger 		kfree(fd_prot.prot_sg);
69742201b55SNicholas Bellinger 		vfree(fd_prot.prot_buf);
69842201b55SNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
69942201b55SNicholas Bellinger 	}
700de103c93SChristoph Hellwig 
7015787cacdSChristoph Hellwig 	if (ret)
7025787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
70303e98c9eSNicholas Bellinger 	return 0;
704c66ac9dbSNicholas Bellinger }
705c66ac9dbSNicholas Bellinger 
706c66ac9dbSNicholas Bellinger enum {
707c66ac9dbSNicholas Bellinger 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
708c66ac9dbSNicholas Bellinger };
709c66ac9dbSNicholas Bellinger 
710c66ac9dbSNicholas Bellinger static match_table_t tokens = {
711c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_name, "fd_dev_name=%s"},
712c66ac9dbSNicholas Bellinger 	{Opt_fd_dev_size, "fd_dev_size=%s"},
713b32f4c7eSNicholas Bellinger 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
714c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
715c66ac9dbSNicholas Bellinger };
716c66ac9dbSNicholas Bellinger 
7170fd97ccfSChristoph Hellwig static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
718c66ac9dbSNicholas Bellinger 		const char *page, ssize_t count)
719c66ac9dbSNicholas Bellinger {
7200fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
721c66ac9dbSNicholas Bellinger 	char *orig, *ptr, *arg_p, *opts;
722c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
723b32f4c7eSNicholas Bellinger 	int ret = 0, arg, token;
724c66ac9dbSNicholas Bellinger 
725c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
726c66ac9dbSNicholas Bellinger 	if (!opts)
727c66ac9dbSNicholas Bellinger 		return -ENOMEM;
728c66ac9dbSNicholas Bellinger 
729c66ac9dbSNicholas Bellinger 	orig = opts;
730c66ac9dbSNicholas Bellinger 
73190c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
732c66ac9dbSNicholas Bellinger 		if (!*ptr)
733c66ac9dbSNicholas Bellinger 			continue;
734c66ac9dbSNicholas Bellinger 
735c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
736c66ac9dbSNicholas Bellinger 		switch (token) {
737c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_name:
738dbc6e022SAl Viro 			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
739dbc6e022SAl Viro 				FD_MAX_DEV_NAME) == 0) {
740dbc6e022SAl Viro 				ret = -EINVAL;
7416d180253SJesper Juhl 				break;
7426d180253SJesper Juhl 			}
7436708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Path: %s\n",
744c66ac9dbSNicholas Bellinger 					fd_dev->fd_dev_name);
745c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_PATH;
746c66ac9dbSNicholas Bellinger 			break;
747c66ac9dbSNicholas Bellinger 		case Opt_fd_dev_size:
748c66ac9dbSNicholas Bellinger 			arg_p = match_strdup(&args[0]);
7496d180253SJesper Juhl 			if (!arg_p) {
7506d180253SJesper Juhl 				ret = -ENOMEM;
7516d180253SJesper Juhl 				break;
7526d180253SJesper Juhl 			}
75357103d7fSJingoo Han 			ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
7546d180253SJesper Juhl 			kfree(arg_p);
755c66ac9dbSNicholas Bellinger 			if (ret < 0) {
75657103d7fSJingoo Han 				pr_err("kstrtoull() failed for"
757c66ac9dbSNicholas Bellinger 						" fd_dev_size=\n");
758c66ac9dbSNicholas Bellinger 				goto out;
759c66ac9dbSNicholas Bellinger 			}
7606708bb27SAndy Grover 			pr_debug("FILEIO: Referencing Size: %llu"
761c66ac9dbSNicholas Bellinger 					" bytes\n", fd_dev->fd_dev_size);
762c66ac9dbSNicholas Bellinger 			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
763c66ac9dbSNicholas Bellinger 			break;
764b32f4c7eSNicholas Bellinger 		case Opt_fd_buffered_io:
765ce31c1b0SJoern Engel 			ret = match_int(args, &arg);
766ce31c1b0SJoern Engel 			if (ret)
767ce31c1b0SJoern Engel 				goto out;
768b32f4c7eSNicholas Bellinger 			if (arg != 1) {
769b32f4c7eSNicholas Bellinger 				pr_err("bogus fd_buffered_io=%d value\n", arg);
770b32f4c7eSNicholas Bellinger 				ret = -EINVAL;
771b32f4c7eSNicholas Bellinger 				goto out;
772b32f4c7eSNicholas Bellinger 			}
773b32f4c7eSNicholas Bellinger 
774b32f4c7eSNicholas Bellinger 			pr_debug("FILEIO: Using buffered I/O"
775b32f4c7eSNicholas Bellinger 				" operations for struct fd_dev\n");
776b32f4c7eSNicholas Bellinger 
777b32f4c7eSNicholas Bellinger 			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
778b32f4c7eSNicholas Bellinger 			break;
779c66ac9dbSNicholas Bellinger 		default:
780c66ac9dbSNicholas Bellinger 			break;
781c66ac9dbSNicholas Bellinger 		}
782c66ac9dbSNicholas Bellinger 	}
783c66ac9dbSNicholas Bellinger 
784c66ac9dbSNicholas Bellinger out:
785c66ac9dbSNicholas Bellinger 	kfree(orig);
786c66ac9dbSNicholas Bellinger 	return (!ret) ? count : ret;
787c66ac9dbSNicholas Bellinger }
788c66ac9dbSNicholas Bellinger 
7890fd97ccfSChristoph Hellwig static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
790c66ac9dbSNicholas Bellinger {
7910fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
792c66ac9dbSNicholas Bellinger 	ssize_t bl = 0;
793c66ac9dbSNicholas Bellinger 
794c66ac9dbSNicholas Bellinger 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
795b32f4c7eSNicholas Bellinger 	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s\n",
796b32f4c7eSNicholas Bellinger 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
797b32f4c7eSNicholas Bellinger 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
798b32f4c7eSNicholas Bellinger 		"Buffered-WCE" : "O_DSYNC");
799c66ac9dbSNicholas Bellinger 	return bl;
800c66ac9dbSNicholas Bellinger }
801c66ac9dbSNicholas Bellinger 
802c66ac9dbSNicholas Bellinger static sector_t fd_get_blocks(struct se_device *dev)
803c66ac9dbSNicholas Bellinger {
8040fd97ccfSChristoph Hellwig 	struct fd_dev *fd_dev = FD_DEV(dev);
805cd9323fdSNicholas Bellinger 	struct file *f = fd_dev->fd_file;
806cd9323fdSNicholas Bellinger 	struct inode *i = f->f_mapping->host;
807cd9323fdSNicholas Bellinger 	unsigned long long dev_size;
808cd9323fdSNicholas Bellinger 	/*
809cd9323fdSNicholas Bellinger 	 * When using a file that references an underlying struct block_device,
810cd9323fdSNicholas Bellinger 	 * ensure dev_size is always based on the current inode size in order
811cd9323fdSNicholas Bellinger 	 * to handle underlying block_device resize operations.
812cd9323fdSNicholas Bellinger 	 */
813cd9323fdSNicholas Bellinger 	if (S_ISBLK(i->i_mode))
81421363ca8SNicholas Bellinger 		dev_size = i_size_read(i);
815cd9323fdSNicholas Bellinger 	else
816cd9323fdSNicholas Bellinger 		dev_size = fd_dev->fd_dev_size;
817c66ac9dbSNicholas Bellinger 
81821363ca8SNicholas Bellinger 	return div_u64(dev_size - dev->dev_attrib.block_size,
81921363ca8SNicholas Bellinger 		       dev->dev_attrib.block_size);
820c66ac9dbSNicholas Bellinger }
821c66ac9dbSNicholas Bellinger 
8220f5e2ec4SNicholas Bellinger static int fd_init_prot(struct se_device *dev)
8230f5e2ec4SNicholas Bellinger {
8240f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
8250f5e2ec4SNicholas Bellinger 	struct file *prot_file, *file = fd_dev->fd_file;
8260f5e2ec4SNicholas Bellinger 	struct inode *inode;
8270f5e2ec4SNicholas Bellinger 	int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
8280f5e2ec4SNicholas Bellinger 	char buf[FD_MAX_DEV_PROT_NAME];
8290f5e2ec4SNicholas Bellinger 
8300f5e2ec4SNicholas Bellinger 	if (!file) {
8310f5e2ec4SNicholas Bellinger 		pr_err("Unable to locate fd_dev->fd_file\n");
8320f5e2ec4SNicholas Bellinger 		return -ENODEV;
8330f5e2ec4SNicholas Bellinger 	}
8340f5e2ec4SNicholas Bellinger 
8350f5e2ec4SNicholas Bellinger 	inode = file->f_mapping->host;
8360f5e2ec4SNicholas Bellinger 	if (S_ISBLK(inode->i_mode)) {
8370f5e2ec4SNicholas Bellinger 		pr_err("FILEIO Protection emulation only supported on"
8380f5e2ec4SNicholas Bellinger 		       " !S_ISBLK\n");
8390f5e2ec4SNicholas Bellinger 		return -ENOSYS;
8400f5e2ec4SNicholas Bellinger 	}
8410f5e2ec4SNicholas Bellinger 
8420f5e2ec4SNicholas Bellinger 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
8430f5e2ec4SNicholas Bellinger 		flags &= ~O_DSYNC;
8440f5e2ec4SNicholas Bellinger 
8450f5e2ec4SNicholas Bellinger 	snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
8460f5e2ec4SNicholas Bellinger 		 fd_dev->fd_dev_name);
8470f5e2ec4SNicholas Bellinger 
8480f5e2ec4SNicholas Bellinger 	prot_file = filp_open(buf, flags, 0600);
8490f5e2ec4SNicholas Bellinger 	if (IS_ERR(prot_file)) {
8500f5e2ec4SNicholas Bellinger 		pr_err("filp_open(%s) failed\n", buf);
8510f5e2ec4SNicholas Bellinger 		ret = PTR_ERR(prot_file);
8520f5e2ec4SNicholas Bellinger 		return ret;
8530f5e2ec4SNicholas Bellinger 	}
8540f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = prot_file;
8550f5e2ec4SNicholas Bellinger 
8560f5e2ec4SNicholas Bellinger 	return 0;
8570f5e2ec4SNicholas Bellinger }
8580f5e2ec4SNicholas Bellinger 
8590f5e2ec4SNicholas Bellinger static int fd_format_prot(struct se_device *dev)
8600f5e2ec4SNicholas Bellinger {
8610f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
8620f5e2ec4SNicholas Bellinger 	struct file *prot_fd = fd_dev->fd_prot_file;
8630f5e2ec4SNicholas Bellinger 	sector_t prot_length, prot;
8640f5e2ec4SNicholas Bellinger 	unsigned char *buf;
8650f5e2ec4SNicholas Bellinger 	loff_t pos = 0;
8660f5e2ec4SNicholas Bellinger 	int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
8670f5e2ec4SNicholas Bellinger 	int rc, ret = 0, size, len;
8680f5e2ec4SNicholas Bellinger 
8690f5e2ec4SNicholas Bellinger 	if (!dev->dev_attrib.pi_prot_type) {
8700f5e2ec4SNicholas Bellinger 		pr_err("Unable to format_prot while pi_prot_type == 0\n");
8710f5e2ec4SNicholas Bellinger 		return -ENODEV;
8720f5e2ec4SNicholas Bellinger 	}
8730f5e2ec4SNicholas Bellinger 	if (!prot_fd) {
8740f5e2ec4SNicholas Bellinger 		pr_err("Unable to locate fd_dev->fd_prot_file\n");
8750f5e2ec4SNicholas Bellinger 		return -ENODEV;
8760f5e2ec4SNicholas Bellinger 	}
8770f5e2ec4SNicholas Bellinger 
8780f5e2ec4SNicholas Bellinger 	buf = vzalloc(unit_size);
8790f5e2ec4SNicholas Bellinger 	if (!buf) {
8800f5e2ec4SNicholas Bellinger 		pr_err("Unable to allocate FILEIO prot buf\n");
8810f5e2ec4SNicholas Bellinger 		return -ENOMEM;
8820f5e2ec4SNicholas Bellinger 	}
8830f5e2ec4SNicholas Bellinger 	prot_length = (dev->transport->get_blocks(dev) + 1) * dev->prot_length;
8840f5e2ec4SNicholas Bellinger 	size = prot_length;
8850f5e2ec4SNicholas Bellinger 
8860f5e2ec4SNicholas Bellinger 	pr_debug("Using FILEIO prot_length: %llu\n",
8870f5e2ec4SNicholas Bellinger 		 (unsigned long long)prot_length);
8880f5e2ec4SNicholas Bellinger 
88980dcd0c1SSagi Grimberg 	memset(buf, 0xff, unit_size);
8900f5e2ec4SNicholas Bellinger 	for (prot = 0; prot < prot_length; prot += unit_size) {
8910f5e2ec4SNicholas Bellinger 		len = min(unit_size, size);
8920f5e2ec4SNicholas Bellinger 		rc = kernel_write(prot_fd, buf, len, pos);
8930f5e2ec4SNicholas Bellinger 		if (rc != len) {
8940f5e2ec4SNicholas Bellinger 			pr_err("vfs_write to prot file failed: %d\n", rc);
8950f5e2ec4SNicholas Bellinger 			ret = -ENODEV;
8960f5e2ec4SNicholas Bellinger 			goto out;
8970f5e2ec4SNicholas Bellinger 		}
8980f5e2ec4SNicholas Bellinger 		pos += len;
8990f5e2ec4SNicholas Bellinger 		size -= len;
9000f5e2ec4SNicholas Bellinger 	}
9010f5e2ec4SNicholas Bellinger 
9020f5e2ec4SNicholas Bellinger out:
9030f5e2ec4SNicholas Bellinger 	vfree(buf);
9040f5e2ec4SNicholas Bellinger 	return ret;
9050f5e2ec4SNicholas Bellinger }
9060f5e2ec4SNicholas Bellinger 
9070f5e2ec4SNicholas Bellinger static void fd_free_prot(struct se_device *dev)
9080f5e2ec4SNicholas Bellinger {
9090f5e2ec4SNicholas Bellinger 	struct fd_dev *fd_dev = FD_DEV(dev);
9100f5e2ec4SNicholas Bellinger 
9110f5e2ec4SNicholas Bellinger 	if (!fd_dev->fd_prot_file)
9120f5e2ec4SNicholas Bellinger 		return;
9130f5e2ec4SNicholas Bellinger 
9140f5e2ec4SNicholas Bellinger 	filp_close(fd_dev->fd_prot_file, NULL);
9150f5e2ec4SNicholas Bellinger 	fd_dev->fd_prot_file = NULL;
9160f5e2ec4SNicholas Bellinger }
9170f5e2ec4SNicholas Bellinger 
9189e999a6cSChristoph Hellwig static struct sbc_ops fd_sbc_ops = {
9190c2ad7d1SChristoph Hellwig 	.execute_rw		= fd_execute_rw,
920ad67f0d9SChristoph Hellwig 	.execute_sync_cache	= fd_execute_sync_cache,
9217b745c84SNicholas Bellinger 	.execute_write_same	= fd_execute_write_same,
92270d3ae5cSAsias He 	.execute_write_same_unmap = fd_execute_write_same_unmap,
92350642762SAsias He 	.execute_unmap		= fd_execute_unmap,
9240c2ad7d1SChristoph Hellwig };
9250c2ad7d1SChristoph Hellwig 
926de103c93SChristoph Hellwig static sense_reason_t
927de103c93SChristoph Hellwig fd_parse_cdb(struct se_cmd *cmd)
9280c2ad7d1SChristoph Hellwig {
9299e999a6cSChristoph Hellwig 	return sbc_parse_cdb(cmd, &fd_sbc_ops);
9300c2ad7d1SChristoph Hellwig }
9310c2ad7d1SChristoph Hellwig 
932c66ac9dbSNicholas Bellinger static struct se_subsystem_api fileio_template = {
933c66ac9dbSNicholas Bellinger 	.name			= "fileio",
9340fd97ccfSChristoph Hellwig 	.inquiry_prod		= "FILEIO",
9350fd97ccfSChristoph Hellwig 	.inquiry_rev		= FD_VERSION,
936c66ac9dbSNicholas Bellinger 	.owner			= THIS_MODULE,
937c66ac9dbSNicholas Bellinger 	.transport_type		= TRANSPORT_PLUGIN_VHBA_PDEV,
938c66ac9dbSNicholas Bellinger 	.attach_hba		= fd_attach_hba,
939c66ac9dbSNicholas Bellinger 	.detach_hba		= fd_detach_hba,
9400fd97ccfSChristoph Hellwig 	.alloc_device		= fd_alloc_device,
9410fd97ccfSChristoph Hellwig 	.configure_device	= fd_configure_device,
942c66ac9dbSNicholas Bellinger 	.free_device		= fd_free_device,
9430c2ad7d1SChristoph Hellwig 	.parse_cdb		= fd_parse_cdb,
944c66ac9dbSNicholas Bellinger 	.set_configfs_dev_params = fd_set_configfs_dev_params,
945c66ac9dbSNicholas Bellinger 	.show_configfs_dev_params = fd_show_configfs_dev_params,
9466f23ac8aSChristoph Hellwig 	.get_device_type	= sbc_get_device_type,
947c66ac9dbSNicholas Bellinger 	.get_blocks		= fd_get_blocks,
9480f5e2ec4SNicholas Bellinger 	.init_prot		= fd_init_prot,
9490f5e2ec4SNicholas Bellinger 	.format_prot		= fd_format_prot,
9500f5e2ec4SNicholas Bellinger 	.free_prot		= fd_free_prot,
951c66ac9dbSNicholas Bellinger };
952c66ac9dbSNicholas Bellinger 
953c66ac9dbSNicholas Bellinger static int __init fileio_module_init(void)
954c66ac9dbSNicholas Bellinger {
955c66ac9dbSNicholas Bellinger 	return transport_subsystem_register(&fileio_template);
956c66ac9dbSNicholas Bellinger }
957c66ac9dbSNicholas Bellinger 
95863b91d5aSAsias He static void __exit fileio_module_exit(void)
959c66ac9dbSNicholas Bellinger {
960c66ac9dbSNicholas Bellinger 	transport_subsystem_release(&fileio_template);
961c66ac9dbSNicholas Bellinger }
962c66ac9dbSNicholas Bellinger 
963c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
964c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
965c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
966c66ac9dbSNicholas Bellinger 
967c66ac9dbSNicholas Bellinger module_init(fileio_module_init);
968c66ac9dbSNicholas Bellinger module_exit(fileio_module_exit);
969