summaryrefslogtreecommitdiff
path: root/src/config.cc
blob: e07cc0fcf9c2c797927880023e5de563935f7ef7 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */


#include "ceph_ver.h"
#include "config.h"
#include "include/types.h"

#include "common/Clock.h"
#include "common/Logger.h"
#include "common/BackTrace.h"

#include <fstream>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

// for tstring stringtable
#include "include/tstring.h"
stringtable g_stab;

#include "include/atomic.h"
#include "include/str_list.h"

#include "osd/osd_types.h"

#include "common/ConfUtils.h"
#include "common/dyn_snprintf.h"

#include "auth/ExportControl.h"
#include "auth/Auth.h"

static bool show_config = false;

static ConfFile *cf = NULL;
static ExportControl *ec = NULL;

static void fini_g_conf();

const char *g_default_id = "admin";

void ceph_set_default_id(const char *id)
{
  g_default_id = strdup(id);
}

class ConfFileDestructor
{
public:
  ConfFileDestructor() {}
  ~ConfFileDestructor() {
    if (cf) {
      delete cf;
      cf = NULL;
      fini_g_conf();
    }
  }
};

static ConfFileDestructor cfd;


atomic_t _num_threads(0);

// file layouts
struct ceph_file_layout g_default_file_layout = {
 fl_stripe_unit: init_le32(1<<22),
 fl_stripe_count: init_le32(1),
 fl_object_size: init_le32(1<<22),
 fl_cas_hash: init_le32(0),
 fl_object_stripe_unit: init_le32(0),
};



#include <msg/msg_types.h>

// fake osd failures: osd -> time
std::map<entity_name_t,float> g_fake_kill_after;


md_config_t g_conf;
bool g_daemon = false;


#include <stdlib.h>
#include <string.h>

void env_to_vec(std::vector<const char*>& args) 
{
  char *p = getenv("CEPH_ARGS");
  if (!p) return;
  
  static char buf[1000];  
  int len = MIN(strlen(p), sizeof(buf)-1);  // bleh.
  memcpy(buf, p, len);
  buf[len] = 0;
  //cout << "CEPH_ARGS='" << p << ";" << endl;

  p = buf;
  while (*p && p < buf + len) {
    char *e = p;
    while (*e && *e != ' ')
      e++;
    *e = 0;
    args.push_back(p);
    //cout << "arg " << p << std::endl;
    p = e+1;
  }
}

void env_to_deq(std::deque<const char*>& args) 
{
  char *p = getenv("CEPH_ARGS");
  if (!p) return;
  
  static char buf[1000];  
  int len = MIN(strlen(p), sizeof(buf)-1);  // bleh.
  memcpy(buf, p, len);
  buf[len] = 0;

  p = buf;
  while (*p && p < buf + len) {
    char *e = p;
    while (*e && *e != ' ')
      e++;
    *e = 0;
    args.push_back(p);
    p = e+1;
  }
}

void argv_to_vec(int argc, const char **argv,
                 std::vector<const char*>& args)
{
  for (int i=1; i<argc; i++)
    args.push_back(argv[i]);
}

void argv_to_deq(int argc, const char **argv,
                 std::deque<const char*>& args)
{
  for (int i=1; i<argc; i++)
    args.push_back(argv[i]);
}

void vec_to_argv(std::vector<const char*>& args,
                 int& argc, const char **&argv)
{
  const char *myname = "asdf";
  if (argc && argv)
    myname = argv[0];
  argv = (const char**)malloc(sizeof(char*) * argc);
  argc = 1;
  argv[0] = myname;

  for (unsigned i=0; i<args.size(); i++) 
    argv[argc++] = args[i];
}


bool parse_ip_port_vec(const char *s, vector<entity_addr_t>& vec)
{
  const char *p = s;
  const char *end = p + strlen(p);
  while (p < end) {
    entity_addr_t a;
    //cout << " parse at '" << p << "'" << std::endl;
    if (!a.parse(p, &p)) {
      //dout(0) << " failed to parse address '" << p << "'" << dendl;
      return false;
    }
    //cout << " got " << a << ", rest is '" << p << "'" << std::endl;
    vec.push_back(a);
    while (*p == ',' || *p == ' ')
      p++;
  }
  return true;
}




void parse_config_option_string(string& s)
{
  char b[s.length()+1];
  strcpy(b, s.c_str());
  vector<const char*> nargs;
  char *p = b;
  while (*p) {
    nargs.push_back(p);
    while (*p && *p != ' ') p++;
    if (!*p)
      break;
    *p++ = 0;
    while (*p && *p == ' ') p++;
  }
  parse_config_options(nargs);
}

typedef void (*signal_handler_t)(int);

static void install_sighandler(int signum, signal_handler_t handler, int flags)
{
  int ret;
  struct sigaction oldact;
  struct sigaction act;
  memset(&act, 0, sizeof(act));

  act.sa_handler = handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = flags;

  ret = sigaction(signum, &act, &oldact);
  if (ret != 0) {
    fprintf(stderr, "failed to install signal handler for signal %d\n", signum);
    exit(1);
  }
}

void sighup_handler(int signum)
{
  _dout_need_open = true;
  logger_reopen_all();
}

void sigsegv_handler(int signum)
{
  *_dout << "*** Caught signal (SEGV) ***" << std::endl;
  BackTrace bt(0);
  bt.print(*_dout);
  _dout->flush();

  // Use default handler to dump core
  kill(getpid(), signum);
}

void sigabrt_handler(int signum)
{
  *_dout << "*** Caught signal (ABRT) ***" << std::endl;
  BackTrace bt(0);
  bt.print(*_dout);
  _dout->flush();

  // Use default handler to dump core
  kill(getpid(), signum);
}

#define _STR(x) #x
#define STRINGIFY(x) _STR(x)

struct config_option {
  const char *section;
  const char *conf_name;
  const char *name;
  void *val_ptr;
  
  const char *def_str;
  long long def_longlong;
  double def_double;

  opt_type_t type;
  char char_option;  // if any
};

#define OPTION_OPT_STR(section, name, schar, type, def_val) \
       { STRINGIFY(section), NULL, STRINGIFY(name), \
	   &g_conf.name, def_val, 0, 0, type, schar }

#define OPTION_OPT_ADDR(section, name, schar, type, def_val) \
       { STRINGIFY(section), NULL, STRINGIFY(name), \
	   &g_conf.name, def_val, 0, 0, type, schar }

#define OPTION_OPT_LONGLONG(section, name, schar, type, def_val) \
       { STRINGIFY(section), NULL, STRINGIFY(name), \
	   &g_conf.name, 0, def_val, 0, type, schar }
#define OPTION_OPT_INT OPTION_OPT_LONGLONG
#define OPTION_OPT_BOOL OPTION_OPT_INT

#define OPTION_OPT_DOUBLE(section, name, schar, type, def_val) \
       { STRINGIFY(section), NULL, STRINGIFY(name), \
	   &g_conf.name, 0, 0, def_val, type, schar }
#define OPTION_OPT_FLOAT OPTION_OPT_DOUBLE

#define OPTION(name, schar, type, def_val) OPTION_##type("global", name, schar, type, def_val)

#define OPTION_ALT(section, conf_name, name, schar, type, def_val) \
       { STRINGIFY(section), NULL, STRINGIFY(conf_name), \
         &g_conf.name, STRINGIFY(def_val), type, schar }

static struct config_option config_optionsp[] = {
	OPTION(host, 0, OPT_STR, "localhost"),
	OPTION(public_addr, 0, OPT_ADDR, ""),
	OPTION(cluster_addr, 0, OPT_ADDR, ""),
	OPTION(num_mon, 0, OPT_INT, 1),
	OPTION(num_mds, 0, OPT_INT, 1),
	OPTION(num_osd, 0, OPT_INT, 4),
	OPTION(num_client, 0, OPT_INT, 1),
	OPTION(monmap, 'M', OPT_STR, 0),
	OPTION(mon_host, 'm', OPT_STR, 0),
	OPTION(daemonize, 'd', OPT_BOOL, false),
        OPTION(tcmalloc_profiler_run, 0, OPT_BOOL, false),
        OPTION(profiler_allocation_interval, 0, OPT_INT, 1073741824),
        OPTION(profiler_highwater_interval, 0, OPT_INT, 104857600),
	OPTION(logger, 0, OPT_BOOL, false),
	OPTION(logger_interval, 0, OPT_INT, 1),
	OPTION(logger_calc_variance, 0, OPT_BOOL, true),
	OPTION(logger_subdir, 0, OPT_STR, 0),
	OPTION(logger_dir, 0, OPT_STR, "/var/log/ceph/stat"),
	OPTION(log_file, 0, OPT_STR, 0),
	OPTION(log_dir, 0, OPT_STR, "/var/log/ceph"),
	OPTION(log_sym_dir, 0, OPT_STR, 0),
	OPTION(log_sym_history, 0, OPT_INT, 10),
	OPTION(log_to_stdout, 0, OPT_BOOL, true),
	OPTION(log_to_syslog, 0, OPT_BOOL, false),
	OPTION(log_per_instance, 0, OPT_BOOL, false),
	OPTION(log_to_file, 0, OPT_BOOL, true),
	OPTION(clog_to_monitors, 0, OPT_BOOL, true),
	OPTION(clog_to_syslog, 0, OPT_BOOL, false),
	OPTION(pid_file, 0, OPT_STR, "/var/run/ceph/$type.$id.pid"),
	OPTION(conf, 'c', OPT_STR, "/etc/ceph/ceph.conf, ~/.ceph/config, ceph.conf"),
	OPTION(chdir, 0, OPT_STR, "/"),
	OPTION(fake_clock, 0, OPT_BOOL, false),
	OPTION(fakemessenger_serialize, 0, OPT_BOOL, true),
	OPTION(kill_after, 0, OPT_INT, 0),
	OPTION(debug, 0, OPT_INT, 0),
	OPTION(debug_lockdep, 0, OPT_INT, 0),
	OPTION(debug_mds, 0, OPT_INT, 1),
	OPTION(debug_mds_balancer, 0, OPT_INT, 1),
	OPTION(debug_mds_log, 0, OPT_INT, 1),
	OPTION(debug_mds_log_expire, 0, OPT_INT, 1),
	OPTION(debug_mds_migrator, 0, OPT_INT, 1),
	OPTION(debug_buffer, 0, OPT_INT, 0),
	OPTION(debug_timer, 0, OPT_INT, 0),
	OPTION(debug_filer, 0, OPT_INT, 0),
	OPTION(debug_objecter, 0, OPT_INT, 0),
	OPTION(debug_rados, 0, OPT_INT, 0),
	OPTION(debug_journaler, 0, OPT_INT, 0),
	OPTION(debug_objectcacher, 0, OPT_INT, 0),
	OPTION(debug_client, 0, OPT_INT, 0),
	OPTION(debug_osd, 0, OPT_INT, 0),
	OPTION(debug_ebofs, 0, OPT_INT, 1),
	OPTION(debug_filestore, 0, OPT_INT, 1),
	OPTION(debug_journal, 0, OPT_INT, 1),
	OPTION(debug_bdev, 0, OPT_INT, 1),         // block device
	OPTION(debug_ns, 0, OPT_INT, 0),
	OPTION(debug_ms, 0, OPT_INT, 0),
	OPTION(debug_mon, 0, OPT_INT, 1),
	OPTION(debug_monc, 0, OPT_INT, 0),
	OPTION(debug_paxos, 0, OPT_INT, 0),
	OPTION(debug_tp, 0, OPT_INT, 0),
	OPTION(debug_auth, 0, OPT_INT, 1),
	OPTION(debug_finisher, 0, OPT_INT, 1),
	OPTION(key, 0, OPT_STR, ""),
	OPTION(keyfile, 'K', OPT_STR, ""),
	OPTION(keyring, 'k', OPT_STR, "~/.ceph/keyring.bin, /etc/ceph/keyring.bin, .ceph_keyring"),
	OPTION(clock_lock, 0, OPT_BOOL, false),
	OPTION(clock_tare, 0, OPT_BOOL, false),
	OPTION(ms_tcp_nodelay, 0, OPT_BOOL, true),
	OPTION(ms_initial_backoff, 0, OPT_DOUBLE, .2),
	OPTION(ms_max_backoff, 0, OPT_DOUBLE, 15.0),
	OPTION(ms_die_on_failure, 0, OPT_BOOL, false),
	OPTION(ms_nocrc, 0, OPT_BOOL, false),
	OPTION(ms_die_on_bad_msg, 0, OPT_BOOL, false),
	OPTION(ms_dispatch_throttle_bytes, 0, OPT_INT, 100 << 20),
	OPTION(ms_bind_ipv6, 0, OPT_BOOL, false),
        OPTION(ms_rwthread_stack_bytes, 0, OPT_INT, 1024 << 10),
        OPTION(ms_tcp_read_timeout, 0, OPT_LONGLONG, 900),
	OPTION(ms_inject_socket_failures, 0, OPT_LONGLONG, 0),
	OPTION(mon_data, 0, OPT_STR, ""),
	OPTION(mon_tick_interval, 0, OPT_INT, 5),
	OPTION(mon_subscribe_interval, 0, OPT_DOUBLE, 300),
	OPTION(mon_osd_down_out_interval, 0, OPT_INT, 300), // seconds
	OPTION(mon_lease, 0, OPT_FLOAT, 5),  		    // lease interval
	OPTION(mon_lease_renew_interval, 0, OPT_FLOAT, 3), // on leader, to renew the lease
	OPTION(mon_lease_ack_timeout, 0, OPT_FLOAT, 10.0), // on leader, if lease isn't acked by all peons
	OPTION(mon_clock_drift_allowed, 0, OPT_FLOAT, .010), // allowed clock drift between monitors
	OPTION(mon_clock_drift_warn_backoff, 0, OPT_FLOAT, 5), // exponential backoff for clock drift warnings
	OPTION(mon_lease_timeout, 0, OPT_FLOAT, 10.0),     // on peon, if lease isn't extended
	OPTION(mon_accept_timeout, 0, OPT_FLOAT, 10.0),    // on leader, if paxos update isn't accepted
	OPTION(mon_stop_on_last_unmount, 0, OPT_BOOL, false),
	OPTION(mon_stop_with_last_mds, 0, OPT_BOOL, false),
	OPTION(mon_allow_mds_bully, 0, OPT_BOOL, false),   // allow a booting mds to (forcibly) claim an mds # .. FIXME
	OPTION(mon_pg_create_interval, 0, OPT_FLOAT, 30.0), // no more than every 30s
	OPTION(mon_clientid_prealloc, 0, OPT_INT, 100),   // how many clientids to prealloc
	OPTION(mon_globalid_prealloc, 0, OPT_INT, 100),   // how many globalids to prealloc
	OPTION(paxos_propose_interval, 0, OPT_DOUBLE, 1.0),  // gather updates for this long before proposing a map update
	OPTION(paxos_min_wait, 0, OPT_DOUBLE, 0.05),  // min time to gather updates for after period of inactivity
	OPTION(paxos_observer_timeout, 0, OPT_DOUBLE, 5*60), // gather updates for this long before proposing a map update
	OPTION(auth_supported, 0, OPT_STR, "none"),
	OPTION(auth_mon_ticket_ttl, 0, OPT_DOUBLE, 60*60*12),
	OPTION(auth_service_ticket_ttl, 0, OPT_DOUBLE, 60*60),
	OPTION(auth_nonce_len, 0, OPT_INT, 16),
	OPTION(mon_client_hunt_interval, 0, OPT_DOUBLE, 3.0),   // try new mon every N seconds until we connect
	OPTION(mon_client_ping_interval, 0, OPT_DOUBLE, 10.0),  // ping every N seconds
	OPTION(client_cache_size, 0, OPT_INT, 16384),
	OPTION(client_cache_mid, 0, OPT_FLOAT, .75),
	OPTION(client_cache_stat_ttl, 0, OPT_INT, 0), // seconds until cached stat results become invalid
	OPTION(client_cache_readdir_ttl, 0, OPT_INT, 1),  // 1 second only
	OPTION(client_use_random_mds, 0, OPT_BOOL, false),
	OPTION(client_mount_timeout, 0, OPT_DOUBLE, 30.0),
	OPTION(client_unmount_timeout, 0, OPT_DOUBLE, 10.0),
	OPTION(client_tick_interval, 0, OPT_DOUBLE, 1.0),
	OPTION(client_hack_balance_reads, 0, OPT_BOOL, false),
	OPTION(client_trace, 0, OPT_STR, 0),
	OPTION(client_readahead_min, 0, OPT_LONGLONG, 128*1024),  // readahead at _least_ this much.
	OPTION(client_readahead_max_bytes, 0, OPT_LONGLONG, 0),  //8 * 1024*1024,
	OPTION(client_readahead_max_periods, 0, OPT_LONGLONG, 4),  // as multiple of file layout period (object size * num stripes)
	OPTION(client_snapdir, 0, OPT_STR, ".snap"),
	OPTION(client_mountpoint, 'r', OPT_STR, "/"),
	OPTION(fuse_direct_io, 0, OPT_INT, 0),
	OPTION(fuse_ll, 0, OPT_BOOL, true),
	OPTION(client_oc, 0, OPT_BOOL, true),
	OPTION(client_oc_size, 0, OPT_INT, 1024*1024* 200),    // MB * n
	OPTION(client_oc_max_dirty, 0, OPT_INT, 1024*1024* 100),    // MB * n  (dirty OR tx.. bigish)
	OPTION(client_oc_target_dirty, 0, OPT_INT, 1024*1024* 8), // target dirty (keep this smallish)
	// note: the max amount of "in flight" dirty data is roughly (max - target)
	OPTION(client_oc_max_sync_write, 0, OPT_LONGLONG, 128*1024),   // sync writes >= this use wrlock
	OPTION(objecter_buffer_uncommitted, 0, OPT_BOOL, true),  // this must be true for proper failure handling
	OPTION(objecter_map_request_interval, 0, OPT_DOUBLE, 15.0), // request a new map every N seconds, if we have pending io
	OPTION(objecter_tick_interval, 0, OPT_DOUBLE, 5.0),
	OPTION(objecter_mon_retry_interval, 0, OPT_DOUBLE, 5.0),
	OPTION(objecter_timeout, 0, OPT_DOUBLE, 10.0),    // before we ask for a map
	OPTION(objecter_inflight_op_bytes, 0, OPT_LONGLONG, 1024*1024*100), //max in-flight data (both directions)
	OPTION(journaler_allow_split_entries, 0, OPT_BOOL, true),
	OPTION(journaler_safe, 0, OPT_BOOL, true),  // wait for COMMIT on journal writes
	OPTION(journaler_write_head_interval, 0, OPT_INT, 15),
	OPTION(journaler_cache, 0, OPT_BOOL, false), // cache writes for later readback
	OPTION(journaler_prefetch_periods, 0, OPT_INT, 50),   // * journal object size (1~MB? see above)
	OPTION(journaler_batch_interval, 0, OPT_DOUBLE, .001),   // seconds.. max add'l latency we artificially incur
	OPTION(journaler_batch_max, 0, OPT_LONGLONG, 0),  // max bytes we'll delay flushing; disable, for now....
	OPTION(mds_max_file_size, 0, OPT_LONGLONG, 1ULL << 40), 
	OPTION(mds_cache_size, 0, OPT_INT, 100000),
	OPTION(mds_cache_mid, 0, OPT_FLOAT, .7),
	OPTION(mds_mem_max, 0, OPT_INT, 1048576),        // KB
	OPTION(mds_decay_halflife, 0, OPT_FLOAT, 5),
	OPTION(mds_beacon_interval, 0, OPT_FLOAT, 4),
	OPTION(mds_beacon_grace, 0, OPT_FLOAT, 15),
	OPTION(mds_blacklist_interval, 0, OPT_FLOAT, 24.0*60.0),  // how long to blacklist failed nodes
	OPTION(mds_session_timeout, 0, OPT_FLOAT, 60),    // cap bits and leases time out if client idle
	OPTION(mds_session_autoclose, 0, OPT_FLOAT, 300), // autoclose idle session 
	OPTION(mds_client_lease, 0, OPT_FLOAT, 120),      // (assuming session stays alive)
	OPTION(mds_reconnect_timeout, 0, OPT_FLOAT, 45),  // seconds to wait for clients during mds restart
							  //  make it (mds_session_timeout - mds_beacon_grace)
	OPTION(mds_tick_interval, 0, OPT_FLOAT, 5),
	OPTION(mds_dirstat_min_interval, 0, OPT_FLOAT, 1),    // try to avoid propagating more often than this
	OPTION(mds_scatter_nudge_interval, 0, OPT_FLOAT, 5),  // how quickly dirstat changes propagate up the hierarchy
	OPTION(mds_client_prealloc_inos, 0, OPT_INT, 1000),
	OPTION(mds_early_reply, 0, OPT_BOOL, true),
	OPTION(mds_short_reply_trace, 0, OPT_BOOL, true),
	OPTION(mds_use_tmap, 0, OPT_BOOL, true),        // use trivialmap for dir updates
	OPTION(mds_default_dir_hash, 0, OPT_INT, CEPH_STR_HASH_RJENKINS),
	OPTION(mds_log, 0, OPT_BOOL, true),
	OPTION(mds_log_unsafe, 0, OPT_BOOL, false),      // only wait for log sync, when it's mostly safe to do so
	OPTION(mds_log_skip_corrupt_events, 0, OPT_BOOL, false),
	OPTION(mds_log_max_events, 0, OPT_INT, -1),
	OPTION(mds_log_max_segments, 0, OPT_INT, 30),  // segment size defined by FileLayout, above
	OPTION(mds_log_max_expiring, 0, OPT_INT, 20),
	OPTION(mds_log_pad_entry, 0, OPT_INT, 128),
	OPTION(mds_log_eopen_size, 0, OPT_INT, 100),   // # open inodes per log entry
	OPTION(mds_bal_sample_interval, 0, OPT_FLOAT, 3.0),  // every 5 seconds
	OPTION(mds_bal_replicate_threshold, 0, OPT_FLOAT, 8000),
	OPTION(mds_bal_unreplicate_threshold, 0, OPT_FLOAT, 0),
	OPTION(mds_bal_frag, 0, OPT_BOOL, false),
	OPTION(mds_bal_split_size, 0, OPT_INT, 10000),
	OPTION(mds_bal_split_rd, 0, OPT_FLOAT, 25000),
	OPTION(mds_bal_split_wr, 0, OPT_FLOAT, 10000),
	OPTION(mds_bal_split_bits, 0, OPT_INT, 3),
	OPTION(mds_bal_merge_size, 0, OPT_INT, 50),
	OPTION(mds_bal_merge_rd, 0, OPT_FLOAT, 1000),
	OPTION(mds_bal_merge_wr, 0, OPT_FLOAT, 1000),
	OPTION(mds_bal_interval, 0, OPT_INT, 10),           // seconds
	OPTION(mds_bal_fragment_interval, 0, OPT_INT, 5),      // seconds
	OPTION(mds_bal_idle_threshold, 0, OPT_FLOAT, 0),
	OPTION(mds_bal_max, 0, OPT_INT, -1),
	OPTION(mds_bal_max_until, 0, OPT_INT, -1),
	OPTION(mds_bal_mode, 0, OPT_INT, 0),
	OPTION(mds_bal_min_rebalance, 0, OPT_FLOAT, .1),  // must be this much above average before we export anything
	OPTION(mds_bal_min_start, 0, OPT_FLOAT, .2),      // if we need less than this, we don't do anything
	OPTION(mds_bal_need_min, 0, OPT_FLOAT, .8),       // take within this range of what we need
	OPTION(mds_bal_need_max, 0, OPT_FLOAT, 1.2),
	OPTION(mds_bal_midchunk, 0, OPT_FLOAT, .3),       // any sub bigger than this taken in full
	OPTION(mds_bal_minchunk, 0, OPT_FLOAT, .001),     // never take anything smaller than this
	OPTION(mds_bal_target_removal_min, 0, OPT_INT, 5), // min balance iterations before old target is removed
	OPTION(mds_bal_target_removal_max, 0, OPT_INT, 10), // max balance iterations before old target is removed
	OPTION(mds_trim_on_rejoin, 0, OPT_BOOL, true),
	OPTION(mds_shutdown_check, 0, OPT_INT, 0),
	OPTION(mds_verify_export_dirauth, 0, OPT_BOOL, true),
	OPTION(mds_local_osd, 0, OPT_BOOL, false),
	OPTION(mds_thrash_exports, 0, OPT_INT, 0),
	OPTION(mds_thrash_fragments, 0, OPT_INT, 0),
	OPTION(mds_dump_cache_on_map, 0, OPT_BOOL, false),
	OPTION(mds_dump_cache_after_rejoin, 0, OPT_BOOL, false),
	OPTION(mds_hack_log_expire_for_better_stats, 0, OPT_BOOL, false),
	OPTION(mds_verify_scatter, 0, OPT_BOOL, false),
	OPTION(mds_debug_scatterstat, 0, OPT_BOOL, false),
	OPTION(mds_kill_mdstable_at, 0, OPT_INT, 0),
	OPTION(mds_kill_export_at, 0, OPT_INT, 0),
	OPTION(mds_kill_import_at, 0, OPT_INT, 0),
	OPTION(mds_kill_rename_at, 0, OPT_INT, 0),
	OPTION(mds_wipe_sessions, 0, OPT_BOOL, 0),
	OPTION(mds_wipe_ino_prealloc, 0, OPT_BOOL, 0),
	OPTION(mds_skip_ino, 0, OPT_INT, 0),
	OPTION(max_mds, 0, OPT_INT, 1),
	OPTION(osd_data, 0, OPT_STR, ""),
	OPTION(osd_journal, 0, OPT_STR, ""),
	OPTION(osd_journal_size, 0, OPT_INT, 0),         // in mb
	OPTION(osd_balance_reads, 0, OPT_BOOL, false),
	OPTION(osd_flash_crowd_iat_threshold, 0, OPT_INT, 0),
	OPTION(osd_flash_crowd_iat_alpha, 0, OPT_DOUBLE, 0.125),
	OPTION(osd_balance_reads_temp, 0, OPT_DOUBLE, 100),  // send from client to replica
	OPTION(osd_shed_reads, 0, OPT_INT, false),     // forward from primary to replica
	OPTION(osd_shed_reads_min_latency, 0, OPT_DOUBLE, .01),       // min local latency
	OPTION(osd_shed_reads_min_latency_diff, 0, OPT_DOUBLE, .01),  // min latency difference
	OPTION(osd_shed_reads_min_latency_ratio, 0, OPT_DOUBLE, 1.5),  // 1.2 == 20% higher than peer
	OPTION(osd_client_message_size_cap, 0, OPT_LONGLONG, 500*1024L*1024L), // default to 200MB client data allowed in-memory
	OPTION(osd_immediate_read_from_cache, 0, OPT_BOOL, false), // osds to read from the cache immediately?
	OPTION(osd_exclusive_caching, 0, OPT_BOOL, true),         // replicas evict replicated writes
	OPTION(osd_stat_refresh_interval, 0, OPT_DOUBLE, .5),
	OPTION(osd_min_pg_size_without_alive, 0, OPT_INT, 2),  // smallest pg we allow to activate without telling the monitor
	OPTION(osd_pg_bits, 0, OPT_INT, 6),  // bits per osd
	OPTION(osd_lpg_bits, 0, OPT_INT, 2),  // bits per osd
	OPTION(osd_object_layout, 0, OPT_INT, CEPH_OBJECT_LAYOUT_HASHINO),
	OPTION(osd_pg_layout, 0, OPT_INT, CEPH_PG_LAYOUT_CRUSH),
	OPTION(osd_min_rep, 0, OPT_INT, 1),
	OPTION(osd_max_rep, 0, OPT_INT, 10),
	OPTION(osd_min_raid_width, 0, OPT_INT, 3),
	OPTION(osd_max_raid_width, 0, OPT_INT, 2),
	OPTION(osd_pool_default_crush_rule, 0, OPT_INT, 0),
	OPTION(osd_pool_default_size, 0, OPT_INT, 2),
	OPTION(osd_pool_default_pg_num, 0, OPT_INT, 8),
	OPTION(osd_pool_default_pgp_num, 0, OPT_INT, 8),
	OPTION(osd_op_threads, 0, OPT_INT, 2),    // 0 == no threading
	OPTION(osd_max_opq, 0, OPT_INT, 10),
	OPTION(osd_disk_threads, 0, OPT_INT, 1),
	OPTION(osd_recovery_threads, 0, OPT_INT, 1),
	OPTION(osd_age, 0, OPT_FLOAT, .8),
	OPTION(osd_age_time, 0, OPT_INT, 0),
	OPTION(osd_heartbeat_interval, 0, OPT_INT, 1),
	OPTION(osd_mon_heartbeat_interval, 0, OPT_INT, 30),  // if no peers, ping monitor
	OPTION(osd_heartbeat_grace, 0, OPT_INT, 20),
	OPTION(osd_mon_report_interval, 0, OPT_INT, 5),  // pg stats, failures, up_thru, boot.
	OPTION(osd_min_down_reporters, 0, OPT_INT, 1),   // number of OSDs who need to report a down OSD for it to count
	OPTION(osd_min_down_reports, 0, OPT_INT, 3),     // number of times a down OSD must be reported for it to count
	OPTION(osd_replay_window, 0, OPT_INT, 45),
	OPTION(osd_max_pull, 0, OPT_INT, 2),
	OPTION(osd_preserve_trimmed_log, 0, OPT_BOOL, true),
	OPTION(osd_recovery_delay_start, 0, OPT_FLOAT, 15),
	OPTION(osd_recovery_max_active, 0, OPT_INT, 5),
	OPTION(osd_recovery_max_chunk, 0, OPT_LONGLONG, 1<<20),  // max size of push chunk
	OPTION(osd_recovery_forget_lost_objects, 0, OPT_BOOL, false),   // off for now
	OPTION(osd_max_scrubs, 0, OPT_INT, 1),
	OPTION(osd_scrub_load_threshold, 0, OPT_FLOAT, 0.5),
	OPTION(osd_scrub_min_interval, 0, OPT_FLOAT, 300),
	OPTION(osd_scrub_max_interval, 0, OPT_FLOAT, 60*60*24),   // once a day
	OPTION(osd_auto_weight, 0, OPT_BOOL, false),
	OPTION(osd_class_error_timeout, 0, OPT_DOUBLE, 60.0),  // seconds
	OPTION(osd_class_timeout, 0, OPT_DOUBLE, 60*60.0), // seconds
	OPTION(osd_class_tmp, 0, OPT_STR, "/var/lib/ceph/tmp"),
	OPTION(osd_check_for_log_corruption, 0, OPT_BOOL, false),
	OPTION(osd_use_stale_snap, 0, OPT_BOOL, false),
	OPTION(filestore, 0, OPT_BOOL, false),
	OPTION(filestore_max_sync_interval, 0, OPT_DOUBLE, 5),    // seconds
	OPTION(filestore_min_sync_interval, 0, OPT_DOUBLE, .01),  // seconds
	OPTION(filestore_fake_attrs, 0, OPT_BOOL, false),
	OPTION(filestore_fake_collections, 0, OPT_BOOL, false),
	OPTION(filestore_dev, 0, OPT_STR, 0),
	OPTION(filestore_btrfs_trans, 0, OPT_BOOL, false),
	OPTION(filestore_btrfs_snap, 0, OPT_BOOL, true),
	OPTION(filestore_btrfs_clone_range, 0, OPT_BOOL, true),
	OPTION(filestore_fsync_flushes_journal_data, 0, OPT_BOOL, false),
	OPTION(filestore_flusher, 0, OPT_BOOL, true),
	OPTION(filestore_flusher_max_fds, 0, OPT_INT, 512),
	OPTION(filestore_sync_flush, 0, OPT_BOOL, false),
	OPTION(filestore_journal_parallel, 0, OPT_BOOL, false),
	OPTION(filestore_journal_writeahead, 0, OPT_BOOL, false),
	OPTION(filestore_journal_trailing, 0, OPT_BOOL, false),
	OPTION(filestore_queue_max_ops, 0, OPT_INT, 500),
	OPTION(filestore_queue_max_bytes, 0, OPT_INT, 100 << 20),
	OPTION(filestore_op_threads, 0, OPT_INT, 2),
	OPTION(ebofs, 0, OPT_BOOL, false),
	OPTION(ebofs_cloneable, 0, OPT_BOOL, true),
	OPTION(ebofs_verify, 0, OPT_BOOL, false),
	OPTION(ebofs_commit_ms, 0, OPT_INT, 200),       // 0 = no forced commit timeout (for debugging/tracing)
	OPTION(ebofs_oc_size, 0, OPT_INT, 10000),      // onode cache
	OPTION(ebofs_cc_size, 0, OPT_INT, 10000),      // cnode cache
	OPTION(ebofs_bc_size, 0, OPT_LONGLONG, 50*256), // 4k blocks, *256 for MB
	OPTION(ebofs_bc_max_dirty, 0, OPT_LONGLONG, 30*256), // before write() will block
	OPTION(ebofs_max_prefetch, 0, OPT_INT, 1000), // 4k blocks
	OPTION(ebofs_realloc, 0, OPT_BOOL, false),    // hrm, this can cause bad fragmentation, don't use!
	OPTION(ebofs_verify_csum_on_read, 0, OPT_BOOL, true),
	OPTION(journal_dio, 0, OPT_BOOL, true),
	OPTION(journal_block_align, 0, OPT_BOOL, true),
	OPTION(journal_max_write_bytes, 0, OPT_INT, 10 << 20),
	OPTION(journal_max_write_entries, 0, OPT_INT, 100),
	OPTION(journal_queue_max_ops, 0, OPT_INT, 500),
	OPTION(journal_queue_max_bytes, 0, OPT_INT, 100 << 20),
	OPTION(journal_align_min_size, 0, OPT_INT, 64 << 10),  // align data payloads >= this.
	OPTION(bdev_lock, 0, OPT_BOOL, true),
	OPTION(bdev_iothreads, 0, OPT_INT, 1),         // number of ios to queue with kernel
	OPTION(bdev_idle_kick_after_ms, 0, OPT_INT, 100),  // ms
	OPTION(bdev_el_fw_max_ms, 0, OPT_INT, 10000),      // restart elevator at least once every 1000 ms
	OPTION(bdev_el_bw_max_ms, 0, OPT_INT, 3000),       // restart elevator at least once every 300 ms
	OPTION(bdev_el_bidir, 0, OPT_BOOL, false),          // bidirectional elevator?
	OPTION(bdev_iov_max, 0, OPT_INT, 512),            // max # iov's to collect into a single readv()/writev() call
	OPTION(bdev_debug_check_io_overlap, 0, OPT_BOOL, true),  // [DEBUG] check for any pending io overlaps
	OPTION(bdev_fake_mb, 0, OPT_INT, 0),
	OPTION(bdev_fake_max_mb, 0, OPT_INT, 0),
};

bool conf_set_conf_val(void *field, opt_type_t type, const char *val)
{
  switch (type) {
  case OPT_BOOL:
    if (strcasecmp(val, "false") == 0)
      *(bool *)field = false;
    else if (strcasecmp(val, "true") == 0)
      *(bool *)field = true;
    else
      *(bool *)field = (bool)atoi(val);
    break;
  case OPT_INT:
    *(int *)field = atoi(val);
    break;
  case OPT_LONGLONG:
    *(long long *)field = atoll(val);
    break;
  case OPT_STR:
    if (val)
      *(char **)field = strdup(val);
    else
      *(char **)field = NULL;
    break;
  case OPT_FLOAT:
    *(float *)field = atof(val);
    break;
  case OPT_DOUBLE:
    *(double *)field = strtod(val, NULL);
    break;
  case OPT_ADDR:
    ((entity_addr_t *)field)->parse(val);
    break;
  default:
    return false;
  }
  
  return true;
}

bool conf_set_conf_val(void *field, opt_type_t type, const char *val, long long intval, double doubleval)
{
  switch (type) {
  case OPT_BOOL:
    *(bool *)field = intval;
    break;
  case OPT_INT:
    *(int *)field = intval;
    break;
  case OPT_LONGLONG:
    *(long long *)field = intval;
    break;
  case OPT_STR:
    if (val) {
      *(char **)field = strdup(val);
    } else {
      *(char **)field = NULL;
    }
    break;
  case OPT_FLOAT:
    *(float *)field = doubleval;
    break;
  case OPT_DOUBLE:
    *(double *)field = doubleval;
    break;
  case OPT_ADDR:
    ((entity_addr_t *)field)->parse(val);
    break;
  default:
    return false;
  }
  
  return true;
}

static bool conf_reset_val(void *field, opt_type_t type)
{
  switch (type) {
  case OPT_BOOL:
    *(bool *)field = 0;
    break;
  case OPT_INT:
    *(int *)field = 0;
    break;
  case OPT_LONGLONG:
    *(long long *)field = 0;
    break;
  case OPT_STR:
      *(char **)field = NULL;
    break;
  case OPT_FLOAT:
    *(float *)field = 0;
    break;
  case OPT_DOUBLE:
    *(double *)field = 0;
    break;
  case OPT_ADDR:
    *(entity_addr_t *)field = entity_addr_t();
    break;
  default:
    return false;
  }

  return true;
}


static void set_conf_name(config_option *opt)
{
  char *newsection = (char *)opt->section;
  char *newconf = (char *)opt->name;
  int i;

  if (opt->section[0] == 0) {
    newsection = strdup("global");
  }

  if (strncmp(newsection, opt->name, strlen(newsection)) == 0) {
    /* if key starts with the name of the section, remove name of the section
       unless key equals to it */

    if (strcmp(newsection, opt->name) == 0)
      goto done;

    newconf = strdup(&opt->name[strlen(newsection)+1]);
  } else {
    newconf = strdup(opt->name);
  }

  i = 0;
  while (newconf[i]) {
    if (newconf[i] == '_')
      newconf[i] = ' ';

    ++i;
  }

  done:
    opt->section = newsection;
    opt->conf_name = (const char *)newconf;
}

static bool init_g_conf()
{
  int len = sizeof(config_optionsp)/sizeof(config_option);
  int i;
  config_option *opt;

  memset(&g_conf, 0, sizeof(g_conf));

  for (i = 0; i<len; i++) {
    opt = &config_optionsp[i];
    if (opt->val_ptr) {
      conf_reset_val(opt->val_ptr, opt->type);
    }
    if (!conf_set_conf_val(opt->val_ptr,
			   opt->type,
			   opt->def_str,
			   opt->def_longlong,
			   opt->def_double)) {
      cerr << "error initializing g_conf value num " << i << std::endl;
      return false;
    }

    set_conf_name(opt);
  }

  return true;
}

static int def_conf_to_str(config_option *opt, char *buf, int len)
{
  int ret = 0;

  switch (opt->type) {
  case OPT_INT:
  case OPT_BOOL:
    ret = snprintf(buf, len, "%d", (int)opt->def_longlong);
    break;
  case OPT_LONGLONG:
    ret = snprintf(buf, len, "%lld", opt->def_longlong);
    break;
  case OPT_STR:
  case OPT_ADDR:
    ret = snprintf(buf, len, "%s", opt->def_str);
    break;
  case OPT_FLOAT:
  case OPT_DOUBLE:
    ret = snprintf(buf, len, "%f", opt->def_double);
    break;
  default:
    break;
  }
  return ret;
}

int ceph_def_conf_by_name(const char *name, char *buf, int buflen)
{
  char *newname = strdup(name);
  int len = strlen(name);
  config_option *opt;
  int ret = 0;
  int i;

  for (i = 0; i < len; i++) {
    if (newname[i] == ' ')
      newname[i] = '_';
  }

  len = sizeof(config_optionsp)/sizeof(config_option);

  for (i = 0; i < len; i++) {
    opt = &config_optionsp[i];
    if (strcmp(opt->name, newname) == 0) {
      ret = def_conf_to_str(opt, buf, buflen);
      break;
    }
  }
  free(newname);
  return ret;
}

static void fini_g_conf()
{
  int len = sizeof(config_optionsp)/sizeof(config_option);
  int i;
  config_option *opt;

  for (i = 0; i<len; i++) {
    opt = &config_optionsp[i];
    if (opt->type == OPT_STR) {
      free(*(char **)opt->val_ptr);
    }
    free((void *)opt->conf_name);
  }
}

static bool g_conf_initialized = init_g_conf();

static bool cmd_is_char(const char *cmd)
{
	return ((cmd[0] == '-') &&
		cmd[1] && !cmd[2]);
}

bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos)
{
	unsigned int i;
	unsigned int len = strlen(opt);

	*val_pos = 0;

	if (!*cmd)
		return false;

	if (char_opt && cmd_is_char(cmd))
		return (char_opt == cmd[1]);

	if ((cmd[0] != '-') || (cmd[1] != '-'))
		return false;

	for (i=0; i<len; i++) {
		if ((opt[i] == '_') || (opt[i] == '-')) {
			switch (cmd[i+2]) {
			case '-':
			case '_':
				continue;
			default:
				break;
			}
		}

		if (cmd[i+2] != opt[i])
			return false;
	}

	if (cmd[i+2] == '=')
		*val_pos = i+3;
	else if (cmd[i+2])
		return false;

	return true;
}

static bool get_var(const char *str, int pos, char *var_name, int len, int *new_pos)
{
  int bracket = (str[pos] == '{');
  int out_pos = 0;

  if (bracket) {
    pos++;
  }

  while (str[pos] &&
	((bracket && str[pos] != '}') ||
	 isalnum(str[pos]) ||
         str[pos] == '_')) {
	var_name[out_pos] = str[pos];
	
	out_pos	++;
	if (out_pos == len)
		return false;
	pos++;
  }

  var_name[out_pos] = '\0';


  if (bracket && (str[pos] == '}'))
	pos++;

  *new_pos = pos;

  return true;
}

static const char *var_val(char *var_name)
{
	const char *val;

	if (strcmp(var_name, "type")==0)
		return g_conf.type;
	if (strcmp(var_name, "id")==0)
		return g_conf.id;
	if (strcmp(var_name, "num")==0)
		return g_conf.id;
	if (strcmp(var_name, "name")==0)
		return g_conf.name;
	if (strcmp(var_name, "host")==0)
		return g_conf.host;

	val = getenv(var_name);
	if (!val)
		val = "";

	return val;
}

#define MAX_LINE 256
#define MAX_VAR_LEN 32

char *conf_post_process_val(const char *val)
{
  char var_name[MAX_VAR_LEN];
  char *buf;
  int i=0;
  size_t out_pos = 0;
  size_t max_line = MAX_LINE;

  buf = (char *)malloc(max_line);
  *buf = 0;

  while (val[i]) {
    if (val[i] == '$') {
	if (get_var(val, i+1, var_name, MAX_VAR_LEN, &i)) {
		out_pos = dyn_snprintf(&buf, &max_line, 2, "%s%s", buf, var_val(var_name));
	} else {
	  ++i;
	}
    } else {
	if (out_pos == max_line - 1) {
		max_line *= 2;
		buf = (char *)realloc(buf, max_line);
	}
	buf[out_pos] = val[i];
	buf[out_pos + 1] = '\0';
    	++out_pos;
    	++i;
    }
  }

  buf[out_pos] = '\0';

  return buf;
}

#define OPT_READ_TYPE(ret, section, var, type, out, def) \
do { \
  if (def) \
    ret = cf->read(section, var, (type *)out, *(type *)def); \
  else \
    ret = cf->read(section, var, (type *)out, 0); \
} while (0)
    

int conf_read_key_ext(const char *conf_name, const char *conf_alt_name, const char *conf_type,
		      const char *alt_section, const char *key, opt_type_t type, void *out, void *def,
		      bool free_old_val)
{
  int s;
  int ret;
  char *tmp = 0;
  for (s=0; s<5; s++) {
    const char *section;

    switch (s) {
      case 0:
          section = conf_name;
          if (section)
            break;
      case 1:
          section = conf_alt_name;
          if (section)
            break;
      case 2:
	    s = 2;
            section = conf_type;
            if (section)
              break;
      case 3:
	    s = 3;
            section = alt_section;
	    if (section)
	      break;
      default:
	    s = 4;
	    section = "global";
    }

    switch (type) {
    case OPT_STR:
      if (free_old_val)
        tmp = *(char **)out;
      OPT_READ_TYPE(ret, section, key, char *, out, def);
      if (free_old_val &&
          *(char **)out != tmp)
          free(tmp);
      break;
    case OPT_BOOL:
      OPT_READ_TYPE(ret, section, key, bool, out, def);
      break;
    case OPT_LONGLONG:
      OPT_READ_TYPE(ret, section, key, long long, out, def);
      break;
    case OPT_INT:
      OPT_READ_TYPE(ret, section, key, int, out, def);
      break;
    case OPT_FLOAT:
      OPT_READ_TYPE(ret, section, key, float, out, def);
      break;
    case OPT_DOUBLE:
      OPT_READ_TYPE(ret, section, key, double, out, def);
      break;
    case OPT_ADDR:
      ret = cf->read(section, key, &tmp, (char *)def);
      if (*tmp == *((char *)def)) {
          ret = 0;
      }
      else {
        ret = 1;
      }
      if ((1 == ret) &&!(((entity_addr_t*)out)->parse(tmp))) {
        cerr << "Addr " << tmp << " failed to parse! Shutting down" << std::endl;
        exit(1);
      }
      break;
    default:
	ret = 0;
        break;
    }

    if (ret)
	break;
  }

  return ret;
}

int conf_read_key(const char *alt_section, const char *key, opt_type_t type, void *out, void *def, bool free_old_val)
{
	return conf_read_key_ext(g_conf.name, g_conf.alt_name, g_conf.type,
				 alt_section, key, type, out, def, free_old_val);
}

bool parse_config_file(ConfFile *cf, bool auto_update)
{
  int opt_len = sizeof(config_optionsp)/sizeof(config_option);

  cf->set_auto_update(false);
  cf->set_post_process_func(conf_post_process_val);
  if (!cf->parse())
	return false;

  for (int i=0; i<opt_len; i++) {
      config_option *opt = &config_optionsp[i];
      conf_read_key(NULL, opt->conf_name, opt->type, opt->val_ptr, opt->val_ptr, true);
  }
  conf_read_key(NULL, "lockdep", OPT_INT, &g_lockdep, &g_lockdep, false);

  return true;
}

bool is_bool_param(const char *param)
{
	return ((strcasecmp(param, "true")==0) || (strcasecmp(param, "false")==0));
}

void parse_startup_config_options(std::vector<const char*>& args, const char *module_type)
{
  DEFINE_CONF_VARS(NULL);
  std::vector<const char *> nargs;
  bool conf_specified = false;

  if (!g_conf.id)
    g_conf.id = (char *)g_default_id;
  if (!g_conf.type)
    g_conf.type = (char *)"";

  bool isdaemon = g_conf.daemonize;

  FOR_EACH_ARG(args) {
    if (CONF_ARG_EQ("version", 'v')) {
      cout << "ceph version " << VERSION << " (commit:" << STRINGIFY(CEPH_GIT_VER) << ")" << std::endl;
      _exit(0);
    } else if (CONF_ARG_EQ("conf", 'c')) {
	CONF_SAFE_SET_ARG_VAL(&g_conf.conf, OPT_STR);
	conf_specified = true;
    } else if (CONF_ARG_EQ("monmap", 'M')) {
	CONF_SAFE_SET_ARG_VAL(&g_conf.monmap, OPT_STR);
    } else if (CONF_ARG_EQ("show_conf", 'S')) {
      show_config = true;
    } else if (isdaemon && CONF_ARG_EQ("bind", 0)) {
      g_conf.public_addr.parse(args[++i]);
    } else if (isdaemon && CONF_ARG_EQ("nodaemon", 'D')) {
      g_conf.daemonize = false;
      g_conf.log_to_stdout = true;
      /*
    } else if (isdaemon && CONF_ARG_EQ("daemonize", 'd')) {
      g_conf.daemonize = true;
      g_conf.log_to_stdout = false;
      */
    } else if (isdaemon && CONF_ARG_EQ("foreground", 'f')) {
      g_conf.daemonize = false;
      //g_conf.log_to_stdout = false;
    } else if (isdaemon && (CONF_ARG_EQ("id", 'i') || CONF_ARG_EQ("name", 'n'))) {
      CONF_SAFE_SET_ARG_VAL(&g_conf.id, OPT_STR);
    } else if (!isdaemon && (CONF_ARG_EQ("id", 'I') || CONF_ARG_EQ("name", 'n'))) {
      CONF_SAFE_SET_ARG_VAL(&g_conf.id, OPT_STR);
    } else {
      nargs.push_back(args[i]);
    }
  }
  args.swap(nargs);
  nargs.clear();

  if (module_type) {
    g_conf.type = strdup(module_type);
    if (g_conf.id) {
      // is it "type.name"?
      const char *dot = strchr(g_conf.id, '.');
      if (dot) {
	int tlen = dot - g_conf.id;
	g_conf.type = (char *)malloc(tlen + 1);
	memcpy(g_conf.type, g_conf.id, tlen);
	g_conf.type[tlen] = 0;
	g_conf.id = strdup(dot + 1);
      }
      
      int len = strlen(g_conf.type) + strlen(g_conf.id) + 2;
      g_conf.name = (char *)malloc(len);
      snprintf(g_conf.name, len, "%s.%s", g_conf.type, g_conf.id);
      g_conf.alt_name = (char *)malloc(len - 1);
      snprintf(g_conf.alt_name, len - 1, "%s%s", module_type, g_conf.id);
    } else {
      g_conf.name = g_conf.type;
    }
  }
  g_conf.entity_name = new EntityName;
  assert(g_conf.entity_name);

  g_conf.entity_name->from_type_id(g_conf.type, g_conf.id);

  if (cf) {
    delete cf;
    cf = NULL;
  }

  // do post_process substitutions
  int len = sizeof(config_optionsp)/sizeof(config_option);
  for (int i = 0; i<len; i++) {
    config_option *opt = &config_optionsp[i];
    if (opt->type == OPT_STR && opt->val_ptr) {
      if (*(char**)opt->val_ptr) {
	*(char **)opt->val_ptr = conf_post_process_val(*(char **)opt->val_ptr);
      }
    }
  }
  // open new conf
  string fn = g_conf.conf;
  list<string> ls;
  get_str_list(fn, ls);
  bool read_conf = false;
  for (list<string>::iterator p = ls.begin(); p != ls.end(); p++) {
    cf = new ConfFile(p->c_str());
    read_conf = parse_config_file(cf, true);
    if (read_conf)
      break;
    delete cf;
    cf = NULL;
  }

  if (conf_specified && !read_conf) {
    cerr << "error reading config file(s) " << g_conf.conf << std::endl;
    exit(1);
  }

  if (!cf)
    return;

  if (show_config) {
    cf->dump();
    exit(0);
  }

  if (!ec) {
    ec = new ExportControl();
  }

  ec->load(cf);
}

void generic_usage()
{
  cerr << "   -c ceph.conf or --conf=ceph.conf\n";
  cerr << "        get options from given conf file" << std::endl;
}

void generic_server_usage()
{
  cerr << "   --debug_ms N\n";
  cerr << "        set message debug level (e.g. 1)\n";
  cerr << "   -D   debug (no fork, log to stdout)\n";
  cerr << "   -f   foreground (no fork, log to file)\n";
  generic_usage();
  exit(1);
}
void generic_client_usage()
{
  generic_usage();
  cerr << "   -d   daemonize (detach, fork, log to file)\n";
  cerr << "   -f   foreground (no fork, log to file)" << std::endl;
  exit(1);
}

ConfFile *conf_get_conf_file()
{
  return cf;
}

ExportControl *conf_get_export_control()
{
  return ec;
}

void parse_config_options(std::vector<const char*>& args)
{
  int opt_len = sizeof(config_optionsp)/sizeof(config_option);
  DEFINE_CONF_VARS(NULL);

  std::vector<const char*> nargs;
  FOR_EACH_ARG(args) {
    int optn;

    for (optn = 0; optn < opt_len; optn++) {
      if (CONF_ARG_EQ("lockdep", '\0')) {
	CONF_SAFE_SET_ARG_VAL(&g_lockdep, OPT_INT);
      } else if (CONF_ARG_EQ(config_optionsp[optn].name,
	    config_optionsp[optn].char_option)) {
        if (__isarg || val_pos || config_optionsp[optn].type == OPT_BOOL)
	    CONF_SAFE_SET_ARG_VAL(config_optionsp[optn].val_ptr, config_optionsp[optn].type);
        else
          continue;
      } else {
        continue;
      }
      break;
    }

    if (optn == opt_len)
        nargs.push_back(args[i]);
  }

  install_sighandler(SIGHUP, sighup_handler, SA_RESTART);
  install_sighandler(SIGSEGV, sigsegv_handler, SA_RESETHAND);
  install_sighandler(SIGSEGV, sigabrt_handler, SA_RESETHAND);

  args = nargs;
}