From patchwork Tue Sep 8 08:37:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 7144241 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 2DCB19F1D3 for ; Wed, 9 Sep 2015 05:11:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2ED2020815 for ; Wed, 9 Sep 2015 05:11:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1D28C20948 for ; Wed, 9 Sep 2015 05:11:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752515AbbIIFF3 (ORCPT ); Wed, 9 Sep 2015 01:05:29 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:48982 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751469AbbIIFFW (ORCPT ); Wed, 9 Sep 2015 01:05:22 -0400 X-IronPort-AV: E=Sophos;i="5.13,665,1427731200"; d="scan'208";a="49825029" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 08 Sep 2015 16:38:24 +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 t888dQv6019126 for ; Tue, 8 Sep 2015 16:39:37 +0800 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 07/19] btrfs: qgroup: Introduce function to release reserved range Date: Tue, 8 Sep 2015 16:37:24 +0800 Message-Id: <1441701456-8034-8-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.5.1 In-Reply-To: <1441701456-8034-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1441701456-8034-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 337b784..e24c10d 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -2849,6 +2849,136 @@ int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len) 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 = 0; + 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; + int range_freed = 0; + + /* + * |<---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; + + 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; + + range_freed = 1; + 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; + + range->len = range->start + range->len - start - len; + range->start = start + len; + goto next; + } +next: + __update_rsv(map, reserved, cur_rsv); + if (!range_freed) + 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. *