Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
info: Keep request within 4G bound
Otherwise, we get a failure due to Numerical result out of range.

And for safety's sake, we are best capping our request to an aligned
value, if the server insists on minimum alignment.

Maybe nbdinfo should consolidate adjacent lines with identical status,
but that's a different patch.

Fixes: f3fd935
  • Loading branch information
ebblake committed Oct 17, 2020
1 parent e1c008d commit 46072f6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions info/Makefile.am
Expand Up @@ -31,6 +31,7 @@ info_sh_files = \
info-description-qemu.sh \
info-map-base-allocation.sh \
info-map-base-allocation-json.sh \
info-map-base-allocation-large.sh \
info-map-qemu-dirty-bitmap.sh \
info-atomic-output.sh \
$(NULL)
Expand Down
50 changes: 50 additions & 0 deletions info/info-map-base-allocation-large.sh
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# nbd client library in userspace
# Copyright (C) 2020 Red Hat Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

. ../tests/functions.sh

set -e
set -x

requires nbdkit --version
requires nbdsh --version
requires tr --version

out=info-base-allocation.out
cleanup_fn rm -f $out
rm -f $out

# Note the memory plugin uses a 32K page size, and extents
# are always aligned with this.
nbdkit -U - memory 6G --run '
nbdsh -u "$uri" \
-c "h.pwrite(b\"\\x01\"*131072, 0)" \
-c "h.pwrite(b\"\\x02\"*131072, 320*1024)" &&
$VG nbdinfo --map "$uri"
' > $out

cat $out

if [ "$(tr -s ' ' < $out)" != " 0 131072 0 allocated
131072 196608 3 hole,zero
327680 131072 0 allocated
458752 4294508544 3 hole,zero
4294967296 2147483648 3 hole,zero" ]; then
echo "$0: unexpected output from nbdinfo --map"
exit 1
fi
8 changes: 6 additions & 2 deletions info/nbdinfo.c
Expand Up @@ -31,6 +31,8 @@

#include <libnbd.h>

#define MIN(a,b) ((a) < (b) ? (a) : (b))

static const char *progname;
static FILE *fp;
static bool list_all = false;
Expand Down Expand Up @@ -267,7 +269,7 @@ main (int argc, char *argv[])
fprintf (fp, "%" PRIi64 "\n", size);
}
else if (map) { /* --map (!list_all) */
uint64_t offset, prev_offset;
uint64_t offset, prev_offset, align, max_len;

/* Did we get the requested map? */
if (!nbd_can_meta_context (nbd, map)) {
Expand All @@ -276,6 +278,8 @@ main (int argc, char *argv[])
progname, map);
exit (EXIT_FAILURE);
}
align = nbd_get_block_size (nbd, LIBNBD_SIZE_MINIMUM) ?: 512;
max_len = UINT32_MAX - align + 1;

size = nbd_get_size (nbd);
if (size == -1) {
Expand All @@ -286,7 +290,7 @@ main (int argc, char *argv[])
if (json_output) fprintf (fp, "[\n");
for (offset = 0; offset < size;) {
prev_offset = offset;
if (nbd_block_status (nbd, size - offset, offset,
if (nbd_block_status (nbd, MIN (size - offset, max_len), offset,
(nbd_extent_callback) { .callback = extent_callback,
.user_data = &offset },
0) == -1) {
Expand Down

0 comments on commit 46072f6

Please sign in to comment.