1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_file.c
4  *
5  * This file contains the Storage Engine <-> FILEIO transport specific functions
6  *
7  * (c) Copyright 2005-2013 Datera, Inc.
8  *
9  * Nicholas A. Bellinger <nab@kernel.org>
10  *
11  ******************************************************************************/
12 
13 #include <linux/string.h>
14 #include <linux/parser.h>
15 #include <linux/timer.h>
16 #include <linux/blkdev.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/module.h>
20 #include <linux/vmalloc.h>
21 #include <linux/falloc.h>
22 #include <linux/uio.h>
23 #include <linux/scatterlist.h>
24 #include <scsi/scsi_proto.h>
25 #include <asm/unaligned.h>
26 
27 #include <target/target_core_base.h>
28 #include <target/target_core_backend.h>
29 
30 #include "target_core_file.h"
31 
32 static inline struct fd_dev *FD_DEV(struct se_device *dev)
33 {
34 	return container_of(dev, struct fd_dev, dev);
35 }
36 
37 static int fd_attach_hba(struct se_hba *hba, u32 host_id)
38 {
39 	struct fd_host *fd_host;
40 
41 	fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
42 	if (!fd_host) {
43 		pr_err("Unable to allocate memory for struct fd_host\n");
44 		return -ENOMEM;
45 	}
46 
47 	fd_host->fd_host_id = host_id;
48 
49 	hba->hba_ptr = fd_host;
50 
51 	pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
52 		" Target Core Stack %s\n", hba->hba_id, FD_VERSION,
53 		TARGET_CORE_VERSION);
54 	pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
55 		hba->hba_id, fd_host->fd_host_id);
56 
57 	return 0;
58 }
59 
60 static void fd_detach_hba(struct se_hba *hba)
61 {
62 	struct fd_host *fd_host = hba->hba_ptr;
63 
64 	pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
65 		" Target Core\n", hba->hba_id, fd_host->fd_host_id);
66 
67 	kfree(fd_host);
68 	hba->hba_ptr = NULL;
69 }
70 
71 static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
72 {
73 	struct fd_dev *fd_dev;
74 	struct fd_host *fd_host = hba->hba_ptr;
75 
76 	fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
77 	if (!fd_dev) {
78 		pr_err("Unable to allocate memory for struct fd_dev\n");
79 		return NULL;
80 	}
81 
82 	fd_dev->fd_host = fd_host;
83 
84 	pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
85 
86 	return &fd_dev->dev;
87 }
88 
89 static int fd_configure_device(struct se_device *dev)
90 {
91 	struct fd_dev *fd_dev = FD_DEV(dev);
92 	struct fd_host *fd_host = dev->se_hba->hba_ptr;
93 	struct file *file;
94 	struct inode *inode = NULL;
95 	int flags, ret = -EINVAL;
96 
97 	if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
98 		pr_err("Missing fd_dev_name=\n");
99 		return -EINVAL;
100 	}
101 
102 	/*
103 	 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
104 	 * of pure timestamp updates.
105 	 */
106 	flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
107 
108 	/*
109 	 * Optionally allow fd_buffered_io=1 to be enabled for people
110 	 * who want use the fs buffer cache as an WriteCache mechanism.
111 	 *
112 	 * This means that in event of a hard failure, there is a risk
113 	 * of silent data-loss if the SCSI client has *not* performed a
114 	 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
115 	 * to write-out the entire device cache.
116 	 */
117 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
118 		pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
119 		flags &= ~O_DSYNC;
120 	}
121 
122 	file = filp_open(fd_dev->fd_dev_name, flags, 0600);
123 	if (IS_ERR(file)) {
124 		pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
125 		ret = PTR_ERR(file);
126 		goto fail;
127 	}
128 	fd_dev->fd_file = file;
129 	/*
130 	 * If using a block backend with this struct file, we extract
131 	 * fd_dev->fd_[block,dev]_size from struct block_device.
132 	 *
133 	 * Otherwise, we use the passed fd_size= from configfs
134 	 */
135 	inode = file->f_mapping->host;
136 	if (S_ISBLK(inode->i_mode)) {
137 		struct block_device *bdev = I_BDEV(inode);
138 		struct request_queue *q = bdev_get_queue(bdev);
139 		unsigned long long dev_size;
140 
141 		fd_dev->fd_block_size = bdev_logical_block_size(bdev);
142 		/*
143 		 * Determine the number of bytes from i_size_read() minus
144 		 * one (1) logical sector from underlying struct block_device
145 		 */
146 		dev_size = (i_size_read(file->f_mapping->host) -
147 				       fd_dev->fd_block_size);
148 
149 		pr_debug("FILEIO: Using size: %llu bytes from struct"
150 			" block_device blocks: %llu logical_block_size: %d\n",
151 			dev_size, div_u64(dev_size, fd_dev->fd_block_size),
152 			fd_dev->fd_block_size);
153 
154 		if (target_configure_unmap_from_queue(&dev->dev_attrib, bdev))
155 			pr_debug("IFILE: BLOCK Discard support available,"
156 				 " disabled by default\n");
157 		/*
158 		 * Enable write same emulation for IBLOCK and use 0xFFFF as
159 		 * the smaller WRITE_SAME(10) only has a two-byte block count.
160 		 */
161 		dev->dev_attrib.max_write_same_len = 0xFFFF;
162 
163 		if (blk_queue_nonrot(q))
164 			dev->dev_attrib.is_nonrot = 1;
165 	} else {
166 		if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
167 			pr_err("FILEIO: Missing fd_dev_size="
168 				" parameter, and no backing struct"
169 				" block_device\n");
170 			goto fail;
171 		}
172 
173 		fd_dev->fd_block_size = FD_BLOCKSIZE;
174 		/*
175 		 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
176 		 */
177 		dev->dev_attrib.max_unmap_lba_count = 0x2000;
178 		/*
179 		 * Currently hardcoded to 1 in Linux/SCSI code..
180 		 */
181 		dev->dev_attrib.max_unmap_block_desc_count = 1;
182 		dev->dev_attrib.unmap_granularity = 1;
183 		dev->dev_attrib.unmap_granularity_alignment = 0;
184 
185 		/*
186 		 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
187 		 * based upon struct iovec limit for vfs_writev()
188 		 */
189 		dev->dev_attrib.max_write_same_len = 0x1000;
190 	}
191 
192 	dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
193 	dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
194 	dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
195 	dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
196 
197 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
198 		pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
199 			" with FDBD_HAS_BUFFERED_IO_WCE\n");
200 		dev->dev_attrib.emulate_write_cache = 1;
201 	}
202 
203 	fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
204 	fd_dev->fd_queue_depth = dev->queue_depth;
205 
206 	pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
207 		" %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
208 			fd_dev->fd_dev_name, fd_dev->fd_dev_size);
209 
210 	return 0;
211 fail:
212 	if (fd_dev->fd_file) {
213 		filp_close(fd_dev->fd_file, NULL);
214 		fd_dev->fd_file = NULL;
215 	}
216 	return ret;
217 }
218 
219 static void fd_dev_call_rcu(struct rcu_head *p)
220 {
221 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
222 	struct fd_dev *fd_dev = FD_DEV(dev);
223 
224 	kfree(fd_dev);
225 }
226 
227 static void fd_free_device(struct se_device *dev)
228 {
229 	call_rcu(&dev->rcu_head, fd_dev_call_rcu);
230 }
231 
232 static void fd_destroy_device(struct se_device *dev)
233 {
234 	struct fd_dev *fd_dev = FD_DEV(dev);
235 
236 	if (fd_dev->fd_file) {
237 		filp_close(fd_dev->fd_file, NULL);
238 		fd_dev->fd_file = NULL;
239 	}
240 }
241 
242 struct target_core_file_cmd {
243 	unsigned long	len;
244 	struct se_cmd	*cmd;
245 	struct kiocb	iocb;
246 	struct bio_vec	bvecs[];
247 };
248 
249 static void cmd_rw_aio_complete(struct kiocb *iocb, long ret)
250 {
251 	struct target_core_file_cmd *cmd;
252 
253 	cmd = container_of(iocb, struct target_core_file_cmd, iocb);
254 
255 	if (ret != cmd->len)
256 		target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION);
257 	else
258 		target_complete_cmd(cmd->cmd, SAM_STAT_GOOD);
259 
260 	kfree(cmd);
261 }
262 
263 static sense_reason_t
264 fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
265 	      enum dma_data_direction data_direction)
266 {
267 	int is_write = !(data_direction == DMA_FROM_DEVICE);
268 	struct se_device *dev = cmd->se_dev;
269 	struct fd_dev *fd_dev = FD_DEV(dev);
270 	struct file *file = fd_dev->fd_file;
271 	struct target_core_file_cmd *aio_cmd;
272 	struct iov_iter iter;
273 	struct scatterlist *sg;
274 	ssize_t len = 0;
275 	int ret = 0, i;
276 
277 	aio_cmd = kmalloc(struct_size(aio_cmd, bvecs, sgl_nents), GFP_KERNEL);
278 	if (!aio_cmd)
279 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
280 
281 	for_each_sg(sgl, sg, sgl_nents, i) {
282 		aio_cmd->bvecs[i].bv_page = sg_page(sg);
283 		aio_cmd->bvecs[i].bv_len = sg->length;
284 		aio_cmd->bvecs[i].bv_offset = sg->offset;
285 
286 		len += sg->length;
287 	}
288 
289 	iov_iter_bvec(&iter, is_write, aio_cmd->bvecs, sgl_nents, len);
290 
291 	aio_cmd->cmd = cmd;
292 	aio_cmd->len = len;
293 	aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size;
294 	aio_cmd->iocb.ki_filp = file;
295 	aio_cmd->iocb.ki_complete = cmd_rw_aio_complete;
296 	aio_cmd->iocb.ki_flags = IOCB_DIRECT;
297 
298 	if (is_write && (cmd->se_cmd_flags & SCF_FUA))
299 		aio_cmd->iocb.ki_flags |= IOCB_DSYNC;
300 
301 	if (is_write)
302 		ret = call_write_iter(file, &aio_cmd->iocb, &iter);
303 	else
304 		ret = call_read_iter(file, &aio_cmd->iocb, &iter);
305 
306 	if (ret != -EIOCBQUEUED)
307 		cmd_rw_aio_complete(&aio_cmd->iocb, ret);
308 
309 	return 0;
310 }
311 
312 static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
313 		    u32 block_size, struct scatterlist *sgl,
314 		    u32 sgl_nents, u32 data_length, int is_write)
315 {
316 	struct scatterlist *sg;
317 	struct iov_iter iter;
318 	struct bio_vec *bvec;
319 	ssize_t len = 0;
320 	loff_t pos = (cmd->t_task_lba * block_size);
321 	int ret = 0, i;
322 
323 	bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
324 	if (!bvec) {
325 		pr_err("Unable to allocate fd_do_readv iov[]\n");
326 		return -ENOMEM;
327 	}
328 
329 	for_each_sg(sgl, sg, sgl_nents, i) {
330 		bvec[i].bv_page = sg_page(sg);
331 		bvec[i].bv_len = sg->length;
332 		bvec[i].bv_offset = sg->offset;
333 
334 		len += sg->length;
335 	}
336 
337 	iov_iter_bvec(&iter, READ, bvec, sgl_nents, len);
338 	if (is_write)
339 		ret = vfs_iter_write(fd, &iter, &pos, 0);
340 	else
341 		ret = vfs_iter_read(fd, &iter, &pos, 0);
342 
343 	if (is_write) {
344 		if (ret < 0 || ret != data_length) {
345 			pr_err("%s() write returned %d\n", __func__, ret);
346 			if (ret >= 0)
347 				ret = -EINVAL;
348 		}
349 	} else {
350 		/*
351 		 * Return zeros and GOOD status even if the READ did not return
352 		 * the expected virt_size for struct file w/o a backing struct
353 		 * block_device.
354 		 */
355 		if (S_ISBLK(file_inode(fd)->i_mode)) {
356 			if (ret < 0 || ret != data_length) {
357 				pr_err("%s() returned %d, expecting %u for "
358 						"S_ISBLK\n", __func__, ret,
359 						data_length);
360 				if (ret >= 0)
361 					ret = -EINVAL;
362 			}
363 		} else {
364 			if (ret < 0) {
365 				pr_err("%s() returned %d for non S_ISBLK\n",
366 						__func__, ret);
367 			} else if (ret != data_length) {
368 				/*
369 				 * Short read case:
370 				 * Probably some one truncate file under us.
371 				 * We must explicitly zero sg-pages to prevent
372 				 * expose uninizialized pages to userspace.
373 				 */
374 				if (ret < data_length)
375 					ret += iov_iter_zero(data_length - ret, &iter);
376 				else
377 					ret = -EINVAL;
378 			}
379 		}
380 	}
381 	kfree(bvec);
382 	return ret;
383 }
384 
385 static sense_reason_t
386 fd_execute_sync_cache(struct se_cmd *cmd)
387 {
388 	struct se_device *dev = cmd->se_dev;
389 	struct fd_dev *fd_dev = FD_DEV(dev);
390 	int immed = (cmd->t_task_cdb[1] & 0x2);
391 	loff_t start, end;
392 	int ret;
393 
394 	/*
395 	 * If the Immediate bit is set, queue up the GOOD response
396 	 * for this SYNCHRONIZE_CACHE op
397 	 */
398 	if (immed)
399 		target_complete_cmd(cmd, SAM_STAT_GOOD);
400 
401 	/*
402 	 * Determine if we will be flushing the entire device.
403 	 */
404 	if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
405 		start = 0;
406 		end = LLONG_MAX;
407 	} else {
408 		start = cmd->t_task_lba * dev->dev_attrib.block_size;
409 		if (cmd->data_length)
410 			end = start + cmd->data_length - 1;
411 		else
412 			end = LLONG_MAX;
413 	}
414 
415 	ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
416 	if (ret != 0)
417 		pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
418 
419 	if (immed)
420 		return 0;
421 
422 	if (ret)
423 		target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
424 	else
425 		target_complete_cmd(cmd, SAM_STAT_GOOD);
426 
427 	return 0;
428 }
429 
430 static sense_reason_t
431 fd_execute_write_same(struct se_cmd *cmd)
432 {
433 	struct se_device *se_dev = cmd->se_dev;
434 	struct fd_dev *fd_dev = FD_DEV(se_dev);
435 	loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
436 	sector_t nolb = sbc_get_write_same_sectors(cmd);
437 	struct iov_iter iter;
438 	struct bio_vec *bvec;
439 	unsigned int len = 0, i;
440 	ssize_t ret;
441 
442 	if (!nolb) {
443 		target_complete_cmd(cmd, SAM_STAT_GOOD);
444 		return 0;
445 	}
446 	if (cmd->prot_op) {
447 		pr_err("WRITE_SAME: Protection information with FILEIO"
448 		       " backends not supported\n");
449 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
450 	}
451 
452 	if (cmd->t_data_nents > 1 ||
453 	    cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {
454 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
455 			" block_size: %u\n",
456 			cmd->t_data_nents,
457 			cmd->t_data_sg[0].length,
458 			cmd->se_dev->dev_attrib.block_size);
459 		return TCM_INVALID_CDB_FIELD;
460 	}
461 
462 	bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
463 	if (!bvec)
464 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
465 
466 	for (i = 0; i < nolb; i++) {
467 		bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]);
468 		bvec[i].bv_len = cmd->t_data_sg[0].length;
469 		bvec[i].bv_offset = cmd->t_data_sg[0].offset;
470 
471 		len += se_dev->dev_attrib.block_size;
472 	}
473 
474 	iov_iter_bvec(&iter, READ, bvec, nolb, len);
475 	ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos, 0);
476 
477 	kfree(bvec);
478 	if (ret < 0 || ret != len) {
479 		pr_err("vfs_iter_write() returned %zd for write same\n", ret);
480 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
481 	}
482 
483 	target_complete_cmd(cmd, SAM_STAT_GOOD);
484 	return 0;
485 }
486 
487 static int
488 fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
489 		void *buf, size_t bufsize)
490 {
491 	struct fd_dev *fd_dev = FD_DEV(se_dev);
492 	struct file *prot_fd = fd_dev->fd_prot_file;
493 	sector_t prot_length, prot;
494 	loff_t pos = lba * se_dev->prot_length;
495 
496 	if (!prot_fd) {
497 		pr_err("Unable to locate fd_dev->fd_prot_file\n");
498 		return -ENODEV;
499 	}
500 
501 	prot_length = nolb * se_dev->prot_length;
502 
503 	memset(buf, 0xff, bufsize);
504 	for (prot = 0; prot < prot_length;) {
505 		sector_t len = min_t(sector_t, bufsize, prot_length - prot);
506 		ssize_t ret = kernel_write(prot_fd, buf, len, &pos);
507 
508 		if (ret != len) {
509 			pr_err("vfs_write to prot file failed: %zd\n", ret);
510 			return ret < 0 ? ret : -ENODEV;
511 		}
512 		prot += ret;
513 	}
514 
515 	return 0;
516 }
517 
518 static int
519 fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
520 {
521 	void *buf;
522 	int rc;
523 
524 	buf = (void *)__get_free_page(GFP_KERNEL);
525 	if (!buf) {
526 		pr_err("Unable to allocate FILEIO prot buf\n");
527 		return -ENOMEM;
528 	}
529 
530 	rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
531 
532 	free_page((unsigned long)buf);
533 
534 	return rc;
535 }
536 
537 static sense_reason_t
538 fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
539 {
540 	struct file *file = FD_DEV(cmd->se_dev)->fd_file;
541 	struct inode *inode = file->f_mapping->host;
542 	int ret;
543 
544 	if (!nolb) {
545 		return 0;
546 	}
547 
548 	if (cmd->se_dev->dev_attrib.pi_prot_type) {
549 		ret = fd_do_prot_unmap(cmd, lba, nolb);
550 		if (ret)
551 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
552 	}
553 
554 	if (S_ISBLK(inode->i_mode)) {
555 		/* The backend is block device, use discard */
556 		struct block_device *bdev = I_BDEV(inode);
557 		struct se_device *dev = cmd->se_dev;
558 
559 		ret = blkdev_issue_discard(bdev,
560 					   target_to_linux_sector(dev, lba),
561 					   target_to_linux_sector(dev,  nolb),
562 					   GFP_KERNEL, 0);
563 		if (ret < 0) {
564 			pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
565 				ret);
566 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
567 		}
568 	} else {
569 		/* The backend is normal file, use fallocate */
570 		struct se_device *se_dev = cmd->se_dev;
571 		loff_t pos = lba * se_dev->dev_attrib.block_size;
572 		unsigned int len = nolb * se_dev->dev_attrib.block_size;
573 		int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
574 
575 		if (!file->f_op->fallocate)
576 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
577 
578 		ret = file->f_op->fallocate(file, mode, pos, len);
579 		if (ret < 0) {
580 			pr_warn("FILEIO: fallocate() failed: %d\n", ret);
581 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
582 		}
583 	}
584 
585 	return 0;
586 }
587 
588 static sense_reason_t
589 fd_execute_rw_buffered(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
590 	      enum dma_data_direction data_direction)
591 {
592 	struct se_device *dev = cmd->se_dev;
593 	struct fd_dev *fd_dev = FD_DEV(dev);
594 	struct file *file = fd_dev->fd_file;
595 	struct file *pfile = fd_dev->fd_prot_file;
596 	sense_reason_t rc;
597 	int ret = 0;
598 	/*
599 	 * Call vectorized fileio functions to map struct scatterlist
600 	 * physical memory addresses to struct iovec virtual memory.
601 	 */
602 	if (data_direction == DMA_FROM_DEVICE) {
603 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
604 			ret = fd_do_rw(cmd, pfile, dev->prot_length,
605 				       cmd->t_prot_sg, cmd->t_prot_nents,
606 				       cmd->prot_length, 0);
607 			if (ret < 0)
608 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
609 		}
610 
611 		ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
612 			       sgl, sgl_nents, cmd->data_length, 0);
613 
614 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type &&
615 		    dev->dev_attrib.pi_prot_verify) {
616 			u32 sectors = cmd->data_length >>
617 					ilog2(dev->dev_attrib.block_size);
618 
619 			rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
620 					    0, cmd->t_prot_sg, 0);
621 			if (rc)
622 				return rc;
623 		}
624 	} else {
625 		if (cmd->prot_type && dev->dev_attrib.pi_prot_type &&
626 		    dev->dev_attrib.pi_prot_verify) {
627 			u32 sectors = cmd->data_length >>
628 					ilog2(dev->dev_attrib.block_size);
629 
630 			rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
631 					    0, cmd->t_prot_sg, 0);
632 			if (rc)
633 				return rc;
634 		}
635 
636 		ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
637 			       sgl, sgl_nents, cmd->data_length, 1);
638 		/*
639 		 * Perform implicit vfs_fsync_range() for fd_do_writev() ops
640 		 * for SCSI WRITEs with Forced Unit Access (FUA) set.
641 		 * Allow this to happen independent of WCE=0 setting.
642 		 */
643 		if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) {
644 			loff_t start = cmd->t_task_lba *
645 				dev->dev_attrib.block_size;
646 			loff_t end;
647 
648 			if (cmd->data_length)
649 				end = start + cmd->data_length - 1;
650 			else
651 				end = LLONG_MAX;
652 
653 			vfs_fsync_range(fd_dev->fd_file, start, end, 1);
654 		}
655 
656 		if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
657 			ret = fd_do_rw(cmd, pfile, dev->prot_length,
658 				       cmd->t_prot_sg, cmd->t_prot_nents,
659 				       cmd->prot_length, 1);
660 			if (ret < 0)
661 				return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
662 		}
663 	}
664 
665 	if (ret < 0)
666 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
667 
668 	target_complete_cmd(cmd, SAM_STAT_GOOD);
669 	return 0;
670 }
671 
672 static sense_reason_t
673 fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
674 	      enum dma_data_direction data_direction)
675 {
676 	struct se_device *dev = cmd->se_dev;
677 	struct fd_dev *fd_dev = FD_DEV(dev);
678 
679 	/*
680 	 * We are currently limited by the number of iovecs (2048) per
681 	 * single vfs_[writev,readv] call.
682 	 */
683 	if (cmd->data_length > FD_MAX_BYTES) {
684 		pr_err("FILEIO: Not able to process I/O of %u bytes due to"
685 		       "FD_MAX_BYTES: %u iovec count limitation\n",
686 			cmd->data_length, FD_MAX_BYTES);
687 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
688 	}
689 
690 	if (fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO)
691 		return fd_execute_rw_aio(cmd, sgl, sgl_nents, data_direction);
692 	return fd_execute_rw_buffered(cmd, sgl, sgl_nents, data_direction);
693 }
694 
695 enum {
696 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io,
697 	Opt_fd_async_io, Opt_err
698 };
699 
700 static match_table_t tokens = {
701 	{Opt_fd_dev_name, "fd_dev_name=%s"},
702 	{Opt_fd_dev_size, "fd_dev_size=%s"},
703 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
704 	{Opt_fd_async_io, "fd_async_io=%d"},
705 	{Opt_err, NULL}
706 };
707 
708 static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
709 		const char *page, ssize_t count)
710 {
711 	struct fd_dev *fd_dev = FD_DEV(dev);
712 	char *orig, *ptr, *arg_p, *opts;
713 	substring_t args[MAX_OPT_ARGS];
714 	int ret = 0, arg, token;
715 
716 	opts = kstrdup(page, GFP_KERNEL);
717 	if (!opts)
718 		return -ENOMEM;
719 
720 	orig = opts;
721 
722 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
723 		if (!*ptr)
724 			continue;
725 
726 		token = match_token(ptr, tokens, args);
727 		switch (token) {
728 		case Opt_fd_dev_name:
729 			if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
730 				FD_MAX_DEV_NAME) == 0) {
731 				ret = -EINVAL;
732 				break;
733 			}
734 			pr_debug("FILEIO: Referencing Path: %s\n",
735 					fd_dev->fd_dev_name);
736 			fd_dev->fbd_flags |= FBDF_HAS_PATH;
737 			break;
738 		case Opt_fd_dev_size:
739 			arg_p = match_strdup(&args[0]);
740 			if (!arg_p) {
741 				ret = -ENOMEM;
742 				break;
743 			}
744 			ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
745 			kfree(arg_p);
746 			if (ret < 0) {
747 				pr_err("kstrtoull() failed for"
748 						" fd_dev_size=\n");
749 				goto out;
750 			}
751 			pr_debug("FILEIO: Referencing Size: %llu"
752 					" bytes\n", fd_dev->fd_dev_size);
753 			fd_dev->fbd_flags |= FBDF_HAS_SIZE;
754 			break;
755 		case Opt_fd_buffered_io:
756 			ret = match_int(args, &arg);
757 			if (ret)
758 				goto out;
759 			if (arg != 1) {
760 				pr_err("bogus fd_buffered_io=%d value\n", arg);
761 				ret = -EINVAL;
762 				goto out;
763 			}
764 
765 			pr_debug("FILEIO: Using buffered I/O"
766 				" operations for struct fd_dev\n");
767 
768 			fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
769 			break;
770 		case Opt_fd_async_io:
771 			ret = match_int(args, &arg);
772 			if (ret)
773 				goto out;
774 			if (arg != 1) {
775 				pr_err("bogus fd_async_io=%d value\n", arg);
776 				ret = -EINVAL;
777 				goto out;
778 			}
779 
780 			pr_debug("FILEIO: Using async I/O"
781 				" operations for struct fd_dev\n");
782 
783 			fd_dev->fbd_flags |= FDBD_HAS_ASYNC_IO;
784 			break;
785 		default:
786 			break;
787 		}
788 	}
789 
790 out:
791 	kfree(orig);
792 	return (!ret) ? count : ret;
793 }
794 
795 static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
796 {
797 	struct fd_dev *fd_dev = FD_DEV(dev);
798 	ssize_t bl = 0;
799 
800 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
801 	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s Async: %d\n",
802 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
803 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
804 		"Buffered-WCE" : "O_DSYNC",
805 		!!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO));
806 	return bl;
807 }
808 
809 static sector_t fd_get_blocks(struct se_device *dev)
810 {
811 	struct fd_dev *fd_dev = FD_DEV(dev);
812 	struct file *f = fd_dev->fd_file;
813 	struct inode *i = f->f_mapping->host;
814 	unsigned long long dev_size;
815 	/*
816 	 * When using a file that references an underlying struct block_device,
817 	 * ensure dev_size is always based on the current inode size in order
818 	 * to handle underlying block_device resize operations.
819 	 */
820 	if (S_ISBLK(i->i_mode))
821 		dev_size = i_size_read(i);
822 	else
823 		dev_size = fd_dev->fd_dev_size;
824 
825 	return div_u64(dev_size - dev->dev_attrib.block_size,
826 		       dev->dev_attrib.block_size);
827 }
828 
829 static int fd_init_prot(struct se_device *dev)
830 {
831 	struct fd_dev *fd_dev = FD_DEV(dev);
832 	struct file *prot_file, *file = fd_dev->fd_file;
833 	struct inode *inode;
834 	int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
835 	char buf[FD_MAX_DEV_PROT_NAME];
836 
837 	if (!file) {
838 		pr_err("Unable to locate fd_dev->fd_file\n");
839 		return -ENODEV;
840 	}
841 
842 	inode = file->f_mapping->host;
843 	if (S_ISBLK(inode->i_mode)) {
844 		pr_err("FILEIO Protection emulation only supported on"
845 		       " !S_ISBLK\n");
846 		return -ENOSYS;
847 	}
848 
849 	if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
850 		flags &= ~O_DSYNC;
851 
852 	snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
853 		 fd_dev->fd_dev_name);
854 
855 	prot_file = filp_open(buf, flags, 0600);
856 	if (IS_ERR(prot_file)) {
857 		pr_err("filp_open(%s) failed\n", buf);
858 		ret = PTR_ERR(prot_file);
859 		return ret;
860 	}
861 	fd_dev->fd_prot_file = prot_file;
862 
863 	return 0;
864 }
865 
866 static int fd_format_prot(struct se_device *dev)
867 {
868 	unsigned char *buf;
869 	int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
870 	int ret;
871 
872 	if (!dev->dev_attrib.pi_prot_type) {
873 		pr_err("Unable to format_prot while pi_prot_type == 0\n");
874 		return -ENODEV;
875 	}
876 
877 	buf = vzalloc(unit_size);
878 	if (!buf) {
879 		pr_err("Unable to allocate FILEIO prot buf\n");
880 		return -ENOMEM;
881 	}
882 
883 	pr_debug("Using FILEIO prot_length: %llu\n",
884 		 (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
885 					dev->prot_length);
886 
887 	ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
888 			      buf, unit_size);
889 	vfree(buf);
890 	return ret;
891 }
892 
893 static void fd_free_prot(struct se_device *dev)
894 {
895 	struct fd_dev *fd_dev = FD_DEV(dev);
896 
897 	if (!fd_dev->fd_prot_file)
898 		return;
899 
900 	filp_close(fd_dev->fd_prot_file, NULL);
901 	fd_dev->fd_prot_file = NULL;
902 }
903 
904 static struct sbc_ops fd_sbc_ops = {
905 	.execute_rw		= fd_execute_rw,
906 	.execute_sync_cache	= fd_execute_sync_cache,
907 	.execute_write_same	= fd_execute_write_same,
908 	.execute_unmap		= fd_execute_unmap,
909 };
910 
911 static sense_reason_t
912 fd_parse_cdb(struct se_cmd *cmd)
913 {
914 	return sbc_parse_cdb(cmd, &fd_sbc_ops);
915 }
916 
917 static const struct target_backend_ops fileio_ops = {
918 	.name			= "fileio",
919 	.inquiry_prod		= "FILEIO",
920 	.inquiry_rev		= FD_VERSION,
921 	.owner			= THIS_MODULE,
922 	.attach_hba		= fd_attach_hba,
923 	.detach_hba		= fd_detach_hba,
924 	.alloc_device		= fd_alloc_device,
925 	.configure_device	= fd_configure_device,
926 	.destroy_device		= fd_destroy_device,
927 	.free_device		= fd_free_device,
928 	.parse_cdb		= fd_parse_cdb,
929 	.set_configfs_dev_params = fd_set_configfs_dev_params,
930 	.show_configfs_dev_params = fd_show_configfs_dev_params,
931 	.get_device_type	= sbc_get_device_type,
932 	.get_blocks		= fd_get_blocks,
933 	.init_prot		= fd_init_prot,
934 	.format_prot		= fd_format_prot,
935 	.free_prot		= fd_free_prot,
936 	.tb_dev_attrib_attrs	= sbc_attrib_attrs,
937 };
938 
939 static int __init fileio_module_init(void)
940 {
941 	return transport_backend_register(&fileio_ops);
942 }
943 
944 static void __exit fileio_module_exit(void)
945 {
946 	target_backend_unregister(&fileio_ops);
947 }
948 
949 MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
950 MODULE_AUTHOR("nab@Linux-iSCSI.org");
951 MODULE_LICENSE("GPL");
952 
953 module_init(fileio_module_init);
954 module_exit(fileio_module_exit);
955