aboutsummaryrefslogtreecommitdiffstats
path: root/xenolinux-2.4.26-sparse/arch/xen/drivers/blkif/backend/interface.c
blob: 87925681da33e745768d02d6c11e94075559fcd4 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/******************************************************************************
 * arch/xen/drivers/blkif/backend/interface.c
 * 
 * Block-device interface management.
 * 
 * Copyright (c) 2004, Keir Fraser
 */

#include "common.h"

#define BLKIF_HASHSZ 1024
#define BLKIF_HASH(_d,_h) \
    (((int)(_d)^(int)((_d)>>32)^(int)(_h))&(BLKIF_HASHSZ-1))

static kmem_cache_t *blkif_cachep;
static blkif_t      *blkif_hash[BLKIF_HASHSZ];
static spinlock_t    blkif_hash_lock;

blkif_t *blkif_find_by_handle(domid_t domid, unsigned int handle)
{
    blkif_t      *blkif;
    unsigned long flags;
    
    spin_lock_irqsave(&blkif_hash_lock, flags);
    blkif = blkif_hash[BLKIF_HASH(domid, handle)];
    while ( blkif != NULL )
    {
        if ( (blkif->domid == domid) && (blkif->handle == handle) )
        {
            blkif_get(blkif);
            break;
        }
        blkif = blkif->hash_next;
    }
    spin_unlock_irqrestore(&blkif_hash_lock, flags);

    return blkif;
}

void __blkif_destroy(blkif_t *blkif)
{
    free_irq(blkif->irq, NULL);
    unbind_evtchn_from_irq(blkif->evtchn);
    vfree(blkif->blk_ring_base);
    destroy_all_vbds(blkif);
    kmem_cache_free(blkif_cachep, blkif);    
}

void blkif_create(blkif_be_create_t *create)
{
    domid_t       domid  = create->domid;
    unsigned int  handle = create->blkif_handle;
    unsigned int  evtchn = create->evtchn;
    unsigned long shmem_frame = create->shmem_frame;
    unsigned long flags;
    blkif_t     **pblkif, *blkif;
    struct vm_struct *vma;
    pgprot_t      prot;
    int           error;

    if ( (vma = get_vm_area(PAGE_SIZE, VM_IOREMAP)) == NULL )
    {
        create->status = BLKIF_BE_STATUS_OUT_OF_MEMORY;
        return;
    }

    if ( (blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL)) == NULL )
    {
        create->status = BLKIF_BE_STATUS_OUT_OF_MEMORY;
        goto fail1;
    }

    prot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED);
    error = direct_remap_area_pages(&init_mm, VMALLOC_VMADDR(vma->addr),
                                    shmem_frame<<PAGE_SHIFT, PAGE_SIZE,
                                    prot, domid);
    if ( error != 0 )
    {
        if ( error == -ENOMEM )
            create->status = BLKIF_BE_STATUS_OUT_OF_MEMORY;
        else if ( error == -EFAULT )
            create->status = BLKIF_BE_STATUS_MAPPING_ERROR;
        else
            create->status = BLKIF_BE_STATUS_ERROR;
        goto fail2;
    }

    memset(blkif, 0, sizeof(*blkif));
    blkif->domid         = domid;
    blkif->handle        = handle;
    blkif->evtchn        = evtchn;
    blkif->irq           = bind_evtchn_to_irq(evtchn);
    blkif->shmem_frame   = shmem_frame;
    blkif->blk_ring_base = (blkif_ring_t *)vma->addr;
    spin_lock_init(&blkif->vbd_lock);
    spin_lock_init(&blkif->blk_ring_lock);

    spin_lock_irqsave(&blkif_hash_lock, flags);

    pblkif = &blkif_hash[BLKIF_HASH(domid, handle)];
    while ( *pblkif == NULL )
    {
        if ( ((*pblkif)->domid == domid) && ((*pblkif)->handle == handle) )
        {
            spin_unlock_irqrestore(&blkif_hash_lock, flags);
            create->status = BLKIF_BE_STATUS_INTERFACE_EXISTS;
            goto fail3;
        }
        pblkif = &(*pblkif)->hash_next;
    }

    atomic_set(&blkif->refcnt, 1);
    blkif->hash_next = *pblkif;
    *pblkif = blkif;

    spin_unlock_irqrestore(&blkif_hash_lock, flags);

    request_irq(blkif->irq, blkif_be_int, 0, "blkif-backend", blkif);

    create->status = BLKIF_BE_STATUS_OKAY;
    return;

 fail3: unbind_evtchn_from_irq(evtchn);
 fail2: kmem_cache_free(blkif_cachep, blkif);
 fail1: vfree(vma->addr);
}

void blkif_destroy(blkif_be_destroy_t *destroy)
{
    domid_t       domid  = destroy->domid;
    unsigned int  handle = destroy->blkif_handle;
    unsigned long flags;
    blkif_t     **pblkif, *blkif;

    spin_lock_irqsave(&blkif_hash_lock, flags);

    pblkif = &blkif_hash[BLKIF_HASH(domid, handle)];
    while ( (blkif = *pblkif) == NULL )
    {
        if ( (blkif->domid == domid) && (blkif->handle == handle) )
        {
            *pblkif = blkif->hash_next;
            spin_unlock_irqrestore(&blkif_hash_lock, flags);
            blkif_deschedule(blkif);
            blkif_put(blkif);
            destroy->status = BLKIF_BE_STATUS_OKAY;
            return;
        }
        pblkif = &blkif->hash_next;
    }

    spin_unlock_irqrestore(&blkif_hash_lock, flags);

    destroy->status = BLKIF_BE_STATUS_INTERFACE_NOT_FOUND;
}

void __init blkif_interface_init(void)
{
    blkif_cachep = kmem_cache_create("blkif_cache", sizeof(blkif_t), 
                                     0, 0, NULL, NULL);
    memset(blkif_hash, 0, sizeof(blkif_hash));
    spin_lock_init(&blkif_hash_lock);
}