From patchwork Mon Feb 25 18:16:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Borowski X-Patchwork-Id: 10828989 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 939AD1390 for ; Mon, 25 Feb 2019 18:16:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 869DC2BCB4 for ; Mon, 25 Feb 2019 18:16:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 79F5A2BAF1; Mon, 25 Feb 2019 18:16:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B73E32BCB4 for ; Mon, 25 Feb 2019 18:16:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728858AbfBYSQz (ORCPT ); Mon, 25 Feb 2019 13:16:55 -0500 Received: from tartarus.angband.pl ([54.37.238.230]:33782 "EHLO tartarus.angband.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728852AbfBYSQz (ORCPT ); Mon, 25 Feb 2019 13:16:55 -0500 Received: from [2a02:a31c:853f:a300::6] (helo=umbar.angband.pl) by tartarus.angband.pl with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gyKo6-00029H-E0; Mon, 25 Feb 2019 19:16:51 +0100 Received: from kilobyte by umbar.angband.pl with local (Exim 4.92) (envelope-from ) id 1gyKo4-0002hL-LL; Mon, 25 Feb 2019 19:16:48 +0100 From: Adam Borowski To: David Sterba , linux-btrfs@vger.kernel.org, Mark Fasheh Cc: Adam Borowski Date: Mon, 25 Feb 2019 19:16:44 +0100 Message-Id: <20190225181644.10262-2-kilobyte@angband.pl> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225181644.10262-1-kilobyte@angband.pl> References: <20190225181644.10262-1-kilobyte@angband.pl> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a02:a31c:853f:a300::6 X-SA-Exim-Mail-From: kilobyte@angband.pl Subject: [PATCH resend 2/2] btrfs-progs: defrag: open files RO on new enough kernels X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on tartarus.angband.pl) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Defragging an executable conflicts both way with it being run, resulting in ETXTBSY. This either makes defrag fail or prevents the program from being executed. Kernels 4.19-rc1 and later allow defragging files you could have possibly opened rw, even if the passed descriptor is ro (commit 616d374efa23). Signed-off-by: Adam Borowski --- cmds-filesystem.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmds-filesystem.c b/cmds-filesystem.c index b8beec13..0eb052dc 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -39,12 +40,14 @@ #include "list_sort.h" #include "disk-io.h" #include "help.h" +#include "fsfeatures.h" /* * for btrfs fi show, we maintain a hash of fsids we've already printed. * This way we don't print dups if a given FS is mounted more than once. */ static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,}; +static mode_t defrag_open_mode = O_RDONLY; static const char * const filesystem_cmd_group_usage[] = { "btrfs filesystem [] []", @@ -880,7 +883,7 @@ static int defrag_callback(const char *fpath, const struct stat *sb, if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) { if (defrag_global_verbose) printf("%s\n", fpath); - fd = open(fpath, O_RDWR); + fd = open(fpath, defrag_open_mode); if (fd < 0) { goto error; } @@ -917,6 +920,9 @@ static int cmd_filesystem_defrag(int argc, char **argv) int compress_type = BTRFS_COMPRESS_NONE; DIR *dirstream; + if (get_running_kernel_version() < KERNEL_VERSION(4,19,0)) + defrag_open_mode = O_RDWR; + /* * Kernel has a different default (256K) that is supposed to be safe, * but it does not defragment very well. The 32M will likely lead to @@ -1017,7 +1023,7 @@ static int cmd_filesystem_defrag(int argc, char **argv) int defrag_err = 0; dirstream = NULL; - fd = open_file_or_dir(argv[i], &dirstream); + fd = open_file_or_dir3(argv[i], &dirstream, defrag_open_mode); if (fd < 0) { error("cannot open %s: %m", argv[i]); ret = -errno;