From patchwork Thu May 10 06:21:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10391259 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 1282A6028E for ; Thu, 10 May 2018 06:21:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 02B0C2873A for ; Thu, 10 May 2018 06:21:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB4B0288AD; Thu, 10 May 2018 06:21:47 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, 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 752CD2873A for ; Thu, 10 May 2018 06:21:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933944AbeEJGVp (ORCPT ); Thu, 10 May 2018 02:21:45 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:33346 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933589AbeEJGVd (ORCPT ); Thu, 10 May 2018 02:21:33 -0400 Received: by mail-pg0-f66.google.com with SMTP id i194-v6so507490pgd.0 for ; Wed, 09 May 2018 23:21:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=xnhAJV+UlbOu7psAReB4UG6F0HuhtY+dJEyB/ANkAXE=; b=OEnJSk3LiD9OlvF2bbB/YAD/KjoQaUP6I5/6rPXFyMNJiRYEPF7OteZb17P0maJd2W RWMtMlRqElAteC8RKZUcBkavwzr3gIpmH3vClKxw+a33w/DBHtM/rS1fOYOVMkkDFaW+ Xm40yJVXEvfgFIXswoOGiytzU031Y177OrYGL7UGWp5dMbSA5goC8C5joo3b8GylBWkS VbQ5giFnV/ymRg23CCC8BuaYZRAKjjLhHYJOTuL1ENzltK+2OWpJutGUMxXrDfgMDRGk h2evMwwZi6Bgu5bYsGmcZCDLmSGUsiL39CWbZUSIVeAl0gfLcJZJWAp6ZpUvrRHScDaC HT/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=xnhAJV+UlbOu7psAReB4UG6F0HuhtY+dJEyB/ANkAXE=; b=jTHl1Uq7vjOI0klxYR1hFG5rNldKaW0RgN3PiFSaXVcF4qx1nVjAMz2lwLqxtbFbgm 3cvsmORBYMTVvFpskEpjOntwe0TLo7ubreHiIXTAGUgTUSADRVpBXsTduDlt1wLsXABO hIK2h3Wgw1RxxR2ahJjfU0p8o/kXU9U4pmi2zgsZYtiBKkEH/D+1Fm+1M4Ez5ODnf4xx dH4/knYDFOgX2Mn7zE1yYuqvb+rCEvwVr2zm04xpfEEvb0+HkgtCAZIw7rO488XfYUYB moIQxeLAvc0WKgp7LoAUAGpRuAXLiPdymlqYuMX+D6lLMAZdcY7MFVLm1UwjCBkzFCLr Zc/A== X-Gm-Message-State: ALKqPwdbjHFaC98Rixg9Yj05oX+vZkDd1VlUsN9CFTFMUhqTOjg2kT5Z HBWuWEDqCIS3F3viYTHxWx9GDT57jdE= X-Google-Smtp-Source: AB8JxZoZr7/2f0D16VzYZMlVgVnLXcok3HYVYzX28BkLbAL1fX4VoJvOOf7antr9lqsFaN0Gqioyvw== X-Received: by 2002:a63:3c08:: with SMTP id j8-v6mr95917pga.203.1525933292413; Wed, 09 May 2018 23:21:32 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:180::1:65fc]) by smtp.gmail.com with ESMTPSA id 68-v6sm496206pff.35.2018.05.09.23.21.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 May 2018 23:21:31 -0700 (PDT) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com, Chris Mason , Josef Bacik Subject: [PATCH 07/10] Btrfs: refactor btrfs_evict_inode() reserve refill dance Date: Wed, 9 May 2018 23:21:08 -0700 Message-Id: <9abab6d7f00cdb1b83b2041336553e8ca24b570f.1525932796.git.osandov@fb.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: In-Reply-To: References: 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 From: Omar Sandoval The truncate loop in btrfs_evict_inode() does two things at once: - It refills the temporary block reserve, potentially stealing from the global reserve or committing - It calls btrfs_truncate_inode_items() The tangle of continues hides the fact that these two steps are actually separate. Split the first step out into a separate function both for clarity and so that we can reuse it in a later patch. Signed-off-by: Omar Sandoval Reviewed-by: Nikolay Borisov --- fs/btrfs/inode.c | 113 ++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 9a6a4e626e01..348dc57920f5 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -5224,13 +5224,52 @@ static void evict_inode_truncate_pages(struct inode *inode) spin_unlock(&io_tree->lock); } +static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root, + struct btrfs_block_rsv *rsv, + u64 min_size) +{ + struct btrfs_fs_info *fs_info = root->fs_info; + struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; + int failures = 0; + + for (;;) { + struct btrfs_trans_handle *trans; + int ret; + + ret = btrfs_block_rsv_refill(root, rsv, min_size, + BTRFS_RESERVE_FLUSH_LIMIT); + + if (ret && ++failures > 2) { + btrfs_warn(fs_info, + "could not allocate space for a delete; will truncate on mount"); + return ERR_PTR(-ENOSPC); + } + + trans = btrfs_join_transaction(root); + if (IS_ERR(trans) || !ret) + return trans; + + /* + * Try to steal from the global reserve if there is space for + * it. + */ + if (!btrfs_check_space_for_delayed_refs(trans, fs_info) && + !btrfs_block_rsv_migrate(global_rsv, rsv, min_size, 0)) + return trans; + + /* If not, commit and try again. */ + ret = btrfs_commit_transaction(trans); + if (ret) + return ERR_PTR(ret); + } +} + void btrfs_evict_inode(struct inode *inode) { struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); struct btrfs_trans_handle *trans; struct btrfs_root *root = BTRFS_I(inode)->root; - struct btrfs_block_rsv *rsv, *global_rsv; - int steal_from_global = 0; + struct btrfs_block_rsv *rsv; u64 min_size; int ret; @@ -5286,85 +5325,17 @@ void btrfs_evict_inode(struct inode *inode) } rsv->size = min_size; rsv->failfast = 1; - global_rsv = &fs_info->global_block_rsv; btrfs_i_size_write(BTRFS_I(inode), 0); - /* - * This is a bit simpler than btrfs_truncate since we've already - * reserved our space for our orphan item in the unlink, so we just - * need to reserve some slack space in case we add bytes and update - * inode item when doing the truncate. - */ while (1) { - ret = btrfs_block_rsv_refill(root, rsv, min_size, - BTRFS_RESERVE_FLUSH_LIMIT); - - /* - * Try and steal from the global reserve since we will - * likely not use this space anyway, we want to try as - * hard as possible to get this to work. - */ - if (ret) - steal_from_global++; - else - steal_from_global = 0; - ret = 0; - - /* - * steal_from_global == 0: we reserved stuff, hooray! - * steal_from_global == 1: we didn't reserve stuff, boo! - * steal_from_global == 2: we've committed, still not a lot of - * room but maybe we'll have room in the global reserve this - * time. - * steal_from_global == 3: abandon all hope! - */ - if (steal_from_global > 2) { - btrfs_warn(fs_info, - "Could not get space for a delete, will truncate on mount %d", - ret); - btrfs_orphan_del(NULL, BTRFS_I(inode)); - btrfs_free_block_rsv(fs_info, rsv); - goto no_delete; - } - - trans = btrfs_join_transaction(root); + trans = evict_refill_and_join(root, rsv, min_size); if (IS_ERR(trans)) { btrfs_orphan_del(NULL, BTRFS_I(inode)); btrfs_free_block_rsv(fs_info, rsv); goto no_delete; } - /* - * We can't just steal from the global reserve, we need to make - * sure there is room to do it, if not we need to commit and try - * again. - */ - if (steal_from_global) { - if (!btrfs_check_space_for_delayed_refs(trans, fs_info)) - ret = btrfs_block_rsv_migrate(global_rsv, rsv, - min_size, 0); - else - ret = -ENOSPC; - } - - /* - * Couldn't steal from the global reserve, we have too much - * pending stuff built up, commit the transaction and try it - * again. - */ - if (ret) { - ret = btrfs_commit_transaction(trans); - if (ret) { - btrfs_orphan_del(NULL, BTRFS_I(inode)); - btrfs_free_block_rsv(fs_info, rsv); - goto no_delete; - } - continue; - } else { - steal_from_global = 0; - } - trans->block_rsv = rsv; ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);