2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device Common Code
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "nbd-internal.h"
23 /* Discard length bytes from channel. Return -errno on failure and 0 on
25 int nbd_drop(QIOChannel
*ioc
, size_t size
, Error
**errp
)
31 buffer
= sizeof(small
) >= size
? small
: g_malloc(MIN(65536, size
));
33 ssize_t count
= MIN(65536, size
);
34 ret
= nbd_read(ioc
, buffer
, MIN(65536, size
), NULL
, errp
);
43 if (buffer
!= small
) {
50 void nbd_tls_handshake(QIOTask
*task
,
53 struct NBDTLSHandshakeData
*data
= opaque
;
55 qio_task_propagate_error(task
, &data
->error
);
56 data
->complete
= true;
57 g_main_loop_quit(data
->loop
);
61 const char *nbd_opt_lookup(uint32_t opt
)
64 case NBD_OPT_EXPORT_NAME
:
70 case NBD_OPT_STARTTLS
:
76 case NBD_OPT_STRUCTURED_REPLY
:
77 return "structured reply";
78 case NBD_OPT_LIST_META_CONTEXT
:
79 return "list meta context";
80 case NBD_OPT_SET_META_CONTEXT
:
81 return "set meta context";
82 case NBD_OPT_EXTENDED_HEADERS
:
83 return "extended headers";
90 const char *nbd_rep_lookup(uint32_t rep
)
99 case NBD_REP_META_CONTEXT
:
100 return "meta context";
101 case NBD_REP_ERR_UNSUP
:
102 return "unsupported";
103 case NBD_REP_ERR_POLICY
:
104 return "denied by policy";
105 case NBD_REP_ERR_INVALID
:
107 case NBD_REP_ERR_PLATFORM
:
108 return "platform lacks support";
109 case NBD_REP_ERR_TLS_REQD
:
110 return "TLS required";
111 case NBD_REP_ERR_UNKNOWN
:
112 return "export unknown";
113 case NBD_REP_ERR_SHUTDOWN
:
114 return "server shutting down";
115 case NBD_REP_ERR_BLOCK_SIZE_REQD
:
116 return "block size required";
117 case NBD_REP_ERR_TOO_BIG
:
118 return "option payload too big";
119 case NBD_REP_ERR_EXT_HEADER_REQD
:
120 return "extended headers required";
127 const char *nbd_info_lookup(uint16_t info
)
130 case NBD_INFO_EXPORT
:
134 case NBD_INFO_DESCRIPTION
:
135 return "description";
136 case NBD_INFO_BLOCK_SIZE
:
144 const char *nbd_cmd_lookup(uint16_t cmd
)
159 case NBD_CMD_WRITE_ZEROES
:
160 return "write zeroes";
161 case NBD_CMD_BLOCK_STATUS
:
162 return "block status";
169 const char *nbd_reply_type_lookup(uint16_t type
)
172 case NBD_REPLY_TYPE_NONE
:
174 case NBD_REPLY_TYPE_OFFSET_DATA
:
176 case NBD_REPLY_TYPE_OFFSET_HOLE
:
178 case NBD_REPLY_TYPE_BLOCK_STATUS
:
179 return "block status (32-bit)";
180 case NBD_REPLY_TYPE_BLOCK_STATUS_EXT
:
181 return "block status (64-bit)";
182 case NBD_REPLY_TYPE_ERROR
:
183 return "generic error";
184 case NBD_REPLY_TYPE_ERROR_OFFSET
:
185 return "error at offset";
187 if (type
& (1 << 15)) {
188 return "<unknown error>";
195 const char *nbd_err_lookup(int err
)
222 int nbd_errno_to_system_errno(int err
)
251 trace_nbd_unknown_error(err
);
261 const char *nbd_mode_lookup(NBDMode mode
)
264 case NBD_MODE_OLDSTYLE
:
266 case NBD_MODE_EXPORT_NAME
:
267 return "export name only";
268 case NBD_MODE_SIMPLE
:
269 return "simple headers";
270 case NBD_MODE_STRUCTURED
:
271 return "structured replies";
272 case NBD_MODE_EXTENDED
:
273 return "extended headers";