All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Cc: "nsoffer@redhat.com" <nsoffer@redhat.com>,
	"rjones@redhat.com" <rjones@redhat.com>,
	"jsnow@redhat.com" <jsnow@redhat.com>,
	"qemu-block@nongnu.org" <qemu-block@nongnu.org>,
	Kevin Wolf <kwolf@redhat.com>, Max Reitz <mreitz@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v3 05/19] nbd/server: Favor [u]int64_t over off_t
Date: Tue, 15 Jan 2019 09:33:57 -0600	[thread overview]
Message-ID: <f42d51fa-1268-0abf-11b8-7661655e995d@redhat.com> (raw)
In-Reply-To: <acc77355-221d-0497-4b14-6b3c1bb79c7d@virtuozzo.com>

[-- Attachment #1: Type: text/plain, Size: 6414 bytes --]

On 1/15/19 4:19 AM, Vladimir Sementsov-Ogievskiy wrote:
> 12.01.2019 20:57, Eric Blake wrote:
>> Although our compile-time environment is set up so that we always
>> support long files with 64-bit off_t, we have no guarantee whether
>> off_t is the same type as int64_t.  This requires casts when
>> printing values, and prevents us from directly using qemu_strtoi64().
>> Let's just flip to [u]int64_t (signed for length, because we have to
>> detect failure of blk_getlength()
> 
> we have not, after previous patch

nbd/server.c no longer has to check for blk_getlength() failures, but
blockdev-nbd.c and qemu-nbd.c still do.  Since the callers have to use
an int64_t type for the length as part of their error checking, it's
easier to accept an int64_t length to nbd_export_new(), even if
nbd_export_new() could also use an unsigned type.

> 
> and because off_t was signed;
>> unsigned for offset because it lets us simplify some math without
>> having to worry about signed overflow).
>>
>> Suggested-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>>
>> ---
>> v3: new patch
>> ---
>>   include/block/nbd.h |  4 ++--
>>   nbd/server.c        | 14 +++++++-------
>>   qemu-nbd.c          | 26 ++++++++++----------------
>>   3 files changed, 19 insertions(+), 25 deletions(-)
>>
>> diff --git a/include/block/nbd.h b/include/block/nbd.h
>> index 1971b557896..0f252829376 100644
>> --- a/include/block/nbd.h
>> +++ b/include/block/nbd.h
>> @@ -294,8 +294,8 @@ int nbd_errno_to_system_errno(int err);
>>   typedef struct NBDExport NBDExport;
>>   typedef struct NBDClient NBDClient;
>>
>> -NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
>> -                          const char *name, const char *description,
>> +NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
>> +                          int64_t size, const char *name, const char *desc,
> 
> in previous patch you drop use of negative @size parameter, so it looks better
> to use unsigned for size like for offset

You can't have a size larger than 2^63; but an unsigned type holds
nearly 2^64.  I prefer a signed size, for the same reason that off_t is
signed, in that checking for a negative size is easier to rule out sizes
that are too large.


>>
>>   static int find_partition(BlockBackend *blk, int partition,
>> -                          off_t *offset, off_t *size)
>> +                          uint64_t *offset, int64_t *size)
> 
> function never return negative @size, so what is the reason to keep it signed?

Because the C compiler does NOT like:

int64_t len;
find_partition(..., &len);

with a uint64_t* parameter type - you HAVE to match the signed-ness of
your caller's parameter with your pointer type. Since the caller already
has to use a signed type (to check for blk_getlength() failure AND
because sizes really cannot exceed 2^63), it's easier to keep it signed
here.

> 
> Also, type conversion (uint64_t) should be dropped from the function code I think.

Are you talking about this part:

                if ((ext_partnum + j + 1) == partition) {
                    *offset = (uint64_t)ext[j].start_sector_abs << 9;
                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;
                    return 0;
                }
            }
            ext_partnum += 4;
        } else if ((i + 1) == partition) {
            *offset = (uint64_t)mbr[i].start_sector_abs << 9;
            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
            return 0;

No - that has to keep the cast, because .start_sector_abs and
.nb_sectors_abs are uint32_t values, but we want to shift into 64-bit
results.  You need the cast to force the correct arithmetic rather than
truncating into a 32-bit value that then gets widened into 64-bit storage.

>> @@ -665,10 +665,6 @@ int main(int argc, char **argv)
>>                   error_report("Invalid offset `%s'", optarg);
>>                   exit(EXIT_FAILURE);
>>               }
>> -            if (dev_offset < 0) {
>> -                error_report("Offset must be positive `%s'", optarg);
>> -                exit(EXIT_FAILURE);
>> -            }
> 
> hm, then, may be, s/strtoll/strtoull before this?

I clean that up in patch 6/19.

> 
>>               break;
>>           case 'l':
>>               if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
>> @@ -1005,15 +1001,14 @@ int main(int argc, char **argv)
>>       }
>>
>>       if (dev_offset >= fd_size) {
>> -        error_report("Offset (%lld) has to be smaller than the image size "
>> -                     "(%lld)",
>> -                     (long long int)dev_offset, (long long int)fd_size);
>> +        error_report("Offset (%" PRIu64 ") has to be smaller than the image "
>> +                     "size (%" PRIu64 ")", dev_offset, fd_size);
> 
> PRId64 for fd_size

Sure.


>> @@ -1026,12 +1021,11 @@ int main(int argc, char **argv)
>>               exit(EXIT_FAILURE);
>>           }
>>           /* partition limits are (32-bit << 9); can't overflow 64 bits */
>> -        assert(dev_offset >= 0 && dev_offset + limit >= dev_offset);
>> +        assert(dev_offset + limit >= dev_offset);
>>           if (dev_offset + limit > fd_size) {
>> -            error_report("Discovered partition %d at offset %lld size %lld, "
>> -                         "but size exceeds file length %lld", partition,
>> -                         (long long int) dev_offset, (long long int) limit,
>> -                         (long long int) fd_size);
>> +            error_report("Discovered partition %d at offset %" PRIu64
>> +                         " size %" PRId64 ", but size exceeds file length %"
>> +                         PRId64, partition, dev_offset, limit, fd_size);
>>               exit(EXIT_FAILURE);
> 
> hmm, it may be better to place this patch before [03], to squash this chunk into [03]

I didn't mind the churn; also, I prefer patch 3 first, because it's more
likely to get backported as a bug fix than the rest of the series (and
the earlier you stick backport candidates in a series, the easier it is
to backport).

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 484 bytes --]

  reply	other threads:[~2019-01-15 15:34 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-12 17:57 [Qemu-devel] [PATCH v3 00/19] nbd: add qemu-nbd --list Eric Blake
2019-01-12 17:57 ` [Qemu-devel] [PATCH v3 01/19] maint: Allow for EXAMPLES in texi2pod Eric Blake
2019-01-15 17:51   ` Richard W.M. Jones
2019-01-12 17:57 ` [Qemu-devel] [PATCH v3 02/19] qemu-nbd: Enhance man page Eric Blake
2019-01-15 17:53   ` Richard W.M. Jones
2019-01-12 17:57 ` [Qemu-devel] [PATCH v3 03/19] qemu-nbd: Sanity check partition bounds Eric Blake
2019-01-15 16:20   ` Vladimir Sementsov-Ogievskiy
2019-01-15 16:53     ` Eric Blake
2019-01-15 18:00   ` Richard W.M. Jones
2019-01-15 18:08     ` Eric Blake
2019-01-16  7:46   ` Vladimir Sementsov-Ogievskiy
2019-01-12 17:57 ` [Qemu-devel] [PATCH v3 04/19] nbd/server: Hoist length check to qemp_nbd_server_add Eric Blake
2019-01-15  9:44   ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:25     ` Eric Blake
2019-01-15 16:26       ` Vladimir Sementsov-Ogievskiy
2019-01-15 16:58         ` Eric Blake
2019-01-16 18:03           ` Eric Blake
2019-01-16 18:05   ` Eric Blake
2019-01-12 17:57 ` [Qemu-devel] [PATCH v3 05/19] nbd/server: Favor [u]int64_t over off_t Eric Blake
2019-01-15 10:19   ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:33     ` Eric Blake [this message]
2019-01-15 15:41       ` Vladimir Sementsov-Ogievskiy
2019-01-16  8:23       ` Vladimir Sementsov-Ogievskiy
2019-01-16 14:23         ` Eric Blake
2019-01-12 17:57 ` [Qemu-devel] [PATCH v3 06/19] qemu-nbd: Avoid strtol open-coding Eric Blake
2019-01-15 12:31   ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:35     ` Eric Blake
2019-01-15 18:09   ` Richard W.M. Jones
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 07/19] nbd/client: Refactor nbd_receive_list() Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 08/19] nbd/client: Move export name into NBDExportInfo Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 09/19] nbd/client: Change signature of nbd_negotiate_simple_meta_context() Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 10/19] nbd/client: Split out nbd_send_one_meta_context() Eric Blake
2019-01-15 13:18   ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:44     ` Eric Blake
2019-01-15 15:52       ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:55         ` Eric Blake
2019-01-15 15:59           ` Vladimir Sementsov-Ogievskiy
2019-01-16 10:40       ` Vladimir Sementsov-Ogievskiy
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 11/19] nbd/client: Split out nbd_receive_one_meta_context() Eric Blake
2019-01-15 15:05   ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:50     ` Eric Blake
2019-01-15 15:53       ` Vladimir Sementsov-Ogievskiy
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 12/19] nbd/client: Refactor return of nbd_receive_negotiate() Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 13/19] nbd/client: Split handshake into two functions Eric Blake
2019-01-15 15:34   ` Vladimir Sementsov-Ogievskiy
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 14/19] nbd/client: Pull out oldstyle size determination Eric Blake
2019-01-15 15:35   ` Vladimir Sementsov-Ogievskiy
2019-01-15 15:45     ` Vladimir Sementsov-Ogievskiy
2019-01-16 19:47     ` Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 15/19] nbd/client: Add nbd_receive_export_list() Eric Blake
2019-01-16 10:15   ` Vladimir Sementsov-Ogievskiy
2019-01-16 14:33     ` Eric Blake
2019-01-16 20:01     ` Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 16/19] nbd/client: Add meta contexts to nbd_receive_export_list() Eric Blake
2019-01-16 10:54   ` Vladimir Sementsov-Ogievskiy
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 17/19] qemu-nbd: Add --list option Eric Blake
2019-01-17 10:05   ` Vladimir Sementsov-Ogievskiy
2019-01-17 16:58     ` Eric Blake
2019-01-17 17:11       ` Vladimir Sementsov-Ogievskiy
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 18/19] nbd/client: Work around 3.0 bug for listing meta contexts Eric Blake
2019-01-16 15:43   ` Vladimir Sementsov-Ogievskiy
2019-01-17  3:21     ` Eric Blake
2019-01-17  8:07       ` Vladimir Sementsov-Ogievskiy
2019-01-17 14:20         ` Eric Blake
2019-01-12 17:58 ` [Qemu-devel] [PATCH v3 19/19] iotests: Enhance 223, 233 to cover 'qemu-nbd --list' Eric Blake
2019-01-17 13:34   ` Eric Blake
2019-01-14 12:22 ` [Qemu-devel] [PATCH v3 00/19] nbd: add qemu-nbd --list Vladimir Sementsov-Ogievskiy
2019-01-14 16:46   ` Eric Blake
2019-01-17 11:38 ` Vladimir Sementsov-Ogievskiy
2019-01-17 14:20   ` Eric Blake
2019-01-23 12:36 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f42d51fa-1268-0abf-11b8-7661655e995d@redhat.com \
    --to=eblake@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=nsoffer@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.