From patchwork Thu Mar 30 06:21:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9652927 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 1EBA3602BD for ; Thu, 30 Mar 2017 06:21:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F89D2856C for ; Thu, 30 Mar 2017 06:21:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 047F628571; Thu, 30 Mar 2017 06:21:33 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 74B562856C for ; Thu, 30 Mar 2017 06:21:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932410AbdC3GVa (ORCPT ); Thu, 30 Mar 2017 02:21:30 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:13217 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S932097AbdC3GV3 (ORCPT ); Thu, 30 Mar 2017 02:21:29 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="17165824" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 30 Mar 2017 14:21:23 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id D658149F9A82 for ; Thu, 30 Mar 2017 14:21:22 +0800 (CST) Received: from localhost.localdomain (10.167.226.34) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 30 Mar 2017 14:21:22 +0800 From: Qu Wenruo To: CC: Su Yue Subject: [PATCH v3 08/19] btrfs-progs: csum: Introduce function to read out data csums Date: Thu, 30 Mar 2017 14:21:04 +0800 Message-ID: <20170330062116.14379-9-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.12.1 In-Reply-To: <20170330062116.14379-1-quwenruo@cn.fujitsu.com> References: <20170330062116.14379-1-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.34] X-yoursite-MailScanner-ID: D658149F9A82.A0B41 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com 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 Introduce a new function: btrfs_read_data_csums(), to read out csums for sectors in range. This is quite useful for read out data csum so we don't need to do it using open code. Signed-off-by: Qu Wenruo Signed-off-by: Su Yue --- Makefile | 2 +- csum.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ctree.h | 4 ++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 csum.c diff --git a/Makefile b/Makefile index 89dd8d5d..24c3057b 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ objects = ctree.o disk-io.o kernel-lib/radix-tree.o extent-tree.o print-tree.o \ qgroup.o free-space-cache.o kernel-lib/list_sort.o props.o \ kernel-shared/ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \ inode.o file.o find-root.o free-space-tree.o help.o send-dump.o \ - fsfeatures.o messages.o kernel-lib/tables.o kernel-lib/raid56.o + fsfeatures.o messages.o kernel-lib/tables.o kernel-lib/raid56.o csum.o cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \ cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \ diff --git a/csum.c b/csum.c new file mode 100644 index 00000000..2c886bf4 --- /dev/null +++ b/csum.c @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2017 Fujitsu. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#include "kerncompat.h" +#include "kernel-lib/bitops.h" +#include "ctree.h" +#include "utils.h" + +/* + * TODO: + * 1) Add write support for csum + * So we can write new data extents and add csum into csum tree + * + * Get csums of range[@start, @start + len). + * + * @start: Start offset, shall be aligned to sectorsize. + * @len: Length, shall be aligned to sectorsize. + * @csum_ret: The size of csum_ret shall be @len / sectorsize * csum_size. + * @bit_map: Every bit corresponds to the offset have csum or not. + * The size in byte of bit_map should be + * round_up(csum_ret's size/csum_size, BITS_PER_BYTE). + * + * Returns 0 means success + * Returns >0 means on error + * Returns <0 means on fatal error + */ + +int btrfs_read_data_csums(struct btrfs_fs_info *fs_info, u64 start, u64 len, + void *csum_ret, unsigned long *bitmap_ret) + +{ + struct btrfs_path path; + struct btrfs_key key; + struct btrfs_root *csum_root = fs_info->csum_root; + u32 item_offset; + u32 item_size; + u32 final_offset; + u32 final_len; + u32 sectorsize = fs_info->tree_root->sectorsize; + u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); + u64 cur_start; + u64 cur_end; + int found = 0; + int ret; + + ASSERT(IS_ALIGNED(start, sectorsize)); + ASSERT(IS_ALIGNED(len, sectorsize)); + ASSERT(csum_ret); + ASSERT(bitmap_ret); + + memset(bitmap_ret, 0, round_up(len / sectorsize, BITS_PER_BYTE)); + btrfs_init_path(&path); + + key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; + key.type = BTRFS_EXTENT_CSUM_KEY; + key.offset = start; + + ret = btrfs_search_slot(NULL, csum_root, &key, &path, 0, 0); + if (ret < 0) + goto out; + if (ret > 0) { + ret = btrfs_previous_item(csum_root, &path, + BTRFS_EXTENT_CSUM_OBJECTID, + BTRFS_EXTENT_CSUM_KEY); + if (ret < 0) + goto out; + } + /* The csum tree may be empty. */ + if (!btrfs_header_nritems(path.nodes[0])) + goto next; + + while (1) { + btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); + + if (!IS_ALIGNED(key.offset, sectorsize)) { + error("csum item bytenr %llu is not aligned to %u", + key.offset, sectorsize); + ret = -EIO; + break; + } + /* exceeds end */ + if (key.offset >= start + len) + break; + + item_offset = btrfs_item_ptr_offset(path.nodes[0], + path.slots[0]); + item_size = btrfs_item_size_nr(path.nodes[0], path.slots[0]); + + if (key.offset + item_size / csum_size * sectorsize < start) + goto next; + + /* get start of the extent */ + cur_start = max(start, key.offset); + /* get end of the extent */ + cur_end = min(start + len, key.offset + item_size / csum_size * + sectorsize); + + final_offset = (cur_start - key.offset) / sectorsize * + csum_size + item_offset; + final_len = (cur_end - cur_start) / sectorsize * csum_size; + read_extent_buffer(path.nodes[0], + (csum_ret + (cur_start - start) / + sectorsize * csum_size), + final_offset, final_len); + + for (u32 i = 0; i != final_len / csum_size; i++) + set_bit(i + (cur_start - start) / sectorsize, + bitmap_ret); + + found = 1; +next: + ret = btrfs_next_item(csum_root, &path); + if (ret) + break; + } +out: + if (ret >= 0) + ret = !found; + btrfs_release_path(&path); + return ret; +} diff --git a/ctree.h b/ctree.h index 13cf3b00..9c999b1f 100644 --- a/ctree.h +++ b/ctree.h @@ -2779,4 +2779,8 @@ int btrfs_punch_hole(struct btrfs_trans_handle *trans, int btrfs_read_file(struct btrfs_root *root, u64 ino, u64 start, int len, char *dest); +/* csum.c */ +int btrfs_read_data_csums(struct btrfs_fs_info *fs_info, u64 start, u64 len, + void *csum_ret, unsigned long *bitmap_ret); + #endif