qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Sam Li <faithilikerun@gmail.com>
To: Hannes Reinecke <hare@suse.de>
Cc: qemu-devel <qemu-devel@nongnu.org>,
	Hanna Reitz <hreitz@redhat.com>,
	 Dmitry Fomichev <dmitry.fomichev@wdc.com>,
	Kevin Wolf <kwolf@redhat.com>, Fam Zheng <fam@euphon.net>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Damien Le Moal <damien.lemoal@opensource.wdc.com>,
	 qemu block <qemu-block@nongnu.org>
Subject: Re: [RFC v3 4/5] file-posix: introduce get_sysfs_str_val for device zoned model.
Date: Mon, 27 Jun 2022 16:44:44 +0800	[thread overview]
Message-ID: <CAAAx-8KO_beNZ2i=3smsfGs67f28TT7puYFGbMbgxmr5OO3-WA@mail.gmail.com> (raw)
In-Reply-To: <b9fab319-aaef-9f4e-090e-b10fa010eb5d@suse.de>

Hannes Reinecke <hare@suse.de> 于2022年6月27日周一 15:41写道:
>
> On 6/27/22 02:19, Sam Li wrote:
> > ---
> >   block/file-posix.c           | 60 ++++++++++++++++++++++++++++++++++++
> >   include/block/block-common.h |  4 +--
> >   2 files changed, 62 insertions(+), 2 deletions(-)
> >
> > diff --git a/block/file-posix.c b/block/file-posix.c
> > index 73c2cdfbca..74c0245e0f 100644
> > --- a/block/file-posix.c
> > +++ b/block/file-posix.c
> > @@ -1277,6 +1277,66 @@ out:
> >   #endif
> >   }
> >
> > +/*
> > + * Convert the zoned attribute file in sysfs to internal value.
> > + */
> > +static zone_model get_sysfs_str_val(int fd, struct stat *st) {
> > +#ifdef CONFIG_LINUX
> > +    char buf[32];
> > +    char *sysfspath = NULL;
> > +    int ret;
> > +    int sysfd = -1;
> > +
> > +    if (S_ISCHR(st->st_mode)) {
> > +        if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
> > +            return ret;
> > +        }
> > +        return -ENOTSUP;
> > +    }
> > +
> > +    if (!S_ISBLK(st->st_mode)) {
> > +        return -ENOTSUP;
> > +    }
> > +
> > +    sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/zoned",
> > +                                major(st->st_rdev), minor(st->st_rdev));
> > +    sysfd = open(sysfspath, O_RDONLY);
> > +    if (sysfd == -1) {
> > +        ret = -errno;
> > +        goto out;
> > +    }
> > +    do {
> > +        ret = read(sysfd, buf, sizeof(buf) - 1);
> > +    } while (ret == -1 && errno == EINTR);
>
> This is wrong.
> read() might return a value smaller than the 'len' argument (sizeof(buf)
> -1 in your case). But in that case it's a short read, and one need to
> call 'read()' again to fetch the remaining bytes.
>
> So the correct code would be something like:
>
> offset = 0;
> do {
>      ret = read(sysfd, buf + offset, sizeof(buf) - 1 + offset);
>      if (ret > 0)
>          offset += ret;
> } while (ret > 0);
>
> Not that you'd actually need it; reads from sysfs are basically never
> interrupted, so you should be able to read from an attribute in one go.
> IE alternatively you can drop the 'while' loop and just call read().
>
Yes, I didn't think it through.

> > +    if (ret < 0) {
> > +        ret = -errno;
> > +        goto out;
> > +    } else if (ret == 0) {
> > +        ret = -EIO;
> > +        goto out;
> > +    }
> > +    buf[ret] = 0;
> > +
> > +    /* The file is ended with '\n' */
>
> I'd rather check if the string ends with an '\n', and overwrite
> it with a '\0'. That way you'd be insulated against any changes
> to sysfs.
>
Right! I was thinking about it but got hasty to hardcode everything.

> > +    if (strcmp(buf, "host-managed\n") == 0) {
> > +        return BLK_Z_HM;
> > +    } else if (strcmp(buf, "host-aware\n") == 0) {
> > +        return BLK_Z_HA;
> > +    } else {
> > +        return -ENOTSUP;
> > +    }
> > +
> > +out:
> > +    if (sysfd != -1) {
> > +        close(sysfd);
> > +    }
> > +    g_free(sysfspath);
> > +    return ret;
> > +#else
> > +    return -ENOTSUP;
> > +#endif
> > +}
> > +
> >   static int hdev_get_max_segments(int fd, struct stat *st) {
> >       int ret;
> >       ret = get_sysfs_long_val(fd, st, "max_segments");
>
> And as you already set a precedent in your previous patch, I'd recommend
> split this in two patches, one introducing a generic function
> 'get_sysfs_str_val()' which returns a string and another function
> (eg hdev_get_zone_model()) which calls this function to fetch the device
> zoned model.
>
Maybe we can just return a str in get_sysfs_str_val and ignore
returning a zone_model for now? I was using zone_model for testing
purpose. Right now, this function is not used by others in
file-posix.c that causes compilation error in QEMU.

Thanks for reviewing!
Sam
> > diff --git a/include/block/block-common.h b/include/block/block-common.h
> > index 78cddeeda5..35e00afe8e 100644
> > --- a/include/block/block-common.h
> > +++ b/include/block/block-common.h
> > @@ -56,8 +56,8 @@ typedef enum zone_op {
> >   } zone_op;
> >
> >   typedef enum zone_model {
> > -    BLK_Z_HM,
> > -    BLK_Z_HA,
> > +    BLK_Z_HM = 0x1,
> > +    BLK_Z_HA = 0x2,
> >   } zone_model;
> >
> >   typedef enum BlkZoneCondition {
>
> Cheers,
>
> Hannes
> --
> Dr. Hannes Reinecke                        Kernel Storage Architect
> hare@suse.de                                      +49 911 74053 688
> SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg
> HRB 36809 (AG Nürnberg), GF: Felix Imendörffer


  reply	other threads:[~2022-06-27  8:48 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27  0:19 [RFC v3 0/5] *** Add support for zoned device *** Sam Li
2022-06-27  0:19 ` [RFC v3 1/5] block: add block layer APIs resembling Linux ZonedBlockDevice ioctls Sam Li
2022-06-27  7:21   ` Hannes Reinecke
2022-06-27  7:45     ` Sam Li
2022-06-27 14:15   ` Stefan Hajnoczi
2022-06-28  8:05     ` Sam Li
2022-06-28  9:05       ` Damien Le Moal
2022-06-28 10:23         ` Sam Li
2022-06-29  1:43           ` Damien Le Moal
2022-06-29  1:50             ` Sam Li
2022-06-29  2:32               ` Damien Le Moal
2022-06-29  2:35                 ` Sam Li
2022-06-27  0:19 ` [RFC v3 2/5] qemu-io: add zoned block device operations Sam Li
2022-06-27  7:30   ` Hannes Reinecke
2022-06-27  8:31     ` Sam Li
2022-06-28  7:56   ` Stefan Hajnoczi
2022-06-28  9:02     ` Sam Li
2022-06-28  9:11     ` Damien Le Moal
2022-06-28 10:27       ` Sam Li
2022-06-27  0:19 ` [RFC v3 3/5] file-posix: introduce get_sysfs_long_val for zoned device information Sam Li
2022-06-27  7:31   ` Hannes Reinecke
2022-06-27  8:32     ` Sam Li
2022-06-28  8:09   ` Stefan Hajnoczi
2022-06-28  9:18     ` Sam Li
2022-06-27  0:19 ` [RFC v3 4/5] file-posix: introduce get_sysfs_str_val for device zoned model Sam Li
2022-06-27  7:41   ` Hannes Reinecke
2022-06-27  8:44     ` Sam Li [this message]
2022-06-27  0:19 ` [RFC v3 5/5] qemu-iotests: add zone operation tests Sam Li
2022-06-27  7:42   ` Hannes Reinecke
2022-06-28  8:19   ` Stefan Hajnoczi
2022-06-28  9:34     ` Sam Li
2022-06-30 10:11       ` Stefan Hajnoczi

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='CAAAx-8KO_beNZ2i=3smsfGs67f28TT7puYFGbMbgxmr5OO3-WA@mail.gmail.com' \
    --to=faithilikerun@gmail.com \
    --cc=damien.lemoal@opensource.wdc.com \
    --cc=dmitry.fomichev@wdc.com \
    --cc=fam@euphon.net \
    --cc=hare@suse.de \
    --cc=hreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).