summaryrefslogtreecommitdiffstats
path: root/xlators/nfs/server/src/exports.c
blob: 342ad503a6a16a309f7e569149b4b778eb1e9a99 (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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
/*
  Copyright 2014-present Facebook. All Rights Reserved

  This file is part of GlusterFS.

   Author :
   Shreyas Siravara <shreyas.siravara@gmail.com>

  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 "exports.h"
#include "hashfn.h"
#include "parse-utils.h"
#include "nfs-messages.h"

static void
_exp_dict_destroy(dict_t *ng_dict);
static void
_export_options_print(const struct export_options *opts);
static void
_export_options_deinit(struct export_options *opts);
static void
_export_dir_deinit(struct export_dir *dir);

static struct parser *netgroup_parser;
static struct parser *hostname_parser;
static struct parser *options_parser;

/**
 * _exp_init_parsers -- Initialize parsers to be used in this file
 *
 * @return: success: 0
 *          failure: -1
 */
static int
_exp_init_parsers()
{
    int ret = -1;

    netgroup_parser = parser_init(NETGROUP_REGEX_PATTERN);
    if (!netgroup_parser)
        goto out;

    hostname_parser = parser_init(HOSTNAME_REGEX_PATTERN);
    if (!hostname_parser)
        goto out;

    options_parser = parser_init(OPTIONS_REGEX_PATTERN);
    if (!options_parser)
        goto out;

    ret = 0;
out:
    return ret;
}

/**
 * _exp_deinit_parsers -- Free parsers used in this file
 */
static void
_exp_deinit_parsers()
{
    parser_deinit(netgroup_parser);
    parser_deinit(hostname_parser);
    parser_deinit(options_parser);
}

/**
 * _export_file_init -- Initialize an exports file structure.
 *
 * @return  : success: Pointer to an allocated exports file struct
 *            failure: NULL
 *
 * Not for external use.
 */
struct exports_file *
_exports_file_init()
{
    struct exports_file *file = NULL;

    file = GF_CALLOC(1, sizeof(*file), gf_common_mt_nfs_exports);
    if (!file) {
        gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to allocate file struct!");
        goto out;
    }

    file->exports_dict = dict_new();
    file->exports_map = dict_new();
    if (!file->exports_dict || !file->exports_map) {
        gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to allocate dict!");
        goto free_and_out;
    }

    goto out;

free_and_out:
    if (file->exports_dict)
        dict_unref(file->exports_dict);

    GF_FREE(file);
    file = NULL;
out:
    return file;
}

/**
 * _exp_file_dict_destroy -- Delete each item in the dict
 *
 * @dict : Dict to free elements from
 * @key  : Key in the dict we are on
 * @val  : Value associated with that dict
 * @tmp  : Not used
 *
 * Not for external use.
 */
static int
_exp_file_dict_destroy(dict_t *dict, char *key, data_t *val, void *tmp)
{
    struct export_dir *dir = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, dict, out);

    if (val) {
        dir = (struct export_dir *)val->data;

        if (dir) {
            _export_dir_deinit(dir);
            val->data = NULL;
        }
        dict_del(dict, key);
    }

out:
    return 0;
}

/**
 * _exp_file_deinit -- Free memory used by an export file
 *
 * @expfile : Pointer to the exports file to free
 *
 * Externally usable.
 */
void
exp_file_deinit(struct exports_file *expfile)
{
    if (!expfile)
        goto out;

    if (expfile->exports_dict) {
        dict_foreach(expfile->exports_dict, _exp_file_dict_destroy, NULL);
        dict_unref(expfile->exports_dict);
    }

    if (expfile->exports_map) {
        dict_foreach(expfile->exports_map, _exp_file_dict_destroy, NULL);
        dict_unref(expfile->exports_map);
    }

    GF_FREE(expfile->filename);
    GF_FREE(expfile);
out:
    return;
}

/**
 * _export_dir_init -- Initialize an export directory structure.
 *
 * @return  : success: Pointer to an allocated exports directory struct
 *            failure: NULL
 *
 * Not for external use.
 */
static struct export_dir *
_export_dir_init()
{
    struct export_dir *expdir = GF_CALLOC(1, sizeof(*expdir),
                                          gf_common_mt_nfs_exports);

    if (!expdir)
        gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to allocate export dir structure!");

    return expdir;
}

/**
 * _export_dir_deinit -- Free memory used by an export dir
 *
 * @expdir : Pointer to the export directory to free
 *
 * Not for external use.
 */
static void
_export_dir_deinit(struct export_dir *dir)
{
    GF_VALIDATE_OR_GOTO(GF_EXP, dir, out);
    GF_FREE(dir->dir_name);
    _exp_dict_destroy(dir->netgroups);
    _exp_dict_destroy(dir->hosts);
    GF_FREE(dir);

out:
    return;
}

/**
 * _export_item_print -- Print the elements in the export item.
 *
 * @expdir : Pointer to the item struct to print out.
 *
 * Not for external use.
 */
static void
_export_item_print(const struct export_item *item)
{
    GF_VALIDATE_OR_GOTO(GF_EXP, item, out);
    printf("%s", item->name);
    _export_options_print(item->opts);
out:
    return;
}

/**
 * _export_item_deinit -- Free memory used by an export item
 *
 * @expdir : Pointer to the export item to free
 *
 * Not for external use.
 */
static void
_export_item_deinit(struct export_item *item)
{
    if (!item)
        return;

    _export_options_deinit(item->opts);
    GF_FREE(item->name);
    GF_FREE(item);
}

/**
 * _export_item_init -- Initialize an export item structure
 *
 * @return  : success: Pointer to an allocated exports item struct
 *            failure: NULL
 *
 * Not for external use.
 */
static struct export_item *
_export_item_init()
{
    struct export_item *item = GF_CALLOC(1, sizeof(*item),
                                         gf_common_mt_nfs_exports);

    if (item) {
        GF_REF_INIT(item, _export_item_deinit);
    } else {
        gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to allocate export item!");
    }

    return item;
}

/**
 * _export_host_init -- Initialize an export options struct
 *
 * @return  : success: Pointer to an allocated options struct
 *            failure: NULL
 *
 * Not for external use.
 */
static struct export_options *
_export_options_init()
{
    struct export_options *opts = GF_CALLOC(1, sizeof(*opts),
                                            gf_common_mt_nfs_exports);

    if (!opts)
        gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to allocate options structure!");

    return opts;
}

/**
 * _export_options_deinit -- Free memory used by a options struct
 *
 * @expdir : Pointer to the options struct to free
 *
 * Not for external use.
 */
static void
_export_options_deinit(struct export_options *opts)
{
    if (!opts)
        return;

    GF_FREE(opts->anon_uid);
    GF_FREE(opts->sec_type);
    GF_FREE(opts);
}

/**
 * _export_options_print -- Print the elements in the options struct.
 *
 * @expdir : Pointer to the options struct to print out.
 *
 * Not for external use.
 */
static void
_export_options_print(const struct export_options *opts)
{
    GF_VALIDATE_OR_GOTO(GF_EXP, opts, out);

    printf("(");
    if (opts->rw)
        printf("rw,");
    else
        printf("ro,");

    if (opts->nosuid)
        printf("nosuid,");

    if (opts->root)
        printf("root,");

    if (opts->anon_uid)
        printf("anonuid=%s,", opts->anon_uid);

    if (opts->sec_type)
        printf("sec=%s,", opts->sec_type);

    printf(") ");
out:
    return;
}

/**
 * __exp_dict_free_walk -- Delete each item in the dict
 *
 * @dict : Dict to free elements from
 * @key  : Key in the dict we are on
 * @val  : Value associated with that dict
 * @tmp  : Not used
 *
 * Passed as a function pointer to dict_foreach()
 *
 * Not for external use.
 */
static int
__exp_dict_free_walk(dict_t *dict, char *key, data_t *val, void *tmp)
{
    if (val) {
        GF_REF_PUT((struct export_item *)val->data);
        val->data = NULL;
        dict_del(dict, key);
    }
    return 0;
}

/**
 * _exp_dict_destroy -- Delete all the items from this dict
 *                               through the helper function above.
 *
 * @ng_dict : Dict to free
 *
 * Not for external use.
 */
static void
_exp_dict_destroy(dict_t *ng_dict)
{
    if (!ng_dict)
        goto out;

    dict_foreach(ng_dict, __exp_dict_free_walk, NULL);
out:
    return;
}

/**
 * exp_file_dir_from_uuid -- Using a uuid as the key, retrieve an exports
 *                           directory from the file.
 *
 * @file: File to retrieve data from
 * @export_uuid: UUID of the export (mountid in the NFS xlator)
 *
 * @return : success: Pointer to an export dir struct
 *           failure: NULL
 */
struct export_dir *
exp_file_dir_from_uuid(const struct exports_file *file,
                       const uuid_t export_uuid)
{
    char export_uuid_str[512] = {
        0,
    };
    data_t *dirdata = NULL;
    struct export_dir *dir = NULL;

    gf_uuid_unparse(export_uuid, export_uuid_str);

    dirdata = dict_get(file->exports_map, export_uuid_str);
    if (dirdata)
        dir = (struct export_dir *)dirdata->data;

    return dir;
}

/**
 * _exp_file_insert -- Insert the exports directory into the file structure
 *                     using the directory as a dict. Also hashes the dirname,
 *                     stores it in a uuid type, converts the uuid type to a
 *                     string and uses that as the key to the exports map.
 *                     The exports map maps an export "uuid" to an export
 *                     directory struct.
 *
 * @file : Exports file struct to insert into
 * @dir  : Export directory to insert
 *
 * Not for external use.
 */
static void
_exp_file_insert(struct exports_file *file, struct export_dir *dir)
{
    data_t *dirdata = NULL;
    uint32_t hashedval = 0;
    uuid_t export_uuid = {
        0,
    };
    char export_uuid_str[512] = {
        0,
    };
    char *dirdup = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, file, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, dir, out);

    dirdata = bin_to_data(dir, sizeof(*dir));
    dict_set(file->exports_dict, dir->dir_name, dirdata);

    dirdup = strdupa(dir->dir_name);
    while (strlen(dirdup) > 0 && dirdup[0] == '/')
        dirdup++;

    hashedval = SuperFastHash(dirdup, strlen(dirdup));
    memset(export_uuid, 0, sizeof(export_uuid));
    memcpy(export_uuid, &hashedval, sizeof(hashedval));
    gf_uuid_unparse(export_uuid, export_uuid_str);

    dict_set(file->exports_map, export_uuid_str, dirdata);
out:
    return;
}

/**
 * __exp_item_print_walk -- Print all the keys and values in the dict
 *
 * @dict : the dict to walk
 * @key  : the key in the dict we are currently on
 * @val  : the value in the dict associated with the key
 * @tmp  : Additional parameter data (not used)
 *
 * Passed as a function pointer to dict_foreach ().
 *
 * Not for external use.
 */
static int
__exp_item_print_walk(dict_t *dict, char *key, data_t *val, void *tmp)
{
    if (val)
        _export_item_print((struct export_item *)val->data);

    return 0;
}

/**
 * __exp_file_print_walk -- Print all the keys and values in the dict
 *
 * @dict : the dict to walk
 * @key  : the key in the dict we are currently on
 * @val  : the value in the dict associated with the key
 * @tmp  : Additional parameter data (not used)
 *
 * Passed as a function pointer to dict_foreach ().
 *
 * Not for external use.
 */
static int
__exp_file_print_walk(dict_t *dict, char *key, data_t *val, void *tmp)
{
    if (val) {
        struct export_dir *dir = (struct export_dir *)val->data;

        printf("%s ", key);

        if (dir->netgroups)
            dict_foreach(dir->netgroups, __exp_item_print_walk, NULL);

        if (dir->hosts)
            dict_foreach(dir->hosts, __exp_item_print_walk, NULL);

        printf("\n");
    }
    return 0;
}

/**
 * exp_file_print --  Print out the contents of the exports file
 *
 * @file : Exports file to print
 *
 * Not for external use.
 */
void
exp_file_print(const struct exports_file *file)
{
    GF_VALIDATE_OR_GOTO(GF_EXP, file, out);
    dict_foreach(file->exports_dict, __exp_file_print_walk, NULL);
out:
    return;
}

#define __exp_line_get_opt_val(val, equals, ret, errlabel)                     \
    do {                                                                       \
        (val) = (equals) + 1;                                                  \
        if (!(*(val))) {                                                       \
            (ret) = 1;                                                         \
            goto errlabel;                                                     \
        }                                                                      \
    } while (0)

enum gf_exp_parse_status {
    GF_EXP_PARSE_SUCCESS = 0,
    GF_EXP_PARSE_ITEM_NOT_FOUND = 1,
    GF_EXP_PARSE_ITEM_FAILURE = 2,
    GF_EXP_PARSE_ITEM_NOT_IN_MOUNT_STATE = 3,
    GF_EXP_PARSE_LINE_IGNORING = 4,
};

/**
 * __exp_line_opt_key_value_parse -- Parse the key-value options in the options
 *                                   string.
 *
 * Given a string like (sec=sys,anonuid=0,rw), to parse, this function
 * will get called once with 'sec=sys' and again with 'anonuid=0'.
 * It will check for the '=', make sure there is data to be read
 * after the '=' and copy the data into the options struct.
 *
 * @option    : An option string like sec=sys or anonuid=0
 * @opts      : Pointer to an struct export_options that holds all the export
 *              options.
 *
 * @return: success: GF_EXP_PARSE_SUCCESS
 *          failure: GF_EXP_PARSE_ITEM_FAILURE on parse failure,
 *                   -EINVAL on bad args, -ENOMEM on allocation errors.
 *
 * Not for external use.
 */
static int
__exp_line_opt_key_value_parse(char *option, struct export_options *opts)
{
    char *equals = NULL;
    char *right = NULL;
    char *strmatch = option;
    int ret = -EINVAL;

    GF_VALIDATE_OR_GOTO(GF_EXP, option, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, opts, out);

    equals = strchr(option, '=');
    if (!equals) {
        ret = GF_EXP_PARSE_ITEM_FAILURE;
        goto out;
    }

    *equals = 0;
    /* Now that an '=' has been found the left side is the option and
     * the right side is the value. We simply have to compare those and
     * extract it.
     */
    if (strcmp(strmatch, "anonuid") == 0) {
        *equals = '=';
        /* Get the value for this option */
        __exp_line_get_opt_val(right, equals, ret, out);
        opts->anon_uid = gf_strdup(right);
        GF_CHECK_ALLOC(opts->anon_uid, ret, out);
    } else if (strcmp(strmatch, "sec") == 0) {
        *equals = '=';
        /* Get the value for this option */
        __exp_line_get_opt_val(right, equals, ret, out);
        opts->sec_type = gf_strdup(right);
        GF_CHECK_ALLOC(opts->sec_type, ret, out);
    } else {
        *equals = '=';
        ret = GF_EXP_PARSE_ITEM_FAILURE;
        goto out;
    }

    ret = GF_EXP_PARSE_SUCCESS;
out:
    return ret;
}

/**
 * __exp_line_opt_parse -- Parse the options part of an
 *                          exports or netgroups string.
 *
 * @opt_str     : The option string to parse
 * @exp_opts    : Double pointer to the options we are going
 *                to allocate and setup.
 *
 *
 * @return: success: GF_EXP_PARSE_SUCCESS
 *          failure: GF_EXP_PARSE_ITEM_FAILURE on parse failure,
 *                   -EINVAL on bad args, -ENOMEM on allocation errors.
 *
 * Not for external use.
 */
static int
__exp_line_opt_parse(const char *opt_str, struct export_options **exp_opts)
{
    struct export_options *opts = NULL;
    char *strmatch = NULL;
    int ret = -EINVAL;
    char *equals = NULL;

    ret = parser_set_string(options_parser, opt_str);
    if (ret < 0)
        goto out;

    while ((strmatch = parser_get_next_match(options_parser))) {
        if (!opts) {
            /* If the options have not been allocated,
             * allocate it.
             */
            opts = _export_options_init();
            if (!opts) {
                ret = -ENOMEM;
                parser_unset_string(options_parser);
                goto out;
            }
        }

        /* First,  check for all the boolean options Second, check for
         * an '=', and check the available options there. The string
         * parsing here gets slightly messy, but the concept itself
         * is pretty simple.
         */
        equals = strchr(strmatch, '=');
        if (strcmp(strmatch, "root") == 0)
            opts->root = _gf_true;
        else if (strcmp(strmatch, "ro") == 0)
            opts->rw = _gf_false;
        else if (strcmp(strmatch, "rw") == 0)
            opts->rw = _gf_true;
        else if (strcmp(strmatch, "nosuid") == 0)
            opts->nosuid = _gf_true;
        else if (equals) {
            ret = __exp_line_opt_key_value_parse(strmatch, opts);
            if (ret < 0) {
                /* This means invalid key value options were
                 * specified, or memory allocation failed.
                 * The ret value gets bubbled up to the caller.
                 */
                GF_FREE(strmatch);
                parser_unset_string(options_parser);
                _export_options_deinit(opts);
                goto out;
            }
        } else
            /* Cannot change to gf_msg.
             * gf_msg not giving output to STDOUT
             * Bug id : BZ1215017
             */
            gf_log(GF_EXP, GF_LOG_WARNING,
                   "Could not find any valid options for "
                   "string: %s",
                   strmatch);
        GF_FREE(strmatch);
    }

    if (!opts) {
        /* If opts is not allocated
         * that means no matches were found
         * which is a parse error. Not marking
         * it as "not found" because it is a parse
         * error to not have options.
         */
        ret = GF_EXP_PARSE_ITEM_FAILURE;
        parser_unset_string(options_parser);
        goto out;
    }

    *exp_opts = opts;
    parser_unset_string(options_parser);
    ret = GF_EXP_PARSE_SUCCESS;
out:
    return ret;
}

/**
 * __exp_line_ng_host_str_parse -- Parse the netgroup or host string
 *
 *      e.g. @mygroup(<options>), parsing @mygroup and (<options>)
 *      or   myhost001.dom(<options>), parsing myhost001.dom and (<options>)
 *
 * @line      : The line to parse
 * @exp_item  : Double pointer to a struct export_item
 *
 * @return: success: GF_PARSE_SUCCESS
 *          failure: GF_EXP_PARSE_ITEM_FAILURE on parse failure,
 *                   -EINVAL on bad args, -ENOMEM on allocation errors.
 *
 * Not for external use.
 */
static int
__exp_line_ng_host_str_parse(char *str, struct export_item **exp_item)
{
    struct export_item *item = NULL;
    int ret = -EINVAL;
    char *parens = NULL;
    char *optstr = NULL;
    struct export_options *exp_opts = NULL;
    char *item_name = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, str, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, exp_item, out);

    /* A netgroup/host string looks like this:
     * @test(sec=sys,rw,anonuid=0) or host(sec=sys,rw,anonuid=0)
     * We want to extract the name, 'test' or 'host'
     * Again, we could setup a regex and use it here,
     * but its simpler to find the '(' and copy until
     * there.
     */
    parens = strchr(str, '(');
    if (!parens) {
        /* Parse error if there are no parens. */
        ret = GF_EXP_PARSE_ITEM_FAILURE;
        goto out;
    }

    *parens = '\0'; /* Temporarily terminate it so we can do a copy */

    if (strlen(str) > FQDN_MAX_LEN) {
        ret = GF_EXP_PARSE_ITEM_FAILURE;
        goto out;
    }

    /* Strip leading whitespaces */
    while (*str == ' ' || *str == '\t')
        str++;

    item_name = gf_strdup(str);
    GF_CHECK_ALLOC(item_name, ret, out);

    gf_msg_trace(GF_EXP, 0, "found hostname/netgroup: %s", item_name);

    /* Initialize an export item for this */
    item = _export_item_init();
    GF_CHECK_ALLOC(item, ret, free_and_out);
    item->name = item_name;

    *parens = '('; /* Restore the string */

    /* Options start at the parentheses */
    optstr = parens;

    ret = __exp_line_opt_parse(optstr, &exp_opts);
    if (ret != 0) {
        /* Bubble up the error to the caller */
        GF_REF_PUT(item);
        goto out;
    }

    item->opts = exp_opts;

    *exp_item = item;

    ret = GF_EXP_PARSE_SUCCESS;
    goto out;

free_and_out:
    GF_FREE(item_name);
out:
    return ret;
}

/**
 * __exp_line_ng_parse -- Extract the netgroups in the line
 *                        and call helper functions to parse
 *                        the string.
 *
 * The call chain goes like this:
 *
 * 1) __exp_line_ng_parse ("/test  @test(sec=sys,rw,anonuid=0)")
 * 2) __exp_line_ng_str_parse ("@test(sec=sys,rw,anonuid=0)");
 * 3) __exp_line_opt_parse("(sec=sys,rw,anonuid=0)");
 *
 *
 * @line    : The line to parse
 * @ng_dict : Double pointer to the dict we want to
 *            insert netgroups into.
 *
 * Allocates the dict, extracts netgroup strings from the line,
 * parses them into a struct export_item structure and inserts
 * them in the dict.
 *
 * @return: success: GF_EXP_PARSE_SUCCESS
 *          failure: GF_EXP_PARSE_ITEM_FAILURE on parse failure,
 *                   GF_EXP_PARSE_ITEM_NOT_FOUND if the netgroup was not found
 *                   -EINVAL on bad args, -ENOMEM on allocation errors.
 *
 * Not for external use.
 */
static int
__exp_line_ng_parse(const char *line, dict_t **ng_dict)
{
    dict_t *netgroups = NULL;
    char *strmatch = NULL;
    int ret = -EINVAL;
    struct export_item *exp_ng = NULL;
    data_t *ngdata = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, line, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, ng_dict, out);

    *ng_dict = NULL; /* Will be set if parse is successful */

    /* Initialize a parser with the line to parse
     * and the regex used to parse it.
     */
    ret = parser_set_string(netgroup_parser, line);
    if (ret < 0) {
        goto out;
    }

    gf_msg_trace(GF_EXP, 0, "parsing line: %s", line);

    while ((strmatch = parser_get_next_match(netgroup_parser))) {
        if (!netgroups) {
            /* Allocate a new dict to store the netgroups. */
            netgroups = dict_new();
            if (!netgroups) {
                ret = -ENOMEM;
                goto free_and_out;
            }
        }

        gf_msg_trace(GF_EXP, 0, "parsing netgroup: %s", strmatch);

        ret = __exp_line_ng_host_str_parse(strmatch, &exp_ng);

        if (ret != 0) {
            /* Parsing or other critical errors.
             * caller will handle return value.
             */
            _exp_dict_destroy(netgroups);
            goto free_and_out;
        }

        ngdata = bin_to_data(exp_ng, sizeof(*exp_ng));
        dict_set(netgroups, exp_ng->name, ngdata);

        /* Free this matched string and continue parsing. */
        GF_FREE(strmatch);
    }

    /* If the netgroups dict was not allocated, then we know that
     * no matches were found.
     */
    if (!netgroups) {
        ret = GF_EXP_PARSE_ITEM_NOT_FOUND;
        parser_unset_string(netgroup_parser);
        goto out;
    }

    ret = GF_EXP_PARSE_SUCCESS;
    *ng_dict = netgroups;

free_and_out:
    parser_unset_string(netgroup_parser);
    GF_FREE(strmatch);
out:
    return ret;
}

/**
 * __exp_line_host_parse -- Extract the hosts in the line
 *                          and call helper functions to parse
 *                          the string.
 *
 * The call chain goes like this:
 *
 * 1) __exp_line_host_parse ("/test  hostip(sec=sys,rw,anonuid=0)")
 * 2) __exp_line_ng_host_str_parse ("hostip(sec=sys,rw,anonuid=0)");
 * 3) __exp_line_opt_parse("(sec=sys,rw,anonuid=0)");
 *
 *
 * @line    : The line to parse
 * @ng_dict : Double pointer to the dict we want to
 *            insert hosts into.
 *
 * Allocates the dict, extracts host strings from the line,
 * parses them into a struct export_item structure and inserts
 * them in the dict.
 *
 * @return: success: GF_EXP_PARSE_SUCCESS
 *          failure: GF_EXP_PARSE_ITEM_FAILURE on parse failure,
 *                   GF_EXP_PARSE_ITEM_NOT_FOUND if the host was not found,
 *                   -EINVAL on bad args, -ENOMEM on allocation errors.
 *
 * Not for external use.
 */
static int
__exp_line_host_parse(const char *line, dict_t **host_dict)
{
    dict_t *hosts = NULL;
    char *strmatch = NULL;
    int ret = -EINVAL;
    struct export_item *exp_host = NULL;
    data_t *hostdata = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, line, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, host_dict, out);

    *host_dict = NULL;

    /* Initialize a parser with the line to parse and the regex used to
     * parse it.
     */
    ret = parser_set_string(hostname_parser, line);
    if (ret < 0) {
        goto out;
    }

    gf_msg_trace(GF_EXP, 0, "parsing line: %s", line);

    while ((strmatch = parser_get_next_match(hostname_parser))) {
        if (!hosts) {
            /* Allocate a new dictto store the netgroups. */
            hosts = dict_new();
            GF_CHECK_ALLOC(hosts, ret, free_and_out);
        }

        gf_msg_trace(GF_EXP, 0, "parsing hostname: %s", strmatch);

        ret = __exp_line_ng_host_str_parse(strmatch, &exp_host);

        if (ret != 0) {
            /* Parsing or other critical error, free allocated
             * memory and exit. The caller will handle the errors.
             */
            _exp_dict_destroy(hosts);
            goto free_and_out;
        }

        /* Insert export item structure into the hosts dict. */
        hostdata = bin_to_data(exp_host, sizeof(*exp_host));
        dict_set(hosts, exp_host->name, hostdata);

        /* Free this matched string and continue parsing.*/
        GF_FREE(strmatch);
    }

    /* If the hosts dict was not allocated, then we know that
     * no matches were found.
     */
    if (!exp_host) {
        ret = GF_EXP_PARSE_ITEM_NOT_FOUND;
        parser_unset_string(hostname_parser);
        goto out;
    }

    ret = GF_EXP_PARSE_SUCCESS;
    *host_dict = hosts;

free_and_out:
    parser_unset_string(hostname_parser);
    GF_FREE(strmatch);
out:
    return ret;
}

/**
 * __exp_line_dir_parse -- Extract directory name from a line in the exports
 *                         file.
 *
 * @line    : The line to parse
 * @dirname : Double pointer to the string we need to hold the directory name.
 *            If the parsing failed, the string will point to NULL, otherwise
 *            it will point to a valid memory region that is allocated by
 *            this function.
 * @check_ms: If this variable is set then we cross check the directory line
 *            with what's in gluster's vol files and reject them if they don't
 *            match.
 *
 * @return : success: GF_EXP_PARSE_SUCCESS
 *           failure: GF_EXP_PARSE_ITEM_FAILURE on parse failure,
 *           -EINVAL on bad arguments, -ENOMEM on allocation failures,
 *           GF_EXP_PARSE_ITEM_NOT_IN_MOUNT_STATE if we failed to match
 *           with gluster's mountstate.
 *
 * The caller is responsible for freeing memory allocated by this function
 *
 * Not for external use.
 */
static int
__exp_line_dir_parse(const char *line, char **dirname, struct mount3_state *ms)
{
    char *dir = NULL;
    char *delim = NULL;
    int ret = -EINVAL;
    char *linedup = NULL;
    struct mnt3_export *mnt3export = NULL;
    size_t dirlen = 0;

    GF_VALIDATE_OR_GOTO(GF_EXP, line, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, dirname, out);

    /* Duplicate the line because we don't
     * want to modify the original string.
     */
    linedup = strdupa(line);

    /* We use strtok_r () here to split the string by space/tab and get the
     * the result. We only need the first result of the split.
     * a simple task. It is worth noting that dirnames always have to be
     * validated against gluster's vol files so if they don't
     * match it will be rejected.
     */
    dir = linedup;
    delim = linedup + strcspn(linedup, " \t");
    *delim = 0;

    if (ms) {
        /* Match the directory name with an existing
         * export in the mount state.
         */
        mnt3export = mnt3_mntpath_to_export(ms, dir, _gf_true);
        if (!mnt3export) {
            gf_msg_debug(GF_EXP, 0,
                         "%s not in mount state, "
                         "ignoring!",
                         dir);
            ret = GF_EXP_PARSE_ITEM_NOT_IN_MOUNT_STATE;
            goto out;
        }
    }

    /* Directories can be 1024 bytes in length, check
     * that the argument provided adheres to
     * that restriction.
     */
    if (strlen(dir) > DIR_MAX_LEN) {
        ret = -EINVAL;
        goto out;
    }

    /* Copy the result of the split */
    dir = gf_strdup(dir);
    GF_CHECK_ALLOC(dir, ret, out);

    /* Ensure that trailing slashes are stripped before storing the name */
    dirlen = strlen(dir);
    if (dirlen > 0 && dir[dirlen - 1] == '/')
        dir[dirlen - 1] = '\0';

    /* Set the argument to point to the allocated string */
    *dirname = dir;
    ret = GF_EXP_PARSE_SUCCESS;
out:
    return ret;
}

/**
 * _exp_line_parse -- Parse a line in an exports file into a structure
 *                    that holds all the parts of the line. An exports
 *                    structure has a dict of netgroups and a dict of hosts.
 *
 * An export line looks something like this /test  @test(sec=sys,rw,anonuid=0)
 * or /test  @test(sec=sys,rw,anonuid=0) hostA(sec=sys,rw,anonuid=0), etc.
 *
 * We use regexes to parse the line into three separate pieces:
 * 1) The directory (exports.h -- DIRECTORY_REGEX_PATTERN)
 * 2) The netgroup if it exists (exports.h -- NETGROUP_REGEX_PATTERN)
 * 3) The host if it exists (exports.h -- HOST_REGEX_PATTERN)
 *
 * In this case, the netgroup would be @test(sec=sys,rw,anonuid=0)
 * and the host would be hostA(sec=sys,rw,anonuid=0).
 *
 * @line        : The line to parse
 * @dir         : Double pointer to the struct we need to parse the line into.
 *                If the parsing failed, the struct will point to NULL,
 *                otherwise it will point to a valid memory region that is
 *                allocated by this function.
 * @parse_full  : This parameter tells us whether we should parse all the lines
 *                in the file, even if they are not present in gluster's config.
 *                The gluster config holds the volumes that it exports so
 *                if parse_full is set to FALSE then we will ensure that
 *                the export file structure holds only those volumes
 *                that gluster has exported. It is important to note that
 *                If gluster exports a volume named '/test', '/test' and all
 *                of its subdirectories that may be in the exports file
 *                are valid exports.
 *  @ms         : The mount state that holds the list of volumes that gluster
 *                currently exports.
 *
 * @return : success: GF_EXP_PARSE_SUCCESS on success, -EINVAL on bad arguments,
 *                    -ENOMEM on memory allocation errors,
 *                    GF_EXP_PARSE_LINE_IGNORING if we ignored the line,
 *                    GF_EXP_PARSE_ITEM_FAILURE if there was error parsing
 *           failure: NULL
 *
 * The caller is responsible for freeing memory allocated by this function
 * The caller should free this memory using the _exp_dir_deinit () function.
 *
 * Not for external use.
 */
static int
_exp_line_parse(const char *line, struct export_dir **dir,
                gf_boolean_t parse_full, struct mount3_state *ms)
{
    struct export_dir *expdir = NULL;
    char *dirstr = NULL;
    dict_t *netgroups = NULL;
    dict_t *hosts = NULL;
    int ret = -EINVAL;
    gf_boolean_t netgroups_failed = _gf_false;

    GF_VALIDATE_OR_GOTO(GF_EXP, line, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, dir, out);

    if (*line == '#' || *line == ' ' || *line == '\t' || *line == '\0' ||
        *line == '\n') {
        ret = GF_EXP_PARSE_LINE_IGNORING;
        goto out;
    }

    expdir = _export_dir_init();
    if (!expdir) {
        *dir = NULL;
        ret = -ENOMEM;
        goto out;
    }

    /* Get the directory string from the line */
    ret = __exp_line_dir_parse(line, &dirstr, ms);
    if (ret < 0) {
        gf_msg(GF_EXP, GF_LOG_ERROR, 0, NFS_MSG_PARSE_DIR_FAIL,
               "Parsing directory failed: %s", strerror(-ret));
        /* If parsing the directory failed,
         * we should simply fail because there's
         * nothing else we can extract from the string to make
         * the data valuable.
         */
        goto free_and_out;
    }

    /* Set the dir str */
    expdir->dir_name = dirstr;

    /* Parse the netgroup part of the string */
    ret = __exp_line_ng_parse(line, &netgroups);
    if (ret < 0) {
        gf_msg(GF_EXP, GF_LOG_ERROR, -ret, NFS_MSG_PARSE_FAIL,
               "Critical error: %s", strerror(-ret));
        /* Return values less than 0
         * indicate critical failures (null parameters,
         * failure to allocate memory, etc).
         */
        goto free_and_out;
    }
    if (ret != 0) {
        if (ret == GF_EXP_PARSE_ITEM_FAILURE)
            /* Cannot change to gf_msg.
             * gf_msg not giving output to STDOUT
             * Bug id : BZ1215017
             */
            gf_log(GF_EXP, GF_LOG_WARNING, "Error parsing netgroups for: %s",
                   line);
        /* Even though parsing failed for the netgroups we should let
         * host parsing proceed.
         */
        netgroups_failed = _gf_true;
    }

    /* Parse the host part of the string */
    ret = __exp_line_host_parse(line, &hosts);
    if (ret < 0) {
        gf_msg(GF_EXP, GF_LOG_ERROR, -ret, NFS_MSG_PARSE_FAIL,
               "Critical error: %s", strerror(-ret));
        goto free_and_out;
    }
    if (ret != 0) {
        if (ret == GF_EXP_PARSE_ITEM_FAILURE)
            gf_msg(GF_EXP, GF_LOG_WARNING, 0, NFS_MSG_PARSE_FAIL,
                   "Error parsing hosts for: %s", line);
        /* If netgroups parsing failed, AND
         * host parsing failed, then there's something really
         * wrong with this line, so we're just going to
         * log it and fail out.
         */
        if (netgroups_failed)
            goto free_and_out;
    }

    expdir->hosts = hosts;
    expdir->netgroups = netgroups;
    *dir = expdir;
    goto out;

free_and_out:
    _export_dir_deinit(expdir);
out:
    return ret;
}

struct export_item *
exp_dir_get_netgroup(const struct export_dir *expdir, const char *netgroup)
{
    struct export_item *lookup_res = NULL;
    data_t *dict_res = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, expdir, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, netgroup, out);

    if (!expdir->netgroups)
        goto out;

    dict_res = dict_get(expdir->netgroups, (char *)netgroup);
    if (!dict_res) {
        gf_msg_debug(GF_EXP, 0, "%s not found for %s", netgroup,
                     expdir->dir_name);
        goto out;
    }

    lookup_res = (struct export_item *)dict_res->data;
out:
    return lookup_res;
}
/**
 * exp_dir_get_host -- Given a host string and an exports directory structure,
 *                     find and return an struct export_item structure that
 *                     represents the requested host.
 *
 * @expdir: Export directory to lookup from
 * @host  : Host string to lookup
 *
 * @return: success: Pointer to a export item structure
 *          failure: NULL
 */
struct export_item *
exp_dir_get_host(const struct export_dir *expdir, const char *host)
{
    struct export_item *lookup_res = NULL;
    data_t *dict_res = NULL;

    GF_VALIDATE_OR_GOTO(GF_EXP, expdir, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, host, out);

    if (!expdir->hosts)
        goto out;

    dict_res = dict_get(expdir->hosts, (char *)host);
    if (!dict_res) {
        gf_msg_debug(GF_EXP, 0, "%s not found for %s", host, expdir->dir_name);

        /* Check if wildcards are allowed for the host */
        dict_res = dict_get(expdir->hosts, "*");
        if (!dict_res) {
            goto out;
        }
    }

    lookup_res = (struct export_item *)dict_res->data;
out:
    return lookup_res;
}

/**
 * exp_file_get_dir -- Return an export dir given a directory name
 *                     Does a lookup from the dict in the file structure.
 *
 * @file : Exports file structure to lookup from
 * @dir  : Directory name to lookup
 *
 * @return : success: Pointer to an export directory structure
 *           failure: NULL
 */
struct export_dir *
exp_file_get_dir(const struct exports_file *file, const char *dir)
{
    struct export_dir *lookup_res = NULL;
    data_t *dict_res = NULL;
    char *dirdup = NULL;
    size_t dirlen = 0;

    GF_VALIDATE_OR_GOTO(GF_EXP, file, out);
    GF_VALIDATE_OR_GOTO(GF_EXP, dir, out);

    dirlen = strlen(dir);
    if (dirlen <= 0)
        goto out;

    dirdup = (char *)dir; /* Point at the directory */

    /* If directories don't contain a leading slash */
    if (*dir != '/') {
        dirdup = alloca(dirlen + 2); /* Leading slash & null byte */
        snprintf(dirdup, dirlen + 2, "/%s", dir);
    }

    dict_res = dict_get(file->exports_dict, dirdup);
    if (!dict_res) {
        gf_msg_debug(GF_EXP, 0, "%s not found in %s", dirdup, file->filename);
        goto out;
    }

    lookup_res = (struct export_dir *)dict_res->data;
out:
    return lookup_res;
}

/**
 * exp_file_parse -- Parse an exports file into a structure
 *                   that can be looked up through simple
 *                   function calls.
 *
 * @filepath: Path to the exports file
 * @ms      : Current mount state (useful to match with gluster vol files)
 *
 * @return  : success: 0
 *            failure: -1 on parsing failure, -EINVAL on bad arguments,
 *                     -ENOMEM on allocation failures.
 *
 * The caller is responsible for freeing memory allocated by this function.
 * The caller should free this memory using the exp_file_deinit () function.
 * Calling GF_FREE ( ) on the pointer will NOT free all the allocated memory.
 *
 * Externally usable.
 */
int
exp_file_parse(const char *filepath, struct exports_file **expfile,
               struct mount3_state *ms)
{
    FILE *fp = NULL;
    struct exports_file *file = NULL;
    size_t len = 0;
    int ret = -EINVAL;
    unsigned long line_number = 0;
    char *line = NULL;
    struct export_dir *expdir = NULL;

    /* Sets whether we we should parse the entire file or just that which
     * is present in the mount state */
    gf_boolean_t parse_complete_file = _gf_false;

    GF_VALIDATE_OR_GOTO(GF_EXP, expfile, parse_done);

    if (!ms) {
        /* If mount state is null that means that we
         * should go through and parse the whole file
         * since we don't have anything to compare against.
         */
        parse_complete_file = _gf_true;
    }

    fp = fopen(filepath, "r");
    if (!fp) {
        ret = -errno;
        goto parse_done;
    }

    ret = _exp_init_parsers();
    if (ret < 0)
        goto parse_done;

    /* Process the file line by line, with each line being parsed into
     * an struct export_dir struct. If 'parse_complete_file' is set to TRUE
     * then
     */
    while (getline(&line, &len, fp) != -1) {
        line_number++;      /* Keeping track of line number allows us to
                             * to log which line numbers were wrong
                             */
        strtok(line, "\n"); /* removes the newline character from
                             * the line
                             */

        /* Parse the line from the file into an struct export_dir
         * structure. The process is as follows:
         * Given a line like :
         * "/vol @test(sec=sys,rw,anonuid=0) 10.35.11.31(sec=sys,rw)"
         *
         * This function will allocate an export dir and set its name
         * to '/vol', using the function _exp_line_dir_parse ().
         *
         * Then it will extract the netgroups from the line, in this
         * case it would be '@test(sec=sys,rw,anonuid=0)', and set the
         * item structure's name to '@test'.
         * It will also extract the options from that string and parse
         * them into an struct export_options which will be pointed
         * to by the item structure. This will be put into a dict
         * which will be pointed to by the export directory structure.
         *
         * The same process happens above for the host string
         * '10.35.11.32(sec=sys,rw)'
         */
        ret = _exp_line_parse(line, &expdir, parse_complete_file, ms);
        if (ret == -ENOMEM) {
            /* If we get memory allocation errors, we really should
             * not continue parsing, so just free the allocated
             * memory and exit.
             */
            goto free_and_done;
        }

        if (ret < 0) {
            gf_msg(GF_EXP, GF_LOG_ERROR, -ret, NFS_MSG_PARSE_FAIL,
                   "Failed to parse line #%lu", line_number);
            continue; /* Skip entering this line and continue */
        }

        if (ret == GF_EXP_PARSE_LINE_IGNORING) {
            /* This just means the line was empty or started with a
             * '#' or a ' ' and we are ignoring it.
             */
            gf_msg_debug(GF_EXP, 0,
                         "Ignoring line #%lu because it started "
                         "with a %c",
                         line_number, *line);
            continue;
        }

        if (!file) {
            file = _exports_file_init();
            GF_CHECK_ALLOC_AND_LOG(GF_EXP, file, ret,
                                   "Allocation error while "
                                   "allocating file struct",
                                   parse_done);

            file->filename = gf_strdup(filepath);
            GF_CHECK_ALLOC_AND_LOG(GF_EXP, file, ret,
                                   "Allocation error while "
                                   "duping filepath",
                                   free_and_done);
        }

        /* If the parsing is successful store the export directory
         * in the file structure.
         */
        _exp_file_insert(file, expdir);
    }

    /* line got allocated through getline(), don't use GF_FREE() for it */
    free(line);

    *expfile = file;
    goto parse_done;

free_and_done:
    exp_file_deinit(file);

parse_done:
    if (fp)
        fclose(fp);
    _exp_deinit_parsers();
    return ret;
}