Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 1 | /* Xenbus code for blkif backend |
| 2 | Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au> |
| 3 | Copyright (C) 2005 XenSource Ltd |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/kthread.h> |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 20 | #include <xen/events.h> |
| 21 | #include <xen/grant_table.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 22 | #include "common.h" |
| 23 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 24 | struct backend_info { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 25 | struct xenbus_device *dev; |
Konrad Rzeszutek Wilk | 5185432 | 2011-05-12 18:02:28 -0400 | [diff] [blame] | 26 | struct xen_blkif *blkif; |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 27 | struct xenbus_watch backend_watch; |
| 28 | unsigned major; |
| 29 | unsigned minor; |
| 30 | char *mode; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 31 | }; |
| 32 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 33 | static struct kmem_cache *xen_blkif_cachep; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 34 | static void connect(struct backend_info *); |
| 35 | static int connect_ring(struct backend_info *); |
| 36 | static void backend_changed(struct xenbus_watch *, const char **, |
| 37 | unsigned int); |
| 38 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 39 | struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) |
Jeremy Fitzhardinge | 98e036a | 2010-03-18 15:35:05 -0700 | [diff] [blame] | 40 | { |
| 41 | return be->dev; |
| 42 | } |
| 43 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 44 | static int blkback_name(struct xen_blkif *blkif, char *buf) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 45 | { |
| 46 | char *devpath, *devname; |
| 47 | struct xenbus_device *dev = blkif->be->dev; |
| 48 | |
| 49 | devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); |
| 50 | if (IS_ERR(devpath)) |
| 51 | return PTR_ERR(devpath); |
| 52 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 53 | devname = strstr(devpath, "/dev/"); |
| 54 | if (devname != NULL) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 55 | devname += strlen("/dev/"); |
| 56 | else |
| 57 | devname = devpath; |
| 58 | |
| 59 | snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname); |
| 60 | kfree(devpath); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 65 | static void xen_update_blkif_status(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 66 | { |
| 67 | int err; |
| 68 | char name[TASK_COMM_LEN]; |
| 69 | |
| 70 | /* Not ready to connect? */ |
| 71 | if (!blkif->irq || !blkif->vbd.bdev) |
| 72 | return; |
| 73 | |
| 74 | /* Already connected? */ |
| 75 | if (blkif->be->dev->state == XenbusStateConnected) |
| 76 | return; |
| 77 | |
| 78 | /* Attempt to connect: exit if we fail to. */ |
| 79 | connect(blkif->be); |
| 80 | if (blkif->be->dev->state != XenbusStateConnected) |
| 81 | return; |
| 82 | |
| 83 | err = blkback_name(blkif, name); |
| 84 | if (err) { |
| 85 | xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); |
| 86 | return; |
| 87 | } |
| 88 | |
Chris Lalancette | cbf4629 | 2010-07-21 12:41:45 -0700 | [diff] [blame] | 89 | err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); |
| 90 | if (err) { |
| 91 | xenbus_dev_error(blkif->be->dev, err, "block flush"); |
| 92 | return; |
| 93 | } |
| 94 | invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); |
| 95 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 96 | blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 97 | if (IS_ERR(blkif->xenblkd)) { |
| 98 | err = PTR_ERR(blkif->xenblkd); |
| 99 | blkif->xenblkd = NULL; |
| 100 | xenbus_dev_error(blkif->be->dev, err, "start xenblkd"); |
Roger Pau Monne | 3f3aad5 | 2013-04-17 20:18:57 +0200 | [diff] [blame] | 101 | return; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 105 | static struct xen_blkif *xen_blkif_alloc(domid_t domid) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 106 | { |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 107 | struct xen_blkif *blkif; |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame^] | 108 | int i; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 109 | |
Wei Yongjun | 654dbef | 2012-08-27 12:28:57 +0800 | [diff] [blame] | 110 | blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 111 | if (!blkif) |
| 112 | return ERR_PTR(-ENOMEM); |
| 113 | |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 114 | blkif->domid = domid; |
| 115 | spin_lock_init(&blkif->blk_ring_lock); |
| 116 | atomic_set(&blkif->refcnt, 1); |
| 117 | init_waitqueue_head(&blkif->wq); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 118 | init_completion(&blkif->drain_complete); |
| 119 | atomic_set(&blkif->drain, 0); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 120 | blkif->st_print = jiffies; |
| 121 | init_waitqueue_head(&blkif->waiting_to_free); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 122 | blkif->persistent_gnts.rb_node = NULL; |
Roger Pau Monne | c6cc142 | 2013-04-17 20:18:56 +0200 | [diff] [blame] | 123 | spin_lock_init(&blkif->free_pages_lock); |
| 124 | INIT_LIST_HEAD(&blkif->free_pages); |
| 125 | blkif->free_pages_num = 0; |
Roger Pau Monne | 3f3aad5 | 2013-04-17 20:18:57 +0200 | [diff] [blame] | 126 | atomic_set(&blkif->persistent_gnt_in_use, 0); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 127 | |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame^] | 128 | blkif->pending_reqs = kcalloc(XEN_BLKIF_REQS, |
| 129 | sizeof(blkif->pending_reqs[0]), |
| 130 | GFP_KERNEL); |
| 131 | if (!blkif->pending_reqs) { |
| 132 | kmem_cache_free(xen_blkif_cachep, blkif); |
| 133 | return ERR_PTR(-ENOMEM); |
| 134 | } |
| 135 | INIT_LIST_HEAD(&blkif->pending_free); |
| 136 | spin_lock_init(&blkif->pending_free_lock); |
| 137 | init_waitqueue_head(&blkif->pending_free_wq); |
| 138 | |
| 139 | for (i = 0; i < XEN_BLKIF_REQS; i++) |
| 140 | list_add_tail(&blkif->pending_reqs[i].free_list, |
| 141 | &blkif->pending_free); |
| 142 | |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 143 | return blkif; |
| 144 | } |
| 145 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 146 | static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 147 | unsigned int evtchn) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 148 | { |
| 149 | int err; |
| 150 | |
| 151 | /* Already connected through? */ |
| 152 | if (blkif->irq) |
| 153 | return 0; |
| 154 | |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 155 | err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring); |
| 156 | if (err < 0) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 157 | return err; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 158 | |
| 159 | switch (blkif->blk_protocol) { |
| 160 | case BLKIF_PROTOCOL_NATIVE: |
| 161 | { |
| 162 | struct blkif_sring *sring; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 163 | sring = (struct blkif_sring *)blkif->blk_ring; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 164 | BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); |
| 165 | break; |
| 166 | } |
| 167 | case BLKIF_PROTOCOL_X86_32: |
| 168 | { |
| 169 | struct blkif_x86_32_sring *sring_x86_32; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 170 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 171 | BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); |
| 172 | break; |
| 173 | } |
| 174 | case BLKIF_PROTOCOL_X86_64: |
| 175 | { |
| 176 | struct blkif_x86_64_sring *sring_x86_64; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 177 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 178 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); |
| 179 | break; |
| 180 | } |
| 181 | default: |
| 182 | BUG(); |
| 183 | } |
| 184 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 185 | err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, |
| 186 | xen_blkif_be_int, 0, |
| 187 | "blkif-backend", blkif); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 188 | if (err < 0) { |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 189 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 190 | blkif->blk_rings.common.sring = NULL; |
| 191 | return err; |
| 192 | } |
| 193 | blkif->irq = err; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 198 | static void xen_blkif_disconnect(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 199 | { |
| 200 | if (blkif->xenblkd) { |
| 201 | kthread_stop(blkif->xenblkd); |
| 202 | blkif->xenblkd = NULL; |
| 203 | } |
| 204 | |
| 205 | atomic_dec(&blkif->refcnt); |
| 206 | wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0); |
| 207 | atomic_inc(&blkif->refcnt); |
| 208 | |
| 209 | if (blkif->irq) { |
| 210 | unbind_from_irqhandler(blkif->irq, blkif); |
| 211 | blkif->irq = 0; |
| 212 | } |
| 213 | |
| 214 | if (blkif->blk_rings.common.sring) { |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 215 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 216 | blkif->blk_rings.common.sring = NULL; |
| 217 | } |
| 218 | } |
| 219 | |
Konrad Rzeszutek Wilk | 2911758 | 2012-08-13 10:53:17 -0400 | [diff] [blame] | 220 | static void xen_blkif_free(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 221 | { |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame^] | 222 | struct pending_req *req; |
| 223 | int i = 0; |
| 224 | |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 225 | if (!atomic_dec_and_test(&blkif->refcnt)) |
| 226 | BUG(); |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame^] | 227 | |
| 228 | /* Check that there is no request in use */ |
| 229 | list_for_each_entry(req, &blkif->pending_free, free_list) |
| 230 | i++; |
| 231 | BUG_ON(i != XEN_BLKIF_REQS); |
| 232 | |
| 233 | kfree(blkif->pending_reqs); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 234 | kmem_cache_free(xen_blkif_cachep, blkif); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 237 | int __init xen_blkif_interface_init(void) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 238 | { |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 239 | xen_blkif_cachep = kmem_cache_create("blkif_cache", |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 240 | sizeof(struct xen_blkif), |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 241 | 0, 0, NULL); |
| 242 | if (!xen_blkif_cachep) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 243 | return -ENOMEM; |
| 244 | |
| 245 | return 0; |
| 246 | } |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 247 | |
Konrad Rzeszutek Wilk | a1397fa | 2011-04-14 17:05:23 -0400 | [diff] [blame] | 248 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 249 | * sysfs interface for VBD I/O requests |
| 250 | */ |
| 251 | |
| 252 | #define VBD_SHOW(name, format, args...) \ |
| 253 | static ssize_t show_##name(struct device *_dev, \ |
| 254 | struct device_attribute *attr, \ |
| 255 | char *buf) \ |
| 256 | { \ |
| 257 | struct xenbus_device *dev = to_xenbus_device(_dev); \ |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 258 | struct backend_info *be = dev_get_drvdata(&dev->dev); \ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 259 | \ |
| 260 | return sprintf(buf, format, ##args); \ |
| 261 | } \ |
| 262 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 263 | |
Zoltan Kiss | 986cacb | 2013-03-11 16:15:50 +0000 | [diff] [blame] | 264 | VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req); |
| 265 | VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req); |
| 266 | VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req); |
| 267 | VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req); |
| 268 | VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req); |
| 269 | VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect); |
| 270 | VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 271 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 272 | static struct attribute *xen_vbdstat_attrs[] = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 273 | &dev_attr_oo_req.attr, |
| 274 | &dev_attr_rd_req.attr, |
| 275 | &dev_attr_wr_req.attr, |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 276 | &dev_attr_f_req.attr, |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 277 | &dev_attr_ds_req.attr, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 278 | &dev_attr_rd_sect.attr, |
| 279 | &dev_attr_wr_sect.attr, |
| 280 | NULL |
| 281 | }; |
| 282 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 283 | static struct attribute_group xen_vbdstat_group = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 284 | .name = "statistics", |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 285 | .attrs = xen_vbdstat_attrs, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); |
| 289 | VBD_SHOW(mode, "%s\n", be->mode); |
| 290 | |
Konrad Rzeszutek Wilk | 2911758 | 2012-08-13 10:53:17 -0400 | [diff] [blame] | 291 | static int xenvbd_sysfs_addif(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 292 | { |
| 293 | int error; |
| 294 | |
| 295 | error = device_create_file(&dev->dev, &dev_attr_physical_device); |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 296 | if (error) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 297 | goto fail1; |
| 298 | |
| 299 | error = device_create_file(&dev->dev, &dev_attr_mode); |
| 300 | if (error) |
| 301 | goto fail2; |
| 302 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 303 | error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 304 | if (error) |
| 305 | goto fail3; |
| 306 | |
| 307 | return 0; |
| 308 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 309 | fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 310 | fail2: device_remove_file(&dev->dev, &dev_attr_mode); |
| 311 | fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); |
| 312 | return error; |
| 313 | } |
| 314 | |
Konrad Rzeszutek Wilk | 2911758 | 2012-08-13 10:53:17 -0400 | [diff] [blame] | 315 | static void xenvbd_sysfs_delif(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 316 | { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 317 | sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 318 | device_remove_file(&dev->dev, &dev_attr_mode); |
| 319 | device_remove_file(&dev->dev, &dev_attr_physical_device); |
| 320 | } |
| 321 | |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 322 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 323 | static void xen_vbd_free(struct xen_vbd *vbd) |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 324 | { |
| 325 | if (vbd->bdev) |
| 326 | blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); |
| 327 | vbd->bdev = NULL; |
| 328 | } |
| 329 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 330 | static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, |
| 331 | unsigned major, unsigned minor, int readonly, |
| 332 | int cdrom) |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 333 | { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 334 | struct xen_vbd *vbd; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 335 | struct block_device *bdev; |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 336 | struct request_queue *q; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 337 | |
| 338 | vbd = &blkif->vbd; |
| 339 | vbd->handle = handle; |
| 340 | vbd->readonly = readonly; |
| 341 | vbd->type = 0; |
| 342 | |
| 343 | vbd->pdevice = MKDEV(major, minor); |
| 344 | |
| 345 | bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? |
| 346 | FMODE_READ : FMODE_WRITE, NULL); |
| 347 | |
| 348 | if (IS_ERR(bdev)) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 349 | DPRINTK("xen_vbd_create: device %08x could not be opened.\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 350 | vbd->pdevice); |
| 351 | return -ENOENT; |
| 352 | } |
| 353 | |
| 354 | vbd->bdev = bdev; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 355 | if (vbd->bdev->bd_disk == NULL) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 356 | DPRINTK("xen_vbd_create: device %08x doesn't exist.\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 357 | vbd->pdevice); |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 358 | xen_vbd_free(vbd); |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 359 | return -ENOENT; |
| 360 | } |
Laszlo Ersek | 6464920 | 2011-05-25 12:24:25 +0200 | [diff] [blame] | 361 | vbd->size = vbd_sz(vbd); |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 362 | |
| 363 | if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) |
| 364 | vbd->type |= VDISK_CDROM; |
| 365 | if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) |
| 366 | vbd->type |= VDISK_REMOVABLE; |
| 367 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 368 | q = bdev_get_queue(bdev); |
| 369 | if (q && q->flush_flags) |
| 370 | vbd->flush_support = true; |
| 371 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 372 | if (q && blk_queue_secdiscard(q)) |
| 373 | vbd->discard_secure = true; |
| 374 | |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 375 | DPRINTK("Successful creation of handle=%04x (dom=%u)\n", |
| 376 | handle, blkif->domid); |
| 377 | return 0; |
| 378 | } |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 379 | static int xen_blkbk_remove(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 380 | { |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 381 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 382 | |
| 383 | DPRINTK(""); |
| 384 | |
| 385 | if (be->major || be->minor) |
| 386 | xenvbd_sysfs_delif(dev); |
| 387 | |
| 388 | if (be->backend_watch.node) { |
| 389 | unregister_xenbus_watch(&be->backend_watch); |
| 390 | kfree(be->backend_watch.node); |
| 391 | be->backend_watch.node = NULL; |
| 392 | } |
| 393 | |
| 394 | if (be->blkif) { |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 395 | xen_blkif_disconnect(be->blkif); |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 396 | xen_vbd_free(&be->blkif->vbd); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 397 | xen_blkif_free(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 398 | be->blkif = NULL; |
| 399 | } |
| 400 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 401 | kfree(be->mode); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 402 | kfree(be); |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 403 | dev_set_drvdata(&dev->dev, NULL); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 407 | int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, |
| 408 | struct backend_info *be, int state) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 409 | { |
| 410 | struct xenbus_device *dev = be->dev; |
| 411 | int err; |
| 412 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 413 | err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 414 | "%d", state); |
| 415 | if (err) |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 416 | dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 417 | |
| 418 | return err; |
| 419 | } |
| 420 | |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 421 | static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 422 | { |
| 423 | struct xenbus_device *dev = be->dev; |
| 424 | struct xen_blkif *blkif = be->blkif; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 425 | int err; |
| 426 | int state = 0; |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 427 | struct block_device *bdev = be->blkif->vbd.bdev; |
| 428 | struct request_queue *q = bdev_get_queue(bdev); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 429 | |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 430 | if (blk_queue_discard(q)) { |
| 431 | err = xenbus_printf(xbt, dev->nodename, |
| 432 | "discard-granularity", "%u", |
| 433 | q->limits.discard_granularity); |
| 434 | if (err) { |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 435 | dev_warn(&dev->dev, "writing discard-granularity (%d)", err); |
| 436 | return; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 437 | } |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 438 | err = xenbus_printf(xbt, dev->nodename, |
| 439 | "discard-alignment", "%u", |
| 440 | q->limits.discard_alignment); |
| 441 | if (err) { |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 442 | dev_warn(&dev->dev, "writing discard-alignment (%d)", err); |
| 443 | return; |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 444 | } |
| 445 | state = 1; |
| 446 | /* Optional. */ |
| 447 | err = xenbus_printf(xbt, dev->nodename, |
| 448 | "discard-secure", "%d", |
| 449 | blkif->vbd.discard_secure); |
| 450 | if (err) { |
Konrad Rzeszutek Wilk | a71e23d | 2012-04-16 21:55:04 -0400 | [diff] [blame] | 451 | dev_warn(&dev->dev, "writing discard-secure (%d)", err); |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 452 | return; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 453 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 454 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 455 | err = xenbus_printf(xbt, dev->nodename, "feature-discard", |
| 456 | "%d", state); |
| 457 | if (err) |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 458 | dev_warn(&dev->dev, "writing feature-discard (%d)", err); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 459 | } |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 460 | int xen_blkbk_barrier(struct xenbus_transaction xbt, |
| 461 | struct backend_info *be, int state) |
| 462 | { |
| 463 | struct xenbus_device *dev = be->dev; |
| 464 | int err; |
| 465 | |
| 466 | err = xenbus_printf(xbt, dev->nodename, "feature-barrier", |
| 467 | "%d", state); |
| 468 | if (err) |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 469 | dev_warn(&dev->dev, "writing feature-barrier (%d)", err); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 470 | |
| 471 | return err; |
| 472 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 473 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 474 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 475 | * Entry point to this code when a new device is created. Allocate the basic |
| 476 | * structures, and watch the store waiting for the hotplug scripts to tell us |
| 477 | * the device's physical major and minor numbers. Switch to InitWait. |
| 478 | */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 479 | static int xen_blkbk_probe(struct xenbus_device *dev, |
| 480 | const struct xenbus_device_id *id) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 481 | { |
| 482 | int err; |
| 483 | struct backend_info *be = kzalloc(sizeof(struct backend_info), |
| 484 | GFP_KERNEL); |
| 485 | if (!be) { |
| 486 | xenbus_dev_fatal(dev, -ENOMEM, |
| 487 | "allocating backend structure"); |
| 488 | return -ENOMEM; |
| 489 | } |
| 490 | be->dev = dev; |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 491 | dev_set_drvdata(&dev->dev, be); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 492 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 493 | be->blkif = xen_blkif_alloc(dev->otherend_id); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 494 | if (IS_ERR(be->blkif)) { |
| 495 | err = PTR_ERR(be->blkif); |
| 496 | be->blkif = NULL; |
| 497 | xenbus_dev_fatal(dev, err, "creating block interface"); |
| 498 | goto fail; |
| 499 | } |
| 500 | |
| 501 | /* setup back pointer */ |
| 502 | be->blkif->be = be; |
| 503 | |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 504 | err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, |
| 505 | "%s/%s", dev->nodename, "physical-device"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 506 | if (err) |
| 507 | goto fail; |
| 508 | |
| 509 | err = xenbus_switch_state(dev, XenbusStateInitWait); |
| 510 | if (err) |
| 511 | goto fail; |
| 512 | |
| 513 | return 0; |
| 514 | |
| 515 | fail: |
| 516 | DPRINTK("failed"); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 517 | xen_blkbk_remove(dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 518 | return err; |
| 519 | } |
| 520 | |
| 521 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 522 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 523 | * Callback received when the hotplug scripts have placed the physical-device |
| 524 | * node. Read it and the mode node, and create a vbd. If the frontend is |
| 525 | * ready, connect. |
| 526 | */ |
| 527 | static void backend_changed(struct xenbus_watch *watch, |
| 528 | const char **vec, unsigned int len) |
| 529 | { |
| 530 | int err; |
| 531 | unsigned major; |
| 532 | unsigned minor; |
| 533 | struct backend_info *be |
| 534 | = container_of(watch, struct backend_info, backend_watch); |
| 535 | struct xenbus_device *dev = be->dev; |
| 536 | int cdrom = 0; |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 537 | unsigned long handle; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 538 | char *device_type; |
| 539 | |
| 540 | DPRINTK(""); |
| 541 | |
| 542 | err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", |
| 543 | &major, &minor); |
| 544 | if (XENBUS_EXIST_ERR(err)) { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 545 | /* |
| 546 | * Since this watch will fire once immediately after it is |
| 547 | * registered, we expect this. Ignore it, and wait for the |
| 548 | * hotplug scripts. |
| 549 | */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 550 | return; |
| 551 | } |
| 552 | if (err != 2) { |
| 553 | xenbus_dev_fatal(dev, err, "reading physical-device"); |
| 554 | return; |
| 555 | } |
| 556 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 557 | if (be->major | be->minor) { |
| 558 | if (be->major != major || be->minor != minor) |
| 559 | pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n", |
| 560 | be->major, be->minor, major, minor); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 561 | return; |
| 562 | } |
| 563 | |
| 564 | be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); |
| 565 | if (IS_ERR(be->mode)) { |
| 566 | err = PTR_ERR(be->mode); |
| 567 | be->mode = NULL; |
| 568 | xenbus_dev_fatal(dev, err, "reading mode"); |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); |
| 573 | if (!IS_ERR(device_type)) { |
| 574 | cdrom = strcmp(device_type, "cdrom") == 0; |
| 575 | kfree(device_type); |
| 576 | } |
| 577 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 578 | /* Front end dir is a number, which is used as the handle. */ |
| 579 | err = strict_strtoul(strrchr(dev->otherend, '/') + 1, 0, &handle); |
| 580 | if (err) |
| 581 | return; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 582 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 583 | be->major = major; |
| 584 | be->minor = minor; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 585 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 586 | err = xen_vbd_create(be->blkif, handle, major, minor, |
| 587 | !strchr(be->mode, 'w'), cdrom); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 588 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 589 | if (err) |
| 590 | xenbus_dev_fatal(dev, err, "creating vbd structure"); |
| 591 | else { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 592 | err = xenvbd_sysfs_addif(dev); |
| 593 | if (err) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 594 | xen_vbd_free(&be->blkif->vbd); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 595 | xenbus_dev_fatal(dev, err, "creating sysfs entries"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 596 | } |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 597 | } |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 598 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 599 | if (err) { |
| 600 | kfree(be->mode); |
| 601 | be->mode = NULL; |
| 602 | be->major = 0; |
| 603 | be->minor = 0; |
| 604 | } else { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 605 | /* We're potentially connected now */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 606 | xen_update_blkif_status(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 611 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 612 | * Callback received when the frontend's state changes. |
| 613 | */ |
| 614 | static void frontend_changed(struct xenbus_device *dev, |
| 615 | enum xenbus_state frontend_state) |
| 616 | { |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 617 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 618 | int err; |
| 619 | |
| 620 | DPRINTK("%s", xenbus_strstate(frontend_state)); |
| 621 | |
| 622 | switch (frontend_state) { |
| 623 | case XenbusStateInitialising: |
| 624 | if (dev->state == XenbusStateClosed) { |
Konrad Rzeszutek Wilk | 22b20f2 | 2011-05-12 16:43:12 -0400 | [diff] [blame] | 625 | pr_info(DRV_PFX "%s: prepare for reconnect\n", |
Konrad Rzeszutek Wilk | ebe8190 | 2011-05-12 16:42:31 -0400 | [diff] [blame] | 626 | dev->nodename); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 627 | xenbus_switch_state(dev, XenbusStateInitWait); |
| 628 | } |
| 629 | break; |
| 630 | |
| 631 | case XenbusStateInitialised: |
| 632 | case XenbusStateConnected: |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 633 | /* |
| 634 | * Ensure we connect even when two watches fire in |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 635 | * close succession and we miss the intermediate value |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 636 | * of frontend_state. |
| 637 | */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 638 | if (dev->state == XenbusStateConnected) |
| 639 | break; |
| 640 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 641 | /* |
| 642 | * Enforce precondition before potential leak point. |
Joe Jin | 1bc05b0 | 2011-08-15 12:57:07 +0800 | [diff] [blame] | 643 | * xen_blkif_disconnect() is idempotent. |
Keir Fraser | 313d7b0 | 2010-11-24 22:08:20 -0800 | [diff] [blame] | 644 | */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 645 | xen_blkif_disconnect(be->blkif); |
Keir Fraser | 313d7b0 | 2010-11-24 22:08:20 -0800 | [diff] [blame] | 646 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 647 | err = connect_ring(be); |
| 648 | if (err) |
| 649 | break; |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 650 | xen_update_blkif_status(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 651 | break; |
| 652 | |
| 653 | case XenbusStateClosing: |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 654 | xenbus_switch_state(dev, XenbusStateClosing); |
| 655 | break; |
| 656 | |
| 657 | case XenbusStateClosed: |
Joe Jin | 6f5986b | 2011-08-15 12:51:31 +0800 | [diff] [blame] | 658 | xen_blkif_disconnect(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 659 | xenbus_switch_state(dev, XenbusStateClosed); |
| 660 | if (xenbus_dev_is_online(dev)) |
| 661 | break; |
| 662 | /* fall through if not online */ |
| 663 | case XenbusStateUnknown: |
Joe Jin | 1bc05b0 | 2011-08-15 12:57:07 +0800 | [diff] [blame] | 664 | /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 665 | device_unregister(&dev->dev); |
| 666 | break; |
| 667 | |
| 668 | default: |
| 669 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", |
| 670 | frontend_state); |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | |
| 676 | /* ** Connection ** */ |
| 677 | |
| 678 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 679 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 680 | * Write the physical details regarding the block device to the store, and |
| 681 | * switch to Connected state. |
| 682 | */ |
| 683 | static void connect(struct backend_info *be) |
| 684 | { |
| 685 | struct xenbus_transaction xbt; |
| 686 | int err; |
| 687 | struct xenbus_device *dev = be->dev; |
| 688 | |
| 689 | DPRINTK("%s", dev->otherend); |
| 690 | |
| 691 | /* Supply the information about the device the frontend needs */ |
| 692 | again: |
| 693 | err = xenbus_transaction_start(&xbt); |
| 694 | if (err) { |
| 695 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 696 | return; |
| 697 | } |
| 698 | |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 699 | /* If we can't advertise it is OK. */ |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 700 | xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); |
| 701 | |
| 702 | xen_blkbk_discard(xbt, be); |
| 703 | |
| 704 | xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 705 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 706 | err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1); |
| 707 | if (err) { |
| 708 | xenbus_dev_fatal(dev, err, "writing %s/feature-persistent", |
| 709 | dev->nodename); |
| 710 | goto abort; |
| 711 | } |
| 712 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 713 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 714 | (unsigned long long)vbd_sz(&be->blkif->vbd)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 715 | if (err) { |
| 716 | xenbus_dev_fatal(dev, err, "writing %s/sectors", |
| 717 | dev->nodename); |
| 718 | goto abort; |
| 719 | } |
| 720 | |
| 721 | /* FIXME: use a typename instead */ |
| 722 | err = xenbus_printf(xbt, dev->nodename, "info", "%u", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 723 | be->blkif->vbd.type | |
| 724 | (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 725 | if (err) { |
| 726 | xenbus_dev_fatal(dev, err, "writing %s/info", |
| 727 | dev->nodename); |
| 728 | goto abort; |
| 729 | } |
| 730 | err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 731 | (unsigned long) |
| 732 | bdev_logical_block_size(be->blkif->vbd.bdev)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 733 | if (err) { |
| 734 | xenbus_dev_fatal(dev, err, "writing %s/sector-size", |
| 735 | dev->nodename); |
| 736 | goto abort; |
| 737 | } |
| 738 | |
| 739 | err = xenbus_transaction_end(xbt, 0); |
| 740 | if (err == -EAGAIN) |
| 741 | goto again; |
| 742 | if (err) |
| 743 | xenbus_dev_fatal(dev, err, "ending transaction"); |
| 744 | |
| 745 | err = xenbus_switch_state(dev, XenbusStateConnected); |
| 746 | if (err) |
Joe Perches | 08b8bfc | 2011-06-12 09:21:13 -0700 | [diff] [blame] | 747 | xenbus_dev_fatal(dev, err, "%s: switching to Connected state", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 748 | dev->nodename); |
| 749 | |
| 750 | return; |
| 751 | abort: |
| 752 | xenbus_transaction_end(xbt, 1); |
| 753 | } |
| 754 | |
| 755 | |
| 756 | static int connect_ring(struct backend_info *be) |
| 757 | { |
| 758 | struct xenbus_device *dev = be->dev; |
| 759 | unsigned long ring_ref; |
| 760 | unsigned int evtchn; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 761 | unsigned int pers_grants; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 762 | char protocol[64] = ""; |
| 763 | int err; |
| 764 | |
| 765 | DPRINTK("%s", dev->otherend); |
| 766 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 767 | err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", |
| 768 | &ring_ref, "event-channel", "%u", &evtchn, NULL); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 769 | if (err) { |
| 770 | xenbus_dev_fatal(dev, err, |
| 771 | "reading %s/ring-ref and event-channel", |
| 772 | dev->otherend); |
| 773 | return err; |
| 774 | } |
| 775 | |
| 776 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; |
| 777 | err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", |
| 778 | "%63s", protocol, NULL); |
| 779 | if (err) |
| 780 | strcpy(protocol, "unspecified, assuming native"); |
| 781 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) |
| 782 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; |
| 783 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) |
| 784 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; |
| 785 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) |
| 786 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; |
| 787 | else { |
| 788 | xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); |
| 789 | return -1; |
| 790 | } |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 791 | err = xenbus_gather(XBT_NIL, dev->otherend, |
Roger Pau Monne | cb5bd4d | 2012-11-02 16:43:04 +0100 | [diff] [blame] | 792 | "feature-persistent", "%u", |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 793 | &pers_grants, NULL); |
| 794 | if (err) |
| 795 | pers_grants = 0; |
| 796 | |
| 797 | be->blkif->vbd.feature_gnt_persistent = pers_grants; |
| 798 | be->blkif->vbd.overflow_max_grants = 0; |
| 799 | |
| 800 | pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n", |
| 801 | ring_ref, evtchn, be->blkif->blk_protocol, protocol, |
| 802 | pers_grants ? "persistent grants" : ""); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 803 | |
| 804 | /* Map the shared frame, irq etc. */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 805 | err = xen_blkif_map(be->blkif, ring_ref, evtchn); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 806 | if (err) { |
| 807 | xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u", |
| 808 | ring_ref, evtchn); |
| 809 | return err; |
| 810 | } |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | /* ** Driver Registration ** */ |
| 817 | |
| 818 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 819 | static const struct xenbus_device_id xen_blkbk_ids[] = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 820 | { "vbd" }, |
| 821 | { "" } |
| 822 | }; |
| 823 | |
| 824 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 825 | static DEFINE_XENBUS_DRIVER(xen_blkbk, , |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 826 | .probe = xen_blkbk_probe, |
| 827 | .remove = xen_blkbk_remove, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 828 | .otherend_changed = frontend_changed |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 829 | ); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 830 | |
| 831 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 832 | int xen_blkif_xenbus_init(void) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 833 | { |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 834 | return xenbus_register_backend(&xen_blkbk_driver); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 835 | } |