summaryrefslogtreecommitdiffstats
path: root/src/erasure-code/shec/ErasureCodeShec.cc
blob: 3634be2fb3b9d14a00ba5e222d3c1bf9d3699780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 FUJITSU LIMITED
 * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
 * Copyright (C) 2014 Red Hat <contact@redhat.com>
 *
 * Author: Takanori Nakao <nakao.takanori@jp.fujitsu.com>
 * Author: Takeshi Miyamae <miyamae.takeshi@jp.fujitsu.com>
 * Author: Loic Dachary <loic@dachary.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <algorithm>
#include "common/debug.h"
#include "ErasureCodeShec.h"
extern "C" {
#include "jerasure/include/jerasure.h"
#include "jerasure/include/galois.h"

extern int calc_determinant(int *matrix, int dim);
extern int* reed_sol_vandermonde_coding_matrix(int k, int m, int w);
}

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_osd
#undef dout_prefix
#define dout_prefix _prefix(_dout)

using namespace std;
using namespace ceph;


static ostream& _prefix(std::ostream* _dout)
{
  return *_dout << "ErasureCodeShec: ";
}

int ErasureCodeShec::init(ErasureCodeProfile &profile,
			  ostream *ss)
{
  int err = 0;
  err |= parse(profile);
  if (err)
    return err;
  prepare();
  return ErasureCode::init(profile, ss);
}

unsigned int ErasureCodeShec::get_chunk_size(unsigned int object_size) const
{
  unsigned alignment = get_alignment();
  unsigned tail = object_size % alignment;
  unsigned padded_length = object_size + ( tail ?  ( alignment - tail ) : 0 );

  ceph_assert(padded_length % k == 0);
  return padded_length / k;
}

int ErasureCodeShec::_minimum_to_decode(const set<int> &want_to_read,
				       const set<int> &available_chunks,
				       set<int> *minimum_chunks)
{
  if (!minimum_chunks) return -EINVAL;

  for (set<int>::iterator it = available_chunks.begin(); it != available_chunks.end(); ++it){
    if (*it < 0 || k+m <= *it) return -EINVAL;
  }

  for (set<int>::iterator it = want_to_read.begin(); it != want_to_read.end(); ++it){
    if (*it < 0 || k+m <= *it) return -EINVAL;
  }

  int want[k + m];
  int avails[k + m];
  int minimum[k + m];

  memset(want, 0, sizeof(want));
  memset(avails, 0, sizeof(avails));
  memset(minimum, 0, sizeof(minimum));
  (*minimum_chunks).clear();

  for (set<int>::const_iterator i = want_to_read.begin();
       i != want_to_read.end();
       ++i) {
    want[*i] = 1;
  }

  for (set<int>::const_iterator i = available_chunks.begin();
       i != available_chunks.end();
       ++i) {
    avails[*i] = 1;
  }

  {
    int decoding_matrix[k*k];
    int dm_row[k];
    int dm_column[k];
    memset(decoding_matrix, 0, sizeof(decoding_matrix));
    memset(dm_row, 0, sizeof(dm_row));
    memset(dm_column, 0, sizeof(dm_column));
    if (shec_make_decoding_matrix(true, want, avails, decoding_matrix, dm_row, dm_column, minimum) < 0) {
      return -EIO;
    }
  }

  for (int i = 0; i < k + m; i++) {
    if (minimum[i] == 1) minimum_chunks->insert(i);
  }

  return 0;
}

int ErasureCodeShec::minimum_to_decode_with_cost(const set<int> &want_to_read,
						 const map<int, int> &available,
						 set<int> *minimum_chunks)
{
  set <int> available_chunks;

  for (map<int, int>::const_iterator i = available.begin();
       i != available.end();
       ++i)
    available_chunks.insert(i->first);

  return _minimum_to_decode(want_to_read, available_chunks, minimum_chunks);
}

int ErasureCodeShec::encode(const set<int> &want_to_encode,
			    const bufferlist &in,
			    map<int, bufferlist> *encoded)
{
  unsigned int k = get_data_chunk_count();
  unsigned int m = get_chunk_count() - k;
  bufferlist out;

  if (!encoded || !encoded->empty()){
    return -EINVAL;
  }

  int err = encode_prepare(in, *encoded);
  if (err)
    return err;
  encode_chunks(want_to_encode, encoded);
  for (unsigned int i = 0; i < k + m; i++) {
    if (want_to_encode.count(i) == 0)
      encoded->erase(i);
  }
  return 0;
}

int ErasureCodeShec::encode_chunks(const set<int> &want_to_encode,
				   map<int, bufferlist> *encoded)
{
  char *chunks[k + m];
  for (int i = 0; i < k + m; i++){
    chunks[i] = (*encoded)[i].c_str();
  }
  shec_encode(&chunks[0], &chunks[k], (*encoded)[0].length());
  return 0;
}

int ErasureCodeShec::_decode(const set<int> &want_to_read,
			    const map<int, bufferlist> &chunks,
			    map<int, bufferlist> *decoded)
{
  vector<int> have;

  if (!decoded || !decoded->empty()){
    return -EINVAL;
  }
  if (!want_to_read.empty() && chunks.empty()) {
    // i need to get the blocksize from the first element of chunks
    return -1;
  }

  have.reserve(chunks.size());
  for (map<int, bufferlist>::const_iterator i = chunks.begin();
       i != chunks.end();
       ++i) {
    have.push_back(i->first);
  }
  if (includes(
	have.begin(), have.end(), want_to_read.begin(), want_to_read.end())) {
    for (set<int>::iterator i = want_to_read.begin();
	 i != want_to_read.end();
	 ++i) {
      (*decoded)[*i] = chunks.find(*i)->second;
    }
    return 0;
  }
  unsigned int k = get_data_chunk_count();
  unsigned int m = get_chunk_count() - k;
  unsigned blocksize = (*chunks.begin()).second.length();
  for (unsigned int i =  0; i < k + m; i++) {
    if (chunks.find(i) == chunks.end()) {
      bufferlist tmp;
      bufferptr ptr(buffer::create_aligned(blocksize, SIMD_ALIGN));
      tmp.push_back(ptr);
      tmp.claim_append((*decoded)[i]);
      (*decoded)[i].swap(tmp);
    } else {
      (*decoded)[i] = chunks.find(i)->second;
      (*decoded)[i].rebuild_aligned(SIMD_ALIGN);
    }
  }
  return decode_chunks(want_to_read, chunks, decoded);
}

int ErasureCodeShec::decode_chunks(const set<int> &want_to_read,
				   const map<int, bufferlist> &chunks,
				   map<int, bufferlist> *decoded)
{
  unsigned blocksize = (*chunks.begin()).second.length();
  int erased[k + m];
  int erased_count = 0;
  int avails[k + m];
  char *data[k];
  char *coding[m];

  for (int i = 0; i < k + m; i++) {
    erased[i] = 0;
    if (chunks.find(i) == chunks.end()) {
      if (want_to_read.count(i) > 0) {
	erased[i] = 1;
	erased_count++;
      }
      avails[i] = 0;
    } else {
      avails[i] = 1;
    }
    if (i < k)
      data[i] = (*decoded)[i].c_str();
    else
      coding[i - k] = (*decoded)[i].c_str();
  }

  if (erased_count > 0) {
    return shec_decode(erased, avails, data, coding, blocksize);
  } else {
    return 0;
  }
}

//
// ErasureCodeShecReedSolomonVandermonde
//

void ErasureCodeShecReedSolomonVandermonde::shec_encode(char **data,
					     char **coding,
					     int blocksize)
{
  jerasure_matrix_encode(k, m, w, matrix, data, coding, blocksize);
}

int ErasureCodeShecReedSolomonVandermonde::shec_decode(int *erased,
					    int *avails,
					    char **data,
					    char **coding,
					    int blocksize)
{
  return shec_matrix_decode(erased, avails, data, coding, blocksize);
}

unsigned ErasureCodeShecReedSolomonVandermonde::get_alignment() const
{
  return k*w*sizeof(int);
}

int ErasureCodeShecReedSolomonVandermonde::parse(const ErasureCodeProfile &profile)
{
  int err = 0;
  // k, m, c
  if (profile.find("k") == profile.end() &&
      profile.find("m") == profile.end() &&
      profile.find("c") == profile.end()){
    dout(10) << "(k, m, c) default to " << "(" << DEFAULT_K
	     << ", " << DEFAULT_M << ", " << DEFAULT_C << ")" << dendl;
    k = DEFAULT_K; m = DEFAULT_M; c = DEFAULT_C;
  } else if (profile.find("k") == profile.end() ||
	     profile.find("m") == profile.end() ||
	     profile.find("c") == profile.end()){
    dout(10) << "(k, m, c) must be chosen" << dendl;
    err = -EINVAL;
  } else {
    std::string err_k, err_m, err_c, value_k, value_m, value_c;
    value_k = profile.find("k")->second;
    value_m = profile.find("m")->second;
    value_c = profile.find("c")->second;
    k = strict_strtol(value_k.c_str(), 10, &err_k);
    m = strict_strtol(value_m.c_str(), 10, &err_m);
    c = strict_strtol(value_c.c_str(), 10, &err_c);

    if (!err_k.empty() || !err_m.empty() || !err_c.empty()){
      if (!err_k.empty()){
	derr << "could not convert k=" << value_k << "to int" << dendl;
      } else if (!err_m.empty()){
	derr << "could not convert m=" << value_m << "to int" << dendl;
      } else if (!err_c.empty()){
	derr << "could not convert c=" << value_c << "to int" << dendl;
      }
      err = -EINVAL;
    } else if (k <= 0){
      derr << "k=" << k
	   << " must be a positive number" << dendl;
      err = -EINVAL;
    } else if (m <= 0){
      derr << "m=" << m
	   << " must be a positive number" << dendl;
      err = -EINVAL;
    } else if (c <= 0){
      derr << "c=" << c
	   << " must be a positive number" << dendl;
      err = -EINVAL;
    } else if (m < c){
      derr << "c=" << c
	   << " must be less than or equal to m=" << m << dendl;
      err = -EINVAL;
    } else if (k > 12){
      derr << "k=" << k
	   << " must be less than or equal to 12" << dendl;
      err = -EINVAL;
    } else if (k+m > 20){
      derr << "k+m=" << k+m
	   << " must be less than or equal to 20" << dendl;
      err = -EINVAL;
    } else if (k<m){
      derr << "m=" << m
	   << " must be less than or equal to k=" << k << dendl;
      err = -EINVAL;
    }
  }

  if (err) {
    derr << "(k, m, c)=(" << k << ", " << m << ", " << c
	 << ") is not a valid parameter." << dendl;
    return err;
  }

  dout(10) << "(k, m, c) set to " << "(" << k << ", " << m << ", "
	   << c << ")"<< dendl;

  // w
  if (profile.find("w") == profile.end()){
    dout(10) << "w default to " << DEFAULT_W << dendl;
    w = DEFAULT_W;
  } else {
    std::string err_w, value_w;
    value_w = profile.find("w")->second;
    w = strict_strtol(value_w.c_str(), 10, &err_w);

    if (!err_w.empty()){
      derr << "could not convert w=" << value_w << "to int" << dendl;
      dout(10) << "w default to " << DEFAULT_W << dendl;
      w = DEFAULT_W;

    } else if (w != 8 && w != 16 && w != 32) {
      derr << "w=" << w
	   << " must be one of {8, 16, 32}" << dendl;
      dout(10) << "w default to " << DEFAULT_W << dendl;
      w = DEFAULT_W;

    } else {
      dout(10) << "w set to " << w << dendl;
    }
  }
  return 0;
}

void ErasureCodeShecReedSolomonVandermonde::prepare()
{
  // setup shared encoding table
  int** p_enc_table =
    tcache.getEncodingTable(technique, k, m, c, w);

  if (!*p_enc_table) {
    dout(10) << "[ cache tables ] creating coeff for k=" <<
      k << " m=" << m << " c=" << c << " w=" << w << dendl;

    matrix = shec_reedsolomon_coding_matrix(technique);

    // either our new created table is stored or if it has been
    // created in the meanwhile the locally allocated table will be
    // freed by setEncodingTable
    matrix = tcache.setEncodingTable(technique, k, m, c, w, matrix);

    dout(10) << "matrix = " << dendl;
    for (int i=0; i<m; i++) {
      char mat[k+1];
      for (int j=0; j<k; j++) {
        if (matrix[i*k+j] > 0) {
          mat[j] = '1';
        } else {
          mat[j] = '0';
        }
      }
      mat[k] = '\0';
      dout(10) << mat << dendl;
    }
  } else {
    matrix = *p_enc_table;
  }

  dout(10) << " [ technique ] = " <<
    ((technique == MULTIPLE) ? "multiple" : "single") << dendl;

  ceph_assert((technique == SINGLE) || (technique == MULTIPLE));

}

// ErasureCodeShec::
// Mearged from shec.cc.

double ErasureCodeShec::shec_calc_recovery_efficiency1(int k, int m1, int m2, int c1, int c2){
  int r_eff_k[k];
  double r_e1;
  int i, rr, cc, start, end;
  int first_flag;

  if (m1 < c1 || m2 < c2) return -1;
  if ((m1 == 0 && c1 != 0) || (m2 == 0 && c2 != 0)) return -1;

  for (i=0; i<k; i++) r_eff_k[i] = 100000000;
  r_e1 = 0;

  for (rr=0; rr<m1; rr++){
    start = ((rr*k)/m1) % k;
    end = (((rr+c1)*k)/m1) % k;
    for (cc=start, first_flag=1; first_flag || cc!=end; cc=(cc+1)%k){
      first_flag = 0;
      r_eff_k[cc] = std::min(r_eff_k[cc], ((rr+c1)*k)/m1 - (rr*k)/m1);
    }
    r_e1 += ((rr+c1)*k)/m1 - (rr*k)/m1;
  }

  for (rr=0; rr<m2; rr++){
    start = ((rr*k)/m2) % k;
    end = (((rr+c2)*k)/m2) % k;
    for (cc=start, first_flag=1; first_flag || cc!=end; cc=(cc+1)%k){
      first_flag = 0;
      r_eff_k[cc] = std::min(r_eff_k[cc], ((rr+c2)*k)/m2 - (rr*k)/m2);
    }
    r_e1 += ((rr+c2)*k)/m2 - (rr*k)/m2;
  }

  for (i=0; i<k; i++){
    r_e1 += r_eff_k[i];
  }

  r_e1 /= (k+m1+m2);

  return r_e1;
}

int* ErasureCodeShec::shec_reedsolomon_coding_matrix(int is_single)
{
  int *matrix;
  int rr, cc, start, end;
  int m1, m2, c1, c2;

  if (w != 8 && w != 16 && w != 32) return NULL;

  if (!is_single){
    int c1_best = -1, m1_best = -1;
    double min_r_e1 = 100.0;

    // create all multiple shec pattern and choose best.

    for (c1=0; c1 <= c/2; c1++){
      for (m1=0; m1 <= m; m1++){
        c2 = c-c1;
        m2 = m-m1;

        if (m1 < c1 || m2 < c2) continue;
        if ((m1 == 0 && c1 != 0) || (m2 == 0 && c2 != 0)) continue;
        if ((m1 != 0 && c1 == 0) || (m2 != 0 && c2 == 0)) continue;

        // minimize r_e1

        if (true) {
          double r_e1;
          r_e1 = shec_calc_recovery_efficiency1(k, m1, m2, c1, c2);
          if (min_r_e1 - r_e1 > std::numeric_limits<double>::epsilon() &&
	      r_e1 < min_r_e1) {
            min_r_e1 = r_e1;
            c1_best = c1;
            m1_best = m1;
          }
        }
      }
    }
    m1 = m1_best;
    c1 = c1_best;
    m2 = m - m1_best;
    c2 = c - c1_best;
  } else {
    m1 = 0;
    c1 = 0;
    m2 = m;
    c2 = c;
  }

  // create matrix
  matrix = reed_sol_vandermonde_coding_matrix(k, m, w);

  for (rr=0; rr<m1; rr++){
    end = ((rr*k)/m1) % k;
    start = (((rr+c1)*k)/m1) % k;
    for (cc=start; cc!=end; cc=(cc+1)%k){
      matrix[cc + rr*k] = 0;
    }
  }

  for (rr=0; rr<m2; rr++){
    end = ((rr*k)/m2) % k;
    start = (((rr+c2)*k)/m2) % k;
    for (cc=start; cc!=end; cc=(cc+1)%k){
      matrix[cc + (rr+m1)*k] = 0;
    }
  }

  return matrix;
}

int ErasureCodeShec::shec_make_decoding_matrix(bool prepare, int *want_, int *avails,
                                               int *decoding_matrix, int *dm_row, int *dm_column,
                                               int *minimum)
{
  int mindup = k+1, minp = k+1;
  int want[k + m];

  memset(want, 0, sizeof(want));

  for (int i = 0; i < k + m; ++i) {
    want[i] = want_[i];
  }

  for (int i = 0; i < m; ++i) {
    if (want[i + k] && !avails[i + k]) {
      for (int j=0; j < k; ++j) {
        if (matrix[i * k + j] > 0) {
          want[j] = 1;
        }
      }
    }
  }

  if (tcache.getDecodingTableFromCache(decoding_matrix,
                                       dm_row, dm_column, minimum,
                                       technique,
                                       k, m, c, w,
                                       want, avails)) {
    return 0;
  }

  for (unsigned long long pp = 0; pp < (1ull << m); ++pp) {

    // select parity chunks
    int ek = 0;
    int p[m];
    for (int i=0; i < m; ++i) {
      if (pp & (1ull << i)) {
        p[ek++] = i;
      }
    }
    if (ek > minp) {
      continue;
    }

    // Are selected parity chunks avail?
    bool ok = true;
    for (int i = 0; i < ek && ok; i++) {
      if (!avails[k+p[i]]) {
        ok = false;
        break;
      }
    }

    if (!ok) {
      continue;
    }

    int tmprow[k + m];
    int tmpcolumn[k];
    for (int i = 0; i < k + m; i++) {
      tmprow[i] = 0;
    }
    for (int i = 0; i < k; i++) {
      tmpcolumn[i] = 0;
    }

    for (int i=0; i < k; i++) {
      if (want[i] && !avails[i]) {
        tmpcolumn[i] = 1;
      }
    }

    // Parity chunks which are used to recovery erased data chunks, are added to tmprow.
    for (int i = 0; i < ek; i++) {
      tmprow[k + p[i]] = 1;
      for (int j = 0; j < k; j++) {
        int element = matrix[(p[i]) * k + j];
        if (element != 0) {
          tmpcolumn[j] = 1;
        }
        if (element != 0 && avails[j] == 1) {
          tmprow[j] = 1;
        }
      }
    }

    int dup_row = 0, dup_column = 0, dup = 0;
    for (int i = 0; i < k + m; i++) {
      if (tmprow[i]) {
        dup_row++;
      }
    }

    for (int i = 0; i < k; i++) {
      if (tmpcolumn[i]) {
        dup_column++;
      }
    }

    if (dup_row != dup_column) {
      continue;
    }
    dup = dup_row;
    if (dup == 0) {
      mindup = dup;
      for (int i = 0; i < k; i++) {
        dm_row[i] = -1;
      }
      for (int i = 0; i < k; i++) {
        dm_column[i] = -1;
      }
      break;
    }

    // minimum is updated.
    if (dup < mindup) {
      int tmpmat[dup * dup];
      {
        for (int i = 0, row = 0; i < k + m; i++) {
          if (tmprow[i]) {
            for (int j = 0, column = 0; j < k; j++) {
              if (tmpcolumn[j]) {
                if (i < k) {
                  tmpmat[row * dup + column] = (i == j ? 1 : 0);
                } else {
                  tmpmat[row * dup + column] = matrix[(i - k) * k + j];
                }
                column++;
              }
            }
            row++;
          }
        }
      }
      int det = calc_determinant(tmpmat, dup);

      if (det != 0) {
        int row_id = 0;
        int column_id = 0;
        for (int i = 0; i < k; i++) {
          dm_row[i] = -1;
        }
        for (int i = 0; i < k; i++) {
          dm_column[i] = -1;
        }

        mindup = dup;
        for (int i=0; i < k + m; i++) {
          if (tmprow[i]) {
            dm_row[row_id++] = i;
          }
        }
        for (int i=0; i < k; i++) {
          if (tmpcolumn[i]) {
            dm_column[column_id++] = i;
          }
        }
        minp = ek;
      }
    }
  }


  if (mindup == k+1) {
    dout(10) << __func__ << ": can't find recover matrix." << dendl;
    return -1;
  }

  for (int i = 0; i < k + m; i++) {
    minimum[i] = 0;
  }

  for (int i=0; i < k && dm_row[i] != -1; i++) {
    minimum[dm_row[i]] = 1;
  }

  for (int i = 0; i < k; ++i) {
    if (want[i] && avails[i]) {
      minimum[i] = 1;
    }
  }

  for (int i = 0; i < m; ++i) {
    if (want[k + i] && avails[k + i] && !minimum[k + i]) {
      for (int j = 0; j < k; ++j) {
        if (matrix[i * k + j] > 0 && !want[j]) {
          minimum[k + i] = 1;
          break;
        }
      }
    }
  }

  if (mindup == 0) {
    return 0;
  }

  int tmpmat[mindup * mindup];
  for (int i=0; i < mindup; i++) {
    for (int j=0; j < mindup; j++) {
      if (dm_row[i] < k) {
        tmpmat[i * mindup + j] = (dm_row[i] == dm_column[j] ? 1 : 0);
      } else {
        tmpmat[i * mindup + j] = matrix[(dm_row[i] - k) * k + dm_column[j]];
      }
    }
    if (dm_row[i] < k) {
      for (int j = 0; j < mindup; j++) {
        if (dm_row[i] == dm_column[j]) {
          dm_row[i] = j;
        }
      }
    } else {
      dm_row[i] -= (k - mindup);
    }
  }

  if (prepare) {
    return 0;
  }

  int ret = jerasure_invert_matrix(tmpmat, decoding_matrix, mindup, w);

  tcache.putDecodingTableToCache(decoding_matrix, dm_row, dm_column, minimum, technique,
                                 k, m, c, w, want, avails);

  return ret;
}

int ErasureCodeShec::shec_matrix_decode(int *want, int *avails, char **data_ptrs,
                                        char **coding_ptrs, int size)
{
  int decoding_matrix[k*k];
  int dm_row[k], dm_column[k];
  int minimum[k + m];

  memset(decoding_matrix, 0, sizeof(decoding_matrix));
  memset(dm_row, -1, sizeof(dm_row));
  memset(dm_column, -1, sizeof(dm_column));
  memset(minimum, -1, sizeof(minimum));

  if (w != 8 && w != 16 && w != 32) return -1;

  if (shec_make_decoding_matrix(false, want, avails, decoding_matrix,
                                dm_row, dm_column, minimum) < 0) {
    return -1;
  }

  // Get decoding matrix size
  int dm_size = 0;
  for (int i = 0; i < k; i++) {
    if (dm_row[i] == -1) {
      break;
    }
    dm_size++;
  }

  char *dm_data_ptrs[dm_size];
  for (int i = 0; i < dm_size; i++) {
    dm_data_ptrs[i] = data_ptrs[dm_column[i]];
  }

  // Decode the data drives
  for (int i = 0; i < dm_size; i++) {
    if (!avails[dm_column[i]]) {
      jerasure_matrix_dotprod(dm_size, w, decoding_matrix + (i * dm_size),
                              dm_row, i, dm_data_ptrs, coding_ptrs, size);
    }
  }

  // Re-encode any erased coding devices
  for (int i = 0; i < m; i++) {
    if (want[k+i] && !avails[k+i]) {
      jerasure_matrix_dotprod(k, w, matrix + (i * k), NULL, i+k,
                              data_ptrs, coding_ptrs, size);
    }
  }

  return 0;
}