1 /*******************************************************************************
2 * Filename: target_core_file.c
4 * This file contains the Storage Engine <-> FILEIO transport specific functions
6 * (c) Copyright 2005-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/string.h>
27 #include <linux/parser.h>
28 #include <linux/timer.h>
29 #include <linux/blkdev.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/module.h>
33 #include <linux/falloc.h>
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_host.h>
36 #include <asm/unaligned.h>
38 #include <target/target_core_base.h>
39 #include <target/target_core_backend.h>
40 #include <target/target_core_backend_configfs.h>
42 #include "target_core_file.h"
44 static inline struct fd_dev *FD_DEV(struct se_device *dev)
46 return container_of(dev, struct fd_dev, dev);
49 static int fd_attach_hba(struct se_hba *hba, u32 host_id)
51 struct fd_host *fd_host;
53 fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
55 pr_err("Unable to allocate memory for struct fd_host\n");
59 fd_host->fd_host_id = host_id;
61 hba->hba_ptr = fd_host;
63 pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
64 " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
65 TARGET_CORE_MOD_VERSION);
66 pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
67 hba->hba_id, fd_host->fd_host_id);
72 static void fd_detach_hba(struct se_hba *hba)
74 struct fd_host *fd_host = hba->hba_ptr;
76 pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
77 " Target Core\n", hba->hba_id, fd_host->fd_host_id);
83 static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
85 struct fd_dev *fd_dev;
86 struct fd_host *fd_host = hba->hba_ptr;
88 fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
90 pr_err("Unable to allocate memory for struct fd_dev\n");
94 fd_dev->fd_host = fd_host;
96 pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
101 static int fd_configure_device(struct se_device *dev)
103 struct fd_dev *fd_dev = FD_DEV(dev);
104 struct fd_host *fd_host = dev->se_hba->hba_ptr;
106 struct inode *inode = NULL;
107 int flags, ret = -EINVAL;
109 if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
110 pr_err("Missing fd_dev_name=\n");
115 * Use O_DSYNC by default instead of O_SYNC to forgo syncing
116 * of pure timestamp updates.
118 flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
121 * Optionally allow fd_buffered_io=1 to be enabled for people
122 * who want use the fs buffer cache as an WriteCache mechanism.
124 * This means that in event of a hard failure, there is a risk
125 * of silent data-loss if the SCSI client has *not* performed a
126 * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
127 * to write-out the entire device cache.
129 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
130 pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
134 file = filp_open(fd_dev->fd_dev_name, flags, 0600);
136 pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
140 fd_dev->fd_file = file;
142 * If using a block backend with this struct file, we extract
143 * fd_dev->fd_[block,dev]_size from struct block_device.
145 * Otherwise, we use the passed fd_size= from configfs
147 inode = file->f_mapping->host;
148 if (S_ISBLK(inode->i_mode)) {
149 struct request_queue *q = bdev_get_queue(inode->i_bdev);
150 unsigned long long dev_size;
152 fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
154 * Determine the number of bytes from i_size_read() minus
155 * one (1) logical sector from underlying struct block_device
157 dev_size = (i_size_read(file->f_mapping->host) -
158 fd_dev->fd_block_size);
160 pr_debug("FILEIO: Using size: %llu bytes from struct"
161 " block_device blocks: %llu logical_block_size: %d\n",
162 dev_size, div_u64(dev_size, fd_dev->fd_block_size),
163 fd_dev->fd_block_size);
165 * Check if the underlying struct block_device request_queue supports
166 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
167 * in ATA and we need to set TPE=1
169 if (blk_queue_discard(q)) {
170 dev->dev_attrib.max_unmap_lba_count =
171 q->limits.max_discard_sectors;
173 * Currently hardcoded to 1 in Linux/SCSI code..
175 dev->dev_attrib.max_unmap_block_desc_count = 1;
176 dev->dev_attrib.unmap_granularity =
177 q->limits.discard_granularity >> 9;
178 dev->dev_attrib.unmap_granularity_alignment =
179 q->limits.discard_alignment;
180 pr_debug("IFILE: BLOCK Discard support available,"
181 " disabled by default\n");
184 * Enable write same emulation for IBLOCK and use 0xFFFF as
185 * the smaller WRITE_SAME(10) only has a two-byte block count.
187 dev->dev_attrib.max_write_same_len = 0xFFFF;
189 if (blk_queue_nonrot(q))
190 dev->dev_attrib.is_nonrot = 1;
192 if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
193 pr_err("FILEIO: Missing fd_dev_size="
194 " parameter, and no backing struct"
199 fd_dev->fd_block_size = FD_BLOCKSIZE;
201 * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
203 dev->dev_attrib.max_unmap_lba_count = 0x2000;
205 * Currently hardcoded to 1 in Linux/SCSI code..
207 dev->dev_attrib.max_unmap_block_desc_count = 1;
208 dev->dev_attrib.unmap_granularity = 1;
209 dev->dev_attrib.unmap_granularity_alignment = 0;
212 * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
213 * based upon struct iovec limit for vfs_writev()
215 dev->dev_attrib.max_write_same_len = 0x1000;
218 dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
219 dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
220 dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
221 dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
223 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
224 pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
225 " with FDBD_HAS_BUFFERED_IO_WCE\n");
226 dev->dev_attrib.emulate_write_cache = 1;
229 fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
230 fd_dev->fd_queue_depth = dev->queue_depth;
232 pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
233 " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
234 fd_dev->fd_dev_name, fd_dev->fd_dev_size);
238 if (fd_dev->fd_file) {
239 filp_close(fd_dev->fd_file, NULL);
240 fd_dev->fd_file = NULL;
245 static void fd_free_device(struct se_device *dev)
247 struct fd_dev *fd_dev = FD_DEV(dev);
249 if (fd_dev->fd_file) {
250 filp_close(fd_dev->fd_file, NULL);
251 fd_dev->fd_file = NULL;
257 static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
258 u32 block_size, struct scatterlist *sgl,
259 u32 sgl_nents, u32 data_length, int is_write)
261 struct scatterlist *sg;
262 struct iov_iter iter;
263 struct bio_vec *bvec;
265 loff_t pos = (cmd->t_task_lba * block_size);
268 bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
270 pr_err("Unable to allocate fd_do_readv iov[]\n");
274 for_each_sg(sgl, sg, sgl_nents, i) {
275 bvec[i].bv_page = sg_page(sg);
276 bvec[i].bv_len = sg->length;
277 bvec[i].bv_offset = sg->offset;
282 iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len);
284 ret = vfs_iter_write(fd, &iter, &pos);
286 ret = vfs_iter_read(fd, &iter, &pos);
291 if (ret < 0 || ret != data_length) {
292 pr_err("%s() write returned %d\n", __func__, ret);
293 return (ret < 0 ? ret : -EINVAL);
297 * Return zeros and GOOD status even if the READ did not return
298 * the expected virt_size for struct file w/o a backing struct
301 if (S_ISBLK(file_inode(fd)->i_mode)) {
302 if (ret < 0 || ret != data_length) {
303 pr_err("%s() returned %d, expecting %u for "
304 "S_ISBLK\n", __func__, ret,
306 return (ret < 0 ? ret : -EINVAL);
310 pr_err("%s() returned %d for non S_ISBLK\n",
319 static sense_reason_t
320 fd_execute_sync_cache(struct se_cmd *cmd)
322 struct se_device *dev = cmd->se_dev;
323 struct fd_dev *fd_dev = FD_DEV(dev);
324 int immed = (cmd->t_task_cdb[1] & 0x2);
329 * If the Immediate bit is set, queue up the GOOD response
330 * for this SYNCHRONIZE_CACHE op
333 target_complete_cmd(cmd, SAM_STAT_GOOD);
336 * Determine if we will be flushing the entire device.
338 if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
342 start = cmd->t_task_lba * dev->dev_attrib.block_size;
343 if (cmd->data_length)
344 end = start + cmd->data_length - 1;
349 ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
351 pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
357 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
359 target_complete_cmd(cmd, SAM_STAT_GOOD);
364 static sense_reason_t
365 fd_execute_write_same(struct se_cmd *cmd)
367 struct se_device *se_dev = cmd->se_dev;
368 struct fd_dev *fd_dev = FD_DEV(se_dev);
369 loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
370 sector_t nolb = sbc_get_write_same_sectors(cmd);
371 struct iov_iter iter;
372 struct bio_vec *bvec;
373 unsigned int len = 0, i;
377 target_complete_cmd(cmd, SAM_STAT_GOOD);
381 pr_err("WRITE_SAME: Protection information with FILEIO"
382 " backends not supported\n");
383 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
386 if (cmd->t_data_nents > 1 ||
387 cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {
388 pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
391 cmd->t_data_sg[0].length,
392 cmd->se_dev->dev_attrib.block_size);
393 return TCM_INVALID_CDB_FIELD;
396 bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
398 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
400 for (i = 0; i < nolb; i++) {
401 bvec[i].bv_page = sg_page(&cmd->t_data_sg[0]);
402 bvec[i].bv_len = cmd->t_data_sg[0].length;
403 bvec[i].bv_offset = cmd->t_data_sg[0].offset;
405 len += se_dev->dev_attrib.block_size;
408 iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len);
409 ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos);
412 if (ret < 0 || ret != len) {
413 pr_err("vfs_iter_write() returned %zd for write same\n", ret);
414 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
417 target_complete_cmd(cmd, SAM_STAT_GOOD);
422 fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
423 void *buf, size_t bufsize)
425 struct fd_dev *fd_dev = FD_DEV(se_dev);
426 struct file *prot_fd = fd_dev->fd_prot_file;
427 sector_t prot_length, prot;
428 loff_t pos = lba * se_dev->prot_length;
431 pr_err("Unable to locate fd_dev->fd_prot_file\n");
435 prot_length = nolb * se_dev->prot_length;
437 for (prot = 0; prot < prot_length;) {
438 sector_t len = min_t(sector_t, bufsize, prot_length - prot);
439 ssize_t ret = kernel_write(prot_fd, buf, len, pos + prot);
442 pr_err("vfs_write to prot file failed: %zd\n", ret);
443 return ret < 0 ? ret : -ENODEV;
452 fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
457 buf = (void *)__get_free_page(GFP_KERNEL);
459 pr_err("Unable to allocate FILEIO prot buf\n");
462 memset(buf, 0xff, PAGE_SIZE);
464 rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
466 free_page((unsigned long)buf);
471 static sense_reason_t
472 fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
474 struct file *file = priv;
475 struct inode *inode = file->f_mapping->host;
478 if (cmd->se_dev->dev_attrib.pi_prot_type) {
479 ret = fd_do_prot_unmap(cmd, lba, nolb);
481 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
484 if (S_ISBLK(inode->i_mode)) {
485 /* The backend is block device, use discard */
486 struct block_device *bdev = inode->i_bdev;
488 ret = blkdev_issue_discard(bdev, lba,
489 nolb, GFP_KERNEL, 0);
491 pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
493 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
496 /* The backend is normal file, use fallocate */
497 struct se_device *se_dev = cmd->se_dev;
498 loff_t pos = lba * se_dev->dev_attrib.block_size;
499 unsigned int len = nolb * se_dev->dev_attrib.block_size;
500 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
502 if (!file->f_op->fallocate)
503 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
505 ret = file->f_op->fallocate(file, mode, pos, len);
507 pr_warn("FILEIO: fallocate() failed: %d\n", ret);
508 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
515 static sense_reason_t
516 fd_execute_write_same_unmap(struct se_cmd *cmd)
518 struct se_device *se_dev = cmd->se_dev;
519 struct fd_dev *fd_dev = FD_DEV(se_dev);
520 struct file *file = fd_dev->fd_file;
521 sector_t lba = cmd->t_task_lba;
522 sector_t nolb = sbc_get_write_same_sectors(cmd);
526 target_complete_cmd(cmd, SAM_STAT_GOOD);
530 ret = fd_do_unmap(cmd, file, lba, nolb);
534 target_complete_cmd(cmd, GOOD);
538 static sense_reason_t
539 fd_execute_unmap(struct se_cmd *cmd)
541 struct file *file = FD_DEV(cmd->se_dev)->fd_file;
543 return sbc_execute_unmap(cmd, fd_do_unmap, file);
546 static sense_reason_t
547 fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
548 enum dma_data_direction data_direction)
550 struct se_device *dev = cmd->se_dev;
551 struct fd_dev *fd_dev = FD_DEV(dev);
552 struct file *file = fd_dev->fd_file;
553 struct file *pfile = fd_dev->fd_prot_file;
557 * We are currently limited by the number of iovecs (2048) per
558 * single vfs_[writev,readv] call.
560 if (cmd->data_length > FD_MAX_BYTES) {
561 pr_err("FILEIO: Not able to process I/O of %u bytes due to"
562 "FD_MAX_BYTES: %u iovec count limitiation\n",
563 cmd->data_length, FD_MAX_BYTES);
564 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
567 * Call vectorized fileio functions to map struct scatterlist
568 * physical memory addresses to struct iovec virtual memory.
570 if (data_direction == DMA_FROM_DEVICE) {
571 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
572 ret = fd_do_rw(cmd, pfile, dev->prot_length,
573 cmd->t_prot_sg, cmd->t_prot_nents,
574 cmd->prot_length, 0);
576 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
579 ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
580 sgl, sgl_nents, cmd->data_length, 0);
582 if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
583 u32 sectors = cmd->data_length >>
584 ilog2(dev->dev_attrib.block_size);
586 rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
587 0, cmd->t_prot_sg, 0);
592 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
593 u32 sectors = cmd->data_length >>
594 ilog2(dev->dev_attrib.block_size);
596 rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
597 0, cmd->t_prot_sg, 0);
602 ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
603 sgl, sgl_nents, cmd->data_length, 1);
605 * Perform implicit vfs_fsync_range() for fd_do_writev() ops
606 * for SCSI WRITEs with Forced Unit Access (FUA) set.
607 * Allow this to happen independent of WCE=0 setting.
609 if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) {
610 loff_t start = cmd->t_task_lba *
611 dev->dev_attrib.block_size;
614 if (cmd->data_length)
615 end = start + cmd->data_length - 1;
619 vfs_fsync_range(fd_dev->fd_file, start, end, 1);
622 if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
623 ret = fd_do_rw(cmd, pfile, dev->prot_length,
624 cmd->t_prot_sg, cmd->t_prot_nents,
625 cmd->prot_length, 1);
627 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
632 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
635 target_complete_cmd(cmd, SAM_STAT_GOOD);
640 Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
643 static match_table_t tokens = {
644 {Opt_fd_dev_name, "fd_dev_name=%s"},
645 {Opt_fd_dev_size, "fd_dev_size=%s"},
646 {Opt_fd_buffered_io, "fd_buffered_io=%d"},
650 static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
651 const char *page, ssize_t count)
653 struct fd_dev *fd_dev = FD_DEV(dev);
654 char *orig, *ptr, *arg_p, *opts;
655 substring_t args[MAX_OPT_ARGS];
656 int ret = 0, arg, token;
658 opts = kstrdup(page, GFP_KERNEL);
664 while ((ptr = strsep(&opts, ",\n")) != NULL) {
668 token = match_token(ptr, tokens, args);
670 case Opt_fd_dev_name:
671 if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
672 FD_MAX_DEV_NAME) == 0) {
676 pr_debug("FILEIO: Referencing Path: %s\n",
677 fd_dev->fd_dev_name);
678 fd_dev->fbd_flags |= FBDF_HAS_PATH;
680 case Opt_fd_dev_size:
681 arg_p = match_strdup(&args[0]);
686 ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
689 pr_err("kstrtoull() failed for"
693 pr_debug("FILEIO: Referencing Size: %llu"
694 " bytes\n", fd_dev->fd_dev_size);
695 fd_dev->fbd_flags |= FBDF_HAS_SIZE;
697 case Opt_fd_buffered_io:
698 ret = match_int(args, &arg);
702 pr_err("bogus fd_buffered_io=%d value\n", arg);
707 pr_debug("FILEIO: Using buffered I/O"
708 " operations for struct fd_dev\n");
710 fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
719 return (!ret) ? count : ret;
722 static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
724 struct fd_dev *fd_dev = FD_DEV(dev);
727 bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
728 bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
729 fd_dev->fd_dev_name, fd_dev->fd_dev_size,
730 (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
731 "Buffered-WCE" : "O_DSYNC");
735 static sector_t fd_get_blocks(struct se_device *dev)
737 struct fd_dev *fd_dev = FD_DEV(dev);
738 struct file *f = fd_dev->fd_file;
739 struct inode *i = f->f_mapping->host;
740 unsigned long long dev_size;
742 * When using a file that references an underlying struct block_device,
743 * ensure dev_size is always based on the current inode size in order
744 * to handle underlying block_device resize operations.
746 if (S_ISBLK(i->i_mode))
747 dev_size = i_size_read(i);
749 dev_size = fd_dev->fd_dev_size;
751 return div_u64(dev_size - dev->dev_attrib.block_size,
752 dev->dev_attrib.block_size);
755 static int fd_init_prot(struct se_device *dev)
757 struct fd_dev *fd_dev = FD_DEV(dev);
758 struct file *prot_file, *file = fd_dev->fd_file;
760 int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
761 char buf[FD_MAX_DEV_PROT_NAME];
764 pr_err("Unable to locate fd_dev->fd_file\n");
768 inode = file->f_mapping->host;
769 if (S_ISBLK(inode->i_mode)) {
770 pr_err("FILEIO Protection emulation only supported on"
775 if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
778 snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
779 fd_dev->fd_dev_name);
781 prot_file = filp_open(buf, flags, 0600);
782 if (IS_ERR(prot_file)) {
783 pr_err("filp_open(%s) failed\n", buf);
784 ret = PTR_ERR(prot_file);
787 fd_dev->fd_prot_file = prot_file;
792 static int fd_format_prot(struct se_device *dev)
795 int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
798 if (!dev->dev_attrib.pi_prot_type) {
799 pr_err("Unable to format_prot while pi_prot_type == 0\n");
803 buf = vzalloc(unit_size);
805 pr_err("Unable to allocate FILEIO prot buf\n");
809 pr_debug("Using FILEIO prot_length: %llu\n",
810 (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
813 memset(buf, 0xff, unit_size);
814 ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
820 static void fd_free_prot(struct se_device *dev)
822 struct fd_dev *fd_dev = FD_DEV(dev);
824 if (!fd_dev->fd_prot_file)
827 filp_close(fd_dev->fd_prot_file, NULL);
828 fd_dev->fd_prot_file = NULL;
831 static struct sbc_ops fd_sbc_ops = {
832 .execute_rw = fd_execute_rw,
833 .execute_sync_cache = fd_execute_sync_cache,
834 .execute_write_same = fd_execute_write_same,
835 .execute_write_same_unmap = fd_execute_write_same_unmap,
836 .execute_unmap = fd_execute_unmap,
839 static sense_reason_t
840 fd_parse_cdb(struct se_cmd *cmd)
842 return sbc_parse_cdb(cmd, &fd_sbc_ops);
845 DEF_TB_DEFAULT_ATTRIBS(fileio);
847 static struct configfs_attribute *fileio_backend_dev_attrs[] = {
848 &fileio_dev_attrib_emulate_model_alias.attr,
849 &fileio_dev_attrib_emulate_dpo.attr,
850 &fileio_dev_attrib_emulate_fua_write.attr,
851 &fileio_dev_attrib_emulate_fua_read.attr,
852 &fileio_dev_attrib_emulate_write_cache.attr,
853 &fileio_dev_attrib_emulate_ua_intlck_ctrl.attr,
854 &fileio_dev_attrib_emulate_tas.attr,
855 &fileio_dev_attrib_emulate_tpu.attr,
856 &fileio_dev_attrib_emulate_tpws.attr,
857 &fileio_dev_attrib_emulate_caw.attr,
858 &fileio_dev_attrib_emulate_3pc.attr,
859 &fileio_dev_attrib_pi_prot_type.attr,
860 &fileio_dev_attrib_hw_pi_prot_type.attr,
861 &fileio_dev_attrib_pi_prot_format.attr,
862 &fileio_dev_attrib_enforce_pr_isids.attr,
863 &fileio_dev_attrib_is_nonrot.attr,
864 &fileio_dev_attrib_emulate_rest_reord.attr,
865 &fileio_dev_attrib_force_pr_aptpl.attr,
866 &fileio_dev_attrib_hw_block_size.attr,
867 &fileio_dev_attrib_block_size.attr,
868 &fileio_dev_attrib_hw_max_sectors.attr,
869 &fileio_dev_attrib_optimal_sectors.attr,
870 &fileio_dev_attrib_hw_queue_depth.attr,
871 &fileio_dev_attrib_queue_depth.attr,
872 &fileio_dev_attrib_max_unmap_lba_count.attr,
873 &fileio_dev_attrib_max_unmap_block_desc_count.attr,
874 &fileio_dev_attrib_unmap_granularity.attr,
875 &fileio_dev_attrib_unmap_granularity_alignment.attr,
876 &fileio_dev_attrib_max_write_same_len.attr,
880 static const struct target_backend_ops fileio_ops = {
882 .inquiry_prod = "FILEIO",
883 .inquiry_rev = FD_VERSION,
884 .owner = THIS_MODULE,
885 .attach_hba = fd_attach_hba,
886 .detach_hba = fd_detach_hba,
887 .alloc_device = fd_alloc_device,
888 .configure_device = fd_configure_device,
889 .free_device = fd_free_device,
890 .parse_cdb = fd_parse_cdb,
891 .set_configfs_dev_params = fd_set_configfs_dev_params,
892 .show_configfs_dev_params = fd_show_configfs_dev_params,
893 .get_device_type = sbc_get_device_type,
894 .get_blocks = fd_get_blocks,
895 .init_prot = fd_init_prot,
896 .format_prot = fd_format_prot,
897 .free_prot = fd_free_prot,
898 .tb_dev_attrib_attrs = fileio_backend_dev_attrs,
901 static int __init fileio_module_init(void)
903 return transport_backend_register(&fileio_ops);
906 static void __exit fileio_module_exit(void)
908 target_backend_unregister(&fileio_ops);
911 MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
912 MODULE_AUTHOR("nab@Linux-iSCSI.org");
913 MODULE_LICENSE("GPL");
915 module_init(fileio_module_init);
916 module_exit(fileio_module_exit);