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