summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/ec/src/ec-combine.c
blob: c5af2ab5e39f6cc4c06c01738e29b3710b5c9060 (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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
/*
  Copyright (c) 2012-2014 DataLab, s.l. <http://www.datalab.es>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <fnmatch.h>

#include "libxlator.h"
#include <glusterfs/byte-order.h>

#include "ec-types.h"
#include "ec-helpers.h"
#include "ec-common.h"
#include "ec-combine.h"
#include "ec-messages.h"
#include <glusterfs/quota-common-utils.h>

#define EC_QUOTA_PREFIX "trusted.glusterfs.quota."

#define EC_MISSING_DATA ((data_t *)1ULL)

struct _ec_dict_info;
typedef struct _ec_dict_info ec_dict_info_t;

struct _ec_dict_combine;
typedef struct _ec_dict_combine ec_dict_combine_t;

struct _ec_dict_info {
    dict_t *dict;
    int32_t count;
};

struct _ec_dict_combine {
    ec_cbk_data_t *cbk;
    int32_t which;
};

int32_t
ec_combine_write(ec_fop_data_t *fop, ec_cbk_data_t *dst, ec_cbk_data_t *src)
{
    int valid = 0;

    if (!fop || !dst || !src)
        return 0;

    switch (fop->id) {
        case GF_FOP_REMOVEXATTR:
        case GF_FOP_FREMOVEXATTR:
        case GF_FOP_SETXATTR:
        case GF_FOP_FSETXATTR:
            return 1;

        case GF_FOP_SYMLINK:
        case GF_FOP_LINK:
        case GF_FOP_CREATE:
        case GF_FOP_MKNOD:
        case GF_FOP_MKDIR:
            valid = 3;
            break;
        case GF_FOP_UNLINK:
        case GF_FOP_RMDIR:
        case GF_FOP_SETATTR:
        case GF_FOP_FSETATTR:
        case GF_FOP_TRUNCATE:
        case GF_FOP_FTRUNCATE:
        case GF_FOP_WRITE:
        case GF_FOP_FALLOCATE:
        case GF_FOP_DISCARD:
        case GF_FOP_ZEROFILL:
            valid = 2;
            break;
        case GF_FOP_RENAME:
            valid = 5;
            break;
        default:
            gf_msg_callingfn(fop->xl->name, GF_LOG_WARNING, EINVAL,
                             EC_MSG_INVALID_FOP, "Invalid fop %d", fop->id);
            return 0;
            break;
    }

    if (!ec_iatt_combine(fop, dst->iatt, src->iatt, valid)) {
        gf_msg(fop->xl->name, GF_LOG_NOTICE, 0, EC_MSG_IATT_MISMATCH,
               "Mismatching iatt in "
               "answers of '%s'",
               gf_fop_list[fop->id]);
        return 0;
    }
    return 1;
}

void
ec_iatt_time_merge(int64_t *dst_sec, uint32_t *dst_nsec, int64_t src_sec,
                   uint32_t src_nsec)
{
    if ((*dst_sec < src_sec) ||
        ((*dst_sec == src_sec) && (*dst_nsec < src_nsec))) {
        *dst_sec = src_sec;
        *dst_nsec = src_nsec;
    }
}

static gf_boolean_t
ec_iatt_is_trusted(ec_fop_data_t *fop, struct iatt *iatt)
{
    uint64_t ino;
    int32_t i;

    /* Only the top level fop will have fop->locks filled. */
    while (fop->parent != NULL) {
        fop = fop->parent;
    }

    /* Lookups are special requests always done without locks taken but they
     * require to be able to identify differences between bricks. Special
     * handling of these differences is already done in lookup specific code
     * so we shouldn't ignore any difference here and consider all iatt
     * structures as trusted. */
    if (fop->id == GF_FOP_LOOKUP) {
        return _gf_true;
    }

    /* Check if the iatt references an inode locked by the current fop */
    for (i = 0; i < fop->lock_count; i++) {
        ino = gfid_to_ino(fop->locks[i].lock->loc.inode->gfid);
        if (iatt->ia_ino == ino) {
            return _gf_true;
        }
    }

    return _gf_false;
}

int32_t
ec_iatt_combine(ec_fop_data_t *fop, struct iatt *dst, struct iatt *src,
                int32_t count)
{
    int32_t i;
    gf_boolean_t failed = _gf_false;

    for (i = 0; i < count; i++) {
        /* Check for basic fields. These fields must be equal always, even if
         * the inode is not locked because in these cases the parent inode
         * will be locked and differences in these fields require changes in
         * the parent directory. */
        if ((dst[i].ia_ino != src[i].ia_ino) ||
            (((dst[i].ia_type == IA_IFBLK) || (dst[i].ia_type == IA_IFCHR)) &&
             (dst[i].ia_rdev != src[i].ia_rdev)) ||
            (gf_uuid_compare(dst[i].ia_gfid, src[i].ia_gfid) != 0)) {
            failed = _gf_true;
        }
        /* Check for not so stable fields. These fields can change if the
         * inode is not locked. */
        if (!failed && ((dst[i].ia_uid != src[i].ia_uid) ||
                        (dst[i].ia_gid != src[i].ia_gid) ||
                        (st_mode_from_ia(dst[i].ia_prot, dst[i].ia_type) !=
                         st_mode_from_ia(src[i].ia_prot, src[i].ia_type)))) {
            if (ec_iatt_is_trusted(fop, dst)) {
                /* If the iatt contains information from an inode that is
                 * locked, these differences are real problems, so we need to
                 * report them. Otherwise we ignore them and don't care which
                 * data is returned. */
                failed = _gf_true;
            } else {
                gf_msg_debug(fop->xl->name, 0,
                             "Ignoring iatt differences because inode is not "
                             "locked");
            }
        }
        if (failed) {
            gf_msg(fop->xl->name, GF_LOG_WARNING, 0, EC_MSG_IATT_COMBINE_FAIL,
                   "Failed to combine iatt (inode: %" PRIu64 "-%" PRIu64
                   ", "
                   "links: %u-%u, uid: %u-%u, gid: %u-%u, "
                   "rdev: %" PRIu64 "-%" PRIu64 ", size: %" PRIu64 "-%" PRIu64
                   ", "
                   "mode: %o-%o)",
                   dst[i].ia_ino, src[i].ia_ino, dst[i].ia_nlink,
                   src[i].ia_nlink, dst[i].ia_uid, src[i].ia_uid, dst[i].ia_gid,
                   src[i].ia_gid, dst[i].ia_rdev, src[i].ia_rdev,
                   dst[i].ia_size, src[i].ia_size,
                   st_mode_from_ia(dst[i].ia_prot, dst[i].ia_type),
                   st_mode_from_ia(src[i].ia_prot, dst[i].ia_type));

            return 0;
        }
    }

    while (count-- > 0) {
        dst[count].ia_blocks += src[count].ia_blocks;
        if (dst[count].ia_blksize < src[count].ia_blksize) {
            dst[count].ia_blksize = src[count].ia_blksize;
        }

        ec_iatt_time_merge(&dst[count].ia_atime, &dst[count].ia_atime_nsec,
                           src[count].ia_atime, src[count].ia_atime_nsec);
        ec_iatt_time_merge(&dst[count].ia_mtime, &dst[count].ia_mtime_nsec,
                           src[count].ia_mtime, src[count].ia_mtime_nsec);
        ec_iatt_time_merge(&dst[count].ia_ctime, &dst[count].ia_ctime_nsec,
                           src[count].ia_ctime, src[count].ia_ctime_nsec);
    }

    return 1;
}

void
ec_iatt_rebuild(ec_t *ec, struct iatt *iatt, int32_t count, int32_t answers)
{
    uint64_t blocks;

    while (count-- > 0) {
        blocks = iatt[count].ia_blocks * ec->fragments + answers - 1;
        blocks /= answers;
        iatt[count].ia_blocks = blocks;
    }
}

gf_boolean_t
ec_xattr_match(dict_t *dict, char *key, data_t *value, void *arg)
{
    if ((fnmatch(GF_XATTR_STIME_PATTERN, key, 0) == 0) ||
        (strcmp(key, GET_LINK_COUNT) == 0) ||
        (strcmp(key, GLUSTERFS_INODELK_COUNT) == 0) ||
        (strcmp(key, GLUSTERFS_ENTRYLK_COUNT) == 0) ||
        (strcmp(key, GLUSTERFS_OPEN_FD_COUNT) == 0)) {
        return _gf_false;
    }

    return _gf_true;
}

gf_boolean_t
ec_value_ignore(char *key)
{
    if ((strcmp(key, GF_CONTENT_KEY) == 0) ||
        (strcmp(key, GF_XATTR_PATHINFO_KEY) == 0) ||
        (strcmp(key, GF_XATTR_USER_PATHINFO_KEY) == 0) ||
        (strcmp(key, GF_XATTR_LOCKINFO_KEY) == 0) ||
        (strcmp(key, GLUSTERFS_OPEN_FD_COUNT) == 0) ||
        (strcmp(key, GLUSTERFS_INODELK_COUNT) == 0) ||
        (strcmp(key, GLUSTERFS_ENTRYLK_COUNT) == 0) ||
        (strncmp(key, GF_XATTR_CLRLK_CMD, SLEN(GF_XATTR_CLRLK_CMD)) == 0) ||
        (strcmp(key, DHT_IATT_IN_XDATA_KEY) == 0) ||
        (strncmp(key, EC_QUOTA_PREFIX, SLEN(EC_QUOTA_PREFIX)) == 0) ||
        (fnmatch(MARKER_XATTR_PREFIX ".*." XTIME, key, 0) == 0) ||
        (fnmatch(GF_XATTR_MARKER_KEY ".*", key, 0) == 0) ||
        (XATTR_IS_NODE_UUID(key))) {
        return _gf_true;
    }

    return _gf_false;
}

int32_t
ec_dict_compare(dict_t *dict1, dict_t *dict2)
{
    if (are_dicts_equal(dict1, dict2, ec_xattr_match, ec_value_ignore))
        return 1;
    return 0;
}

static uint32_t
ec_dict_list(data_t **list, ec_cbk_data_t *cbk, int32_t which, char *key,
             gf_boolean_t global)
{
    ec_t *ec = cbk->fop->xl->private;
    ec_cbk_data_t *ans = NULL;
    dict_t *dict = NULL;
    data_t *data;
    uint32_t count;
    int32_t i;

    for (i = 0; i < ec->nodes; i++) {
        /* We initialize the list with EC_MISSING_DATA if we are
         * returning a global list or the current subvolume belongs
         * to the group of the accepted answer. Note that if some
         * subvolume is known to be down before issuing the request,
         * we won't have any answer from it, so we set here the
         * appropriate default value. */
        if (global || ((cbk->mask & (1ULL << i)) != 0)) {
            list[i] = EC_MISSING_DATA;
        } else {
            list[i] = NULL;
        }
    }

    count = 0;
    list_for_each_entry(ans, &cbk->fop->answer_list, answer_list)
    {
        if (global || ((cbk->mask & ans->mask) != 0)) {
            dict = (which == EC_COMBINE_XDATA) ? ans->xdata : ans->dict;
            data = dict_get(dict, key);
            if (data != NULL) {
                list[ans->idx] = data;
                count++;
            }
        }
    }

    return count;
}

int32_t
ec_concat_prepare(xlator_t *xl, char **str, char **sep, char **post,
                  const char *fmt, va_list args)
{
    char *tmp;
    int32_t len;

    len = gf_vasprintf(str, fmt, args);
    if (len < 0) {
        return -ENOMEM;
    }

    tmp = strchr(*str, '{');
    if (tmp == NULL) {
        goto out;
    }
    *tmp++ = 0;
    *sep = tmp;
    tmp = strchr(tmp, '}');
    if (tmp == NULL) {
        goto out;
    }
    *tmp++ = 0;
    *post = tmp;

    return 0;

out:
    gf_msg(xl->name, GF_LOG_ERROR, EINVAL, EC_MSG_INVALID_FORMAT,
           "Invalid concat format");

    GF_FREE(*str);

    return -EINVAL;
}

static int32_t
ec_dict_data_concat(const char *fmt, ec_cbk_data_t *cbk, int32_t which,
                    char *key, char *new_key, const char *def,
                    gf_boolean_t global, ...)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    char *str = NULL, *pre = NULL, *sep, *post;
    dict_t *dict;
    va_list args;
    int32_t i, num, len, deflen, prelen, postlen, seplen, tmp;
    int32_t err;

    ec_dict_list(data, cbk, which, key, global);

    va_start(args, global);
    err = ec_concat_prepare(cbk->fop->xl, &pre, &sep, &post, fmt, args);
    va_end(args);

    if (err != 0) {
        return err;
    }

    prelen = strlen(pre);
    seplen = strlen(sep);
    postlen = strlen(post);

    deflen = 0;
    if (def != NULL) {
        deflen = strlen(def);
    }

    len = prelen + postlen + 1;
    num = -1;
    for (i = 0; i < ec->nodes; i++) {
        if (data[i] == NULL) {
            continue;
        }
        if (data[i] == EC_MISSING_DATA) {
            if (def == NULL) {
                continue;
            }
            len += deflen;
        } else {
            len += data[i]->len - 1;
        }
        if (num >= 0) {
            len += seplen;
        }
        num++;
    }

    err = -ENOMEM;

    str = GF_MALLOC(len, gf_common_mt_char);
    if (str == NULL) {
        goto out;
    }

    memcpy(str, pre, prelen);
    len = prelen;
    for (i = 0; i < ec->nodes; i++) {
        if (data[i] == NULL) {
            continue;
        }
        if (data[i] == EC_MISSING_DATA) {
            if (deflen == 0) {
                continue;
            }
            tmp = deflen;
            memcpy(str + len, def, tmp);
        } else {
            tmp = data[i]->len - 1;
            memcpy(str + len, data[i]->data, tmp);
        }
        len += tmp;
        if (i < num) {
            memcpy(str + len, sep, seplen);
            len += seplen;
        }
    }
    memcpy(str + len, post, postlen + 1);

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    if (new_key) {
        key = new_key;
    }
    err = dict_set_dynstr(dict, key, str);
    if (err != 0) {
        goto out;
    }

    str = NULL;

out:
    GF_FREE(str);
    GF_FREE(pre);

    return err;
}

int32_t
ec_dict_data_merge(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    dict_t *dict, *lockinfo, *tmp = NULL;
    char *ptr = NULL;
    int32_t i, len;
    int32_t err;

    ec_dict_list(data, cbk, which, key, _gf_false);

    lockinfo = dict_new();
    if (lockinfo == NULL) {
        return -ENOMEM;
    }

    for (i = 0; i < ec->nodes; i++) {
        if ((data[i] == NULL) || (data[i] == EC_MISSING_DATA)) {
            continue;
        }

        tmp = dict_new();
        if (tmp == NULL) {
            err = -ENOMEM;

            goto out;
        }
        err = dict_unserialize(data[i]->data, data[i]->len, &tmp);
        if (err != 0) {
            goto out;
        }
        if (dict_copy(tmp, lockinfo) == NULL) {
            err = -ENOMEM;

            goto out;
        }

        dict_unref(tmp);
    }

    tmp = NULL;

    len = dict_serialized_length(lockinfo);
    if (len < 0) {
        err = len;

        goto out;
    }
    ptr = GF_MALLOC(len, gf_common_mt_char);
    if (ptr == NULL) {
        err = -ENOMEM;

        goto out;
    }
    err = dict_serialize(lockinfo, ptr);
    if (err != 0) {
        goto out;
    }
    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    err = dict_set_dynptr(dict, key, ptr, len);
    if (err != 0) {
        goto out;
    }

    ptr = NULL;

out:
    GF_FREE(ptr);
    dict_unref(lockinfo);
    if (tmp != NULL) {
        dict_unref(tmp);
    }

    return err;
}

int32_t
ec_dict_data_uuid(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_cbk_data_t *ans, *min;
    dict_t *src, *dst;
    data_t *data;

    min = cbk;
    for (ans = cbk->next; ans != NULL; ans = ans->next) {
        if (ans->idx < min->idx) {
            min = ans;
        }
    }

    if (min != cbk) {
        src = (which == EC_COMBINE_XDATA) ? min->xdata : min->dict;
        dst = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;

        data = dict_get(src, key);
        if (data == NULL) {
            return -ENOENT;
        }
        if (dict_set(dst, key, data) != 0) {
            return -ENOMEM;
        }
    }

    return 0;
}

int32_t
ec_dict_data_iatt(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    dict_t *dict;
    struct iatt *stbuf, *tmp;
    int32_t i, ret;

    ec_dict_list(data, cbk, which, key, _gf_false);

    stbuf = NULL;
    for (i = 0; i < ec->nodes; i++) {
        if ((data[i] == NULL) || (data[i] == EC_MISSING_DATA)) {
            continue;
        }
        tmp = data_to_iatt(data[i], key);
        if (tmp == NULL) {
            ret = -EINVAL;
            goto out;
        }
        if (stbuf == NULL) {
            stbuf = GF_MALLOC(sizeof(struct iatt), gf_common_mt_char);
            if (stbuf == NULL) {
                ret = -ENOMEM;
                goto out;
            }
            *stbuf = *tmp;
        } else {
            if (!ec_iatt_combine(cbk->fop, stbuf, tmp, 1)) {
                ret = -EINVAL;
                goto out;
            }
        }
    }

    if ((stbuf != NULL) && (stbuf->ia_type == IA_IFREG)) {
        ec_iatt_rebuild(ec, stbuf, 1, cbk->count);
        /* TODO: not sure if an iatt could come in xdata from a fop that takes
         *       no locks. */
        if (!ec_get_inode_size(cbk->fop, cbk->fop->locks[0].lock->loc.inode,
                               &stbuf->ia_size)) {
            ret = -EINVAL;
            goto out;
        }
    }

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    ret = dict_set_iatt(dict, key, stbuf, false);
    if (ret >= 0) {
        stbuf = NULL;
    }

out:
    GF_FREE(stbuf);

    return ret;
}

int32_t
ec_dict_data_max32(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    dict_t *dict;
    int32_t i;
    uint32_t max, tmp;

    ec_dict_list(data, cbk, which, key, _gf_false);

    max = 0;
    for (i = 0; i < ec->nodes; i++) {
        if ((data[i] == NULL) || (data[i] == EC_MISSING_DATA)) {
            continue;
        }

        tmp = data_to_uint32(data[i]);
        if (max < tmp) {
            max = tmp;
        }
    }

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    return dict_set_uint32(dict, key, max);
}

int32_t
ec_dict_data_max64(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    dict_t *dict;
    int32_t i;
    uint64_t max, tmp;

    ec_dict_list(data, cbk, which, key, _gf_false);

    max = 0;
    for (i = 0; i < ec->nodes; i++) {
        if ((data[i] == NULL) || (data[i] == EC_MISSING_DATA)) {
            continue;
        }

        tmp = data_to_uint64(data[i]);
        if (max < tmp) {
            max = tmp;
        }
    }

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    return dict_set_uint64(dict, key, max);
}

int32_t
ec_dict_data_quota(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    dict_t *dict = NULL;
    int32_t i = 0;
    quota_meta_t size = {
        0,
    };
    quota_meta_t max_size = {
        0,
    };

    if (ec_dict_list(data, cbk, which, key, _gf_false) == 0) {
        return 0;
    }

    /* Quota size xattr is managed outside of the control of the ec xlator.
     * This means that it might not be updated at the same time on all
     * bricks and we can receive slightly different values. If that's the
     * case, we take the maximum of all received values.
     */
    for (i = 0; i < ec->nodes; i++) {
        if ((data[i] == NULL) || (data[i] == EC_MISSING_DATA) ||
            (quota_data_to_meta(data[i], &size) < 0)) {
            continue;
        }

        if (size.size > max_size.size)
            max_size.size = size.size;
        if (size.file_count > max_size.file_count)
            max_size.file_count = size.file_count;
        if (size.dir_count > max_size.dir_count)
            max_size.dir_count = size.dir_count;
    }

    max_size.size *= ec->fragments;

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    return quota_dict_set_meta(dict, key, &max_size, IA_IFDIR);
}

int32_t
ec_dict_data_stime(ec_cbk_data_t *cbk, int32_t which, char *key)
{
    ec_t *ec = cbk->fop->xl->private;
    data_t *data[ec->nodes];
    dict_t *dict;
    int32_t i, err;

    ec_dict_list(data, cbk, which, key, _gf_false);

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    for (i = 0; i < ec->nodes; i++) {
        if ((data[i] == NULL) || (data[i] == EC_MISSING_DATA)) {
            continue;
        }
        err = gf_get_max_stime(cbk->fop->xl, dict, key, data[i]);
        if (err != 0) {
            gf_msg(cbk->fop->xl->name, GF_LOG_ERROR, -err,
                   EC_MSG_STIME_COMBINE_FAIL, "STIME combination failed");

            return err;
        }
    }

    return 0;
}

int32_t
ec_dict_data_combine(dict_t *dict, char *key, data_t *value, void *arg)
{
    ec_dict_combine_t *data = arg;

    if ((strcmp(key, GF_XATTR_PATHINFO_KEY) == 0) ||
        (strcmp(key, GF_XATTR_USER_PATHINFO_KEY) == 0)) {
        return ec_dict_data_concat("(<EC:%s> { })", data->cbk, data->which, key,
                                   NULL, NULL, _gf_false,
                                   data->cbk->fop->xl->name);
    }

    if (strncmp(key, GF_XATTR_CLRLK_CMD, SLEN(GF_XATTR_CLRLK_CMD)) == 0) {
        return ec_dict_data_concat("{\n}", data->cbk, data->which, key, NULL,
                                   NULL, _gf_false);
    }

    if (strncmp(key, GF_XATTR_LOCKINFO_KEY, SLEN(GF_XATTR_LOCKINFO_KEY)) == 0) {
        return ec_dict_data_merge(data->cbk, data->which, key);
    }

    if (strcmp(key, GET_LINK_COUNT) == 0) {
        return ec_dict_data_max32(data->cbk, data->which, key);
    }

    if (strcmp(key, GLUSTERFS_OPEN_FD_COUNT) == 0) {
        return ec_dict_data_max32(data->cbk, data->which, key);
    }
    if ((strcmp(key, GLUSTERFS_INODELK_COUNT) == 0) ||
        (strcmp(key, GLUSTERFS_ENTRYLK_COUNT) == 0)) {
        return ec_dict_data_max32(data->cbk, data->which, key);
    }

    if (strcmp(key, QUOTA_SIZE_KEY) == 0) {
        return ec_dict_data_quota(data->cbk, data->which, key);
    }
    /* Ignore all other quota attributes */
    if (strncmp(key, EC_QUOTA_PREFIX, SLEN(EC_QUOTA_PREFIX)) == 0) {
        return 0;
    }

    if (XATTR_IS_NODE_UUID(key)) {
        if (data->cbk->fop->int32) {
            /* List of node uuid is requested */
            return ec_dict_data_concat("{ }", data->cbk, data->which, key,
                                       GF_XATTR_LIST_NODE_UUIDS_KEY, UUID0_STR,
                                       _gf_true);
        } else {
            return ec_dict_data_uuid(data->cbk, data->which, key);
        }
    }

    if (fnmatch(GF_XATTR_STIME_PATTERN, key, FNM_NOESCAPE) == 0) {
        return ec_dict_data_stime(data->cbk, data->which, key);
    }

    if (fnmatch(MARKER_XATTR_PREFIX ".*." XTIME, key, FNM_NOESCAPE) == 0) {
        return ec_dict_data_max64(data->cbk, data->which, key);
    }

    if (strcmp(key, GF_PRESTAT) == 0 || strcmp(key, GF_POSTSTAT) == 0) {
        return ec_dict_data_iatt(data->cbk, data->which, key);
    }

    return 0;
}

int32_t
ec_dict_combine(ec_cbk_data_t *cbk, int32_t which)
{
    dict_t *dict = NULL;
    ec_dict_combine_t data;
    int32_t err = 0;

    data.cbk = cbk;
    data.which = which;

    dict = (which == EC_COMBINE_XDATA) ? cbk->xdata : cbk->dict;
    if (dict != NULL) {
        err = dict_foreach(dict, ec_dict_data_combine, &data);
        if (err != 0) {
            gf_msg(cbk->fop->xl->name, GF_LOG_ERROR, -err,
                   EC_MSG_DICT_COMBINE_FAIL, "Dictionary combination failed");

            return err;
        }
    }

    return 0;
}

int32_t
ec_vector_compare(struct iovec *dst_vector, int32_t dst_count,
                  struct iovec *src_vector, int32_t src_count)
{
    int32_t dst_size = 0, src_size = 0;

    if (dst_count > 0) {
        dst_size = iov_length(dst_vector, dst_count);
    }
    if (src_count > 0) {
        src_size = iov_length(src_vector, src_count);
    }

    return (dst_size == src_size);
}

int32_t
ec_flock_compare(struct gf_flock *dst, struct gf_flock *src)
{
    if ((dst->l_type != src->l_type) || (dst->l_whence != src->l_whence) ||
        (dst->l_start != src->l_start) || (dst->l_len != src->l_len) ||
        (dst->l_pid != src->l_pid) ||
        !is_same_lkowner(&dst->l_owner, &src->l_owner)) {
        return 0;
    }

    return 1;
}

void
ec_statvfs_combine(struct statvfs *dst, struct statvfs *src)
{
    if (dst->f_bsize < src->f_bsize) {
        dst->f_bsize = src->f_bsize;
    }

    if (dst->f_frsize < src->f_frsize) {
        dst->f_blocks *= dst->f_frsize;
        dst->f_blocks /= src->f_frsize;

        dst->f_bfree *= dst->f_frsize;
        dst->f_bfree /= src->f_frsize;

        dst->f_bavail *= dst->f_frsize;
        dst->f_bavail /= src->f_frsize;

        dst->f_frsize = src->f_frsize;
    } else if (dst->f_frsize > src->f_frsize) {
        src->f_blocks *= src->f_frsize;
        src->f_blocks /= dst->f_frsize;

        src->f_bfree *= src->f_frsize;
        src->f_bfree /= dst->f_frsize;

        src->f_bavail *= src->f_frsize;
        src->f_bavail /= dst->f_frsize;
    }
    if (dst->f_blocks > src->f_blocks) {
        dst->f_blocks = src->f_blocks;
    }
    if (dst->f_bfree > src->f_bfree) {
        dst->f_bfree = src->f_bfree;
    }
    if (dst->f_bavail > src->f_bavail) {
        dst->f_bavail = src->f_bavail;
    }

    if (dst->f_files < src->f_files) {
        dst->f_files = src->f_files;
    }
    if (dst->f_ffree > src->f_ffree) {
        dst->f_ffree = src->f_ffree;
    }
    if (dst->f_favail > src->f_favail) {
        dst->f_favail = src->f_favail;
    }
    if (dst->f_namemax > src->f_namemax) {
        dst->f_namemax = src->f_namemax;
    }

    if (dst->f_flag != src->f_flag) {
        gf_msg_debug(THIS->name, 0,
                     "Mismatching file system flags "
                     "(%lX, %lX)",
                     dst->f_flag, src->f_flag);
    }
    dst->f_flag &= src->f_flag;
}

int32_t
ec_combine_check(ec_cbk_data_t *dst, ec_cbk_data_t *src, ec_combine_f combine)
{
    ec_fop_data_t *fop = dst->fop;

    if (dst->op_ret != src->op_ret) {
        gf_msg_debug(fop->xl->name, 0,
                     "Mismatching return code in "
                     "answers of '%s': %d <-> %d",
                     ec_fop_name(fop->id), dst->op_ret, src->op_ret);

        return 0;
    }
    if (dst->op_ret < 0) {
        if (dst->op_errno != src->op_errno) {
            gf_msg_debug(fop->xl->name, 0,
                         "Mismatching errno code in "
                         "answers of '%s': %d <-> %d",
                         ec_fop_name(fop->id), dst->op_errno, src->op_errno);

            return 0;
        }
    }

    if (!ec_dict_compare(dst->xdata, src->xdata)) {
        gf_msg(fop->xl->name, GF_LOG_DEBUG, 0, EC_MSG_XDATA_MISMATCH,
               "Mismatching xdata in answers "
               "of '%s'",
               ec_fop_name(fop->id));

        return 0;
    }

    if ((dst->op_ret >= 0) && (combine != NULL)) {
        return combine(fop, dst, src);
    }

    return 1;
}

void
ec_combine(ec_cbk_data_t *newcbk, ec_combine_f combine)
{
    ec_fop_data_t *fop = newcbk->fop;
    ec_cbk_data_t *cbk = NULL, *tmp = NULL;
    struct list_head *item = NULL;
    int32_t needed = 0;
    char str[32];

    LOCK(&fop->lock);

    fop->received |= newcbk->mask;

    item = fop->cbk_list.prev;
    list_for_each_entry(cbk, &fop->cbk_list, list)
    {
        if (ec_combine_check(newcbk, cbk, combine)) {
            newcbk->count += cbk->count;
            newcbk->mask |= cbk->mask;

            item = cbk->list.prev;
            while (item != &fop->cbk_list) {
                tmp = list_entry(item, ec_cbk_data_t, list);
                if (tmp->count >= newcbk->count) {
                    break;
                }
                item = item->prev;
            }
            list_del(&cbk->list);

            newcbk->next = cbk;

            break;
        }
    }
    list_add(&newcbk->list, item);

    ec_trace("ANSWER", fop, "combine=%s[%d]",
             ec_bin(str, sizeof(str), newcbk->mask, 0), newcbk->count);

    cbk = list_entry(fop->cbk_list.next, ec_cbk_data_t, list);
    if ((fop->mask ^ fop->remaining) == fop->received) {
        needed = fop->minimum - cbk->count;
    }

    UNLOCK(&fop->lock);

    if (needed > 0) {
        ec_dispatch_next(fop, newcbk->idx);
    }
}