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