-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathlvm-dbus.c
4979 lines (4391 loc) · 180 KB
/
lvm-dbus.c
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
/*
* Copyright (C) 2015-2016 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Vratislav Podzimek <vpodzime@redhat.com>
*/
#include <glib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <blockdev/utils.h>
#include <gio/gio.h>
#include <libdevmapper.h>
#include "lvm.h"
#include "check_deps.h"
#include "dm_logging.h"
#include "vdo_stats.h"
#define INT_FLOAT_EPS 1e-5
#define SECTOR_SIZE 512
#define VDO_POOL_SUFFIX "vpool"
#define DEFAULT_PE_SIZE (4 MiB)
#define USE_DEFAULT_PE_SIZE 0
#define RESOLVE_PE_SIZE(size) ((size) == USE_DEFAULT_PE_SIZE ? DEFAULT_PE_SIZE : (size))
#define THPOOL_MD_FACTOR_NEW (0.2)
#define THPOOL_MD_FACTOR_EXISTS (1 / 6.0)
#define MIN_PE_SIZE (1 KiB)
#define MAX_PE_SIZE (16 GiB)
#define MIN_THPOOL_MD_SIZE (4 MiB)
/* DM_THIN_MAX_METADATA_SIZE is in 512 sectors */
#define MAX_THPOOL_MD_SIZE (DM_THIN_MAX_METADATA_SIZE * 512)
#define MIN_THPOOL_CHUNK_SIZE (64 KiB)
#define MAX_THPOOL_CHUNK_SIZE (1 GiB)
#define DEFAULT_CHUNK_SIZE (64 KiB)
/* according to lvmcache (7) */
#define MIN_CACHE_MD_SIZE (8 MiB)
#define LVM_MIN_VERSION "2.02.116"
#define LVM_VERSION_FSRESIZE "2.03.19"
#ifdef __LP64__
/* 64bit system */
#define MAX_LV_SIZE (8 EiB)
#else
/* 32bit system */
#define MAX_LV_SIZE (16 TiB)
#endif
static GMutex global_config_lock;
static gchar *global_config_str = NULL;
static gchar *global_devices_str = NULL;
#define LVM_BUS_NAME "com.redhat.lvmdbus1"
#define LVM_OBJ_PREFIX "/com/redhat/lvmdbus1"
#define MANAGER_OBJ "/com/redhat/lvmdbus1/Manager"
#define MANAGER_INTF "com.redhat.lvmdbus1.Manager"
#define JOB_OBJ_PREFIX "/com/redhat/lvmdbus1/Job/"
#define JOB_INTF "com.redhat.lvmdbus1.Job"
#define PV_OBJ_PREFIX LVM_OBJ_PREFIX"/Pv"
#define VG_OBJ_PREFIX LVM_OBJ_PREFIX"/Vg"
#define LV_OBJ_PREFIX LVM_OBJ_PREFIX"/Lv"
#define HIDDEN_LV_OBJ_PREFIX LVM_OBJ_PREFIX"/HiddenLv"
#define THIN_POOL_OBJ_PREFIX LVM_OBJ_PREFIX"/ThinPool"
#define CACHE_POOL_OBJ_PREFIX LVM_OBJ_PREFIX"/CachePool"
#define VDO_POOL_OBJ_PREFIX LVM_OBJ_PREFIX"/VdoPool"
#define PV_INTF LVM_BUS_NAME".Pv"
#define VG_INTF LVM_BUS_NAME".Vg"
#define VG_VDO_INTF LVM_BUS_NAME".VgVdo"
#define LV_CMN_INTF LVM_BUS_NAME".LvCommon"
#define LV_INTF LVM_BUS_NAME".Lv"
#define CACHED_LV_INTF LVM_BUS_NAME".CachedLv"
#define SNAP_INTF LVM_BUS_NAME".Snapshot"
#define THPOOL_INTF LVM_BUS_NAME".ThinPool"
#define CACHE_POOL_INTF LVM_BUS_NAME".CachePool"
#define VDO_POOL_INTF LVM_BUS_NAME".VdoPool"
#define DBUS_PROPS_IFACE "org.freedesktop.DBus.Properties"
#define DBUS_INTRO_IFACE "org.freedesktop.DBus.Introspectable"
#define METHOD_CALL_TIMEOUT 5000
#define PROGRESS_WAIT 500 * 1000 /* microseconds */
static GDBusConnection *bus = NULL;
/**
* SECTION: lvm
* @short_description: plugin for operations with LVM
* @title: LVM
* @include: lvm.h
*
* A plugin for operations with LVM. All sizes passed in/out to/from
* the functions are in bytes.
*/
/**
* bd_lvm_error_quark: (skip)
*/
GQuark bd_lvm_error_quark (void)
{
return g_quark_from_static_string ("g-bd-lvm-error-quark");
}
BDLVMPVdata* bd_lvm_pvdata_copy (BDLVMPVdata *data) {
if (data == NULL)
return NULL;
BDLVMPVdata *new_data = g_new0 (BDLVMPVdata, 1);
new_data->pv_name = g_strdup (data->pv_name);
new_data->pv_uuid = g_strdup (data->pv_uuid);
new_data->pv_free = data->pv_free;
new_data->pv_size = data->pv_size;
new_data->pe_start = data->pe_start;
new_data->vg_name = g_strdup (data->vg_name);
new_data->vg_uuid = g_strdup (data->vg_uuid);
new_data->vg_size = data->vg_size;
new_data->vg_free = data->vg_free;
new_data->vg_extent_size = data->vg_extent_size;
new_data->vg_extent_count = data->vg_extent_count;
new_data->vg_free_count = data->vg_free_count;
new_data->vg_pv_count = data->vg_pv_count;
new_data->pv_tags = g_strdupv (data->pv_tags);
return new_data;
}
void bd_lvm_pvdata_free (BDLVMPVdata *data) {
if (data == NULL)
return;
g_free (data->pv_name);
g_free (data->pv_uuid);
g_free (data->vg_name);
g_free (data->vg_uuid);
g_strfreev (data->pv_tags);
g_free (data);
}
BDLVMVGdata* bd_lvm_vgdata_copy (BDLVMVGdata *data) {
if (data == NULL)
return NULL;
BDLVMVGdata *new_data = g_new0 (BDLVMVGdata, 1);
new_data->name = g_strdup (data->name);
new_data->uuid = g_strdup (data->uuid);
new_data->size = data->size;
new_data->free = data->free;
new_data->extent_size = data->extent_size;
new_data->extent_count = data->extent_count;
new_data->free_count = data->free_count;
new_data->pv_count = data->pv_count;
new_data->vg_tags = g_strdupv (data->vg_tags);
return new_data;
}
void bd_lvm_vgdata_free (BDLVMVGdata *data) {
if (data == NULL)
return;
g_free (data->name);
g_free (data->uuid);
g_strfreev (data->vg_tags);
g_free (data);
}
BDLVMLVdata* bd_lvm_lvdata_copy (BDLVMLVdata *data) {
if (data == NULL)
return NULL;
BDLVMLVdata *new_data = g_new0 (BDLVMLVdata, 1);
new_data->lv_name = g_strdup (data->lv_name);
new_data->vg_name = g_strdup (data->vg_name);
new_data->uuid = g_strdup (data->uuid);
new_data->size = data->size;
new_data->attr = g_strdup (data->attr);
new_data->segtype = g_strdup (data->segtype);
new_data->origin = g_strdup (data->origin);
new_data->pool_lv = g_strdup (data->pool_lv);
new_data->data_lv = g_strdup (data->data_lv);
new_data->metadata_lv = g_strdup (data->metadata_lv);
new_data->roles = g_strdup (data->roles);
new_data->move_pv = g_strdup (data->move_pv);
new_data->data_percent = data->data_percent;
new_data->metadata_percent = data->metadata_percent;
new_data->copy_percent = data->copy_percent;
new_data->lv_tags = g_strdupv (data->lv_tags);
return new_data;
}
void bd_lvm_lvdata_free (BDLVMLVdata *data) {
if (data == NULL)
return;
g_free (data->lv_name);
g_free (data->vg_name);
g_free (data->uuid);
g_free (data->attr);
g_free (data->segtype);
g_free (data->origin);
g_free (data->pool_lv);
g_free (data->data_lv);
g_free (data->metadata_lv);
g_free (data->roles);
g_free (data->move_pv);
g_strfreev (data->lv_tags);
g_free (data);
}
BDLVMCacheStats* bd_lvm_cache_stats_copy (BDLVMCacheStats *data) {
if (data == NULL)
return NULL;
BDLVMCacheStats *new = g_new0 (BDLVMCacheStats, 1);
new->block_size = data->block_size;
new->cache_size = data->cache_size;
new->cache_used = data->cache_used;
new->md_block_size = data->md_block_size;
new->md_size = data->md_size;
new->md_used = data->md_used;
new->read_hits = data->read_hits;
new->read_misses = data->read_misses;
new->write_hits = data->write_hits;
new->write_misses = data->write_misses;
new->mode = data->mode;
return new;
}
void bd_lvm_cache_stats_free (BDLVMCacheStats *data) {
g_free (data);
}
static gboolean setup_dbus_connection (GError **error) {
gchar *addr = NULL;
addr = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, NULL, error);
if (!addr) {
g_prefix_error (error, "Failed to get system bus address: ");
return FALSE;
}
bus = g_dbus_connection_new_for_address_sync (addr,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT|
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, NULL, error);
g_free (addr);
if (!bus) {
g_prefix_error (error, "Failed to create a new connection for the system bus: ");
return FALSE;
}
if (g_dbus_connection_is_closed (bus)) {
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
"Connection is closed");
return FALSE;
}
g_dbus_connection_set_exit_on_close (bus, FALSE);
return TRUE;
}
static volatile guint avail_deps = 0;
static volatile guint avail_dbus_deps = 0;
static volatile guint avail_features = 0;
static volatile guint avail_module_deps = 0;
static GMutex deps_check_lock;
#define DEPS_LVM 0
#define DEPS_LVM_MASK (1 << DEPS_LVM)
#define DEPS_LVMDEVICES 1
#define DEPS_LVMDEVICES_MASK (1 << DEPS_LVMDEVICES)
#define DEPS_LVMCONFIG 2
#define DEPS_LVMCONFIG_MASK (1 << DEPS_LVMCONFIG)
#define DEPS_LAST 3
static const UtilDep deps[DEPS_LAST] = {
{"lvm", LVM_MIN_VERSION, "version", "LVM version:\\s+([\\d\\.]+)"},
{"lvmdevices", NULL, NULL, NULL},
{"lvmconfig", "2.03.17", "--version", "LVM version:\\s+([\\d\\.]+)"},
};
#define DBUS_DEPS_LVMDBUSD 0
#define DBUS_DEPS_LVMDBUSD_MASK (1 << DBUS_DEPS_LVMDBUSD)
#define DBUS_DEPS_LVMDBUSD_WRITECACHE 1
#define DBUS_DEPS_LVMDBUSD_WRITECACHE_MASK (1 << DBUS_DEPS_LVMDBUSD_WRITECACHE)
#define DBUS_DEPS_LAST 2
static const DBusDep dbus_deps[DBUS_DEPS_LAST] = {
{LVM_BUS_NAME, LVM_OBJ_PREFIX, G_BUS_TYPE_SYSTEM, NULL, NULL, NULL, NULL},
{LVM_BUS_NAME, LVM_OBJ_PREFIX, G_BUS_TYPE_SYSTEM, "1.1.0", "Version", MANAGER_INTF, MANAGER_OBJ},
};
#define FEATURES_VDO 0
#define FEATURES_VDO_MASK (1 << FEATURES_VDO)
#define FEATURES_WRITECACHE 0
#define FEATURES_WRITECACHE_MASK (1 << FEATURES_WRITECACHE)
#define FEATURES_LAST 2
static const UtilFeatureDep features[FEATURES_LAST] = {
{"lvm", "vdo", "segtypes", NULL},
{"lvm", "writecache", "segtypes", NULL},
};
#define MODULE_DEPS_VDO 0
#define MODULE_DEPS_VDO_MASK (1 << MODULE_DEPS_VDO)
#define MODULE_DEPS_LAST 1
static const gchar*const module_deps[MODULE_DEPS_LAST] = { "dm-vdo" };
/**
* bd_lvm_init:
*
* Initializes the plugin. **This function is called automatically by the
* library's initialization functions.**
*
*/
gboolean bd_lvm_init (void) {
GError *error = NULL;
/* the check() call should create the DBus connection for us, but let's not
completely rely on it */
if (G_UNLIKELY (!bus) && !setup_dbus_connection (&error)) {
bd_utils_log_format (BD_UTILS_LOG_CRIT, "Failed to setup DBus connection: %s", error->message);
g_clear_error (&error);
return FALSE;
}
dm_log_with_errno_init ((dm_log_with_errno_fn) redirect_dm_log);
#ifdef DEBUG
dm_log_init_verbose (LOG_DEBUG);
#else
dm_log_init_verbose (LOG_INFO);
#endif
return TRUE;
}
/**
* bd_lvm_close:
*
* Cleans up after the plugin. **This function is called automatically by the
* library's functions that unload it.**
*
*/
void bd_lvm_close (void) {
GError *error = NULL;
/* the check() call should create the DBus connection for us, but let's not
completely rely on it */
if (!g_dbus_connection_flush_sync (bus, NULL, &error)) {
bd_utils_log_format (BD_UTILS_LOG_CRIT, "Failed to flush DBus connection: %s", error->message);
g_clear_error (&error);
}
if (!g_dbus_connection_close_sync (bus, NULL, &error)) {
bd_utils_log_format (BD_UTILS_LOG_CRIT, "Failed to close DBus connection: %s", error->message);
g_clear_error (&error);
}
dm_log_with_errno_init (NULL);
dm_log_init_verbose (0);
}
/**
* bd_lvm_is_tech_avail:
* @tech: the queried tech
* @mode: a bit mask of queried modes of operation (#BDLVMTechMode) for @tech
* @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
*
* Returns: whether the @tech-@mode combination is available -- supported by the
* plugin implementation and having all the runtime dependencies available
*/
gboolean bd_lvm_is_tech_avail (BDLVMTech tech, guint64 mode, GError **error) {
switch (tech) {
case BD_LVM_TECH_THIN_CALCS:
if (mode & ~BD_LVM_TECH_MODE_QUERY) {
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_TECH_UNAVAIL,
"Only 'query' supported for thin calculations");
return FALSE;
} else
return TRUE;
case BD_LVM_TECH_CALCS:
if (mode & ~BD_LVM_TECH_MODE_QUERY) {
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_TECH_UNAVAIL,
"Only 'query' supported for calculations");
return FALSE;
} else
return TRUE;
case BD_LVM_TECH_VDO:
return check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error) &&
check_features (&avail_features, FEATURES_VDO_MASK, features, FEATURES_LAST, &deps_check_lock, error) &&
check_module_deps (&avail_module_deps, MODULE_DEPS_VDO_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error);
case BD_LVM_TECH_WRITECACHE:
return check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK|DBUS_DEPS_LVMDBUSD_WRITECACHE_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error) &&
check_features (&avail_features, FEATURES_WRITECACHE_MASK, features, FEATURES_LAST, &deps_check_lock, error);
case BD_LVM_TECH_DEVICES:
return check_deps (&avail_deps, DEPS_LVMDEVICES_MASK, deps, DEPS_LAST, &deps_check_lock, error);
case BD_LVM_TECH_CONFIG:
return check_deps (&avail_deps, DEPS_LVMCONFIG_MASK, deps, DEPS_LAST, &deps_check_lock, error);
default:
/* everything is supported by this implementation of the plugin */
return check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error);
}
}
static gchar** get_existing_objects (const gchar *obj_prefix, GError **error) {
GVariant *intro_v = NULL;
gchar *intro_data = NULL;
GDBusNodeInfo *info = NULL;
gchar **ret = NULL;
GDBusNodeInfo **nodes;
guint64 n_nodes = 0;
guint64 i = 0;
intro_v = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj_prefix, DBUS_INTRO_IFACE,
"Introspect", NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, error);
if (!intro_v)
/* no introspection data, something went wrong (error must be set) */
return NULL;
g_variant_get (intro_v, "(s)", &intro_data);
g_variant_unref (intro_v);
info = g_dbus_node_info_new_for_xml (intro_data, error);
g_free (intro_data);
for (nodes = info->nodes; (*nodes); nodes++)
n_nodes++;
ret = g_new0 (gchar*, n_nodes + 1);
for (nodes = info->nodes, i=0; (*nodes); nodes++, i++) {
ret[i] = g_strdup_printf ("%s/%s", obj_prefix, ((*nodes)->path));
}
ret[i] = NULL;
g_dbus_node_info_unref (info);
return ret;
}
/**
* get_object_path:
* @obj_id: get object path for an LVM object (vgname/lvname)
* @error: (out) (optional): place to store error (if any)
*
* Returns: (transfer full): object path
*/
static gchar* get_object_path (const gchar *obj_id, GError **error) {
GVariant *args = NULL;
GVariant *ret = NULL;
gchar *obj_path = NULL;
args = g_variant_new ("(s)", obj_id);
/* consumes (frees) the 'args' parameter */
ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, MANAGER_OBJ, MANAGER_INTF,
"LookUpByLvmId", args, NULL, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, error);
if (!ret)
/* error is already set */
return NULL;
g_variant_get (ret, "(o)", &obj_path);
g_variant_unref (ret);
if (g_strcmp0 (obj_path, "/") == 0) {
/* not a valid path (at least for us) */
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
"The object with LVM ID '%s' doesn't exist", obj_id);
g_free (obj_path);
return NULL;
}
return obj_path;
}
/**
* get_object_property:
* @obj_path: lvmdbusd object path
* @iface: interface on @obj_path object
* @property: property to get from @obj_path and @iface
* @error: (out) (optional): place to store error (if any)
*
* Returns: (transfer full): object path
*/
static GVariant* get_object_property (const gchar *obj_path, const gchar *iface, const gchar *property, GError **error) {
GVariant *args = NULL;
GVariant *ret = NULL;
GVariant *real_ret = NULL;
args = g_variant_new ("(ss)", iface, property);
/* consumes (frees) the 'args' parameter */
ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj_path, DBUS_PROPS_IFACE,
"Get", args, NULL, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, error);
if (!ret) {
g_prefix_error (error, "Failed to get %s property of the %s object: ", property, obj_path);
return NULL;
}
g_variant_get (ret, "(v)", &real_ret);
g_variant_unref (ret);
return real_ret;
}
/**
* get_lvm_object_property:
* @obj_id: LVM object to get the property for (vgname/lvname)
* @iface: interface where @property is defined
* @property: property to get from @obj_id and @iface
* @error: (out) (optional): place to store error (if any)
*
* Returns: (transfer full): property variant
*/
static GVariant* get_lvm_object_property (const gchar *obj_id, const gchar *iface, const gchar *property, GError **error) {
gchar *obj_path = NULL;
GVariant *ret = NULL;
obj_path = get_object_path (obj_id, error);
if (!obj_path)
/* error is already set */
return NULL;
else {
ret = get_object_property (obj_path, iface, property, error);
g_free (obj_path);
return ret;
}
}
static gboolean unbox_params_and_add (GVariant *params, GVariantBuilder *builder) {
GVariantIter iter;
GVariant *param = NULL;
gboolean ret = FALSE;
if (g_variant_is_of_type (params, G_VARIANT_TYPE_DICTIONARY)) {
g_variant_iter_init (&iter, params);
while ((param = g_variant_iter_next_value (&iter))) {
g_variant_builder_add_value (builder, param);
ret = TRUE;
}
return ret;
}
if (g_variant_is_of_type (params, G_VARIANT_TYPE_VARIANT)) {
param = g_variant_get_variant (params);
return unbox_params_and_add (param, builder);
}
if (g_variant_is_container (params)) {
g_variant_iter_init (&iter, params);
while ((param = g_variant_iter_next_value (&iter)))
ret = unbox_params_and_add (param, builder);
return ret;
}
return FALSE;
}
/**
* call_lvm_method
* @obj: lvmdbusd object path
* @intf: interface to call @method on
* @method: method to call
* @params: parameters for @method
* @extra_params: extra parameters for @method
* @extra_args: extra command line argument to be passed to the LVM command
* @task_id: (out): task ID to watch progress of the operation
* @progress_id: (out): progress ID to watch progress of the operation
* @lock_config: whether to lock %global_config_lock or not (if %FALSE is given, caller is responsible
* for holding the lock for this call)
* @error: (out) (optional): place to store error (if any)
*
* Returns: (transfer full): return value of @method (variant)
*/
static GVariant* call_lvm_method (const gchar *obj, const gchar *intf, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, guint64 *task_id, guint64 *progress_id, gboolean lock_config, GError **error) {
GVariant *config = NULL;
GVariant *devices = NULL;
GVariant *param = NULL;
GVariantIter iter;
GVariantBuilder builder;
GVariantBuilder extra_builder;
GVariant *config_extra_params = NULL;
GVariant *tmo = NULL;
GVariant *all_params = NULL;
GVariant *ret = NULL;
gchar *params_str = NULL;
gchar *log_msg = NULL;
gchar *prog_msg = NULL;
const BDExtraArg **extra_p = NULL;
gboolean added_extra = FALSE;
if (!check_dbus_deps (&avail_dbus_deps, DBUS_DEPS_LVMDBUSD_MASK, dbus_deps, DBUS_DEPS_LAST, &deps_check_lock, error))
return NULL;
/* don't allow global config string changes during the run */
if (lock_config)
g_mutex_lock (&global_config_lock);
if (global_config_str || global_devices_str || extra_params || extra_args) {
if (global_config_str || global_devices_str || extra_args) {
/* add the global config to the extra_params */
g_variant_builder_init (&extra_builder, G_VARIANT_TYPE_DICTIONARY);
if (extra_params)
added_extra = unbox_params_and_add (extra_params, &extra_builder);
if (extra_args) {
for (extra_p=extra_args; *extra_p; extra_p++) {
g_variant_builder_add (&extra_builder, "{sv}",
(*extra_p)->opt ? (*extra_p)->opt : "",
g_variant_new ("s",
(*extra_p)->val ? (*extra_p)->val : ""));
added_extra = TRUE;
}
}
if (global_config_str) {
config = g_variant_new ("s", global_config_str);
g_variant_builder_add (&extra_builder, "{sv}", "--config", config);
added_extra = TRUE;
}
if (global_devices_str) {
devices = g_variant_new ("s", global_devices_str);
g_variant_builder_add (&extra_builder, "{sv}", "--devices", devices);
added_extra = TRUE;
}
if (added_extra)
config_extra_params = g_variant_builder_end (&extra_builder);
g_variant_builder_clear (&extra_builder);
} else
/* just use the extra_params */
config_extra_params = extra_params;
}
if (!config_extra_params)
/* create an empty dictionary with the extra arguments */
config_extra_params = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
/* create new GVariant holding the given parameters with the global
config and extra_params merged together appended */
g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
if (params) {
/* add parameters */
g_variant_iter_init (&iter, params);
while ((param = g_variant_iter_next_value (&iter))) {
g_variant_builder_add_value (&builder, param);
g_variant_unref (param);
}
}
/* add the timeout spec (in seconds) */
tmo = g_variant_new ("i", 1);
g_variant_builder_add_value (&builder, tmo);
/* add extra parameters including config */
g_variant_builder_add_value (&builder, config_extra_params);
all_params = g_variant_builder_end (&builder);
g_variant_builder_clear (&builder);
params_str = g_variant_print (all_params, FALSE);
*task_id = bd_utils_get_next_task_id ();
log_msg = g_strdup_printf ("Calling the '%s.%s' method on the '%s' object with the following parameters: '%s'",
intf, method, obj, params_str);
bd_utils_log_task_status (*task_id, log_msg);
g_free (log_msg);
/* now do the call with all the parameters */
ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj, intf, method, all_params,
NULL, G_DBUS_CALL_FLAGS_NONE, METHOD_CALL_TIMEOUT, NULL, error);
if (lock_config)
g_mutex_unlock (&global_config_lock);
prog_msg = g_strdup_printf ("Started the '%s.%s' method on the '%s' object with the following parameters: '%s'",
intf, method, obj, params_str);
g_free (params_str);
*progress_id = bd_utils_report_started (prog_msg);
g_free (prog_msg);
if (!ret) {
g_prefix_error (error, "Failed to call the '%s' method on the '%s' object: ", method, obj);
return NULL;
}
return ret;
}
/**
* call_lvm_method_sync
* @obj: lvmdbusd object path
* @intf: interface to call @method on
* @method: method to call
* @params: parameters for @method
* @extra_params: extra parameters for @method
* @extra_args: extra command line argument to be passed to the LVM command
* @lock_config: whether to lock %global_config_lock or not (if %FALSE is given, caller is responsible
* for holding the lock for this call)
* @error: (out) (optional): place to store error (if any)
*
* Returns: whether calling the method was successful or not
*/
static gboolean call_lvm_method_sync (const gchar *obj, const gchar *intf, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
GVariant *ret = NULL;
gchar *obj_path = NULL;
gchar *task_path = NULL;
guint64 log_task_id = 0;
guint64 prog_id = 0;
gdouble progress = 0.0;
gchar *log_msg = NULL;
gboolean completed = FALSE;
gint64 error_code = 0;
gchar *error_msg = NULL;
GError *l_error = NULL;
ret = call_lvm_method (obj, intf, method, params, extra_params, extra_args, &log_task_id, &prog_id, lock_config, &l_error);
bd_utils_log_task_status (log_task_id, "Done.");
if (!ret) {
if (l_error) {
log_msg = g_strdup_printf ("Got error: %s", l_error->message);
bd_utils_log_task_status (log_task_id, log_msg);
bd_utils_report_finished (prog_id, log_msg);
g_free (log_msg);
g_propagate_error (error, l_error);
} else {
bd_utils_log_task_status (log_task_id, "Got unknown error");
bd_utils_report_finished (prog_id, "Got unknown error");
}
return FALSE;
}
if (g_variant_check_format_string (ret, "((oo))", TRUE)) {
g_variant_get (ret, "((oo))", &obj_path, &task_path);
if (g_strcmp0 (obj_path, "/") != 0) {
log_msg = g_strdup_printf ("Got result: %s", obj_path);
bd_utils_log_task_status (log_task_id, log_msg);
g_free (log_msg);
/* got a valid result, just return */
g_variant_unref (ret);
g_free (task_path);
g_free (obj_path);
bd_utils_report_finished (prog_id, "Completed");
return TRUE;
} else {
g_variant_unref (ret);
g_free (obj_path);
if (g_strcmp0 (task_path, "/") == 0) {
log_msg = g_strdup_printf ("Task finished without result and without job started");
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
"Running '%s' method on the '%s' object failed: %s",
method, obj, log_msg);
bd_utils_log_task_status (log_task_id, log_msg);
bd_utils_report_finished (prog_id, log_msg);
g_free (task_path);
g_free (log_msg);
return FALSE;
}
}
} else if (g_variant_check_format_string (ret, "(o)", TRUE)) {
g_variant_get (ret, "(o)", &task_path);
if (g_strcmp0 (task_path, "/") != 0) {
g_variant_unref (ret);
} else {
bd_utils_log_task_status (log_task_id, "No result, no job started");
g_free (task_path);
bd_utils_report_finished (prog_id, "Completed");
g_variant_unref (ret);
return TRUE;
}
} else {
g_variant_unref (ret);
bd_utils_log_task_status (log_task_id, "Failed to parse the returned value!");
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_PARSE,
"Failed to parse the returned value!");
bd_utils_report_finished (prog_id, "Failed to parse the returned value!");
return FALSE;
}
log_msg = g_strdup_printf ("Waiting for job '%s' to finish", task_path);
bd_utils_log_task_status (log_task_id, log_msg);
g_free (log_msg);
ret = NULL;
while (!completed && !l_error) {
g_usleep (PROGRESS_WAIT);
ret = get_object_property (task_path, JOB_INTF, "Complete", &l_error);
if (ret) {
g_variant_get (ret, "b", &completed);
g_variant_unref (ret);
ret = NULL;
}
if (!completed && !l_error) {
/* let's report progress and wait longer */
ret = get_object_property (task_path, JOB_INTF, "Percent", &l_error);
if (ret) {
g_variant_get (ret, "d", &progress);
bd_utils_report_progress (prog_id, (gint) progress, NULL);
g_variant_unref (ret);
ret = NULL;
} else {
bd_utils_log_format (BD_UTILS_LOG_DEBUG, "Got error when getting progress: %s", l_error->message);
g_clear_error (&l_error);
}
log_msg = g_strdup_printf ("Still waiting for job '%s' to finish", task_path);
bd_utils_log_task_status (log_task_id, log_msg);
g_free (log_msg);
}
}
log_msg = g_strdup_printf ("Job '%s' finished", task_path);
bd_utils_log_task_status (log_task_id, log_msg);
g_free (log_msg);
obj_path = NULL;
if (!l_error) {
ret = get_object_property (task_path, JOB_INTF, "Result", &l_error);
if (!ret) {
g_prefix_error (&l_error, "Getting result after waiting for '%s' method of the '%s' object failed: ",
method, obj);
bd_utils_report_finished (prog_id, l_error->message);
g_propagate_error (error, l_error);
g_free (task_path);
return FALSE;
} else {
g_variant_get (ret, "o", &obj_path);
g_variant_unref (ret);
if (g_strcmp0 (obj_path, "/") != 0) {
log_msg = g_strdup_printf ("Got result: %s", obj_path);
bd_utils_log_task_status (log_task_id, log_msg);
g_free (log_msg);
} else {
ret = get_object_property (task_path, JOB_INTF, "GetError", &l_error);
g_variant_get (ret, "(is)", &error_code, &error_msg);
if (error_code != 0) {
if (error_msg) {
log_msg = g_strdup_printf ("Got error: %s", error_msg);
bd_utils_log_task_status (log_task_id, log_msg);
bd_utils_report_finished (prog_id, log_msg);
g_set_error (&l_error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
"Running '%s' method on the '%s' object failed: %s",
method, obj, error_msg);
g_free (log_msg);
g_free (error_msg);
} else {
bd_utils_log_task_status (log_task_id, "Got unknown error");
bd_utils_report_finished (prog_id, "Got unknown error");
g_set_error (&l_error, BD_LVM_ERROR, BD_LVM_ERROR_FAIL,
"Got unknown error when running '%s' method on the '%s' object.",
method, obj);
}
g_free (task_path);
g_propagate_error (error, l_error);
return FALSE;
} else
bd_utils_log_task_status (log_task_id, "No result");
}
bd_utils_report_finished (prog_id, "Completed");
g_free (obj_path);
/* remove the job object and clean after ourselves */
ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, task_path, JOB_INTF, "Remove", NULL,
NULL, G_DBUS_CALL_FLAGS_NONE, METHOD_CALL_TIMEOUT, NULL, NULL);
if (ret)
g_variant_unref (ret);
g_free (task_path);
return TRUE;
}
} else {
/* some real error */
g_prefix_error (&l_error, "Waiting for '%s' method of the '%s' object to finish failed: ",
method, obj);
g_propagate_error (error, l_error);
bd_utils_report_finished (prog_id, "Completed");
return FALSE;
}
g_free (task_path);
return TRUE;
}
static gboolean call_lvm_obj_method_sync (const gchar *obj_id, const gchar *intf, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
g_autofree gchar *obj_path = get_object_path (obj_id, error);
if (!obj_path)
return FALSE;
return call_lvm_method_sync (obj_path, intf, method, params, extra_params, extra_args, lock_config, error);
}
static gboolean call_lv_method_sync (const gchar *vg_name, const gchar *lv_name, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
g_autofree gchar *obj_id = g_strdup_printf ("%s/%s", vg_name, lv_name);
return call_lvm_obj_method_sync (obj_id, LV_INTF, method, params, extra_params, extra_args, lock_config, error);
}
static gboolean call_thpool_method_sync (const gchar *vg_name, const gchar *pool_name, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
g_autofree gchar *obj_id = g_strdup_printf ("%s/%s", vg_name, pool_name);
return call_lvm_obj_method_sync (obj_id, THPOOL_INTF, method, params, extra_params, extra_args, lock_config, error);
}
static gboolean call_vdopool_method_sync (const gchar *vg_name, const gchar *pool_name, const gchar *method, GVariant *params, GVariant *extra_params, const BDExtraArg **extra_args, gboolean lock_config, GError **error) {
g_autofree gchar *obj_id = g_strdup_printf ("%s/%s", vg_name, pool_name);
return call_lvm_obj_method_sync (obj_id, VDO_POOL_INTF, method, params, extra_params, extra_args, lock_config, error);
}
static GVariant* get_lv_property (const gchar *vg_name, const gchar *lv_name, const gchar *property, GError **error) {
gchar *lv_spec = NULL;
GVariant *ret = NULL;
lv_spec = g_strdup_printf ("%s/%s", vg_name, lv_name);
ret = get_lvm_object_property (lv_spec, LV_CMN_INTF, property, error);
g_free (lv_spec);
return ret;
}
static GVariant* get_object_properties (const gchar *obj_path, const gchar *iface, GError **error) {
GVariant *args = NULL;
GVariant *ret = NULL;
GVariant *real_ret = NULL;
args = g_variant_new ("(s)", iface);
/* consumes (frees) the 'args' parameter */
ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, obj_path, DBUS_PROPS_IFACE,
"GetAll", args, NULL, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, error);
if (!ret) {
g_prefix_error (error, "Failed to get properties of the %s object: ", obj_path);
return NULL;
}
real_ret = g_variant_get_child_value (ret, 0);
g_variant_unref (ret);
return real_ret;
}
static GVariant* get_lvm_object_properties (const gchar *obj_id, const gchar *iface, GError **error) {
GVariant *args = NULL;
GVariant *ret = NULL;
gchar *obj_path = NULL;
args = g_variant_new ("(s)", obj_id);
/* consumes (frees) the 'args' parameter */
ret = g_dbus_connection_call_sync (bus, LVM_BUS_NAME, MANAGER_OBJ, MANAGER_INTF,
"LookUpByLvmId", args, NULL, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, error);
g_variant_get (ret, "(o)", &obj_path);
g_variant_unref (ret);
if (g_strcmp0 (obj_path, "/") == 0) {
g_set_error (error, BD_LVM_ERROR, BD_LVM_ERROR_NOEXIST,
"The object with LVM ID '%s' doesn't exist", obj_id);
g_free (obj_path);
return NULL;
}
ret = get_object_properties (obj_path, iface, error);
g_free (obj_path);
return ret;
}
static GVariant* get_pv_properties (const gchar *pv_name, GError **error) {
gchar *obj_id = NULL;
GVariant *ret = NULL;
if (!g_str_has_prefix (pv_name, "/dev/")) {
obj_id = g_strdup_printf ("/dev/%s", pv_name);
ret = get_lvm_object_properties (obj_id, PV_INTF, error);
g_free (obj_id);