<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 2025/12/26 14:51, Gao Xiang wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:ba3929d3-b8f3-4027-bfa1-5ade5ec348f2@linux.alibaba.com">Hi
      Yifan,
      <br>
      <br>
      On 2025/12/23 18:04, Yifan Zhao wrote:
      <br>
      <blockquote type="cite">The `current erofsmount_nbd()`
        implementation verifies that the value in
        <br>
        `/sys/block/nbdX/pid`` matches the PID of the process executing
        <br>
        `erofsmount_nbd_loopfn()`, using this as an indicator that the
        NBD
        <br>
        device is ready. This check is incorrect, as the PID recorded in
        the
        <br>
        aforementioned sysfs file may belong to a kernel thread rather
        than a
        <br>
        userspace process (see [1]).
        <br>
      </blockquote>
      <br>
      Do you have a way to reproduce that? <br>
    </blockquote>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">This issue is consistently reproducible in my WSL2 environment (kernel version 6.6.87.2-microsoft-standard-WSL2),</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">but works correctly in other environments (e.g., openEuler 24.03 SP2). The finer-grained difference</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">between these 2 environments remains unclear.</span></p>
    <blockquote type="cite"
      cite="mid:ba3929d3-b8f3-4027-bfa1-5ade5ec348f2@linux.alibaba.com"><br>
      <blockquote type="cite">
        <br>
        Moreover, since this verification only occurs after the child
        process
        <br>
        has successfully issued and returned from the NBD connect
        request,
        <br>
        removing it introduces no risk of NBD device hijacking by
        malicious
        <br>
        actors. This patch removes the erroneous check.
        <br>
      </blockquote>
      <br>
      It's not used to avoid device hijacking by malicious actors but
      <br>
      detecting if the NBD device is reused by another daemon. <br>
      <br>
    </blockquote>
    <p>ok. <span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">I believe under the ioctl interface, there is indeed no reliable way to determine exactly </span><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">whether the current daemon</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">is the one holding the NBD device open (unlike with netlink, where </span><code
      class="qwen-markdown-codespan"
style="margin: 0px; padding: 0px 0.25rem; box-sizing: border-box; border-width: 0px; border-style: solid; border-color: rgb(227, 227, 227); display: inline-block; font-size: 0.875rem; line-height: 1.375rem; letter-spacing: -0.04em; color: rgb(97, 92, 237); border-radius: 0.25rem; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(239, 238, 255); word-break: break-word; font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; cursor: pointer;">/sys/block/nbdX/backend</code><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> can be used for such verification).</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">This is indeed a genuine limitation.</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">
</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Thanks,</span></p>
    <p><span
style="color: rgb(29, 29, 31); font-family: system-ui, ui-sans-serif, -apple-system, BlinkMacSystemFont, Inter, NotoSansHans, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-line; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Yifan</span></p>
    <blockquote type="cite"
      cite="mid:ba3929d3-b8f3-4027-bfa1-5ade5ec348f2@linux.alibaba.com">Thanks,
      <br>
      Gao Xiang
      <br>
      <br>
      <blockquote type="cite">
        <br>
        [1]
        <a class="moz-txt-link-freetext" href="https://elixir.bootlin.com/linux/latest/source/drivers/block/nbd.c#L1501">https://elixir.bootlin.com/linux/latest/source/drivers/block/nbd.c#L1501</a>
        <br>
        <br>
        Signed-off-by: Yifan Zhao <a class="moz-txt-link-rfc2396E" href="mailto:zhaoyifan28@huawei.com"><zhaoyifan28@huawei.com></a>
        <br>
        ---
        <br>
          lib/backends/nbd.c | 16 +++++-----------
        <br>
          lib/liberofs_nbd.h |  2 +-
        <br>
          mount/main.c       |  5 ++---
        <br>
          3 files changed, 8 insertions(+), 15 deletions(-)
        <br>
        <br>
        diff --git a/lib/backends/nbd.c b/lib/backends/nbd.c
        <br>
        index 46e75cd..2d941a9 100644
        <br>
        --- a/lib/backends/nbd.c
        <br>
        +++ b/lib/backends/nbd.c
        <br>
        @@ -52,7 +52,8 @@ struct nbd_reply {
        <br>
              };
        <br>
          } __packed;
        <br>
          -long erofs_nbd_in_service(int nbdnum)
        <br>
        +/* Return: 0 while nbd is in service, <0 otherwise */
        <br>
        +int erofs_nbd_in_service(int nbdnum)
        <br>
          {
        <br>
              int fd, err;
        <br>
              char s[32];
        <br>
        @@ -72,17 +73,10 @@ long erofs_nbd_in_service(int nbdnum)
        <br>
                  return -ENOTCONN;
        <br>
                (void)snprintf(s, sizeof(s), "/sys/block/nbd%d/pid",
        nbdnum);
        <br>
        -    fd = open(s, O_RDONLY);
        <br>
        -    if (fd < 0)
        <br>
        +    if (access(s, F_OK) < 0)
        <br>
                  return -errno;
        <br>
        -    err = read(fd, s, sizeof(s));
        <br>
        -    if (err < 0) {
        <br>
        -        err = -errno;
        <br>
        -        close(fd);
        <br>
        -        return err;
        <br>
        -    }
        <br>
        -    close(fd);
        <br>
        -    return strtol(s, NULL, 10);
        <br>
        +
        <br>
        +    return 0;
        <br>
          }
        <br>
            int erofs_nbd_devscan(void)
        <br>
        diff --git a/lib/liberofs_nbd.h b/lib/liberofs_nbd.h
        <br>
        index 78c8af5..b719d80 100644
        <br>
        --- a/lib/liberofs_nbd.h
        <br>
        +++ b/lib/liberofs_nbd.h
        <br>
        @@ -34,7 +34,7 @@ struct erofs_nbd_request {
        <br>
          /* 30-day timeout for NBD recovery */
        <br>
          #define EROFS_NBD_DEAD_CONN_TIMEOUT    (3600 * 24 * 30)
        <br>
          -long erofs_nbd_in_service(int nbdnum);
        <br>
        +int erofs_nbd_in_service(int nbdnum);
        <br>
          int erofs_nbd_devscan(void);
        <br>
          int erofs_nbd_connect(int nbdfd, int blkbits, u64 blocks);
        <br>
          char *erofs_nbd_get_identifier(int nbdnum);
        <br>
        diff --git a/mount/main.c b/mount/main.c
        <br>
        index d2d4815..f6cba33 100644
        <br>
        --- a/mount/main.c
        <br>
        +++ b/mount/main.c
        <br>
        @@ -1270,6 +1270,8 @@ static int erofsmount_nbd(struct
        erofs_nbd_source *source,
        <br>
                while (1) {
        <br>
                  err = erofs_nbd_in_service(msg.nbdnum);
        <br>
        +        if (!err)
        <br>
        +            break;
        <br>
                  if (err == -ENOENT || err == -ENOTCONN) {
        <br>
                      err = waitpid(child, &child_status, WNOHANG);
        <br>
                      if (err < 0)
        <br>
        @@ -1280,9 +1282,6 @@ static int erofsmount_nbd(struct
        erofs_nbd_source *source,
        <br>
                      usleep(50000);
        <br>
                      continue;
        <br>
                  }
        <br>
        -        if (err >= 0)
        <br>
        -            err = (err != child ? -EBUSY : 0);
        <br>
        -        break;
        <br>
              }
        <br>
                if (!err) {
        <br>
      </blockquote>
      <br>
    </blockquote>
  </body>
</html>