Received: from list by lists.gnu.org with archive (Exim 4.71) id 1fB3Nx-0002F0-3M for mharc-grub-devel@gnu.org; Tue, 24 Apr 2018 15:13:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54222) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fB3Nv-0002DS-8I for grub-devel@gnu.org; Tue, 24 Apr 2018 15:13:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fB3Nu-0005ym-05 for grub-devel@gnu.org; Tue, 24 Apr 2018 15:13:51 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:40157) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fB3Nt-0005yP-Ml for grub-devel@gnu.org; Tue, 24 Apr 2018 15:13:49 -0400 Received: by mail-wm0-x243.google.com with SMTP id j5so2655224wme.5 for ; Tue, 24 Apr 2018 12:13:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pO56+8xQcN3jLthCHT9flfInoXTghdz6BGp1smzgKP0=; b=a25XV2gddkK+c9wpG60zqvJ2KGQ1PttavZ5xwtXNKTua40zOd6o58GcgzNJknhb56C yQc6YbhonhshFR09wh3tPI+dJNdOODlw0hwU83xevoSkQvl/kSgZDptP7YuRneE1fE25 EjZoesYo1e/yxmsK/Rsc5dQDD/fzGd3vuxbFdwaI8RCZ1PWQK/6F4tx35EG3iqGx7eYX dlpireRKG2tDJJ0D2vtuSRBLDa8lUhWWZOkKfsUKOpI+sFULzV+kOGyYCDTUi9Zbkb0c P3gVA60xjzTQIWiVjG9CwAfRGQ8YD2mxfw5EqPO9b/jYeJpafiUN7nLOIyxjrTvBe+8q TasA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=pO56+8xQcN3jLthCHT9flfInoXTghdz6BGp1smzgKP0=; b=o6CGzlOlqaL/v5giM+5TcZqL1CeZKvaQsC8U7kT0m3jinQ/wsJbB44kmaJc+bk28JG N850lZhql/WYP0z7eh4xNrWu0P1HqM+SXooKDfhsbGLbBFAHo5dHWPd5n/WbyO0k6X93 c2f+HV21oQ/RUWGFRethOtvxD2XQZ8p4YZ/uGXXzTTxaFQIX2mjDcYW8VvqOaNM+ya4V dMWMFZkUI90Rx7mLTTMC/CPTGIYVY2II9oTadGsSjzJNqnbceGXc7i2uVOUg7sgh1ABn 1cU+oINQkpJurSiCOB0XYHCPUF2Z881BjlmL43KJ1PZTiUh+hD0QkVy4rIDDk5tYJKcr Qbdg== X-Gm-Message-State: ALQs6tA9qUTWm8gFDt+2DomAx4JrDRd6ZC69NGF7w+kFt7rsN5TaWQO8 3FP0Ps9pL6BKSP3ix0VSGydr0A== X-Google-Smtp-Source: AB8JxZqEsa2WH1Cf4fKga2XTT0LyFsyZnPWtpE7lybmC1y4+2B0OkuplI3avzsnGbxxYKCUcO1jrFg== X-Received: by 10.28.135.149 with SMTP id j143mr4052564wmd.149.1524597228305; Tue, 24 Apr 2018 12:13:48 -0700 (PDT) Received: from venice.bhome (94-38-18-113.adsl-ull.clienti.tiscali.it. [94.38.18.113]) by smtp.gmail.com with ESMTPSA id w186sm11049733wmw.27.2018.04.24.12.13.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 12:13:47 -0700 (PDT) From: Goffredo Baroncelli X-Google-Original-From: Goffredo Baroncelli To: grub-devel@gnu.org Cc: Goffredo Baroncelli Subject: [PATCH 7/7] Add raid6 recovery. Date: Tue, 24 Apr 2018 21:13:16 +0200 Message-Id: <20180424191316.11031-8-kreijack@inwind.it> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180424191316.11031-1-kreijack@inwind.it> References: <20180424191316.11031-1-kreijack@inwind.it> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 24 Apr 2018 19:13:52 -0000 The code origins from "raid6_recovery.c". The code was changed because the original one assumed that both the disk address and size are multiple of GRUB_DISK_SECTOR_SIZE. This is not true for grub btrfs driver. The code was made more generalized using a function pointer for reading the data from the memory or disk. Signed-off-by: Goffredo Baroncelli --- grub-core/fs/btrfs.c | 211 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 204 insertions(+), 7 deletions(-) diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c index 5c76a68f3..195313a31 100644 --- a/grub-core/fs/btrfs.c +++ b/grub-core/fs/btrfs.c @@ -30,6 +30,7 @@ #include #include #include +#include GRUB_MOD_LICENSE ("GPLv3+"); @@ -695,19 +696,215 @@ rebuild_raid5 (struct raid56_buffer *buffers, grub_uint64_t nstripes, csize); } + +/* copied from raid6_recover */ +/* x**y. */ +static grub_uint8_t powx[255 * 2]; +/* Such an s that x**s = y */ +static unsigned powx_inv[256]; +static const grub_uint8_t poly = 0x1d; + +static void +grub_raid_block_mulx (unsigned mul, char *buf, grub_size_t size) +{ + grub_size_t i; + grub_uint8_t *p; + + p = (grub_uint8_t *) buf; + for (i = 0; i < size; i++, p++) + if (*p) + *p = powx[mul + powx_inv[*p]]; +} + +static void +grub_raid6_init_table (void) +{ + unsigned i; + + grub_uint8_t cur = 1; + for (i = 0; i < 255; i++) + { + powx[i] = cur; + powx[i + 255] = cur; + powx_inv[cur] = i; + if (cur & 0x80) + cur = (cur << 1) ^ poly; + else + cur <<= 1; + } +} + +static unsigned +mod_255 (unsigned x) +{ + while (x > 0xff) + x = (x >> 8) + (x & 0xff); + if (x == 0xff) + return 0; + return x; +} + +typedef grub_err_t (* raid_recover_read_t)(void *data, int disk_nr, + grub_uint64_t addr, void *dest, + grub_size_t size); +#if 0 +/* + * code example to be used in raid6_recover. + */ +static grub_err_t +raid_recover_read_diskfilter_array (void *data, int disk_nr, + grub_uint64_t sector, + void *buf, grub_size_t size) +{ + struct grub_diskfilter_segment *array = data; + return grub_diskfilter_read_node (&array->nodes[disk_nr], + (grub_disk_addr_t)sector, + size >> GRUB_DISK_SECTOR_BITS, buf); +} +#endif + +static grub_err_t +raid_recover_read_raid56_buffer (void *data, int disk_nr, grub_uint64_t addr, + void *dest, grub_size_t size) +{ + struct raid56_buffer *buffers = data; + + (void)addr; + grub_memcpy(dest, buffers[disk_nr].buf, size); + + grub_errno = buffers[disk_nr].ok ? GRUB_ERR_NONE : GRUB_ERR_READ_ERROR; + return grub_errno; +} + +static grub_err_t +grub_raid6_recover (void *data, grub_uint64_t nstripes, int disknr, int p, + char *buf, grub_uint64_t sector, grub_size_t size, + raid_recover_read_t read_func, int layout) +{ + int i, q, pos; + int bad1 = -1, bad2 = -1; + char *pbuf = 0, *qbuf = 0; + static int table_initializated = 0; + + if (!table_initializated) + { + grub_raid6_init_table(); + table_initializated = 1; + } + + pbuf = grub_zalloc (size); + if (!pbuf) + goto quit; + + qbuf = grub_zalloc (size); + if (!qbuf) + goto quit; + + q = p + 1; + if (q == (int) nstripes) + q = 0; + + pos = q + 1; + if (pos == (int) nstripes) + pos = 0; + + for (i = 0; i < (int) nstripes - 2; i++) + { + int c; + if (layout & GRUB_RAID_LAYOUT_MUL_FROM_POS) + c = pos; + else + c = i; + if (pos == disknr) + bad1 = c; + else + { + if (!read_func(data, pos, sector, buf, size)) + { + grub_crypto_xor (pbuf, pbuf, buf, size); + grub_raid_block_mulx (c, buf, size); + grub_crypto_xor (qbuf, qbuf, buf, size); + } + else + { + /* Too many bad devices */ + if (bad2 >= 0) + goto quit; + + bad2 = c; + grub_errno = GRUB_ERR_NONE; + } + } + + pos++; + if (pos == (int) nstripes) + pos = 0; + } + + /* Invalid disknr or p */ + if (bad1 < 0) + goto quit; + + if (bad2 < 0) + { + /* One bad device */ + if (!read_func(data, p, sector, buf, size)) + { + grub_crypto_xor (buf, buf, pbuf, size); + goto quit; + } + + grub_errno = GRUB_ERR_NONE; + if (read_func(data, q, sector, buf, size)) + goto quit; + + grub_crypto_xor (buf, buf, qbuf, size); + grub_raid_block_mulx (255 - bad1, buf, + size); + } + else + { + /* Two bad devices */ + unsigned c; + + if (read_func(data, p, sector, buf, size)) + goto quit; + + grub_crypto_xor (pbuf, pbuf, buf, size); + + if (read_func(data, q, sector, buf, size)) + goto quit; + + grub_crypto_xor (qbuf, qbuf, buf, size); + + c = mod_255((255 ^ bad1) + + (255 ^ powx_inv[(powx[bad2 + (bad1 ^ 255)] ^ 1)])); + grub_raid_block_mulx (c, qbuf, size); + + c = mod_255((unsigned) bad2 + c); + grub_raid_block_mulx (c, pbuf, size); + + grub_crypto_xor (pbuf, pbuf, qbuf, size); + grub_memcpy (buf, pbuf, size); + } + +quit: + grub_free (pbuf); + grub_free (qbuf); + + return grub_errno; +} + +/* end copy */ static void rebuild_raid6 (struct raid56_buffer *buffers, grub_uint64_t nstripes, grub_uint64_t csize, grub_uint64_t parities_pos, void *dest, grub_uint64_t stripen) { - (void)buffers; - (void)nstripes; - (void)csize; - (void)parities_pos; - (void)dest; - (void)stripen; - grub_dprintf ("btrfs", "called rebuild_raid6(), NOT IMPLEMENTED\n"); + grub_raid6_recover (buffers, nstripes, stripen, parities_pos, + dest, 0, csize, + raid_recover_read_raid56_buffer, 0); } static grub_err_t -- 2.17.0