From patchwork Thu Oct 19 18:16:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 10018119 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BB0A760215 for ; Thu, 19 Oct 2017 18:16:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B765628DE8 for ; Thu, 19 Oct 2017 18:16:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC73428DEC; Thu, 19 Oct 2017 18:16:25 +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=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 0F03028DE8 for ; Thu, 19 Oct 2017 18:16:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754017AbdJSSQX (ORCPT ); Thu, 19 Oct 2017 14:16:23 -0400 Received: from mail-qt0-f195.google.com ([209.85.216.195]:44458 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752466AbdJSSQS (ORCPT ); Thu, 19 Oct 2017 14:16:18 -0400 Received: by mail-qt0-f195.google.com with SMTP id 8so15583436qtv.1 for ; Thu, 19 Oct 2017 11:16:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=IICQF80JFi1HhaCGfK/SdlyJD6H0fL90XTDDHWpRnnQ=; b=QFukSAqWHIVMWxiprDXOSPl4PVAf6etzzly+OUJEVTRJxLIQwycbt9mpPuXuDOYp3G qGOK3cNnlWQZyyjrdyLhQm3ZfKYaeVl8hPRd0RG5+m4ZzU5uJp03wx4FgLJL25PTl1aJ NM8txZ6haqzg9FGuczNvDbhNHJEux3vYDhsqf64jckaZc4SBnn0Fos7NexoA4bP52Jfb tr5BNaNCH4an7rNlkhN10+qVO9Hj6GwSpd0MmgL2sbuqBMWQHJAVM3BJArExJmAqndTO EBtjBqAWZyIYz75LWoZeoyzdHxyGWuxbElbXU6scl6zwvwfyWwfkDoIqIDeRW4A6rL65 +G3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=IICQF80JFi1HhaCGfK/SdlyJD6H0fL90XTDDHWpRnnQ=; b=LZaxGp4xvGz0bD03Lx4FFm73AdKC/Vauqra2SbMClHw171WPiB8kgHMHv86yIpZYZ3 PkA5W6TfXygrLXl1BiiTuvkTVLTMPfwdZRWMHnD9Fe6cq+vzrLAkVrbZ4v9NQ4VWiNEm lu7ScyIo9E7Mw8qjEuGY/hjCqgTO8FEK80J/C0Rbrtcaq+OkKDkuwFNJTMirW8e/pLiI 79gSR7juKC8Qc77z0as+Voe/XVf5ZufiN2FgWxDYt8K8sHDTzida3aCW7oJb/+fr6H3+ W0Fst6J6bizHZlPyYlETeDqKEIs/Djo7W3/HEWiOjQE8qUEx6OC16KbBQk/kQjgm0lHg s91Q== X-Gm-Message-State: AMCzsaX82N+324qwoYMe4gFESH/MJu19Y7QyKe/xdy3mXuYEMW1eiVQC FS1XVbybYcum3OcyFVVx3umomg== X-Google-Smtp-Source: ABhQp+Q/4Kg4ZBFxY6GswKr2JxY22gR3oA6eSw/XGLZydkP8YbSBSd5xoWOhpSXqKg3eVGYLXoSMvA== X-Received: by 10.200.42.118 with SMTP id l51mr3565148qtl.37.1508436978111; Thu, 19 Oct 2017 11:16:18 -0700 (PDT) Received: from localhost ([2606:a000:4381:1201:225:22ff:feb3:e51a]) by smtp.gmail.com with ESMTPSA id r6sm9597253qkh.22.2017.10.19.11.16.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Oct 2017 11:16:17 -0700 (PDT) From: Josef Bacik To: kernel-team@fb.com, linux-btrfs@vger.kernel.org Subject: [PATCH 8/8] btrfs: move btrfs_truncate_block out of trans handle Date: Thu, 19 Oct 2017 14:16:02 -0400 Message-Id: <1508436962-6851-9-git-send-email-josef@toxicpanda.com> X-Mailer: git-send-email 2.7.5 In-Reply-To: <1508436962-6851-1-git-send-email-josef@toxicpanda.com> References: <1508436962-6851-1-git-send-email-josef@toxicpanda.com> 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 Since we do a delalloc reserve in btrfs_truncate_block we can deadlock with freeze. If somebody else is trying to allocate metadata for this inode and it gets stuck in start_delalloc_inodes because of freeze we will deadlock. Be safe and move this outside of a trans handle. This also has a side-effect of making sure that we're not leaving stale data behind in the other_encoding or encryption case. Not an issue now since nobody uses it, but it would be a problem in the future. Signed-off-by: Josef Bacik --- fs/btrfs/inode.c | 119 ++++++++++++++++++++----------------------------------- 1 file changed, 44 insertions(+), 75 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 68e28375e159..c94e8938b574 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -4357,47 +4357,11 @@ static int truncate_space_check(struct btrfs_trans_handle *trans, } -static int truncate_inline_extent(struct inode *inode, - struct btrfs_path *path, - struct btrfs_key *found_key, - const u64 item_end, - const u64 new_size) -{ - struct extent_buffer *leaf = path->nodes[0]; - int slot = path->slots[0]; - struct btrfs_file_extent_item *fi; - u32 size = (u32)(new_size - found_key->offset); - struct btrfs_root *root = BTRFS_I(inode)->root; - - fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); - - if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { - loff_t offset = new_size; - loff_t page_end = ALIGN(offset, PAGE_SIZE); - - /* - * Zero out the remaining of the last page of our inline extent, - * instead of directly truncating our inline extent here - that - * would be much more complex (decompressing all the data, then - * compressing the truncated data, which might be bigger than - * the size of the inline extent, resize the extent, etc). - * We release the path because to get the page we might need to - * read the extent item from disk (data not in the page cache). - */ - btrfs_release_path(path); - return btrfs_truncate_block(inode, offset, page_end - offset, - 0); - } - - btrfs_set_file_extent_ram_bytes(leaf, fi, size); - size = btrfs_file_extent_calc_inline_size(size); - btrfs_truncate_item(root->fs_info, path, size, 1); - - if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) - inode_sub_bytes(inode, item_end + 1 - new_size); - - return 0; -} +/* + * Return this if we need to call truncate_block for the last bit of the + * truncate. + */ +#define NEED_TRUNCATE_BLOCK 1 /* * this can truncate away extent items, csum items and directory items. @@ -4558,11 +4522,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, if (found_type != BTRFS_EXTENT_DATA_KEY) goto delete; - if (del_item) - last_size = found_key.offset; - else - last_size = new_size; - if (extent_type != BTRFS_FILE_EXTENT_INLINE) { u64 num_dec; extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); @@ -4604,40 +4563,29 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, */ if (!del_item && btrfs_file_extent_encryption(leaf, fi) == 0 && - btrfs_file_extent_other_encoding(leaf, fi) == 0) { - + btrfs_file_extent_other_encoding(leaf, fi) == 0 && + btrfs_file_extent_compression(leaf, fi) == 0) { + u32 size = (u32)(new_size - found_key.offset); + btrfs_set_file_extent_ram_bytes(leaf, fi, size); + size = btrfs_file_extent_calc_inline_size(size); + btrfs_truncate_item(root->fs_info, path, size, 1); + } else if (!del_item) { /* - * Need to release path in order to truncate a - * compressed extent. So delete any accumulated - * extent items so far. + * We have to bail so the last_size is set to + * just before this extent. */ - if (btrfs_file_extent_compression(leaf, fi) != - BTRFS_COMPRESS_NONE && pending_del_nr) { - err = btrfs_del_items(trans, root, path, - pending_del_slot, - pending_del_nr); - if (err) { - btrfs_abort_transaction(trans, - err); - goto error; - } - pending_del_nr = 0; - } + err = NEED_TRUNCATE_BLOCK; + break; + } - err = truncate_inline_extent(inode, path, - &found_key, - item_end, - new_size); - if (err) { - btrfs_abort_transaction(trans, err); - goto error; - } - } else if (test_bit(BTRFS_ROOT_REF_COWS, - &root->state)) { + if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) inode_sub_bytes(inode, item_end + 1 - new_size); - } } delete: + if (del_item) + last_size = found_key.offset; + else + last_size = new_size; if (del_item) { if (!pending_del_nr) { /* no pending yet, add ourselves */ @@ -9335,12 +9283,12 @@ static int btrfs_truncate(struct inode *inode) ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, BTRFS_EXTENT_DATA_KEY); + trans->block_rsv = &fs_info->trans_block_rsv; if (ret != -ENOSPC && ret != -EAGAIN) { err = ret; break; } - trans->block_rsv = &fs_info->trans_block_rsv; ret = btrfs_update_inode(trans, root, inode); if (ret) { err = ret; @@ -9364,6 +9312,27 @@ static int btrfs_truncate(struct inode *inode) trans->block_rsv = rsv; } + /* + * We can't call btrfs_truncate_block inside a trans handle as we could + * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know + * we've truncated everything except the last little bit, and can do + * btrfs_truncate_block and then update the disk_i_size. + */ + if (ret == NEED_TRUNCATE_BLOCK) { + btrfs_end_transaction(trans); + btrfs_btree_balance_dirty(fs_info); + + ret = btrfs_truncate_block(inode, inode->i_size, 0, 0); + if (ret) + goto out; + trans = btrfs_start_transaction(root, 1); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + goto out; + } + btrfs_ordered_update_i_size(inode, inode->i_size, NULL); + } + if (ret == 0 && inode->i_nlink > 0) { trans->block_rsv = root->orphan_block_rsv; ret = btrfs_orphan_del(trans, BTRFS_I(inode));