X-Loop: help-debbugs@HIDDEN Subject: bug#58598: [PATCH]parted: acquire flock before modifying the device superblock Resent-From: "zhanghongtao (A)" <zhanghongtao22@HIDDEN> Original-Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> Resent-CC: bug-parted@HIDDEN Resent-Date: Mon, 17 Oct 2022 23:04:02 +0000 Resent-Message-ID: <handler.58598.B.166604778730945 <at> debbugs.gnu.org> Resent-Sender: help-debbugs@HIDDEN X-GNU-PR-Message: report 58598 X-GNU-PR-Package: parted X-GNU-PR-Keywords: patch To: parted-devel@HIDDEN, 58598 <at> debbugs.gnu.org Cc: linfeilong@HIDDEN, liuzhiqiang26@HIDDEN X-Debbugs-Original-To: <parted-devel@HIDDEN>, <bug-parted@HIDDEN> Received: via spool by submit <at> debbugs.gnu.org id=B.166604778730945 (code B ref -1); Mon, 17 Oct 2022 23:04:02 +0000 Received: (at submit) by debbugs.gnu.org; 17 Oct 2022 23:03:07 +0000 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 From: "zhanghongtao (A)" <zhanghongtao22@HIDDEN> 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-Mailman-Approved-At: Mon, 17 Oct 2022 19:03:02 -0400 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
Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.505 (Entity 5.505) Content-Type: text/plain; charset=utf-8 X-Loop: help-debbugs@HIDDEN From: help-debbugs@HIDDEN (GNU bug Tracking System) To: "zhanghongtao (A)" <zhanghongtao22@HIDDEN> Subject: bug#58598: Acknowledgement ([PATCH]parted: acquire flock before modifying the device superblock) Message-ID: <handler.58598.B.166604778730945.ack <at> debbugs.gnu.org> References: <6a790da3-55d7-8671-ec0d-642356979a34@HIDDEN> X-Gnu-PR-Message: ack 58598 X-Gnu-PR-Package: parted X-Gnu-PR-Keywords: patch Reply-To: 58598 <at> debbugs.gnu.org Date: Mon, 17 Oct 2022 23:04:02 +0000 Thank you for filing a new bug report with debbugs.gnu.org. This is an automatically generated reply to let you know your message has been received. Your message is being forwarded to the package maintainers and other interested parties for their attention; they will reply in due course. Your message has been sent to the package maintainer(s): bug-parted@HIDDEN If you wish to submit further information on this problem, please send it to 58598 <at> debbugs.gnu.org. Please do not send mail to help-debbugs@HIDDEN unless you wish to report a problem with the Bug-tracking system. --=20 58598: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D58598 GNU Bug Tracking System Contact help-debbugs@HIDDEN with problems
X-Loop: help-debbugs@HIDDEN Subject: bug#58598: [parted-devel] [PATCH]parted: acquire flock before modifying the device superblock Resent-From: John Gilmore <gnu@HIDDEN> Original-Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org> Resent-CC: bug-parted@HIDDEN Resent-Date: Fri, 21 Oct 2022 08:38:01 +0000 Resent-Message-ID: <handler.58598.B.16663414585385 <at> debbugs.gnu.org> Resent-Sender: help-debbugs@HIDDEN X-GNU-PR-Message: followup 58598 X-GNU-PR-Package: parted X-GNU-PR-Keywords: patch To: "zhanghongtao (A)" <zhanghongtao22@HIDDEN> Cc: linfeilong@HIDDEN, parted-devel@HIDDEN, 58598 <at> debbugs.gnu.org, liuzhiqiang26@HIDDEN X-Debbugs-Original-Cc: linfeilong@HIDDEN, parted-devel@HIDDEN, bug-parted@HIDDEN, liuzhiqiang26@HIDDEN Received: via spool by submit <at> debbugs.gnu.org id=B.16663414585385 (code B ref -1); Fri, 21 Oct 2022 08:38:01 +0000 Received: (at submit) by debbugs.gnu.org; 21 Oct 2022 08:37:38 +0000 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 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-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
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997 nCipher Corporation Ltd,
1994-97 Ian Jackson.