Statistics
| Branch: | Revision:

root / block-nbd.c @ 8c5e95d8

History | View | Annotate | Download (4.7 kB)

1
/*
2
 * QEMU Block driver for  NBD
3
 *
4
 * Copyright (C) 2008 Bull S.A.S.
5
 *     Author: Laurent Vivier <Laurent.Vivier@bull;net>
6
 *
7
 * Some parts:
8
 *    Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28

    
29
#include "qemu-common.h"
30
#include "nbd.h"
31

    
32
#include <sys/types.h>
33
#include <unistd.h>
34
#include <sys/socket.h>
35
#include <sys/un.h>
36
#include <netinet/in.h>
37
#include <arpa/inet.h>
38
#include <pthread.h>
39

    
40
typedef struct BDRVNBDState {
41
    int sock;
42
    off_t size;
43
    size_t blocksize;
44
} BDRVNBDState;
45

    
46
static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
47
{
48
    BDRVNBDState *s = bs->opaque;
49
    const char *host;
50
    const char *unixpath;
51
    int sock;
52
    off_t size;
53
    size_t blocksize;
54
    int ret;
55

    
56
    if ((flags & BDRV_O_CREAT))
57
        return -EINVAL;
58

    
59
    if (!strstart(filename, "nbd:", &host))
60
        return -EINVAL;
61

    
62
    if (strstart(host, "unix:", &unixpath)) {
63

    
64
        if (unixpath[0] != '/')
65
            return -EINVAL;
66

    
67
        sock = unix_socket_outgoing(unixpath);
68

    
69
    } else {
70
        uint16_t port;
71
        char *p, *r;
72
        char hostname[128];
73

    
74
        pstrcpy(hostname, 128, host);
75

    
76
        p = strchr(hostname, ':');
77
        if (p == NULL)
78
            return -EINVAL;
79

    
80
        *p = '\0';
81
        p++;
82

    
83
        port = strtol(p, &r, 0);
84
        if (r == p)
85
            return -EINVAL;
86
        sock = tcp_socket_outgoing(hostname, port);
87
    }
88

    
89
    if (sock == -1)
90
        return -errno;
91

    
92
    ret = nbd_receive_negotiate(sock, &size, &blocksize);
93
    if (ret == -1)
94
        return -errno;
95

    
96
    s->sock = sock;
97
    s->size = size;
98
    s->blocksize = blocksize;
99

    
100
    return 0;
101
}
102

    
103
static int nbd_read(BlockDriverState *bs, int64_t sector_num,
104
                    uint8_t *buf, int nb_sectors)
105
{
106
    BDRVNBDState *s = bs->opaque;
107
    struct nbd_request request;
108
    struct nbd_reply reply;
109

    
110
    request.type = NBD_CMD_READ;
111
    request.handle = (uint64_t)(intptr_t)bs;
112
    request.from = sector_num * 512;;
113
    request.len = nb_sectors * 512;
114

    
115
    if (nbd_send_request(s->sock, &request) == -1)
116
        return -errno;
117

    
118
    if (nbd_receive_reply(s->sock, &reply) == -1)
119
        return -errno;
120

    
121
    if (reply.error !=0)
122
        return -reply.error;
123

    
124
    if (reply.handle != request.handle)
125
        return -EIO;
126

    
127
    if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len)
128
        return -EIO;
129

    
130
    return 0;
131
}
132

    
133
static int nbd_write(BlockDriverState *bs, int64_t sector_num,
134
                     const uint8_t *buf, int nb_sectors)
135
{
136
    BDRVNBDState *s = bs->opaque;
137
    struct nbd_request request;
138
    struct nbd_reply reply;
139

    
140
    request.type = NBD_CMD_WRITE;
141
    request.handle = (uint64_t)(intptr_t)bs;
142
    request.from = sector_num * 512;;
143
    request.len = nb_sectors * 512;
144

    
145
    if (nbd_send_request(s->sock, &request) == -1)
146
        return -errno;
147

    
148
    if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len)
149
        return -EIO;
150

    
151
    if (nbd_receive_reply(s->sock, &reply) == -1)
152
        return -errno;
153

    
154
    if (reply.error !=0)
155
        return -reply.error;
156

    
157
    if (reply.handle != request.handle)
158
        return -EIO;
159

    
160
    return 0;
161
}
162

    
163
static void nbd_close(BlockDriverState *bs)
164
{
165
    BDRVNBDState *s = bs->opaque;
166
    struct nbd_request request;
167

    
168
    request.type = NBD_CMD_DISC;
169
    request.handle = (uint64_t)(intptr_t)bs;
170
    request.from = 0;
171
    request.len = 0;
172
    nbd_send_request(s->sock, &request);
173

    
174
    close(s->sock);
175
}
176

    
177
static int64_t nbd_getlength(BlockDriverState *bs)
178
{
179
    BDRVNBDState *s = bs->opaque;
180

    
181
    return s->size;
182
}
183

    
184
BlockDriver bdrv_nbd = {
185
    "nbd",
186
    sizeof(BDRVNBDState),
187
    NULL, /* no probe for protocols */
188
    nbd_open,
189
    nbd_read,
190
    nbd_write,
191
    nbd_close,
192
    .bdrv_getlength = nbd_getlength,
193
    .protocol_name = "nbd",
194
};