Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C82BC74A5B for ; Sat, 18 Mar 2023 03:36:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229581AbjCRDgK (ORCPT ); Fri, 17 Mar 2023 23:36:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbjCRDgJ (ORCPT ); Fri, 17 Mar 2023 23:36:09 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 779261CF47 for ; Fri, 17 Mar 2023 20:36:07 -0700 (PDT) Received: from kwepemm600003.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4PdmmH3ZN1znXLJ; Sat, 18 Mar 2023 11:33:03 +0800 (CST) Received: from [10.174.179.254] (10.174.179.254) by kwepemm600003.china.huawei.com (7.193.23.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Sat, 18 Mar 2023 11:36:04 +0800 To: Ext4 Developers List CC: "Theodore Y. Ts'o" , , Jan Kara , linfeilong , wuguanghao , zhanchengbin , From: Zhiqiang Liu Subject: [PATCH] tune2fs: check whether dev is mounted or in use before setting Message-ID: <8babc8eb-1713-91c9-1efa-496909340a6f@huawei.com> Date: Sat, 18 Mar 2023 11:36:03 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.254] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemm600003.china.huawei.com (7.193.23.202) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Zhiqiang Liu tune2fs is used to adjust various tunable filesystem pars, which may conflict with kernel operations. So we should check whether device is mounted or in use at the begin similar to e2fsck and mke2fs. Of course, we can ignore this check if -f is set. Reported-by: Baokun Li Signed-off-by: Zhiqiang Liu Signed-off-by: zhanchengbin --- misc/tune2fs.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/misc/tune2fs.c b/misc/tune2fs.c index 458f7cf6..b667e1f4 100644 --- a/misc/tune2fs.c +++ b/misc/tune2fs.c @@ -3327,6 +3327,22 @@ retry_open: goto closefs; } + if (open_flag & EXT2_FLAG_RW) { + if (mount_flags & EXT2_MF_MOUNTED) { + fprintf(stderr, _("Warning! %s is mounted.\n"), device_name); + if (!f_flag) { + rc = 1; + goto closefs; + } + } else if (mount_flags & EXT2_MF_BUSY) { + fprintf(stderr, _("Warning! %s is in use by the system.\n"), + device_name); + if (!f_flag) { + rc = 1; + goto closefs; + } + } + } #ifdef NO_RECOVERY /* Warn if file system needs recovery and it is opened for writing. */ if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & EXT2_MF_MOUNTED) && -- 2.33.0