GNU bug report logs - #58598
[PATCH]parted: acquire flock before modifying the device superblock

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: parted; Reported by: "zhanghongtao (A)" <zhanghongtao22@HIDDEN>; Keywords: patch; dated Mon, 17 Oct 2022 23:04:02 UTC; Maintainer for parted is bug-parted@HIDDEN.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 21 Oct 2022 08:37:38 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Oct 21 04:37:38 2022
Received: from localhost ([127.0.0.1]:36969 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1olnWt-0001OV-Ca
	for submit <at> debbugs.gnu.org; Fri, 21 Oct 2022 04:37:38 -0400
Received: from lists.gnu.org ([209.51.188.17]:50196)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <gnu@HIDDEN>) id 1olnWr-0001ON-Hg
 for submit <at> debbugs.gnu.org; Fri, 21 Oct 2022 04:37:21 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <gnu@HIDDEN>) id 1olnWf-000651-UA
 for bug-parted@HIDDEN; Fri, 21 Oct 2022 04:37:15 -0400
Received: from 75-101-100-43.dsl.static.fusionbroadband.com ([75.101.100.43]
 helo=hop.toad.com)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <gnu@HIDDEN>) id 1olnWc-0007fg-VV
 for bug-parted@HIDDEN; Fri, 21 Oct 2022 04:37:09 -0400
Received: from hop.toad.com (localhost [127.0.0.1])
 by hop.toad.com (8.12.9/8.12.9) with ESMTP id 29L8arDI016057;
 Fri, 21 Oct 2022 01:36:53 -0700
To: "zhanghongtao (A)" <zhanghongtao22@HIDDEN>
Subject: Re: [parted-devel] [PATCH]parted: acquire flock before modifying the
 device superblock 
In-reply-to: <6a790da3-55d7-8671-ec0d-642356979a34@HIDDEN> 
References: <6a790da3-55d7-8671-ec0d-642356979a34@HIDDEN>
Comments: In-reply-to "zhanghongtao (A)" <zhanghongtao22@HIDDEN>
 message dated "Mon, 17 Oct 2022 21:14:41 +0800."
Date: Fri, 21 Oct 2022 01:36:53 -0700
Message-ID: <16056.1666341413@HIDDEN>
From: John Gilmore <gnu@HIDDEN>
Received-SPF: pass client-ip=75.101.100.43; envelope-from=gnu@HIDDEN;
 helo=hop.toad.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, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001, TVD_RCVD_IP=0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: -1.4 (-)
X-Debbugs-Envelope-To: submit
Cc: linfeilong@HIDDEN, parted-devel@HIDDEN,
 bug-parted@HIDDEN, liuzhiqiang26@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)

Parted shouldn't lock itself (or systemd) out of concurrent access
to the same device.  But if there is a bug relating to concurrent
"formatting a filesystem and systemd", then why not get an flock
just for the duration of formatting a file system?  Formatting
is a write-only operation and there's no point in trying to do two
of them concurrently on the same file system.

	John




Information forwarded to bug-parted@HIDDEN:
bug#58598; Package parted. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 17 Oct 2022 23:03:07 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Oct 17 19:03:06 2022
Received: from localhost ([127.0.0.1]:50404 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1okZ8T-00082w-Cm
	for submit <at> debbugs.gnu.org; Mon, 17 Oct 2022 19:03:06 -0400
Received: from lists.gnu.org ([209.51.188.17]:44260)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <zhanghongtao22@HIDDEN>) id 1okPxX-0008Gr-Hn
 for submit <at> debbugs.gnu.org; Mon, 17 Oct 2022 09:15:12 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10]:52938)
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <zhanghongtao22@HIDDEN>)
 id 1okPxX-00046f-DG
 for bug-parted@HIDDEN; Mon, 17 Oct 2022 09:15:11 -0400
Received: from szxga01-in.huawei.com ([45.249.212.187]:4527)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <zhanghongtao22@HIDDEN>)
 id 1okPxT-000071-4X
 for bug-parted@HIDDEN; Mon, 17 Oct 2022 09:15:11 -0400
Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.54])
 by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MrcmG07ZCzmVJn;
 Mon, 17 Oct 2022 21:10:06 +0800 (CST)
Received: from [10.174.179.0] (10.174.179.0) by dggpemm500024.china.huawei.com
 (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct
 2022 21:14:47 +0800
Message-ID: <6a790da3-55d7-8671-ec0d-642356979a34@HIDDEN>
Date: Mon, 17 Oct 2022 21:14:41 +0800
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
 Thunderbird/91.9.0
To: <parted-devel@HIDDEN>, <bug-parted@HIDDEN>
From: "zhanghongtao (A)" <zhanghongtao22@HIDDEN>
Subject: [PATCH]parted: acquire flock before modifying the device superblock
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Originating-IP: [10.174.179.0]
X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To
 dggpemm500024.china.huawei.com (7.185.36.203)
X-CFilter-Loop: Reflected
Received-SPF: pass client-ip=45.249.212.187;
 envelope-from=zhanghongtao22@HIDDEN; helo=szxga01-in.huawei.com
X-Spam_score_int: -41
X-Spam_score: -4.2
X-Spam_bar: ----
X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: -1.4 (-)
X-Debbugs-Envelope-To: submit
X-Mailman-Approved-At: Mon, 17 Oct 2022 19:03:02 -0400
Cc: linfeilong@HIDDEN, liuzhiqiang26@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -2.4 (--)

We noticed that systemd has an issue about symlink unreliable caused by
formatting filesystem and systemd operating on same device.
Issue Link: https://github.com/systemd/systemd/issues/23746

According to systemd doc, a BSD flock needs to be acquired before
formatting the device.
Related Link: https://systemd.io/BLOCK_DEVICE_LOCKING/

So we acquire flock after opening the device.
But this patch causes multiple parted processes to fail to run
simultaneously in interactive mode.
Or, can we add an option for lock mode?

Signed-off-by: Hongtao Zhang <zhanghongtao22@HIDDEN>
---
 libparted/arch/linux.c | 49 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index f3bf14d..f7645f4 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -22,6 +22,7 @@
 #include <linux/blkpg.h>
 #include <parted/parted.h>
 #include <parted/debug.h>
+#include <sys/file.h>
 #if defined __s390__ || defined __s390x__
 #include <parted/fdasd.h>
 #endif
@@ -1706,6 +1707,50 @@ _device_open_ro (PedDevice* dev)
     return rc;
 }

+static int lock_blkdev (int fd, char *path)
+{
+        int rc, msg = 0;
+
+        /* Try non-block first to provide message */
+        rc = flock(fd, LOCK_EX | LOCK_NB);
+        if (rc == 0)
+                return 0;
+        if (rc != 0 && errno == EWOULDBLOCK) {
+                ped_exception_throw (
+                        PED_EXCEPTION_WARNING,
+                        PED_EXCEPTION_OK,
+                        _("%s: device already locked, "
+                        "waiting to get lock ..."),
+                        path);
+                msg = 1;
+        }
+
+        rc = flock (fd, LOCK_EX);
+        if (rc != 0) {
+                switch (errno) {
+                case EWOULDBLOCK: /* LOCK_NB */
+                        ped_exception_throw (
+                                PED_EXCEPTION_WARNING,
+                                PED_EXCEPTION_OK,
+                                _("%s: device already locked"),
+                                path);
+                        break;
+                default:
+                        ped_exception_throw (
+                                PED_EXCEPTION_WARNING,
+                                PED_EXCEPTION_OK,
+                                _("%s: failed to get lock"),
+                                path);
+                }
+        } else if (msg)
+                ped_exception_throw (
+                        PED_EXCEPTION_WARNING,
+                        PED_EXCEPTION_OK,
+                        _("%s: lock ok"),
+                        path);
+        return rc;
+}
+
 static int
 linux_open (PedDevice* dev)
 {
@@ -1747,6 +1792,10 @@ retry:
                 }
         } else {
                 dev->read_only = 0;
+                if (lock_blkdev (arch_specific->fd, dev->path) != 0) {
+                        close (arch_specific->fd);
+                        return 0;
+                }
         }

         _flush_cache (dev);
--
2.33.0




Acknowledgement sent to "zhanghongtao (A)" <zhanghongtao22@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-parted@HIDDEN. Full text available.
Report forwarded to bug-parted@HIDDEN:
bug#58598; Package parted. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Fri, 21 Oct 2022 08:45:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.