Debian Codegraph @

modules/lvm2/udiskslinuxvdovolume.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2019 Vojtech Trefny <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "config.h"
22 #include <fcntl.h>
23 #include <glib/gi18n-lib.h>
24
25 #include <blockdev/lvm.h>
26
27 #include <src/udiskslogging.h>
28 #include <src/udiskslinuxblockobject.h>
29 #include <src/udisksdaemon.h>
30 #include <src/udisksstate.h>
31 #include <src/udisksdaemonutil.h>
32 #include <src/udiskslinuxdevice.h>
33 #include <src/udiskslinuxblock.h>
34
35 #include "udiskslinuxvdovolume.h"
36 #include "udiskslinuxlogicalvolume.h"
37 #include "udiskslinuxlogicalvolumeobject.h"
38 #include "udiskslinuxvolumegroup.h"
39 #include "udiskslinuxvolumegroupobject.h"
40 #include "udiskslinuxmodulelvm2.h"
41
42 #include "udiskslvm2daemonutil.h"
43 #include "jobhelpers.h"
44
45 /**
46 * SECTION:udisklinuxvdovolume
47 * @title: UDisksLinuxVDOVolume
48 * @short_description: Linux implementation of #UDisksVDOVolume
49 *
50 * This type provides an implementation of the #UDisksVDOVolume
51 * interface on Linux.
52 */
53
54 typedef struct _UDisksLinuxVDOVolumeClass UDisksLinuxVDOVolumeClass;
55
56 /**
57 * UDisksLinuxVDOVolume:
58 *
59 * The #UDisksLinuxVDOVolume structure contains only private data and should
60 * only be accessed using the provided API.
61 */
62 struct _UDisksLinuxVDOVolume
63 {
64 UDisksVDOVolumeSkeleton parent_instance;
65 };
66
67 struct _UDisksLinuxVDOVolumeClass
68 {
69 UDisksVDOVolumeSkeletonClass parent_class;
70 };
71
72 static void vdo_volume_iface_init (UDisksVDOVolumeIface *iface);
73
74 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxVDOVolume, udisks_linux_vdo_volume, UDISKS_TYPE_VDO_VOLUME_SKELETON,
75 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_VDO_VOLUME, vdo_volume_iface_init));
76
77 /* ---------------------------------------------------------------------------------------------------- */
78
79 static void
80 udisks_linux_vdo_volume_init (UDisksLinuxVDOVolume *vdo_volume)
81 {
82 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (vdo_volume),
83 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
84 }
85
86 static void
87 udisks_linux_vdo_volume_constructed (GObject *_object)
88 {
89 if (G_OBJECT_CLASS (udisks_linux_vdo_volume_parent_class)->constructed != NULL)
90 G_OBJECT_CLASS (udisks_linux_vdo_volume_parent_class)->constructed (_object);
91 }
92
93 static void
94 udisks_linux_vdo_volume_finalize (GObject *_object)
95 {
96 if (G_OBJECT_CLASS (udisks_linux_vdo_volume_parent_class)->finalize != NULL)
97 G_OBJECT_CLASS (udisks_linux_vdo_volume_parent_class)->finalize (_object);
98 }
99
100 static void
101 udisks_linux_vdo_volume_class_init (UDisksLinuxVDOVolumeClass *klass)
102 {
103 GObjectClass *gobject_class;
104
105 gobject_class = G_OBJECT_CLASS (klass);
106 gobject_class->finalize = udisks_linux_vdo_volume_finalize;
107 gobject_class->constructed = udisks_linux_vdo_volume_constructed;
108 }
109
110 /**
111 * udisks_linux_vdo_volume_new:
112 *
113 * Creates a new #UDisksLinuxVDOVolume instance.
114 *
115 * Returns: A new #UDisksLinuxVDOVolume. Free with g_object_unref().
116 */
117 UDisksVDOVolume *
118 udisks_linux_vdo_volume_new (void)
119 {
120 return UDISKS_VDO_VOLUME (g_object_new (UDISKS_TYPE_LINUX_VDO_VOLUME,
121 NULL));
122 }
123
124 /* ---------------------------------------------------------------------------------------------------- */
125
126 void
127 udisks_linux_vdo_volume_update (UDisksLinuxVDOVolume *vdo_volume,
128 UDisksLinuxVolumeGroupObject *group_object,
129 BDLVMLVdata *lv_info,
130 BDLVMVDOPooldata *vdo_info)
131 {
132 UDisksVDOVolume *iface;
133 UDisksLinuxLogicalVolumeObject *pool_object = NULL;
134 const gchar *value = NULL;
135 const char *pool_objpath;
136 GError *loc_error = NULL;
137
138 iface = UDISKS_VDO_VOLUME (vdo_volume);
139
140 pool_objpath = "/";
141 if (lv_info->pool_lv)
142 {
143 pool_object = udisks_linux_volume_group_object_find_logical_volume_object (group_object, lv_info->pool_lv);
144 if (pool_object)
145 pool_objpath = g_dbus_object_get_object_path (G_DBUS_OBJECT (pool_object));
146 }
147 udisks_vdo_volume_set_vdo_pool (iface, pool_objpath);
148
149 value = bd_lvm_get_vdo_operating_mode_str (vdo_info->operating_mode, &loc_error);
150 if (!value)
151 {
152 g_clear_error (&loc_error);
153 udisks_vdo_volume_set_operating_mode (iface, "");
154 }
155 else
156 udisks_vdo_volume_set_operating_mode (iface, value);
157
158 value = bd_lvm_get_vdo_compression_state_str (vdo_info->compression_state, &loc_error);
159 if (!value)
160 {
161 g_clear_error (&loc_error);
162 udisks_vdo_volume_set_compression_state (iface, "");
163 }
164 else
165 udisks_vdo_volume_set_compression_state (iface, value);
166
167 value = bd_lvm_get_vdo_index_state_str (vdo_info->index_state, &loc_error);
168 if (!value)
169 {
170 g_clear_error (&loc_error);
171 udisks_vdo_volume_set_index_state (iface, "");
172 }
173 else
174 udisks_vdo_volume_set_index_state (iface, value);
175
176 udisks_vdo_volume_set_used_size (iface, vdo_info->used_size);
177
178 udisks_vdo_volume_set_compression (iface, vdo_info->compression);
179 udisks_vdo_volume_set_deduplication (iface, vdo_info->deduplication);
180
181 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (iface));
182 }
183
184 /* ---------------------------------------------------------------------------------------------------- */
185
186 static gboolean
187 common_setup (UDisksLinuxLogicalVolumeObject *object,
188 GDBusMethodInvocation *invocation,
189 GVariant *options,
190 const gchar *auth_err_msg,
191 UDisksLinuxModuleLVM2 **module,
192 UDisksDaemon **daemon,
193 uid_t *out_uid)
194 {
195 gboolean rc = FALSE;
196 GError *error = NULL;
197
198 *module = udisks_linux_logical_volume_object_get_module (object);
199 *daemon = udisks_module_get_daemon (UDISKS_MODULE (*module));
200
201 if (!udisks_daemon_util_get_caller_uid_sync (*daemon,
202 invocation,
203 NULL /* GCancellable */,
204 out_uid,
205 &error))
206 {
207 g_dbus_method_invocation_return_gerror (invocation, error);
208 g_clear_error (&error);
209 goto out;
210 }
211
212 /* Policy check. */
213 UDISKS_DAEMON_CHECK_AUTHORIZATION (*daemon,
214 UDISKS_OBJECT (object),
215 LVM2_POLICY_ACTION_ID,
216 options,
217 auth_err_msg,
218 invocation);
219 rc = TRUE;
220 out:
221 return rc;
222 }
223
224 static gboolean
225 _set_compression_deduplication (UDisksVDOVolume *_volume,
226 GDBusMethodInvocation *invocation,
227 gboolean enable,
228 gboolean compression,
229 gboolean deduplication,
230 GVariant *options)
231 {
232 GError *error = NULL;
233 UDisksLinuxVDOVolume *volume = UDISKS_LINUX_VDO_VOLUME (_volume);
234 UDisksLinuxLogicalVolumeObject *object = NULL;
235 UDisksLinuxModuleLVM2 *module = NULL;
236 UDisksDaemon *daemon = NULL;
237 uid_t caller_uid;
238 UDisksLinuxVolumeGroupObject *group_object = NULL;
239 LVJobData data = {0,};
240
241 object = udisks_daemon_util_dup_object (volume, &error);
242 if (object == NULL)
243 {
244 g_dbus_method_invocation_take_error (invocation, error);
245 goto out;
246 }
247
248 if (!common_setup (object, invocation, options,
249 N_("Authentication is required to set deduplication/compression on a VDO volume"),
250 &module, &daemon, &caller_uid))
251 goto out;
252
253 group_object = udisks_linux_logical_volume_object_get_volume_group (object);
254 data.vg_name = udisks_linux_volume_group_object_get_name (group_object);
255 data.lv_name = udisks_linux_logical_volume_object_get_name (object);
256 if (compression)
257 data.compression = enable;
258 else if (deduplication)
259 data.deduplication = enable;
260
261 if (!udisks_daemon_launch_threaded_job_sync (daemon,
262 UDISKS_OBJECT (object),
263 "lvm-vdo-dedup-comp",
264 caller_uid,
265 FALSE,
266 compression ? lv_vdo_compression_job_func : lv_vdo_deduplication_job_func,
267 &data,
268 NULL, /* user_data_free_func */
269 NULL, /* GCancellable */
270 &error))
271 {
272 g_dbus_method_invocation_return_error (invocation,
273 UDISKS_ERROR,
274 UDISKS_ERROR_FAILED,
275 "Error setting deduplication/compression on the VDO volume: %s",
276 error->message);
277 g_clear_error (&error);
278 goto out;
279 }
280
281 if (compression)
282 udisks_vdo_volume_complete_enable_compression (_volume, invocation);
283 else if (deduplication)
284 udisks_vdo_volume_complete_enable_deduplication (_volume, invocation);
285
286 out:
287 g_clear_object (&object);
288 return TRUE;
289 }
290
291 static gboolean
292 handle_enable_compression (UDisksVDOVolume *_volume,
293 GDBusMethodInvocation *invocation,
294 gboolean enable,
295 GVariant *options)
296 {
297 return _set_compression_deduplication (_volume, invocation, enable,
298 TRUE /* compression */, FALSE /* deduplication */, options);
299 }
300
301 static gboolean
302 handle_enable_deduplication (UDisksVDOVolume *_volume,
303 GDBusMethodInvocation *invocation,
304 gboolean enable,
305 GVariant *options)
306 {
307 return _set_compression_deduplication (_volume, invocation, enable,
308 FALSE /* compression */, TRUE /* deduplication */, options);
309 }
310
311 static gboolean
312 _vdo_resize (UDisksLinuxLogicalVolumeObject *object,
313 GDBusMethodInvocation *invocation,
314 guint64 new_size,
315 GVariant *options)
316 {
317 GError *error = NULL;
318 UDisksLinuxModuleLVM2 *module = NULL;
319 UDisksDaemon *daemon = NULL;
320 UDisksLinuxVolumeGroupObject *group_object = NULL;
321 uid_t caller_uid;
322 LVJobData data = {0,};
323 gboolean success = FALSE;
324
325 if (!common_setup (object, invocation, options,
326 N_("Authentication is required to resize a VDO volume"),
327 &module, &daemon, &caller_uid))
328 goto out;
329
330 group_object = udisks_linux_logical_volume_object_get_volume_group (object);
331 data.vg_name = udisks_linux_volume_group_object_get_name (group_object);
332 data.lv_name = udisks_linux_logical_volume_object_get_name (object);
333 data.new_lv_size = new_size;
334
335 data.resize_fs = FALSE;
336 data.force = FALSE;
337 g_variant_lookup (options, "resize_fsys", "b", &(data.resize_fs));
338 g_variant_lookup (options, "force", "b", &(data.force));
339
340 if (!udisks_daemon_launch_threaded_job_sync (daemon,
341 UDISKS_OBJECT (object),
342 "lvm-lvol-resize",
343 caller_uid,
344 FALSE,
345 lvresize_job_func,
346 &data,
347 NULL, /* user_data_free_func */
348 NULL, /* GCancellable */
349 &error))
350 {
351 g_dbus_method_invocation_return_error (invocation,
352 UDISKS_ERROR,
353 UDISKS_ERROR_FAILED,
354 "Error resizing VDO volume: %s",
355 error->message);
356 g_clear_error (&error);
357 goto out;
358 }
359
360 success = TRUE;
361
362 out:
363 return success;
364 }
365
366 static gboolean
367 handle_resize_logical (UDisksVDOVolume *_volume,
368 GDBusMethodInvocation *invocation,
369 guint64 new_size,
370 GVariant *options)
371 {
372 UDisksLinuxLogicalVolumeObject *object = NULL;
373 GError *error = NULL;
374
375 object = udisks_daemon_util_dup_object (_volume, &error);
376 if (object == NULL)
377 {
378 g_dbus_method_invocation_take_error (invocation, error);
379 goto out;
380 }
381
382 if (_vdo_resize (object, invocation, new_size, options))
383 udisks_vdo_volume_complete_resize_logical (_volume, invocation);
384
385 out:
386 g_clear_object (&object);
387 return TRUE;
388 }
389
390 static gboolean
391 handle_resize_physical (UDisksVDOVolume *_volume,
392 GDBusMethodInvocation *invocation,
393 guint64 new_size,
394 GVariant *options)
395 {
396 const gchar *pool_path = NULL;
397 UDisksObject *pool_object = NULL;
398 UDisksLinuxLogicalVolumeObject *object = NULL;
399 GError *error = NULL;
400 UDisksLinuxModuleLVM2 *module;
401 UDisksDaemon *daemon = NULL;
402
403 object = udisks_daemon_util_dup_object (_volume, &error);
404 if (object == NULL)
405 {
406 g_dbus_method_invocation_take_error (invocation, error);
407 goto out;
408 }
409
410 pool_path = udisks_vdo_volume_get_vdo_pool (_volume);
411 if (pool_path == NULL || g_strcmp0 (pool_path, "/") == 0)
412 {
413 g_dbus_method_invocation_return_error (invocation,
414 UDISKS_ERROR,
415 UDISKS_ERROR_FAILED,
416 "Failed to get VDO pool path.");
417 goto out;
418 }
419
420 module = udisks_linux_logical_volume_object_get_module (object);
421 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
422 pool_object = udisks_daemon_find_object (daemon, pool_path);
423 if (pool_object == NULL)
424 {
425 g_dbus_method_invocation_return_error (invocation,
426 UDISKS_ERROR,
427 UDISKS_ERROR_FAILED,
428 "Failed to get VDO pool object.");
429 goto out;
430 }
431
432 if (_vdo_resize (UDISKS_LINUX_LOGICAL_VOLUME_OBJECT (pool_object),
433 invocation, new_size, options))
434 udisks_vdo_volume_complete_resize_physical (_volume, invocation);
435
436 out:
437 g_clear_object (&object);
438 g_clear_object (&pool_object);
439 return TRUE;
440 }
441
442 static void
443 stats_add_element (const gchar *key, const gchar *value, GVariantBuilder *builder)
444 {
445 g_variant_builder_add (builder, "{ss}", key, value);
446 }
447
448 static gboolean
449 handle_get_statistics (UDisksVDOVolume *_volume,
450 GDBusMethodInvocation *invocation,
451 GVariant *options)
452 {
453 const gchar *pool_path = NULL;
454 UDisksObject *pool_object = NULL;
455 UDisksLinuxLogicalVolumeObject *object = NULL;
456 UDisksLinuxVolumeGroupObject *group_object = NULL;
457 GError *error = NULL;
458 UDisksLinuxModuleLVM2 *module;
459 UDisksDaemon *daemon = NULL;
460 GHashTable *stats = NULL;
461 GVariantBuilder builder;
462 const gchar *pool_name = NULL;
463 const gchar *vg_name = NULL;
464
465 object = udisks_daemon_util_dup_object (_volume, &error);
466 if (object == NULL)
467 {
468 g_dbus_method_invocation_take_error (invocation, error);
469 goto out;
470 }
471
472 group_object = udisks_linux_logical_volume_object_get_volume_group (object);
473 vg_name = udisks_linux_volume_group_object_get_name (group_object);
474
475 pool_path = udisks_vdo_volume_get_vdo_pool (_volume);
476 if (pool_path == NULL || g_strcmp0 (pool_path, "/") == 0)
477 {
478 g_dbus_method_invocation_return_error (invocation,
479 UDISKS_ERROR,
480 UDISKS_ERROR_FAILED,
481 "Failed to get VDO pool path.");
482 goto out;
483 }
484
485 module = udisks_linux_logical_volume_object_get_module (object);
486 daemon = udisks_module_get_daemon (UDISKS_MODULE (module));
487 pool_object = udisks_daemon_find_object (daemon, pool_path);
488 if (pool_object == NULL)
489 {
490 g_dbus_method_invocation_return_error (invocation,
491 UDISKS_ERROR,
492 UDISKS_ERROR_FAILED,
493 "Failed to get VDO pool object.");
494 goto out;
495 }
496
497 pool_name = udisks_linux_logical_volume_object_get_name (UDISKS_LINUX_LOGICAL_VOLUME_OBJECT (pool_object));
498
499 stats = bd_lvm_vdo_get_stats_full (vg_name, pool_name, &error);
500 if (stats == NULL)
501 {
502 g_dbus_method_invocation_return_error (invocation,
503 UDISKS_ERROR,
504 UDISKS_ERROR_FAILED,
505 "Error retrieving volume statistics: %s",
506 error->message);
507 g_error_free (error);
508 goto out;
509 }
510
511 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
512 g_hash_table_foreach (stats, (GHFunc) stats_add_element, &builder);
513
514 udisks_vdo_volume_complete_get_statistics (_volume, invocation, g_variant_builder_end (&builder));
515 g_hash_table_destroy (stats);
516
517 out:
518 g_clear_object (&object);
519 g_clear_object (&pool_object);
520 return TRUE;
521 }
522
523 /* ---------------------------------------------------------------------------------------------------- */
524
525 static void
526 vdo_volume_iface_init (UDisksVDOVolumeIface *iface)
527 {
528 iface->handle_enable_compression = handle_enable_compression;
529 iface->handle_enable_deduplication = handle_enable_deduplication;
530 iface->handle_resize_logical = handle_resize_logical;
531 iface->handle_resize_physical = handle_resize_physical;
532 iface->handle_get_statistics = handle_get_statistics;
533 }