Added patch headers.
[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/delay.h>
43 #include <xen/balloon.h>
44 #include <asm/hypervisor.h>
45 #include "common.h"
46
47 /*
48  * These are rather arbitrary. They are fairly large because adjacent requests
49  * pulled from a communication ring are quite likely to end up being part of
50  * the same scatter/gather request at the disc.
51  * 
52  * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
53  * 
54  * This will increase the chances of being able to write whole tracks.
55  * 64 should be enough to keep us competitive with Linux.
56  */
57 static int blkif_reqs = 64;
58 module_param_named(reqs, blkif_reqs, int, 0);
59 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
60
61 /* Run-time switchable: /sys/module/blkback/parameters/ */
62 static unsigned int log_stats = 0;
63 static unsigned int debug_lvl = 0;
64 module_param(log_stats, int, 0644);
65 module_param(debug_lvl, int, 0644);
66
67 /*
68  * Each outstanding request that we've passed to the lower device layers has a 
69  * 'pending_req' allocated to it. Each buffer_head that completes decrements 
70  * the pendcnt towards zero. When it hits zero, the specified domain has a 
71  * response queued for it, with the saved 'id' passed back.
72  */
73 typedef struct {
74         blkif_t       *blkif;
75         u64            id;
76         int            nr_pages;
77         atomic_t       pendcnt;
78         unsigned short operation;
79         int            status;
80         struct list_head free_list;
81 } pending_req_t;
82
83 static pending_req_t *pending_reqs;
84 static struct list_head pending_free;
85 static DEFINE_SPINLOCK(pending_free_lock);
86 static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
87
88 #define BLKBACK_INVALID_HANDLE (~0)
89
90 static struct page **pending_pages;
91 static grant_handle_t *pending_grant_handles;
92
93 static inline int vaddr_pagenr(pending_req_t *req, int seg)
94 {
95         return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
96 }
97
98 #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
99
100 static inline unsigned long vaddr(pending_req_t *req, int seg)
101 {
102         unsigned long pfn = page_to_pfn(pending_page(req, seg));
103         return (unsigned long)pfn_to_kaddr(pfn);
104 }
105
106 #define pending_handle(_req, _seg) \
107         (pending_grant_handles[vaddr_pagenr(_req, _seg)])
108
109
110 static int do_block_io_op(blkif_t *blkif);
111 static int dispatch_rw_block_io(blkif_t *blkif,
112                                  blkif_request_t *req,
113                                  pending_req_t *pending_req);
114 static void make_response(blkif_t *blkif, u64 id,
115                           unsigned short op, int st);
116
117 /******************************************************************
118  * misc small helpers
119  */
120 static pending_req_t* alloc_req(void)
121 {
122         pending_req_t *req = NULL;
123         unsigned long flags;
124
125         spin_lock_irqsave(&pending_free_lock, flags);
126         if (!list_empty(&pending_free)) {
127                 req = list_entry(pending_free.next, pending_req_t, free_list);
128                 list_del(&req->free_list);
129         }
130         spin_unlock_irqrestore(&pending_free_lock, flags);
131         return req;
132 }
133
134 static void free_req(pending_req_t *req)
135 {
136         unsigned long flags;
137         int was_empty;
138
139         spin_lock_irqsave(&pending_free_lock, flags);
140         was_empty = list_empty(&pending_free);
141         list_add(&req->free_list, &pending_free);
142         spin_unlock_irqrestore(&pending_free_lock, flags);
143         if (was_empty)
144                 wake_up(&pending_free_wq);
145 }
146
147 static void unplug_queue(blkif_t *blkif)
148 {
149         if (blkif->plug == NULL)
150                 return;
151         if (blkif->plug->unplug_fn)
152                 blkif->plug->unplug_fn(blkif->plug);
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 |  pk %4d\n",
199                current->comm, blkif->st_oo_req,
200                blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req,
201                blkif->st_pk_req);
202         blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
203         blkif->st_rd_req = 0;
204         blkif->st_wr_req = 0;
205         blkif->st_oo_req = 0;
206         blkif->st_pk_req = 0;
207 }
208
209 int blkif_schedule(void *arg)
210 {
211         blkif_t *blkif = arg;
212         struct vbd *vbd = &blkif->vbd;
213
214         blkif_get(blkif);
215
216         if (debug_lvl)
217                 printk(KERN_DEBUG "%s: started\n", current->comm);
218
219         while (!kthread_should_stop()) {
220                 if (try_to_freeze())
221                         continue;
222                 if (unlikely(vbd->size != vbd_size(vbd)))
223                         vbd_resize(blkif);
224
225                 wait_event_interruptible(
226                         blkif->wq,
227                         blkif->waiting_reqs || kthread_should_stop());
228                 wait_event_interruptible(
229                         pending_free_wq,
230                         !list_empty(&pending_free) || kthread_should_stop());
231
232                 blkif->waiting_reqs = 0;
233                 smp_mb(); /* clear flag *before* checking for work */
234
235                 if (do_block_io_op(blkif))
236                         blkif->waiting_reqs = 1;
237                 unplug_queue(blkif);
238
239                 if (log_stats && time_after(jiffies, blkif->st_print))
240                         print_stats(blkif);
241         }
242
243         if (log_stats)
244                 print_stats(blkif);
245         if (debug_lvl)
246                 printk(KERN_DEBUG "%s: exiting\n", current->comm);
247
248         blkif->xenblkd = NULL;
249         blkif_put(blkif);
250
251         return 0;
252 }
253
254 /******************************************************************
255  * COMPLETION CALLBACK -- Called as bh->b_end_io()
256  */
257
258 static void __end_block_io_op(pending_req_t *pending_req, int error)
259 {
260         /* An error fails the entire request. */
261         if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
262             (error == -EOPNOTSUPP)) {
263                 DPRINTK("blkback: write barrier op failed, not supported\n");
264                 blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
265                 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
266         } else if (error) {
267                 DPRINTK("Buffer not up-to-date at end of operation, "
268                         "error=%d\n", error);
269                 pending_req->status = BLKIF_RSP_ERROR;
270         }
271
272         if (atomic_dec_and_test(&pending_req->pendcnt)) {
273                 fast_flush_area(pending_req);
274                 make_response(pending_req->blkif, pending_req->id,
275                               pending_req->operation, pending_req->status);
276                 blkif_put(pending_req->blkif);
277                 free_req(pending_req);
278         }
279 }
280
281 static void end_block_io_op(struct bio *bio, int error)
282 {
283         __end_block_io_op(bio->bi_private, error);
284         bio_put(bio);
285 }
286
287
288 /******************************************************************************
289  * NOTIFICATION FROM GUEST OS.
290  */
291
292 static void blkif_notify_work(blkif_t *blkif)
293 {
294         blkif->waiting_reqs = 1;
295         wake_up(&blkif->wq);
296 }
297
298 irqreturn_t blkif_be_int(int irq, void *dev_id)
299 {
300         blkif_notify_work(dev_id);
301         return IRQ_HANDLED;
302 }
303
304
305
306 /******************************************************************
307  * DOWNWARD CALLS -- These interface with the block-device layer proper.
308  */
309
310 static int do_block_io_op(blkif_t *blkif)
311 {
312         blkif_back_rings_t *blk_rings = &blkif->blk_rings;
313         blkif_request_t req;
314         pending_req_t *pending_req;
315         RING_IDX rc, rp;
316         int more_to_do = 0, ret;
317
318         rc = blk_rings->common.req_cons;
319         rp = blk_rings->common.sring->req_prod;
320         rmb(); /* Ensure we see queued requests up to 'rp'. */
321
322         while ((rc != rp) || (blkif->is_suspended_req)) {
323
324                 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
325                         break;
326
327                 if (kthread_should_stop()) {
328                         more_to_do = 1;
329                         break;
330                 }
331
332                 pending_req = alloc_req();
333                 if (NULL == pending_req) {
334                         blkif->st_oo_req++;
335                         more_to_do = 1;
336                         break;
337                 }
338
339         /* Handle the suspended request first, if one exists */
340         if(blkif->is_suspended_req)
341         {
342             memcpy(&req, &blkif->suspended_req, sizeof(req));
343             blkif->is_suspended_req = 0;
344             goto handle_request;
345         }
346
347                 switch (blkif->blk_protocol) {
348                 case BLKIF_PROTOCOL_NATIVE:
349                         memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
350                         break;
351                 case BLKIF_PROTOCOL_X86_32:
352                         blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
353                         break;
354                 case BLKIF_PROTOCOL_X86_64:
355                         blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
356                         break;
357                 default:
358                         BUG();
359                 }
360                 blk_rings->common.req_cons = ++rc; /* before make_response() */
361
362                 /* Apply all sanity checks to /private copy/ of request. */
363                 barrier();
364
365 handle_request:
366         ret = 0;
367                 switch (req.operation) {
368                 case BLKIF_OP_READ:
369                         blkif->st_rd_req++;
370                         ret = dispatch_rw_block_io(blkif, &req, pending_req); 
371                         break;
372                 case BLKIF_OP_WRITE_BARRIER:
373                         blkif->st_br_req++;
374                         /* fall through */
375                 case BLKIF_OP_WRITE:
376                         blkif->st_wr_req++;
377                         ret = dispatch_rw_block_io(blkif, &req, pending_req);
378                         break;
379                 case BLKIF_OP_PACKET:
380                         DPRINTK("error: block operation BLKIF_OP_PACKET not implemented\n");
381                         blkif->st_pk_req++;
382                         make_response(blkif, req.id, req.operation,
383                                       BLKIF_RSP_ERROR);
384                         free_req(pending_req);
385                         break;
386                 default:
387                         /* A good sign something is wrong: sleep for a while to
388                          * avoid excessive CPU consumption by a bad guest. */
389                         msleep(1);
390                         DPRINTK("error: unknown block io operation [%d]\n",
391                                 req.operation);
392                         make_response(blkif, req.id, req.operation,
393                                       BLKIF_RSP_ERROR);
394                         free_req(pending_req);
395                         break;
396                 }
397         BUG_ON(ret != 0 && ret != -EAGAIN);
398         /* If we can't handle the request at the moment, save it, and break the
399          * loop */ 
400         if(ret == -EAGAIN)
401         {
402             memcpy(&blkif->suspended_req, &req, sizeof(req));
403             blkif->is_suspended_req = 1;
404             /* Return "no more work pending", restart will be handled 'out of
405              * band' */
406             return 0;
407         }
408
409                 /* Yield point for this unbounded loop. */
410                 cond_resched();
411         }
412
413         return more_to_do;
414 }
415
416 static int dispatch_rw_block_io(blkif_t *blkif,
417                                  blkif_request_t *req,
418                                  pending_req_t *pending_req)
419 {
420         extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
421         struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
422         struct phys_req preq;
423         struct { 
424                 unsigned long buf; unsigned int nsec;
425         } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
426         unsigned int nseg;
427         struct bio *bio = NULL;
428         int ret, i;
429         int operation;
430
431         switch (req->operation) {
432         case BLKIF_OP_READ:
433                 operation = READ;
434                 break;
435         case BLKIF_OP_WRITE:
436                 operation = WRITE;
437                 break;
438         case BLKIF_OP_WRITE_BARRIER:
439                 operation = WRITE_BARRIER;
440                 break;
441         default:
442                 operation = 0; /* make gcc happy */
443                 BUG();
444         }
445
446         /* Check that number of segments is sane. */
447         nseg = req->nr_segments;
448         if (unlikely(nseg == 0 && operation != WRITE_BARRIER) || 
449             unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
450                 DPRINTK("Bad number of segments in request (%d)\n", nseg);
451                 goto fail_response;
452         }
453
454         preq.dev           = req->handle;
455         preq.sector_number = req->sector_number;
456         preq.nr_sects      = 0;
457
458         pending_req->blkif     = blkif;
459         pending_req->id        = req->id;
460         pending_req->operation = req->operation;
461         pending_req->status    = BLKIF_RSP_OKAY;
462         pending_req->nr_pages  = nseg;
463
464         for (i = 0; i < nseg; i++) {
465                 uint32_t flags;
466
467                 seg[i].nsec = req->seg[i].last_sect -
468                         req->seg[i].first_sect + 1;
469
470                 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
471                     (req->seg[i].last_sect < req->seg[i].first_sect))
472                         goto fail_response;
473                 preq.nr_sects += seg[i].nsec;
474
475                 flags = GNTMAP_host_map;
476                 if (operation != READ)
477                         flags |= GNTMAP_readonly;
478                 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
479                                   req->seg[i].gref, blkif->domid);
480         }
481
482         ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
483         BUG_ON(ret);
484
485 #define GENERAL_ERR   (1<<0)
486 #define EAGAIN_ERR    (1<<1)
487         for (i = 0; i < nseg; i++) {
488                 if (unlikely(map[i].status != 0)) {
489                         DPRINTK("invalid buffer -- could not remap it\n");
490                         map[i].handle = BLKBACK_INVALID_HANDLE;
491                         ret |= GENERAL_ERR;
492             if(map[i].status == GNTST_eagain)
493                             ret |= EAGAIN_ERR;
494                 } else {
495                         blkback_pagemap_set(vaddr_pagenr(pending_req, i),
496                                             pending_page(pending_req, i),
497                                             blkif->domid, req->handle,
498                                             req->seg[i].gref);
499                 }
500
501                 pending_handle(pending_req, i) = map[i].handle;
502
503                 if (ret)
504                         continue;
505
506                 set_phys_to_machine(
507                         page_to_pfn(pending_page(pending_req, i)),
508                         FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
509                 seg[i].buf  = map[i].dev_bus_addr | 
510                         (req->seg[i].first_sect << 9);
511         }
512
513     /* If any of grant maps failed with GNTST_eagain, suspend and retry later */
514     if(ret & EAGAIN_ERR)
515     {
516         fast_flush_area(pending_req);
517         free_req(pending_req);
518         return -EAGAIN;
519     }
520
521         if (ret)
522                 goto fail_flush;
523
524         if (vbd_translate(&preq, blkif, operation) != 0) {
525                 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n", 
526                         operation == READ ? "read" : "write",
527                         preq.sector_number,
528                         preq.sector_number + preq.nr_sects, preq.dev);
529                 goto fail_flush;
530         }
531
532         plug_queue(blkif, preq.bdev);
533         atomic_set(&pending_req->pendcnt, 1);
534         blkif_get(blkif);
535
536         for (i = 0; i < nseg; i++) {
537                 if (((int)preq.sector_number|(int)seg[i].nsec) &
538                     ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
539                         DPRINTK("Misaligned I/O request from domain %d",
540                                 blkif->domid);
541                         goto fail_put_bio;
542                 }
543
544                 while ((bio == NULL) ||
545                        (bio_add_page(bio,
546                                      pending_page(pending_req, i),
547                                      seg[i].nsec << 9,
548                                      seg[i].buf & ~PAGE_MASK) == 0)) {
549                         if (bio) {
550                                 atomic_inc(&pending_req->pendcnt);
551                                 submit_bio(operation, bio);
552                         }
553
554                         bio = bio_alloc(GFP_KERNEL, nseg-i);
555                         if (unlikely(bio == NULL))
556                                 goto fail_put_bio;
557
558                         bio->bi_bdev    = preq.bdev;
559                         bio->bi_private = pending_req;
560                         bio->bi_end_io  = end_block_io_op;
561                         bio->bi_sector  = preq.sector_number;
562                 }
563
564                 preq.sector_number += seg[i].nsec;
565         }
566
567         if (!bio) {
568                 BUG_ON(operation != WRITE_BARRIER);
569                 bio = bio_alloc(GFP_KERNEL, 0);
570                 if (unlikely(bio == NULL))
571                         goto fail_put_bio;
572
573                 bio->bi_bdev    = preq.bdev;
574                 bio->bi_private = pending_req;
575                 bio->bi_end_io  = end_block_io_op;
576                 bio->bi_sector  = -1;
577         }
578
579         submit_bio(operation, bio);
580
581         if (operation == READ)
582                 blkif->st_rd_sect += preq.nr_sects;
583         else if (operation == WRITE || operation == WRITE_BARRIER)
584                 blkif->st_wr_sect += preq.nr_sects;
585
586         return 0;
587
588  fail_flush:
589         fast_flush_area(pending_req);
590  fail_response:
591         make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
592         free_req(pending_req);
593         msleep(1); /* back off a bit */
594         return 0;
595
596  fail_put_bio:
597         __end_block_io_op(pending_req, -EINVAL);
598         if (bio)
599                 bio_put(bio);
600         unplug_queue(blkif);
601         msleep(1); /* back off a bit */
602         return 0;
603 }
604
605
606
607 /******************************************************************
608  * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
609  */
610
611
612 static void make_response(blkif_t *blkif, u64 id,
613                           unsigned short op, int st)
614 {
615         blkif_response_t  resp;
616         unsigned long     flags;
617         blkif_back_rings_t *blk_rings = &blkif->blk_rings;
618         int more_to_do = 0;
619         int notify;
620
621         resp.id        = id;
622         resp.operation = op;
623         resp.status    = st;
624
625         spin_lock_irqsave(&blkif->blk_ring_lock, flags);
626         /* Place on the response ring for the relevant domain. */
627         switch (blkif->blk_protocol) {
628         case BLKIF_PROTOCOL_NATIVE:
629                 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
630                        &resp, sizeof(resp));
631                 break;
632         case BLKIF_PROTOCOL_X86_32:
633                 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
634                        &resp, sizeof(resp));
635                 break;
636         case BLKIF_PROTOCOL_X86_64:
637                 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
638                        &resp, sizeof(resp));
639                 break;
640         default:
641                 BUG();
642         }
643         blk_rings->common.rsp_prod_pvt++;
644         RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
645         if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
646                 /*
647                  * Tail check for pending requests. Allows frontend to avoid
648                  * notifications if requests are already in flight (lower
649                  * overheads and promotes batching).
650                  */
651                 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
652
653         } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
654                 more_to_do = 1;
655         }
656
657         spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
658
659         if (more_to_do)
660                 blkif_notify_work(blkif);
661         if (notify)
662                 notify_remote_via_irq(blkif->irq);
663 }
664
665 static int __init blkif_init(void)
666 {
667         int i, mmap_pages;
668
669         if (!is_running_on_xen())
670                 return -ENODEV;
671
672         mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
673
674         pending_reqs          = kzalloc(sizeof(pending_reqs[0]) *
675                                         blkif_reqs, GFP_KERNEL);
676         pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
677                                         mmap_pages, GFP_KERNEL);
678         pending_pages         = alloc_empty_pages_and_pagevec(mmap_pages);
679
680         if (blkback_pagemap_init(mmap_pages))
681                 goto out_of_memory;
682
683         if (!pending_reqs || !pending_grant_handles || !pending_pages)
684                 goto out_of_memory;
685
686         for (i = 0; i < mmap_pages; i++)
687                 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
688
689         blkif_interface_init();
690
691         INIT_LIST_HEAD(&pending_free);
692
693         for (i = 0; i < blkif_reqs; i++)
694                 list_add_tail(&pending_reqs[i].free_list, &pending_free);
695
696         blkif_xenbus_init();
697
698         return 0;
699
700  out_of_memory:
701         kfree(pending_reqs);
702         kfree(pending_grant_handles);
703         free_empty_pages_and_pagevec(pending_pages, mmap_pages);
704         printk("%s: out of memory\n", __FUNCTION__);
705         return -ENOMEM;
706 }
707
708 module_init(blkif_init);
709
710 MODULE_LICENSE("Dual BSD/GPL");