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