btrfs-progs: readme: fix link to issue tracker on github.
[btrfs-progs-unstable/devel.git] / raid56.c
blob8c79c4561c3d3be9f9e85103536795fa4563fbdf
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
18 * raid6int1.c
20 * 1-way unrolled portable integer math RAID-6 instruction set
22 * This file was postprocessed using unroll.pl and then ported to userspace
24 #include <stdint.h>
25 #include <unistd.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "volumes.h"
30 #include "utils.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)
39 # define NSIZE 8
40 # define NSHIFT 3
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))
44 #else
45 # define NBYTES(x) ((x) * 0x01010101U)
46 # define NSIZE 4
47 # define NSHIFT 2
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))
51 #endif
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)
64 unative_t vv;
66 vv = (v << 1) & NBYTES(0xfe);
67 return vv;
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)
76 unative_t vv;
78 vv = v & NBYTES(0x80);
79 vv = (vv << 1) - (vv >> 7); /* Overflow on the top bit is OK */
80 return vv;
84 void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs)
86 uint8_t **dptr = (uint8_t **)ptrs;
87 uint8_t *p, *q;
88 int d, z, z0;
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]);
100 wp0 ^= wd0;
101 w20 = MASK(wq0);
102 w10 = SHLBYTE(wq0);
103 w20 &= NBYTES(0x1d);
104 w10 ^= w20;
105 wq0 = w10 ^ wd0;
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))) {
116 *dst++ ^= *src++;
117 size--;
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);
127 /* Remaining */
128 while (size) {
129 *dst++ ^= *src++;
130 size--;
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)
147 int i;
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__);
153 return -EINVAL;
156 if (dest >= nr_devs || nr_devs < 2) {
157 error("invalid parameter for %s", __func__);
158 return -EINVAL;
160 /* Shortcut for 2 devs RAID5, which is just RAID1 */
161 if (nr_devs == 2) {
162 memcpy(data[dest], data[1 - dest], stripe_len);
163 return 0;
165 memset(buf, 0, stripe_len);
166 for (i = 0; i < nr_devs; i++) {
167 if (i == dest)
168 continue;
169 xor_range(buf, data[i], stripe_len);
171 return 0;