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
|
/*
Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "cli.h"
#include "cli-cmd.h"
#include "cli-mem-types.h"
#include "cli1-xdr.h"
extern struct rpc_clnt *global_rpc;
extern rpc_clnt_prog_t *cli_rpc_prog;
int
cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word,
const char **words, int wordcount);
void
cli_cmd_volume_start_usage ()
{
cli_out ("Usage: volume start <VOLNAME>");
}
void
cli_cmd_volume_stop_usage ()
{
cli_out ("Usage: volume stop <VOLNAME> [force]");
}
void
cli_cmd_volume_rename_usage ()
{
cli_out ("Usage: volume rename <VOLNAME> <NEW-VOLNAME>");
}
void
cli_cmd_volume_delete_usage ()
{
cli_out ("Usage: volume delete <VOLNAME>");
}
void
cli_cmd_volume_info_usage ()
{
cli_out ("Usage: volume info [all|<VOLNAME>]");
}
int
cli_cmd_volume_info_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
cli_cmd_volume_get_ctx_t ctx = {0,};
cli_local_t *local = NULL;
proc = &cli_rpc_prog->proctable[GF1_CLI_GET_VOLUME];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
if ((wordcount == 2) || (wordcount == 3 &&
!strcmp (words[2], "all"))) {
ctx.flags = GF_CLI_GET_NEXT_VOLUME;
proc = &cli_rpc_prog->proctable[GF1_CLI_GET_NEXT_VOLUME];
} else if (wordcount == 3) {
ctx.flags = GF_CLI_GET_VOLUME;
ctx.volname = (char *)words[2];
if (strlen (ctx.volname) > 1024) {
cli_out ("Invalid volume name");
goto out;
}
proc = &cli_rpc_prog->proctable[GF1_CLI_GET_VOLUME];
} else {
cli_cmd_volume_info_usage ();
return -1;
}
local = cli_local_get ();
if (!local)
goto out;
local->u.get_vol.flags = ctx.flags;
if (ctx.volname)
local->u.get_vol.volname = gf_strdup (ctx.volname);
frame->local = local;
if (proc->fn) {
ret = proc->fn (frame, THIS, &ctx);
}
out:
if (ret)
cli_out ("Getting Volume information failed!");
return ret;
}
void
cli_cmd_volume_create_usage ()
{
cli_out ("Usage: volume create <NEW-VOLNAME> "
"[stripe <COUNT>] [replica <COUNT>] [transport <tcp|rdma>] "
"<NEW-BRICK> ...");
}
int
cli_cmd_volume_create_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
proc = &cli_rpc_prog->proctable[GF1_CLI_CREATE_VOLUME];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_volume_create_parse (words, wordcount, &options);
if (ret) {
cli_cmd_volume_create_usage ();
goto out;
}
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (options)
dict_unref (options);
return ret;
}
int
cli_cmd_volume_delete_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
char *volname = NULL;
proc = &cli_rpc_prog->proctable[GF1_CLI_DELETE_VOLUME];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
if (wordcount != 3) {
cli_cmd_volume_delete_usage ();
goto out;
}
volname = (char *)words[2];
if (proc->fn) {
ret = proc->fn (frame, THIS, volname);
}
out:
if (ret && volname)
cli_out ("Deleting Volume %s failed", volname);
return ret;
}
int
cli_cmd_volume_start_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
char *volname = NULL;
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
if (wordcount != 3) {
cli_cmd_volume_start_usage ();
goto out;
}
volname = (char *)words[2];
proc = &cli_rpc_prog->proctable[GF1_CLI_START_VOLUME];
if (proc->fn) {
ret = proc->fn (frame, THIS, volname);
}
out:
if (!proc && ret && volname)
cli_out ("Starting Volume %s failed", volname);
return ret;
}
int
cli_cmd_volume_stop_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
int flags = 0;
gf1_cli_stop_vol_req req = {0,};
char answer;
char flush;
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
if (wordcount < 3 || wordcount > 4) {
cli_cmd_volume_stop_usage ();
goto out;
}
req.volname = (char *)words[2];
if (!req.volname)
goto out;
if (wordcount == 4) {
if (!strcmp("force", words[3])) {
flags |= GF_CLI_FLAG_OP_FORCE;
} else {
ret = -1;
cli_cmd_volume_stop_usage ();
goto out;
}
}
printf ("Stopping volume will make its data inaccessible. "
"Do you want to Continue? (y/n) ");
answer = getchar ();
flush = answer;
while ('\n' != flush)
flush = getchar ();
if ('y' != answer) {
ret = 0;
goto out;
}
req.flags = flags;
proc = &cli_rpc_prog->proctable[GF1_CLI_STOP_VOLUME];
if (proc->fn) {
ret = proc->fn (frame, THIS, &req);
}
out:
if (!proc && ret && req.volname)
cli_out ("Stopping Volume %s failed", req.volname);
return ret;
}
int
cli_cmd_volume_rename_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *dict = NULL;
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
dict = dict_new ();
if (!dict)
goto out;
if (wordcount != 4) {
cli_cmd_volume_rename_usage ();
goto out;
}
ret = dict_set_str (dict, "old-volname", (char *)words[2]);
if (ret)
goto out;
ret = dict_set_str (dict, "new-volname", (char *)words[3]);
if (ret)
goto out;
proc = &cli_rpc_prog->proctable[GF1_CLI_RENAME_VOLUME];
if (proc->fn) {
ret = proc->fn (frame, THIS, dict);
}
out:
if (!proc && ret) {
if (dict)
dict_destroy (dict);
if (wordcount > 2)
cli_out ("Renaming Volume %s failed", (char *)words[2]);
}
return ret;
}
void
cli_cmd_volume_defrag_usage ()
{
cli_out ("Usage: volume rebalance <VOLNAME> <start|stop|status>");
}
int
cli_cmd_volume_defrag_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *dict = NULL;
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
dict = dict_new ();
if (!dict)
goto out;
if (wordcount != 4) {
cli_cmd_volume_defrag_usage();
goto out;
}
ret = dict_set_str (dict, "volname", (char *)words[2]);
if (ret)
goto out;
ret = dict_set_str (dict, "command", (char *)words[3]);
if (ret)
goto out;
proc = &cli_rpc_prog->proctable[GF1_CLI_DEFRAG_VOLUME];
if (proc->fn) {
ret = proc->fn (frame, THIS, dict);
}
out:
if (!proc && ret) {
if (dict)
dict_destroy (dict);
if (wordcount > 2)
cli_out ("Rebalance of Volume %s failed",
(char *)words[2]);
}
return 0;
}
int
cli_cmd_volume_set_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
cli_cmd_broadcast_response (0);
return 0;
}
void
cli_cmd_volume_add_brick_usage ()
{
cli_out ("Usage: volume add-brick <VOLNAME> "
"<NEW-BRICK> ...");
}
int
cli_cmd_volume_add_brick_cbk (struct cli_state *state,
struct cli_cmd_word *word, const char **words,
int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_volume_add_brick_parse (words, wordcount, &options);
if (ret) {
cli_cmd_volume_add_brick_usage ();
goto out;
}
proc = &cli_rpc_prog->proctable[GF1_CLI_ADD_BRICK];
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (!proc && ret) {
if (wordcount > 2) {
char *volname = (char *) words[2];
cli_out ("Adding brick to Volume %s failed",volname );
}
}
return ret;
}
void
cli_cmd_volume_remove_brick_usage ()
{
cli_out ("Usage: volume remove-brick <VOLNAME> "
"<BRICK> ...");
}
int
cli_cmd_volume_remove_brick_cbk (struct cli_state *state,
struct cli_cmd_word *word, const char **words,
int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
char answer;
char flush;
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_volume_remove_brick_parse (words, wordcount, &options);
if (ret) {
cli_cmd_volume_remove_brick_usage ();
goto out;
}
printf ("Removing brick(s) can result in data loss. "
"Do you want to Continue? (y/n) ");
answer = getchar ();
flush = answer;
while ('\n' != flush)
flush = getchar ();
if ('y' != answer) {
ret = 0;
goto out;
}
proc = &cli_rpc_prog->proctable[GF1_CLI_REMOVE_BRICK];
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (!proc && ret) {
if (wordcount > 2) {
char *volname = (char *) words[2];
cli_out ("Removing brick from Volume %s failed",volname );
}
}
return ret;
}
void
cli_cmd_volume_replace_brick_usage ()
{
cli_out("Usage: volume replace-brick <VOLNAME> "
"(<BRICK> <NEW-BRICK>) start|pause|abort|status");
}
int
cli_cmd_volume_replace_brick_cbk (struct cli_state *state,
struct cli_cmd_word *word,
const char **words,
int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
proc = &cli_rpc_prog->proctable[GF1_CLI_REPLACE_BRICK];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_volume_replace_brick_parse (words, wordcount, &options);
if (ret) {
cli_cmd_volume_replace_brick_usage ();
goto out;
}
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (ret) {
if (wordcount > 2) {
char *volname = (char *) words[2];
cli_out ("Replacing brick from Volume %s failed",volname );
}
}
return ret;
}
int
cli_cmd_volume_set_transport_cbk (struct cli_state *state,
struct cli_cmd_word *word,
const char **words, int wordcount)
{
cli_cmd_broadcast_response (0);
return 0;
}
void
cli_cmd_log_filename_usage ()
{
cli_out ("Usage: volume log filename <VOLNAME> [BRICK] <PATH>");
}
int
cli_cmd_log_filename_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
if (!((wordcount == 5) || (wordcount == 6))) {
cli_cmd_log_filename_usage ();
goto out;
}
proc = &cli_rpc_prog->proctable[GF1_CLI_LOG_FILENAME];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_log_filename_parse (words, wordcount, &options);
if (ret)
goto out;
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (ret)
cli_out ("setting log filename failed");
if (options)
dict_destroy (options);
return ret;
}
void
cli_cmd_log_locate_usage ()
{
cli_out ("Usage: volume log locate <VOLNAME> [BRICK]");
}
int
cli_cmd_log_locate_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
if (!((wordcount == 4) || (wordcount == 5))) {
cli_cmd_log_locate_usage ();
goto out;
}
proc = &cli_rpc_prog->proctable[GF1_CLI_LOG_LOCATE];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_log_locate_parse (words, wordcount, &options);
if (ret)
goto out;
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (ret)
cli_out ("getting log file location information failed");
if (options)
dict_destroy (options);
return ret;
}
void
cli_cmd_log_rotate_usage ()
{
cli_out ("Usage: volume log rotate <VOLNAME> [BRICK]");
}
int
cli_cmd_log_rotate_cbk (struct cli_state *state, struct cli_cmd_word *word,
const char **words, int wordcount)
{
int ret = -1;
rpc_clnt_procedure_t *proc = NULL;
call_frame_t *frame = NULL;
dict_t *options = NULL;
if (!((wordcount == 4) || (wordcount == 5))) {
cli_cmd_log_rotate_usage ();
goto out;
}
proc = &cli_rpc_prog->proctable[GF1_CLI_LOG_ROTATE];
frame = create_frame (THIS, THIS->ctx->pool);
if (!frame)
goto out;
ret = cli_cmd_log_rotate_parse (words, wordcount, &options);
if (ret)
goto out;
if (proc->fn) {
ret = proc->fn (frame, THIS, options);
}
out:
if (ret)
cli_out ("getting log file location information failed");
if (options)
dict_destroy (options);
return ret;
}
struct cli_cmd volume_cmds[] = {
{ "volume info [all|<VOLNAME>]",
cli_cmd_volume_info_cbk,
"list information of all volumes"},
{ "volume create <NEW-VOLNAME> [stripe <COUNT>] [replica <COUNT>] [transport <tcp|rdma>] <NEW-BRICK> ...",
cli_cmd_volume_create_cbk,
"create a new volume of specified type with mentioned bricks"},
{ "volume delete <VOLNAME>",
cli_cmd_volume_delete_cbk,
"delete volume specified by <VOLNAME>"},
{ "volume start <VOLNAME>",
cli_cmd_volume_start_cbk,
"start volume specified by <VOLNAME>"},
{ "volume stop <VOLNAME> [force]",
cli_cmd_volume_stop_cbk,
"stop volume specified by <VOLNAME>"},
{ "volume rename <VOLNAME> <NEW-VOLNAME>",
cli_cmd_volume_rename_cbk,
"rename volume <VOLNAME> to <NEW-VOLNAME>"},
{ "volume add-brick <VOLNAME> [(replica <COUNT>)|(stripe <COUNT>)] <NEW-BRICK> ...",
cli_cmd_volume_add_brick_cbk,
"add brick to volume <VOLNAME>"},
{ "volume remove-brick <VOLNAME> [(replica <COUNT>)|(stripe <COUNT>)] <BRICK> ...",
cli_cmd_volume_remove_brick_cbk,
"remove brick from volume <VOLNAME>"},
{ "volume rebalance <VOLNAME> start",
cli_cmd_volume_defrag_cbk,
"start rebalance of volume <VOLNAME>"},
{ "volume rebalance <VOLNAME> stop",
cli_cmd_volume_defrag_cbk,
"stop rebalance of volume <VOLNAME>"},
{ "volume rebalance <VOLNAME> status",
cli_cmd_volume_defrag_cbk,
"rebalance status of volume <VOLNAME>"},
{ "volume replace-brick <VOLNAME> (<BRICK> <NEW-BRICK>) start|pause|abort|status",
cli_cmd_volume_replace_brick_cbk,
"replace-brick operations"},
{ "volume set-transport <VOLNAME> <TRANSPORT-TYPE> [<TRANSPORT-TYPE>] ...",
cli_cmd_volume_set_transport_cbk,
"set transport type for volume <VOLNAME>"},
{ "volume set <VOLNAME> <KEY> <VALUE>",
cli_cmd_volume_set_cbk,
"set options for volume <VOLNAME>"},
{ "volume --help",
cli_cmd_volume_help_cbk,
"display help for the volume command"},
{ "volume log filename <VOLNAME> [BRICK] <PATH>",
cli_cmd_log_filename_cbk,
"set the log file for corresponding volume/brick"},
{ "volume log locate <VOLNAME> [BRICK]",
cli_cmd_log_locate_cbk,
"locate the log file for corresponding volume/brick"},
{ "volume log rotate <VOLNAME> [BRICK]",
cli_cmd_log_rotate_cbk,
"rotate the log file for corresponding volume/brick"},
{ NULL, NULL, NULL }
};
int
cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word,
const char **words, int wordcount)
{
struct cli_cmd *cmd = NULL;
for (cmd = volume_cmds; cmd->pattern; cmd++)
cli_out ("%s - %s", cmd->pattern, cmd->desc);
if (!state->rl_enabled)
exit (0);
return 0;
}
int
cli_cmd_volume_register (struct cli_state *state)
{
int ret = 0;
struct cli_cmd *cmd = NULL;
for (cmd = volume_cmds; cmd->pattern; cmd++) {
ret = cli_cmd_register (&state->tree, cmd->pattern, cmd->cbk,
cmd->desc);
if (ret)
goto out;
}
out:
return ret;
}
|