1 /* 2 * blkfront.c 3 * 4 * XenLinux virtual block device driver. 5 * 6 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand 7 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge 8 * Copyright (c) 2004, Christian Limpach 9 * Copyright (c) 2004, Andrew Warfield 10 * Copyright (c) 2005, Christopher Clark 11 * Copyright (c) 2005, XenSource Ltd 12 * 13 * This program is free software; you can redistribute it and/or 14 * modify it under the terms of the GNU General Public License version 2 15 * as published by the Free Software Foundation; or, when distributed 16 * separately from the Linux kernel or incorporated into other 17 * software packages, subject to the following license: 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining a copy 20 * of this source file (the "Software"), to deal in the Software without 21 * restriction, including without limitation the rights to use, copy, modify, 22 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 23 * and to permit persons to whom the Software is furnished to do so, subject to 24 * the following conditions: 25 * 26 * The above copyright notice and this permission notice shall be included in 27 * all copies or substantial portions of the Software. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 35 * IN THE SOFTWARE. 36 */ 37 38 #include <linux/interrupt.h> 39 #include <linux/blkdev.h> 40 #include <linux/blk-mq.h> 41 #include <linux/hdreg.h> 42 #include <linux/cdrom.h> 43 #include <linux/module.h> 44 #include <linux/slab.h> 45 #include <linux/major.h> 46 #include <linux/mutex.h> 47 #include <linux/scatterlist.h> 48 #include <linux/bitmap.h> 49 #include <linux/list.h> 50 #include <linux/workqueue.h> 51 #include <linux/sched/mm.h> 52 53 #include <xen/xen.h> 54 #include <xen/xenbus.h> 55 #include <xen/grant_table.h> 56 #include <xen/events.h> 57 #include <xen/page.h> 58 #include <xen/platform_pci.h> 59 60 #include <xen/interface/grant_table.h> 61 #include <xen/interface/io/blkif.h> 62 #include <xen/interface/io/protocols.h> 63 64 #include <asm/xen/hypervisor.h> 65 66 /* 67 * The minimal size of segment supported by the block framework is PAGE_SIZE. 68 * When Linux is using a different page size than Xen, it may not be possible 69 * to put all the data in a single segment. 70 * This can happen when the backend doesn't support indirect descriptor and 71 * therefore the maximum amount of data that a request can carry is 72 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB 73 * 74 * Note that we only support one extra request. So the Linux page size 75 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) = 76 * 88KB. 77 */ 78 #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE) 79 80 enum blkif_state { 81 BLKIF_STATE_DISCONNECTED, 82 BLKIF_STATE_CONNECTED, 83 BLKIF_STATE_SUSPENDED, 84 BLKIF_STATE_ERROR, 85 }; 86 87 struct grant { 88 grant_ref_t gref; 89 struct page *page; 90 struct list_head node; 91 }; 92 93 enum blk_req_status { 94 REQ_PROCESSING, 95 REQ_WAITING, 96 REQ_DONE, 97 REQ_ERROR, 98 REQ_EOPNOTSUPP, 99 }; 100 101 struct blk_shadow { 102 struct blkif_request req; 103 struct request *request; 104 struct grant **grants_used; 105 struct grant **indirect_grants; 106 struct scatterlist *sg; 107 unsigned int num_sg; 108 enum blk_req_status status; 109 110 #define NO_ASSOCIATED_ID ~0UL 111 /* 112 * Id of the sibling if we ever need 2 requests when handling a 113 * block I/O request 114 */ 115 unsigned long associated_id; 116 }; 117 118 struct blkif_req { 119 blk_status_t error; 120 }; 121 122 static inline struct blkif_req *blkif_req(struct request *rq) 123 { 124 return blk_mq_rq_to_pdu(rq); 125 } 126 127 static DEFINE_MUTEX(blkfront_mutex); 128 static const struct block_device_operations xlvbd_block_fops; 129 static struct delayed_work blkfront_work; 130 static LIST_HEAD(info_list); 131 132 /* 133 * Maximum number of segments in indirect requests, the actual value used by 134 * the frontend driver is the minimum of this value and the value provided 135 * by the backend driver. 136 */ 137 138 static unsigned int xen_blkif_max_segments = 32; 139 module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444); 140 MODULE_PARM_DESC(max_indirect_segments, 141 "Maximum amount of segments in indirect requests (default is 32)"); 142 143 static unsigned int xen_blkif_max_queues = 4; 144 module_param_named(max_queues, xen_blkif_max_queues, uint, 0444); 145 MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk"); 146 147 /* 148 * Maximum order of pages to be used for the shared ring between front and 149 * backend, 4KB page granularity is used. 150 */ 151 static unsigned int xen_blkif_max_ring_order; 152 module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444); 153 MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring"); 154 155 #define BLK_RING_SIZE(info) \ 156 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages) 157 158 /* 159 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19 160 * characters are enough. Define to 20 to keep consistent with backend. 161 */ 162 #define RINGREF_NAME_LEN (20) 163 /* 164 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters. 165 */ 166 #define QUEUE_NAME_LEN (17) 167 168 /* 169 * Per-ring info. 170 * Every blkfront device can associate with one or more blkfront_ring_info, 171 * depending on how many hardware queues/rings to be used. 172 */ 173 struct blkfront_ring_info { 174 /* Lock to protect data in every ring buffer. */ 175 spinlock_t ring_lock; 176 struct blkif_front_ring ring; 177 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS]; 178 unsigned int evtchn, irq; 179 struct work_struct work; 180 struct gnttab_free_callback callback; 181 struct list_head indirect_pages; 182 struct list_head grants; 183 unsigned int persistent_gnts_c; 184 unsigned long shadow_free; 185 struct blkfront_info *dev_info; 186 struct blk_shadow shadow[]; 187 }; 188 189 /* 190 * We have one of these per vbd, whether ide, scsi or 'other'. They 191 * hang in private_data off the gendisk structure. We may end up 192 * putting all kinds of interesting stuff here :-) 193 */ 194 struct blkfront_info 195 { 196 struct mutex mutex; 197 struct xenbus_device *xbdev; 198 struct gendisk *gd; 199 u16 sector_size; 200 unsigned int physical_sector_size; 201 unsigned long vdisk_info; 202 int vdevice; 203 blkif_vdev_t handle; 204 enum blkif_state connected; 205 /* Number of pages per ring buffer. */ 206 unsigned int nr_ring_pages; 207 struct request_queue *rq; 208 unsigned int feature_flush:1; 209 unsigned int feature_fua:1; 210 unsigned int feature_discard:1; 211 unsigned int feature_secdiscard:1; 212 unsigned int feature_persistent:1; 213 unsigned int discard_granularity; 214 unsigned int discard_alignment; 215 /* Number of 4KB segments handled */ 216 unsigned int max_indirect_segments; 217 int is_ready; 218 struct blk_mq_tag_set tag_set; 219 struct blkfront_ring_info *rinfo; 220 unsigned int nr_rings; 221 unsigned int rinfo_size; 222 /* Save uncomplete reqs and bios for migration. */ 223 struct list_head requests; 224 struct bio_list bio_list; 225 struct list_head info_list; 226 }; 227 228 static unsigned int nr_minors; 229 static unsigned long *minors; 230 static DEFINE_SPINLOCK(minor_lock); 231 232 #define PARTS_PER_DISK 16 233 #define PARTS_PER_EXT_DISK 256 234 235 #define BLKIF_MAJOR(dev) ((dev)>>8) 236 #define BLKIF_MINOR(dev) ((dev) & 0xff) 237 238 #define EXT_SHIFT 28 239 #define EXTENDED (1<<EXT_SHIFT) 240 #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED)) 241 #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED)) 242 #define EMULATED_HD_DISK_MINOR_OFFSET (0) 243 #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256) 244 #define EMULATED_SD_DISK_MINOR_OFFSET (0) 245 #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256) 246 247 #define DEV_NAME "xvd" /* name in /dev */ 248 249 /* 250 * Grants are always the same size as a Xen page (i.e 4KB). 251 * A physical segment is always the same size as a Linux page. 252 * Number of grants per physical segment 253 */ 254 #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE) 255 256 #define GRANTS_PER_INDIRECT_FRAME \ 257 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment)) 258 259 #define INDIRECT_GREFS(_grants) \ 260 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME) 261 262 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo); 263 static void blkfront_gather_backend_features(struct blkfront_info *info); 264 static int negotiate_mq(struct blkfront_info *info); 265 266 #define for_each_rinfo(info, ptr, idx) \ 267 for ((ptr) = (info)->rinfo, (idx) = 0; \ 268 (idx) < (info)->nr_rings; \ 269 (idx)++, (ptr) = (void *)(ptr) + (info)->rinfo_size) 270 271 static inline struct blkfront_ring_info * 272 get_rinfo(const struct blkfront_info *info, unsigned int i) 273 { 274 BUG_ON(i >= info->nr_rings); 275 return (void *)info->rinfo + i * info->rinfo_size; 276 } 277 278 static int get_id_from_freelist(struct blkfront_ring_info *rinfo) 279 { 280 unsigned long free = rinfo->shadow_free; 281 282 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info)); 283 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id; 284 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */ 285 return free; 286 } 287 288 static int add_id_to_freelist(struct blkfront_ring_info *rinfo, 289 unsigned long id) 290 { 291 if (rinfo->shadow[id].req.u.rw.id != id) 292 return -EINVAL; 293 if (rinfo->shadow[id].request == NULL) 294 return -EINVAL; 295 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free; 296 rinfo->shadow[id].request = NULL; 297 rinfo->shadow_free = id; 298 return 0; 299 } 300 301 static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num) 302 { 303 struct blkfront_info *info = rinfo->dev_info; 304 struct page *granted_page; 305 struct grant *gnt_list_entry, *n; 306 int i = 0; 307 308 while (i < num) { 309 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO); 310 if (!gnt_list_entry) 311 goto out_of_memory; 312 313 if (info->feature_persistent) { 314 granted_page = alloc_page(GFP_NOIO); 315 if (!granted_page) { 316 kfree(gnt_list_entry); 317 goto out_of_memory; 318 } 319 gnt_list_entry->page = granted_page; 320 } 321 322 gnt_list_entry->gref = INVALID_GRANT_REF; 323 list_add(&gnt_list_entry->node, &rinfo->grants); 324 i++; 325 } 326 327 return 0; 328 329 out_of_memory: 330 list_for_each_entry_safe(gnt_list_entry, n, 331 &rinfo->grants, node) { 332 list_del(&gnt_list_entry->node); 333 if (info->feature_persistent) 334 __free_page(gnt_list_entry->page); 335 kfree(gnt_list_entry); 336 i--; 337 } 338 BUG_ON(i != 0); 339 return -ENOMEM; 340 } 341 342 static struct grant *get_free_grant(struct blkfront_ring_info *rinfo) 343 { 344 struct grant *gnt_list_entry; 345 346 BUG_ON(list_empty(&rinfo->grants)); 347 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant, 348 node); 349 list_del(&gnt_list_entry->node); 350 351 if (gnt_list_entry->gref != INVALID_GRANT_REF) 352 rinfo->persistent_gnts_c--; 353 354 return gnt_list_entry; 355 } 356 357 static inline void grant_foreign_access(const struct grant *gnt_list_entry, 358 const struct blkfront_info *info) 359 { 360 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref, 361 info->xbdev->otherend_id, 362 gnt_list_entry->page, 363 0); 364 } 365 366 static struct grant *get_grant(grant_ref_t *gref_head, 367 unsigned long gfn, 368 struct blkfront_ring_info *rinfo) 369 { 370 struct grant *gnt_list_entry = get_free_grant(rinfo); 371 struct blkfront_info *info = rinfo->dev_info; 372 373 if (gnt_list_entry->gref != INVALID_GRANT_REF) 374 return gnt_list_entry; 375 376 /* Assign a gref to this page */ 377 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); 378 BUG_ON(gnt_list_entry->gref == -ENOSPC); 379 if (info->feature_persistent) 380 grant_foreign_access(gnt_list_entry, info); 381 else { 382 /* Grant access to the GFN passed by the caller */ 383 gnttab_grant_foreign_access_ref(gnt_list_entry->gref, 384 info->xbdev->otherend_id, 385 gfn, 0); 386 } 387 388 return gnt_list_entry; 389 } 390 391 static struct grant *get_indirect_grant(grant_ref_t *gref_head, 392 struct blkfront_ring_info *rinfo) 393 { 394 struct grant *gnt_list_entry = get_free_grant(rinfo); 395 struct blkfront_info *info = rinfo->dev_info; 396 397 if (gnt_list_entry->gref != INVALID_GRANT_REF) 398 return gnt_list_entry; 399 400 /* Assign a gref to this page */ 401 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); 402 BUG_ON(gnt_list_entry->gref == -ENOSPC); 403 if (!info->feature_persistent) { 404 struct page *indirect_page; 405 406 /* Fetch a pre-allocated page to use for indirect grefs */ 407 BUG_ON(list_empty(&rinfo->indirect_pages)); 408 indirect_page = list_first_entry(&rinfo->indirect_pages, 409 struct page, lru); 410 list_del(&indirect_page->lru); 411 gnt_list_entry->page = indirect_page; 412 } 413 grant_foreign_access(gnt_list_entry, info); 414 415 return gnt_list_entry; 416 } 417 418 static const char *op_name(int op) 419 { 420 static const char *const names[] = { 421 [BLKIF_OP_READ] = "read", 422 [BLKIF_OP_WRITE] = "write", 423 [BLKIF_OP_WRITE_BARRIER] = "barrier", 424 [BLKIF_OP_FLUSH_DISKCACHE] = "flush", 425 [BLKIF_OP_DISCARD] = "discard" }; 426 427 if (op < 0 || op >= ARRAY_SIZE(names)) 428 return "unknown"; 429 430 if (!names[op]) 431 return "reserved"; 432 433 return names[op]; 434 } 435 static int xlbd_reserve_minors(unsigned int minor, unsigned int nr) 436 { 437 unsigned int end = minor + nr; 438 int rc; 439 440 if (end > nr_minors) { 441 unsigned long *bitmap, *old; 442 443 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap), 444 GFP_KERNEL); 445 if (bitmap == NULL) 446 return -ENOMEM; 447 448 spin_lock(&minor_lock); 449 if (end > nr_minors) { 450 old = minors; 451 memcpy(bitmap, minors, 452 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap)); 453 minors = bitmap; 454 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG; 455 } else 456 old = bitmap; 457 spin_unlock(&minor_lock); 458 kfree(old); 459 } 460 461 spin_lock(&minor_lock); 462 if (find_next_bit(minors, end, minor) >= end) { 463 bitmap_set(minors, minor, nr); 464 rc = 0; 465 } else 466 rc = -EBUSY; 467 spin_unlock(&minor_lock); 468 469 return rc; 470 } 471 472 static void xlbd_release_minors(unsigned int minor, unsigned int nr) 473 { 474 unsigned int end = minor + nr; 475 476 BUG_ON(end > nr_minors); 477 spin_lock(&minor_lock); 478 bitmap_clear(minors, minor, nr); 479 spin_unlock(&minor_lock); 480 } 481 482 static void blkif_restart_queue_callback(void *arg) 483 { 484 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg; 485 schedule_work(&rinfo->work); 486 } 487 488 static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) 489 { 490 /* We don't have real geometry info, but let's at least return 491 values consistent with the size of the device */ 492 sector_t nsect = get_capacity(bd->bd_disk); 493 sector_t cylinders = nsect; 494 495 hg->heads = 0xff; 496 hg->sectors = 0x3f; 497 sector_div(cylinders, hg->heads * hg->sectors); 498 hg->cylinders = cylinders; 499 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) 500 hg->cylinders = 0xffff; 501 return 0; 502 } 503 504 static int blkif_ioctl(struct block_device *bdev, fmode_t mode, 505 unsigned command, unsigned long argument) 506 { 507 struct blkfront_info *info = bdev->bd_disk->private_data; 508 int i; 509 510 switch (command) { 511 case CDROMMULTISESSION: 512 for (i = 0; i < sizeof(struct cdrom_multisession); i++) 513 if (put_user(0, (char __user *)(argument + i))) 514 return -EFAULT; 515 return 0; 516 case CDROM_GET_CAPABILITY: 517 if (!(info->vdisk_info & VDISK_CDROM)) 518 return -EINVAL; 519 return 0; 520 default: 521 return -EINVAL; 522 } 523 } 524 525 static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo, 526 struct request *req, 527 struct blkif_request **ring_req) 528 { 529 unsigned long id; 530 531 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt); 532 rinfo->ring.req_prod_pvt++; 533 534 id = get_id_from_freelist(rinfo); 535 rinfo->shadow[id].request = req; 536 rinfo->shadow[id].status = REQ_PROCESSING; 537 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID; 538 539 rinfo->shadow[id].req.u.rw.id = id; 540 541 return id; 542 } 543 544 static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo) 545 { 546 struct blkfront_info *info = rinfo->dev_info; 547 struct blkif_request *ring_req, *final_ring_req; 548 unsigned long id; 549 550 /* Fill out a communications ring structure. */ 551 id = blkif_ring_get_request(rinfo, req, &final_ring_req); 552 ring_req = &rinfo->shadow[id].req; 553 554 ring_req->operation = BLKIF_OP_DISCARD; 555 ring_req->u.discard.nr_sectors = blk_rq_sectors(req); 556 ring_req->u.discard.id = id; 557 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req); 558 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard) 559 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE; 560 else 561 ring_req->u.discard.flag = 0; 562 563 /* Copy the request to the ring page. */ 564 *final_ring_req = *ring_req; 565 rinfo->shadow[id].status = REQ_WAITING; 566 567 return 0; 568 } 569 570 struct setup_rw_req { 571 unsigned int grant_idx; 572 struct blkif_request_segment *segments; 573 struct blkfront_ring_info *rinfo; 574 struct blkif_request *ring_req; 575 grant_ref_t gref_head; 576 unsigned int id; 577 /* Only used when persistent grant is used and it's a write request */ 578 bool need_copy; 579 unsigned int bvec_off; 580 char *bvec_data; 581 582 bool require_extra_req; 583 struct blkif_request *extra_ring_req; 584 }; 585 586 static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset, 587 unsigned int len, void *data) 588 { 589 struct setup_rw_req *setup = data; 590 int n, ref; 591 struct grant *gnt_list_entry; 592 unsigned int fsect, lsect; 593 /* Convenient aliases */ 594 unsigned int grant_idx = setup->grant_idx; 595 struct blkif_request *ring_req = setup->ring_req; 596 struct blkfront_ring_info *rinfo = setup->rinfo; 597 /* 598 * We always use the shadow of the first request to store the list 599 * of grant associated to the block I/O request. This made the 600 * completion more easy to handle even if the block I/O request is 601 * split. 602 */ 603 struct blk_shadow *shadow = &rinfo->shadow[setup->id]; 604 605 if (unlikely(setup->require_extra_req && 606 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) { 607 /* 608 * We are using the second request, setup grant_idx 609 * to be the index of the segment array. 610 */ 611 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST; 612 ring_req = setup->extra_ring_req; 613 } 614 615 if ((ring_req->operation == BLKIF_OP_INDIRECT) && 616 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) { 617 if (setup->segments) 618 kunmap_atomic(setup->segments); 619 620 n = grant_idx / GRANTS_PER_INDIRECT_FRAME; 621 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo); 622 shadow->indirect_grants[n] = gnt_list_entry; 623 setup->segments = kmap_atomic(gnt_list_entry->page); 624 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref; 625 } 626 627 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo); 628 ref = gnt_list_entry->gref; 629 /* 630 * All the grants are stored in the shadow of the first 631 * request. Therefore we have to use the global index. 632 */ 633 shadow->grants_used[setup->grant_idx] = gnt_list_entry; 634 635 if (setup->need_copy) { 636 void *shared_data; 637 638 shared_data = kmap_atomic(gnt_list_entry->page); 639 /* 640 * this does not wipe data stored outside the 641 * range sg->offset..sg->offset+sg->length. 642 * Therefore, blkback *could* see data from 643 * previous requests. This is OK as long as 644 * persistent grants are shared with just one 645 * domain. It may need refactoring if this 646 * changes 647 */ 648 memcpy(shared_data + offset, 649 setup->bvec_data + setup->bvec_off, 650 len); 651 652 kunmap_atomic(shared_data); 653 setup->bvec_off += len; 654 } 655 656 fsect = offset >> 9; 657 lsect = fsect + (len >> 9) - 1; 658 if (ring_req->operation != BLKIF_OP_INDIRECT) { 659 ring_req->u.rw.seg[grant_idx] = 660 (struct blkif_request_segment) { 661 .gref = ref, 662 .first_sect = fsect, 663 .last_sect = lsect }; 664 } else { 665 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] = 666 (struct blkif_request_segment) { 667 .gref = ref, 668 .first_sect = fsect, 669 .last_sect = lsect }; 670 } 671 672 (setup->grant_idx)++; 673 } 674 675 static void blkif_setup_extra_req(struct blkif_request *first, 676 struct blkif_request *second) 677 { 678 uint16_t nr_segments = first->u.rw.nr_segments; 679 680 /* 681 * The second request is only present when the first request uses 682 * all its segments. It's always the continuity of the first one. 683 */ 684 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; 685 686 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST; 687 second->u.rw.sector_number = first->u.rw.sector_number + 688 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512; 689 690 second->u.rw.handle = first->u.rw.handle; 691 second->operation = first->operation; 692 } 693 694 static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo) 695 { 696 struct blkfront_info *info = rinfo->dev_info; 697 struct blkif_request *ring_req, *extra_ring_req = NULL; 698 struct blkif_request *final_ring_req, *final_extra_ring_req = NULL; 699 unsigned long id, extra_id = NO_ASSOCIATED_ID; 700 bool require_extra_req = false; 701 int i; 702 struct setup_rw_req setup = { 703 .grant_idx = 0, 704 .segments = NULL, 705 .rinfo = rinfo, 706 .need_copy = rq_data_dir(req) && info->feature_persistent, 707 }; 708 709 /* 710 * Used to store if we are able to queue the request by just using 711 * existing persistent grants, or if we have to get new grants, 712 * as there are not sufficiently many free. 713 */ 714 bool new_persistent_gnts = false; 715 struct scatterlist *sg; 716 int num_sg, max_grefs, num_grant; 717 718 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG; 719 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST) 720 /* 721 * If we are using indirect segments we need to account 722 * for the indirect grefs used in the request. 723 */ 724 max_grefs += INDIRECT_GREFS(max_grefs); 725 726 /* Check if we have enough persistent grants to allocate a requests */ 727 if (rinfo->persistent_gnts_c < max_grefs) { 728 new_persistent_gnts = true; 729 730 if (gnttab_alloc_grant_references( 731 max_grefs - rinfo->persistent_gnts_c, 732 &setup.gref_head) < 0) { 733 gnttab_request_free_callback( 734 &rinfo->callback, 735 blkif_restart_queue_callback, 736 rinfo, 737 max_grefs - rinfo->persistent_gnts_c); 738 return 1; 739 } 740 } 741 742 /* Fill out a communications ring structure. */ 743 id = blkif_ring_get_request(rinfo, req, &final_ring_req); 744 ring_req = &rinfo->shadow[id].req; 745 746 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg); 747 num_grant = 0; 748 /* Calculate the number of grant used */ 749 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) 750 num_grant += gnttab_count_grant(sg->offset, sg->length); 751 752 require_extra_req = info->max_indirect_segments == 0 && 753 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST; 754 BUG_ON(!HAS_EXTRA_REQ && require_extra_req); 755 756 rinfo->shadow[id].num_sg = num_sg; 757 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST && 758 likely(!require_extra_req)) { 759 /* 760 * The indirect operation can only be a BLKIF_OP_READ or 761 * BLKIF_OP_WRITE 762 */ 763 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA); 764 ring_req->operation = BLKIF_OP_INDIRECT; 765 ring_req->u.indirect.indirect_op = rq_data_dir(req) ? 766 BLKIF_OP_WRITE : BLKIF_OP_READ; 767 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req); 768 ring_req->u.indirect.handle = info->handle; 769 ring_req->u.indirect.nr_segments = num_grant; 770 } else { 771 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req); 772 ring_req->u.rw.handle = info->handle; 773 ring_req->operation = rq_data_dir(req) ? 774 BLKIF_OP_WRITE : BLKIF_OP_READ; 775 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) { 776 /* 777 * Ideally we can do an unordered flush-to-disk. 778 * In case the backend onlysupports barriers, use that. 779 * A barrier request a superset of FUA, so we can 780 * implement it the same way. (It's also a FLUSH+FUA, 781 * since it is guaranteed ordered WRT previous writes.) 782 */ 783 if (info->feature_flush && info->feature_fua) 784 ring_req->operation = 785 BLKIF_OP_WRITE_BARRIER; 786 else if (info->feature_flush) 787 ring_req->operation = 788 BLKIF_OP_FLUSH_DISKCACHE; 789 else 790 ring_req->operation = 0; 791 } 792 ring_req->u.rw.nr_segments = num_grant; 793 if (unlikely(require_extra_req)) { 794 extra_id = blkif_ring_get_request(rinfo, req, 795 &final_extra_ring_req); 796 extra_ring_req = &rinfo->shadow[extra_id].req; 797 798 /* 799 * Only the first request contains the scatter-gather 800 * list. 801 */ 802 rinfo->shadow[extra_id].num_sg = 0; 803 804 blkif_setup_extra_req(ring_req, extra_ring_req); 805 806 /* Link the 2 requests together */ 807 rinfo->shadow[extra_id].associated_id = id; 808 rinfo->shadow[id].associated_id = extra_id; 809 } 810 } 811 812 setup.ring_req = ring_req; 813 setup.id = id; 814 815 setup.require_extra_req = require_extra_req; 816 if (unlikely(require_extra_req)) 817 setup.extra_ring_req = extra_ring_req; 818 819 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) { 820 BUG_ON(sg->offset + sg->length > PAGE_SIZE); 821 822 if (setup.need_copy) { 823 setup.bvec_off = sg->offset; 824 setup.bvec_data = kmap_atomic(sg_page(sg)); 825 } 826 827 gnttab_foreach_grant_in_range(sg_page(sg), 828 sg->offset, 829 sg->length, 830 blkif_setup_rw_req_grant, 831 &setup); 832 833 if (setup.need_copy) 834 kunmap_atomic(setup.bvec_data); 835 } 836 if (setup.segments) 837 kunmap_atomic(setup.segments); 838 839 /* Copy request(s) to the ring page. */ 840 *final_ring_req = *ring_req; 841 rinfo->shadow[id].status = REQ_WAITING; 842 if (unlikely(require_extra_req)) { 843 *final_extra_ring_req = *extra_ring_req; 844 rinfo->shadow[extra_id].status = REQ_WAITING; 845 } 846 847 if (new_persistent_gnts) 848 gnttab_free_grant_references(setup.gref_head); 849 850 return 0; 851 } 852 853 /* 854 * Generate a Xen blkfront IO request from a blk layer request. Reads 855 * and writes are handled as expected. 856 * 857 * @req: a request struct 858 */ 859 static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo) 860 { 861 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED)) 862 return 1; 863 864 if (unlikely(req_op(req) == REQ_OP_DISCARD || 865 req_op(req) == REQ_OP_SECURE_ERASE)) 866 return blkif_queue_discard_req(req, rinfo); 867 else 868 return blkif_queue_rw_req(req, rinfo); 869 } 870 871 static inline void flush_requests(struct blkfront_ring_info *rinfo) 872 { 873 int notify; 874 875 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify); 876 877 if (notify) 878 notify_remote_via_irq(rinfo->irq); 879 } 880 881 static inline bool blkif_request_flush_invalid(struct request *req, 882 struct blkfront_info *info) 883 { 884 return (blk_rq_is_passthrough(req) || 885 ((req_op(req) == REQ_OP_FLUSH) && 886 !info->feature_flush) || 887 ((req->cmd_flags & REQ_FUA) && 888 !info->feature_fua)); 889 } 890 891 static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx, 892 const struct blk_mq_queue_data *qd) 893 { 894 unsigned long flags; 895 int qid = hctx->queue_num; 896 struct blkfront_info *info = hctx->queue->queuedata; 897 struct blkfront_ring_info *rinfo = NULL; 898 899 rinfo = get_rinfo(info, qid); 900 blk_mq_start_request(qd->rq); 901 spin_lock_irqsave(&rinfo->ring_lock, flags); 902 if (RING_FULL(&rinfo->ring)) 903 goto out_busy; 904 905 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info)) 906 goto out_err; 907 908 if (blkif_queue_request(qd->rq, rinfo)) 909 goto out_busy; 910 911 flush_requests(rinfo); 912 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 913 return BLK_STS_OK; 914 915 out_err: 916 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 917 return BLK_STS_IOERR; 918 919 out_busy: 920 blk_mq_stop_hw_queue(hctx); 921 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 922 return BLK_STS_DEV_RESOURCE; 923 } 924 925 static void blkif_complete_rq(struct request *rq) 926 { 927 blk_mq_end_request(rq, blkif_req(rq)->error); 928 } 929 930 static const struct blk_mq_ops blkfront_mq_ops = { 931 .queue_rq = blkif_queue_rq, 932 .complete = blkif_complete_rq, 933 }; 934 935 static void blkif_set_queue_limits(struct blkfront_info *info) 936 { 937 struct request_queue *rq = info->rq; 938 struct gendisk *gd = info->gd; 939 unsigned int segments = info->max_indirect_segments ? : 940 BLKIF_MAX_SEGMENTS_PER_REQUEST; 941 942 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq); 943 944 if (info->feature_discard) { 945 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq); 946 blk_queue_max_discard_sectors(rq, get_capacity(gd)); 947 rq->limits.discard_granularity = info->discard_granularity ?: 948 info->physical_sector_size; 949 rq->limits.discard_alignment = info->discard_alignment; 950 if (info->feature_secdiscard) 951 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq); 952 } 953 954 /* Hard sector size and max sectors impersonate the equiv. hardware. */ 955 blk_queue_logical_block_size(rq, info->sector_size); 956 blk_queue_physical_block_size(rq, info->physical_sector_size); 957 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512); 958 959 /* Each segment in a request is up to an aligned page in size. */ 960 blk_queue_segment_boundary(rq, PAGE_SIZE - 1); 961 blk_queue_max_segment_size(rq, PAGE_SIZE); 962 963 /* Ensure a merged request will fit in a single I/O ring slot. */ 964 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG); 965 966 /* Make sure buffer addresses are sector-aligned. */ 967 blk_queue_dma_alignment(rq, 511); 968 } 969 970 static const char *flush_info(struct blkfront_info *info) 971 { 972 if (info->feature_flush && info->feature_fua) 973 return "barrier: enabled;"; 974 else if (info->feature_flush) 975 return "flush diskcache: enabled;"; 976 else 977 return "barrier or flush: disabled;"; 978 } 979 980 static void xlvbd_flush(struct blkfront_info *info) 981 { 982 blk_queue_write_cache(info->rq, info->feature_flush ? true : false, 983 info->feature_fua ? true : false); 984 pr_info("blkfront: %s: %s %s %s %s %s\n", 985 info->gd->disk_name, flush_info(info), 986 "persistent grants:", info->feature_persistent ? 987 "enabled;" : "disabled;", "indirect descriptors:", 988 info->max_indirect_segments ? "enabled;" : "disabled;"); 989 } 990 991 static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset) 992 { 993 int major; 994 major = BLKIF_MAJOR(vdevice); 995 *minor = BLKIF_MINOR(vdevice); 996 switch (major) { 997 case XEN_IDE0_MAJOR: 998 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET; 999 *minor = ((*minor / 64) * PARTS_PER_DISK) + 1000 EMULATED_HD_DISK_MINOR_OFFSET; 1001 break; 1002 case XEN_IDE1_MAJOR: 1003 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET; 1004 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) + 1005 EMULATED_HD_DISK_MINOR_OFFSET; 1006 break; 1007 case XEN_SCSI_DISK0_MAJOR: 1008 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET; 1009 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET; 1010 break; 1011 case XEN_SCSI_DISK1_MAJOR: 1012 case XEN_SCSI_DISK2_MAJOR: 1013 case XEN_SCSI_DISK3_MAJOR: 1014 case XEN_SCSI_DISK4_MAJOR: 1015 case XEN_SCSI_DISK5_MAJOR: 1016 case XEN_SCSI_DISK6_MAJOR: 1017 case XEN_SCSI_DISK7_MAJOR: 1018 *offset = (*minor / PARTS_PER_DISK) + 1019 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) + 1020 EMULATED_SD_DISK_NAME_OFFSET; 1021 *minor = *minor + 1022 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) + 1023 EMULATED_SD_DISK_MINOR_OFFSET; 1024 break; 1025 case XEN_SCSI_DISK8_MAJOR: 1026 case XEN_SCSI_DISK9_MAJOR: 1027 case XEN_SCSI_DISK10_MAJOR: 1028 case XEN_SCSI_DISK11_MAJOR: 1029 case XEN_SCSI_DISK12_MAJOR: 1030 case XEN_SCSI_DISK13_MAJOR: 1031 case XEN_SCSI_DISK14_MAJOR: 1032 case XEN_SCSI_DISK15_MAJOR: 1033 *offset = (*minor / PARTS_PER_DISK) + 1034 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) + 1035 EMULATED_SD_DISK_NAME_OFFSET; 1036 *minor = *minor + 1037 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) + 1038 EMULATED_SD_DISK_MINOR_OFFSET; 1039 break; 1040 case XENVBD_MAJOR: 1041 *offset = *minor / PARTS_PER_DISK; 1042 break; 1043 default: 1044 printk(KERN_WARNING "blkfront: your disk configuration is " 1045 "incorrect, please use an xvd device instead\n"); 1046 return -ENODEV; 1047 } 1048 return 0; 1049 } 1050 1051 static char *encode_disk_name(char *ptr, unsigned int n) 1052 { 1053 if (n >= 26) 1054 ptr = encode_disk_name(ptr, n / 26 - 1); 1055 *ptr = 'a' + n % 26; 1056 return ptr + 1; 1057 } 1058 1059 static int xlvbd_alloc_gendisk(blkif_sector_t capacity, 1060 struct blkfront_info *info, u16 sector_size, 1061 unsigned int physical_sector_size) 1062 { 1063 struct gendisk *gd; 1064 int nr_minors = 1; 1065 int err; 1066 unsigned int offset; 1067 int minor; 1068 int nr_parts; 1069 char *ptr; 1070 1071 BUG_ON(info->gd != NULL); 1072 BUG_ON(info->rq != NULL); 1073 1074 if ((info->vdevice>>EXT_SHIFT) > 1) { 1075 /* this is above the extended range; something is wrong */ 1076 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice); 1077 return -ENODEV; 1078 } 1079 1080 if (!VDEV_IS_EXTENDED(info->vdevice)) { 1081 err = xen_translate_vdev(info->vdevice, &minor, &offset); 1082 if (err) 1083 return err; 1084 nr_parts = PARTS_PER_DISK; 1085 } else { 1086 minor = BLKIF_MINOR_EXT(info->vdevice); 1087 nr_parts = PARTS_PER_EXT_DISK; 1088 offset = minor / nr_parts; 1089 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4) 1090 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with " 1091 "emulated IDE disks,\n\t choose an xvd device name" 1092 "from xvde on\n", info->vdevice); 1093 } 1094 if (minor >> MINORBITS) { 1095 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n", 1096 info->vdevice, minor); 1097 return -ENODEV; 1098 } 1099 1100 if ((minor % nr_parts) == 0) 1101 nr_minors = nr_parts; 1102 1103 err = xlbd_reserve_minors(minor, nr_minors); 1104 if (err) 1105 return err; 1106 1107 memset(&info->tag_set, 0, sizeof(info->tag_set)); 1108 info->tag_set.ops = &blkfront_mq_ops; 1109 info->tag_set.nr_hw_queues = info->nr_rings; 1110 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) { 1111 /* 1112 * When indirect descriptior is not supported, the I/O request 1113 * will be split between multiple request in the ring. 1114 * To avoid problems when sending the request, divide by 1115 * 2 the depth of the queue. 1116 */ 1117 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2; 1118 } else 1119 info->tag_set.queue_depth = BLK_RING_SIZE(info); 1120 info->tag_set.numa_node = NUMA_NO_NODE; 1121 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 1122 info->tag_set.cmd_size = sizeof(struct blkif_req); 1123 info->tag_set.driver_data = info; 1124 1125 err = blk_mq_alloc_tag_set(&info->tag_set); 1126 if (err) 1127 goto out_release_minors; 1128 1129 gd = blk_mq_alloc_disk(&info->tag_set, info); 1130 if (IS_ERR(gd)) { 1131 err = PTR_ERR(gd); 1132 goto out_free_tag_set; 1133 } 1134 1135 strcpy(gd->disk_name, DEV_NAME); 1136 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset); 1137 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN); 1138 if (nr_minors > 1) 1139 *ptr = 0; 1140 else 1141 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr, 1142 "%d", minor & (nr_parts - 1)); 1143 1144 gd->major = XENVBD_MAJOR; 1145 gd->first_minor = minor; 1146 gd->minors = nr_minors; 1147 gd->fops = &xlvbd_block_fops; 1148 gd->private_data = info; 1149 set_capacity(gd, capacity); 1150 1151 info->rq = gd->queue; 1152 info->gd = gd; 1153 info->sector_size = sector_size; 1154 info->physical_sector_size = physical_sector_size; 1155 blkif_set_queue_limits(info); 1156 1157 xlvbd_flush(info); 1158 1159 if (info->vdisk_info & VDISK_READONLY) 1160 set_disk_ro(gd, 1); 1161 if (info->vdisk_info & VDISK_REMOVABLE) 1162 gd->flags |= GENHD_FL_REMOVABLE; 1163 1164 return 0; 1165 1166 out_free_tag_set: 1167 blk_mq_free_tag_set(&info->tag_set); 1168 out_release_minors: 1169 xlbd_release_minors(minor, nr_minors); 1170 return err; 1171 } 1172 1173 /* Already hold rinfo->ring_lock. */ 1174 static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo) 1175 { 1176 if (!RING_FULL(&rinfo->ring)) 1177 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true); 1178 } 1179 1180 static void kick_pending_request_queues(struct blkfront_ring_info *rinfo) 1181 { 1182 unsigned long flags; 1183 1184 spin_lock_irqsave(&rinfo->ring_lock, flags); 1185 kick_pending_request_queues_locked(rinfo); 1186 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 1187 } 1188 1189 static void blkif_restart_queue(struct work_struct *work) 1190 { 1191 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work); 1192 1193 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED) 1194 kick_pending_request_queues(rinfo); 1195 } 1196 1197 static void blkif_free_ring(struct blkfront_ring_info *rinfo) 1198 { 1199 struct grant *persistent_gnt, *n; 1200 struct blkfront_info *info = rinfo->dev_info; 1201 int i, j, segs; 1202 1203 /* 1204 * Remove indirect pages, this only happens when using indirect 1205 * descriptors but not persistent grants 1206 */ 1207 if (!list_empty(&rinfo->indirect_pages)) { 1208 struct page *indirect_page, *n; 1209 1210 BUG_ON(info->feature_persistent); 1211 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { 1212 list_del(&indirect_page->lru); 1213 __free_page(indirect_page); 1214 } 1215 } 1216 1217 /* Remove all persistent grants. */ 1218 if (!list_empty(&rinfo->grants)) { 1219 list_for_each_entry_safe(persistent_gnt, n, 1220 &rinfo->grants, node) { 1221 list_del(&persistent_gnt->node); 1222 if (persistent_gnt->gref != INVALID_GRANT_REF) { 1223 gnttab_end_foreign_access(persistent_gnt->gref, 1224 0UL); 1225 rinfo->persistent_gnts_c--; 1226 } 1227 if (info->feature_persistent) 1228 __free_page(persistent_gnt->page); 1229 kfree(persistent_gnt); 1230 } 1231 } 1232 BUG_ON(rinfo->persistent_gnts_c != 0); 1233 1234 for (i = 0; i < BLK_RING_SIZE(info); i++) { 1235 /* 1236 * Clear persistent grants present in requests already 1237 * on the shared ring 1238 */ 1239 if (!rinfo->shadow[i].request) 1240 goto free_shadow; 1241 1242 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ? 1243 rinfo->shadow[i].req.u.indirect.nr_segments : 1244 rinfo->shadow[i].req.u.rw.nr_segments; 1245 for (j = 0; j < segs; j++) { 1246 persistent_gnt = rinfo->shadow[i].grants_used[j]; 1247 gnttab_end_foreign_access(persistent_gnt->gref, 0UL); 1248 if (info->feature_persistent) 1249 __free_page(persistent_gnt->page); 1250 kfree(persistent_gnt); 1251 } 1252 1253 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT) 1254 /* 1255 * If this is not an indirect operation don't try to 1256 * free indirect segments 1257 */ 1258 goto free_shadow; 1259 1260 for (j = 0; j < INDIRECT_GREFS(segs); j++) { 1261 persistent_gnt = rinfo->shadow[i].indirect_grants[j]; 1262 gnttab_end_foreign_access(persistent_gnt->gref, 0UL); 1263 __free_page(persistent_gnt->page); 1264 kfree(persistent_gnt); 1265 } 1266 1267 free_shadow: 1268 kvfree(rinfo->shadow[i].grants_used); 1269 rinfo->shadow[i].grants_used = NULL; 1270 kvfree(rinfo->shadow[i].indirect_grants); 1271 rinfo->shadow[i].indirect_grants = NULL; 1272 kvfree(rinfo->shadow[i].sg); 1273 rinfo->shadow[i].sg = NULL; 1274 } 1275 1276 /* No more gnttab callback work. */ 1277 gnttab_cancel_free_callback(&rinfo->callback); 1278 1279 /* Flush gnttab callback work. Must be done with no locks held. */ 1280 flush_work(&rinfo->work); 1281 1282 /* Free resources associated with old device channel. */ 1283 for (i = 0; i < info->nr_ring_pages; i++) { 1284 if (rinfo->ring_ref[i] != INVALID_GRANT_REF) { 1285 gnttab_end_foreign_access(rinfo->ring_ref[i], 0); 1286 rinfo->ring_ref[i] = INVALID_GRANT_REF; 1287 } 1288 } 1289 free_pages_exact(rinfo->ring.sring, 1290 info->nr_ring_pages * XEN_PAGE_SIZE); 1291 rinfo->ring.sring = NULL; 1292 1293 if (rinfo->irq) 1294 unbind_from_irqhandler(rinfo->irq, rinfo); 1295 rinfo->evtchn = rinfo->irq = 0; 1296 } 1297 1298 static void blkif_free(struct blkfront_info *info, int suspend) 1299 { 1300 unsigned int i; 1301 struct blkfront_ring_info *rinfo; 1302 1303 /* Prevent new requests being issued until we fix things up. */ 1304 info->connected = suspend ? 1305 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; 1306 /* No more blkif_request(). */ 1307 if (info->rq) 1308 blk_mq_stop_hw_queues(info->rq); 1309 1310 for_each_rinfo(info, rinfo, i) 1311 blkif_free_ring(rinfo); 1312 1313 kvfree(info->rinfo); 1314 info->rinfo = NULL; 1315 info->nr_rings = 0; 1316 } 1317 1318 struct copy_from_grant { 1319 const struct blk_shadow *s; 1320 unsigned int grant_idx; 1321 unsigned int bvec_offset; 1322 char *bvec_data; 1323 }; 1324 1325 static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset, 1326 unsigned int len, void *data) 1327 { 1328 struct copy_from_grant *info = data; 1329 char *shared_data; 1330 /* Convenient aliases */ 1331 const struct blk_shadow *s = info->s; 1332 1333 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page); 1334 1335 memcpy(info->bvec_data + info->bvec_offset, 1336 shared_data + offset, len); 1337 1338 info->bvec_offset += len; 1339 info->grant_idx++; 1340 1341 kunmap_atomic(shared_data); 1342 } 1343 1344 static enum blk_req_status blkif_rsp_to_req_status(int rsp) 1345 { 1346 switch (rsp) 1347 { 1348 case BLKIF_RSP_OKAY: 1349 return REQ_DONE; 1350 case BLKIF_RSP_EOPNOTSUPP: 1351 return REQ_EOPNOTSUPP; 1352 case BLKIF_RSP_ERROR: 1353 default: 1354 return REQ_ERROR; 1355 } 1356 } 1357 1358 /* 1359 * Get the final status of the block request based on two ring response 1360 */ 1361 static int blkif_get_final_status(enum blk_req_status s1, 1362 enum blk_req_status s2) 1363 { 1364 BUG_ON(s1 < REQ_DONE); 1365 BUG_ON(s2 < REQ_DONE); 1366 1367 if (s1 == REQ_ERROR || s2 == REQ_ERROR) 1368 return BLKIF_RSP_ERROR; 1369 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP) 1370 return BLKIF_RSP_EOPNOTSUPP; 1371 return BLKIF_RSP_OKAY; 1372 } 1373 1374 /* 1375 * Return values: 1376 * 1 response processed. 1377 * 0 missing further responses. 1378 * -1 error while processing. 1379 */ 1380 static int blkif_completion(unsigned long *id, 1381 struct blkfront_ring_info *rinfo, 1382 struct blkif_response *bret) 1383 { 1384 int i = 0; 1385 struct scatterlist *sg; 1386 int num_sg, num_grant; 1387 struct blkfront_info *info = rinfo->dev_info; 1388 struct blk_shadow *s = &rinfo->shadow[*id]; 1389 struct copy_from_grant data = { 1390 .grant_idx = 0, 1391 }; 1392 1393 num_grant = s->req.operation == BLKIF_OP_INDIRECT ? 1394 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments; 1395 1396 /* The I/O request may be split in two. */ 1397 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) { 1398 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id]; 1399 1400 /* Keep the status of the current response in shadow. */ 1401 s->status = blkif_rsp_to_req_status(bret->status); 1402 1403 /* Wait the second response if not yet here. */ 1404 if (s2->status < REQ_DONE) 1405 return 0; 1406 1407 bret->status = blkif_get_final_status(s->status, 1408 s2->status); 1409 1410 /* 1411 * All the grants is stored in the first shadow in order 1412 * to make the completion code simpler. 1413 */ 1414 num_grant += s2->req.u.rw.nr_segments; 1415 1416 /* 1417 * The two responses may not come in order. Only the 1418 * first request will store the scatter-gather list. 1419 */ 1420 if (s2->num_sg != 0) { 1421 /* Update "id" with the ID of the first response. */ 1422 *id = s->associated_id; 1423 s = s2; 1424 } 1425 1426 /* 1427 * We don't need anymore the second request, so recycling 1428 * it now. 1429 */ 1430 if (add_id_to_freelist(rinfo, s->associated_id)) 1431 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n", 1432 info->gd->disk_name, s->associated_id); 1433 } 1434 1435 data.s = s; 1436 num_sg = s->num_sg; 1437 1438 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) { 1439 for_each_sg(s->sg, sg, num_sg, i) { 1440 BUG_ON(sg->offset + sg->length > PAGE_SIZE); 1441 1442 data.bvec_offset = sg->offset; 1443 data.bvec_data = kmap_atomic(sg_page(sg)); 1444 1445 gnttab_foreach_grant_in_range(sg_page(sg), 1446 sg->offset, 1447 sg->length, 1448 blkif_copy_from_grant, 1449 &data); 1450 1451 kunmap_atomic(data.bvec_data); 1452 } 1453 } 1454 /* Add the persistent grant into the list of free grants */ 1455 for (i = 0; i < num_grant; i++) { 1456 if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) { 1457 /* 1458 * If the grant is still mapped by the backend (the 1459 * backend has chosen to make this grant persistent) 1460 * we add it at the head of the list, so it will be 1461 * reused first. 1462 */ 1463 if (!info->feature_persistent) { 1464 pr_alert("backed has not unmapped grant: %u\n", 1465 s->grants_used[i]->gref); 1466 return -1; 1467 } 1468 list_add(&s->grants_used[i]->node, &rinfo->grants); 1469 rinfo->persistent_gnts_c++; 1470 } else { 1471 /* 1472 * If the grant is not mapped by the backend we add it 1473 * to the tail of the list, so it will not be picked 1474 * again unless we run out of persistent grants. 1475 */ 1476 s->grants_used[i]->gref = INVALID_GRANT_REF; 1477 list_add_tail(&s->grants_used[i]->node, &rinfo->grants); 1478 } 1479 } 1480 if (s->req.operation == BLKIF_OP_INDIRECT) { 1481 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) { 1482 if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) { 1483 if (!info->feature_persistent) { 1484 pr_alert("backed has not unmapped grant: %u\n", 1485 s->indirect_grants[i]->gref); 1486 return -1; 1487 } 1488 list_add(&s->indirect_grants[i]->node, &rinfo->grants); 1489 rinfo->persistent_gnts_c++; 1490 } else { 1491 struct page *indirect_page; 1492 1493 /* 1494 * Add the used indirect page back to the list of 1495 * available pages for indirect grefs. 1496 */ 1497 if (!info->feature_persistent) { 1498 indirect_page = s->indirect_grants[i]->page; 1499 list_add(&indirect_page->lru, &rinfo->indirect_pages); 1500 } 1501 s->indirect_grants[i]->gref = INVALID_GRANT_REF; 1502 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants); 1503 } 1504 } 1505 } 1506 1507 return 1; 1508 } 1509 1510 static irqreturn_t blkif_interrupt(int irq, void *dev_id) 1511 { 1512 struct request *req; 1513 struct blkif_response bret; 1514 RING_IDX i, rp; 1515 unsigned long flags; 1516 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id; 1517 struct blkfront_info *info = rinfo->dev_info; 1518 unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; 1519 1520 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { 1521 xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS); 1522 return IRQ_HANDLED; 1523 } 1524 1525 spin_lock_irqsave(&rinfo->ring_lock, flags); 1526 again: 1527 rp = READ_ONCE(rinfo->ring.sring->rsp_prod); 1528 virt_rmb(); /* Ensure we see queued responses up to 'rp'. */ 1529 if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) { 1530 pr_alert("%s: illegal number of responses %u\n", 1531 info->gd->disk_name, rp - rinfo->ring.rsp_cons); 1532 goto err; 1533 } 1534 1535 for (i = rinfo->ring.rsp_cons; i != rp; i++) { 1536 unsigned long id; 1537 unsigned int op; 1538 1539 eoiflag = 0; 1540 1541 RING_COPY_RESPONSE(&rinfo->ring, i, &bret); 1542 id = bret.id; 1543 1544 /* 1545 * The backend has messed up and given us an id that we would 1546 * never have given to it (we stamp it up to BLK_RING_SIZE - 1547 * look in get_id_from_freelist. 1548 */ 1549 if (id >= BLK_RING_SIZE(info)) { 1550 pr_alert("%s: response has incorrect id (%ld)\n", 1551 info->gd->disk_name, id); 1552 goto err; 1553 } 1554 if (rinfo->shadow[id].status != REQ_WAITING) { 1555 pr_alert("%s: response references no pending request\n", 1556 info->gd->disk_name); 1557 goto err; 1558 } 1559 1560 rinfo->shadow[id].status = REQ_PROCESSING; 1561 req = rinfo->shadow[id].request; 1562 1563 op = rinfo->shadow[id].req.operation; 1564 if (op == BLKIF_OP_INDIRECT) 1565 op = rinfo->shadow[id].req.u.indirect.indirect_op; 1566 if (bret.operation != op) { 1567 pr_alert("%s: response has wrong operation (%u instead of %u)\n", 1568 info->gd->disk_name, bret.operation, op); 1569 goto err; 1570 } 1571 1572 if (bret.operation != BLKIF_OP_DISCARD) { 1573 int ret; 1574 1575 /* 1576 * We may need to wait for an extra response if the 1577 * I/O request is split in 2 1578 */ 1579 ret = blkif_completion(&id, rinfo, &bret); 1580 if (!ret) 1581 continue; 1582 if (unlikely(ret < 0)) 1583 goto err; 1584 } 1585 1586 if (add_id_to_freelist(rinfo, id)) { 1587 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n", 1588 info->gd->disk_name, op_name(bret.operation), id); 1589 continue; 1590 } 1591 1592 if (bret.status == BLKIF_RSP_OKAY) 1593 blkif_req(req)->error = BLK_STS_OK; 1594 else 1595 blkif_req(req)->error = BLK_STS_IOERR; 1596 1597 switch (bret.operation) { 1598 case BLKIF_OP_DISCARD: 1599 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) { 1600 struct request_queue *rq = info->rq; 1601 1602 pr_warn_ratelimited("blkfront: %s: %s op failed\n", 1603 info->gd->disk_name, op_name(bret.operation)); 1604 blkif_req(req)->error = BLK_STS_NOTSUPP; 1605 info->feature_discard = 0; 1606 info->feature_secdiscard = 0; 1607 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq); 1608 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq); 1609 } 1610 break; 1611 case BLKIF_OP_FLUSH_DISKCACHE: 1612 case BLKIF_OP_WRITE_BARRIER: 1613 if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) { 1614 pr_warn_ratelimited("blkfront: %s: %s op failed\n", 1615 info->gd->disk_name, op_name(bret.operation)); 1616 blkif_req(req)->error = BLK_STS_NOTSUPP; 1617 } 1618 if (unlikely(bret.status == BLKIF_RSP_ERROR && 1619 rinfo->shadow[id].req.u.rw.nr_segments == 0)) { 1620 pr_warn_ratelimited("blkfront: %s: empty %s op failed\n", 1621 info->gd->disk_name, op_name(bret.operation)); 1622 blkif_req(req)->error = BLK_STS_NOTSUPP; 1623 } 1624 if (unlikely(blkif_req(req)->error)) { 1625 if (blkif_req(req)->error == BLK_STS_NOTSUPP) 1626 blkif_req(req)->error = BLK_STS_OK; 1627 info->feature_fua = 0; 1628 info->feature_flush = 0; 1629 xlvbd_flush(info); 1630 } 1631 fallthrough; 1632 case BLKIF_OP_READ: 1633 case BLKIF_OP_WRITE: 1634 if (unlikely(bret.status != BLKIF_RSP_OKAY)) 1635 dev_dbg_ratelimited(&info->xbdev->dev, 1636 "Bad return from blkdev data request: %#x\n", 1637 bret.status); 1638 1639 break; 1640 default: 1641 BUG(); 1642 } 1643 1644 if (likely(!blk_should_fake_timeout(req->q))) 1645 blk_mq_complete_request(req); 1646 } 1647 1648 rinfo->ring.rsp_cons = i; 1649 1650 if (i != rinfo->ring.req_prod_pvt) { 1651 int more_to_do; 1652 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do); 1653 if (more_to_do) 1654 goto again; 1655 } else 1656 rinfo->ring.sring->rsp_event = i + 1; 1657 1658 kick_pending_request_queues_locked(rinfo); 1659 1660 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 1661 1662 xen_irq_lateeoi(irq, eoiflag); 1663 1664 return IRQ_HANDLED; 1665 1666 err: 1667 info->connected = BLKIF_STATE_ERROR; 1668 1669 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 1670 1671 /* No EOI in order to avoid further interrupts. */ 1672 1673 pr_alert("%s disabled for further use\n", info->gd->disk_name); 1674 return IRQ_HANDLED; 1675 } 1676 1677 1678 static int setup_blkring(struct xenbus_device *dev, 1679 struct blkfront_ring_info *rinfo) 1680 { 1681 struct blkif_sring *sring; 1682 int err, i; 1683 struct blkfront_info *info = rinfo->dev_info; 1684 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE; 1685 grant_ref_t gref[XENBUS_MAX_RING_GRANTS]; 1686 1687 for (i = 0; i < info->nr_ring_pages; i++) 1688 rinfo->ring_ref[i] = INVALID_GRANT_REF; 1689 1690 sring = alloc_pages_exact(ring_size, GFP_NOIO); 1691 if (!sring) { 1692 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); 1693 return -ENOMEM; 1694 } 1695 SHARED_RING_INIT(sring); 1696 FRONT_RING_INIT(&rinfo->ring, sring, ring_size); 1697 1698 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref); 1699 if (err < 0) { 1700 free_pages_exact(sring, ring_size); 1701 rinfo->ring.sring = NULL; 1702 goto fail; 1703 } 1704 for (i = 0; i < info->nr_ring_pages; i++) 1705 rinfo->ring_ref[i] = gref[i]; 1706 1707 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn); 1708 if (err) 1709 goto fail; 1710 1711 err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt, 1712 0, "blkif", rinfo); 1713 if (err <= 0) { 1714 xenbus_dev_fatal(dev, err, 1715 "bind_evtchn_to_irqhandler failed"); 1716 goto fail; 1717 } 1718 rinfo->irq = err; 1719 1720 return 0; 1721 fail: 1722 blkif_free(info, 0); 1723 return err; 1724 } 1725 1726 /* 1727 * Write out per-ring/queue nodes including ring-ref and event-channel, and each 1728 * ring buffer may have multi pages depending on ->nr_ring_pages. 1729 */ 1730 static int write_per_ring_nodes(struct xenbus_transaction xbt, 1731 struct blkfront_ring_info *rinfo, const char *dir) 1732 { 1733 int err; 1734 unsigned int i; 1735 const char *message = NULL; 1736 struct blkfront_info *info = rinfo->dev_info; 1737 1738 if (info->nr_ring_pages == 1) { 1739 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]); 1740 if (err) { 1741 message = "writing ring-ref"; 1742 goto abort_transaction; 1743 } 1744 } else { 1745 for (i = 0; i < info->nr_ring_pages; i++) { 1746 char ring_ref_name[RINGREF_NAME_LEN]; 1747 1748 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); 1749 err = xenbus_printf(xbt, dir, ring_ref_name, 1750 "%u", rinfo->ring_ref[i]); 1751 if (err) { 1752 message = "writing ring-ref"; 1753 goto abort_transaction; 1754 } 1755 } 1756 } 1757 1758 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn); 1759 if (err) { 1760 message = "writing event-channel"; 1761 goto abort_transaction; 1762 } 1763 1764 return 0; 1765 1766 abort_transaction: 1767 xenbus_transaction_end(xbt, 1); 1768 if (message) 1769 xenbus_dev_fatal(info->xbdev, err, "%s", message); 1770 1771 return err; 1772 } 1773 1774 /* Common code used when first setting up, and when resuming. */ 1775 static int talk_to_blkback(struct xenbus_device *dev, 1776 struct blkfront_info *info) 1777 { 1778 const char *message = NULL; 1779 struct xenbus_transaction xbt; 1780 int err; 1781 unsigned int i, max_page_order; 1782 unsigned int ring_page_order; 1783 struct blkfront_ring_info *rinfo; 1784 1785 if (!info) 1786 return -ENODEV; 1787 1788 max_page_order = xenbus_read_unsigned(info->xbdev->otherend, 1789 "max-ring-page-order", 0); 1790 ring_page_order = min(xen_blkif_max_ring_order, max_page_order); 1791 info->nr_ring_pages = 1 << ring_page_order; 1792 1793 err = negotiate_mq(info); 1794 if (err) 1795 goto destroy_blkring; 1796 1797 for_each_rinfo(info, rinfo, i) { 1798 /* Create shared ring, alloc event channel. */ 1799 err = setup_blkring(dev, rinfo); 1800 if (err) 1801 goto destroy_blkring; 1802 } 1803 1804 again: 1805 err = xenbus_transaction_start(&xbt); 1806 if (err) { 1807 xenbus_dev_fatal(dev, err, "starting transaction"); 1808 goto destroy_blkring; 1809 } 1810 1811 if (info->nr_ring_pages > 1) { 1812 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u", 1813 ring_page_order); 1814 if (err) { 1815 message = "writing ring-page-order"; 1816 goto abort_transaction; 1817 } 1818 } 1819 1820 /* We already got the number of queues/rings in _probe */ 1821 if (info->nr_rings == 1) { 1822 err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename); 1823 if (err) 1824 goto destroy_blkring; 1825 } else { 1826 char *path; 1827 size_t pathsize; 1828 1829 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u", 1830 info->nr_rings); 1831 if (err) { 1832 message = "writing multi-queue-num-queues"; 1833 goto abort_transaction; 1834 } 1835 1836 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN; 1837 path = kmalloc(pathsize, GFP_KERNEL); 1838 if (!path) { 1839 err = -ENOMEM; 1840 message = "ENOMEM while writing ring references"; 1841 goto abort_transaction; 1842 } 1843 1844 for_each_rinfo(info, rinfo, i) { 1845 memset(path, 0, pathsize); 1846 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i); 1847 err = write_per_ring_nodes(xbt, rinfo, path); 1848 if (err) { 1849 kfree(path); 1850 goto destroy_blkring; 1851 } 1852 } 1853 kfree(path); 1854 } 1855 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", 1856 XEN_IO_PROTO_ABI_NATIVE); 1857 if (err) { 1858 message = "writing protocol"; 1859 goto abort_transaction; 1860 } 1861 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1862 info->feature_persistent); 1863 if (err) 1864 dev_warn(&dev->dev, 1865 "writing persistent grants feature to xenbus"); 1866 1867 err = xenbus_transaction_end(xbt, 0); 1868 if (err) { 1869 if (err == -EAGAIN) 1870 goto again; 1871 xenbus_dev_fatal(dev, err, "completing transaction"); 1872 goto destroy_blkring; 1873 } 1874 1875 for_each_rinfo(info, rinfo, i) { 1876 unsigned int j; 1877 1878 for (j = 0; j < BLK_RING_SIZE(info); j++) 1879 rinfo->shadow[j].req.u.rw.id = j + 1; 1880 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff; 1881 } 1882 xenbus_switch_state(dev, XenbusStateInitialised); 1883 1884 return 0; 1885 1886 abort_transaction: 1887 xenbus_transaction_end(xbt, 1); 1888 if (message) 1889 xenbus_dev_fatal(dev, err, "%s", message); 1890 destroy_blkring: 1891 blkif_free(info, 0); 1892 return err; 1893 } 1894 1895 static int negotiate_mq(struct blkfront_info *info) 1896 { 1897 unsigned int backend_max_queues; 1898 unsigned int i; 1899 struct blkfront_ring_info *rinfo; 1900 1901 BUG_ON(info->nr_rings); 1902 1903 /* Check if backend supports multiple queues. */ 1904 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend, 1905 "multi-queue-max-queues", 1); 1906 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues); 1907 /* We need at least one ring. */ 1908 if (!info->nr_rings) 1909 info->nr_rings = 1; 1910 1911 info->rinfo_size = struct_size(info->rinfo, shadow, 1912 BLK_RING_SIZE(info)); 1913 info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL); 1914 if (!info->rinfo) { 1915 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure"); 1916 info->nr_rings = 0; 1917 return -ENOMEM; 1918 } 1919 1920 for_each_rinfo(info, rinfo, i) { 1921 INIT_LIST_HEAD(&rinfo->indirect_pages); 1922 INIT_LIST_HEAD(&rinfo->grants); 1923 rinfo->dev_info = info; 1924 INIT_WORK(&rinfo->work, blkif_restart_queue); 1925 spin_lock_init(&rinfo->ring_lock); 1926 } 1927 return 0; 1928 } 1929 1930 /* Enable the persistent grants feature. */ 1931 static bool feature_persistent = true; 1932 module_param(feature_persistent, bool, 0644); 1933 MODULE_PARM_DESC(feature_persistent, 1934 "Enables the persistent grants feature"); 1935 1936 /* 1937 * Entry point to this code when a new device is created. Allocate the basic 1938 * structures and the ring buffer for communication with the backend, and 1939 * inform the backend of the appropriate details for those. Switch to 1940 * Initialised state. 1941 */ 1942 static int blkfront_probe(struct xenbus_device *dev, 1943 const struct xenbus_device_id *id) 1944 { 1945 int err, vdevice; 1946 struct blkfront_info *info; 1947 1948 /* FIXME: Use dynamic device id if this is not set. */ 1949 err = xenbus_scanf(XBT_NIL, dev->nodename, 1950 "virtual-device", "%i", &vdevice); 1951 if (err != 1) { 1952 /* go looking in the extended area instead */ 1953 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext", 1954 "%i", &vdevice); 1955 if (err != 1) { 1956 xenbus_dev_fatal(dev, err, "reading virtual-device"); 1957 return err; 1958 } 1959 } 1960 1961 if (xen_hvm_domain()) { 1962 char *type; 1963 int len; 1964 /* no unplug has been done: do not hook devices != xen vbds */ 1965 if (xen_has_pv_and_legacy_disk_devices()) { 1966 int major; 1967 1968 if (!VDEV_IS_EXTENDED(vdevice)) 1969 major = BLKIF_MAJOR(vdevice); 1970 else 1971 major = XENVBD_MAJOR; 1972 1973 if (major != XENVBD_MAJOR) { 1974 printk(KERN_INFO 1975 "%s: HVM does not support vbd %d as xen block device\n", 1976 __func__, vdevice); 1977 return -ENODEV; 1978 } 1979 } 1980 /* do not create a PV cdrom device if we are an HVM guest */ 1981 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len); 1982 if (IS_ERR(type)) 1983 return -ENODEV; 1984 if (strncmp(type, "cdrom", 5) == 0) { 1985 kfree(type); 1986 return -ENODEV; 1987 } 1988 kfree(type); 1989 } 1990 info = kzalloc(sizeof(*info), GFP_KERNEL); 1991 if (!info) { 1992 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); 1993 return -ENOMEM; 1994 } 1995 1996 info->xbdev = dev; 1997 1998 mutex_init(&info->mutex); 1999 info->vdevice = vdevice; 2000 info->connected = BLKIF_STATE_DISCONNECTED; 2001 2002 info->feature_persistent = feature_persistent; 2003 2004 /* Front end dir is a number, which is used as the id. */ 2005 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); 2006 dev_set_drvdata(&dev->dev, info); 2007 2008 mutex_lock(&blkfront_mutex); 2009 list_add(&info->info_list, &info_list); 2010 mutex_unlock(&blkfront_mutex); 2011 2012 return 0; 2013 } 2014 2015 static int blkif_recover(struct blkfront_info *info) 2016 { 2017 unsigned int r_index; 2018 struct request *req, *n; 2019 int rc; 2020 struct bio *bio; 2021 unsigned int segs; 2022 struct blkfront_ring_info *rinfo; 2023 2024 blkfront_gather_backend_features(info); 2025 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */ 2026 blkif_set_queue_limits(info); 2027 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST; 2028 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG); 2029 2030 for_each_rinfo(info, rinfo, r_index) { 2031 rc = blkfront_setup_indirect(rinfo); 2032 if (rc) 2033 return rc; 2034 } 2035 xenbus_switch_state(info->xbdev, XenbusStateConnected); 2036 2037 /* Now safe for us to use the shared ring */ 2038 info->connected = BLKIF_STATE_CONNECTED; 2039 2040 for_each_rinfo(info, rinfo, r_index) { 2041 /* Kick any other new requests queued since we resumed */ 2042 kick_pending_request_queues(rinfo); 2043 } 2044 2045 list_for_each_entry_safe(req, n, &info->requests, queuelist) { 2046 /* Requeue pending requests (flush or discard) */ 2047 list_del_init(&req->queuelist); 2048 BUG_ON(req->nr_phys_segments > segs); 2049 blk_mq_requeue_request(req, false); 2050 } 2051 blk_mq_start_stopped_hw_queues(info->rq, true); 2052 blk_mq_kick_requeue_list(info->rq); 2053 2054 while ((bio = bio_list_pop(&info->bio_list)) != NULL) { 2055 /* Traverse the list of pending bios and re-queue them */ 2056 submit_bio(bio); 2057 } 2058 2059 return 0; 2060 } 2061 2062 /* 2063 * We are reconnecting to the backend, due to a suspend/resume, or a backend 2064 * driver restart. We tear down our blkif structure and recreate it, but 2065 * leave the device-layer structures intact so that this is transparent to the 2066 * rest of the kernel. 2067 */ 2068 static int blkfront_resume(struct xenbus_device *dev) 2069 { 2070 struct blkfront_info *info = dev_get_drvdata(&dev->dev); 2071 int err = 0; 2072 unsigned int i, j; 2073 struct blkfront_ring_info *rinfo; 2074 2075 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); 2076 2077 bio_list_init(&info->bio_list); 2078 INIT_LIST_HEAD(&info->requests); 2079 for_each_rinfo(info, rinfo, i) { 2080 struct bio_list merge_bio; 2081 struct blk_shadow *shadow = rinfo->shadow; 2082 2083 for (j = 0; j < BLK_RING_SIZE(info); j++) { 2084 /* Not in use? */ 2085 if (!shadow[j].request) 2086 continue; 2087 2088 /* 2089 * Get the bios in the request so we can re-queue them. 2090 */ 2091 if (req_op(shadow[j].request) == REQ_OP_FLUSH || 2092 req_op(shadow[j].request) == REQ_OP_DISCARD || 2093 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE || 2094 shadow[j].request->cmd_flags & REQ_FUA) { 2095 /* 2096 * Flush operations don't contain bios, so 2097 * we need to requeue the whole request 2098 * 2099 * XXX: but this doesn't make any sense for a 2100 * write with the FUA flag set.. 2101 */ 2102 list_add(&shadow[j].request->queuelist, &info->requests); 2103 continue; 2104 } 2105 merge_bio.head = shadow[j].request->bio; 2106 merge_bio.tail = shadow[j].request->biotail; 2107 bio_list_merge(&info->bio_list, &merge_bio); 2108 shadow[j].request->bio = NULL; 2109 blk_mq_end_request(shadow[j].request, BLK_STS_OK); 2110 } 2111 } 2112 2113 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); 2114 2115 err = talk_to_blkback(dev, info); 2116 if (!err) 2117 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings); 2118 2119 /* 2120 * We have to wait for the backend to switch to 2121 * connected state, since we want to read which 2122 * features it supports. 2123 */ 2124 2125 return err; 2126 } 2127 2128 static void blkfront_closing(struct blkfront_info *info) 2129 { 2130 struct xenbus_device *xbdev = info->xbdev; 2131 struct blkfront_ring_info *rinfo; 2132 unsigned int i; 2133 2134 if (xbdev->state == XenbusStateClosing) 2135 return; 2136 2137 /* No more blkif_request(). */ 2138 blk_mq_stop_hw_queues(info->rq); 2139 blk_mark_disk_dead(info->gd); 2140 set_capacity(info->gd, 0); 2141 2142 for_each_rinfo(info, rinfo, i) { 2143 /* No more gnttab callback work. */ 2144 gnttab_cancel_free_callback(&rinfo->callback); 2145 2146 /* Flush gnttab callback work. Must be done with no locks held. */ 2147 flush_work(&rinfo->work); 2148 } 2149 2150 xenbus_frontend_closed(xbdev); 2151 } 2152 2153 static void blkfront_setup_discard(struct blkfront_info *info) 2154 { 2155 info->feature_discard = 1; 2156 info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend, 2157 "discard-granularity", 2158 0); 2159 info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend, 2160 "discard-alignment", 0); 2161 info->feature_secdiscard = 2162 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure", 2163 0); 2164 } 2165 2166 static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo) 2167 { 2168 unsigned int psegs, grants, memflags; 2169 int err, i; 2170 struct blkfront_info *info = rinfo->dev_info; 2171 2172 memflags = memalloc_noio_save(); 2173 2174 if (info->max_indirect_segments == 0) { 2175 if (!HAS_EXTRA_REQ) 2176 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST; 2177 else { 2178 /* 2179 * When an extra req is required, the maximum 2180 * grants supported is related to the size of the 2181 * Linux block segment. 2182 */ 2183 grants = GRANTS_PER_PSEG; 2184 } 2185 } 2186 else 2187 grants = info->max_indirect_segments; 2188 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG); 2189 2190 err = fill_grant_buffer(rinfo, 2191 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info)); 2192 if (err) 2193 goto out_of_memory; 2194 2195 if (!info->feature_persistent && info->max_indirect_segments) { 2196 /* 2197 * We are using indirect descriptors but not persistent 2198 * grants, we need to allocate a set of pages that can be 2199 * used for mapping indirect grefs 2200 */ 2201 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info); 2202 2203 BUG_ON(!list_empty(&rinfo->indirect_pages)); 2204 for (i = 0; i < num; i++) { 2205 struct page *indirect_page = alloc_page(GFP_KERNEL); 2206 if (!indirect_page) 2207 goto out_of_memory; 2208 list_add(&indirect_page->lru, &rinfo->indirect_pages); 2209 } 2210 } 2211 2212 for (i = 0; i < BLK_RING_SIZE(info); i++) { 2213 rinfo->shadow[i].grants_used = 2214 kvcalloc(grants, 2215 sizeof(rinfo->shadow[i].grants_used[0]), 2216 GFP_KERNEL); 2217 rinfo->shadow[i].sg = kvcalloc(psegs, 2218 sizeof(rinfo->shadow[i].sg[0]), 2219 GFP_KERNEL); 2220 if (info->max_indirect_segments) 2221 rinfo->shadow[i].indirect_grants = 2222 kvcalloc(INDIRECT_GREFS(grants), 2223 sizeof(rinfo->shadow[i].indirect_grants[0]), 2224 GFP_KERNEL); 2225 if ((rinfo->shadow[i].grants_used == NULL) || 2226 (rinfo->shadow[i].sg == NULL) || 2227 (info->max_indirect_segments && 2228 (rinfo->shadow[i].indirect_grants == NULL))) 2229 goto out_of_memory; 2230 sg_init_table(rinfo->shadow[i].sg, psegs); 2231 } 2232 2233 memalloc_noio_restore(memflags); 2234 2235 return 0; 2236 2237 out_of_memory: 2238 for (i = 0; i < BLK_RING_SIZE(info); i++) { 2239 kvfree(rinfo->shadow[i].grants_used); 2240 rinfo->shadow[i].grants_used = NULL; 2241 kvfree(rinfo->shadow[i].sg); 2242 rinfo->shadow[i].sg = NULL; 2243 kvfree(rinfo->shadow[i].indirect_grants); 2244 rinfo->shadow[i].indirect_grants = NULL; 2245 } 2246 if (!list_empty(&rinfo->indirect_pages)) { 2247 struct page *indirect_page, *n; 2248 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { 2249 list_del(&indirect_page->lru); 2250 __free_page(indirect_page); 2251 } 2252 } 2253 2254 memalloc_noio_restore(memflags); 2255 2256 return -ENOMEM; 2257 } 2258 2259 /* 2260 * Gather all backend feature-* 2261 */ 2262 static void blkfront_gather_backend_features(struct blkfront_info *info) 2263 { 2264 unsigned int indirect_segments; 2265 2266 info->feature_flush = 0; 2267 info->feature_fua = 0; 2268 2269 /* 2270 * If there's no "feature-barrier" defined, then it means 2271 * we're dealing with a very old backend which writes 2272 * synchronously; nothing to do. 2273 * 2274 * If there are barriers, then we use flush. 2275 */ 2276 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) { 2277 info->feature_flush = 1; 2278 info->feature_fua = 1; 2279 } 2280 2281 /* 2282 * And if there is "feature-flush-cache" use that above 2283 * barriers. 2284 */ 2285 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache", 2286 0)) { 2287 info->feature_flush = 1; 2288 info->feature_fua = 0; 2289 } 2290 2291 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0)) 2292 blkfront_setup_discard(info); 2293 2294 if (info->feature_persistent) 2295 info->feature_persistent = 2296 !!xenbus_read_unsigned(info->xbdev->otherend, 2297 "feature-persistent", 0); 2298 2299 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend, 2300 "feature-max-indirect-segments", 0); 2301 if (indirect_segments > xen_blkif_max_segments) 2302 indirect_segments = xen_blkif_max_segments; 2303 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST) 2304 indirect_segments = 0; 2305 info->max_indirect_segments = indirect_segments; 2306 2307 if (info->feature_persistent) { 2308 mutex_lock(&blkfront_mutex); 2309 schedule_delayed_work(&blkfront_work, HZ * 10); 2310 mutex_unlock(&blkfront_mutex); 2311 } 2312 } 2313 2314 /* 2315 * Invoked when the backend is finally 'ready' (and has told produced 2316 * the details about the physical device - #sectors, size, etc). 2317 */ 2318 static void blkfront_connect(struct blkfront_info *info) 2319 { 2320 unsigned long long sectors; 2321 unsigned long sector_size; 2322 unsigned int physical_sector_size; 2323 int err, i; 2324 struct blkfront_ring_info *rinfo; 2325 2326 switch (info->connected) { 2327 case BLKIF_STATE_CONNECTED: 2328 /* 2329 * Potentially, the back-end may be signalling 2330 * a capacity change; update the capacity. 2331 */ 2332 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, 2333 "sectors", "%Lu", §ors); 2334 if (XENBUS_EXIST_ERR(err)) 2335 return; 2336 printk(KERN_INFO "Setting capacity to %Lu\n", 2337 sectors); 2338 set_capacity_and_notify(info->gd, sectors); 2339 2340 return; 2341 case BLKIF_STATE_SUSPENDED: 2342 /* 2343 * If we are recovering from suspension, we need to wait 2344 * for the backend to announce it's features before 2345 * reconnecting, at least we need to know if the backend 2346 * supports indirect descriptors, and how many. 2347 */ 2348 blkif_recover(info); 2349 return; 2350 2351 default: 2352 break; 2353 } 2354 2355 dev_dbg(&info->xbdev->dev, "%s:%s.\n", 2356 __func__, info->xbdev->otherend); 2357 2358 err = xenbus_gather(XBT_NIL, info->xbdev->otherend, 2359 "sectors", "%llu", §ors, 2360 "info", "%u", &info->vdisk_info, 2361 "sector-size", "%lu", §or_size, 2362 NULL); 2363 if (err) { 2364 xenbus_dev_fatal(info->xbdev, err, 2365 "reading backend fields at %s", 2366 info->xbdev->otherend); 2367 return; 2368 } 2369 2370 /* 2371 * physical-sector-size is a newer field, so old backends may not 2372 * provide this. Assume physical sector size to be the same as 2373 * sector_size in that case. 2374 */ 2375 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend, 2376 "physical-sector-size", 2377 sector_size); 2378 blkfront_gather_backend_features(info); 2379 for_each_rinfo(info, rinfo, i) { 2380 err = blkfront_setup_indirect(rinfo); 2381 if (err) { 2382 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s", 2383 info->xbdev->otherend); 2384 blkif_free(info, 0); 2385 break; 2386 } 2387 } 2388 2389 err = xlvbd_alloc_gendisk(sectors, info, sector_size, 2390 physical_sector_size); 2391 if (err) { 2392 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", 2393 info->xbdev->otherend); 2394 goto fail; 2395 } 2396 2397 xenbus_switch_state(info->xbdev, XenbusStateConnected); 2398 2399 /* Kick pending requests. */ 2400 info->connected = BLKIF_STATE_CONNECTED; 2401 for_each_rinfo(info, rinfo, i) 2402 kick_pending_request_queues(rinfo); 2403 2404 err = device_add_disk(&info->xbdev->dev, info->gd, NULL); 2405 if (err) { 2406 blk_cleanup_disk(info->gd); 2407 blk_mq_free_tag_set(&info->tag_set); 2408 info->rq = NULL; 2409 goto fail; 2410 } 2411 2412 info->is_ready = 1; 2413 return; 2414 2415 fail: 2416 blkif_free(info, 0); 2417 return; 2418 } 2419 2420 /* 2421 * Callback received when the backend's state changes. 2422 */ 2423 static void blkback_changed(struct xenbus_device *dev, 2424 enum xenbus_state backend_state) 2425 { 2426 struct blkfront_info *info = dev_get_drvdata(&dev->dev); 2427 2428 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); 2429 2430 switch (backend_state) { 2431 case XenbusStateInitWait: 2432 if (dev->state != XenbusStateInitialising) 2433 break; 2434 if (talk_to_blkback(dev, info)) 2435 break; 2436 break; 2437 case XenbusStateInitialising: 2438 case XenbusStateInitialised: 2439 case XenbusStateReconfiguring: 2440 case XenbusStateReconfigured: 2441 case XenbusStateUnknown: 2442 break; 2443 2444 case XenbusStateConnected: 2445 /* 2446 * talk_to_blkback sets state to XenbusStateInitialised 2447 * and blkfront_connect sets it to XenbusStateConnected 2448 * (if connection went OK). 2449 * 2450 * If the backend (or toolstack) decides to poke at backend 2451 * state (and re-trigger the watch by setting the state repeatedly 2452 * to XenbusStateConnected (4)) we need to deal with this. 2453 * This is allowed as this is used to communicate to the guest 2454 * that the size of disk has changed! 2455 */ 2456 if ((dev->state != XenbusStateInitialised) && 2457 (dev->state != XenbusStateConnected)) { 2458 if (talk_to_blkback(dev, info)) 2459 break; 2460 } 2461 2462 blkfront_connect(info); 2463 break; 2464 2465 case XenbusStateClosed: 2466 if (dev->state == XenbusStateClosed) 2467 break; 2468 fallthrough; 2469 case XenbusStateClosing: 2470 blkfront_closing(info); 2471 break; 2472 } 2473 } 2474 2475 static int blkfront_remove(struct xenbus_device *xbdev) 2476 { 2477 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); 2478 2479 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); 2480 2481 del_gendisk(info->gd); 2482 2483 mutex_lock(&blkfront_mutex); 2484 list_del(&info->info_list); 2485 mutex_unlock(&blkfront_mutex); 2486 2487 blkif_free(info, 0); 2488 xlbd_release_minors(info->gd->first_minor, info->gd->minors); 2489 blk_cleanup_disk(info->gd); 2490 blk_mq_free_tag_set(&info->tag_set); 2491 2492 kfree(info); 2493 return 0; 2494 } 2495 2496 static int blkfront_is_ready(struct xenbus_device *dev) 2497 { 2498 struct blkfront_info *info = dev_get_drvdata(&dev->dev); 2499 2500 return info->is_ready && info->xbdev; 2501 } 2502 2503 static const struct block_device_operations xlvbd_block_fops = 2504 { 2505 .owner = THIS_MODULE, 2506 .getgeo = blkif_getgeo, 2507 .ioctl = blkif_ioctl, 2508 .compat_ioctl = blkdev_compat_ptr_ioctl, 2509 }; 2510 2511 2512 static const struct xenbus_device_id blkfront_ids[] = { 2513 { "vbd" }, 2514 { "" } 2515 }; 2516 2517 static struct xenbus_driver blkfront_driver = { 2518 .ids = blkfront_ids, 2519 .probe = blkfront_probe, 2520 .remove = blkfront_remove, 2521 .resume = blkfront_resume, 2522 .otherend_changed = blkback_changed, 2523 .is_ready = blkfront_is_ready, 2524 }; 2525 2526 static void purge_persistent_grants(struct blkfront_info *info) 2527 { 2528 unsigned int i; 2529 unsigned long flags; 2530 struct blkfront_ring_info *rinfo; 2531 2532 for_each_rinfo(info, rinfo, i) { 2533 struct grant *gnt_list_entry, *tmp; 2534 LIST_HEAD(grants); 2535 2536 spin_lock_irqsave(&rinfo->ring_lock, flags); 2537 2538 if (rinfo->persistent_gnts_c == 0) { 2539 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 2540 continue; 2541 } 2542 2543 list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants, 2544 node) { 2545 if (gnt_list_entry->gref == INVALID_GRANT_REF || 2546 !gnttab_try_end_foreign_access(gnt_list_entry->gref)) 2547 continue; 2548 2549 list_del(&gnt_list_entry->node); 2550 rinfo->persistent_gnts_c--; 2551 gnt_list_entry->gref = INVALID_GRANT_REF; 2552 list_add_tail(&gnt_list_entry->node, &grants); 2553 } 2554 2555 list_splice_tail(&grants, &rinfo->grants); 2556 2557 spin_unlock_irqrestore(&rinfo->ring_lock, flags); 2558 } 2559 } 2560 2561 static void blkfront_delay_work(struct work_struct *work) 2562 { 2563 struct blkfront_info *info; 2564 bool need_schedule_work = false; 2565 2566 mutex_lock(&blkfront_mutex); 2567 2568 list_for_each_entry(info, &info_list, info_list) { 2569 if (info->feature_persistent) { 2570 need_schedule_work = true; 2571 mutex_lock(&info->mutex); 2572 purge_persistent_grants(info); 2573 mutex_unlock(&info->mutex); 2574 } 2575 } 2576 2577 if (need_schedule_work) 2578 schedule_delayed_work(&blkfront_work, HZ * 10); 2579 2580 mutex_unlock(&blkfront_mutex); 2581 } 2582 2583 static int __init xlblk_init(void) 2584 { 2585 int ret; 2586 int nr_cpus = num_online_cpus(); 2587 2588 if (!xen_domain()) 2589 return -ENODEV; 2590 2591 if (!xen_has_pv_disk_devices()) 2592 return -ENODEV; 2593 2594 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { 2595 pr_warn("xen_blk: can't get major %d with name %s\n", 2596 XENVBD_MAJOR, DEV_NAME); 2597 return -ENODEV; 2598 } 2599 2600 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST) 2601 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; 2602 2603 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) { 2604 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n", 2605 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER); 2606 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER; 2607 } 2608 2609 if (xen_blkif_max_queues > nr_cpus) { 2610 pr_info("Invalid max_queues (%d), will use default max: %d.\n", 2611 xen_blkif_max_queues, nr_cpus); 2612 xen_blkif_max_queues = nr_cpus; 2613 } 2614 2615 INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work); 2616 2617 ret = xenbus_register_frontend(&blkfront_driver); 2618 if (ret) { 2619 unregister_blkdev(XENVBD_MAJOR, DEV_NAME); 2620 return ret; 2621 } 2622 2623 return 0; 2624 } 2625 module_init(xlblk_init); 2626 2627 2628 static void __exit xlblk_exit(void) 2629 { 2630 cancel_delayed_work_sync(&blkfront_work); 2631 2632 xenbus_unregister_driver(&blkfront_driver); 2633 unregister_blkdev(XENVBD_MAJOR, DEV_NAME); 2634 kfree(minors); 2635 } 2636 module_exit(xlblk_exit); 2637 2638 MODULE_DESCRIPTION("Xen virtual block device frontend"); 2639 MODULE_LICENSE("GPL"); 2640 MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); 2641 MODULE_ALIAS("xen:vbd"); 2642 MODULE_ALIAS("xenblk"); 2643