1 /******************************************************************************* 2 * Filename: target_core_iblock.c 3 * 4 * This file contains the Storage Engine <-> Linux BlockIO transport 5 * specific functions. 6 * 7 * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc. 8 * Copyright (c) 2005, 2006, 2007 SBE, Inc. 9 * Copyright (c) 2007-2010 Rising Tide Systems 10 * Copyright (c) 2008-2010 Linux-iSCSI.org 11 * 12 * Nicholas A. Bellinger <nab@kernel.org> 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License 25 * along with this program; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 27 * 28 ******************************************************************************/ 29 30 #include <linux/string.h> 31 #include <linux/parser.h> 32 #include <linux/timer.h> 33 #include <linux/fs.h> 34 #include <linux/blkdev.h> 35 #include <linux/slab.h> 36 #include <linux/spinlock.h> 37 #include <linux/bio.h> 38 #include <linux/genhd.h> 39 #include <linux/file.h> 40 #include <linux/module.h> 41 #include <scsi/scsi.h> 42 #include <scsi/scsi_host.h> 43 44 #include <target/target_core_base.h> 45 #include <target/target_core_backend.h> 46 47 #include "target_core_iblock.h" 48 49 static struct se_subsystem_api iblock_template; 50 51 static void iblock_bio_done(struct bio *, int); 52 53 /* iblock_attach_hba(): (Part of se_subsystem_api_t template) 54 * 55 * 56 */ 57 static int iblock_attach_hba(struct se_hba *hba, u32 host_id) 58 { 59 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on" 60 " Generic Target Core Stack %s\n", hba->hba_id, 61 IBLOCK_VERSION, TARGET_CORE_MOD_VERSION); 62 return 0; 63 } 64 65 static void iblock_detach_hba(struct se_hba *hba) 66 { 67 } 68 69 static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name) 70 { 71 struct iblock_dev *ib_dev = NULL; 72 73 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL); 74 if (!ib_dev) { 75 pr_err("Unable to allocate struct iblock_dev\n"); 76 return NULL; 77 } 78 79 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name); 80 81 return ib_dev; 82 } 83 84 static struct se_device *iblock_create_virtdevice( 85 struct se_hba *hba, 86 struct se_subsystem_dev *se_dev, 87 void *p) 88 { 89 struct iblock_dev *ib_dev = p; 90 struct se_device *dev; 91 struct se_dev_limits dev_limits; 92 struct block_device *bd = NULL; 93 struct request_queue *q; 94 struct queue_limits *limits; 95 u32 dev_flags = 0; 96 int ret = -EINVAL; 97 98 if (!ib_dev) { 99 pr_err("Unable to locate struct iblock_dev parameter\n"); 100 return ERR_PTR(ret); 101 } 102 memset(&dev_limits, 0, sizeof(struct se_dev_limits)); 103 /* 104 * These settings need to be made tunable.. 105 */ 106 ib_dev->ibd_bio_set = bioset_create(32, 0); 107 if (!ib_dev->ibd_bio_set) { 108 pr_err("IBLOCK: Unable to create bioset()\n"); 109 return ERR_PTR(-ENOMEM); 110 } 111 pr_debug("IBLOCK: Created bio_set()\n"); 112 /* 113 * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path 114 * must already have been set in order for echo 1 > $HBA/$DEV/enable to run. 115 */ 116 pr_debug( "IBLOCK: Claiming struct block_device: %s\n", 117 ib_dev->ibd_udev_path); 118 119 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, 120 FMODE_WRITE|FMODE_READ|FMODE_EXCL, ib_dev); 121 if (IS_ERR(bd)) { 122 ret = PTR_ERR(bd); 123 goto failed; 124 } 125 /* 126 * Setup the local scope queue_limits from struct request_queue->limits 127 * to pass into transport_add_device_to_core_hba() as struct se_dev_limits. 128 */ 129 q = bdev_get_queue(bd); 130 limits = &dev_limits.limits; 131 limits->logical_block_size = bdev_logical_block_size(bd); 132 limits->max_hw_sectors = queue_max_hw_sectors(q); 133 limits->max_sectors = queue_max_sectors(q); 134 dev_limits.hw_queue_depth = q->nr_requests; 135 dev_limits.queue_depth = q->nr_requests; 136 137 ib_dev->ibd_bd = bd; 138 139 dev = transport_add_device_to_core_hba(hba, 140 &iblock_template, se_dev, dev_flags, ib_dev, 141 &dev_limits, "IBLOCK", IBLOCK_VERSION); 142 if (!dev) 143 goto failed; 144 145 /* 146 * Check if the underlying struct block_device request_queue supports 147 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM 148 * in ATA and we need to set TPE=1 149 */ 150 if (blk_queue_discard(q)) { 151 dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count = 152 q->limits.max_discard_sectors; 153 /* 154 * Currently hardcoded to 1 in Linux/SCSI code.. 155 */ 156 dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1; 157 dev->se_sub_dev->se_dev_attrib.unmap_granularity = 158 q->limits.discard_granularity >> 9; 159 dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment = 160 q->limits.discard_alignment; 161 162 pr_debug("IBLOCK: BLOCK Discard support available," 163 " disabled by default\n"); 164 } 165 166 if (blk_queue_nonrot(q)) 167 dev->se_sub_dev->se_dev_attrib.is_nonrot = 1; 168 169 return dev; 170 171 failed: 172 if (ib_dev->ibd_bio_set) { 173 bioset_free(ib_dev->ibd_bio_set); 174 ib_dev->ibd_bio_set = NULL; 175 } 176 ib_dev->ibd_bd = NULL; 177 return ERR_PTR(ret); 178 } 179 180 static void iblock_free_device(void *p) 181 { 182 struct iblock_dev *ib_dev = p; 183 184 if (ib_dev->ibd_bd != NULL) 185 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); 186 if (ib_dev->ibd_bio_set != NULL) 187 bioset_free(ib_dev->ibd_bio_set); 188 kfree(ib_dev); 189 } 190 191 static inline struct iblock_req *IBLOCK_REQ(struct se_task *task) 192 { 193 return container_of(task, struct iblock_req, ib_task); 194 } 195 196 static struct se_task * 197 iblock_alloc_task(unsigned char *cdb) 198 { 199 struct iblock_req *ib_req; 200 201 ib_req = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); 202 if (!ib_req) { 203 pr_err("Unable to allocate memory for struct iblock_req\n"); 204 return NULL; 205 } 206 207 atomic_set(&ib_req->ib_bio_cnt, 0); 208 return &ib_req->ib_task; 209 } 210 211 static unsigned long long iblock_emulate_read_cap_with_block_size( 212 struct se_device *dev, 213 struct block_device *bd, 214 struct request_queue *q) 215 { 216 unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode), 217 bdev_logical_block_size(bd)) - 1); 218 u32 block_size = bdev_logical_block_size(bd); 219 220 if (block_size == dev->se_sub_dev->se_dev_attrib.block_size) 221 return blocks_long; 222 223 switch (block_size) { 224 case 4096: 225 switch (dev->se_sub_dev->se_dev_attrib.block_size) { 226 case 2048: 227 blocks_long <<= 1; 228 break; 229 case 1024: 230 blocks_long <<= 2; 231 break; 232 case 512: 233 blocks_long <<= 3; 234 default: 235 break; 236 } 237 break; 238 case 2048: 239 switch (dev->se_sub_dev->se_dev_attrib.block_size) { 240 case 4096: 241 blocks_long >>= 1; 242 break; 243 case 1024: 244 blocks_long <<= 1; 245 break; 246 case 512: 247 blocks_long <<= 2; 248 break; 249 default: 250 break; 251 } 252 break; 253 case 1024: 254 switch (dev->se_sub_dev->se_dev_attrib.block_size) { 255 case 4096: 256 blocks_long >>= 2; 257 break; 258 case 2048: 259 blocks_long >>= 1; 260 break; 261 case 512: 262 blocks_long <<= 1; 263 break; 264 default: 265 break; 266 } 267 break; 268 case 512: 269 switch (dev->se_sub_dev->se_dev_attrib.block_size) { 270 case 4096: 271 blocks_long >>= 3; 272 break; 273 case 2048: 274 blocks_long >>= 2; 275 break; 276 case 1024: 277 blocks_long >>= 1; 278 break; 279 default: 280 break; 281 } 282 break; 283 default: 284 break; 285 } 286 287 return blocks_long; 288 } 289 290 static void iblock_end_io_flush(struct bio *bio, int err) 291 { 292 struct se_cmd *cmd = bio->bi_private; 293 294 if (err) 295 pr_err("IBLOCK: cache flush failed: %d\n", err); 296 297 if (cmd) 298 transport_complete_sync_cache(cmd, err == 0); 299 bio_put(bio); 300 } 301 302 /* 303 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must 304 * always flush the whole cache. 305 */ 306 static void iblock_emulate_sync_cache(struct se_task *task) 307 { 308 struct se_cmd *cmd = task->task_se_cmd; 309 struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr; 310 int immed = (cmd->t_task_cdb[1] & 0x2); 311 struct bio *bio; 312 313 /* 314 * If the Immediate bit is set, queue up the GOOD response 315 * for this SYNCHRONIZE_CACHE op. 316 */ 317 if (immed) 318 transport_complete_sync_cache(cmd, 1); 319 320 bio = bio_alloc(GFP_KERNEL, 0); 321 bio->bi_end_io = iblock_end_io_flush; 322 bio->bi_bdev = ib_dev->ibd_bd; 323 if (!immed) 324 bio->bi_private = cmd; 325 submit_bio(WRITE_FLUSH, bio); 326 } 327 328 static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range) 329 { 330 struct iblock_dev *ibd = dev->dev_ptr; 331 struct block_device *bd = ibd->ibd_bd; 332 int barrier = 0; 333 334 return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier); 335 } 336 337 static void iblock_free_task(struct se_task *task) 338 { 339 kfree(IBLOCK_REQ(task)); 340 } 341 342 enum { 343 Opt_udev_path, Opt_force, Opt_err 344 }; 345 346 static match_table_t tokens = { 347 {Opt_udev_path, "udev_path=%s"}, 348 {Opt_force, "force=%d"}, 349 {Opt_err, NULL} 350 }; 351 352 static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba, 353 struct se_subsystem_dev *se_dev, 354 const char *page, ssize_t count) 355 { 356 struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr; 357 char *orig, *ptr, *arg_p, *opts; 358 substring_t args[MAX_OPT_ARGS]; 359 int ret = 0, token; 360 361 opts = kstrdup(page, GFP_KERNEL); 362 if (!opts) 363 return -ENOMEM; 364 365 orig = opts; 366 367 while ((ptr = strsep(&opts, ",\n")) != NULL) { 368 if (!*ptr) 369 continue; 370 371 token = match_token(ptr, tokens, args); 372 switch (token) { 373 case Opt_udev_path: 374 if (ib_dev->ibd_bd) { 375 pr_err("Unable to set udev_path= while" 376 " ib_dev->ibd_bd exists\n"); 377 ret = -EEXIST; 378 goto out; 379 } 380 arg_p = match_strdup(&args[0]); 381 if (!arg_p) { 382 ret = -ENOMEM; 383 break; 384 } 385 snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN, 386 "%s", arg_p); 387 kfree(arg_p); 388 pr_debug("IBLOCK: Referencing UDEV path: %s\n", 389 ib_dev->ibd_udev_path); 390 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH; 391 break; 392 case Opt_force: 393 break; 394 default: 395 break; 396 } 397 } 398 399 out: 400 kfree(orig); 401 return (!ret) ? count : ret; 402 } 403 404 static ssize_t iblock_check_configfs_dev_params( 405 struct se_hba *hba, 406 struct se_subsystem_dev *se_dev) 407 { 408 struct iblock_dev *ibd = se_dev->se_dev_su_ptr; 409 410 if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) { 411 pr_err("Missing udev_path= parameters for IBLOCK\n"); 412 return -EINVAL; 413 } 414 415 return 0; 416 } 417 418 static ssize_t iblock_show_configfs_dev_params( 419 struct se_hba *hba, 420 struct se_subsystem_dev *se_dev, 421 char *b) 422 { 423 struct iblock_dev *ibd = se_dev->se_dev_su_ptr; 424 struct block_device *bd = ibd->ibd_bd; 425 char buf[BDEVNAME_SIZE]; 426 ssize_t bl = 0; 427 428 if (bd) 429 bl += sprintf(b + bl, "iBlock device: %s", 430 bdevname(bd, buf)); 431 if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH) { 432 bl += sprintf(b + bl, " UDEV PATH: %s\n", 433 ibd->ibd_udev_path); 434 } else 435 bl += sprintf(b + bl, "\n"); 436 437 bl += sprintf(b + bl, " "); 438 if (bd) { 439 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n", 440 MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ? 441 "" : (bd->bd_holder == ibd) ? 442 "CLAIMED: IBLOCK" : "CLAIMED: OS"); 443 } else { 444 bl += sprintf(b + bl, "Major: 0 Minor: 0\n"); 445 } 446 447 return bl; 448 } 449 450 static void iblock_bio_destructor(struct bio *bio) 451 { 452 struct se_task *task = bio->bi_private; 453 struct iblock_dev *ib_dev = task->task_se_cmd->se_dev->dev_ptr; 454 455 bio_free(bio, ib_dev->ibd_bio_set); 456 } 457 458 static struct bio * 459 iblock_get_bio(struct se_task *task, sector_t lba, u32 sg_num) 460 { 461 struct iblock_dev *ib_dev = task->task_se_cmd->se_dev->dev_ptr; 462 struct iblock_req *ib_req = IBLOCK_REQ(task); 463 struct bio *bio; 464 465 /* 466 * Only allocate as many vector entries as the bio code allows us to, 467 * we'll loop later on until we have handled the whole request. 468 */ 469 if (sg_num > BIO_MAX_PAGES) 470 sg_num = BIO_MAX_PAGES; 471 472 bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set); 473 if (!bio) { 474 pr_err("Unable to allocate memory for bio\n"); 475 return NULL; 476 } 477 478 pr_debug("Allocated bio: %p task_sg_nents: %u using ibd_bio_set:" 479 " %p\n", bio, task->task_sg_nents, ib_dev->ibd_bio_set); 480 pr_debug("Allocated bio: %p task_size: %u\n", bio, task->task_size); 481 482 bio->bi_bdev = ib_dev->ibd_bd; 483 bio->bi_private = task; 484 bio->bi_destructor = iblock_bio_destructor; 485 bio->bi_end_io = &iblock_bio_done; 486 bio->bi_sector = lba; 487 atomic_inc(&ib_req->ib_bio_cnt); 488 489 pr_debug("Set bio->bi_sector: %llu\n", (unsigned long long)bio->bi_sector); 490 pr_debug("Set ib_req->ib_bio_cnt: %d\n", 491 atomic_read(&ib_req->ib_bio_cnt)); 492 return bio; 493 } 494 495 static int iblock_do_task(struct se_task *task) 496 { 497 struct se_cmd *cmd = task->task_se_cmd; 498 struct se_device *dev = cmd->se_dev; 499 struct bio *bio; 500 struct bio_list list; 501 struct scatterlist *sg; 502 u32 i, sg_num = task->task_sg_nents; 503 sector_t block_lba; 504 struct blk_plug plug; 505 int rw; 506 507 if (task->task_data_direction == DMA_TO_DEVICE) { 508 /* 509 * Force data to disk if we pretend to not have a volatile 510 * write cache, or the initiator set the Force Unit Access bit. 511 */ 512 if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 || 513 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 && 514 (cmd->se_cmd_flags & SCF_FUA))) 515 rw = WRITE_FUA; 516 else 517 rw = WRITE; 518 } else { 519 rw = READ; 520 } 521 522 /* 523 * Do starting conversion up from non 512-byte blocksize with 524 * struct se_task SCSI blocksize into Linux/Block 512 units for BIO. 525 */ 526 if (dev->se_sub_dev->se_dev_attrib.block_size == 4096) 527 block_lba = (task->task_lba << 3); 528 else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048) 529 block_lba = (task->task_lba << 2); 530 else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024) 531 block_lba = (task->task_lba << 1); 532 else if (dev->se_sub_dev->se_dev_attrib.block_size == 512) 533 block_lba = task->task_lba; 534 else { 535 pr_err("Unsupported SCSI -> BLOCK LBA conversion:" 536 " %u\n", dev->se_sub_dev->se_dev_attrib.block_size); 537 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 538 return -ENOSYS; 539 } 540 541 bio = iblock_get_bio(task, block_lba, sg_num); 542 if (!bio) { 543 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 544 return -ENOMEM; 545 } 546 547 bio_list_init(&list); 548 bio_list_add(&list, bio); 549 550 for_each_sg(task->task_sg, sg, task->task_sg_nents, i) { 551 /* 552 * XXX: if the length the device accepts is shorter than the 553 * length of the S/G list entry this will cause and 554 * endless loop. Better hope no driver uses huge pages. 555 */ 556 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) 557 != sg->length) { 558 bio = iblock_get_bio(task, block_lba, sg_num); 559 if (!bio) 560 goto fail; 561 bio_list_add(&list, bio); 562 } 563 564 /* Always in 512 byte units for Linux/Block */ 565 block_lba += sg->length >> IBLOCK_LBA_SHIFT; 566 sg_num--; 567 } 568 569 blk_start_plug(&plug); 570 while ((bio = bio_list_pop(&list))) 571 submit_bio(rw, bio); 572 blk_finish_plug(&plug); 573 574 return 0; 575 576 fail: 577 while ((bio = bio_list_pop(&list))) 578 bio_put(bio); 579 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 580 return -ENOMEM; 581 } 582 583 static u32 iblock_get_device_rev(struct se_device *dev) 584 { 585 return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */ 586 } 587 588 static u32 iblock_get_device_type(struct se_device *dev) 589 { 590 return TYPE_DISK; 591 } 592 593 static sector_t iblock_get_blocks(struct se_device *dev) 594 { 595 struct iblock_dev *ibd = dev->dev_ptr; 596 struct block_device *bd = ibd->ibd_bd; 597 struct request_queue *q = bdev_get_queue(bd); 598 599 return iblock_emulate_read_cap_with_block_size(dev, bd, q); 600 } 601 602 static void iblock_bio_done(struct bio *bio, int err) 603 { 604 struct se_task *task = bio->bi_private; 605 struct iblock_req *ibr = IBLOCK_REQ(task); 606 607 /* 608 * Set -EIO if !BIO_UPTODATE and the passed is still err=0 609 */ 610 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err) 611 err = -EIO; 612 613 if (err != 0) { 614 pr_err("test_bit(BIO_UPTODATE) failed for bio: %p," 615 " err: %d\n", bio, err); 616 /* 617 * Bump the ib_bio_err_cnt and release bio. 618 */ 619 atomic_inc(&ibr->ib_bio_err_cnt); 620 smp_mb__after_atomic_inc(); 621 } 622 623 bio_put(bio); 624 625 if (!atomic_dec_and_test(&ibr->ib_bio_cnt)) 626 return; 627 628 pr_debug("done[%p] bio: %p task_lba: %llu bio_lba: %llu err=%d\n", 629 task, bio, task->task_lba, 630 (unsigned long long)bio->bi_sector, err); 631 632 transport_complete_task(task, !atomic_read(&ibr->ib_bio_err_cnt)); 633 } 634 635 static struct se_subsystem_api iblock_template = { 636 .name = "iblock", 637 .owner = THIS_MODULE, 638 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV, 639 .write_cache_emulated = 1, 640 .fua_write_emulated = 1, 641 .attach_hba = iblock_attach_hba, 642 .detach_hba = iblock_detach_hba, 643 .allocate_virtdevice = iblock_allocate_virtdevice, 644 .create_virtdevice = iblock_create_virtdevice, 645 .free_device = iblock_free_device, 646 .alloc_task = iblock_alloc_task, 647 .do_task = iblock_do_task, 648 .do_discard = iblock_do_discard, 649 .do_sync_cache = iblock_emulate_sync_cache, 650 .free_task = iblock_free_task, 651 .check_configfs_dev_params = iblock_check_configfs_dev_params, 652 .set_configfs_dev_params = iblock_set_configfs_dev_params, 653 .show_configfs_dev_params = iblock_show_configfs_dev_params, 654 .get_device_rev = iblock_get_device_rev, 655 .get_device_type = iblock_get_device_type, 656 .get_blocks = iblock_get_blocks, 657 }; 658 659 static int __init iblock_module_init(void) 660 { 661 return transport_subsystem_register(&iblock_template); 662 } 663 664 static void iblock_module_exit(void) 665 { 666 transport_subsystem_release(&iblock_template); 667 } 668 669 MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); 670 MODULE_AUTHOR("nab@Linux-iSCSI.org"); 671 MODULE_LICENSE("GPL"); 672 673 module_init(iblock_module_init); 674 module_exit(iblock_module_exit); 675