]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/target/target_core_file.c
7c5b1ef57d34cf32b20cc05a6b82657002307764
[karo-tx-linux.git] / drivers / target / target_core_file.c
1 /*******************************************************************************
2  * Filename:  target_core_file.c
3  *
4  * This file contains the Storage Engine <-> FILEIO transport specific functions
5  *
6  * (c) Copyright 2005-2013 Datera, Inc.
7  *
8  * Nicholas A. Bellinger <nab@kernel.org>
9  *
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.
14  *
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.
19  *
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.
23  *
24  ******************************************************************************/
25
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>
37
38 #include <target/target_core_base.h>
39 #include <target/target_core_backend.h>
40 #include <target/target_core_backend_configfs.h>
41
42 #include "target_core_file.h"
43
44 static inline struct fd_dev *FD_DEV(struct se_device *dev)
45 {
46         return container_of(dev, struct fd_dev, dev);
47 }
48
49 static int fd_attach_hba(struct se_hba *hba, u32 host_id)
50 {
51         struct fd_host *fd_host;
52
53         fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
54         if (!fd_host) {
55                 pr_err("Unable to allocate memory for struct fd_host\n");
56                 return -ENOMEM;
57         }
58
59         fd_host->fd_host_id = host_id;
60
61         hba->hba_ptr = fd_host;
62
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);
68
69         return 0;
70 }
71
72 static void fd_detach_hba(struct se_hba *hba)
73 {
74         struct fd_host *fd_host = hba->hba_ptr;
75
76         pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
77                 " Target Core\n", hba->hba_id, fd_host->fd_host_id);
78
79         kfree(fd_host);
80         hba->hba_ptr = NULL;
81 }
82
83 static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
84 {
85         struct fd_dev *fd_dev;
86         struct fd_host *fd_host = hba->hba_ptr;
87
88         fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
89         if (!fd_dev) {
90                 pr_err("Unable to allocate memory for struct fd_dev\n");
91                 return NULL;
92         }
93
94         fd_dev->fd_host = fd_host;
95
96         pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
97
98         return &fd_dev->dev;
99 }
100
101 static int fd_configure_device(struct se_device *dev)
102 {
103         struct fd_dev *fd_dev = FD_DEV(dev);
104         struct fd_host *fd_host = dev->se_hba->hba_ptr;
105         struct file *file;
106         struct inode *inode = NULL;
107         int flags, ret = -EINVAL;
108
109         if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
110                 pr_err("Missing fd_dev_name=\n");
111                 return -EINVAL;
112         }
113
114         /*
115          * Use O_DSYNC by default instead of O_SYNC to forgo syncing
116          * of pure timestamp updates.
117          */
118         flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
119
120         /*
121          * Optionally allow fd_buffered_io=1 to be enabled for people
122          * who want use the fs buffer cache as an WriteCache mechanism.
123          *
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.
128          */
129         if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
130                 pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
131                 flags &= ~O_DSYNC;
132         }
133
134         file = filp_open(fd_dev->fd_dev_name, flags, 0600);
135         if (IS_ERR(file)) {
136                 pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
137                 ret = PTR_ERR(file);
138                 goto fail;
139         }
140         fd_dev->fd_file = file;
141         /*
142          * If using a block backend with this struct file, we extract
143          * fd_dev->fd_[block,dev]_size from struct block_device.
144          *
145          * Otherwise, we use the passed fd_size= from configfs
146          */
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;
151
152                 fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
153                 /*
154                  * Determine the number of bytes from i_size_read() minus
155                  * one (1) logical sector from underlying struct block_device
156                  */
157                 dev_size = (i_size_read(file->f_mapping->host) -
158                                        fd_dev->fd_block_size);
159
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);
164                 /*
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
168                  */
169                 if (blk_queue_discard(q)) {
170                         dev->dev_attrib.max_unmap_lba_count =
171                                 q->limits.max_discard_sectors;
172                         /*
173                          * Currently hardcoded to 1 in Linux/SCSI code..
174                          */
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");
182                 }
183                 /*
184                  * Enable write same emulation for IBLOCK and use 0xFFFF as
185                  * the smaller WRITE_SAME(10) only has a two-byte block count.
186                  */
187                 dev->dev_attrib.max_write_same_len = 0xFFFF;
188
189                 if (blk_queue_nonrot(q))
190                         dev->dev_attrib.is_nonrot = 1;
191         } else {
192                 if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
193                         pr_err("FILEIO: Missing fd_dev_size="
194                                 " parameter, and no backing struct"
195                                 " block_device\n");
196                         goto fail;
197                 }
198
199                 fd_dev->fd_block_size = FD_BLOCKSIZE;
200                 /*
201                  * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
202                  */
203                 dev->dev_attrib.max_unmap_lba_count = 0x2000;
204                 /*
205                  * Currently hardcoded to 1 in Linux/SCSI code..
206                  */
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;
210
211                 /*
212                  * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
213                  * based upon struct iovec limit for vfs_writev()
214                  */
215                 dev->dev_attrib.max_write_same_len = 0x1000;
216         }
217
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;
222
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;
227         }
228
229         fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
230         fd_dev->fd_queue_depth = dev->queue_depth;
231
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);
235
236         return 0;
237 fail:
238         if (fd_dev->fd_file) {
239                 filp_close(fd_dev->fd_file, NULL);
240                 fd_dev->fd_file = NULL;
241         }
242         return ret;
243 }
244
245 static void fd_free_device(struct se_device *dev)
246 {
247         struct fd_dev *fd_dev = FD_DEV(dev);
248
249         if (fd_dev->fd_file) {
250                 filp_close(fd_dev->fd_file, NULL);
251                 fd_dev->fd_file = NULL;
252         }
253
254         kfree(fd_dev);
255 }
256
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)
260 {
261         struct scatterlist *sg;
262         struct iov_iter iter;
263         struct bio_vec *bvec;
264         ssize_t len = 0;
265         loff_t pos = (cmd->t_task_lba * block_size);
266         int ret = 0, i;
267
268         bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
269         if (!bvec) {
270                 pr_err("Unable to allocate fd_do_readv iov[]\n");
271                 return -ENOMEM;
272         }
273
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;
278
279                 len += sg->length;
280         }
281
282         iov_iter_bvec(&iter, ITER_BVEC, bvec, sgl_nents, len);
283         if (is_write)
284                 ret = vfs_iter_write(fd, &iter, &pos);
285         else
286                 ret = vfs_iter_read(fd, &iter, &pos);
287
288         kfree(bvec);
289
290         if (is_write) {
291                 if (ret < 0 || ret != data_length) {
292                         pr_err("%s() write returned %d\n", __func__, ret);
293                         return (ret < 0 ? ret : -EINVAL);
294                 }
295         } else {
296                 /*
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
299                  * block_device.
300                  */
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,
305                                                 data_length);
306                                 return (ret < 0 ? ret : -EINVAL);
307                         }
308                 } else {
309                         if (ret < 0) {
310                                 pr_err("%s() returned %d for non S_ISBLK\n",
311                                                 __func__, ret);
312                                 return ret;
313                         }
314                 }
315         }
316         return 1;
317 }
318
319 static sense_reason_t
320 fd_execute_sync_cache(struct se_cmd *cmd)
321 {
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);
325         loff_t start, end;
326         int ret;
327
328         /*
329          * If the Immediate bit is set, queue up the GOOD response
330          * for this SYNCHRONIZE_CACHE op
331          */
332         if (immed)
333                 target_complete_cmd(cmd, SAM_STAT_GOOD);
334
335         /*
336          * Determine if we will be flushing the entire device.
337          */
338         if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
339                 start = 0;
340                 end = LLONG_MAX;
341         } else {
342                 start = cmd->t_task_lba * dev->dev_attrib.block_size;
343                 if (cmd->data_length)
344                         end = start + cmd->data_length - 1;
345                 else
346                         end = LLONG_MAX;
347         }
348
349         ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
350         if (ret != 0)
351                 pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
352
353         if (immed)
354                 return 0;
355
356         if (ret)
357                 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
358         else
359                 target_complete_cmd(cmd, SAM_STAT_GOOD);
360
361         return 0;
362 }
363
364 static sense_reason_t
365 fd_execute_write_same(struct se_cmd *cmd)
366 {
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;
374         ssize_t ret;
375
376         if (!nolb) {
377                 target_complete_cmd(cmd, SAM_STAT_GOOD);
378                 return 0;
379         }
380         if (cmd->prot_op) {
381                 pr_err("WRITE_SAME: Protection information with FILEIO"
382                        " backends not supported\n");
383                 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
384         }
385
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"
389                         " block_size: %u\n",
390                         cmd->t_data_nents,
391                         cmd->t_data_sg[0].length,
392                         cmd->se_dev->dev_attrib.block_size);
393                 return TCM_INVALID_CDB_FIELD;
394         }
395
396         bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
397         if (!bvec)
398                 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
399
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;
404
405                 len += se_dev->dev_attrib.block_size;
406         }
407
408         iov_iter_bvec(&iter, ITER_BVEC, bvec, nolb, len);
409         ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos);
410
411         kfree(bvec);
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;
415         }
416
417         target_complete_cmd(cmd, SAM_STAT_GOOD);
418         return 0;
419 }
420
421 static int
422 fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
423                 void *buf, size_t bufsize)
424 {
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;
429
430         if (!prot_fd) {
431                 pr_err("Unable to locate fd_dev->fd_prot_file\n");
432                 return -ENODEV;
433         }
434
435         prot_length = nolb * se_dev->prot_length;
436
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);
440
441                 if (ret != len) {
442                         pr_err("vfs_write to prot file failed: %zd\n", ret);
443                         return ret < 0 ? ret : -ENODEV;
444                 }
445                 prot += ret;
446         }
447
448         return 0;
449 }
450
451 static int
452 fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
453 {
454         void *buf;
455         int rc;
456
457         buf = (void *)__get_free_page(GFP_KERNEL);
458         if (!buf) {
459                 pr_err("Unable to allocate FILEIO prot buf\n");
460                 return -ENOMEM;
461         }
462         memset(buf, 0xff, PAGE_SIZE);
463
464         rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
465
466         free_page((unsigned long)buf);
467
468         return rc;
469 }
470
471 static sense_reason_t
472 fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
473 {
474         struct file *file = priv;
475         struct inode *inode = file->f_mapping->host;
476         int ret;
477
478         if (cmd->se_dev->dev_attrib.pi_prot_type) {
479                 ret = fd_do_prot_unmap(cmd, lba, nolb);
480                 if (ret)
481                         return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
482         }
483
484         if (S_ISBLK(inode->i_mode)) {
485                 /* The backend is block device, use discard */
486                 struct block_device *bdev = inode->i_bdev;
487
488                 ret = blkdev_issue_discard(bdev, lba,
489                                 nolb, GFP_KERNEL, 0);
490                 if (ret < 0) {
491                         pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
492                                 ret);
493                         return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
494                 }
495         } else {
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;
501
502                 if (!file->f_op->fallocate)
503                         return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
504
505                 ret = file->f_op->fallocate(file, mode, pos, len);
506                 if (ret < 0) {
507                         pr_warn("FILEIO: fallocate() failed: %d\n", ret);
508                         return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
509                 }
510         }
511
512         return 0;
513 }
514
515 static sense_reason_t
516 fd_execute_write_same_unmap(struct se_cmd *cmd)
517 {
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);
523         sense_reason_t ret;
524
525         if (!nolb) {
526                 target_complete_cmd(cmd, SAM_STAT_GOOD);
527                 return 0;
528         }
529
530         ret = fd_do_unmap(cmd, file, lba, nolb);
531         if (ret)
532                 return ret;
533
534         target_complete_cmd(cmd, GOOD);
535         return 0;
536 }
537
538 static sense_reason_t
539 fd_execute_unmap(struct se_cmd *cmd)
540 {
541         struct file *file = FD_DEV(cmd->se_dev)->fd_file;
542
543         return sbc_execute_unmap(cmd, fd_do_unmap, file);
544 }
545
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)
549 {
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;
554         sense_reason_t rc;
555         int ret = 0;
556         /*
557          * We are currently limited by the number of iovecs (2048) per
558          * single vfs_[writev,readv] call.
559          */
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;
565         }
566         /*
567          * Call vectorized fileio functions to map struct scatterlist
568          * physical memory addresses to struct iovec virtual memory.
569          */
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);
575                         if (ret < 0)
576                                 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
577                 }
578
579                 ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
580                                sgl, sgl_nents, cmd->data_length, 0);
581
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);
585
586                         rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
587                                             0, cmd->t_prot_sg, 0);
588                         if (rc)
589                                 return rc;
590                 }
591         } else {
592                 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
593                         u32 sectors = cmd->data_length >>
594                                         ilog2(dev->dev_attrib.block_size);
595
596                         rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
597                                             0, cmd->t_prot_sg, 0);
598                         if (rc)
599                                 return rc;
600                 }
601
602                 ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
603                                sgl, sgl_nents, cmd->data_length, 1);
604                 /*
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.
608                  */
609                 if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) {
610                         loff_t start = cmd->t_task_lba *
611                                 dev->dev_attrib.block_size;
612                         loff_t end;
613
614                         if (cmd->data_length)
615                                 end = start + cmd->data_length - 1;
616                         else
617                                 end = LLONG_MAX;
618
619                         vfs_fsync_range(fd_dev->fd_file, start, end, 1);
620                 }
621
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);
626                         if (ret < 0)
627                                 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
628                 }
629         }
630
631         if (ret < 0)
632                 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
633
634         if (ret)
635                 target_complete_cmd(cmd, SAM_STAT_GOOD);
636         return 0;
637 }
638
639 enum {
640         Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
641 };
642
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"},
647         {Opt_err, NULL}
648 };
649
650 static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
651                 const char *page, ssize_t count)
652 {
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;
657
658         opts = kstrdup(page, GFP_KERNEL);
659         if (!opts)
660                 return -ENOMEM;
661
662         orig = opts;
663
664         while ((ptr = strsep(&opts, ",\n")) != NULL) {
665                 if (!*ptr)
666                         continue;
667
668                 token = match_token(ptr, tokens, args);
669                 switch (token) {
670                 case Opt_fd_dev_name:
671                         if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
672                                 FD_MAX_DEV_NAME) == 0) {
673                                 ret = -EINVAL;
674                                 break;
675                         }
676                         pr_debug("FILEIO: Referencing Path: %s\n",
677                                         fd_dev->fd_dev_name);
678                         fd_dev->fbd_flags |= FBDF_HAS_PATH;
679                         break;
680                 case Opt_fd_dev_size:
681                         arg_p = match_strdup(&args[0]);
682                         if (!arg_p) {
683                                 ret = -ENOMEM;
684                                 break;
685                         }
686                         ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
687                         kfree(arg_p);
688                         if (ret < 0) {
689                                 pr_err("kstrtoull() failed for"
690                                                 " fd_dev_size=\n");
691                                 goto out;
692                         }
693                         pr_debug("FILEIO: Referencing Size: %llu"
694                                         " bytes\n", fd_dev->fd_dev_size);
695                         fd_dev->fbd_flags |= FBDF_HAS_SIZE;
696                         break;
697                 case Opt_fd_buffered_io:
698                         ret = match_int(args, &arg);
699                         if (ret)
700                                 goto out;
701                         if (arg != 1) {
702                                 pr_err("bogus fd_buffered_io=%d value\n", arg);
703                                 ret = -EINVAL;
704                                 goto out;
705                         }
706
707                         pr_debug("FILEIO: Using buffered I/O"
708                                 " operations for struct fd_dev\n");
709
710                         fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
711                         break;
712                 default:
713                         break;
714                 }
715         }
716
717 out:
718         kfree(orig);
719         return (!ret) ? count : ret;
720 }
721
722 static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
723 {
724         struct fd_dev *fd_dev = FD_DEV(dev);
725         ssize_t bl = 0;
726
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");
732         return bl;
733 }
734
735 static sector_t fd_get_blocks(struct se_device *dev)
736 {
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;
741         /*
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.
745          */
746         if (S_ISBLK(i->i_mode))
747                 dev_size = i_size_read(i);
748         else
749                 dev_size = fd_dev->fd_dev_size;
750
751         return div_u64(dev_size - dev->dev_attrib.block_size,
752                        dev->dev_attrib.block_size);
753 }
754
755 static int fd_init_prot(struct se_device *dev)
756 {
757         struct fd_dev *fd_dev = FD_DEV(dev);
758         struct file *prot_file, *file = fd_dev->fd_file;
759         struct inode *inode;
760         int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
761         char buf[FD_MAX_DEV_PROT_NAME];
762
763         if (!file) {
764                 pr_err("Unable to locate fd_dev->fd_file\n");
765                 return -ENODEV;
766         }
767
768         inode = file->f_mapping->host;
769         if (S_ISBLK(inode->i_mode)) {
770                 pr_err("FILEIO Protection emulation only supported on"
771                        " !S_ISBLK\n");
772                 return -ENOSYS;
773         }
774
775         if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
776                 flags &= ~O_DSYNC;
777
778         snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
779                  fd_dev->fd_dev_name);
780
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);
785                 return ret;
786         }
787         fd_dev->fd_prot_file = prot_file;
788
789         return 0;
790 }
791
792 static int fd_format_prot(struct se_device *dev)
793 {
794         unsigned char *buf;
795         int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
796         int ret;
797
798         if (!dev->dev_attrib.pi_prot_type) {
799                 pr_err("Unable to format_prot while pi_prot_type == 0\n");
800                 return -ENODEV;
801         }
802
803         buf = vzalloc(unit_size);
804         if (!buf) {
805                 pr_err("Unable to allocate FILEIO prot buf\n");
806                 return -ENOMEM;
807         }
808
809         pr_debug("Using FILEIO prot_length: %llu\n",
810                  (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
811                                         dev->prot_length);
812
813         memset(buf, 0xff, unit_size);
814         ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
815                               buf, unit_size);
816         vfree(buf);
817         return ret;
818 }
819
820 static void fd_free_prot(struct se_device *dev)
821 {
822         struct fd_dev *fd_dev = FD_DEV(dev);
823
824         if (!fd_dev->fd_prot_file)
825                 return;
826
827         filp_close(fd_dev->fd_prot_file, NULL);
828         fd_dev->fd_prot_file = NULL;
829 }
830
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,
837 };
838
839 static sense_reason_t
840 fd_parse_cdb(struct se_cmd *cmd)
841 {
842         return sbc_parse_cdb(cmd, &fd_sbc_ops);
843 }
844
845 DEF_TB_DEFAULT_ATTRIBS(fileio);
846
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,
877         NULL,
878 };
879
880 static const struct target_backend_ops fileio_ops = {
881         .name                   = "fileio",
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,
899 };
900
901 static int __init fileio_module_init(void)
902 {
903         return transport_backend_register(&fileio_ops);
904 }
905
906 static void __exit fileio_module_exit(void)
907 {
908         target_backend_unregister(&fileio_ops);
909 }
910
911 MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
912 MODULE_AUTHOR("nab@Linux-iSCSI.org");
913 MODULE_LICENSE("GPL");
914
915 module_init(fileio_module_init);
916 module_exit(fileio_module_exit);