Received: from list by lists.gnu.org with archive (Exim 4.71) id 1fGVr9-0003FC-NY for mharc-grub-devel@gnu.org; Wed, 09 May 2018 16:38:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53319) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fGUxO-0001tu-MC for grub-devel@gnu.org; Wed, 09 May 2018 15:41:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fGUxL-0003Rf-G9 for grub-devel@gnu.org; Wed, 09 May 2018 15:40:58 -0400 Received: from smtp-36-i6.italiaonline.it ([213.209.14.36]:43182 helo=libero.it) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fGUxL-0003RR-4B for grub-devel@gnu.org; Wed, 09 May 2018 15:40:55 -0400 Received: from venice.bhome ([84.223.216.156]) by smtp-36.iol.local with SMTP id GUxJfOHAUxK79GUxJf6W7n; Wed, 09 May 2018 21:40:53 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inwind.it; s=s2014; t=1525894853; bh=0WrajTGyZwmsvI8ojJhrl6v+V0yaVxcRlKSubqAJInY=; h=Reply-To:Subject:To:References:From:Date:In-Reply-To; b=fRdT2dq6r/eLxsz9tjO+MhmLhga2NdOf8rqkMjpKA+z3FBITBamVBr6AOIjRevRGe yYmYEigfxVzhIw/6MgMAa6peBGThnF0aS52P5UNqivvJDgAK7QL8C+414A+ePnWqkC XWzuvvV1ziPf+EIUXFZVuDTCM/9SqZabGKZW3pY/qgj5o/M6vW2MBmd1x4/zKNr9+p XgJN4XB7jJqU52YMCz2WnY6FkuxWD4R4vwsgNOnmYpOm910Dojrvudmq6kTx2ZVpDI nAKaLR/JDKgkn/r6ZRZHVBoz6B/NCAFRHfRT17q8VxHEWm5BciP5OX+eCahfKamGnj hCwV0Ih79AkBg== X-CNFS-Analysis: v=2.2 cv=WJIPZjkR c=1 sm=1 tr=0 a=aJCR3EiSKf4gI6xYJITVbg==:117 a=aJCR3EiSKf4gI6xYJITVbg==:17 a=IkcTkHD0fZMA:10 a=y4var56W6aEAlv5SNEEA:9 a=QEXdDO2ut3YA:10 Reply-To: kreijack@inwind.it Subject: Re: [PATCH 7/7] Add raid6 recovery. To: Daniel Kiper , The development of GNU GRUB References: <20180424191316.11031-1-kreijack@inwind.it> <20180424191316.11031-8-kreijack@inwind.it> <20180509165814.GH25320@router-fw-old.local.net-space.pl> From: Goffredo Baroncelli Message-ID: <7d7da3eb-ae48-280c-29a7-a221553d2dbb@inwind.it> Date: Wed, 9 May 2018 21:40:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180509165814.GH25320@router-fw-old.local.net-space.pl> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4wfOThsBiJukDyQVc+MwHvCotoFoToR75nFj+QnxBSFtX6pP+q82rOu75KOunPBSq4Q8p1bEh9g6zkLOOOHWy/eFK1zEEERN0jcOWOtHdMdGvftDXVj4+T 3dfLTUWeKdxCJOA+uZ5Tm1E5OSJ6myewzFkSxWQT0td0sF+kchpZrJUlYrRfVKX1m9w2Pmv4k8O1EW9BQLoDReHK4QZ2kTR3cSs= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 213.209.14.36 X-Mailman-Approved-At: Wed, 09 May 2018 16:38:30 -0400 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: Wed, 09 May 2018 19:41:00 -0000 On 05/09/2018 06:58 PM, Daniel Kiper wrote: > On Tue, Apr 24, 2018 at 09:13:16PM +0200, Goffredo Baroncelli wrote: >> 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; > > Could you define this as a constant? In the original code from raid6_recover.c is the same. I prefer to not diverge too much. > >> +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; >> + } >> +} > > Could not we do this as a static? It is initialized only once. Ditto > >> +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 > > Please drop this. See below > >> + >> +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_uint64_t addr __attribute__ ((unused))" in prototype definition? It is ugly ! :-) > >> + 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: > > One space before the label please? ok > >> + 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); > > grub_raid6_recover() should be called directly from right place. replacing raid_recover_read_raid56_buffer() with raid_recover_read_diskfilter_array(), could allow to use grub_raid6_recover() even in the grub-core/disk/raid6_recover.c. This is the reason of "double call". > > Daniel > -- gpg @keyserver.linux.it: Goffredo Baroncelli Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5