Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / blkback / blkback.c
1 /******************************************************************************
2  * arch/xen/drivers/blkif/backend/main.c
3  * 
4  * Back-end of the driver for virtual block devices. This portion of the
5  * driver exports a 'unified' block-device interface that can be accessed
6  * by any operating system that implements a compatible front end. A 
7  * reference front-end implementation can be found in:
8  *  arch/xen/drivers/blkif/frontend
9  * 
10  * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
11  * Copyright (c) 2005, Christopher Clark
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/spinlock.h>
39 #include <linux/kthread.h>
40 #include <linux/freezer.h>
41 #include <linux/list.h>
42 #include <linux/module.h>
43 #include <linux/delay.h>
44 #include <xen/balloon.h>
45 #include <xen/evtchn.h>
46 #include <xen/gnttab.h>
47 #include <asm/hypervisor.h>
48 #include "common.h"
49
50 /*
51  * These are rather arbitrary. They are fairly large because adjacent requests
52  * pulled from a communication ring are quite likely to end up being part of
53  * the same scatter/gather request at the disc.
54  * 
55  * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
56  * 
57  * This will increase the chances of being able to write whole tracks.
58  * 64 should be enough to keep us competitive with Linux.
59  */
60 static int blkif_reqs = 64;
61 module_param_named(reqs, blkif_reqs, int, 0);
62 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
63
64 /* Run-time switchable: /sys/module/blkback/parameters/ */
65 static unsigned int log_stats = 0;
66 static unsigned int debug_lvl = 0;
67 module_param(log_stats, int, 0644);
68 module_param(debug_lvl, int, 0644);
69
70 /*
71  * Each outstanding request that we've passed to the lower device layers has a 
72  * 'pending_req' allocated to it. Each buffer_head that completes decrements 
73  * the pendcnt towards zero. When it hits zero, the specified domain has a 
74  * response queued for it, with the saved 'id' passed back.
75  */
76 typedef struct {
77         blkif_t       *blkif;
78         u64            id;
79         atomic_t       pendcnt;
80         unsigned short nr_pages;
81         unsigned short operation;
82         struct list_head free_list;
83 } pending_req_t;
84
85 static pending_req_t *pending_reqs;
86 static struct list_head pending_free;
87 static DEFINE_SPINLOCK(pending_free_lock);
88 static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
89
90 #define BLKBACK_INVALID_HANDLE (~0)
91
92 static struct page **pending_pages;
93 static grant_handle_t *pending_grant_handles;
94
95 static inline int vaddr_pagenr(pending_req_t *req, int seg)
96 {
97         return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
98 }
99
100 #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
101
102 static inline unsigned long vaddr(pending_req_t *req, int seg)
103 {
104         unsigned long pfn = page_to_pfn(pending_page(req, seg));
105         return (unsigned long)pfn_to_kaddr(pfn);
106 }
107
108 #define pending_handle(_req, _seg) \
109         (pending_grant_handles[vaddr_pagenr(_req, _seg)])
110
111
112 static int do_block_io_op(blkif_t *blkif);
113 static void dispatch_rw_block_io(blkif_t *blkif,
114                                  blkif_request_t *req,
115                                  pending_req_t *pending_req);
116 static void make_response(blkif_t *blkif, u64 id,
117                           unsigned short op, int st);
118
119 /******************************************************************
120  * misc small helpers
121  */
122 static pending_req_t* alloc_req(void)
123 {
124         pending_req_t *req = NULL;
125         unsigned long flags;
126
127         spin_lock_irqsave(&pending_free_lock, flags);
128         if (!list_empty(&pending_free)) {
129                 req = list_entry(pending_free.next, pending_req_t, free_list);
130                 list_del(&req->free_list);
131         }
132         spin_unlock_irqrestore(&pending_free_lock, flags);
133         return req;
134 }
135
136 static void free_req(pending_req_t *req)
137 {
138         unsigned long flags;
139         int was_empty;
140
141         spin_lock_irqsave(&pending_free_lock, flags);
142         was_empty = list_empty(&pending_free);
143         list_add(&req->free_list, &pending_free);
144         spin_unlock_irqrestore(&pending_free_lock, flags);
145         if (was_empty)
146                 wake_up(&pending_free_wq);
147 }
148
149 static void unplug_queue(blkif_t *blkif)
150 {
151         if (blkif->plug == NULL)
152                 return;
153         kobject_put(&blkif->plug->kobj);
154         blkif->plug = NULL;
155 }
156
157 static void plug_queue(blkif_t *blkif, struct block_device *bdev)
158 {
159         struct request_queue *q = bdev_get_queue(bdev);
160
161         if (q == blkif->plug)
162                 return;
163         unplug_queue(blkif);
164         WARN_ON(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags));
165         kobject_get(&q->kobj);
166         blkif->plug = q;
167 }
168
169 static void fast_flush_area(pending_req_t *req)
170 {
171         struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
172         unsigned int i, invcount = 0;
173         grant_handle_t handle;
174         int ret;
175
176         for (i = 0; i < req->nr_pages; i++) {
177                 handle = pending_handle(req, i);
178                 if (handle == BLKBACK_INVALID_HANDLE)
179                         continue;
180                 blkback_pagemap_clear(pending_page(req, i));
181                 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
182                                     GNTMAP_host_map, handle);
183                 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
184                 invcount++;
185         }
186
187         ret = HYPERVISOR_grant_table_op(
188                 GNTTABOP_unmap_grant_ref, unmap, invcount);
189         BUG_ON(ret);
190 }
191
192 /******************************************************************
193  * SCHEDULER FUNCTIONS
194  */
195
196 static void print_stats(blkif_t *blkif)
197 {
198         printk(KERN_DEBUG "%s: oo %3d  |  rd %4d  |  wr %4d  |  br %4d"
199                "  |  fl %4d  |  ds %4d  |  pk %4d\n",
200                current->comm, blkif->st_oo_req,
201                blkif->st_rd_req, blkif->st_wr_req,
202                blkif->st_br_req, blkif->st_fl_req,
203                blkif->st_ds_req, blkif->st_pk_req);
204         blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
205         blkif->st_rd_req = 0;
206         blkif->st_wr_req = 0;
207         blkif->st_oo_req = 0;
208         blkif->st_br_req = 0;
209         blkif->st_fl_req = 0;
210         blkif->st_ds_req = 0;
211         blkif->st_pk_req = 0;
212 }
213
214 int blkif_schedule(void *arg)
215 {
216         blkif_t *blkif = arg;
217         struct vbd *vbd = &blkif->vbd;
218
219         blkif_get(blkif);
220
221         if (debug_lvl)
222                 printk(KERN_DEBUG "%s: started\n", current->comm);
223
224         while (!kthread_should_stop()) {
225                 if (try_to_freeze())
226                         continue;
227                 if (unlikely(vbd->size != vbd_size(vbd)))
228                         vbd_resize(blkif);
229
230                 wait_event_interruptible(
231                         blkif->wq,
232                         blkif->waiting_reqs || kthread_should_stop());
233                 wait_event_interruptible(
234                         pending_free_wq,
235                         !list_empty(&pending_free) || kthread_should_stop());
236
237                 blkif->waiting_reqs = 0;
238                 smp_mb(); /* clear flag *before* checking for work */
239
240                 if (do_block_io_op(blkif))
241                         blkif->waiting_reqs = 1;
242                 unplug_queue(blkif);
243
244                 if (log_stats && time_after(jiffies, blkif->st_print))
245                         print_stats(blkif);
246         }
247
248         if (log_stats)
249                 print_stats(blkif);
250         if (debug_lvl)
251                 printk(KERN_DEBUG "%s: exiting\n", current->comm);
252
253         blkif->xenblkd = NULL;
254         blkif_put(blkif);
255
256         return 0;
257 }
258
259 static void drain_io(blkif_t *blkif)
260 {
261         atomic_set(&blkif->drain, 1);
262         do {
263                 /* The initial value is one, and one refcnt taken at the
264                  * start of the blkif_schedule thread. */
265                 if (atomic_read(&blkif->refcnt) <= 2)
266                         break;
267
268                 wait_for_completion_interruptible_timeout(
269                                 &blkif->drain_complete, HZ);
270
271                 if (!atomic_read(&blkif->drain))
272                         break;
273         } while (!kthread_should_stop());
274         atomic_set(&blkif->drain, 0);
275 }
276
277 /******************************************************************
278  * COMPLETION CALLBACK -- Called as bh->b_end_io()
279  */
280
281 static void __end_block_io_op(pending_req_t *pending_req, int error)
282 {
283         blkif_t *blkif = pending_req->blkif;
284         int status = BLKIF_RSP_OKAY;
285
286         /* An error fails the entire request. */
287         if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
288             (error == -EOPNOTSUPP)) {
289                 DPRINTK("blkback: write barrier op failed, not supported\n");
290                 blkback_barrier(XBT_NIL, blkif->be, 0);
291                 status = BLKIF_RSP_EOPNOTSUPP;
292         } else if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
293                    (error == -EOPNOTSUPP)) {
294                 DPRINTK("blkback: flush diskcache op failed, not supported\n");
295                 blkback_flush_diskcache(XBT_NIL, blkif->be, 0);
296                 status = BLKIF_RSP_EOPNOTSUPP;
297         } else if (error) {
298                 DPRINTK("Buffer not up-to-date at end of operation, "
299                         "error=%d\n", error);
300                 status = BLKIF_RSP_ERROR;
301         }
302
303         if (atomic_dec_and_test(&pending_req->pendcnt)) {
304                 fast_flush_area(pending_req);
305                 make_response(blkif, pending_req->id,
306                               pending_req->operation, status);
307                 free_req(pending_req);
308                 if (atomic_read(&blkif->drain)
309                     && atomic_read(&blkif->refcnt) <= 2)
310                         complete(&blkif->drain_complete);
311                 blkif_put(blkif);
312         }
313 }
314
315 static void end_block_io_op(struct bio *bio, int error)
316 {
317         __end_block_io_op(bio->bi_private, error);
318         bio_put(bio);
319 }
320
321
322 /******************************************************************************
323  * NOTIFICATION FROM GUEST OS.
324  */
325
326 static void blkif_notify_work(blkif_t *blkif)
327 {
328         blkif->waiting_reqs = 1;
329         wake_up(&blkif->wq);
330 }
331
332 irqreturn_t blkif_be_int(int irq, void *dev_id)
333 {
334         blkif_notify_work(dev_id);
335         return IRQ_HANDLED;
336 }
337
338
339
340 /******************************************************************
341  * DOWNWARD CALLS -- These interface with the block-device layer proper.
342  */
343
344 static void dispatch_discard(blkif_t *blkif, struct blkif_request_discard *req)
345 {
346         unsigned long secure = (blkif->vbd.discard_secure &&
347                                 (req->flag & BLKIF_DISCARD_SECURE)) ?
348                                BLKDEV_DISCARD_SECURE : 0;
349         struct phys_req preq;
350         int status;
351
352         blkif->st_ds_req++;
353
354         preq.dev           = req->handle;
355         preq.sector_number = req->sector_number;
356         preq.nr_sects      = req->nr_sectors;
357
358         if (vbd_translate(&preq, blkif, REQ_DISCARD) != 0) {
359                 DPRINTK("access denied: discard of [%Lu,%Lu) on dev=%04x\n",
360                         preq.sector_number,
361                         preq.sector_number + preq.nr_sects, preq.dev);
362                 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
363                 msleep(1); /* back off a bit */
364                 return;
365         }
366
367         plug_queue(blkif, preq.bdev);
368
369         switch (blkdev_issue_discard(preq.bdev, preq.sector_number,
370                                      preq.nr_sects, GFP_KERNEL, secure)) {
371         case 0:
372                 status = BLKIF_RSP_OKAY;
373                 break;
374         case -EOPNOTSUPP:
375                 DPRINTK("discard op failed, not supported\n");
376                 status = BLKIF_RSP_EOPNOTSUPP;
377                 break;
378         default:
379                 status = BLKIF_RSP_ERROR;
380                 break;
381         }
382
383         make_response(blkif, req->id, req->operation, status);
384 }
385
386 static int _do_block_io_op(blkif_t *blkif)
387 {
388         blkif_back_rings_t *blk_rings = &blkif->blk_rings;
389         blkif_request_t req;
390         pending_req_t *pending_req;
391         RING_IDX rc, rp;
392
393         rc = blk_rings->common.req_cons;
394         rp = blk_rings->common.sring->req_prod;
395         rmb(); /* Ensure we see queued requests up to 'rp'. */
396
397         while (rc != rp) {
398                 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
399                         break;
400
401                 if (kthread_should_stop())
402                         return 1;
403
404                 switch (blkif->blk_protocol) {
405                 case BLKIF_PROTOCOL_NATIVE:
406                         req = *RING_GET_REQUEST(&blk_rings->native, rc);
407                         break;
408                 case BLKIF_PROTOCOL_X86_32:
409                         blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
410                         break;
411                 case BLKIF_PROTOCOL_X86_64:
412                         blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
413                         break;
414                 default:
415                         BUG();
416                         return 0; /* make compiler happy */
417                 }
418
419                 ++rc;
420
421                 switch (req.operation) {
422                 case BLKIF_OP_READ:
423                 case BLKIF_OP_WRITE:
424                 case BLKIF_OP_WRITE_BARRIER:
425                 case BLKIF_OP_FLUSH_DISKCACHE:
426                         pending_req = alloc_req();
427                         if (!pending_req) {
428                                 blkif->st_oo_req++;
429                                 return 1;
430                         }
431
432                         /* before make_response() */
433                         blk_rings->common.req_cons = rc;
434
435                         /* Apply all sanity checks to /private copy/ of request. */
436                         barrier();
437
438                         dispatch_rw_block_io(blkif, &req, pending_req);
439                         break;
440                 case BLKIF_OP_DISCARD:
441                         blk_rings->common.req_cons = rc;
442                         barrier();
443                         dispatch_discard(blkif, (void *)&req);
444                         break;
445                 case BLKIF_OP_PACKET:
446                         blk_rings->common.req_cons = rc;
447                         barrier();
448                         blkif->st_pk_req++;
449                         DPRINTK("error: block operation BLKIF_OP_PACKET not implemented\n");
450                         make_response(blkif, req.id, req.operation,
451                                       BLKIF_RSP_ERROR);
452                         break;
453                 default:
454                         /* A good sign something is wrong: sleep for a while to
455                          * avoid excessive CPU consumption by a bad guest. */
456                         msleep(1);
457                         blk_rings->common.req_cons = rc;
458                         barrier();
459                         DPRINTK("error: unknown block io operation [%d]\n",
460                                 req.operation);
461                         make_response(blkif, req.id, req.operation,
462                                       BLKIF_RSP_ERROR);
463                         break;
464                 }
465
466                 /* Yield point for this unbounded loop. */
467                 cond_resched();
468         }
469
470         return 0;
471 }
472
473 static int
474 do_block_io_op(blkif_t *blkif)
475 {
476         blkif_back_rings_t *blk_rings = &blkif->blk_rings;
477         int more_to_do;
478
479         do {
480                 more_to_do = _do_block_io_op(blkif);
481                 if (more_to_do)
482                         break;
483
484                 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
485         } while (more_to_do);
486
487         return more_to_do;
488 }
489
490 static void dispatch_rw_block_io(blkif_t *blkif,
491                                  blkif_request_t *req,
492                                  pending_req_t *pending_req)
493 {
494         struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
495         struct phys_req preq;
496         struct { 
497                 unsigned long buf; unsigned int nsec;
498         } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
499         unsigned int nseg;
500         struct bio *bio = NULL;
501         uint32_t flags;
502         int ret, i;
503         int operation;
504
505         switch (req->operation) {
506         case BLKIF_OP_READ:
507                 blkif->st_rd_req++;
508                 operation = READ;
509                 break;
510         case BLKIF_OP_WRITE:
511                 blkif->st_wr_req++;
512                 operation = WRITE;
513                 break;
514         case BLKIF_OP_WRITE_BARRIER:
515                 blkif->st_br_req++;
516                 operation = WRITE_FLUSH_FUA;
517                 break;
518         case BLKIF_OP_FLUSH_DISKCACHE:
519                 blkif->st_fl_req++;
520                 operation = WRITE_FLUSH;
521                 break;
522         default:
523                 operation = 0; /* make gcc happy */
524                 BUG();
525         }
526
527         /* Check that number of segments is sane. */
528         nseg = req->nr_segments;
529         if (unlikely(nseg == 0 && !(operation & REQ_FLUSH)) ||
530             unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
531                 DPRINTK("Bad number of segments in request (%d)\n", nseg);
532                 goto fail_response;
533         }
534
535         preq.dev           = req->handle;
536         preq.sector_number = req->sector_number;
537         preq.nr_sects      = 0;
538
539         pending_req->blkif     = blkif;
540         pending_req->id        = req->id;
541         pending_req->operation = req->operation;
542         pending_req->nr_pages  = nseg;
543
544         flags = GNTMAP_host_map;
545         if (operation != READ)
546                 flags |= GNTMAP_readonly;
547
548         for (i = 0; i < nseg; i++) {
549                 seg[i].nsec = req->seg[i].last_sect -
550                         req->seg[i].first_sect + 1;
551
552                 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
553                     (req->seg[i].last_sect < req->seg[i].first_sect))
554                         goto fail_response;
555                 preq.nr_sects += seg[i].nsec;
556
557                 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
558                                   req->seg[i].gref, blkif->domid);
559         }
560
561         ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
562         BUG_ON(ret);
563
564         for (i = 0; i < nseg; i++) {
565                 if (unlikely(map[i].status == GNTST_eagain))
566                         gnttab_check_GNTST_eagain_do_while(GNTTABOP_map_grant_ref, &map[i])
567                 if (unlikely(map[i].status != GNTST_okay)) {
568                         DPRINTK("invalid buffer -- could not remap it\n");
569                         map[i].handle = BLKBACK_INVALID_HANDLE;
570                         ret = 1;
571                 } else {
572                         blkback_pagemap_set(vaddr_pagenr(pending_req, i),
573                                             pending_page(pending_req, i),
574                                             blkif->domid, req->handle,
575                                             req->seg[i].gref);
576                 }
577
578                 pending_handle(pending_req, i) = map[i].handle;
579
580                 if (ret)
581                         continue;
582
583                 set_phys_to_machine(
584                         page_to_pfn(pending_page(pending_req, i)),
585                         FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
586                 seg[i].buf  = map[i].dev_bus_addr | 
587                         (req->seg[i].first_sect << 9);
588         }
589
590         if (ret)
591                 goto fail_flush;
592
593         if (vbd_translate(&preq, blkif, operation) != 0) {
594                 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n", 
595                         operation == READ ? "read" : "write",
596                         preq.sector_number,
597                         preq.sector_number + preq.nr_sects, preq.dev);
598                 goto fail_flush;
599         }
600
601         /* Wait on all outstanding I/O's and once that has been completed
602          * issue the WRITE_FLUSH.
603          */
604         if (req->operation == BLKIF_OP_WRITE_BARRIER)
605                 drain_io(blkif);
606
607         plug_queue(blkif, preq.bdev);
608         atomic_set(&pending_req->pendcnt, 1);
609         blkif_get(blkif);
610
611         for (i = 0; i < nseg; i++) {
612                 if (((int)preq.sector_number|(int)seg[i].nsec) &
613                     ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
614                         DPRINTK("Misaligned I/O request from domain %d",
615                                 blkif->domid);
616                         goto fail_put_bio;
617                 }
618
619                 while ((bio == NULL) ||
620                        (bio_add_page(bio,
621                                      pending_page(pending_req, i),
622                                      seg[i].nsec << 9,
623                                      seg[i].buf & ~PAGE_MASK) == 0)) {
624                         if (bio) {
625                                 atomic_inc(&pending_req->pendcnt);
626                                 submit_bio(operation, bio);
627                         }
628
629                         bio = bio_alloc(GFP_KERNEL, nseg-i);
630                         if (unlikely(bio == NULL))
631                                 goto fail_put_bio;
632
633                         bio->bi_bdev    = preq.bdev;
634                         bio->bi_private = pending_req;
635                         bio->bi_end_io  = end_block_io_op;
636                         bio->bi_sector  = preq.sector_number;
637                 }
638
639                 preq.sector_number += seg[i].nsec;
640         }
641
642         if (!bio) {
643                 BUG_ON(!(operation & (REQ_FLUSH|REQ_FUA)));
644                 bio = bio_alloc(GFP_KERNEL, 0);
645                 if (unlikely(bio == NULL))
646                         goto fail_put_bio;
647
648                 bio->bi_bdev    = preq.bdev;
649                 bio->bi_private = pending_req;
650                 bio->bi_end_io  = end_block_io_op;
651                 bio->bi_sector  = -1;
652         }
653
654         submit_bio(operation, bio);
655
656         if (operation == READ)
657                 blkif->st_rd_sect += preq.nr_sects;
658         else
659                 blkif->st_wr_sect += preq.nr_sects;
660
661         return;
662
663  fail_flush:
664         fast_flush_area(pending_req);
665  fail_response:
666         make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
667         free_req(pending_req);
668         msleep(1); /* back off a bit */
669         return;
670
671  fail_put_bio:
672         __end_block_io_op(pending_req, -EINVAL);
673         if (bio)
674                 bio_put(bio);
675         unplug_queue(blkif);
676         msleep(1); /* back off a bit */
677         return;
678 }
679
680
681
682 /******************************************************************
683  * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
684  */
685
686
687 static void make_response(blkif_t *blkif, u64 id,
688                           unsigned short op, int st)
689 {
690         blkif_response_t  resp;
691         unsigned long     flags;
692         blkif_back_rings_t *blk_rings = &blkif->blk_rings;
693         int notify;
694
695         resp.id        = id;
696         resp.operation = op;
697         resp.status    = st;
698
699         spin_lock_irqsave(&blkif->blk_ring_lock, flags);
700         /* Place on the response ring for the relevant domain. */
701         switch (blkif->blk_protocol) {
702         case BLKIF_PROTOCOL_NATIVE:
703                 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
704                        &resp, sizeof(resp));
705                 break;
706         case BLKIF_PROTOCOL_X86_32:
707                 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
708                        &resp, sizeof(resp));
709                 break;
710         case BLKIF_PROTOCOL_X86_64:
711                 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
712                        &resp, sizeof(resp));
713                 break;
714         default:
715                 BUG();
716         }
717         blk_rings->common.rsp_prod_pvt++;
718         RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
719         spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
720
721         if (notify)
722                 notify_remote_via_irq(blkif->irq);
723 }
724
725 static int __init blkif_init(void)
726 {
727         int i, mmap_pages;
728
729         if (!is_running_on_xen())
730                 return -ENODEV;
731
732         mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
733
734         pending_reqs          = kzalloc(sizeof(pending_reqs[0]) *
735                                         blkif_reqs, GFP_KERNEL);
736         pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
737                                         mmap_pages, GFP_KERNEL);
738         pending_pages         = alloc_empty_pages_and_pagevec(mmap_pages);
739
740         if (blkback_pagemap_init(mmap_pages))
741                 goto out_of_memory;
742
743         if (!pending_reqs || !pending_grant_handles || !pending_pages)
744                 goto out_of_memory;
745
746         for (i = 0; i < mmap_pages; i++)
747                 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
748
749         blkif_interface_init();
750
751         INIT_LIST_HEAD(&pending_free);
752
753         for (i = 0; i < blkif_reqs; i++)
754                 list_add_tail(&pending_reqs[i].free_list, &pending_free);
755
756         blkif_xenbus_init();
757
758         return 0;
759
760  out_of_memory:
761         kfree(pending_reqs);
762         kfree(pending_grant_handles);
763         free_empty_pages_and_pagevec(pending_pages, mmap_pages);
764         pr_warning("%s: out of memory\n", __FUNCTION__);
765         return -ENOMEM;
766 }
767
768 module_init(blkif_init);
769
770 MODULE_LICENSE("Dual BSD/GPL");
771 MODULE_ALIAS("xen-backend:vbd");