From patchwork Fri Mar 1 11:36:01 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Shilong X-Patchwork-Id: 2200961 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 69AD23FCF6 for ; Fri, 1 Mar 2013 11:36:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752071Ab3CALgI (ORCPT ); Fri, 1 Mar 2013 06:36:08 -0500 Received: from mail-da0-f52.google.com ([209.85.210.52]:37824 "EHLO mail-da0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751826Ab3CALgH (ORCPT ); Fri, 1 Mar 2013 06:36:07 -0500 Received: by mail-da0-f52.google.com with SMTP id x33so1365969dad.39 for ; Fri, 01 Mar 2013 03:36:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer; bh=EbOge8TSWAOLTqrZfd11bkZpmyEpuYnaabd8jLbUfJw=; b=RZYhGYQMGhuKfjeX4r5pU0bV7elOmTTU4XQ1C9lIe2FWAdmvbeZ0CJrtmK0QBrLtaP 6O6kK91SAR0vLiC0hinEcqQlULynvfM7kpI3JuW2Up3sUozUiSxp/JC/c/7C/1J/S20Z i65aBEHo7bXstX1cAgdyeZciRTcj5iJLRXh066BCBZLfrZFm687COoRtl42MLyu4WqpK 1GjK+AlN6KeY6coiGzCANQv2bIU09Y7kCGf7Rl73iLpbTFkCo/O13q38yjoK9KPxp4pj Dpapr9mLr102vl15QMUrUKIOqNYvs5TC1p+yLiOUciUI/qNEwlB16/hu9FM2CKaFVryC UD5A== X-Received: by 10.68.31.230 with SMTP id d6mr14124869pbi.41.1362137766480; Fri, 01 Mar 2013 03:36:06 -0800 (PST) Received: from localhost.localdomain ([223.65.184.168]) by mx.google.com with ESMTPS id b9sm11813922pba.6.2013.03.01.03.36.03 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 01 Mar 2013 03:36:05 -0800 (PST) From: Wang Shilong To: linux-btrfs@vger.kernel.org Cc: sensille@gmx.net, list.btrfs@jan-o-sch.net, chris.mason@fusionio.com, wangshilong1991@gmail.com Subject: [PATCH 2/2] Btrfs: cleanup to make the function btrfs_delalloc_reserve_metadata more logic Date: Fri, 1 Mar 2013 19:36:01 +0800 Message-Id: <1362137761-1864-1-git-send-email-wangshilong1991@gmail.com> X-Mailer: git-send-email 1.7.11.7 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Wang Shilong The original code is a little confusing and not clear, The right way to deal with the kernel code like this: [...] if (ret) goto out; [...] So i move the common clean_up code to the place labeled with out_fail, this will be easier to maintain. Signed-off-by: Wang Shilong --- fs/btrfs/extent-tree.c | 82 ++++++++++++++++++++++------------------------- 1 files changed, 38 insertions(+), 44 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index f0c960b..8e57870 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -4721,6 +4721,8 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; int ret = 0; bool delalloc_lock = true; + u64 to_free = 0; + unsigned dropped; /* If we are a free space inode we need to not flush since we will be in * the middle of a transaction commit. We also don't need the delalloc @@ -4764,55 +4766,19 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) csum_bytes = BTRFS_I(inode)->csum_bytes; spin_unlock(&BTRFS_I(inode)->lock); - if (root->fs_info->quota_enabled) + if (root->fs_info->quota_enabled) { ret = btrfs_qgroup_reserve(root, num_bytes + nr_extents * root->leafsize); + if (ret) + goto out_fail; + } - /* - * ret != 0 here means the qgroup reservation failed, we go straight to - * the shared error handling then. - */ - if (ret == 0) { - ret = reserve_metadata_bytes(root, block_rsv, - to_reserve, flush); - if (ret && root->fs_info->quota_enabled) { + ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); + if (unlikely(ret)) { + if (root->fs_info->quota_enabled) btrfs_qgroup_free(root, num_bytes + nr_extents * root->leafsize); - } - } - - if (ret) { - u64 to_free = 0; - unsigned dropped; - - spin_lock(&BTRFS_I(inode)->lock); - dropped = drop_outstanding_extent(inode); - /* - * If the inodes csum_bytes is the same as the original - * csum_bytes then we know we haven't raced with any free()ers - * so we can just reduce our inodes csum bytes and carry on. - * Otherwise we have to do the normal free thing to account for - * the case that the free side didn't free up its reserve - * because of this outstanding reservation. - */ - if (BTRFS_I(inode)->csum_bytes == csum_bytes) - calc_csum_metadata_size(inode, num_bytes, 0); - else - to_free = calc_csum_metadata_size(inode, num_bytes, 0); - spin_unlock(&BTRFS_I(inode)->lock); - if (dropped) - to_free += btrfs_calc_trans_metadata_size(root, dropped); - - if (to_free) { - btrfs_block_rsv_release(root, block_rsv, to_free); - trace_btrfs_space_reservation(root->fs_info, - "delalloc", - btrfs_ino(inode), - to_free, 0); - } - if (delalloc_lock) - mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); - return ret; + goto out_fail; } spin_lock(&BTRFS_I(inode)->lock); @@ -4833,6 +4799,34 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) block_rsv_add_bytes(block_rsv, to_reserve, 1); return 0; + +out_fail: + spin_lock(&BTRFS_I(inode)->lock); + dropped = drop_outstanding_extent(inode); + /* + * If the inodes csum_bytes is the same as the original + * csum_bytes then we know we haven't raced with any free()ers + * so we can just reduce our inodes csum bytes and carry on. + * Otherwise we have to do the normal free thing to account for + * the case that the free side didn't free up its reserve + * because of this outstanding reservation. + */ + if (BTRFS_I(inode)->csum_bytes == csum_bytes) + calc_csum_metadata_size(inode, num_bytes, 0); + else + to_free = calc_csum_metadata_size(inode, num_bytes, 0); + spin_unlock(&BTRFS_I(inode)->lock); + if (dropped) + to_free += btrfs_calc_trans_metadata_size(root, dropped); + + if (to_free) { + btrfs_block_rsv_release(root, block_rsv, to_free); + trace_btrfs_space_reservation(root->fs_info, "delalloc", + btrfs_ino(inode), to_free, 0); + } + if (delalloc_lock) + mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); + return ret; } /**