From patchwork Mon Jan 25 10:43:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 12048077 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D91CAC4332B for ; Tue, 26 Jan 2021 19:32:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BBBA92311C for ; Tue, 26 Jan 2021 19:32:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388353AbhAZFmj (ORCPT ); Tue, 26 Jan 2021 00:42:39 -0500 Received: from mx2.suse.de ([195.135.220.15]:37454 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727428AbhAYKoq (ORCPT ); Mon, 25 Jan 2021 05:44:46 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1611571440; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=t8p4F4lY0153fmHVn1pyaemCFrPPSyVOX9KN261gsMc=; b=gPyg+ohIrWaHMSTpAuh1ACFHTyq+gDsUIaVnlSZBilJnFSogY5Q432/PAfdSHzHFDYp3Xw 7lyN4HG5hwXsN08ORnEYgywKLX+wDomPRfw2HcXU+ra2vYpD6r3B01c8BCwWe/b8r0PC8f +mpfjq2C5ES/xL/+Lj6osjFrYFeHVMA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 66877ADC4; Mon, 25 Jan 2021 10:44:00 +0000 (UTC) From: Nikolay Borisov To: linux-btrfs@vger.kernel.org Cc: Nikolay Borisov Subject: [PATCH 1/2] btrfs-progs: tests: Extend cli/003 Date: Mon, 25 Jan 2021 12:43:57 +0200 Message-Id: <20210125104358.817072-1-nborisov@suse.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Add a test which ensures that when resize is tried on an image instead of a directory appropriate warning is produced and the command fails. Signed-off-by: Nikolay Borisov --- tests/cli-tests/003-fi-resize-args/test.sh | 7 +++++++ 1 file changed, 7 insertions(+) -- 2.25.1 diff --git a/tests/cli-tests/003-fi-resize-args/test.sh b/tests/cli-tests/003-fi-resize-args/test.sh index 2e03725bb91a..0d2263f4b97f 100755 --- a/tests/cli-tests/003-fi-resize-args/test.sh +++ b/tests/cli-tests/003-fi-resize-args/test.sh @@ -54,4 +54,11 @@ for sep in '' '--'; do run_check $SUDO_HELPER "$TOP/btrfs" filesystem resize $sep 1:max "$TEST_MNT" done + +# test passing a file instead of a directory +run_mustfail_stdout "should fail for image" "$TOP/btrfs" filesystem resize 1:-128M "$TEST_DEV" | + grep -q "ERROR: resize works on mounted filesystems and accepts only" || + _fail "no expected error message in the output 2" + + run_check_umount_test_dev From patchwork Mon Jan 25 10:43:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Borisov X-Patchwork-Id: 12045543 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDDD0C433DB for ; Tue, 26 Jan 2021 05:43:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B5A252223D for ; Tue, 26 Jan 2021 05:43:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388364AbhAZFm5 (ORCPT ); Tue, 26 Jan 2021 00:42:57 -0500 Received: from mx2.suse.de ([195.135.220.15]:37476 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727593AbhAYKor (ORCPT ); Mon, 25 Jan 2021 05:44:47 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1611571440; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qe6lCcfR/lVJ31oI/FVTUkgbA3Bo/fUCRAB/Xk1GYEE=; b=hiNZFUZGCzWW7n/A7/8jOiIH3xckhGfLMWiwsMv7zGX8aZ0ipOroF+sZllFIItp8Cju4fl IXSVE99mm4mcVuuCG49JuetkCR2g+gCzUQoEFWlvU6OW+Ls2Rb+IwPQliMMMFRfmHEkv5P pgPgZ9gVIAcvHEBwuDAPJ1bd+UpbQiA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9ED31B759; Mon, 25 Jan 2021 10:44:00 +0000 (UTC) From: Nikolay Borisov To: linux-btrfs@vger.kernel.org Cc: Nikolay Borisov Subject: [PATCH 2/2] btrfs-progs: Remove duplicate checks from cmd_filesystem_resize Date: Mon, 25 Jan 2021 12:43:58 +0200 Message-Id: <20210125104358.817072-2-nborisov@suse.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210125104358.817072-1-nborisov@suse.com> References: <20210125104358.817072-1-nborisov@suse.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org btrfs_open_dir already has a check whether the passed path is a directory and if so it returns a specific error code (-3) when such an error occurs. Use this instead of open-coding the directory check. To avoid regression in cli/003 test also move directory checks before fs type in btrfs_open Output before this check : ERROR: resize works on mounted filesystems and accepts only directories as argument. Passing file containing a btrfs image would resize the underlying filesystem instead of the image. After: ERROR: not a directory: /root/btrfs-progs/tests/test.img ERROR: resize works on mounted filesystems and accepts only directories as argument. Passing file containing a btrfs image would resize the underlying filesystem instead of the image. Signed-off-by: Nikolay Borisov --- cmds/filesystem.c | 21 +++++++-------------- common/utils.c | 16 ++++++++-------- 2 files changed, 15 insertions(+), 22 deletions(-) -- 2.25.1 diff --git a/cmds/filesystem.c b/cmds/filesystem.c index ba2e5928cc02..8379fd7a8151 100644 --- a/cmds/filesystem.c +++ b/cmds/filesystem.c @@ -1082,7 +1082,6 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd, char *amount, *path; DIR *dirstream = NULL; int ret; - struct stat st; bool enqueue = false; /* @@ -1115,21 +1114,15 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd, return 1; } - res = stat(path, &st); - if (res < 0) { - error("resize: cannot stat %s: %m", path); - return 1; - } - if (!S_ISDIR(st.st_mode)) { - error("resize works on mounted filesystems and accepts only\n" - "directories as argument. Passing file containing a btrfs image\n" - "would resize the underlying filesystem instead of the image.\n"); - return 1; - } - fd = btrfs_open_dir(path, &dirstream, 1); - if (fd < 0) + if (fd < 0) { + if (fd == -3) { + error("resize works on mounted filesystems and accepts only\n" + "directories as argument. Passing file containing a btrfs image\n" + "would resize the underlying filesystem instead of the image.\n"); + } return 1; + } ret = check_running_fs_exclop(fd, BTRFS_EXCLOP_RESIZE, enqueue); if (ret != 0) { diff --git a/common/utils.c b/common/utils.c index f7dc320c8915..15fda84ed291 100644 --- a/common/utils.c +++ b/common/utils.c @@ -185,24 +185,24 @@ int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only) struct stat st; int ret; - if (statfs(path, &stfs) != 0) { + if (stat(path, &st) != 0) { error_on(verbose, "cannot access '%s': %m", path); return -1; } - if (stfs.f_type != BTRFS_SUPER_MAGIC) { - error_on(verbose, "not a btrfs filesystem: %s", path); - return -2; + if (dir_only && !S_ISDIR(st.st_mode)) { + error_on(verbose, "not a directory: %s", path); + return -3; } - if (stat(path, &st) != 0) { + if (statfs(path, &stfs) != 0) { error_on(verbose, "cannot access '%s': %m", path); return -1; } - if (dir_only && !S_ISDIR(st.st_mode)) { - error_on(verbose, "not a directory: %s", path); - return -3; + if (stfs.f_type != BTRFS_SUPER_MAGIC) { + error_on(verbose, "not a btrfs filesystem: %s", path); + return -2; } ret = open_file_or_dir(path, dirstream);