~ubuntu-branches/ubuntu/wily/glusterfs/wily

« back to all changes in this revision

Viewing changes to xlators/mgmt/glusterd/src/glusterd-store.c

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2011-03-15 21:14:50 UTC
  • mfrom: (1.3.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20110315211450-5iekpirgkdntccne
Tags: 3.1.3-1
* New upstream release.
  - glusterfs-volgen and glusterfs-defrag have been removed.
* Suggest nfs-common with the glusterfs-server for native NFS support.
  Closes: #614988
* Migrate to dh_python2.
  Closes: #616826

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include "glusterd1.h"
48
48
#include "cli1.h"
49
49
#include "rpc-clnt.h"
 
50
#include "common-utils.h"
50
51
 
51
52
#include <sys/resource.h>
52
53
#include <inttypes.h>
381
382
        if (ret)
382
383
                goto out;
383
384
 
384
 
        uuid_unparse (volinfo->volume_id, buf);
385
385
        ret = glusterd_store_save_value (volinfo->shandle,
386
 
                                        GLUSTERD_STORE_KEY_VOL_ID, buf);
 
386
                                         GLUSTERD_STORE_KEY_VOL_ID,
 
387
                                         uuid_utoa (volinfo->volume_id));
387
388
        if (ret)
388
389
                goto out;
389
390
 
635
636
        return ret;
636
637
}
637
638
 
 
639
int
 
640
glusterd_store_handle_retrieve (char *path, glusterd_store_handle_t **handle)
 
641
{
 
642
        int32_t                 ret = -1;
 
643
        struct stat statbuf = {0};
 
644
 
 
645
        ret = stat (path, &statbuf);
 
646
        if (ret) {
 
647
                gf_log ("glusterd", GF_LOG_ERROR, "Unable to retrieve store "
 
648
                        "handle for %s, error: %s", path, strerror (errno));
 
649
                goto out;
 
650
        }
 
651
        ret =  glusterd_store_handle_new (path, handle);
 
652
out:
 
653
        gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
 
654
        return ret;
 
655
}
 
656
 
638
657
int32_t
639
658
glusterd_store_handle_destroy (glusterd_store_handle_t *handle)
640
659
{
676
695
int32_t
677
696
glusterd_store_uuid ()
678
697
{
679
 
        char            str[GLUSTERD_UUID_LEN] = {0,};
680
698
        glusterd_conf_t *priv = NULL;
681
699
        char            path[PATH_MAX] = {0,};
682
700
        int32_t         ret = -1;
684
702
 
685
703
        priv = THIS->private;
686
704
 
687
 
        uuid_unparse (priv->uuid, str);
688
 
 
689
705
        snprintf (path, PATH_MAX, "%s/%s", priv->workdir,
690
706
                  GLUSTERD_INFO_FILE);
691
707
 
702
718
        }
703
719
 
704
720
        ret = glusterd_store_save_value (priv->handle, GLUSTERD_STORE_UUID_KEY,
705
 
                                         str);
 
721
                                         uuid_utoa (priv->uuid));
706
722
 
707
723
        if (ret) {
708
724
                gf_log ("", GF_LOG_CRITICAL, "Storing uuid failed"
732
748
        if (!priv->handle) {
733
749
                snprintf (path, PATH_MAX, "%s/%s", priv->workdir,
734
750
                          GLUSTERD_INFO_FILE);
735
 
                ret = glusterd_store_handle_new (path, &handle);
 
751
                ret = glusterd_store_handle_retrieve (path, &handle);
736
752
 
737
753
                if (ret) {
738
754
                        gf_log ("", GF_LOG_ERROR, "Unable to get store "
798
814
                goto out;
799
815
        }
800
816
 
 
817
        strncpy (tmp_iter->filepath, shandle->path, sizeof (tmp_iter->filepath));
801
818
        *iter = tmp_iter;
802
819
        ret = 0;
803
820
 
807
824
}
808
825
 
809
826
int32_t
 
827
glusterd_store_validate_key_value (char *storepath, char *key, char*val,
 
828
                                   glusterd_store_op_errno_t *op_errno)
 
829
{
 
830
        int ret = 0;
 
831
 
 
832
        GF_ASSERT (op_errno);
 
833
        GF_ASSERT (storepath);
 
834
 
 
835
        if ((key == NULL) && (val == NULL)) {
 
836
                ret = -1;
 
837
                gf_log ("glusterd", GF_LOG_ERROR, "Glusterd store may be "
 
838
                        "corrupted, Invalid key and value (null) in %s",
 
839
                        storepath);
 
840
                *op_errno = GD_STORE_KEY_VALUE_NULL;
 
841
        } else if (key == NULL) {
 
842
                ret = -1;
 
843
                gf_log ("glusterd", GF_LOG_ERROR, "Glusterd store may be "
 
844
                        "corrupted, Invalid key (null) in %s", storepath);
 
845
                *op_errno = GD_STORE_KEY_NULL;
 
846
        } else if (val == NULL) {
 
847
                ret = -1;
 
848
                gf_log ("glusterd", GF_LOG_ERROR, "Glusterd store may be "
 
849
                        "corrupted, Invalid value (null) for key %s in %s",
 
850
                        key, storepath);
 
851
                *op_errno = GD_STORE_VALUE_NULL;
 
852
        } else {
 
853
                ret = 0;
 
854
                *op_errno = GD_STORE_SUCCESS;
 
855
        }
 
856
 
 
857
        return ret;
 
858
}
 
859
 
 
860
int32_t
810
861
glusterd_store_iter_get_next (glusterd_store_iter_t *iter,
811
 
                              char  **key, char **value)
 
862
                              char  **key, char **value,
 
863
                              glusterd_store_op_errno_t *op_errno)
812
864
{
813
865
        int32_t         ret = -1;
814
866
        char            scan_str[4096] = {0,};
816
868
        char            *free_str = NULL;
817
869
        char            *iter_key = NULL;
818
870
        char            *iter_val = NULL;
 
871
        glusterd_store_op_errno_t store_errno = GD_STORE_SUCCESS;
819
872
 
820
873
        GF_ASSERT (iter);
821
874
        GF_ASSERT (iter->file);
 
875
        GF_ASSERT (key);
 
876
        GF_ASSERT (value);
 
877
 
 
878
        *key = NULL;
 
879
        *value = NULL;
822
880
 
823
881
        ret = fscanf (iter->file, "%s", scan_str);
824
882
 
825
883
        if (ret <= 0) {
826
884
                ret = -1;
 
885
                store_errno = GD_STORE_EOF;
827
886
                goto out;
828
887
        }
829
888
 
830
889
        str = gf_strdup (scan_str);
831
 
        if (!str)
 
890
        if (!str) {
 
891
                ret = -1;
 
892
                store_errno = GD_STORE_ENOMEM;
832
893
                goto out;
833
 
        else
 
894
        } else {
834
895
                free_str = str;
 
896
        }
835
897
 
836
898
        iter_key = strtok (str, "=");
837
 
        gf_log ("", GF_LOG_DEBUG, "key %s read", iter_key);
838
 
 
839
 
 
840
899
        iter_val = strtok (NULL, "=");
841
 
        gf_log ("", GF_LOG_DEBUG, "value %s read", iter_val);
842
 
 
843
 
        if (iter_val)
844
 
                *value = gf_strdup (iter_val);
 
900
 
 
901
        ret = glusterd_store_validate_key_value (iter->filepath, iter_key,
 
902
                                                 iter_val, &store_errno);
 
903
        if (ret)
 
904
                goto out;
 
905
 
 
906
        *value = gf_strdup (iter_val);
 
907
 
845
908
        *key   = gf_strdup (iter_key);
846
 
 
847
 
        if (!iter_key || !iter_val)
 
909
        if (!iter_key || !iter_val) {
 
910
                ret = -1;
 
911
                store_errno = GD_STORE_ENOMEM;
848
912
                goto out;
 
913
        }
849
914
 
850
915
        ret = 0;
851
916
 
852
917
out:
 
918
        if (ret) {
 
919
                if (*key) {
 
920
                        GF_FREE (*key);
 
921
                        *key = NULL;
 
922
                }
 
923
                if (*value) {
 
924
                        GF_FREE (*value);
 
925
                        *value = NULL;
 
926
                }
 
927
        }
853
928
        if (free_str)
854
929
                GF_FREE (free_str);
 
930
        if (op_errno)
 
931
                *op_errno = store_errno;
855
932
 
856
933
        gf_log ("", GF_LOG_DEBUG, "Returning with %d", ret);
857
934
        return ret;
865
942
        char    *tmp_key = NULL;
866
943
        char    *tmp_value = NULL;
867
944
 
868
 
        ret = glusterd_store_iter_get_next (iter, &tmp_key, &tmp_value);
 
945
        ret = glusterd_store_iter_get_next (iter, &tmp_key, &tmp_value,
 
946
                                            NULL);
869
947
        while (!ret) {
870
948
                if (!strncmp (key, tmp_key, strlen (key))){
871
 
                        *value = tmp_value; 
 
949
                        *value = tmp_value;
872
950
                        GF_FREE (tmp_key);
873
951
                        goto out;
874
952
                }
875
953
                GF_FREE (tmp_key);
876
954
                GF_FREE (tmp_value);
877
 
                ret = glusterd_store_iter_get_next (iter, &tmp_key, 
878
 
                                                    &tmp_value);
 
955
                ret = glusterd_store_iter_get_next (iter, &tmp_key,
 
956
                                                    &tmp_value, NULL);
879
957
        }
880
958
out:
881
959
        return ret;
901
979
        return ret;
902
980
}
903
981
 
 
982
char*
 
983
glusterd_store_strerror (glusterd_store_op_errno_t op_errno)
 
984
{
 
985
        switch (op_errno) {
 
986
        case GD_STORE_SUCCESS:
 
987
                return "Success";
 
988
        case GD_STORE_KEY_NULL:
 
989
                return "Invalid Key";
 
990
        case GD_STORE_VALUE_NULL:
 
991
                return "Invalid Value";
 
992
        case GD_STORE_KEY_VALUE_NULL:
 
993
                return "Invalid Key and Value";
 
994
        case GD_STORE_EOF:
 
995
                return "No data";
 
996
        case GD_STORE_ENOMEM:
 
997
                return "No memory";
 
998
        default:
 
999
                return "Invalid errno";
 
1000
        }
 
1001
        return "Invalid errno";
 
1002
}
 
1003
 
904
1004
int32_t
905
1005
glusterd_store_retrieve_bricks (glusterd_volinfo_t *volinfo)
906
1006
{
918
1018
        glusterd_store_iter_t   *tmpiter = NULL;
919
1019
        char                    *tmpvalue = NULL;
920
1020
        struct pmap_registry *pmap = NULL;
 
1021
        glusterd_store_op_errno_t       op_errno = GD_STORE_SUCCESS;
921
1022
 
922
1023
        GF_ASSERT (volinfo);
923
1024
        GF_ASSERT (volinfo->volname);
946
1047
 
947
1048
                tmpvalue = NULL;
948
1049
 
949
 
                ret = glusterd_store_handle_new (path, &brickinfo->shandle);
 
1050
                ret = glusterd_store_handle_retrieve (path, &brickinfo->shandle);
950
1051
 
951
1052
                if (ret)
952
1053
                        goto out;
956
1057
                if (ret)
957
1058
                        goto out;
958
1059
 
959
 
                ret = glusterd_store_iter_get_next (iter, &key, &value);
960
 
 
 
1060
                ret = glusterd_store_iter_get_next (iter, &key, &value,
 
1061
                                                    &op_errno);
 
1062
                if (ret) {
 
1063
                        gf_log ("glusterd", GF_LOG_ERROR, "Unable to iterate "
 
1064
                                "the store for brick: %s, reason: %s", path,
 
1065
                                glusterd_store_strerror (op_errno));
 
1066
                        goto out;
 
1067
                }
961
1068
                while (!ret) {
962
1069
                        if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_HOSTNAME,
963
1070
                                      strlen (GLUSTERD_STORE_KEY_BRICK_HOSTNAME))) {
984
1091
                        key = NULL;
985
1092
                        value = NULL;
986
1093
 
987
 
                        ret = glusterd_store_iter_get_next (iter, &key, &value);
 
1094
                        ret = glusterd_store_iter_get_next (iter, &key, &value,
 
1095
                                                            &op_errno);
988
1096
                }
989
1097
 
 
1098
                if (op_errno != GD_STORE_EOF)
 
1099
                        goto out;
990
1100
                ret = glusterd_store_iter_destroy (iter);
991
1101
 
992
1102
                if (ret)
1018
1128
        glusterd_conf_t         *priv = NULL;
1019
1129
        char                    path[PATH_MAX] = {0,};
1020
1130
        int                     exists = 0;
 
1131
        glusterd_store_op_errno_t op_errno = GD_STORE_SUCCESS;
1021
1132
 
1022
1133
        ret = glusterd_volinfo_new (&volinfo);
1023
1134
 
1032
1143
        snprintf (path, sizeof (path), "%s/%s", volpath,
1033
1144
                  GLUSTERD_VOLUME_INFO_FILE);
1034
1145
 
1035
 
        ret = glusterd_store_handle_new (path, &volinfo->shandle);
 
1146
        ret = glusterd_store_handle_retrieve (path, &volinfo->shandle);
1036
1147
 
1037
1148
        if (ret)
1038
1149
                goto out;
1042
1153
        if (ret)
1043
1154
                goto out;
1044
1155
 
1045
 
        ret = glusterd_store_iter_get_next (iter, &key, &value);
 
1156
        ret = glusterd_store_iter_get_next (iter, &key, &value, &op_errno);
 
1157
        if (ret)
 
1158
                goto out;
1046
1159
 
1047
1160
        while (!ret) {
1048
1161
                if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_TYPE,
1100
1213
                key = NULL;
1101
1214
                value = NULL;
1102
1215
 
1103
 
                ret = glusterd_store_iter_get_next (iter, &key, &value);
 
1216
                ret = glusterd_store_iter_get_next (iter, &key, &value,
 
1217
                                                    &op_errno);
1104
1218
        }
 
1219
        if (op_errno != GD_STORE_EOF)
 
1220
                goto out;
1105
1221
 
1106
1222
        ret = glusterd_store_iter_destroy (iter);
1107
1223
 
1224
1340
        if (ret)
1225
1341
                goto out;
1226
1342
 
1227
 
        uuid_unparse (volinfo->volume_id, buf);
1228
1343
        ret = glusterd_store_save_value (volinfo->shandle,
1229
 
                                        GLUSTERD_STORE_KEY_VOL_ID, buf);
 
1344
                                        GLUSTERD_STORE_KEY_VOL_ID,
 
1345
                                        uuid_utoa (volinfo->volume_id));
1230
1346
        if (ret)
1231
1347
                goto out;
1232
1348
 
1256
1372
        glusterd_conf_t                 *priv = NULL;
1257
1373
        char                            peerdir[PATH_MAX] = {0,};
1258
1374
        char                            filepath[PATH_MAX] = {0,};
1259
 
        char                            str[512] = {0,};
1260
1375
        char                            hostname_path[PATH_MAX] = {0,};
1261
1376
 
1262
1377
 
1280
1395
                       goto out;
1281
1396
                }
1282
1397
        } else {
1283
 
                uuid_unparse (peerinfo->uuid, str);
1284
1398
 
1285
 
                snprintf (filepath, PATH_MAX, "%s/%s", peerdir, str);
 
1399
                snprintf (filepath, PATH_MAX, "%s/%s", peerdir,
 
1400
                          uuid_utoa (peerinfo->uuid));
1286
1401
                snprintf (hostname_path, PATH_MAX, "%s/%s",
1287
1402
                          peerdir, peerinfo->hostname);
1288
1403
 
1313
1428
        glusterd_conf_t                 *priv = NULL;
1314
1429
        char                            peerdir[PATH_MAX] = {0,};
1315
1430
        char                            filepath[PATH_MAX] = {0,};
1316
 
        char                            str[512] = {0,};
1317
1431
        char                            buf[4096] = {0,};
1318
1432
        char                            hostname_path[PATH_MAX] = {0,};
1319
1433
 
1345
1459
                       goto out;
1346
1460
                }
1347
1461
        } else {
1348
 
                uuid_unparse (peerinfo->uuid, str);
1349
1462
 
1350
 
                snprintf (filepath, PATH_MAX, "%s/%s", peerdir, str);
 
1463
                snprintf (filepath, PATH_MAX, "%s/%s", peerdir,
 
1464
                          uuid_utoa (peerinfo->uuid));
1351
1465
                snprintf (hostname_path, PATH_MAX, "%s/%s",
1352
1466
                          peerdir, peerinfo->hostname);
1353
1467
 
1374
1488
        }
1375
1489
 
1376
1490
        ret = glusterd_store_save_value (peerinfo->shandle,
1377
 
                                         GLUSTERD_STORE_KEY_PEER_UUID, str);
 
1491
                                         GLUSTERD_STORE_KEY_PEER_UUID,
 
1492
                                         uuid_utoa (peerinfo->uuid));
1378
1493
        if (ret)
1379
1494
                goto out;
1380
1495
 
1411
1526
        char                    *key = NULL;
1412
1527
        char                    *value = NULL;
1413
1528
        glusterd_peerctx_args_t args = {0};
 
1529
        glusterd_store_op_errno_t op_errno = GD_STORE_SUCCESS;
1414
1530
 
1415
1531
        GF_ASSERT (this);
1416
1532
        priv = this->private;
1432
1548
 
1433
1549
        while (entry) {
1434
1550
                snprintf (filepath, PATH_MAX, "%s/%s", path, entry->d_name);
1435
 
                ret = glusterd_store_handle_new (filepath, &shandle);
 
1551
                ret = glusterd_store_handle_retrieve (filepath, &shandle);
1436
1552
                if (ret)
1437
1553
                        goto out;
1438
1554
 
1440
1556
                if (ret)
1441
1557
                        goto out;
1442
1558
 
1443
 
                ret = glusterd_store_iter_get_next (iter, &key, &value);
1444
 
                if (ret) {
1445
 
                        gf_log (this->name, GF_LOG_ERROR, "key: %p, and value: %p",
1446
 
                                key, value);
 
1559
                ret = glusterd_store_iter_get_next (iter, &key, &value,
 
1560
                                                    &op_errno);
 
1561
                if (ret)
1447
1562
                        goto out;
1448
 
                }
1449
1563
 
1450
1564
                while (!ret) {
1451
1565
 
1471
1585
                        key = NULL;
1472
1586
                        value = NULL;
1473
1587
 
1474
 
                        ret = glusterd_store_iter_get_next (iter, &key, &value);
 
1588
                        ret = glusterd_store_iter_get_next (iter, &key, &value,
 
1589
                                                            &op_errno);
1475
1590
                }
 
1591
                if (op_errno != GD_STORE_EOF)
 
1592
                        goto out;
1476
1593
 
1477
1594
                (void) glusterd_store_iter_destroy (iter);
1478
1595