From patchwork Mon Aug 31 08:54:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 7109321 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C4E809F32B for ; Wed, 2 Sep 2015 07:52:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B51B920632 for ; Wed, 2 Sep 2015 07:52:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1448920631 for ; Wed, 2 Sep 2015 07:52:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752463AbbIBHvT (ORCPT ); Wed, 2 Sep 2015 03:51:19 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:42571 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1752178AbbIBHvS (ORCPT ); Wed, 2 Sep 2015 03:51:18 -0400 X-Greylist: delayed 152915 seconds by postgrey-1.27 at vger.kernel.org; Wed, 02 Sep 2015 03:51:12 EDT Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 31 Aug 2015 16:55:47 +0800 Received: from localhost.localdomain (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t7V8udC3031126 for ; Mon, 31 Aug 2015 16:56:53 +0800 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH RFC 07/14] btrfs: qgroup: Introduce function to release reserved range Date: Mon, 31 Aug 2015 16:54:38 +0800 Message-Id: <1441011286-23733-8-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1441011286-23733-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1441011286-23733-1-git-send-email-quwenruo@cn.fujitsu.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Introduce new function release_data_range() to release reserved ranges. It will iterate through all existing ranges and remove/shrink them. Note this function will not free reserved space, as the range can be released in the following conditions: 1) The dirty range gets written to disk. In this case, reserved range will be released but reserved bytes will not be freed until the delayed_ref is run. 2) Truncate In this case, dirty ranges will be released and reserved bytes will also be freed. So the new function won't free reserved space, but record them into parameter if called needs. Signed-off-by: Qu Wenruo --- fs/btrfs/qgroup.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index 31ddc6d..eee07c2 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -2811,6 +2811,136 @@ out: return ret; } +/* Small helper used in release_data_range() to update rsv map */ +static inline void __update_rsv(struct btrfs_qgroup_data_rsv_map *map, + u64 *reserved, u64 cur_rsv) +{ + if (reserved) + *reserved += cur_rsv; + if (WARN_ON(map->reserved < cur_rsv)) + map->reserved = 0; + else + map->reserved -= cur_rsv; +} + +/* + * Release the range [start, start + len) from rsv map. + * + * The behavior should be much like reserve_data_range(). + * @tmp: the allocated memory for case which need to split existing + * range into two. + * @reserved: the number of bytes that may need to free + * Return > 0 if 'tmp' memory is used and release range successfully + * Return 0 if 'tmp' memory is not used and release range successfully + * Return < 0 for error + */ +static int release_data_range(struct btrfs_qgroup_data_rsv_map *map, + struct data_rsv_range *tmp, + u64 start, u64 len, u64 *reserved) +{ + struct data_rsv_range *range; + u64 cur_rsv; + int ret = 0; + + range = find_reserve_range(map, start); + /* empty tree, just return */ + if (!range) + return 0; + /* + * For split case + * |<----desired---->| + * |////////////////////////////////////////////| + * In this case, we need to insert one new range. + */ + if (range->start < start && range->start + range->len > start + len) { + u64 new_start = start + len; + u64 new_len = range->start + range->len - start - len; + + cur_rsv = len; + if (reserved) + *reserved += cur_rsv; + map->reserved -= cur_rsv; + + range->len = start - range->start; + ret = insert_data_range(map, tmp, new_start, new_len); + WARN_ON(ret <= 0); + return 1; + } + + /* + * Iterate until the end of the range and free release all + * reserved data from map. + * We iterate by existing range, as that will makes codes a + * little more clean. + * + * |<---------desired------------------------>| + * |//1//| |//2//| |//3//| |//4//| + */ + while (range->start < start + len) { + struct rb_node *next = NULL; + + /* + * |<---desired---->| + * |///////| + */ + if (unlikely(range->start + range->len <= start)) + goto next; + + /* + * |<----desired---->| + * |///////| + */ + if (range->start < start && + range->start + range->len > start) { + cur_rsv = range->start + range->len - start; + __update_rsv(map, reserved, cur_rsv); + + range->len = start - range->start; + goto next; + } + + /* + * |<--desired-->| + * |/////| + * Including same start/end case, so other case don't need + * to check start/end equal case and don't need bother + * deleting range. + */ + if (range->start >= start && + range->start + range->len <= start + len) { + cur_rsv = range->len; + __update_rsv(map, reserved, cur_rsv); + + next = rb_next(&range->node); + rb_erase(&range->node, &map->root); + kfree(range); + goto next; + + } + + /* + * |<--desired-->| + * |///////| + */ + if (range->start < start + len && + range->start + range->len > start + len) { + cur_rsv = start + len - range->start; + __update_rsv(map, reserved, cur_rsv); + + range->start = start + len; + range->len = range->start + range->len - start - len; + goto next; + } +next: + if (!next) + next = rb_next(&range->node); + if (!next) + break; + range = rb_entry(next, struct data_rsv_range, node); + } + return 0; +} + /* * Init data_rsv_map for a given inode. *