xen-blkfront.c
来自「linux 内核源代码」· C语言 代码 · 共 985 行 · 第 1/2 页
C
985 行
dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " "request: %x\n", bret->status); ret = end_that_request_first(req, uptodate, req->hard_nr_sectors); BUG_ON(ret); end_that_request_last(req, uptodate); break; default: BUG(); } } info->ring.rsp_cons = i; if (i != info->ring.req_prod_pvt) { int more_to_do; RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); if (more_to_do) goto again; } else info->ring.sring->rsp_event = i + 1; kick_pending_request_queues(info); spin_unlock_irqrestore(&blkif_io_lock, flags); return IRQ_HANDLED;}static int setup_blkring(struct xenbus_device *dev, struct blkfront_info *info){ struct blkif_sring *sring; int err; info->ring_ref = GRANT_INVALID_REF; sring = (struct blkif_sring *)__get_free_page(GFP_KERNEL); if (!sring) { xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); return -ENOMEM; } SHARED_RING_INIT(sring); FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE); err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring)); if (err < 0) { free_page((unsigned long)sring); info->ring.sring = NULL; goto fail; } info->ring_ref = err; err = xenbus_alloc_evtchn(dev, &info->evtchn); if (err) goto fail; err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, IRQF_SAMPLE_RANDOM, "blkif", info); if (err <= 0) { xenbus_dev_fatal(dev, err, "bind_evtchn_to_irqhandler failed"); goto fail; } info->irq = err; return 0;fail: blkif_free(info, 0); return err;}/* Common code used when first setting up, and when resuming. */static int talk_to_backend(struct xenbus_device *dev, struct blkfront_info *info){ const char *message = NULL; struct xenbus_transaction xbt; int err; /* Create shared ring, alloc event channel. */ err = setup_blkring(dev, info); if (err) goto out;again: err = xenbus_transaction_start(&xbt); if (err) { xenbus_dev_fatal(dev, err, "starting transaction"); goto destroy_blkring; } err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u", info->ring_ref); if (err) { message = "writing ring-ref"; goto abort_transaction; } err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u", info->evtchn); if (err) { message = "writing event-channel"; goto abort_transaction; } err = xenbus_transaction_end(xbt, 0); if (err) { if (err == -EAGAIN) goto again; xenbus_dev_fatal(dev, err, "completing transaction"); goto destroy_blkring; } xenbus_switch_state(dev, XenbusStateInitialised); return 0; abort_transaction: xenbus_transaction_end(xbt, 1); if (message) xenbus_dev_fatal(dev, err, "%s", message); destroy_blkring: blkif_free(info, 0); out: return err;}/** * Entry point to this code when a new device is created. Allocate the basic * structures and the ring buffer for communication with the backend, and * inform the backend of the appropriate details for those. Switch to * Initialised state. */static int blkfront_probe(struct xenbus_device *dev, const struct xenbus_device_id *id){ int err, vdevice, i; struct blkfront_info *info; /* FIXME: Use dynamic device id if this is not set. */ err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device", "%i", &vdevice); if (err != 1) { xenbus_dev_fatal(dev, err, "reading virtual-device"); return err; } info = kzalloc(sizeof(*info), GFP_KERNEL); if (!info) { xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); return -ENOMEM; } info->xbdev = dev; info->vdevice = vdevice; info->connected = BLKIF_STATE_DISCONNECTED; INIT_WORK(&info->work, blkif_restart_queue); for (i = 0; i < BLK_RING_SIZE; i++) info->shadow[i].req.id = i+1; info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; /* Front end dir is a number, which is used as the id. */ info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); dev->dev.driver_data = info; err = talk_to_backend(dev, info); if (err) { kfree(info); dev->dev.driver_data = NULL; return err; } return 0;}static int blkif_recover(struct blkfront_info *info){ int i; struct blkif_request *req; struct blk_shadow *copy; int j; /* Stage 1: Make a safe copy of the shadow state. */ copy = kmalloc(sizeof(info->shadow), GFP_KERNEL); if (!copy) return -ENOMEM; memcpy(copy, info->shadow, sizeof(info->shadow)); /* Stage 2: Set up free list. */ memset(&info->shadow, 0, sizeof(info->shadow)); for (i = 0; i < BLK_RING_SIZE; i++) info->shadow[i].req.id = i+1; info->shadow_free = info->ring.req_prod_pvt; info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; /* Stage 3: Find pending requests and requeue them. */ for (i = 0; i < BLK_RING_SIZE; i++) { /* Not in use? */ if (copy[i].request == 0) continue; /* Grab a request slot and copy shadow state into it. */ req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt); *req = copy[i].req; /* We get a new request id, and must reset the shadow state. */ req->id = get_id_from_freelist(info); memcpy(&info->shadow[req->id], ©[i], sizeof(copy[i])); /* Rewrite any grant references invalidated by susp/resume. */ for (j = 0; j < req->nr_segments; j++) gnttab_grant_foreign_access_ref( req->seg[j].gref, info->xbdev->otherend_id, pfn_to_mfn(info->shadow[req->id].frame[j]), rq_data_dir( (struct request *) info->shadow[req->id].request)); info->shadow[req->id].req = *req; info->ring.req_prod_pvt++; } kfree(copy); xenbus_switch_state(info->xbdev, XenbusStateConnected); spin_lock_irq(&blkif_io_lock); /* Now safe for us to use the shared ring */ info->connected = BLKIF_STATE_CONNECTED; /* Send off requeued requests */ flush_requests(info); /* Kick any other new requests queued since we resumed */ kick_pending_request_queues(info); spin_unlock_irq(&blkif_io_lock); return 0;}/** * We are reconnecting to the backend, due to a suspend/resume, or a backend * driver restart. We tear down our blkif structure and recreate it, but * leave the device-layer structures intact so that this is transparent to the * rest of the kernel. */static int blkfront_resume(struct xenbus_device *dev){ struct blkfront_info *info = dev->dev.driver_data; int err; dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); err = talk_to_backend(dev, info); if (info->connected == BLKIF_STATE_SUSPENDED && !err) err = blkif_recover(info); return err;}/* * Invoked when the backend is finally 'ready' (and has told produced * the details about the physical device - #sectors, size, etc). */static void blkfront_connect(struct blkfront_info *info){ unsigned long long sectors; unsigned long sector_size; unsigned int binfo; int err; if ((info->connected == BLKIF_STATE_CONNECTED) || (info->connected == BLKIF_STATE_SUSPENDED) ) return; dev_dbg(&info->xbdev->dev, "%s:%s.\n", __func__, info->xbdev->otherend); err = xenbus_gather(XBT_NIL, info->xbdev->otherend, "sectors", "%llu", §ors, "info", "%u", &binfo, "sector-size", "%lu", §or_size, NULL); if (err) { xenbus_dev_fatal(info->xbdev, err, "reading backend fields at %s", info->xbdev->otherend); return; } err = xenbus_gather(XBT_NIL, info->xbdev->otherend, "feature-barrier", "%lu", &info->feature_barrier, NULL); if (err) info->feature_barrier = 0; err = xlvbd_alloc_gendisk(BLKIF_MINOR(info->vdevice), sectors, info->vdevice, binfo, sector_size, info); if (err) { xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", info->xbdev->otherend); return; } xenbus_switch_state(info->xbdev, XenbusStateConnected); /* Kick pending requests. */ spin_lock_irq(&blkif_io_lock); info->connected = BLKIF_STATE_CONNECTED; kick_pending_request_queues(info); spin_unlock_irq(&blkif_io_lock); add_disk(info->gd);}/** * Handle the change of state of the backend to Closing. We must delete our * device-layer structures now, to ensure that writes are flushed through to * the backend. Once is this done, we can switch to Closed in * acknowledgement. */static void blkfront_closing(struct xenbus_device *dev){ struct blkfront_info *info = dev->dev.driver_data; unsigned long flags; dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename); if (info->rq == NULL) goto out; spin_lock_irqsave(&blkif_io_lock, flags); del_gendisk(info->gd); /* No more blkif_request(). */ blk_stop_queue(info->rq); /* No more gnttab callback work. */ gnttab_cancel_free_callback(&info->callback); spin_unlock_irqrestore(&blkif_io_lock, flags); /* Flush gnttab callback work. Must be done with no locks held. */ flush_scheduled_work(); blk_cleanup_queue(info->rq); info->rq = NULL; out: xenbus_frontend_closed(dev);}/** * Callback received when the backend's state changes. */static void backend_changed(struct xenbus_device *dev, enum xenbus_state backend_state){ struct blkfront_info *info = dev->dev.driver_data; struct block_device *bd; dev_dbg(&dev->dev, "blkfront:backend_changed.\n"); switch (backend_state) { case XenbusStateInitialising: case XenbusStateInitWait: case XenbusStateInitialised: case XenbusStateUnknown: case XenbusStateClosed: break; case XenbusStateConnected: blkfront_connect(info); break; case XenbusStateClosing: bd = bdget(info->dev); if (bd == NULL) xenbus_dev_fatal(dev, -ENODEV, "bdget failed"); mutex_lock(&bd->bd_mutex); if (info->users > 0) xenbus_dev_error(dev, -EBUSY, "Device in use; refusing to close"); else blkfront_closing(dev); mutex_unlock(&bd->bd_mutex); bdput(bd); break; }}static int blkfront_remove(struct xenbus_device *dev){ struct blkfront_info *info = dev->dev.driver_data; dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename); blkif_free(info, 0); kfree(info); return 0;}static int blkif_open(struct inode *inode, struct file *filep){ struct blkfront_info *info = inode->i_bdev->bd_disk->private_data; info->users++; return 0;}static int blkif_release(struct inode *inode, struct file *filep){ struct blkfront_info *info = inode->i_bdev->bd_disk->private_data; info->users--; if (info->users == 0) { /* Check whether we have been instructed to close. We will have ignored this request initially, as the device was still mounted. */ struct xenbus_device *dev = info->xbdev; enum xenbus_state state = xenbus_read_driver_state(dev->otherend); if (state == XenbusStateClosing) blkfront_closing(dev); } return 0;}static struct block_device_operations xlvbd_block_fops ={ .owner = THIS_MODULE, .open = blkif_open, .release = blkif_release,};static struct xenbus_device_id blkfront_ids[] = { { "vbd" }, { "" }};static struct xenbus_driver blkfront = { .name = "vbd", .owner = THIS_MODULE, .ids = blkfront_ids, .probe = blkfront_probe, .remove = blkfront_remove, .resume = blkfront_resume, .otherend_changed = backend_changed,};static int __init xlblk_init(void){ if (!is_running_on_xen()) return -ENODEV; if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n", XENVBD_MAJOR, DEV_NAME); return -ENODEV; } return xenbus_register_frontend(&blkfront);}module_init(xlblk_init);static void xlblk_exit(void){ return xenbus_unregister_driver(&blkfront);}module_exit(xlblk_exit);MODULE_DESCRIPTION("Xen virtual block device frontend");MODULE_LICENSE("GPL");MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?