1019d6b8fSAnthony Liguori /* 2019d6b8fSAnthony Liguori * QEMU Block driver for NBD 3019d6b8fSAnthony Liguori * 4019d6b8fSAnthony Liguori * Copyright (C) 2008 Bull S.A.S. 5019d6b8fSAnthony Liguori * Author: Laurent Vivier <Laurent.Vivier@bull.net> 6019d6b8fSAnthony Liguori * 7019d6b8fSAnthony Liguori * Some parts: 8019d6b8fSAnthony Liguori * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws> 9019d6b8fSAnthony Liguori * 10019d6b8fSAnthony Liguori * Permission is hereby granted, free of charge, to any person obtaining a copy 11019d6b8fSAnthony Liguori * of this software and associated documentation files (the "Software"), to deal 12019d6b8fSAnthony Liguori * in the Software without restriction, including without limitation the rights 13019d6b8fSAnthony Liguori * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14019d6b8fSAnthony Liguori * copies of the Software, and to permit persons to whom the Software is 15019d6b8fSAnthony Liguori * furnished to do so, subject to the following conditions: 16019d6b8fSAnthony Liguori * 17019d6b8fSAnthony Liguori * The above copyright notice and this permission notice shall be included in 18019d6b8fSAnthony Liguori * all copies or substantial portions of the Software. 19019d6b8fSAnthony Liguori * 20019d6b8fSAnthony Liguori * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21019d6b8fSAnthony Liguori * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22019d6b8fSAnthony Liguori * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23019d6b8fSAnthony Liguori * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24019d6b8fSAnthony Liguori * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25019d6b8fSAnthony Liguori * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26019d6b8fSAnthony Liguori * THE SOFTWARE. 27019d6b8fSAnthony Liguori */ 28019d6b8fSAnthony Liguori 2980c71a24SPeter Maydell #include "qemu/osdep.h" 302302c1caSMarc-André Lureau #include "block/nbd-client.h" 31da34e65cSMarkus Armbruster #include "qapi/error.h" 321de7afc9SPaolo Bonzini #include "qemu/uri.h" 33737e150eSPaolo Bonzini #include "block/block_int.h" 341de7afc9SPaolo Bonzini #include "qemu/module.h" 352019d68bSMax Reitz #include "qapi/qmp/qdict.h" 36f53a1febSKevin Wolf #include "qapi/qmp/qjson.h" 37f53a1febSKevin Wolf #include "qapi/qmp/qint.h" 382019d68bSMax Reitz #include "qapi/qmp/qstring.h" 39f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 40019d6b8fSAnthony Liguori 411d45f8b5SLaurent Vivier #define EN_OPTSTR ":exportname=" 421d45f8b5SLaurent Vivier 43019d6b8fSAnthony Liguori typedef struct BDRVNBDState { 442302c1caSMarc-André Lureau NbdClientSession client; 45019d6b8fSAnthony Liguori } BDRVNBDState; 46019d6b8fSAnthony Liguori 47f53a1febSKevin Wolf static int nbd_parse_uri(const char *filename, QDict *options) 481d7d2a9dSPaolo Bonzini { 491d7d2a9dSPaolo Bonzini URI *uri; 501d7d2a9dSPaolo Bonzini const char *p; 511d7d2a9dSPaolo Bonzini QueryParams *qp = NULL; 521d7d2a9dSPaolo Bonzini int ret = 0; 53f53a1febSKevin Wolf bool is_unix; 541d7d2a9dSPaolo Bonzini 551d7d2a9dSPaolo Bonzini uri = uri_parse(filename); 561d7d2a9dSPaolo Bonzini if (!uri) { 571d7d2a9dSPaolo Bonzini return -EINVAL; 581d7d2a9dSPaolo Bonzini } 591d7d2a9dSPaolo Bonzini 601d7d2a9dSPaolo Bonzini /* transport */ 611d7d2a9dSPaolo Bonzini if (!strcmp(uri->scheme, "nbd")) { 62f53a1febSKevin Wolf is_unix = false; 631d7d2a9dSPaolo Bonzini } else if (!strcmp(uri->scheme, "nbd+tcp")) { 64f53a1febSKevin Wolf is_unix = false; 651d7d2a9dSPaolo Bonzini } else if (!strcmp(uri->scheme, "nbd+unix")) { 66f53a1febSKevin Wolf is_unix = true; 671d7d2a9dSPaolo Bonzini } else { 681d7d2a9dSPaolo Bonzini ret = -EINVAL; 691d7d2a9dSPaolo Bonzini goto out; 701d7d2a9dSPaolo Bonzini } 711d7d2a9dSPaolo Bonzini 721d7d2a9dSPaolo Bonzini p = uri->path ? uri->path : "/"; 731d7d2a9dSPaolo Bonzini p += strspn(p, "/"); 741d7d2a9dSPaolo Bonzini if (p[0]) { 75f53a1febSKevin Wolf qdict_put(options, "export", qstring_from_str(p)); 761d7d2a9dSPaolo Bonzini } 771d7d2a9dSPaolo Bonzini 781d7d2a9dSPaolo Bonzini qp = query_params_parse(uri->query); 79f53a1febSKevin Wolf if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { 801d7d2a9dSPaolo Bonzini ret = -EINVAL; 811d7d2a9dSPaolo Bonzini goto out; 821d7d2a9dSPaolo Bonzini } 831d7d2a9dSPaolo Bonzini 84f53a1febSKevin Wolf if (is_unix) { 851d7d2a9dSPaolo Bonzini /* nbd+unix:///export?socket=path */ 861d7d2a9dSPaolo Bonzini if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { 871d7d2a9dSPaolo Bonzini ret = -EINVAL; 881d7d2a9dSPaolo Bonzini goto out; 891d7d2a9dSPaolo Bonzini } 90f53a1febSKevin Wolf qdict_put(options, "path", qstring_from_str(qp->p[0].value)); 911d7d2a9dSPaolo Bonzini } else { 9223307908SJán Tomko QString *host; 93bebbf7faSKevin Wolf /* nbd[+tcp]://host[:port]/export */ 941d7d2a9dSPaolo Bonzini if (!uri->server) { 951d7d2a9dSPaolo Bonzini ret = -EINVAL; 961d7d2a9dSPaolo Bonzini goto out; 971d7d2a9dSPaolo Bonzini } 98f17c90beSKevin Wolf 9923307908SJán Tomko /* strip braces from literal IPv6 address */ 10023307908SJán Tomko if (uri->server[0] == '[') { 10123307908SJán Tomko host = qstring_from_substr(uri->server, 1, 10223307908SJán Tomko strlen(uri->server) - 2); 10323307908SJán Tomko } else { 10423307908SJán Tomko host = qstring_from_str(uri->server); 10523307908SJán Tomko } 10623307908SJán Tomko 10723307908SJán Tomko qdict_put(options, "host", host); 108bebbf7faSKevin Wolf if (uri->port) { 109bebbf7faSKevin Wolf char* port_str = g_strdup_printf("%d", uri->port); 110f53a1febSKevin Wolf qdict_put(options, "port", qstring_from_str(port_str)); 111f53a1febSKevin Wolf g_free(port_str); 1121d7d2a9dSPaolo Bonzini } 113bebbf7faSKevin Wolf } 1141d7d2a9dSPaolo Bonzini 1151d7d2a9dSPaolo Bonzini out: 1161d7d2a9dSPaolo Bonzini if (qp) { 1171d7d2a9dSPaolo Bonzini query_params_free(qp); 1181d7d2a9dSPaolo Bonzini } 1191d7d2a9dSPaolo Bonzini uri_free(uri); 1201d7d2a9dSPaolo Bonzini return ret; 1211d7d2a9dSPaolo Bonzini } 1221d7d2a9dSPaolo Bonzini 1236963a30dSKevin Wolf static void nbd_parse_filename(const char *filename, QDict *options, 1246963a30dSKevin Wolf Error **errp) 125019d6b8fSAnthony Liguori { 1261d45f8b5SLaurent Vivier char *file; 12733897dc7SNick Thomas char *export_name; 12833897dc7SNick Thomas const char *host_spec; 129019d6b8fSAnthony Liguori const char *unixpath; 130019d6b8fSAnthony Liguori 131681e7ad0SKevin Wolf if (qdict_haskey(options, "host") 132681e7ad0SKevin Wolf || qdict_haskey(options, "port") 133681e7ad0SKevin Wolf || qdict_haskey(options, "path")) 134681e7ad0SKevin Wolf { 135681e7ad0SKevin Wolf error_setg(errp, "host/port/path and a file name may not be specified " 136681e7ad0SKevin Wolf "at the same time"); 137681e7ad0SKevin Wolf return; 138681e7ad0SKevin Wolf } 139681e7ad0SKevin Wolf 1401d7d2a9dSPaolo Bonzini if (strstr(filename, "://")) { 1416963a30dSKevin Wolf int ret = nbd_parse_uri(filename, options); 1426963a30dSKevin Wolf if (ret < 0) { 1436963a30dSKevin Wolf error_setg(errp, "No valid URL specified"); 1446963a30dSKevin Wolf } 1456963a30dSKevin Wolf return; 1461d7d2a9dSPaolo Bonzini } 1471d7d2a9dSPaolo Bonzini 1487267c094SAnthony Liguori file = g_strdup(filename); 1491d45f8b5SLaurent Vivier 15033897dc7SNick Thomas export_name = strstr(file, EN_OPTSTR); 15133897dc7SNick Thomas if (export_name) { 15233897dc7SNick Thomas if (export_name[strlen(EN_OPTSTR)] == 0) { 1531d45f8b5SLaurent Vivier goto out; 1541d45f8b5SLaurent Vivier } 15533897dc7SNick Thomas export_name[0] = 0; /* truncate 'file' */ 15633897dc7SNick Thomas export_name += strlen(EN_OPTSTR); 157f53a1febSKevin Wolf 158f53a1febSKevin Wolf qdict_put(options, "export", qstring_from_str(export_name)); 1591d45f8b5SLaurent Vivier } 1601d45f8b5SLaurent Vivier 16133897dc7SNick Thomas /* extract the host_spec - fail if it's not nbd:... */ 16233897dc7SNick Thomas if (!strstart(file, "nbd:", &host_spec)) { 1636963a30dSKevin Wolf error_setg(errp, "File name string for NBD must start with 'nbd:'"); 1641d45f8b5SLaurent Vivier goto out; 1651d45f8b5SLaurent Vivier } 166019d6b8fSAnthony Liguori 167f53a1febSKevin Wolf if (!*host_spec) { 168f53a1febSKevin Wolf goto out; 169f53a1febSKevin Wolf } 170f53a1febSKevin Wolf 17133897dc7SNick Thomas /* are we a UNIX or TCP socket? */ 17233897dc7SNick Thomas if (strstart(host_spec, "unix:", &unixpath)) { 173f53a1febSKevin Wolf qdict_put(options, "path", qstring_from_str(unixpath)); 174019d6b8fSAnthony Liguori } else { 175f53a1febSKevin Wolf InetSocketAddress *addr = NULL; 176f53a1febSKevin Wolf 1776963a30dSKevin Wolf addr = inet_parse(host_spec, errp); 17892de9012SMarkus Armbruster if (!addr) { 179f17c90beSKevin Wolf goto out; 180f17c90beSKevin Wolf } 181f53a1febSKevin Wolf 182f53a1febSKevin Wolf qdict_put(options, "host", qstring_from_str(addr->host)); 183f53a1febSKevin Wolf qdict_put(options, "port", qstring_from_str(addr->port)); 184f53a1febSKevin Wolf qapi_free_InetSocketAddress(addr); 1851d45f8b5SLaurent Vivier } 1861d45f8b5SLaurent Vivier 1871d45f8b5SLaurent Vivier out: 1887267c094SAnthony Liguori g_free(file); 18933897dc7SNick Thomas } 190f53a1febSKevin Wolf 1917a5ed437SDaniel P. Berrange static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export, 19277e8b9caSPaolo Bonzini Error **errp) 193f53a1febSKevin Wolf { 1947a5ed437SDaniel P. Berrange SocketAddress *saddr; 195f53a1febSKevin Wolf 196a69d9af4SPaolo Bonzini if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) { 197f53a1febSKevin Wolf if (qdict_haskey(options, "path")) { 19877e8b9caSPaolo Bonzini error_setg(errp, "path and host may not be used at the same time."); 199f53a1febSKevin Wolf } else { 20077e8b9caSPaolo Bonzini error_setg(errp, "one of path and host must be specified."); 201a69d9af4SPaolo Bonzini } 2027a5ed437SDaniel P. Berrange return NULL; 203019d6b8fSAnthony Liguori } 204019d6b8fSAnthony Liguori 2057a5ed437SDaniel P. Berrange saddr = g_new0(SocketAddress, 1); 206f53a1febSKevin Wolf 2077a5ed437SDaniel P. Berrange if (qdict_haskey(options, "path")) { 2080399293eSEric Blake UnixSocketAddress *q_unix; 2096a8f9661SEric Blake saddr->type = SOCKET_ADDRESS_KIND_UNIX; 21032bafa8fSEric Blake q_unix = saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1); 2110399293eSEric Blake q_unix->path = g_strdup(qdict_get_str(options, "path")); 2127a5ed437SDaniel P. Berrange qdict_del(options, "path"); 2137a5ed437SDaniel P. Berrange } else { 2140399293eSEric Blake InetSocketAddress *inet; 2156a8f9661SEric Blake saddr->type = SOCKET_ADDRESS_KIND_INET; 21632bafa8fSEric Blake inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1); 2170399293eSEric Blake inet->host = g_strdup(qdict_get_str(options, "host")); 2187a5ed437SDaniel P. Berrange if (!qdict_get_try_str(options, "port")) { 2190399293eSEric Blake inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); 2207a5ed437SDaniel P. Berrange } else { 2210399293eSEric Blake inet->port = g_strdup(qdict_get_str(options, "port")); 2227a5ed437SDaniel P. Berrange } 2237a5ed437SDaniel P. Berrange qdict_del(options, "host"); 2247a5ed437SDaniel P. Berrange qdict_del(options, "port"); 225f53a1febSKevin Wolf } 226f53a1febSKevin Wolf 2276a8f9661SEric Blake s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX; 228bebbf7faSKevin Wolf 229e2bc625fSMarc-André Lureau *export = g_strdup(qdict_get_try_str(options, "export")); 230e2bc625fSMarc-André Lureau if (*export) { 231f53a1febSKevin Wolf qdict_del(options, "export"); 232f53a1febSKevin Wolf } 2337a5ed437SDaniel P. Berrange 2347a5ed437SDaniel P. Berrange return saddr; 235f53a1febSKevin Wolf } 236f53a1febSKevin Wolf 237f53a829bSMax Reitz NbdClientSession *nbd_get_client_session(BlockDriverState *bs) 238f53a829bSMax Reitz { 239f53a829bSMax Reitz BDRVNBDState *s = bs->opaque; 240f53a829bSMax Reitz return &s->client; 241f53a829bSMax Reitz } 242f53a829bSMax Reitz 243064097d9SDaniel P. Berrange static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, 2447a5ed437SDaniel P. Berrange Error **errp) 24533897dc7SNick Thomas { 246064097d9SDaniel P. Berrange QIOChannelSocket *sioc; 247064097d9SDaniel P. Berrange Error *local_err = NULL; 24833897dc7SNick Thomas 249064097d9SDaniel P. Berrange sioc = qio_channel_socket_new(); 25033897dc7SNick Thomas 251064097d9SDaniel P. Berrange qio_channel_socket_connect_sync(sioc, 252064097d9SDaniel P. Berrange saddr, 253064097d9SDaniel P. Berrange &local_err); 254064097d9SDaniel P. Berrange if (local_err) { 255064097d9SDaniel P. Berrange error_propagate(errp, local_err); 256064097d9SDaniel P. Berrange return NULL; 25733897dc7SNick Thomas } 25833897dc7SNick Thomas 259064097d9SDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(sioc), false); 2607a5ed437SDaniel P. Berrange 261064097d9SDaniel P. Berrange return sioc; 26233897dc7SNick Thomas } 26333897dc7SNick Thomas 26475822a12SDaniel P. Berrange 26575822a12SDaniel P. Berrange static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) 26675822a12SDaniel P. Berrange { 26775822a12SDaniel P. Berrange Object *obj; 26875822a12SDaniel P. Berrange QCryptoTLSCreds *creds; 26975822a12SDaniel P. Berrange 27075822a12SDaniel P. Berrange obj = object_resolve_path_component( 27175822a12SDaniel P. Berrange object_get_objects_root(), id); 27275822a12SDaniel P. Berrange if (!obj) { 27375822a12SDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 27475822a12SDaniel P. Berrange id); 27575822a12SDaniel P. Berrange return NULL; 27675822a12SDaniel P. Berrange } 27775822a12SDaniel P. Berrange creds = (QCryptoTLSCreds *) 27875822a12SDaniel P. Berrange object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); 27975822a12SDaniel P. Berrange if (!creds) { 28075822a12SDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 28175822a12SDaniel P. Berrange id); 28275822a12SDaniel P. Berrange return NULL; 28375822a12SDaniel P. Berrange } 28475822a12SDaniel P. Berrange 28575822a12SDaniel P. Berrange if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { 28675822a12SDaniel P. Berrange error_setg(errp, 28775822a12SDaniel P. Berrange "Expecting TLS credentials with a client endpoint"); 28875822a12SDaniel P. Berrange return NULL; 28975822a12SDaniel P. Berrange } 29075822a12SDaniel P. Berrange object_ref(obj); 29175822a12SDaniel P. Berrange return creds; 29275822a12SDaniel P. Berrange } 29375822a12SDaniel P. Berrange 29475822a12SDaniel P. Berrange 295015a1036SMax Reitz static int nbd_open(BlockDriverState *bs, QDict *options, int flags, 296015a1036SMax Reitz Error **errp) 29733897dc7SNick Thomas { 29833897dc7SNick Thomas BDRVNBDState *s = bs->opaque; 299e2bc625fSMarc-André Lureau char *export = NULL; 30075822a12SDaniel P. Berrange QIOChannelSocket *sioc = NULL; 3017a5ed437SDaniel P. Berrange SocketAddress *saddr; 30275822a12SDaniel P. Berrange const char *tlscredsid; 30375822a12SDaniel P. Berrange QCryptoTLSCreds *tlscreds = NULL; 30475822a12SDaniel P. Berrange const char *hostname = NULL; 30575822a12SDaniel P. Berrange int ret = -EINVAL; 306ae255e52SPaolo Bonzini 30733897dc7SNick Thomas /* Pop the config into our state object. Exit if invalid. */ 3087a5ed437SDaniel P. Berrange saddr = nbd_config(s, options, &export, errp); 3097a5ed437SDaniel P. Berrange if (!saddr) { 31075822a12SDaniel P. Berrange goto error; 31175822a12SDaniel P. Berrange } 31275822a12SDaniel P. Berrange 31375822a12SDaniel P. Berrange tlscredsid = g_strdup(qdict_get_try_str(options, "tls-creds")); 31475822a12SDaniel P. Berrange if (tlscredsid) { 31575822a12SDaniel P. Berrange qdict_del(options, "tls-creds"); 31675822a12SDaniel P. Berrange tlscreds = nbd_get_tls_creds(tlscredsid, errp); 31775822a12SDaniel P. Berrange if (!tlscreds) { 31875822a12SDaniel P. Berrange goto error; 31975822a12SDaniel P. Berrange } 32075822a12SDaniel P. Berrange 32175822a12SDaniel P. Berrange if (saddr->type != SOCKET_ADDRESS_KIND_INET) { 32275822a12SDaniel P. Berrange error_setg(errp, "TLS only supported over IP sockets"); 32375822a12SDaniel P. Berrange goto error; 32475822a12SDaniel P. Berrange } 32532bafa8fSEric Blake hostname = saddr->u.inet.data->host; 32633897dc7SNick Thomas } 32733897dc7SNick Thomas 32833897dc7SNick Thomas /* establish TCP connection, return error if it fails 32933897dc7SNick Thomas * TODO: Configurable retry-until-timeout behaviour. 33033897dc7SNick Thomas */ 331064097d9SDaniel P. Berrange sioc = nbd_establish_connection(saddr, errp); 332064097d9SDaniel P. Berrange if (!sioc) { 33375822a12SDaniel P. Berrange ret = -ECONNREFUSED; 33475822a12SDaniel P. Berrange goto error; 33533897dc7SNick Thomas } 33633897dc7SNick Thomas 3372302c1caSMarc-André Lureau /* NBD handshake */ 33875822a12SDaniel P. Berrange ret = nbd_client_init(bs, sioc, export, 33975822a12SDaniel P. Berrange tlscreds, hostname, errp); 34075822a12SDaniel P. Berrange error: 34175822a12SDaniel P. Berrange if (sioc) { 342064097d9SDaniel P. Berrange object_unref(OBJECT(sioc)); 34375822a12SDaniel P. Berrange } 34475822a12SDaniel P. Berrange if (tlscreds) { 34575822a12SDaniel P. Berrange object_unref(OBJECT(tlscreds)); 34675822a12SDaniel P. Berrange } 34775822a12SDaniel P. Berrange qapi_free_SocketAddress(saddr); 348e2bc625fSMarc-André Lureau g_free(export); 34975822a12SDaniel P. Berrange return ret; 350ae255e52SPaolo Bonzini } 351d9b09f13SPaolo Bonzini 3521486d04aSPaolo Bonzini static int nbd_co_flush(BlockDriverState *bs) 3531486d04aSPaolo Bonzini { 354f53a829bSMax Reitz return nbd_client_co_flush(bs); 3551486d04aSPaolo Bonzini } 3561486d04aSPaolo Bonzini 357fa21e6faSDenis V. Lunev static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) 358fa21e6faSDenis V. Lunev { 359b9f7855aSEric Blake bs->bl.max_pdiscard = NBD_MAX_BUFFER_SIZE; 3605def6b80SEric Blake bs->bl.max_transfer = NBD_MAX_BUFFER_SIZE; 361fa21e6faSDenis V. Lunev } 362fa21e6faSDenis V. Lunev 3637a706633SPaolo Bonzini static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num, 3647a706633SPaolo Bonzini int nb_sectors) 3657a706633SPaolo Bonzini { 366f53a829bSMax Reitz return nbd_client_co_discard(bs, sector_num, nb_sectors); 3677a706633SPaolo Bonzini } 3687a706633SPaolo Bonzini 369019d6b8fSAnthony Liguori static void nbd_close(BlockDriverState *bs) 370019d6b8fSAnthony Liguori { 371f53a829bSMax Reitz nbd_client_close(bs); 372019d6b8fSAnthony Liguori } 373019d6b8fSAnthony Liguori 374019d6b8fSAnthony Liguori static int64_t nbd_getlength(BlockDriverState *bs) 375019d6b8fSAnthony Liguori { 376019d6b8fSAnthony Liguori BDRVNBDState *s = bs->opaque; 377019d6b8fSAnthony Liguori 3782302c1caSMarc-André Lureau return s->client.size; 379019d6b8fSAnthony Liguori } 380019d6b8fSAnthony Liguori 38169447cd8SStefan Hajnoczi static void nbd_detach_aio_context(BlockDriverState *bs) 38269447cd8SStefan Hajnoczi { 383f53a829bSMax Reitz nbd_client_detach_aio_context(bs); 38469447cd8SStefan Hajnoczi } 38569447cd8SStefan Hajnoczi 38669447cd8SStefan Hajnoczi static void nbd_attach_aio_context(BlockDriverState *bs, 38769447cd8SStefan Hajnoczi AioContext *new_context) 38869447cd8SStefan Hajnoczi { 389f53a829bSMax Reitz nbd_client_attach_aio_context(bs, new_context); 39069447cd8SStefan Hajnoczi } 39169447cd8SStefan Hajnoczi 3924cdd01d3SKevin Wolf static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) 3932019d68bSMax Reitz { 3942019d68bSMax Reitz QDict *opts = qdict_new(); 3954cdd01d3SKevin Wolf const char *path = qdict_get_try_str(options, "path"); 3964cdd01d3SKevin Wolf const char *host = qdict_get_try_str(options, "host"); 3974cdd01d3SKevin Wolf const char *port = qdict_get_try_str(options, "port"); 3984cdd01d3SKevin Wolf const char *export = qdict_get_try_str(options, "export"); 39975822a12SDaniel P. Berrange const char *tlscreds = qdict_get_try_str(options, "tls-creds"); 4002019d68bSMax Reitz 4012019d68bSMax Reitz qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd"))); 4022019d68bSMax Reitz 403ec0de768SMax Reitz if (path && export) { 404ec0de768SMax Reitz snprintf(bs->exact_filename, sizeof(bs->exact_filename), 405ec0de768SMax Reitz "nbd+unix:///%s?socket=%s", export, path); 406ec0de768SMax Reitz } else if (path && !export) { 407ec0de768SMax Reitz snprintf(bs->exact_filename, sizeof(bs->exact_filename), 408ec0de768SMax Reitz "nbd+unix://?socket=%s", path); 409ec0de768SMax Reitz } else if (!path && export && port) { 410ec0de768SMax Reitz snprintf(bs->exact_filename, sizeof(bs->exact_filename), 411ec0de768SMax Reitz "nbd://%s:%s/%s", host, port, export); 412ec0de768SMax Reitz } else if (!path && export && !port) { 413ec0de768SMax Reitz snprintf(bs->exact_filename, sizeof(bs->exact_filename), 414ec0de768SMax Reitz "nbd://%s/%s", host, export); 415ec0de768SMax Reitz } else if (!path && !export && port) { 416ec0de768SMax Reitz snprintf(bs->exact_filename, sizeof(bs->exact_filename), 417ec0de768SMax Reitz "nbd://%s:%s", host, port); 418ec0de768SMax Reitz } else if (!path && !export && !port) { 419ec0de768SMax Reitz snprintf(bs->exact_filename, sizeof(bs->exact_filename), 420ec0de768SMax Reitz "nbd://%s", host); 421ec0de768SMax Reitz } 422ec0de768SMax Reitz 4232019d68bSMax Reitz if (path) { 4242019d68bSMax Reitz qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path))); 425ec0de768SMax Reitz } else if (port) { 4262019d68bSMax Reitz qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host))); 4272019d68bSMax Reitz qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port))); 4282019d68bSMax Reitz } else { 4292019d68bSMax Reitz qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host))); 430ec0de768SMax Reitz } 431ec0de768SMax Reitz if (export) { 432ec0de768SMax Reitz qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export))); 4332019d68bSMax Reitz } 43475822a12SDaniel P. Berrange if (tlscreds) { 43575822a12SDaniel P. Berrange qdict_put_obj(opts, "tls-creds", QOBJECT(qstring_from_str(tlscreds))); 43675822a12SDaniel P. Berrange } 4372019d68bSMax Reitz 4382019d68bSMax Reitz bs->full_open_options = opts; 4392019d68bSMax Reitz } 4402019d68bSMax Reitz 441019d6b8fSAnthony Liguori static BlockDriver bdrv_nbd = { 442019d6b8fSAnthony Liguori .format_name = "nbd", 4431d7d2a9dSPaolo Bonzini .protocol_name = "nbd", 444019d6b8fSAnthony Liguori .instance_size = sizeof(BDRVNBDState), 4456963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 44666f82ceeSKevin Wolf .bdrv_file_open = nbd_open, 447*fb1a6de1SEric Blake .bdrv_co_readv = nbd_client_co_readv, 44852a46505SEric Blake .bdrv_co_writev_flags = nbd_client_co_writev, 449019d6b8fSAnthony Liguori .bdrv_close = nbd_close, 4501486d04aSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 4517a706633SPaolo Bonzini .bdrv_co_discard = nbd_co_discard, 452fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 453019d6b8fSAnthony Liguori .bdrv_getlength = nbd_getlength, 45469447cd8SStefan Hajnoczi .bdrv_detach_aio_context = nbd_detach_aio_context, 45569447cd8SStefan Hajnoczi .bdrv_attach_aio_context = nbd_attach_aio_context, 4562019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 4571d7d2a9dSPaolo Bonzini }; 4581d7d2a9dSPaolo Bonzini 4591d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_tcp = { 4601d7d2a9dSPaolo Bonzini .format_name = "nbd", 4611d7d2a9dSPaolo Bonzini .protocol_name = "nbd+tcp", 4621d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 4636963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 4641d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 465*fb1a6de1SEric Blake .bdrv_co_readv = nbd_client_co_readv, 46652a46505SEric Blake .bdrv_co_writev_flags = nbd_client_co_writev, 4671d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 4681d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 4691d7d2a9dSPaolo Bonzini .bdrv_co_discard = nbd_co_discard, 470fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 4711d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 47269447cd8SStefan Hajnoczi .bdrv_detach_aio_context = nbd_detach_aio_context, 47369447cd8SStefan Hajnoczi .bdrv_attach_aio_context = nbd_attach_aio_context, 4742019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 4751d7d2a9dSPaolo Bonzini }; 4761d7d2a9dSPaolo Bonzini 4771d7d2a9dSPaolo Bonzini static BlockDriver bdrv_nbd_unix = { 4781d7d2a9dSPaolo Bonzini .format_name = "nbd", 4791d7d2a9dSPaolo Bonzini .protocol_name = "nbd+unix", 4801d7d2a9dSPaolo Bonzini .instance_size = sizeof(BDRVNBDState), 4816963a30dSKevin Wolf .bdrv_parse_filename = nbd_parse_filename, 4821d7d2a9dSPaolo Bonzini .bdrv_file_open = nbd_open, 483*fb1a6de1SEric Blake .bdrv_co_readv = nbd_client_co_readv, 48452a46505SEric Blake .bdrv_co_writev_flags = nbd_client_co_writev, 4851d7d2a9dSPaolo Bonzini .bdrv_close = nbd_close, 4861d7d2a9dSPaolo Bonzini .bdrv_co_flush_to_os = nbd_co_flush, 4871d7d2a9dSPaolo Bonzini .bdrv_co_discard = nbd_co_discard, 488fa21e6faSDenis V. Lunev .bdrv_refresh_limits = nbd_refresh_limits, 4891d7d2a9dSPaolo Bonzini .bdrv_getlength = nbd_getlength, 49069447cd8SStefan Hajnoczi .bdrv_detach_aio_context = nbd_detach_aio_context, 49169447cd8SStefan Hajnoczi .bdrv_attach_aio_context = nbd_attach_aio_context, 4922019d68bSMax Reitz .bdrv_refresh_filename = nbd_refresh_filename, 493019d6b8fSAnthony Liguori }; 494019d6b8fSAnthony Liguori 495019d6b8fSAnthony Liguori static void bdrv_nbd_init(void) 496019d6b8fSAnthony Liguori { 497019d6b8fSAnthony Liguori bdrv_register(&bdrv_nbd); 4981d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_tcp); 4991d7d2a9dSPaolo Bonzini bdrv_register(&bdrv_nbd_unix); 500019d6b8fSAnthony Liguori } 501019d6b8fSAnthony Liguori 502019d6b8fSAnthony Liguori block_init(bdrv_nbd_init); 503