Skip to content

Commit c4e2aff

Browse files
XanClicebblake
authored andcommitted
qemu-nbd: Look up flag names in array
The existing code to convert flag bits into strings looks a bit strange now, and if we ever add more flags, it will look even stranger. Prevent that from happening by making it look up the flag names in an array. Signed-off-by: Max Reitz <[email protected]> Message-Id: <[email protected]> Signed-off-by: Eric Blake <[email protected]>
1 parent 19eb2d4 commit c4e2aff

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

include/block/nbd.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,32 @@ typedef struct NBDExtent {
127127

128128
/* Transmission (export) flags: sent from server to client during handshake,
129129
but describe what will happen during transmission */
130-
#define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
131-
#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
132-
#define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
133-
#define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */
134-
#define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm -
135-
rotational media */
136-
#define NBD_FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */
137-
#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6) /* Send WRITE_ZEROES */
138-
#define NBD_FLAG_SEND_DF (1 << 7) /* Send DF (Do not Fragment) */
139-
#define NBD_FLAG_CAN_MULTI_CONN (1 << 8) /* Multi-client cache consistent */
140-
#define NBD_FLAG_SEND_RESIZE (1 << 9) /* Send resize */
141-
#define NBD_FLAG_SEND_CACHE (1 << 10) /* Send CACHE (prefetch) */
130+
enum {
131+
NBD_FLAG_HAS_FLAGS_BIT = 0, /* Flags are there */
132+
NBD_FLAG_READ_ONLY_BIT = 1, /* Device is read-only */
133+
NBD_FLAG_SEND_FLUSH_BIT = 2, /* Send FLUSH */
134+
NBD_FLAG_SEND_FUA_BIT = 3, /* Send FUA (Force Unit Access) */
135+
NBD_FLAG_ROTATIONAL_BIT = 4, /* Use elevator algorithm -
136+
rotational media */
137+
NBD_FLAG_SEND_TRIM_BIT = 5, /* Send TRIM (discard) */
138+
NBD_FLAG_SEND_WRITE_ZEROES_BIT = 6, /* Send WRITE_ZEROES */
139+
NBD_FLAG_SEND_DF_BIT = 7, /* Send DF (Do not Fragment) */
140+
NBD_FLAG_CAN_MULTI_CONN_BIT = 8, /* Multi-client cache consistent */
141+
NBD_FLAG_SEND_RESIZE_BIT = 9, /* Send resize */
142+
NBD_FLAG_SEND_CACHE_BIT = 10, /* Send CACHE (prefetch) */
143+
};
144+
145+
#define NBD_FLAG_HAS_FLAGS (1 << NBD_FLAG_HAS_FLAGS_BIT)
146+
#define NBD_FLAG_READ_ONLY (1 << NBD_FLAG_READ_ONLY_BIT)
147+
#define NBD_FLAG_SEND_FLUSH (1 << NBD_FLAG_SEND_FLUSH_BIT)
148+
#define NBD_FLAG_SEND_FUA (1 << NBD_FLAG_SEND_FUA_BIT)
149+
#define NBD_FLAG_ROTATIONAL (1 << NBD_FLAG_ROTATIONAL_BIT)
150+
#define NBD_FLAG_SEND_TRIM (1 << NBD_FLAG_SEND_TRIM_BIT)
151+
#define NBD_FLAG_SEND_WRITE_ZEROES (1 << NBD_FLAG_SEND_WRITE_ZEROES_BIT)
152+
#define NBD_FLAG_SEND_DF (1 << NBD_FLAG_SEND_DF_BIT)
153+
#define NBD_FLAG_CAN_MULTI_CONN (1 << NBD_FLAG_CAN_MULTI_CONN_BIT)
154+
#define NBD_FLAG_SEND_RESIZE (1 << NBD_FLAG_SEND_RESIZE_BIT)
155+
#define NBD_FLAG_SEND_CACHE (1 << NBD_FLAG_SEND_CACHE_BIT)
142156

143157
/* New-style handshake (global) flags, sent from server to client, and
144158
control what will happen during handshake phase. */

qemu-nbd.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -279,37 +279,25 @@ static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
279279
printf(" description: %s\n", list[i].description);
280280
}
281281
if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
282+
static const char *const flag_names[] = {
283+
[NBD_FLAG_READ_ONLY_BIT] = "readonly",
284+
[NBD_FLAG_SEND_FLUSH_BIT] = "flush",
285+
[NBD_FLAG_SEND_FUA_BIT] = "fua",
286+
[NBD_FLAG_ROTATIONAL_BIT] = "rotational",
287+
[NBD_FLAG_SEND_TRIM_BIT] = "trim",
288+
[NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
289+
[NBD_FLAG_SEND_DF_BIT] = "df",
290+
[NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
291+
[NBD_FLAG_SEND_RESIZE_BIT] = "resize",
292+
[NBD_FLAG_SEND_CACHE_BIT] = "cache",
293+
};
294+
282295
printf(" size: %" PRIu64 "\n", list[i].size);
283296
printf(" flags: 0x%x (", list[i].flags);
284-
if (list[i].flags & NBD_FLAG_READ_ONLY) {
285-
printf(" readonly");
286-
}
287-
if (list[i].flags & NBD_FLAG_SEND_FLUSH) {
288-
printf(" flush");
289-
}
290-
if (list[i].flags & NBD_FLAG_SEND_FUA) {
291-
printf(" fua");
292-
}
293-
if (list[i].flags & NBD_FLAG_ROTATIONAL) {
294-
printf(" rotational");
295-
}
296-
if (list[i].flags & NBD_FLAG_SEND_TRIM) {
297-
printf(" trim");
298-
}
299-
if (list[i].flags & NBD_FLAG_SEND_WRITE_ZEROES) {
300-
printf(" zeroes");
301-
}
302-
if (list[i].flags & NBD_FLAG_SEND_DF) {
303-
printf(" df");
304-
}
305-
if (list[i].flags & NBD_FLAG_CAN_MULTI_CONN) {
306-
printf(" multi");
307-
}
308-
if (list[i].flags & NBD_FLAG_SEND_RESIZE) {
309-
printf(" resize");
310-
}
311-
if (list[i].flags & NBD_FLAG_SEND_CACHE) {
312-
printf(" cache");
297+
for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
298+
if (flag_names[bit] && (list[i].flags & (1 << bit))) {
299+
printf(" %s", flag_names[bit]);
300+
}
313301
}
314302
printf(" )\n");
315303
}

0 commit comments

Comments
 (0)