summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/nsr-recon/src/recon_xlator.c
blob: ffbf74296974af682360f51cc3100736b17dd08e (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
1006
1007
1008
1009
1010
/*
   Copyright (c) 2013 Red Hat, Inc. <http://www.redhat.com>
   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.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include "call-stub.h"
#include "defaults.h"
#include "xlator.h"

#include "recon_driver.h"
#include "recon_xlator.h"

typedef struct _nsr_recon_fd_s {
        int32_t term;
        nsr_recon_driver_state_t state;
        uint32_t first_index;
        uint32_t last_index;
        call_frame_t *frame;
} nsr_recon_fd_t;

#if defined(NSR_DEBUG)

void
_recon_main_log (const char *func, int line, char *member, FILE *fp,
                 char *fmt, ...)
{
        va_list         ap;
        char            *buf    = NULL;
        int             retval;

        if (!fp) {
                fp = recon_create_log(member,"recon-main-log");
                if (!fp) {
                        return;
                }
        }

        va_start(ap,fmt);
        retval = vasprintf(&buf,fmt,ap);
        if (buf) {
                fprintf(fp,"[%s:%d] %.*s\n",func,line,retval,buf);
                free(buf);
        }
        va_end(ap);
}

#endif

// Given fd, get back the NSR based fd context.
static int32_t this_fd_ctx_get(fd_t *fd, xlator_t *this, nsr_recon_fd_t **rfd)
{
        uint64_t tmp = 0;
        int32_t ret = -1;

        if ((ret = fd_ctx_get(fd, this, &tmp)) != 0) {
                return ret;
        } else {
                *rfd = (nsr_recon_fd_t *)tmp;
                return 0;
        }
}

// Add the frame in q after associating with term
// term usage tbd
static void put_frame(nsr_recon_private_t *priv,
                      call_frame_t *frame,
                      uint32_t term)
{
	xlator_t *this = priv->this;
        recon_main_log (this->name, GF_LOG_INFO, "adding frame for term %d \n", term);
	priv->frame = frame;
	return;
}

// get the frame from the queue given the term
// term usage tbd
static void get_frame(nsr_recon_private_t *priv,
                      call_frame_t **frame,
                      uint32_t term)
{
	if (frame != NULL)
		*frame = priv->frame;
	priv->frame = NULL;
	return;
}

// check if there are outstanding frames
static gf_boolean_t is_frame(nsr_recon_private_t *priv)
{
	return((priv->frame != NULL) ? _gf_true : _gf_false);
}

#define ENTRY_SIZE 128

long
get_entry_count (char *path)
{
        int             fd;
        struct stat     buf;
        unsigned long   entries         = -1;
        long            min;            /* last entry not known to be empty */
        long            max;            /* first entry known to be empty */
        long            curr;
        char            entry[ENTRY_SIZE];
        void            *err_label      = &&done;

        fd = open(path,O_RDONLY);
        if (fd < 0) {
                goto *err_label;
        }
        err_label = &&close_fd;

        if (fstat(fd,&buf) < 0) {
                goto *err_label;
        }

        min = 0;
        max = buf.st_size / ENTRY_SIZE;
        printf("max = %ld\n",max);

        while ((min+1) < max) {
                curr = (min + max) / 2;
                printf("trying entry %ld\n",curr);
                if (lseek(fd,curr*ENTRY_SIZE,SEEK_SET) < 0) {
                        goto *err_label;
                }
                if (read(fd,entry,sizeof(entry)) != sizeof(entry)) {
                        goto *err_label;
                }
                if ((entry[0] == '_') && (entry[1] == 'P')) {
                        min = curr;
                }
                else {
                        max = curr;
                }
        }

        entries = max;

close_fd:
        close(fd);
done:
        return entries;
}

// Get the term info for the term number specified
void nsr_recon_libchangelog_get_this_term_info(xlator_t *this, char *bp, int32_t term, nsr_recon_last_term_info_t *lt)
{
        char path[PATH_MAX];
        long entries;

        bzero(lt, sizeof(nsr_recon_last_term_info_t));
        lt->last_term = term;
        sprintf(path,"%s/%s%d",bp,"TERM.",term);
        entries = get_entry_count(path);
        if (entries > 1) {
                /* The first entry is actually a header. */
                lt->first_index = 1;
                /*
                 * This seems wrong, because it means that last_index*128 will
                 * be exactly at EOF and commited_ops will be one greater than
                 * it should be.  Maybe some other code makes the exact
                 * opposite mistake to compensate.
                 */
                lt->last_index = lt->commited_ops = (int)entries;
        }
        recon_main_log (this->name, GF_LOG_INFO, "for term=%d got first_index=%d last_index=%d commited_ops=%d\n",
                        term, lt->first_index, lt->last_index, lt->commited_ops);
        return;
}

// Given the term number, find the last term in the changelogs
void nsr_recon_libchangelog_get_last_term_info(xlator_t *this, char *bp, int32_t term, nsr_recon_last_term_info_t *lt)
{
        uint32_t t = term;
        struct stat buf;
        char path[PATH_MAX];
        bzero(lt, sizeof(nsr_recon_last_term_info_t));
        while(t) {
                // journal file is of type TERM-1.jnl
                sprintf(path,"%s/%s%d",bp,"TERM.",t);
                if (!stat(path, &buf)) {
                        nsr_recon_libchangelog_get_this_term_info(this, bp, t, lt);
                        recon_main_log (this->name, GF_LOG_INFO, "got last term given current term %d as %d\n", term, t);
                        return;
                }
                t--;
        }
        recon_main_log (this->name, GF_LOG_INFO, "got no last term given current term %d \n", term);

        return;
}

// Return back the frame stored against the term
void nsr_recon_return_back(nsr_recon_private_t *priv, uint32_t term, int32_t status, int32_t op_errno)
{
        call_frame_t *old_frame = NULL;
        xlator_t *this = priv->this;

        get_frame(priv, &old_frame, term);
        if (old_frame) {
                recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_writev returns old frame \n");
                                // first return the original write for which this ack was sent
                STACK_UNWIND_STRICT (writev, old_frame, status, op_errno, NULL, NULL, NULL);
        } else {
                recon_main_log (this->name, GF_LOG_ERROR, "EIII---nsr_recon_writev cnnot return old frame \n");
        }
}

typedef enum records_type_t {
        fop_gfid_pgfid_oldloc_newloc = 1,
        fop_gfid_pgfid_entry = fop_gfid_pgfid_oldloc_newloc + 1,
        fop_gfid = fop_gfid_pgfid_entry + 1 ,
        fop_gfid_offset = fop_gfid + 1,
        fop_gfid_offset_len = fop_gfid_offset + 1,
} records_type_t;

// Get the backend ./glusterfs/xx/xx/<...> path
static void
get_gfid_path(nsr_recon_private_t *priv, char *gfid, char *path)
{
        strcpy(path, priv->base_dir);
        strcat(path, "/.glusterfs/");
        strncat(path,gfid,2);
        strcat(path,"/");
        strncat(path,gfid+2,2);
        strcat(path,"/");
        strcat(path,gfid);
}


// Get the link to which backend points to
static gf_boolean_t
get_link_using_gfid(nsr_recon_private_t *priv, char *gfid, char *path)
{
        char lp[PATH_MAX];
        xlator_t *this = priv->this;
        get_gfid_path(priv,gfid, lp);
        if (readlink(lp, path, 255) == -1) {
                GF_ASSERT(0);
                recon_main_log(priv->this, GF_LOG_ERROR,
                               "cannot get readlink  for %s\n",lp);
                return _gf_false;
        }
        return _gf_true;
}

// Get the list of changelog records given a term , first and last index.
//
// TBD: rewrite this hideous ball of mud in at least the following ways:
//
//      (1) Break out the code for handling a single record into a separate
//          function, to make error handling easier and reduce "indentation
//          creep" so the code's readable.
//
//      (2) Change all of the fop_xxx_yyy nonsense to OR together values
//          like FOP_HAS_FIELD_XXX and FOP_HAS_FIELD_YYY, to reduce code
//          duplication and facilitate the addition of new fields.
//
//      (3) Stop making so many assumptions about the underlying formats.
//          The code as it is won't even work for the existing binary format,
//          let alone as changelog evolves over time.
//
// Really, 90% of this code should just GO AWAY in favor of using
// libgfchangelog, enhanced as necessary to support our needs.

/*
 * Use this macro to skip over a field we're not using yet.
 * NB: the body is a null statement on purpose
 * TBD: all instances of this should be removed eventually!
 */
#define SKIP_FIELD do /* nothing */ ; while (*(start++) != '\0')

#define SKIP_OVER
gf_boolean_t nsr_recon_libchangelog_get_records(xlator_t *this, char *bp, int32_t term, uint32_t first, uint32_t last, void *buf)
{
        // do a mmap; seek into the first and read all records till last.
        // TBD - right now all records are pseudo holes but mark them as fills.
        // TBD - pseudo hole to be implemented when actual fsync gets done on data.
        char *rb = NULL, *orig = NULL;
        char path[PATH_MAX];
        int fd;
        uint32_t index = 0;

        recon_main_log (this->name, GF_LOG_INFO,
                        "libchangelog_get_records called for term %d  index from %d to %d \n",
                        term, first, last );

        orig = rb = GF_CALLOC(128, ((last - first) + 1),
                              gf_mt_recon_changelog_buf_t);

        sprintf(path,"%s/%s%d",bp,"TERM.",term);
        fd = open(path, O_RDONLY);
        if (fd == -1) {
		return _gf_false;
	} else {
                char *start = NULL;
                nsr_recon_record_details_t * rec = (nsr_recon_record_details_t *)buf;

                if (first == 0)
                        lseek(fd, 128, SEEK_SET);
                else
                        lseek(fd, first * 128, SEEK_SET);
                if (read(fd, rb, (last - first  + 1) * 128) == -1) {
			return _gf_false;
		}
                start = rb;
                index = first;
                do {
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "libchangelog_get_records start inspecting records at index %d \n",
                                        index );
                        if (!strncmp(start, "_PRE_", 5)) {
                                uint32_t i;
                                uint32_t opcode = 0;
                                records_type_t type;

                                start += 5;
                                // increment by the NULLs after the PRE
                                start += 4;
                                SKIP_FIELD;     // real index
                                // now we have the opcode
                                while (*start != '\0') {
                                        opcode *= 10;
                                        opcode += (*(start++) - '0');
                                }
                                ++start;
                                recon_main_log (this->name, GF_LOG_ERROR,
                                                "libchangelog_get_records: got opcode %d @index %d\n", opcode, index);
                                if ((opcode == GF_FOP_RENAME)) {
                                        type = fop_gfid_pgfid_oldloc_newloc;
                                } else if ((opcode == GF_FOP_UNLINK) ||
                                    (opcode == GF_FOP_RMDIR) ||
                                    (opcode == GF_FOP_LINK) ||
                                    (opcode == GF_FOP_MKDIR) ||
                                    (opcode == GF_FOP_SYMLINK) ||
                                    (opcode == GF_FOP_MKNOD) ||
                                    (opcode == GF_FOP_CREATE)) {
                                        type = fop_gfid_pgfid_entry;
                                } else if ((opcode == GF_FOP_FSETATTR) ||
                                    (opcode == GF_FOP_SETATTR) ||
                                    (opcode == GF_FOP_FREMOVEXATTR) ||
                                    (opcode == GF_FOP_REMOVEXATTR) ||
                                    (opcode == GF_FOP_SETXATTR) ||
                                    (opcode == GF_FOP_FSETXATTR)) {
                                        type = fop_gfid;
                                } else if ((opcode == GF_FOP_TRUNCATE) ||
                                    (opcode == GF_FOP_FTRUNCATE)) {
                                        type = fop_gfid_offset;
                                } else if (opcode == GF_FOP_WRITE) {
                                        type = fop_gfid_offset_len;
                                } else {
                                        recon_main_log (this->name,
                                                        GF_LOG_ERROR,
                                                        "libchangelog_get_records:got no proper opcode %d @index %d\n",
                                                        opcode, index);
                                        //GF_ASSERT(0);
                                        // make this as a hole.
                                        // TBD - check this logic later. maybe we should raise alarm here because
                                        // this means that changelog is corrupted. We are not handling changelog
                                        // corruptions as of now.
                                        rec->type = NSR_LOG_HOLE;
                                        goto finish;
                                }
                                // TBD - handle psuedo holes once that logic is in.
                                rec->type = NSR_LOG_FILL;
                                recon_main_log (this->name, GF_LOG_ERROR,
                                               "libchangelog_get_records:got type %d at index %d \n",
                                                rec->type, index);
                                rec->op = opcode;

                                // Now get the gfid and parse it
                                // before that increment the pointer
                                for (i=0; i < 36; i++) {
                                        rec->gfid[i] = (*start);
                                        start++;
                                }
                                rec->gfid[i] = '\0';

                                if (opcode == GF_FOP_SYMLINK) {
                                        // the symlink would have been removed. Hence ignore this.
                                        // TBD - have an uniform error policy in case of such cases.
                                        // Right now we are handling some on the source and some on the destination.
                                        if(get_link_using_gfid(this->private, rec->gfid, rec->link_path) == _gf_false) {
                                                rec->type = NSR_LOG_HOLE;
                                                goto finish;
                                        }
                                }

                                GF_ASSERT(*start == 0);
                                start ++;

                                i = 0;
                                // If type is fop_gfid_offset+_len, get offset
                                if ((type == fop_gfid_offset) || (type == fop_gfid_offset_len)) {
                                        char offset_str[128];
                                        while(*start != 0) {
                                                offset_str[i++] = *start;
                                                start ++;
                                        }
                                        offset_str[i] = '\0';
                                        // get over the 0
                                        start++;
                                        rec->offset = strtoul(offset_str, NULL, 10);
                                        recon_main_log (this->name,
                                                        GF_LOG_ERROR,
                                                        "libchangelog_get_records:got offset %d @index %d \n", rec->offset, index);

                                }
                                i = 0;
                                if (type == fop_gfid_offset_len) {
                                        char len_str[128];
                                        while(*start != 0) {
                                                len_str[i++] = *start;
                                                start ++;
                                        }
                                        len_str[i] = '\0';
                                        // get over the 0
                                        start++;
                                        rec->len = strtoul(len_str, NULL, 10);
                                        recon_main_log (this->name,
                                                        GF_LOG_ERROR,
                                                        "libchangelog_get_records:got length %d @index %d \n", rec->len, index);
                                }
                                i = 0;
                                if (type == fop_gfid_pgfid_entry) {
                                        switch (opcode) {
                                        case GF_FOP_CREATE:
                                        case GF_FOP_MKDIR:
                                        case GF_FOP_MKNOD:
                                                SKIP_FIELD;     // mode
                                                break;
                                        /* TBD: handle GF_FOP_SYMLINK target */
                                        default:
                                                ;
                                        }
                                        SKIP_FIELD;             // uid
                                        SKIP_FIELD;             // gid
                                        if (opcode == GF_FOP_MKNOD) {
                                                SKIP_FIELD;     // dev
                                        }
                                        // first get the gfid and then the path
                                        for (i=0; i < 36; i++) {
                                                rec->pargfid[i] = (*start);
                                                start++;
                                        }
                                        rec->pargfid[i] = '\0';
                                        GF_ASSERT(*start == '/');
                                        start ++;

                                        i = 0;
                                        while(*start != 0) {
                                                rec->entry[i++] = *start;
                                                start ++;
                                        }
                                        rec->entry[i] = '\0';
                                        // get over the 0
                                        start++;
                                        /*
                                         * Having to add this as a special case
                                         * is awful.  See the function header
                                         * comment for the real solution.
                                         */
                                        if (opcode == GF_FOP_CREATE) {
                                                rec->mode = 0;
                                                while (*start != '\0') {
                                                        rec->mode *= 10;
                                                        rec->mode += *start
                                                                   - '0';
                                                        ++start;
                                                }
                                                ++start;
                                        }
                                        recon_main_log (this->name,
                                                        GF_LOG_ERROR,
                                                        "libchangelog_get_records:got entry %s @index %d \n", rec->entry, index);

                                }
                                i = 0;
                                if (type == fop_gfid_pgfid_oldloc_newloc) {

                                        // first get the source and then the destination
                                        // source stuff gets stored in pargfid/entry
                                        for (i=0; i < 36; i++) {
                                                rec->pargfid[i] = (*start);
                                                start++;
                                        }
                                        rec->pargfid[i] = '\0';
                                        GF_ASSERT(*start == '/');
                                        start ++;

                                        i=0;
                                        while(*start != 0) {
                                                rec->entry[i++] = *start;
                                                start ++;
                                        }
                                        rec->entry[i] = '\0';
                                        // get over the 0
                                        start++;

                                        // dst stuff gets stored in gfid/newloc
                                        for (i=0; i < 36; i++) {
                                                rec->gfid[i] = (*start);
                                                start++;
                                        }
                                        rec->gfid[i] = '\0';
                                        GF_ASSERT(*start == '/');
                                        start ++;
                                        i = 0;
                                        while(*start != 0) {
                                                rec->newloc[i++] = *start;
                                                start ++;
                                        }
                                        rec->newloc[i] = '\0';
                                        // get over the 0
                                        start++;

                                }
                                ENDIAN_CONVERSION_RD((*rec), _gf_false); //htonl
                        }
finish:
                        if (index == last)
                                break;
                        index++;
                        rb += 128;
                        start = rb;
                        rec++;
                } while(1);
        }
        GF_FREE(orig);
        close(fd);

        recon_main_log (this->name, GF_LOG_INFO,
                        "libchangelog_get_records finsihed inspecting records for  term %d \n",
                        term);
        return _gf_true;
}

int32_t
nsr_recon_open (call_frame_t *frame, xlator_t *this,
                loc_t *loc, int32_t flags, fd_t *fd, dict_t *xdata)
{
        int32_t op_ret       = 0;
        int32_t op_errno     = 0;
        nsr_recon_fd_t *rfd = NULL;

        recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_open called for path %s \n",loc->path );
        rfd = GF_CALLOC (1, sizeof (*rfd), gf_mt_recon_fd_t);
        if (!rfd) {
                op_ret = -1;
                op_errno = ENOMEM;
        }

        op_ret = fd_ctx_set (fd, this, (uint64_t)(long)rfd);
        if (op_ret) {
                op_ret = -1;
                op_errno = EINVAL;
        }
        recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_open returns with %d for path %s \n",op_ret,loc->path );
        STACK_UNWIND_STRICT (open, frame, op_ret, op_errno, fd, NULL);
        return 0;
}

int32_t
nsr_recon_writev (call_frame_t *frame, xlator_t *this, fd_t *fd,
                  struct iovec *vector, int32_t count, off_t offset,
                  uint32_t flags, struct iobref *iobref, dict_t *xdata)
{
        nsr_recon_fd_t *rfd = NULL;
        nsr_recon_private_t *priv = NULL;
        int32_t op_ret = 0;
        int32_t op_errno = 0;
        int32_t ret = 0;

        ret = this_fd_ctx_get (fd, this, &rfd);
        if (ret < 0) {
                return -1;
        }
        priv = (nsr_recon_private_t *)this->private;

        recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_writev called for offset %d \n",(unsigned int)offset );
        GF_ASSERT(count == 1);
        switch (offset) {
                // client(brick, leader) writes the role of the node
                case nsr_recon_xlator_sector_1 :
                {
                        nsr_recon_role_t rr;
                        memcpy((void *)&rr, (void *)vector[0].iov_base, sizeof(rr));
                        ENDIAN_CONVERSION_RR(rr, _gf_true); //ntohl

                        recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_writev called to set role %d\n", rr.role);
                        if ((rr.role != leader) &&
                            (rr.role != reconciliator) &&
                            (rr.role != resolutor) &&
                            (rr.role != joiner)) {
                                recon_main_log (this->name, GF_LOG_ERROR,
                                                "EIII---nsr_recon_writev cannot set state \n");
                                STACK_UNWIND_STRICT (writev, frame, -1, op_errno,
                                                     NULL, NULL, NULL);
                        }

                        GF_ASSERT(rr.num <= MAXIMUM_REPLICA_STRENGTH);

			// Check if already a role play is going on. If yes return with EAGAIN.
			// Ideally we should check if we have got a higher term number while
			// servicing a lower term number; if so abort the older one.
			// However the abort infrastructure needs to be sketched properly; TBD.
			if (is_frame(priv) == _gf_true) {
                                recon_main_log (this->name, GF_LOG_ERROR,
                                                "nsr_recon_writev set_role - already role play \n");
                                STACK_UNWIND_STRICT (writev, frame, -1, EAGAIN,
                                                     NULL, NULL, NULL);
			} else {

				// Store the stack frame so that when the actual job gets finished
				// we send the response back to the brick.
				put_frame(priv, frame, rr.current_term);
				if (nsr_recon_driver_set_role(priv->driver_thread_context,
					                      &rr,
							       rr.current_term) == _gf_false) {
					 get_frame(priv, NULL, rr.current_term);
					 recon_main_log (this->name, GF_LOG_ERROR,
                                                         "nsr_recon_writev set_role - cannot seem to set role \n");
					 STACK_UNWIND_STRICT (writev, frame, -1, op_errno,
							       NULL, NULL, NULL);
				} else {
					 recon_main_log (this->name, GF_LOG_INFO,
                                                         "nsr_recon_writev set_role - set role succesfully \n");
				}
			}
                        break;
                }
                // client(reconciliator) writes how much it needs for the read
                case nsr_recon_xlator_sector_2 :
                {
                        nsr_recon_log_info_t li;
                        memcpy((void *)&li, (void *)vector[0].iov_base, sizeof(li));
                        ENDIAN_CONVERSION_LI(li, _gf_true); //ntohl

                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_writev - setting term info for reconcilation info. term=%d, first_index=%d,start_index=%d \n",
                                        li.term, li.first_index, li.last_index);
                        rfd->term = li.term;
                        rfd->last_index = li.last_index;
                        rfd->first_index = li.first_index;
                        STACK_UNWIND_STRICT (writev, frame, op_ret, op_errno,
                                             NULL, NULL, NULL);
                        break;
                }
                // client(reconciliator) writes term for which it needs info
                case nsr_recon_xlator_sector_3 :
                {
                        int32_t term;

                        memcpy((void *)&term, (void *)vector[0].iov_base, sizeof(term));
                        term = ntohl(term); //ntohl
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_writev - setting term info for term info. term=%d\n",
                                        term);
                        rfd->term = term;
                        STACK_UNWIND_STRICT (writev, frame, op_ret, op_errno,
                                             NULL, NULL, NULL);
                        break;
                }
                // client(reconciliator) writes current term so that it gets last term info later
                case nsr_recon_xlator_sector_4 :
                {
                        int32_t term;

                        memcpy((void *)&term, (void *)vector[0].iov_base, sizeof(term));
                        term = ntohl(term); //ntohl
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_writev - setting term info for last term info  given current term=%d\n",
                                        term);
                        rfd->term = term;
                        STACK_UNWIND_STRICT (writev, frame, op_ret, op_errno,
                                             NULL, NULL, NULL);
                        break;
                }
		default:
		{
                        recon_main_log (this->name, GF_LOG_ERROR,
                                       "nsr_recon_writev called with wrong offset\n");
                        STACK_UNWIND_STRICT (writev, frame, -1, op_errno,
                                             NULL, NULL, NULL);
			break;
		}
        }

        return 0;
}

int
nsr_recon_readv (call_frame_t *frame, xlator_t *this,
                 fd_t *fd, size_t size, off_t offset, uint32_t flags, dict_t *xdata)
{
        nsr_recon_fd_t *rfd = NULL;
        int32_t op_errno = 0;
        // copied stuff from quick-read.c and posix.c
        struct iobuf *iobuf = NULL;
        struct iobref    *iobref = NULL;
        struct iovec      iov = {0, };
        int32_t ret = -1;
        nsr_recon_private_t *priv = NULL;

        iobuf = iobuf_get2 (this->ctx->iobuf_pool, size);
        if (!iobuf) {
                op_errno = ENOMEM;
                goto out;
        }

        iobref = iobref_new ();
        if (!iobref) {
                op_errno = ENOMEM;
                goto out;
        }

        iobref_add (iobref, iobuf);

        ret = this_fd_ctx_get (fd, this, &rfd);
        if (ret < 0) {
                op_errno = -ret;
                goto out;
        }
        priv = (nsr_recon_private_t *)this->private;

        recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_readv called for offset %d \n",(unsigned int)offset );
        switch (offset) {
                // client(leader) reads from here to get info for this term on this node
                // invole libchagelog to get the information
                case nsr_recon_xlator_sector_3 :
                {
                        nsr_recon_last_term_info_t lt;
                        GF_ASSERT(size == sizeof(lt));
                        nsr_recon_libchangelog_get_this_term_info(this,priv->changelog_base_path, rfd->term, &lt);
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_readv - getting term info for term=%d, ops=%d, first=%d, last=%d\n",
                                        rfd->term, lt.commited_ops, lt.first_index, lt.last_index);
                        ENDIAN_CONVERSION_LT(lt, _gf_false); //htonl
                        memcpy(iobuf->ptr, &lt, size);
                        goto out;
                }
                // client(reconciliator) reads individual record information
                case nsr_recon_xlator_sector_2 :
                {
                        uint32_t num = (rfd->last_index - rfd->first_index + 1);
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_readv - expected size %lu got size %lu\n",
                                        (num * sizeof(nsr_recon_record_details_t)), size);

                        GF_ASSERT(size == (num * sizeof(nsr_recon_record_details_t)));
                        bzero(iobuf->ptr, size);
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_readv - getting records for term=%d from %d to %d\n",
                                        rfd->term, rfd->first_index, rfd->last_index);
                        nsr_recon_libchangelog_get_records(this, priv->changelog_base_path,
                                                           rfd->term, rfd->first_index, rfd->last_index, iobuf->ptr);
                        goto out;
                }
                // read last term info
                case nsr_recon_xlator_sector_4 :
                {
                        nsr_recon_last_term_info_t lt;
                        GF_ASSERT(size == sizeof(lt));
                        nsr_recon_libchangelog_get_last_term_info(this, priv->changelog_base_path, rfd->term, &lt);
                        recon_main_log (this->name, GF_LOG_INFO,
                                        "nsr_recon_readv - getting last term info given current term=%d. last term = %d ops=%d, first=%d, last=%d\n",
                                        rfd->term, lt.last_term,  lt.commited_ops, lt.first_index, lt.last_index);
                        ENDIAN_CONVERSION_LT(lt, _gf_false); //htonl
                        memcpy(iobuf->ptr, &lt, size);
                        goto out;
                }
		default:
		{
                        recon_main_log (this->name, GF_LOG_ERROR,
                                       "nsr_recon_readv called with wrong offset\n");
			op_errno = -1;
			break;
		}
        }

out:
        if (op_errno == 0) {
                iov.iov_base = iobuf->ptr;
                ret = iov.iov_len = size;
        }

        STACK_UNWIND_STRICT (readv, frame, ret, op_errno, &iov, 1, NULL, iobref , NULL);

        if (iobref)
                iobref_unref (iobref);
        if (iobuf)
                iobuf_unref (iobuf);
        return 0;
}

int
nsr_recon_lookup (call_frame_t *frame, xlator_t *this,
                  loc_t *loc, dict_t *xdata)
{
        struct iatt buf = {0, };
        // dirty hack to set root as regular but seems to work.
        buf.ia_type = IA_IFREG;
        recon_main_log (this->name, GF_LOG_INFO, "nsr_recon_lookup called \n");

        STACK_UNWIND_STRICT (lookup, frame, 0, 0, this->itable->root, &buf, NULL, NULL);
        return 0;
}


int32_t
nsr_recon_flush (call_frame_t *frame, xlator_t *this,
             fd_t *fd, dict_t *xdata)
{
        STACK_UNWIND_STRICT (flush, frame, 0, 0, NULL);
        return 0;
}


int32_t
mem_acct_init (xlator_t *this)
{
        int     ret = -1;

        GF_VALIDATE_OR_GOTO ("recon", this, out);

        ret = xlator_mem_acct_init (this, gf_mt_recon_end + 1);

        if (ret != 0) {
                gf_log (this->name, GF_LOG_ERROR,
                        "Memory accounting init" "failed");
                return ret;
        }
out:
        return ret;
}


int32_t
init (xlator_t *this)
{
        nsr_recon_private_t *priv        = NULL;
        char  *local, *members;
        unsigned int i=0;

        priv = GF_CALLOC (1, sizeof (*priv), gf_mt_recon_private_t);
        if (!priv) {
                gf_log (this->name, GF_LOG_ERROR,
                        "priv allocation error\n");
                return -1;
        }
        GF_OPTION_INIT ("replica-group-size", priv->replica_group_size, uint32, err);
        GF_OPTION_INIT ("vol-name", priv->volname, str, err);
        if (!priv->volname) {
                gf_log (this->name, GF_LOG_ERROR,
                        "missing volname option (required)");
                return -1;
        }
        GF_OPTION_INIT ("changelog-dir", priv->changelog_base_path, str, err);
        if (!priv->changelog_base_path) {
                gf_log (this->name, GF_LOG_ERROR,
                        "missing changelog directory option (required)");
                return -1;
        }
        GF_OPTION_INIT ("base-dir", priv->base_dir, str, err);
        if (!priv->base_dir) {
                gf_log (this->name, GF_LOG_ERROR,
                        "missing brick base directory option (required)");
                return -1;
        }
        GF_OPTION_INIT ("replica-group-members", members, str, err);
        if (!members) {
                gf_log (this->name, GF_LOG_ERROR,
                        "missing membership option (required)");
                return -1;
        }
        GF_OPTION_INIT ("local-member", local, str, err);
        if (!local) {
                gf_log (this->name, GF_LOG_ERROR,
                        "missing local member option (required)");
                return -1;
        }

        priv->replica_group_members = GF_CALLOC (priv->replica_group_size,
                                                 sizeof(char *),
                                                 gf_mt_recon_members_list_t);
        priv->replica_group_members[0] = GF_CALLOC (1,
                                                    strlen(local),
                                                    gf_mt_recon_member_name_t);
        if (!priv->replica_group_members || !(priv->replica_group_members[0])) {
                gf_log (this->name, GF_LOG_ERROR,
                        "str allocation error\n");
                return -1;
        }
        strcpy(priv->replica_group_members[0], local);
        for (i=1; i < priv->replica_group_size; i++) {
                char *member;
                if (i == 1)
                        member = strtok(members, ",");
                else
                        member = strtok(NULL, ",");
                priv->replica_group_members[i] = GF_CALLOC (1,
                        strlen(member) + 1, gf_mt_recon_member_name_t);
                if (!priv->replica_group_members[i]) {
                        gf_log (this->name, GF_LOG_ERROR,
                                "str allocation error\n");
                        return -1;
                }
                strcpy(priv->replica_group_members[i], member);
        }


        priv->this = this;
        this->private = (void *)priv;

        priv->fp = recon_create_log (priv->replica_group_members[0], "recon-main-log");
        if (!priv->fp)
                return -1;

        recon_main_log (this->name, GF_LOG_INFO, "creating reconciliation driver \n");

        if (pthread_create(&priv->thread_id, NULL, nsr_reconciliation_driver, priv)) {
                recon_main_log (this->name, GF_LOG_ERROR,
                        "pthread creation error \n");
                return -1;
        }

        INIT_LIST_HEAD(&(priv->list));


	return 0;

err:
        return -1;
}


void
fini (xlator_t *this)
{
        nsr_recon_private_t *priv        = NULL;
        void *ret = NULL;

        priv = (nsr_recon_private_t *)this->private;

        pthread_cancel(priv->thread_id);
        pthread_join(priv->thread_id, &ret);
}


struct xlator_fops fops = {
        .open = nsr_recon_open,
        .readv = nsr_recon_readv,
        .writev = nsr_recon_writev,
        .lookup = nsr_recon_lookup,
        .flush = nsr_recon_flush
};

struct xlator_cbks cbks = {
};

struct volume_options options[] = {
        { .key = {"replica-group-size"},
          .type = GF_OPTION_TYPE_INT,
          .min = 2,
          .max = INT_MAX,
          .default_value = "2",
          .description = "Number of bricks in replica group. can be derived but putting it here for testing."
        },
        {
          .key = {"vol-name"},
          .type = GF_OPTION_TYPE_STR,
          .description = "volume name"
        },
        {
          .key = {"local-member"},
          .type = GF_OPTION_TYPE_STR,
          .description = "member(brick) for which this translator is responsible."
        },
        {
          .key = {"replica-group-members"},
          .type = GF_OPTION_TYPE_STR,
          .description = "Comma seperated member names other than local."
        },
        {
          .key = {"changelog-dir"},
          .type = GF_OPTION_TYPE_STR,
          .description = "Base directory where per term changelogs are maintained."
        },
        {
          .key = {"base-dir"},
          .type = GF_OPTION_TYPE_STR,
          .description = "Base directory for this brick. This should go away once we fix gfid based lookups"
        },
	{ .key = {NULL} },
};