aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xc/lib/xc_vbd.c
blob: 00d166d03de72d56ed016c051ec3e8f2e49c6aaf (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/******************************************************************************
 * xc_vbd.c
 * 
 * API for manipulating and accessing per-domain virtual block devices.
 * 
 * Copyright (c) 2003, K A Fraser.
 */

#define _GNU_SOURCE
#include "xc_private.h"

int xc_vbd_create(int xc_handle,
                  u64 domid, 
                  unsigned short vbdid, 
                  int writeable)
{
    block_io_op_t op; 
    op.cmd = BLOCK_IO_OP_VBD_CREATE; 
    op.u.create_params.domain  = (domid_t)domid;
    op.u.create_params.vdevice = vbdid;
    op.u.create_params.mode    = VBD_MODE_R | (writeable ? VBD_MODE_W : 0);
    return do_block_io_op(xc_handle, &op);
}


int xc_vbd_destroy(int xc_handle,
                   u64 domid, 
                   unsigned short vbdid)
{
    block_io_op_t op; 
    op.cmd = BLOCK_IO_OP_VBD_DELETE; 
    op.u.delete_params.domain  = (domid_t)domid;
    op.u.delete_params.vdevice = vbdid;
    return do_block_io_op(xc_handle, &op);
}


int xc_vbd_grow(int xc_handle,
                u64 domid, 
                unsigned short vbdid,
                xc_vbdextent_t *extent)
{
    block_io_op_t op; 
    op.cmd = BLOCK_IO_OP_VBD_GROW; 
    op.u.grow_params.domain  = (domid_t)domid; 
    op.u.grow_params.vdevice = vbdid;
    op.u.grow_params.extent.device       = extent->real_device; 
    op.u.grow_params.extent.start_sector = (xen_sector_t)extent->start_sector;
    op.u.grow_params.extent.nr_sectors   = (xen_sector_t)extent->nr_sectors;
    return do_block_io_op(xc_handle, &op);
}


int xc_vbd_shrink(int xc_handle,
                  u64 domid, 
                  unsigned short vbdid)
{
    block_io_op_t op; 
    op.cmd = BLOCK_IO_OP_VBD_SHRINK; 
    op.u.shrink_params.domain  = (domid_t)domid; 
    op.u.shrink_params.vdevice = vbdid;
    return do_block_io_op(xc_handle, &op);
}


int xc_vbd_setextents(int xc_handle,
                      u64 domid, 
                      unsigned short vbdid,
                      unsigned int nr_extents,
                      xc_vbdextent_t *extents)
{
    int           i, rc;
    block_io_op_t op;
    xen_extent_t *real_extents = NULL;

    if ( nr_extents != 0 )
    {
        real_extents = malloc(nr_extents * sizeof(xc_vbdextent_t));
        if ( (real_extents == NULL) || 
             (mlock(real_extents, nr_extents * sizeof(xc_vbdextent_t)) != 0) )
        {
            if ( real_extents != NULL )
                free(real_extents);
            return -ENOMEM;
        }

        for ( i = 0; i < nr_extents; i++ )
        {
            real_extents[i].device       = extents[i].real_device;
            real_extents[i].start_sector = 
                (xen_sector_t)extents[i].start_sector;
            real_extents[i].nr_sectors   = 
                (xen_sector_t)extents[i].nr_sectors;
        }
    }

    op.cmd = BLOCK_IO_OP_VBD_SET_EXTENTS;
    op.u.setextents_params.domain     = (domid_t)domid;
    op.u.setextents_params.vdevice    = vbdid;
    op.u.setextents_params.nr_extents = nr_extents;
    op.u.setextents_params.extents    = real_extents;
    rc = do_block_io_op(xc_handle, &op);

    if ( real_extents != NULL )
    {
        (void)munlock(real_extents, nr_extents * sizeof(xc_vbdextent_t));
        free(real_extents);
    }

    return rc;
}


int xc_vbd_getextents(int xc_handle,
                      u64 domid, 
                      unsigned short vbdid,
                      unsigned int max_extents,
                      xc_vbdextent_t *extents,
                      int *writeable)
{
    int           i, rc;
    block_io_op_t op;
    xen_extent_t *real_extents = malloc(max_extents * sizeof(xc_vbdextent_t));

    if ( (real_extents == NULL) || 
         (mlock(real_extents, max_extents * sizeof(xc_vbdextent_t)) != 0) )
    {
        if ( real_extents != NULL )
            free(real_extents);
        return -ENOMEM;
    }

    op.cmd = BLOCK_IO_OP_VBD_INFO;
    op.u.info_params.domain     = (domid_t)domid;
    op.u.info_params.vdevice    = vbdid;
    op.u.info_params.maxextents = max_extents;
    op.u.info_params.extents    = real_extents;
    rc = do_block_io_op(xc_handle, &op);

    (void)munlock(real_extents, max_extents * sizeof(xc_vbdextent_t));

    if ( rc >= 0 )
    {
        for ( i = 0; i < op.u.info_params.nextents; i++ )
        {
            extents[i].real_device  = real_extents[i].device;
            extents[i].start_sector = (u64)real_extents[i].start_sector;
            extents[i].nr_sectors   = (u64)real_extents[i].nr_sectors;
        }

        if ( writeable != NULL )
            *writeable = !!(op.u.info_params.mode & VBD_MODE_W);

        rc = op.u.info_params.nextents;
    }

    free(real_extents);

    return rc;
}


int xc_vbd_probe(int xc_handle,
                 u64 domid,
                 unsigned int max_vbds,
                 xc_vbd_t *vbds)
{
    block_io_op_t op; 
    xen_disk_info_t *xdi = &op.u.probe_params.xdi; 
    int i, j, ret, allocsz = max_vbds * sizeof(xen_disk_t); 

    op.cmd = BLOCK_IO_OP_VBD_PROBE; 
    op.u.probe_params.domain = (domid_t)domid; 
    
    xdi->max   = max_vbds;
    xdi->disks = malloc(allocsz);
    xdi->count = 0;

    if ( (xdi->disks == NULL) || (mlock(xdi->disks, allocsz) != 0) )
    {
        if ( xdi->disks != NULL )
            free(xdi->disks);
        return -ENOMEM;
    }

    ret = do_block_io_op(xc_handle, &op);

    (void)munlock(xdi->disks, allocsz);

    if ( ret >= 0 )
    {
        for ( i = 0, j = 0; i < xdi->count; i++ )
        {
            if ( !(xdi->disks[i].info & XD_FLAG_VIRT) )
                continue;
            
            vbds[j].domid = (u64)xdi->disks[i].domain;
            vbds[j].vbdid = xdi->disks[i].device;
            vbds[j].flags = (xdi->disks[i].info & XD_FLAG_RO) ? 
                0 : XC_VBDF_WRITEABLE;
            vbds[j].nr_sectors = (u64)xdi->disks[i].capacity;
            
            j++;
        }

        ret = j;
    }
    
    free(xdi->disks);

    return ret;
}