From nobody Wed May 22 02:55:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1608029691; cv=none; d=zohomail.com; s=zohoarc; b=NPB/9L2RluiB0lq29h3zTOIFzNq1GF3o7m6QMKUVF5sfA62WaEX5V/1eZQI5QBE/QTf7dExjvMeZmT/Jzs1uhPg1diXiyLzRcrsjThJ6KUl+i603yIeWZT7E6jmExDOOq4UeeYBhmF2UxnYyRmkhkLgigPtrk6Hl6EFWtU/PBpk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608029691; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=6c/4ZulLKq+u38VLp1ymzM4cIJt61r6DTsd1rX/zQ/0=; b=kQNlUWIkDCHq1z6zCk09Vm3DaHmGq9HSX/bb93AjBHxdRw76Uj5rqfwHo5anRWHH/PIvUSNuenV34EFJSL04j3DxkVX5+x56vWu2rlkkPl10Iq3yqNtcj3UfEwuW9J4fpjbOEw8dmmXDa/FNzKoXdOH8qDPUljRanArlvNmvmjU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1608029691466781.4061364926744; Tue, 15 Dec 2020 02:54:51 -0800 (PST) Received: from localhost ([::1]:46322 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kp7yk-0005uz-Fn for importer@patchew.org; Tue, 15 Dec 2020 05:54:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kp7xk-00058P-7z for qemu-devel@nongnu.org; Tue, 15 Dec 2020 05:53:48 -0500 Received: from mail-pj1-x1042.google.com ([2607:f8b0:4864:20::1042]:36930) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kp7xg-0007gN-NV for qemu-devel@nongnu.org; Tue, 15 Dec 2020 05:53:48 -0500 Received: by mail-pj1-x1042.google.com with SMTP id b5so7180160pjk.2 for ; Tue, 15 Dec 2020 02:53:44 -0800 (PST) Received: from 64-217.abest.vip ([129.227.156.197]) by smtp.gmail.com with ESMTPSA id a11sm24371484pfr.198.2020.12.15.02.53.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Dec 2020 02:53:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=6c/4ZulLKq+u38VLp1ymzM4cIJt61r6DTsd1rX/zQ/0=; b=F2msXA6cWFcegC3k1QareVmAE7Q4J6aLAyH9PMcZQBhyxluyhxDfJk3IN/csDWO2oe TtaTN9vMv9swRYpCOZLI6Wc4Pbn68uAmeTWAPTGlrz6y2OnjjqtMY8wdooeowfqE+fE4 2nGc5NBuXYHuXpj7HdSNmrg+JKixyKaLvUPveVz6nVdU9AQ/oNdV3gTlIFXGC3kTRkRv /QprwRMK2CaF0kCpheKQcq/lmKpF0JNYvB0pHPB71lIP6TPFIw2Gc3cgpAO7Qd3yKglP m/dCoxCT283iVCfZQvxq9j0y3mDGFz+wB+PjBYBRZXPLoC6TtI+6KI+VKKuLNiQqbDFP zTRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=6c/4ZulLKq+u38VLp1ymzM4cIJt61r6DTsd1rX/zQ/0=; b=GwsvA00sCsbDZBMFUltaoNKAmzBYPRdK3hpBNjnMe1MWIbgqrvFEpmcTO8f3WltZZe 5Uj7q2zV6f/iHxX2lseADU75EBs89w+P985W1+PASAPhH26aCGwutrsvLAF1aiI2FCU8 XOR7BTcw8eyuUJAS6rkf89KnatTYqW7EJhtZA6nP93wNNC25p5FUwAhajcZRI+NBaZlA DXjqA6bGba+2tbReFNxUv4lmgqxH3+S2ucRoCpqW5ZXmjHqORDFsUhTMrfLshldykF6x lZLh/Em8u5GDssbN+grganX5XA3Mh4AvuUBLtalHwg5vsKJNiqJMhR6mbTH0R3T7HntQ KU1w== X-Gm-Message-State: AOAM530VFHRWu3tNlvy1jc0GgCl5sh619R0qJ1JfBiZMEH6hqYsCOAOE qaNl6yyGPpr/DrZ6xphPW/FDMw== X-Google-Smtp-Source: ABdhPJzgaAPIPEdCrxfOL0G3pn2tx8zz6eCxIXVUZluAukSC+ddsdanzA0zn+D2cxhkHIL+XVQC31g== X-Received: by 2002:a17:90b:78d:: with SMTP id l13mr29698100pjz.51.1608029623035; Tue, 15 Dec 2020 02:53:43 -0800 (PST) From: Li Feng To: berrange@redhat.com, Kevin Wolf , Max Reitz , qemu-block@nongnu.org (open list:raw), qemu-devel@nongnu.org (open list:All patches CC here) Subject: [PATCH v5] file-posix: detect the lock using the real file Date: Tue, 15 Dec 2020 18:53:56 +0800 Message-Id: <1608029636-31442-1-git-send-email-fengli@smartx.com> X-Mailer: git-send-email 1.8.3.1 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::1042; envelope-from=fengli@smartx.com; helo=mail-pj1-x1042.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lifeng1519@gmail.com, Li Feng , kyle@smartx.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch addresses this issue: When accessing a volume on an NFS filesystem without supporting the file lo= ck, tools, like qemu-img, will complain "Failed to lock byte 100". In the original code, the qemu_has_ofd_lock will test the lock on the "/dev/null" pseudo-file. Actually, the file.locking is per-drive property, which depends on the underlay filesystem. In this patch, add a new 'qemu_has_file_lock' to detect whether the file supports the file lock. And disable the lock when the underlay file system doesn't support locks. Signed-off-by: Li Feng Reviewed-by: Daniel P. Berrang=C3=A9 --- v5: simplify the code. v4: use the fd as the qemu_has_file_lock argument. v3: don't call the qemu_has_ofd_lock, use a new function instead. v2: remove the refactoring. --- block/file-posix.c | 61 +++++++++++++++++++++++--------------------- include/qemu/osdep.h | 1 + util/osdep.c | 14 ++++++++++ 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 806764f7e3..4e00111031 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -584,6 +584,21 @@ static int raw_open_common(BlockDriverState *bs, QDict= *options, s->use_linux_io_uring =3D (aio =3D=3D BLOCKDEV_AIO_OPTIONS_IO_URING); #endif =20 + s->open_flags =3D open_flags; + raw_parse_flags(bdrv_flags, &s->open_flags, false); + + s->fd =3D -1; + fd =3D qemu_open(filename, s->open_flags, errp); + ret =3D fd < 0 ? -errno : 0; + + if (ret < 0) { + if (ret =3D=3D -EROFS) { + ret =3D -EACCES; + } + goto fail; + } + s->fd =3D fd; + locking =3D qapi_enum_parse(&OnOffAuto_lookup, qemu_opt_get(opts, "locking"), ON_OFF_AUTO_AUTO, &local_err); @@ -606,7 +621,7 @@ static int raw_open_common(BlockDriverState *bs, QDict = *options, s->use_lock =3D false; break; case ON_OFF_AUTO_AUTO: - s->use_lock =3D qemu_has_ofd_lock(); + s->use_lock =3D qemu_has_file_lock(s->fd) && qemu_has_ofd_lock(); break; default: abort(); @@ -625,22 +640,6 @@ static int raw_open_common(BlockDriverState *bs, QDict= *options, s->drop_cache =3D qemu_opt_get_bool(opts, "drop-cache", true); s->check_cache_dropped =3D qemu_opt_get_bool(opts, "x-check-cache-drop= ped", false); - - s->open_flags =3D open_flags; - raw_parse_flags(bdrv_flags, &s->open_flags, false); - - s->fd =3D -1; - fd =3D qemu_open(filename, s->open_flags, errp); - ret =3D fd < 0 ? -errno : 0; - - if (ret < 0) { - if (ret =3D=3D -EROFS) { - ret =3D -EACCES; - } - goto fail; - } - s->fd =3D fd; - /* Check s->open_flags rather than bdrv_flags due to auto-read-only */ if (s->open_flags & O_RDWR) { ret =3D check_hdev_writable(s->fd); @@ -2388,6 +2387,7 @@ raw_co_create(BlockdevCreateOptions *options, Error *= *errp) int fd; uint64_t perm, shared; int result =3D 0; + bool use_lock; =20 /* Validate options and set default values */ assert(options->driver =3D=3D BLOCKDEV_DRIVER_FILE); @@ -2428,19 +2428,22 @@ raw_co_create(BlockdevCreateOptions *options, Error= **errp) perm =3D BLK_PERM_WRITE | BLK_PERM_RESIZE; shared =3D BLK_PERM_ALL & ~BLK_PERM_RESIZE; =20 - /* Step one: Take locks */ - result =3D raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, errp); - if (result < 0) { - goto out_close; - } + use_lock =3D qemu_has_file_lock(fd); + if (use_lock) { + /* Step one: Take locks */ + result =3D raw_apply_lock_bytes(NULL, fd, perm, ~shared, false, er= rp); + if (result < 0) { + goto out_close; + } =20 - /* Step two: Check that nobody else has taken conflicting locks */ - result =3D raw_check_lock_bytes(fd, perm, shared, errp); - if (result < 0) { - error_append_hint(errp, - "Is another process using the image [%s]?\n", - file_opts->filename); - goto out_unlock; + /* Step two: Check that nobody else has taken conflicting locks */ + result =3D raw_check_lock_bytes(fd, perm, shared, errp); + if (result < 0) { + error_append_hint(errp, + "Is another process using the image [%s]?\n", + file_opts->filename); + goto out_unlock; + } } =20 /* Clear the file by truncating it to 0 */ diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index f9ec8c84e9..c7587be99d 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -513,6 +513,7 @@ int qemu_lock_fd(int fd, int64_t start, int64_t len, bo= ol exclusive); int qemu_unlock_fd(int fd, int64_t start, int64_t len); int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive); bool qemu_has_ofd_lock(void); +bool qemu_has_file_lock(int fd); #endif =20 #if defined(__HAIKU__) && defined(__i386__) diff --git a/util/osdep.c b/util/osdep.c index 66d01b9160..dee1f076da 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -225,6 +225,20 @@ static void qemu_probe_lock_ops(void) } } =20 +bool qemu_has_file_lock(int fd) +{ + int ret; + struct flock fl =3D { + .l_whence =3D SEEK_SET, + .l_start =3D 0, + .l_len =3D 0, + .l_type =3D F_WRLCK, + }; + + ret =3D fcntl(fd, F_GETLK, &fl); + return ret =3D=3D 0; +} + bool qemu_has_ofd_lock(void) { qemu_probe_lock_ops(); --=20 2.24.3