From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F4C9C433F5 for ; Tue, 14 Sep 2021 04:49:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6784560EFF for ; Tue, 14 Sep 2021 04:49:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234125AbhINEuU (ORCPT ); Tue, 14 Sep 2021 00:50:20 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:56952 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230311AbhINEuT (ORCPT ); Tue, 14 Sep 2021 00:50:19 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id ED8861FDCC; Tue, 14 Sep 2021 04:49:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1631594941; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AW2kzkDBOg0XUCmxQF8gtpG9FW5LcLCjIHnqioBHc/Q=; b=jpClqFfuQrtptNNvHl5wilvq4pesBUrNW9sjtTBg2KNGj36hdcmfE2Ykq2SykQkA1i+XEE x1e9NwTVgFx//FULVQIllLzxJJ9ogdpCeAxd+410uBLrzU1OVoKSixdUyaRoRficlW4k1X K43LvGMKdK+VaSES9PBCY3IkKwDYdIk= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C71EE13E48; Tue, 14 Sep 2021 04:49:01 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id IN4WLr0pQGEUQAAAMHmgww (envelope-from ); Tue, 14 Sep 2021 04:49:01 +0000 Subject: Re: [PATCH 5/8] btrfs-progs: Add btrfs_uuid_tree_remove To: Qu Wenruo , linux-btrfs@vger.kernel.org References: <20210913131729.37897-1-nborisov@suse.com> <20210913131729.37897-6-nborisov@suse.com> From: Nikolay Borisov Message-ID: <922efc8b-3422-fa61-7936-692b95ada411@suse.com> Date: Tue, 14 Sep 2021 07:49:01 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On 14.09.21 г. 4:07, Qu Wenruo wrote: > > > On 2021/9/13 下午9:17, Nikolay Borisov wrote: >> It will be used to clear received data on RW snapshots that were >> received. >> >> Signed-off-by: Nikolay Borisov >> --- >>   kernel-shared/ctree.h     |  3 ++ >>   kernel-shared/uuid-tree.c | 82 +++++++++++++++++++++++++++++++++++++++ >>   2 files changed, 85 insertions(+) >> >> diff --git a/kernel-shared/ctree.h b/kernel-shared/ctree.h >> index 91a85796a678..158281a9fd67 100644 >> --- a/kernel-shared/ctree.h >> +++ b/kernel-shared/ctree.h >> @@ -2860,6 +2860,9 @@ int btrfs_uuid_tree_add(struct >> btrfs_trans_handle *trans, u8 *uuid, u8 type, >>               u64 subvol_id_cpu); >> >>   int btrfs_is_empty_uuid(u8 *uuid); >> +int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 >> *uuid, u8 type, >> +               u64 subid); >> + > > I have checked the callers in the incoming check patches, they are all > starting a new trans on uuid tree, thus there is no need to start a > trans out of this function. > > Let's just pass the subvolume root into the function, and start a new > trans in side the function. > > So that we only need one parameter @root, everything else can be fetched > from @root, and we can also modify the root item and uuid tree just in > one go. I disagree, this function is a direct copy from the kernel with the only adjustment that btrfs_uuid_to_key doesn't set key.type. If I change it in progs then this means it won't be unified with the kernel when this happens (if ever). I really rather keep shared functions as close as possible. > > Thanks, > Qu >> >>   static inline int is_fstree(u64 rootid) >>   { >> diff --git a/kernel-shared/uuid-tree.c b/kernel-shared/uuid-tree.c >> index 51a7b5d9ff5d..0f0fbf667dda 100644 >> --- a/kernel-shared/uuid-tree.c >> +++ b/kernel-shared/uuid-tree.c >> @@ -120,3 +120,85 @@ int btrfs_is_empty_uuid(u8 *uuid) >>       } >>       return 1; >>   } >> + >> +int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 >> *uuid, u8 type, >> +        u64 subid) >> +{ >> +    struct btrfs_fs_info *fs_info = trans->fs_info; >> +    struct btrfs_root *uuid_root = fs_info->uuid_root; >> +    int ret; >> +    struct btrfs_path *path = NULL; >> +    struct btrfs_key key; >> +    struct extent_buffer *eb; >> +    int slot; >> +    unsigned long offset; >> +    u32 item_size; >> +    unsigned long move_dst; >> +    unsigned long move_src; >> +    unsigned long move_len; >> + >> +    if (!uuid_root) { >> +        ret = -EINVAL; >> +        goto out; >> +    } >> + >> +    btrfs_uuid_to_key(uuid, &key); >> +    key.type = type; >> + >> +    path = btrfs_alloc_path(); >> +    if (!path) { >> +        ret = -ENOMEM; >> +        goto out; >> +    } >> + >> +    ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1); >> +    if (ret < 0) { >> +        warning("error %d while searching for uuid item!", ret); >> +        goto out; >> +    } >> +    if (ret > 0) { >> +        ret = -ENOENT; >> +        goto out; >> +    } >> + >> + >> +    eb = path->nodes[0]; >> +    slot = path->slots[0]; >> +    offset = btrfs_item_ptr_offset(eb, slot); >> +    item_size = btrfs_item_size_nr(eb, slot); >> +    if (!IS_ALIGNED(item_size, sizeof(u64))) { >> +        warning("uuid item with illegal size %lu!",    (unsigned >> long)item_size); >> +        ret = -ENOENT; >> +        goto out; >> +    } >> +    while (item_size) { >> +        __le64 read_subid; >> + >> +        read_extent_buffer(eb, &read_subid, offset, sizeof(read_subid)); >> +        if (le64_to_cpu(read_subid) == subid) >> +            break; >> +        offset += sizeof(read_subid); >> +        item_size -= sizeof(read_subid); >> +    } >> + >> +    if (!item_size) { >> +        ret = -ENOENT; >> +        goto out; >> +    } >> + >> +    item_size = btrfs_item_size_nr(eb, slot); >> +    if (item_size == sizeof(subid)) { >> +        ret = btrfs_del_item(trans, uuid_root, path); >> +        goto out; >> +    } >> + >> +    move_dst = offset; >> +    move_src = offset + sizeof(subid); >> +    move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot)); >> +    memmove_extent_buffer(eb, move_dst, move_src, move_len); >> +    btrfs_truncate_item(path, item_size - sizeof(subid), 1); >> + >> +out: >> +    btrfs_free_path(path); >> +    return ret; >> +} >> >