From unknown Mon Jun 16 23:15:55 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#58598 <58598@debbugs.gnu.org> To: bug#58598 <58598@debbugs.gnu.org> Subject: Status: [PATCH]parted: acquire flock before modifying the device superblock Reply-To: bug#58598 <58598@debbugs.gnu.org> Date: Tue, 17 Jun 2025 06:15:55 +0000 retitle 58598 [PATCH]parted: acquire flock before modifying the device supe= rblock reassign 58598 parted submitter 58598 "zhanghongtao (A)" severity 58598 normal tag 58598 patch thanks From debbugs-submit-bounces@debbugs.gnu.org Mon Oct 17 19:03:06 2022 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 ) id 1okZ8T-00082w-Cm for submit@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 ) id 1okPxX-0008Gr-Hn for submit@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 ) id 1okPxX-00046f-DG for bug-parted@gnu.org; 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 ) id 1okPxT-000071-4X for bug-parted@gnu.org; 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@huawei.com> 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: , From: "zhanghongtao (A)" 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@huawei.com; 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@huawei.com, liuzhiqiang26@huawei.com X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" 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 --- 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 #include #include +#include #if defined __s390__ || defined __s390x__ #include #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 From debbugs-submit-bounces@debbugs.gnu.org Fri Oct 21 04:37:38 2022 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 ) id 1olnWt-0001OV-Ca for submit@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 ) id 1olnWr-0001ON-Hg for submit@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 ) id 1olnWf-000651-UA for bug-parted@gnu.org; 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 ) id 1olnWc-0007fg-VV for bug-parted@gnu.org; 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)" Subject: Re: [parted-devel] [PATCH]parted: acquire flock before modifying the device superblock In-reply-to: <6a790da3-55d7-8671-ec0d-642356979a34@huawei.com> References: <6a790da3-55d7-8671-ec0d-642356979a34@huawei.com> Comments: In-reply-to "zhanghongtao (A)" message dated "Mon, 17 Oct 2022 21:14:41 +0800." Date: Fri, 21 Oct 2022 01:36:53 -0700 Message-ID: <16056.1666341413@hop.toad.com> From: John Gilmore Received-SPF: pass client-ip=75.101.100.43; envelope-from=gnu@toad.com; 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@huawei.com, parted-devel@alioth-lists.debian.net, bug-parted@gnu.org, liuzhiqiang26@huawei.com X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" 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