29 #include <linux/string.h>
33 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <scsi/scsi.h>
54 fd_host = kzalloc(
sizeof(
struct fd_host),
GFP_KERNEL);
56 pr_err(
"Unable to allocate memory for struct fd_host\n");
64 pr_debug(
"CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
67 pr_debug(
"CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
74 static void fd_detach_hba(
struct se_hba *hba)
76 struct fd_host *fd_host = hba->
hba_ptr;
78 pr_debug(
"CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
85 static void *fd_allocate_virtdevice(
struct se_hba *hba,
const char *
name)
88 struct fd_host *fd_host = hba->
hba_ptr;
90 fd_dev = kzalloc(
sizeof(
struct fd_dev),
GFP_KERNEL);
92 pr_err(
"Unable to allocate memory for struct fd_dev\n");
98 pr_debug(
"FILEIO: Allocated fd_dev for %p\n", name);
107 static struct se_device *fd_create_virtdevice(
114 struct queue_limits *limits;
115 struct fd_dev *fd_dev =
p;
116 struct fd_host *fd_host = hba->
hba_ptr;
138 pr_debug(
"FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
158 unsigned long long dev_size;
163 q = bdev_get_queue(inode->
i_bdev);
164 limits = &dev_limits.limits;
165 limits->logical_block_size = bdev_logical_block_size(inode->
i_bdev);
166 limits->max_hw_sectors = queue_max_hw_sectors(q);
167 limits->max_sectors = queue_max_sectors(q);
173 dev_size = (i_size_read(file->
f_mapping->host) -
176 pr_debug(
"FILEIO: Using size: %llu bytes from struct"
177 " block_device blocks: %llu logical_block_size: %d\n",
182 pr_err(
"FILEIO: Missing fd_dev_size="
183 " parameter, and no backing struct"
188 limits = &dev_limits.limits;
199 se_dev, dev_flags, fd_dev,
205 pr_debug(
"FILEIO: Forcing setting of emulate_write_cache=1"
206 " with FDBD_HAS_BUFFERED_IO_WCE\n");
207 dev->
se_sub_dev->se_dev_attrib.emulate_write_cache = 1;
213 pr_debug(
"CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
230 static void fd_free_device(
void *p)
232 struct fd_dev *fd_dev =
p;
246 struct fd_dev *dev = se_dev->
dev_ptr;
252 se_dev->
se_sub_dev->se_dev_attrib.block_size);
257 pr_err(
"Unable to allocate fd_do_readv iov[]\n");
268 ret =
vfs_readv(fd, &iov[0], sgl_nents, &pos);
277 if (
S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
279 pr_err(
"vfs_readv() returned %d,"
280 " expecting %d for S_ISBLK\n", ret,
282 return (ret < 0 ? ret : -
EINVAL);
286 pr_err(
"vfs_readv() returned %d for non"
299 struct fd_dev *dev = se_dev->
dev_ptr;
300 struct file *fd = dev->
fd_file;
305 se_dev->
se_sub_dev->se_dev_attrib.block_size);
310 pr_err(
"Unable to allocate fd_do_writev iov[]\n");
321 ret =
vfs_writev(fd, &iov[0], sgl_nents, &pos);
326 if (ret < 0 || ret != cmd->data_length) {
327 pr_err(
"vfs_writev() returned %d\n", ret);
328 return (ret < 0 ? ret : -
EINVAL);
334 static int fd_execute_sync_cache(
struct se_cmd *cmd)
337 struct fd_dev *fd_dev = dev->
dev_ptr;
365 pr_err(
"FILEIO: vfs_fsync_range() failed: %d\n", ret);
380 static int fd_execute_rw(
struct se_cmd *cmd)
393 ret = fd_do_readv(cmd, sgl, sgl_nents);
395 ret = fd_do_writev(cmd, sgl, sgl_nents);
402 dev->
se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
404 struct fd_dev *fd_dev = dev->
dev_ptr;
433 static ssize_t fd_set_configfs_dev_params(
439 char *orig, *
ptr, *arg_p, *
opts;
449 while ((ptr =
strsep(&opts,
",\n")) !=
NULL) {
461 pr_debug(
"FILEIO: Referencing Path: %s\n",
474 pr_err(
"strict_strtoull() failed for"
478 pr_debug(
"FILEIO: Referencing Size: %llu"
485 pr_err(
"bogus fd_buffered_io=%d value\n",
arg);
490 pr_debug(
"FILEIO: Using buffered I/O"
491 " operations for struct fd_dev\n");
502 return (!ret) ? count :
ret;
510 pr_err(
"Missing fd_dev_name=\n");
517 static ssize_t fd_show_configfs_dev_params(
526 bl +=
sprintf(b + bl,
" File: %s Size: %llu Mode: %s\n",
529 "Buffered-WCE" :
"O_DSYNC");
553 struct fd_dev *fd_dev = dev->
dev_ptr;
556 unsigned long long dev_size;
567 return div_u64(dev_size, dev->
se_sub_dev->se_dev_attrib.block_size);
570 static struct spc_ops fd_spc_ops = {
571 .execute_rw = fd_execute_rw,
572 .execute_sync_cache = fd_execute_sync_cache,
575 static int fd_parse_cdb(
struct se_cmd *cmd)
584 .attach_hba = fd_attach_hba,
585 .detach_hba = fd_detach_hba,
586 .allocate_virtdevice = fd_allocate_virtdevice,
587 .create_virtdevice = fd_create_virtdevice,
588 .free_device = fd_free_device,
589 .parse_cdb = fd_parse_cdb,
590 .check_configfs_dev_params = fd_check_configfs_dev_params,
591 .set_configfs_dev_params = fd_set_configfs_dev_params,
592 .show_configfs_dev_params = fd_show_configfs_dev_params,
593 .get_device_rev = fd_get_device_rev,
594 .get_device_type = fd_get_device_type,
595 .get_blocks = fd_get_blocks,
598 static int __init fileio_module_init(
void)
603 static void fileio_module_exit(
void)