1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * Added helpers for unaligned native int access
20 * 1-way unrolled portable integer math RAID-6 instruction set
22 * This file was postprocessed using unroll.pl and then ported to userspace
26 #include "kerncompat.h"
33 * This is the C data type to use
36 /* Change this from BITS_PER_LONG if there is something better... */
37 #if BITS_PER_LONG == 64
38 # define NBYTES(x) ((x) * 0x0101010101010101UL)
41 typedef uint64_t unative_t;
42 #define put_unaligned_native(val,p) put_unaligned_64((val),(p))
43 #define get_unaligned_native(p) get_unaligned_64((p))
45 # define NBYTES(x) ((x) * 0x01010101U)
48 typedef uint32_t unative_t;
49 #define put_unaligned_native(val,p) put_unaligned_32((val),(p))
50 #define get_unaligned_native(p) get_unaligned_32((p))
54 * These sub-operations are separate inlines since they can sometimes be
55 * specially optimized using architecture-specific hacks.
59 * The SHLBYTE() operation shifts each byte left by 1, *not*
60 * rolling over into the next byte
62 static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
66 vv = (v << 1) & NBYTES(0xfe);
71 * The MASK() operation returns 0xFF in any byte for which the high
72 * bit is 1, 0x00 for any byte for which the high bit is 0.
74 static inline __attribute_const__ unative_t MASK(unative_t v)
78 vv = v & NBYTES(0x80);
79 vv = (vv << 1) - (vv >> 7); /* Overflow on the top bit is OK */
84 void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs)
86 uint8_t **dptr = (uint8_t **)ptrs;
90 unative_t wd0, wq0, wp0, w10, w20;
92 z0 = disks - 3; /* Highest data disk */
93 p = dptr[z0+1]; /* XOR parity */
94 q = dptr[z0+2]; /* RS syndrome */
96 for ( d = 0 ; d < bytes ; d += NSIZE*1 ) {
97 wq0 = wp0 = get_unaligned_native(&dptr[z0][d+0*NSIZE]);
98 for ( z = z0-1 ; z >= 0 ; z-- ) {
99 wd0 = get_unaligned_native(&dptr[z][d+0*NSIZE]);
107 put_unaligned_native(wp0, &p[d+NSIZE*0]);
108 put_unaligned_native(wq0, &q[d+NSIZE*0]);
112 static void xor_range(char *dst, const char*src, size_t size)
114 /* Move to DWORD aligned */
115 while (size && ((unsigned long)dst & sizeof(unsigned long))) {
120 /* DWORD aligned part */
121 while (size >= sizeof(unsigned long)) {
122 *(unsigned long *)dst ^= *(unsigned long *)src;
123 src += sizeof(unsigned long);
124 dst += sizeof(unsigned long);
125 size -= sizeof(unsigned long);
135 * Generate desired data/parity stripe for RAID5
137 * @nr_devs: Total number of devices, including parity
138 * @stripe_len: Stripe length
139 * @data: Data, with special layout:
140 * data[0]: Data stripe 0
141 * data[nr_devs-2]: Last data stripe
142 * data[nr_devs-1]: RAID5 parity
143 * @dest: To generate which data. should follow above data layout
145 int raid5_gen_result(int nr_devs, size_t stripe_len, int dest, void **data)
148 char *buf = data[dest];
150 /* Validation check */
151 if (stripe_len <= 0 || stripe_len != BTRFS_STRIPE_LEN) {
152 error("invalid parameter for %s", __func__);
156 if (dest >= nr_devs || nr_devs < 2) {
157 error("invalid parameter for %s", __func__);
160 /* Shortcut for 2 devs RAID5, which is just RAID1 */
162 memcpy(data[dest], data[1 - dest], stripe_len);
165 memset(buf, 0, stripe_len);
166 for (i = 0; i < nr_devs; i++) {
169 xor_range(buf, data[i], stripe_len);