From patchwork Fri Sep 30 05:04:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9357653 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 14CFF600C8 for ; Fri, 30 Sep 2016 05:05:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0833C29DCF for ; Fri, 30 Sep 2016 05:05:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F0D6D29DD5; Fri, 30 Sep 2016 05:05:25 +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 D5AC229DD2 for ; Fri, 30 Sep 2016 05:05:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935621AbcI3FFV (ORCPT ); Fri, 30 Sep 2016 01:05:21 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:14911 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S932469AbcI3FFU (ORCPT ); Fri, 30 Sep 2016 01:05:20 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="875042" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 30 Sep 2016 13:05:11 +0800 Received: from adam-work.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 3BE7D41B4BC0; Fri, 30 Sep 2016 13:05:10 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz Subject: [PATCH v2 3/4] btrfs-progs: raid56: Introduce new function to calculate raid5 parity or data stripe Date: Fri, 30 Sep 2016 13:04:56 +0800 Message-Id: <20160930050457.22584-3-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.10.0 In-Reply-To: <20160930050457.22584-1-quwenruo@cn.fujitsu.com> References: <20160930050457.22584-1-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-yoursite-MailScanner-ID: 3BE7D41B4BC0.AC986 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 new function raid5_gen_result() to calculate parity or data stripe. Signed-off-by: Qu Wenruo --- v2: Patch split Change parameter type from void* to char* for xor_range() Change parameter order for xxor_range() Handle unaligned memory address for xor_range() --- disk-io.h | 1 + raid56.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/disk-io.h b/disk-io.h index 440baa9..9fc7e92 100644 --- a/disk-io.h +++ b/disk-io.h @@ -192,5 +192,6 @@ int write_and_map_eb(struct btrfs_trans_handle *trans, struct btrfs_root *root, /* raid56.c */ void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs); +int raid5_gen_result(int nr_devs, size_t stripe_len, int dest, void **data); #endif diff --git a/raid56.c b/raid56.c index 833df5f..8c79c45 100644 --- a/raid56.c +++ b/raid56.c @@ -26,6 +26,8 @@ #include "kerncompat.h" #include "ctree.h" #include "disk-io.h" +#include "volumes.h" +#include "utils.h" /* * This is the C data type to use @@ -107,3 +109,64 @@ void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs) } } +static void xor_range(char *dst, const char*src, size_t size) +{ + /* Move to DWORD aligned */ + while (size && ((unsigned long)dst & sizeof(unsigned long))) { + *dst++ ^= *src++; + size--; + } + + /* DWORD aligned part */ + while (size >= sizeof(unsigned long)) { + *(unsigned long *)dst ^= *(unsigned long *)src; + src += sizeof(unsigned long); + dst += sizeof(unsigned long); + size -= sizeof(unsigned long); + } + /* Remaining */ + while (size) { + *dst++ ^= *src++; + size--; + } +} + +/* + * Generate desired data/parity stripe for RAID5 + * + * @nr_devs: Total number of devices, including parity + * @stripe_len: Stripe length + * @data: Data, with special layout: + * data[0]: Data stripe 0 + * data[nr_devs-2]: Last data stripe + * data[nr_devs-1]: RAID5 parity + * @dest: To generate which data. should follow above data layout + */ +int raid5_gen_result(int nr_devs, size_t stripe_len, int dest, void **data) +{ + int i; + char *buf = data[dest]; + + /* Validation check */ + if (stripe_len <= 0 || stripe_len != BTRFS_STRIPE_LEN) { + error("invalid parameter for %s", __func__); + return -EINVAL; + } + + if (dest >= nr_devs || nr_devs < 2) { + error("invalid parameter for %s", __func__); + return -EINVAL; + } + /* Shortcut for 2 devs RAID5, which is just RAID1 */ + if (nr_devs == 2) { + memcpy(data[dest], data[1 - dest], stripe_len); + return 0; + } + memset(buf, 0, stripe_len); + for (i = 0; i < nr_devs; i++) { + if (i == dest) + continue; + xor_range(buf, data[i], stripe_len); + } + return 0; +}