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
|
/*
* Copyright (c) 2005 Christophe Varoqui
*/
#include <checkers.h>
#include <memory.h>
#include <vector.h>
#include <structs.h>
#include <structs_vec.h>
#include <libdevmapper.h>
#include <devmapper.h>
#include <config.h>
#include <configure.h>
#include <blacklist.h>
#include <debug.h>
#include <print.h>
#include <sysfs.h>
#include <errno.h>
#include "main.h"
#include "cli.h"
int
show_paths (char ** r, int * len, struct vectors * vecs, char * style)
{
int i;
struct path * pp;
char * c;
char * reply;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
get_path_layout(vecs->pathvec, 1);
reply = MALLOC(maxlen);
while (again) {
if (!reply)
return 1;
c = reply;
if (VECTOR_SIZE(vecs->pathvec) > 0)
c += snprint_path_header(c, reply + maxlen - c,
style);
vector_foreach_slot(vecs->pathvec, pp, i)
c += snprint_path(c, reply + maxlen - c,
style, pp);
again = ((c - reply) == (maxlen - 1));
if (again)
reply = REALLOC(reply, maxlen *= 2);
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
show_map_topology (char ** r, int * len, struct multipath * mpp)
{
char * c;
char * reply;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
reply = MALLOC(maxlen);
while (again) {
if (!reply)
return 1;
c = reply;
c += snprint_multipath_topology(c, reply + maxlen - c, mpp, 2);
again = ((c - reply) == (maxlen - 1));
if (again)
reply = REALLOC(reply, maxlen *= 2);
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
show_maps_topology (char ** r, int * len, struct vectors * vecs)
{
int i;
struct multipath * mpp;
char * c;
char * reply;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
get_path_layout(vecs->pathvec, 0);
reply = MALLOC(maxlen);
while (again) {
if (!reply)
return 1;
c = reply;
vector_foreach_slot(vecs->mpvec, mpp, i)
c += snprint_multipath_topology(c, reply + maxlen - c,
mpp, 2);
again = ((c - reply) == (maxlen - 1));
if (again)
reply = REALLOC(reply, maxlen *= 2);
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
show_config (char ** r, int * len)
{
char * c;
char * reply;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
reply = MALLOC(maxlen);
while (again) {
if (!reply)
return 1;
c = reply;
c += snprint_defaults(c, reply + maxlen - c);
again = ((c - reply) == maxlen);
if (again) {
reply = REALLOC(reply, maxlen *= 2);
continue;
}
c += snprint_blacklist(c, reply + maxlen - c);
again = ((c - reply) == maxlen);
if (again) {
reply = REALLOC(reply, maxlen *= 2);
continue;
}
c += snprint_blacklist_except(c, reply + maxlen - c);
again = ((c - reply) == maxlen);
if (again) {
reply = REALLOC(reply, maxlen *= 2);
continue;
}
c += snprint_hwtable(c, reply + maxlen - c, conf->hwtable);
again = ((c - reply) == maxlen);
if (again) {
reply = REALLOC(reply, maxlen *= 2);
continue;
}
c += snprint_mptable(c, reply + maxlen - c, conf->mptable);
again = ((c - reply) == maxlen);
if (again)
reply = REALLOC(reply, maxlen *= 2);
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
cli_list_config (void * v, char ** reply, int * len, void * data)
{
condlog(3, "list config (operator)");
return show_config(reply, len);
}
int
cli_list_paths (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list paths (operator)");
return show_paths(reply, len, vecs, PRINT_PATH_CHECKER);
}
int
cli_list_paths_fmt (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * fmt = get_keyparam(v, FMT);
condlog(3, "list paths (operator)");
return show_paths(reply, len, vecs, fmt);
}
int
cli_list_map_topology (void * v, char ** reply, int * len, void * data)
{
struct multipath * mpp;
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, MAP);
get_path_layout(vecs->pathvec, 0);
mpp = find_mp_by_str(vecs->mpvec, param);
if (!mpp)
return 1;
condlog(3, "list multipath %s (operator)", param);
return show_map_topology(reply, len, mpp);
}
int
cli_list_maps_topology (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list multipaths (operator)");
return show_maps_topology(reply, len, vecs);
}
int
cli_list_wildcards (void * v, char ** reply, int * len, void * data)
{
char * c;
*reply = MALLOC(INITIAL_REPLY_LEN);
if (!reply)
return 1;
c = *reply;
c += snprint_wildcards(c, INITIAL_REPLY_LEN);
*len = INITIAL_REPLY_LEN;
return 0;
}
int
show_status (char ** r, int *len, struct vectors * vecs)
{
char * c;
char * reply;
unsigned int maxlen = INITIAL_REPLY_LEN;
reply = MALLOC(maxlen);
if (!reply)
return 1;
c = reply;
c += snprint_status(c, reply + maxlen - c, vecs);
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
show_maps (char ** r, int *len, struct vectors * vecs, char * style)
{
int i;
struct multipath * mpp;
char * c;
char * reply;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
get_multipath_layout(vecs->mpvec, 1);
reply = MALLOC(maxlen);
while (again) {
if (!reply)
return 1;
c = reply;
if (VECTOR_SIZE(vecs->mpvec) > 0)
c += snprint_multipath_header(c, reply + maxlen - c,
style);
vector_foreach_slot(vecs->mpvec, mpp, i)
c += snprint_multipath(c, reply + maxlen - c,
style, mpp);
again = ((c - reply) == (maxlen - 1));
if (again)
reply = REALLOC(reply, maxlen *= 2);
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
cli_list_maps_fmt (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * fmt = get_keyparam(v, FMT);
condlog(3, "list maps (operator)");
return show_maps(reply, len, vecs, fmt);
}
int
cli_list_maps (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list maps (operator)");
return show_maps(reply, len, vecs, PRINT_MAP_NAMES);
}
int
cli_list_status (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list status (operator)");
return show_status(reply, len, vecs);
}
int
cli_list_maps_status (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list maps status (operator)");
return show_maps(reply, len, vecs, PRINT_MAP_STATUS);
}
int
cli_list_maps_stats (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list maps stats (operator)");
return show_maps(reply, len, vecs, PRINT_MAP_STATS);
}
int
cli_add_path (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, PATH);
int r;
condlog(2, "%s: add path (operator)", param);
if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
param) > 0 || (r = ev_add_path(param, vecs)) == 2) {
*reply = strdup("blacklisted");
*len = strlen(*reply) + 1;
condlog(2, "%s: path blacklisted", param);
return 0;
}
return r;
}
int
cli_del_path (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, PATH);
condlog(2, "%s: remove path (operator)", param);
return ev_remove_path(param, vecs);
}
int
cli_add_map (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, MAP);
int minor;
char dev_path[PATH_SIZE];
struct sysfs_device *sysdev;
condlog(2, "%s: add map (operator)", param);
if (filter_wwid(conf->blist_wwid, conf->elist_wwid, param) > 0) {
*reply = strdup("blacklisted");
*len = strlen(*reply) + 1;
condlog(2, "%s: map blacklisted", param);
return 0;
}
minor = dm_get_minor(param);
if (minor < 0) {
condlog(2, "%s: not a device mapper table", param);
return 0;
}
sprintf(dev_path,"/block/dm-%d", minor);
sysdev = sysfs_device_get(dev_path);
if (!sysdev) {
condlog(2, "%s: not found in sysfs", param);
return 0;
}
return ev_add_map(sysdev, vecs);
}
int
cli_del_map (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, MAP);
condlog(2, "%s: remove map (operator)", param);
return ev_remove_map(param, vecs);
}
int resize_map(struct multipath *mpp, unsigned long long size,
struct vectors * vecs)
{
mpp->size = size;
update_mpp_paths(mpp, vecs->pathvec);
setup_map(mpp);
mpp->action = ACT_RESIZE;
if (domap(mpp) <= 0) {
condlog(0, "%s: failed to resize map : %s", mpp->alias,
strerror(errno));
return 1;
}
return 0;
}
int
cli_resize(void *v, char **reply, int *len, void *data)
{
struct vectors * vecs = (struct vectors *)data;
char * mapname = get_keyparam(v, MAP);
struct multipath *mpp;
int minor;
unsigned long long size;
struct pathgroup *pgp;
struct path *pp;
condlog(2, "%s: resize map (operator)", mapname);
if (sscanf(mapname, "dm-%d", &minor) == 1)
mpp = find_mp_by_minor(vecs->mpvec, minor);
else
mpp = find_mp_by_alias(vecs->mpvec, mapname);
if (!mpp) {
condlog(0, "%s: invalid map name. cannot resize", mapname);
return 1;
}
pgp = VECTOR_SLOT(mpp->pg, 0);
pp = VECTOR_SLOT(pgp->paths, 0);
condlog(0,"%s: reading sysfs.", mapname);
if (sysfs_get_size(pp->sysdev, &size)) {
condlog(0, "%s: couldn't get size for sysfs. cannot resize",
mapname);
return 1;
}
if (size == mpp->size) {
condlog(0, "%s: map is still the same size (%llu)", mapname,
mpp->size);
return 0;
}
condlog(3, "%s old size is %llu, new size is %llu", mapname, mpp->size,
size);
condlog(0,"%s: resize_map.");
if (resize_map(mpp, size, vecs) != 0)
return 1;
condlog(0,"%s: dm_lib_release.", mapname);
dm_lib_release();
condlog(0,"%s: setup multipath.", mapname);
setup_multipath(vecs, mpp);
condlog(0,"%s: sync map state.", mapname);
sync_map_state(mpp);
return 0;
}
int
cli_switch_group(void * v, char ** reply, int * len, void * data)
{
char * mapname = get_keyparam(v, MAP);
int groupnum = atoi(get_keyparam(v, GROUP));
condlog(2, "%s: switch to path group #%i (operator)", mapname, groupnum);
return dm_switchgroup(mapname, groupnum);
}
int
cli_reconfigure(void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(2, "reconfigure (operator)");
return reconfigure(vecs);
}
int
cli_suspend(void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, MAP);
int r = dm_simplecmd(DM_DEVICE_SUSPEND, param, 1);
condlog(2, "%s: suspend (operator)", param);
if (!r) /* error */
return 1;
struct multipath * mpp = find_mp_by_alias(vecs->mpvec, param);
if (!mpp)
return 1;
dm_get_info(param, &mpp->dmi);
return 0;
}
int
cli_resume(void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, MAP);
int r = dm_simplecmd(DM_DEVICE_RESUME, param, 1);
condlog(2, "%s: resume (operator)", param);
if (!r) /* error */
return 1;
struct multipath * mpp = find_mp_by_alias(vecs->mpvec, param);
if (!mpp)
return 1;
dm_get_info(param, &mpp->dmi);
return 0;
}
int
cli_reinstate(void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, PATH);
struct path * pp;
pp = find_path_by_dev(vecs->pathvec, param);
if (!pp)
pp = find_path_by_devt(vecs->pathvec, param);
if (!pp || !pp->mpp || !pp->mpp->alias)
return 1;
condlog(2, "%s: reinstate path %s (operator)",
pp->mpp->alias, pp->dev_t);
checker_enable(&pp->checker);
return dm_reinstate_path(pp->mpp->alias, pp->dev_t);
}
int
cli_fail(void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
char * param = get_keyparam(v, PATH);
struct path * pp;
int r;
pp = find_path_by_dev(vecs->pathvec, param);
if (!pp)
pp = find_path_by_devt(vecs->pathvec, param);
if (!pp || !pp->mpp || !pp->mpp->alias)
return 1;
condlog(2, "%s: fail path %s (operator)",
pp->mpp->alias, pp->dev_t);
r = dm_fail_path(pp->mpp->alias, pp->dev_t);
/*
* Suspend path checking to avoid auto-reinstating the path
*/
if (!r)
checker_disable(&pp->checker);
return r;
}
int
show_blacklist (char ** r, int * len)
{
char *c = NULL;
char *reply = NULL;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
while (again) {
reply = MALLOC(maxlen);
if (!reply)
return 1;
c = reply;
c += snprint_blacklist_report(c, maxlen);
again = ((c - reply) == maxlen);
if (again) {
maxlen *= 2;
FREE(reply);
continue;
}
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
cli_list_blacklist (void * v, char ** reply, int * len, void * data)
{
condlog(3, "list blacklist (operator)");
return show_blacklist(reply, len);
}
int
show_devices (char ** r, int * len, struct vectors *vecs)
{
char *c = NULL;
char *reply = NULL;
unsigned int maxlen = INITIAL_REPLY_LEN;
int again = 1;
while (again) {
reply = MALLOC(maxlen);
if (!reply)
return 1;
c = reply;
c += snprint_devices(c, maxlen, vecs);
again = ((c - reply) == maxlen);
if (again) {
maxlen *= 2;
FREE(reply);
continue;
}
}
*r = reply;
*len = (int)(c - reply + 1);
return 0;
}
int
cli_list_devices (void * v, char ** reply, int * len, void * data)
{
struct vectors * vecs = (struct vectors *)data;
condlog(3, "list devices (operator)");
return show_devices(reply, len, vecs);
}
int
cli_quit (void * v, char ** reply, int * len, void * data)
{
return 0;
}
|