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 const char *nbd_opt_lookup(uint32_t opt
)
53 case NBD_OPT_EXPORT_NAME
:
59 case NBD_OPT_STARTTLS
:
65 case NBD_OPT_STRUCTURED_REPLY
:
66 return "structured reply";
67 case NBD_OPT_LIST_META_CONTEXT
:
68 return "list meta context";
69 case NBD_OPT_SET_META_CONTEXT
:
70 return "set meta context";
71 case NBD_OPT_EXTENDED_HEADERS
:
72 return "extended headers";
79 const char *nbd_rep_lookup(uint32_t rep
)
88 case NBD_REP_META_CONTEXT
:
89 return "meta context";
90 case NBD_REP_ERR_UNSUP
:
92 case NBD_REP_ERR_POLICY
:
93 return "denied by policy";
94 case NBD_REP_ERR_INVALID
:
96 case NBD_REP_ERR_PLATFORM
:
97 return "platform lacks support";
98 case NBD_REP_ERR_TLS_REQD
:
99 return "TLS required";
100 case NBD_REP_ERR_UNKNOWN
:
101 return "export unknown";
102 case NBD_REP_ERR_SHUTDOWN
:
103 return "server shutting down";
104 case NBD_REP_ERR_BLOCK_SIZE_REQD
:
105 return "block size required";
106 case NBD_REP_ERR_TOO_BIG
:
107 return "option payload too big";
108 case NBD_REP_ERR_EXT_HEADER_REQD
:
109 return "extended headers required";
116 const char *nbd_info_lookup(uint16_t info
)
119 case NBD_INFO_EXPORT
:
123 case NBD_INFO_DESCRIPTION
:
124 return "description";
125 case NBD_INFO_BLOCK_SIZE
:
133 const char *nbd_cmd_lookup(uint16_t cmd
)
148 case NBD_CMD_WRITE_ZEROES
:
149 return "write zeroes";
150 case NBD_CMD_BLOCK_STATUS
:
151 return "block status";
158 const char *nbd_reply_type_lookup(uint16_t type
)
161 case NBD_REPLY_TYPE_NONE
:
163 case NBD_REPLY_TYPE_OFFSET_DATA
:
165 case NBD_REPLY_TYPE_OFFSET_HOLE
:
167 case NBD_REPLY_TYPE_BLOCK_STATUS
:
168 return "block status (32-bit)";
169 case NBD_REPLY_TYPE_BLOCK_STATUS_EXT
:
170 return "block status (64-bit)";
171 case NBD_REPLY_TYPE_ERROR
:
172 return "generic error";
173 case NBD_REPLY_TYPE_ERROR_OFFSET
:
174 return "error at offset";
176 if (type
& (1 << 15)) {
177 return "<unknown error>";
184 const char *nbd_err_lookup(int err
)
211 int nbd_errno_to_system_errno(int err
)
240 trace_nbd_unknown_error(err
);
250 const char *nbd_mode_lookup(NBDMode mode
)
253 case NBD_MODE_OLDSTYLE
:
255 case NBD_MODE_EXPORT_NAME
:
256 return "export name only";
257 case NBD_MODE_SIMPLE
:
258 return "simple headers";
259 case NBD_MODE_STRUCTURED
:
260 return "structured replies";
261 case NBD_MODE_EXTENDED
:
262 return "extended headers";