Path: blob/master/drivers/block/xen-blkback/blkback.c
15115 views
/******************************************************************************1*2* Back-end of the driver for virtual block devices. This portion of the3* driver exports a 'unified' block-device interface that can be accessed4* by any operating system that implements a compatible front end. A5* reference front-end implementation can be found in:6* drivers/block/xen-blkfront.c7*8* Copyright (c) 2003-2004, Keir Fraser & Steve Hand9* Copyright (c) 2005, Christopher Clark10*11* This program is free software; you can redistribute it and/or12* modify it under the terms of the GNU General Public License version 213* as published by the Free Software Foundation; or, when distributed14* separately from the Linux kernel or incorporated into other15* software packages, subject to the following license:16*17* Permission is hereby granted, free of charge, to any person obtaining a copy18* of this source file (the "Software"), to deal in the Software without19* restriction, including without limitation the rights to use, copy, modify,20* merge, publish, distribute, sublicense, and/or sell copies of the Software,21* and to permit persons to whom the Software is furnished to do so, subject to22* the following conditions:23*24* The above copyright notice and this permission notice shall be included in25* all copies or substantial portions of the Software.26*27* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR28* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,29* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE30* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER31* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING32* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS33* IN THE SOFTWARE.34*/3536#include <linux/spinlock.h>37#include <linux/kthread.h>38#include <linux/list.h>39#include <linux/delay.h>40#include <linux/freezer.h>4142#include <xen/events.h>43#include <xen/page.h>44#include <asm/xen/hypervisor.h>45#include <asm/xen/hypercall.h>46#include "common.h"4748/*49* These are rather arbitrary. They are fairly large because adjacent requests50* pulled from a communication ring are quite likely to end up being part of51* the same scatter/gather request at the disc.52*53* ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **54*55* This will increase the chances of being able to write whole tracks.56* 64 should be enough to keep us competitive with Linux.57*/58static int xen_blkif_reqs = 64;59module_param_named(reqs, xen_blkif_reqs, int, 0);60MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");6162/* Run-time switchable: /sys/module/blkback/parameters/ */63static unsigned int log_stats;64module_param(log_stats, int, 0644);6566/*67* Each outstanding request that we've passed to the lower device layers has a68* 'pending_req' allocated to it. Each buffer_head that completes decrements69* the pendcnt towards zero. When it hits zero, the specified domain has a70* response queued for it, with the saved 'id' passed back.71*/72struct pending_req {73struct xen_blkif *blkif;74u64 id;75int nr_pages;76atomic_t pendcnt;77unsigned short operation;78int status;79struct list_head free_list;80};8182#define BLKBACK_INVALID_HANDLE (~0)8384struct xen_blkbk {85struct pending_req *pending_reqs;86/* List of all 'pending_req' available */87struct list_head pending_free;88/* And its spinlock. */89spinlock_t pending_free_lock;90wait_queue_head_t pending_free_wq;91/* The list of all pages that are available. */92struct page **pending_pages;93/* And the grant handles that are available. */94grant_handle_t *pending_grant_handles;95};9697static struct xen_blkbk *blkbk;9899/*100* Little helpful macro to figure out the index and virtual address of the101* pending_pages[..]. For each 'pending_req' we have have up to102* BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through103* 10 and would index in the pending_pages[..].104*/105static inline int vaddr_pagenr(struct pending_req *req, int seg)106{107return (req - blkbk->pending_reqs) *108BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;109}110111#define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]112113static inline unsigned long vaddr(struct pending_req *req, int seg)114{115unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));116return (unsigned long)pfn_to_kaddr(pfn);117}118119#define pending_handle(_req, _seg) \120(blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])121122123static int do_block_io_op(struct xen_blkif *blkif);124static int dispatch_rw_block_io(struct xen_blkif *blkif,125struct blkif_request *req,126struct pending_req *pending_req);127static void make_response(struct xen_blkif *blkif, u64 id,128unsigned short op, int st);129130/*131* Retrieve from the 'pending_reqs' a free pending_req structure to be used.132*/133static struct pending_req *alloc_req(void)134{135struct pending_req *req = NULL;136unsigned long flags;137138spin_lock_irqsave(&blkbk->pending_free_lock, flags);139if (!list_empty(&blkbk->pending_free)) {140req = list_entry(blkbk->pending_free.next, struct pending_req,141free_list);142list_del(&req->free_list);143}144spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);145return req;146}147148/*149* Return the 'pending_req' structure back to the freepool. We also150* wake up the thread if it was waiting for a free page.151*/152static void free_req(struct pending_req *req)153{154unsigned long flags;155int was_empty;156157spin_lock_irqsave(&blkbk->pending_free_lock, flags);158was_empty = list_empty(&blkbk->pending_free);159list_add(&req->free_list, &blkbk->pending_free);160spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);161if (was_empty)162wake_up(&blkbk->pending_free_wq);163}164165/*166* Routines for managing virtual block devices (vbds).167*/168static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,169int operation)170{171struct xen_vbd *vbd = &blkif->vbd;172int rc = -EACCES;173174if ((operation != READ) && vbd->readonly)175goto out;176177if (likely(req->nr_sects)) {178blkif_sector_t end = req->sector_number + req->nr_sects;179180if (unlikely(end < req->sector_number))181goto out;182if (unlikely(end > vbd_sz(vbd)))183goto out;184}185186req->dev = vbd->pdevice;187req->bdev = vbd->bdev;188rc = 0;189190out:191return rc;192}193194static void xen_vbd_resize(struct xen_blkif *blkif)195{196struct xen_vbd *vbd = &blkif->vbd;197struct xenbus_transaction xbt;198int err;199struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);200unsigned long long new_size = vbd_sz(vbd);201202pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",203blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));204pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);205vbd->size = new_size;206again:207err = xenbus_transaction_start(&xbt);208if (err) {209pr_warn(DRV_PFX "Error starting transaction");210return;211}212err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",213(unsigned long long)vbd_sz(vbd));214if (err) {215pr_warn(DRV_PFX "Error writing new size");216goto abort;217}218/*219* Write the current state; we will use this to synchronize220* the front-end. If the current state is "connected" the221* front-end will get the new size information online.222*/223err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);224if (err) {225pr_warn(DRV_PFX "Error writing the state");226goto abort;227}228229err = xenbus_transaction_end(xbt, 0);230if (err == -EAGAIN)231goto again;232if (err)233pr_warn(DRV_PFX "Error ending transaction");234return;235abort:236xenbus_transaction_end(xbt, 1);237}238239/*240* Notification from the guest OS.241*/242static void blkif_notify_work(struct xen_blkif *blkif)243{244blkif->waiting_reqs = 1;245wake_up(&blkif->wq);246}247248irqreturn_t xen_blkif_be_int(int irq, void *dev_id)249{250blkif_notify_work(dev_id);251return IRQ_HANDLED;252}253254/*255* SCHEDULER FUNCTIONS256*/257258static void print_stats(struct xen_blkif *blkif)259{260pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d\n",261current->comm, blkif->st_oo_req,262blkif->st_rd_req, blkif->st_wr_req, blkif->st_f_req);263blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);264blkif->st_rd_req = 0;265blkif->st_wr_req = 0;266blkif->st_oo_req = 0;267}268269int xen_blkif_schedule(void *arg)270{271struct xen_blkif *blkif = arg;272struct xen_vbd *vbd = &blkif->vbd;273274xen_blkif_get(blkif);275276while (!kthread_should_stop()) {277if (try_to_freeze())278continue;279if (unlikely(vbd->size != vbd_sz(vbd)))280xen_vbd_resize(blkif);281282wait_event_interruptible(283blkif->wq,284blkif->waiting_reqs || kthread_should_stop());285wait_event_interruptible(286blkbk->pending_free_wq,287!list_empty(&blkbk->pending_free) ||288kthread_should_stop());289290blkif->waiting_reqs = 0;291smp_mb(); /* clear flag *before* checking for work */292293if (do_block_io_op(blkif))294blkif->waiting_reqs = 1;295296if (log_stats && time_after(jiffies, blkif->st_print))297print_stats(blkif);298}299300if (log_stats)301print_stats(blkif);302303blkif->xenblkd = NULL;304xen_blkif_put(blkif);305306return 0;307}308309struct seg_buf {310unsigned long buf;311unsigned int nsec;312};313/*314* Unmap the grant references, and also remove the M2P over-rides315* used in the 'pending_req'.316*/317static void xen_blkbk_unmap(struct pending_req *req)318{319struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];320unsigned int i, invcount = 0;321grant_handle_t handle;322int ret;323324for (i = 0; i < req->nr_pages; i++) {325handle = pending_handle(req, i);326if (handle == BLKBACK_INVALID_HANDLE)327continue;328gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),329GNTMAP_host_map, handle);330pending_handle(req, i) = BLKBACK_INVALID_HANDLE;331invcount++;332}333334ret = HYPERVISOR_grant_table_op(335GNTTABOP_unmap_grant_ref, unmap, invcount);336BUG_ON(ret);337/*338* Note, we use invcount, so nr->pages, so we can't index339* using vaddr(req, i).340*/341for (i = 0; i < invcount; i++) {342ret = m2p_remove_override(343virt_to_page(unmap[i].host_addr), false);344if (ret) {345pr_alert(DRV_PFX "Failed to remove M2P override for %lx\n",346(unsigned long)unmap[i].host_addr);347continue;348}349}350}351352static int xen_blkbk_map(struct blkif_request *req,353struct pending_req *pending_req,354struct seg_buf seg[])355{356struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];357int i;358int nseg = req->nr_segments;359int ret = 0;360361/*362* Fill out preq.nr_sects with proper amount of sectors, and setup363* assign map[..] with the PFN of the page in our domain with the364* corresponding grant reference for each page.365*/366for (i = 0; i < nseg; i++) {367uint32_t flags;368369flags = GNTMAP_host_map;370if (pending_req->operation != BLKIF_OP_READ)371flags |= GNTMAP_readonly;372gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,373req->u.rw.seg[i].gref,374pending_req->blkif->domid);375}376377ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);378BUG_ON(ret);379380/*381* Now swizzle the MFN in our domain with the MFN from the other domain382* so that when we access vaddr(pending_req,i) it has the contents of383* the page from the other domain.384*/385for (i = 0; i < nseg; i++) {386if (unlikely(map[i].status != 0)) {387pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");388map[i].handle = BLKBACK_INVALID_HANDLE;389ret |= 1;390}391392pending_handle(pending_req, i) = map[i].handle;393394if (ret)395continue;396397ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),398blkbk->pending_page(pending_req, i), false);399if (ret) {400pr_alert(DRV_PFX "Failed to install M2P override for %lx (ret: %d)\n",401(unsigned long)map[i].dev_bus_addr, ret);402/* We could switch over to GNTTABOP_copy */403continue;404}405406seg[i].buf = map[i].dev_bus_addr |407(req->u.rw.seg[i].first_sect << 9);408}409return ret;410}411412/*413* Completion callback on the bio's. Called as bh->b_end_io()414*/415416static void __end_block_io_op(struct pending_req *pending_req, int error)417{418/* An error fails the entire request. */419if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&420(error == -EOPNOTSUPP)) {421pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");422xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);423pending_req->status = BLKIF_RSP_EOPNOTSUPP;424} else if (error) {425pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"426" error=%d\n", error);427pending_req->status = BLKIF_RSP_ERROR;428}429430/*431* If all of the bio's have completed it is time to unmap432* the grant references associated with 'request' and provide433* the proper response on the ring.434*/435if (atomic_dec_and_test(&pending_req->pendcnt)) {436xen_blkbk_unmap(pending_req);437make_response(pending_req->blkif, pending_req->id,438pending_req->operation, pending_req->status);439xen_blkif_put(pending_req->blkif);440free_req(pending_req);441}442}443444/*445* bio callback.446*/447static void end_block_io_op(struct bio *bio, int error)448{449__end_block_io_op(bio->bi_private, error);450bio_put(bio);451}452453454455/*456* Function to copy the from the ring buffer the 'struct blkif_request'457* (which has the sectors we want, number of them, grant references, etc),458* and transmute it to the block API to hand it over to the proper block disk.459*/460static int do_block_io_op(struct xen_blkif *blkif)461{462union blkif_back_rings *blk_rings = &blkif->blk_rings;463struct blkif_request req;464struct pending_req *pending_req;465RING_IDX rc, rp;466int more_to_do = 0;467468rc = blk_rings->common.req_cons;469rp = blk_rings->common.sring->req_prod;470rmb(); /* Ensure we see queued requests up to 'rp'. */471472while (rc != rp) {473474if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))475break;476477if (kthread_should_stop()) {478more_to_do = 1;479break;480}481482pending_req = alloc_req();483if (NULL == pending_req) {484blkif->st_oo_req++;485more_to_do = 1;486break;487}488489switch (blkif->blk_protocol) {490case BLKIF_PROTOCOL_NATIVE:491memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));492break;493case BLKIF_PROTOCOL_X86_32:494blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));495break;496case BLKIF_PROTOCOL_X86_64:497blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));498break;499default:500BUG();501}502blk_rings->common.req_cons = ++rc; /* before make_response() */503504/* Apply all sanity checks to /private copy/ of request. */505barrier();506507if (dispatch_rw_block_io(blkif, &req, pending_req))508break;509510/* Yield point for this unbounded loop. */511cond_resched();512}513514return more_to_do;515}516517/*518* Transmutation of the 'struct blkif_request' to a proper 'struct bio'519* and call the 'submit_bio' to pass it to the underlying storage.520*/521static int dispatch_rw_block_io(struct xen_blkif *blkif,522struct blkif_request *req,523struct pending_req *pending_req)524{525struct phys_req preq;526struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];527unsigned int nseg;528struct bio *bio = NULL;529struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];530int i, nbio = 0;531int operation;532struct blk_plug plug;533534switch (req->operation) {535case BLKIF_OP_READ:536blkif->st_rd_req++;537operation = READ;538break;539case BLKIF_OP_WRITE:540blkif->st_wr_req++;541operation = WRITE_ODIRECT;542break;543case BLKIF_OP_FLUSH_DISKCACHE:544blkif->st_f_req++;545operation = WRITE_FLUSH;546break;547case BLKIF_OP_WRITE_BARRIER:548default:549operation = 0; /* make gcc happy */550goto fail_response;551break;552}553554/* Check that the number of segments is sane. */555nseg = req->nr_segments;556if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||557unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {558pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",559nseg);560/* Haven't submitted any bio's yet. */561goto fail_response;562}563564preq.dev = req->handle;565preq.sector_number = req->u.rw.sector_number;566preq.nr_sects = 0;567568pending_req->blkif = blkif;569pending_req->id = req->id;570pending_req->operation = req->operation;571pending_req->status = BLKIF_RSP_OKAY;572pending_req->nr_pages = nseg;573574for (i = 0; i < nseg; i++) {575seg[i].nsec = req->u.rw.seg[i].last_sect -576req->u.rw.seg[i].first_sect + 1;577if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||578(req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))579goto fail_response;580preq.nr_sects += seg[i].nsec;581582}583584if (xen_vbd_translate(&preq, blkif, operation) != 0) {585pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",586operation == READ ? "read" : "write",587preq.sector_number,588preq.sector_number + preq.nr_sects, preq.dev);589goto fail_response;590}591592/*593* This check _MUST_ be done after xen_vbd_translate as the preq.bdev594* is set there.595*/596for (i = 0; i < nseg; i++) {597if (((int)preq.sector_number|(int)seg[i].nsec) &598((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {599pr_debug(DRV_PFX "Misaligned I/O request from domain %d",600blkif->domid);601goto fail_response;602}603}604605/*606* If we have failed at this point, we need to undo the M2P override,607* set gnttab_set_unmap_op on all of the grant references and perform608* the hypercall to unmap the grants - that is all done in609* xen_blkbk_unmap.610*/611if (xen_blkbk_map(req, pending_req, seg))612goto fail_flush;613614/* This corresponding xen_blkif_put is done in __end_block_io_op */615xen_blkif_get(blkif);616617for (i = 0; i < nseg; i++) {618while ((bio == NULL) ||619(bio_add_page(bio,620blkbk->pending_page(pending_req, i),621seg[i].nsec << 9,622seg[i].buf & ~PAGE_MASK) == 0)) {623624bio = bio_alloc(GFP_KERNEL, nseg-i);625if (unlikely(bio == NULL))626goto fail_put_bio;627628biolist[nbio++] = bio;629bio->bi_bdev = preq.bdev;630bio->bi_private = pending_req;631bio->bi_end_io = end_block_io_op;632bio->bi_sector = preq.sector_number;633}634635preq.sector_number += seg[i].nsec;636}637638/* This will be hit if the operation was a flush. */639if (!bio) {640BUG_ON(operation != WRITE_FLUSH);641642bio = bio_alloc(GFP_KERNEL, 0);643if (unlikely(bio == NULL))644goto fail_put_bio;645646biolist[nbio++] = bio;647bio->bi_bdev = preq.bdev;648bio->bi_private = pending_req;649bio->bi_end_io = end_block_io_op;650}651652/*653* We set it one so that the last submit_bio does not have to call654* atomic_inc.655*/656atomic_set(&pending_req->pendcnt, nbio);657658/* Get a reference count for the disk queue and start sending I/O */659blk_start_plug(&plug);660661for (i = 0; i < nbio; i++)662submit_bio(operation, biolist[i]);663664/* Let the I/Os go.. */665blk_finish_plug(&plug);666667if (operation == READ)668blkif->st_rd_sect += preq.nr_sects;669else if (operation == WRITE || operation == WRITE_FLUSH)670blkif->st_wr_sect += preq.nr_sects;671672return 0;673674fail_flush:675xen_blkbk_unmap(pending_req);676fail_response:677/* Haven't submitted any bio's yet. */678make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);679free_req(pending_req);680msleep(1); /* back off a bit */681return -EIO;682683fail_put_bio:684for (i = 0; i < nbio; i++)685bio_put(biolist[i]);686__end_block_io_op(pending_req, -EINVAL);687msleep(1); /* back off a bit */688return -EIO;689}690691692693/*694* Put a response on the ring on how the operation fared.695*/696static void make_response(struct xen_blkif *blkif, u64 id,697unsigned short op, int st)698{699struct blkif_response resp;700unsigned long flags;701union blkif_back_rings *blk_rings = &blkif->blk_rings;702int more_to_do = 0;703int notify;704705resp.id = id;706resp.operation = op;707resp.status = st;708709spin_lock_irqsave(&blkif->blk_ring_lock, flags);710/* Place on the response ring for the relevant domain. */711switch (blkif->blk_protocol) {712case BLKIF_PROTOCOL_NATIVE:713memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),714&resp, sizeof(resp));715break;716case BLKIF_PROTOCOL_X86_32:717memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),718&resp, sizeof(resp));719break;720case BLKIF_PROTOCOL_X86_64:721memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),722&resp, sizeof(resp));723break;724default:725BUG();726}727blk_rings->common.rsp_prod_pvt++;728RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);729if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {730/*731* Tail check for pending requests. Allows frontend to avoid732* notifications if requests are already in flight (lower733* overheads and promotes batching).734*/735RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);736737} else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {738more_to_do = 1;739}740741spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);742743if (more_to_do)744blkif_notify_work(blkif);745if (notify)746notify_remote_via_irq(blkif->irq);747}748749static int __init xen_blkif_init(void)750{751int i, mmap_pages;752int rc = 0;753754if (!xen_pv_domain())755return -ENODEV;756757blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);758if (!blkbk) {759pr_alert(DRV_PFX "%s: out of memory!\n", __func__);760return -ENOMEM;761}762763mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;764765blkbk->pending_reqs = kmalloc(sizeof(blkbk->pending_reqs[0]) *766xen_blkif_reqs, GFP_KERNEL);767blkbk->pending_grant_handles = kzalloc(sizeof(blkbk->pending_grant_handles[0]) *768mmap_pages, GFP_KERNEL);769blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *770mmap_pages, GFP_KERNEL);771772if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||773!blkbk->pending_pages) {774rc = -ENOMEM;775goto out_of_memory;776}777778for (i = 0; i < mmap_pages; i++) {779blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;780blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);781if (blkbk->pending_pages[i] == NULL) {782rc = -ENOMEM;783goto out_of_memory;784}785}786rc = xen_blkif_interface_init();787if (rc)788goto failed_init;789790memset(blkbk->pending_reqs, 0, sizeof(blkbk->pending_reqs));791792INIT_LIST_HEAD(&blkbk->pending_free);793spin_lock_init(&blkbk->pending_free_lock);794init_waitqueue_head(&blkbk->pending_free_wq);795796for (i = 0; i < xen_blkif_reqs; i++)797list_add_tail(&blkbk->pending_reqs[i].free_list,798&blkbk->pending_free);799800rc = xen_blkif_xenbus_init();801if (rc)802goto failed_init;803804return 0;805806out_of_memory:807pr_alert(DRV_PFX "%s: out of memory\n", __func__);808failed_init:809kfree(blkbk->pending_reqs);810kfree(blkbk->pending_grant_handles);811if (blkbk->pending_pages) {812for (i = 0; i < mmap_pages; i++) {813if (blkbk->pending_pages[i])814__free_page(blkbk->pending_pages[i]);815}816kfree(blkbk->pending_pages);817}818kfree(blkbk);819blkbk = NULL;820return rc;821}822823module_init(xen_blkif_init);824825MODULE_LICENSE("Dual BSD/GPL");826827828