| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2007-2010 David Zeuthen <[email protected]> |
| 4 |
* Copyright (C) 2013 Marius Vollmer <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include <glib/gi18n-lib.h> |
| 24 |
#include <gio/gunixmounts.h> |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdio.h> |
| 29 |
|
| 30 |
#include <src/udiskslogging.h> |
| 31 |
#include <src/udiskslinuxprovider.h> |
| 32 |
#include <src/udisksdaemon.h> |
| 33 |
#include <src/udisksdaemonutil.h> |
| 34 |
#include <src/udiskslinuxdevice.h> |
| 35 |
#include <src/udiskslinuxblockobject.h> |
| 36 |
|
| 37 |
#include "udiskslinuxvolumegroupobject.h" |
| 38 |
#include "udiskslinuxvolumegroup.h" |
| 39 |
#include "udiskslinuxlogicalvolumeobject.h" |
| 40 |
#include "udiskslinuxphysicalvolume.h" |
| 41 |
#include "udiskslinuxblocklvm2.h" |
| 42 |
|
| 43 |
#include "udiskslvm2daemonutil.h" |
| 44 |
#include "jobhelpers.h" |
| 45 |
|
| 46 |
/** |
| 47 |
* SECTION:udiskslinuxvolumegroupobject |
| 48 |
* @title: UDisksLinuxVolumeGroupObject |
| 49 |
* @short_description: Object representing a LVM volume group |
| 50 |
*/ |
| 51 |
|
| 52 |
typedef struct _UDisksLinuxVolumeGroupObjectClass UDisksLinuxVolumeGroupObjectClass; |
| 53 |
|
| 54 |
/** |
| 55 |
* UDisksLinuxVolumeGroupObject: |
| 56 |
* |
| 57 |
* The #UDisksLinuxVolumeGroupObject structure contains only private data and |
| 58 |
* should only be accessed using the provided API. |
| 59 |
*/ |
| 60 |
struct _UDisksLinuxVolumeGroupObject |
| 61 |
{ |
| 62 |
UDisksObjectSkeleton parent_instance; |
| 63 |
|
| 64 |
UDisksLinuxModuleLVM2 *module; |
| 65 |
|
| 66 |
gchar *name; |
| 67 |
|
| 68 |
GHashTable *logical_volumes; |
| 69 |
guint32 update_epoch; |
| 70 |
guint32 poll_epoch; |
| 71 |
guint poll_timeout_id; |
| 72 |
gboolean poll_requested; |
| 73 |
|
| 74 |
GUnixMountMonitor *mount_monitor; |
| 75 |
|
| 76 |
/* interface */ |
| 77 |
UDisksVolumeGroup *iface_volume_group; |
| 78 |
}; |
| 79 |
|
| 80 |
struct _UDisksLinuxVolumeGroupObjectClass |
| 81 |
{ |
| 82 |
UDisksObjectSkeletonClass parent_class; |
| 83 |
}; |
| 84 |
|
| 85 |
enum |
| 86 |
{ |
| 87 |
PROP_0, |
| 88 |
PROP_MODULE, |
| 89 |
PROP_NAME, |
| 90 |
}; |
| 91 |
|
| 92 |
G_DEFINE_TYPE (UDisksLinuxVolumeGroupObject, udisks_linux_volume_group_object, UDISKS_TYPE_OBJECT_SKELETON); |
| 93 |
|
| 94 |
static void fstab_changed (GUnixMountMonitor *monitor, |
| 95 |
gpointer user_data); |
| 96 |
static void crypttab_changed (UDisksCrypttabMonitor *monitor, |
| 97 |
UDisksCrypttabEntry *entry, |
| 98 |
gpointer user_data); |
| 99 |
|
| 100 |
typedef struct { |
| 101 |
BDLVMVGdata *vg_info; |
| 102 |
GSList *vg_pvs; |
| 103 |
guint32 epoch; |
| 104 |
} VGUpdateData; |
| 105 |
|
| 106 |
static void |
| 107 |
udisks_linux_volume_group_object_finalize (GObject *_object) |
| 108 |
{ |
| 109 |
UDisksLinuxVolumeGroupObject *object = UDISKS_LINUX_VOLUME_GROUP_OBJECT (_object); |
| 110 |
UDisksDaemon *daemon; |
| 111 |
|
| 112 |
daemon = udisks_module_get_daemon (UDISKS_MODULE (object->module)); |
| 113 |
|
| 114 |
g_object_unref (object->module); |
| 115 |
|
| 116 |
if (object->iface_volume_group != NULL) |
| 117 |
g_object_unref (object->iface_volume_group); |
| 118 |
|
| 119 |
g_hash_table_unref (object->logical_volumes); |
| 120 |
g_free (object->name); |
| 121 |
|
| 122 |
g_signal_handlers_disconnect_by_func (object->mount_monitor, |
| 123 |
G_CALLBACK (fstab_changed), |
| 124 |
object); |
| 125 |
g_signal_handlers_disconnect_by_func (udisks_daemon_get_crypttab_monitor (daemon), |
| 126 |
G_CALLBACK (crypttab_changed), |
| 127 |
object); |
| 128 |
g_object_unref (object->mount_monitor); |
| 129 |
|
| 130 |
if (G_OBJECT_CLASS (udisks_linux_volume_group_object_parent_class)->finalize != NULL) |
| 131 |
G_OBJECT_CLASS (udisks_linux_volume_group_object_parent_class)->finalize (_object); |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
udisks_linux_volume_group_object_get_property (GObject *__object, |
| 136 |
guint prop_id, |
| 137 |
GValue *value, |
| 138 |
GParamSpec *pspec) |
| 139 |
{ |
| 140 |
UDisksLinuxVolumeGroupObject *object = UDISKS_LINUX_VOLUME_GROUP_OBJECT (__object); |
| 141 |
|
| 142 |
switch (prop_id) |
| 143 |
{ |
| 144 |
case PROP_MODULE: |
| 145 |
g_value_set_object (value, udisks_linux_volume_group_object_get_module (object)); |
| 146 |
break; |
| 147 |
|
| 148 |
case PROP_NAME: |
| 149 |
g_value_set_string (value, udisks_linux_volume_group_object_get_name (object)); |
| 150 |
break; |
| 151 |
|
| 152 |
default: |
| 153 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
udisks_linux_volume_group_object_set_property (GObject *__object, |
| 160 |
guint prop_id, |
| 161 |
const GValue *value, |
| 162 |
GParamSpec *pspec) |
| 163 |
{ |
| 164 |
UDisksLinuxVolumeGroupObject *object = UDISKS_LINUX_VOLUME_GROUP_OBJECT (__object); |
| 165 |
|
| 166 |
switch (prop_id) |
| 167 |
{ |
| 168 |
case PROP_MODULE: |
| 169 |
g_assert (object->module == NULL); |
| 170 |
object->module = g_value_dup_object (value); |
| 171 |
break; |
| 172 |
|
| 173 |
case PROP_NAME: |
| 174 |
g_assert (object->name == NULL); |
| 175 |
object->name = g_value_dup_string (value); |
| 176 |
break; |
| 177 |
|
| 178 |
default: |
| 179 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
static void |
| 186 |
udisks_linux_volume_group_object_init (UDisksLinuxVolumeGroupObject *object) |
| 187 |
{ |
| 188 |
object->update_epoch = 0; |
| 189 |
object->poll_epoch = 0; |
| 190 |
object->poll_timeout_id = 0; |
| 191 |
object->poll_requested = FALSE; |
| 192 |
} |
| 193 |
|
| 194 |
static void |
| 195 |
udisks_linux_volume_group_object_constructed (GObject *_object) |
| 196 |
{ |
| 197 |
UDisksLinuxVolumeGroupObject *object = UDISKS_LINUX_VOLUME_GROUP_OBJECT (_object); |
| 198 |
UDisksDaemon *daemon; |
| 199 |
GString *s; |
| 200 |
|
| 201 |
if (G_OBJECT_CLASS (udisks_linux_volume_group_object_parent_class)->constructed != NULL) |
| 202 |
G_OBJECT_CLASS (udisks_linux_volume_group_object_parent_class)->constructed (_object); |
| 203 |
|
| 204 |
daemon = udisks_module_get_daemon (UDISKS_MODULE (object->module)); |
| 205 |
|
| 206 |
object->logical_volumes = g_hash_table_new_full (g_str_hash, |
| 207 |
g_str_equal, |
| 208 |
g_free, |
| 209 |
(GDestroyNotify) g_object_unref); |
| 210 |
|
| 211 |
/* compute the object path */ |
| 212 |
s = g_string_new ("/org/freedesktop/UDisks2/lvm/"); |
| 213 |
udisks_safe_append_to_object_path (s, object->name); |
| 214 |
g_dbus_object_skeleton_set_object_path (G_DBUS_OBJECT_SKELETON (object), s->str); |
| 215 |
g_string_free (s, TRUE); |
| 216 |
|
| 217 |
/* create the DBus interface */ |
| 218 |
object->iface_volume_group = udisks_linux_volume_group_new (); |
| 219 |
g_dbus_object_skeleton_add_interface (G_DBUS_OBJECT_SKELETON (object), |
| 220 |
G_DBUS_INTERFACE_SKELETON (object->iface_volume_group)); |
| 221 |
|
| 222 |
/* Watch fstab and crypttab for changes. |
| 223 |
*/ |
| 224 |
object->mount_monitor = g_unix_mount_monitor_get (); |
| 225 |
g_signal_connect (object->mount_monitor, |
| 226 |
"mountpoints-changed", |
| 227 |
G_CALLBACK (fstab_changed), |
| 228 |
object); |
| 229 |
g_signal_connect (udisks_daemon_get_crypttab_monitor (daemon), |
| 230 |
"entry-added", |
| 231 |
G_CALLBACK (crypttab_changed), |
| 232 |
object); |
| 233 |
g_signal_connect (udisks_daemon_get_crypttab_monitor (daemon), |
| 234 |
"entry-removed", |
| 235 |
G_CALLBACK (crypttab_changed), |
| 236 |
object); |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
udisks_linux_volume_group_object_class_init (UDisksLinuxVolumeGroupObjectClass *klass) |
| 241 |
{ |
| 242 |
GObjectClass *gobject_class; |
| 243 |
|
| 244 |
gobject_class = G_OBJECT_CLASS (klass); |
| 245 |
gobject_class->finalize = udisks_linux_volume_group_object_finalize; |
| 246 |
gobject_class->constructed = udisks_linux_volume_group_object_constructed; |
| 247 |
gobject_class->set_property = udisks_linux_volume_group_object_set_property; |
| 248 |
gobject_class->get_property = udisks_linux_volume_group_object_get_property; |
| 249 |
|
| 250 |
/** |
| 251 |
* UDisksLinuxVolumeGroupObject:module: |
| 252 |
* |
| 253 |
* The #UDisksLinuxModuleLVM2 the object is for. |
| 254 |
*/ |
| 255 |
g_object_class_install_property (gobject_class, |
| 256 |
PROP_MODULE, |
| 257 |
g_param_spec_object ("module", |
| 258 |
"Module", |
| 259 |
"The module the object is for", |
| 260 |
UDISKS_TYPE_LINUX_MODULE_LVM2, |
| 261 |
G_PARAM_READABLE | |
| 262 |
G_PARAM_WRITABLE | |
| 263 |
G_PARAM_CONSTRUCT_ONLY | |
| 264 |
G_PARAM_STATIC_STRINGS)); |
| 265 |
|
| 266 |
/** |
| 267 |
* UDisksLinuxVolumeGroupObject:name: |
| 268 |
* |
| 269 |
* The name of the volume group. |
| 270 |
*/ |
| 271 |
g_object_class_install_property (gobject_class, |
| 272 |
PROP_NAME, |
| 273 |
g_param_spec_string ("name", |
| 274 |
"Name", |
| 275 |
"The name of the volume group", |
| 276 |
NULL, |
| 277 |
G_PARAM_WRITABLE | |
| 278 |
G_PARAM_CONSTRUCT_ONLY | |
| 279 |
G_PARAM_STATIC_STRINGS)); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* udisks_linux_volume_group_object_new: |
| 284 |
* @module: A #UDisksLinuxModuleLVM2. |
| 285 |
* @name: The name of the volume group. |
| 286 |
* |
| 287 |
* Create a new VolumeGroup object. |
| 288 |
* |
| 289 |
* Returns: A #UDisksLinuxVolumeGroupObject object. Free with g_object_unref(). |
| 290 |
*/ |
| 291 |
UDisksLinuxVolumeGroupObject * |
| 292 |
udisks_linux_volume_group_object_new (UDisksLinuxModuleLVM2 *module, |
| 293 |
const gchar *name) |
| 294 |
{ |
| 295 |
g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_LVM2 (module), NULL); |
| 296 |
g_return_val_if_fail (name != NULL, NULL); |
| 297 |
return UDISKS_LINUX_VOLUME_GROUP_OBJECT (g_object_new (UDISKS_TYPE_LINUX_VOLUME_GROUP_OBJECT, |
| 298 |
"module", module, |
| 299 |
"name", name, |
| 300 |
NULL)); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* udisks_linux_volume_group_object_get_module: |
| 305 |
* @object: A #UDisksLinuxVolumeGroupObject. |
| 306 |
* |
| 307 |
* Gets the module used by @object. |
| 308 |
* |
| 309 |
* Returns: A #UDisksLinuxModuleLVM2. Do not free, the object is owned by @object. |
| 310 |
*/ |
| 311 |
UDisksLinuxModuleLVM2 * |
| 312 |
udisks_linux_volume_group_object_get_module (UDisksLinuxVolumeGroupObject *object) |
| 313 |
{ |
| 314 |
g_return_val_if_fail (UDISKS_IS_LINUX_VOLUME_GROUP_OBJECT (object), NULL); |
| 315 |
return object->module; |
| 316 |
} |
| 317 |
|
| 318 |
static void |
| 319 |
update_etctabs (UDisksLinuxVolumeGroupObject *object) |
| 320 |
{ |
| 321 |
GHashTableIter iter; |
| 322 |
gpointer key, value; |
| 323 |
|
| 324 |
g_hash_table_iter_init (&iter, object->logical_volumes); |
| 325 |
while (g_hash_table_iter_next (&iter, &key, &value)) |
| 326 |
{ |
| 327 |
UDisksLinuxLogicalVolumeObject *volume = value; |
| 328 |
|
| 329 |
udisks_linux_logical_volume_object_update_etctabs (volume); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
static void |
| 334 |
fstab_changed (GUnixMountMonitor *monitor, |
| 335 |
gpointer user_data) |
| 336 |
{ |
| 337 |
update_etctabs (UDISKS_LINUX_VOLUME_GROUP_OBJECT (user_data)); |
| 338 |
} |
| 339 |
|
| 340 |
static void |
| 341 |
crypttab_changed (UDisksCrypttabMonitor *monitor, |
| 342 |
UDisksCrypttabEntry *entry, |
| 343 |
gpointer user_data) |
| 344 |
{ |
| 345 |
update_etctabs (UDISKS_LINUX_VOLUME_GROUP_OBJECT (user_data)); |
| 346 |
} |
| 347 |
|
| 348 |
static gboolean |
| 349 |
lv_is_pvmove_volume (const gchar *name) |
| 350 |
{ |
| 351 |
return name && g_str_has_prefix (name, "pvmove"); |
| 352 |
} |
| 353 |
|
| 354 |
static void |
| 355 |
update_progress_for_device (UDisksLinuxVolumeGroupObject *object, |
| 356 |
const gchar *operation, |
| 357 |
const gchar *dev, |
| 358 |
double progress) |
| 359 |
{ |
| 360 |
GDBusObjectManager *object_manager; |
| 361 |
UDisksDaemon *daemon; |
| 362 |
GList *objects, *l; |
| 363 |
|
| 364 |
daemon = udisks_module_get_daemon (UDISKS_MODULE (object->module)); |
| 365 |
object_manager = G_DBUS_OBJECT_MANAGER (udisks_daemon_get_object_manager (daemon)); |
| 366 |
objects = g_dbus_object_manager_get_objects (object_manager); |
| 367 |
|
| 368 |
for (l = objects; l; l = l->next) |
| 369 |
{ |
| 370 |
UDisksObject *o = UDISKS_OBJECT (l->data); |
| 371 |
UDisksJob *job; |
| 372 |
const gchar *const *job_objects; |
| 373 |
int i; |
| 374 |
|
| 375 |
job = udisks_object_peek_job (o); |
| 376 |
if (job == NULL) |
| 377 |
continue; |
| 378 |
|
| 379 |
if (g_strcmp0 (udisks_job_get_operation (job), operation) != 0) |
| 380 |
continue; |
| 381 |
|
| 382 |
job_objects = udisks_job_get_objects (job); |
| 383 |
for (i = 0; job_objects[i]; i++) |
| 384 |
{ |
| 385 |
UDisksBlock *block = UDISKS_BLOCK (g_dbus_object_manager_get_interface (object_manager, |
| 386 |
job_objects[i], |
| 387 |
"org.freedesktop.UDisks2.Block")); |
| 388 |
|
| 389 |
if (block) |
| 390 |
{ |
| 391 |
const gchar *const *symlinks; |
| 392 |
int j; |
| 393 |
if (g_strcmp0 (udisks_block_get_device (block), dev) == 0) |
| 394 |
goto found; |
| 395 |
symlinks = udisks_block_get_symlinks (block); |
| 396 |
for (j = 0; symlinks[j]; j++) |
| 397 |
if (g_strcmp0 (symlinks[j], dev) == 0) |
| 398 |
goto found; |
| 399 |
|
| 400 |
continue; |
| 401 |
found: |
| 402 |
udisks_job_set_progress (job, progress); |
| 403 |
udisks_job_set_progress_valid (job, TRUE); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
} |
| 408 |
g_list_free_full (objects, g_object_unref); |
| 409 |
} |
| 410 |
|
| 411 |
static void |
| 412 |
update_operations (UDisksLinuxVolumeGroupObject *object, |
| 413 |
const gchar *lv_name, |
| 414 |
BDLVMLVdata *lv_info, |
| 415 |
gboolean *needs_polling_ret) |
| 416 |
{ |
| 417 |
if (lv_is_pvmove_volume (lv_name)) |
| 418 |
{ |
| 419 |
if (lv_info->move_pv && lv_info->copy_percent) |
| 420 |
{ |
| 421 |
update_progress_for_device (object, |
| 422 |
"lvm-vg-empty-device", |
| 423 |
lv_info->move_pv, |
| 424 |
lv_info->copy_percent/100.0); |
| 425 |
} |
| 426 |
*needs_polling_ret = TRUE; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
static void |
| 431 |
block_object_update_lvm_iface (UDisksLinuxBlockObject *object, |
| 432 |
const gchar *lv_obj_path) |
| 433 |
{ |
| 434 |
UDisksBlockLVM2 *iface_block_lvm2; |
| 435 |
|
| 436 |
iface_block_lvm2 = udisks_object_peek_block_lvm2 (UDISKS_OBJECT (object)); |
| 437 |
|
| 438 |
if (iface_block_lvm2 == NULL) |
| 439 |
{ |
| 440 |
iface_block_lvm2 = udisks_linux_block_lvm2_new (); |
| 441 |
g_dbus_object_skeleton_add_interface (G_DBUS_OBJECT_SKELETON (object), |
| 442 |
G_DBUS_INTERFACE_SKELETON (iface_block_lvm2)); |
| 443 |
g_object_unref (iface_block_lvm2); |
| 444 |
} |
| 445 |
|
| 446 |
udisks_linux_block_lvm2_update (UDISKS_LINUX_BLOCK_LVM2 (iface_block_lvm2), object); |
| 447 |
udisks_block_lvm2_set_logical_volume (iface_block_lvm2, lv_obj_path); |
| 448 |
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (iface_block_lvm2)); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
lv_object_update_block_path (UDisksLinuxBlockObject *block_object, |
| 453 |
UDisksLinuxLogicalVolumeObject *lv_object) |
| 454 |
{ |
| 455 |
UDisksLogicalVolume *lv = NULL; |
| 456 |
const char *block_objpath = NULL; |
| 457 |
|
| 458 |
lv = udisks_object_peek_logical_volume (UDISKS_OBJECT (lv_object)); |
| 459 |
if (lv) |
| 460 |
{ |
| 461 |
block_objpath = g_dbus_object_get_object_path (G_DBUS_OBJECT (block_object)); |
| 462 |
udisks_logical_volume_set_block_device (UDISKS_LOGICAL_VOLUME (lv), block_objpath); |
| 463 |
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (lv)); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
static void |
| 468 |
update_block (UDisksLinuxBlockObject *block_object, |
| 469 |
UDisksLinuxVolumeGroupObject *group_object, |
| 470 |
GHashTable *new_lvs, |
| 471 |
GHashTable *new_pvs) |
| 472 |
{ |
| 473 |
UDisksBlock *block; |
| 474 |
BDLVMPVdata *pv_info; |
| 475 |
|
| 476 |
block = udisks_object_peek_block (UDISKS_OBJECT (block_object)); |
| 477 |
if (block == NULL) |
| 478 |
return; |
| 479 |
|
| 480 |
/* XXX - move this elsewhere? */ |
| 481 |
{ |
| 482 |
UDisksLinuxDevice *device; |
| 483 |
UDisksLinuxLogicalVolumeObject *lv_object; |
| 484 |
const gchar *block_vg_name; |
| 485 |
const gchar *block_lv_name; |
| 486 |
|
| 487 |
device = udisks_linux_block_object_get_device (block_object); |
| 488 |
if (device) |
| 489 |
{ |
| 490 |
block_vg_name = g_udev_device_get_property (device->udev_device, "DM_VG_NAME"); |
| 491 |
block_lv_name = g_udev_device_get_property (device->udev_device, "DM_LV_NAME"); |
| 492 |
|
| 493 |
if (g_strcmp0 (block_vg_name, udisks_linux_volume_group_object_get_name (group_object)) == 0 |
| 494 |
&& (lv_object = g_hash_table_lookup (new_lvs, block_lv_name))) |
| 495 |
{ |
| 496 |
block_object_update_lvm_iface (block_object, g_dbus_object_get_object_path (G_DBUS_OBJECT (lv_object))); |
| 497 |
lv_object_update_block_path (block_object, lv_object); |
| 498 |
} |
| 499 |
|
| 500 |
g_object_unref(device); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
pv_info = g_hash_table_lookup (new_pvs, udisks_block_get_device (block)); |
| 505 |
if (!pv_info) |
| 506 |
{ |
| 507 |
const gchar *const *symlinks; |
| 508 |
int i; |
| 509 |
symlinks = udisks_block_get_symlinks (block); |
| 510 |
for (i = 0; symlinks[i]; i++) |
| 511 |
{ |
| 512 |
pv_info = g_hash_table_lookup (new_pvs, symlinks[i]); |
| 513 |
if (pv_info) |
| 514 |
break; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if (pv_info) |
| 519 |
{ |
| 520 |
udisks_linux_block_object_update_lvm_pv (block_object, group_object, pv_info); |
| 521 |
} |
| 522 |
else |
| 523 |
{ |
| 524 |
UDisksPhysicalVolume *pv = udisks_object_peek_physical_volume (UDISKS_OBJECT (block_object)); |
| 525 |
if (pv && g_strcmp0 (udisks_physical_volume_get_volume_group (pv), |
| 526 |
g_dbus_object_get_object_path (G_DBUS_OBJECT (group_object))) == 0) |
| 527 |
udisks_linux_block_object_update_lvm_pv (block_object, NULL, NULL); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* cmp_int_lv_name: (skip) |
| 533 |
* |
| 534 |
* Compare name of an internal LV (possibly enclosed in square brackets) with a |
| 535 |
* given other name. |
| 536 |
*/ |
| 537 |
static gboolean |
| 538 |
cmp_int_lv_name (const gchar *int_lv_name, const gchar *lv_name) |
| 539 |
{ |
| 540 |
const gchar *c = NULL; |
| 541 |
if (!int_lv_name || !lv_name) |
| 542 |
return FALSE; |
| 543 |
|
| 544 |
if (*int_lv_name == '[') |
| 545 |
int_lv_name++; |
| 546 |
|
| 547 |
for (c=int_lv_name; *c != '\0' && *c != ']'; c++) |
| 548 |
if (*c != lv_name[c - int_lv_name]) |
| 549 |
return FALSE; |
| 550 |
|
| 551 |
if (*c == ']') |
| 552 |
c++; |
| 553 |
if (*c == '\0' && lv_name[c - int_lv_name] == '\0') |
| 554 |
return TRUE; |
| 555 |
|
| 556 |
return FALSE; |
| 557 |
} |
| 558 |
|
| 559 |
static void |
| 560 |
update_vg (GObject *source_obj, |
| 561 |
GAsyncResult *result, |
| 562 |
gpointer user_data) |
| 563 |
{ |
| 564 |
UDisksLinuxVolumeGroupObject *object = UDISKS_LINUX_VOLUME_GROUP_OBJECT (source_obj); |
| 565 |
UDisksDaemon *daemon; |
| 566 |
GDBusObjectManagerServer *manager; |
| 567 |
GHashTableIter volume_iter; |
| 568 |
gpointer key, value; |
| 569 |
GHashTable *new_lvs; |
| 570 |
GHashTable *new_pvs; |
| 571 |
GList *objects, *l; |
| 572 |
gboolean needs_polling = FALSE; |
| 573 |
GTask *task = G_TASK (result); |
| 574 |
VGUpdateData *data = user_data; |
| 575 |
GError *error = NULL; |
| 576 |
BDLVMLVdata **lvs = g_task_propagate_pointer (task, &error); |
| 577 |
BDLVMVGdata *vg_info = data->vg_info; |
| 578 |
GSList *vg_pvs = data->vg_pvs; |
| 579 |
|
| 580 |
if (data->epoch != object->update_epoch) |
| 581 |
{ |
| 582 |
lv_list_free (lvs); |
| 583 |
g_slist_free_full (vg_pvs, (GDestroyNotify) bd_lvm_pvdata_free); |
| 584 |
bd_lvm_vgdata_free (vg_info); |
| 585 |
g_object_unref (object); |
| 586 |
g_free (data); |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
/* free the data container (but not 'vg_info' and 'vg_pvs') */ |
| 591 |
g_free (data); |
| 592 |
|
| 593 |
if (!lvs) |
| 594 |
{ |
| 595 |
if (error) |
| 596 |
{ |
| 597 |
udisks_warning ("Failed to update LVM volume group %s: %s", |
| 598 |
udisks_linux_volume_group_object_get_name (object), |
| 599 |
error->message); |
| 600 |
g_clear_error (&error); |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
/* this should never happen */ |
| 605 |
udisks_warning ("Failed to update LVM volume group %s: no error reported", |
| 606 |
udisks_linux_volume_group_object_get_name (object)); |
| 607 |
} |
| 608 |
g_slist_free_full (vg_pvs, (GDestroyNotify) bd_lvm_pvdata_free); |
| 609 |
bd_lvm_vgdata_free (vg_info); |
| 610 |
g_object_unref (object); |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
daemon = udisks_module_get_daemon (UDISKS_MODULE (object->module)); |
| 615 |
manager = udisks_daemon_get_object_manager (daemon); |
| 616 |
|
| 617 |
udisks_linux_volume_group_update (UDISKS_LINUX_VOLUME_GROUP (object->iface_volume_group), vg_info, vg_pvs, |
| 618 |
&needs_polling); |
| 619 |
|
| 620 |
if (!g_dbus_object_manager_server_is_exported (manager, G_DBUS_OBJECT_SKELETON (object))) |
| 621 |
g_dbus_object_manager_server_export_uniquely (manager, G_DBUS_OBJECT_SKELETON (object)); |
| 622 |
|
| 623 |
new_lvs = g_hash_table_new (g_str_hash, g_str_equal); |
| 624 |
|
| 625 |
for (BDLVMLVdata **lvs_p=lvs; *lvs_p; lvs_p++) |
| 626 |
{ |
| 627 |
UDisksLinuxLogicalVolumeObject *volume; |
| 628 |
BDLVMLVdata *lv_info = *lvs_p; |
| 629 |
const gchar *lv_name = lv_info->lv_name; |
| 630 |
BDLVMLVdata *meta_lv_info = NULL; |
| 631 |
BDLVMVDOPooldata *vdo_info = NULL; |
| 632 |
|
| 633 |
update_operations (object, lv_name, lv_info, &needs_polling); |
| 634 |
|
| 635 |
if (udisks_daemon_util_lvm2_name_is_reserved (lv_name)) |
| 636 |
continue; |
| 637 |
|
| 638 |
if (lv_info->metadata_lv && *(lv_info->metadata_lv) != '\0') |
| 639 |
/* this is not cheap to do, but not many LVs have a metadata LV */ |
| 640 |
for (BDLVMLVdata **lvs_p2=lvs; !meta_lv_info && *lvs_p2; lvs_p2++) |
| 641 |
if (cmp_int_lv_name ((*lvs_p2)->lv_name, lv_info->metadata_lv)) |
| 642 |
meta_lv_info = *lvs_p2; |
| 643 |
|
| 644 |
if (lv_info->pool_lv && g_strcmp0 (lv_info->segtype, "vdo") == 0) |
| 645 |
{ |
| 646 |
vdo_info = bd_lvm_vdo_info (lv_info->vg_name, lv_info->pool_lv, &error); |
| 647 |
if (!vdo_info) |
| 648 |
{ |
| 649 |
udisks_warning ("Failed to get information about VDO volume %s: %s", |
| 650 |
lv_info->lv_name, error->message); |
| 651 |
g_clear_error (&error); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
volume = g_hash_table_lookup (object->logical_volumes, lv_name); |
| 657 |
if (volume == NULL) |
| 658 |
{ |
| 659 |
volume = udisks_linux_logical_volume_object_new (object->module, object, lv_name); |
| 660 |
udisks_linux_logical_volume_object_update (volume, lv_info, meta_lv_info, lvs, vdo_info, &needs_polling); |
| 661 |
udisks_linux_logical_volume_object_update_etctabs (volume); |
| 662 |
g_dbus_object_manager_server_export_uniquely (manager, G_DBUS_OBJECT_SKELETON (volume)); |
| 663 |
g_hash_table_insert (object->logical_volumes, g_strdup (lv_name), volume); |
| 664 |
} |
| 665 |
else |
| 666 |
udisks_linux_logical_volume_object_update (volume, lv_info, meta_lv_info, lvs, vdo_info, &needs_polling); |
| 667 |
|
| 668 |
if (vdo_info) |
| 669 |
bd_lvm_vdopooldata_free (vdo_info); |
| 670 |
|
| 671 |
g_hash_table_insert (new_lvs, (gchar *)lv_name, volume); |
| 672 |
} |
| 673 |
|
| 674 |
g_hash_table_iter_init (&volume_iter, object->logical_volumes); |
| 675 |
while (g_hash_table_iter_next (&volume_iter, &key, &value)) |
| 676 |
{ |
| 677 |
const gchar *name = key; |
| 678 |
UDisksLinuxLogicalVolumeObject *volume = value; |
| 679 |
|
| 680 |
if (!g_hash_table_contains (new_lvs, name)) |
| 681 |
{ |
| 682 |
g_dbus_object_manager_server_unexport (manager, |
| 683 |
g_dbus_object_get_object_path (G_DBUS_OBJECT (volume))); |
| 684 |
g_hash_table_iter_remove (&volume_iter); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
udisks_volume_group_set_needs_polling (UDISKS_VOLUME_GROUP (object->iface_volume_group), |
| 689 |
needs_polling); |
| 690 |
|
| 691 |
/* Update block objects. */ |
| 692 |
new_pvs = g_hash_table_new (g_str_hash, g_str_equal); |
| 693 |
for (GSList *vg_pvs_p=vg_pvs; vg_pvs_p; vg_pvs_p=vg_pvs_p->next) |
| 694 |
{ |
| 695 |
BDLVMPVdata *pv_info = vg_pvs_p->data; |
| 696 |
gchar *pv_name = pv_info->pv_name; |
| 697 |
if (pv_name) |
| 698 |
g_hash_table_insert (new_pvs, pv_name, pv_info); |
| 699 |
} |
| 700 |
|
| 701 |
objects = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager)); |
| 702 |
for (l = objects; l != NULL; l = l->next) |
| 703 |
{ |
| 704 |
if (UDISKS_IS_LINUX_BLOCK_OBJECT (l->data)) |
| 705 |
update_block (UDISKS_LINUX_BLOCK_OBJECT (l->data), object, new_lvs, new_pvs); |
| 706 |
} |
| 707 |
g_list_free_full (objects, g_object_unref); |
| 708 |
|
| 709 |
g_hash_table_destroy (new_lvs); |
| 710 |
g_hash_table_destroy (new_pvs); |
| 711 |
|
| 712 |
g_slist_free_full (vg_pvs, (GDestroyNotify) bd_lvm_pvdata_free); |
| 713 |
bd_lvm_vgdata_free (vg_info); |
| 714 |
lv_list_free (lvs); |
| 715 |
|
| 716 |
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object->iface_volume_group)); |
| 717 |
g_object_unref (object); |
| 718 |
} |
| 719 |
|
| 720 |
void |
| 721 |
udisks_linux_volume_group_object_update (UDisksLinuxVolumeGroupObject *object, BDLVMVGdata *vg_info, GSList *pvs) |
| 722 |
{ |
| 723 |
VGUpdateData *data = g_new0 (VGUpdateData, 1); |
| 724 |
gchar *vg_name = g_strdup (vg_info->name); |
| 725 |
GTask *task = NULL; |
| 726 |
|
| 727 |
object->update_epoch++; |
| 728 |
|
| 729 |
data->vg_info = vg_info; |
| 730 |
data->vg_pvs = pvs; |
| 731 |
data->epoch = object->update_epoch; |
| 732 |
|
| 733 |
/* the callback (update_vg) is called in the default main loop (context) */ |
| 734 |
task = g_task_new (g_object_ref (object), NULL /* cancellable */, update_vg, data /* callback_data */); |
| 735 |
g_task_set_task_data (task, vg_name, g_free); |
| 736 |
|
| 737 |
/* holds a reference to 'task' until it is finished */ |
| 738 |
g_task_run_in_thread (task, (GTaskThreadFunc) lvs_task_func); |
| 739 |
|
| 740 |
g_object_unref (task); |
| 741 |
} |
| 742 |
|
| 743 |
static void |
| 744 |
poll_vg_update (GObject *source_obj, |
| 745 |
GAsyncResult *result, |
| 746 |
gpointer user_data) |
| 747 |
{ |
| 748 |
gboolean needs_polling; |
| 749 |
GError *error = NULL; |
| 750 |
UDisksLinuxVolumeGroupObject *object = UDISKS_LINUX_VOLUME_GROUP_OBJECT (source_obj); |
| 751 |
GTask *task = G_TASK (result); |
| 752 |
guint32 epoch_started = GPOINTER_TO_UINT (user_data); |
| 753 |
BDLVMLVdata **lvs = g_task_propagate_pointer (task, &error); |
| 754 |
|
| 755 |
if (epoch_started != object->poll_epoch) |
| 756 |
{ |
| 757 |
/* epoch has changed -> another poll update is on the way */ |
| 758 |
lv_list_free (lvs); |
| 759 |
g_object_unref (object); |
| 760 |
return; |
| 761 |
} |
| 762 |
|
| 763 |
if (!lvs) |
| 764 |
{ |
| 765 |
if (error) |
| 766 |
{ |
| 767 |
udisks_warning ("Failed to poll LVM volume group %s: %s", |
| 768 |
udisks_linux_volume_group_object_get_name (object), |
| 769 |
error->message); |
| 770 |
g_clear_error (&error); |
| 771 |
} |
| 772 |
else |
| 773 |
/* this should never happen */ |
| 774 |
udisks_warning ("Failed to poll LVM volume group %s: no error reported", |
| 775 |
udisks_linux_volume_group_object_get_name (object)); |
| 776 |
|
| 777 |
g_object_unref (object); |
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
/* XXX: we used to do this, but it seems to be pointless (how could a VG change without emitting a uevent on the PVs?) */ |
| 782 |
/* udisks_linux_volume_group_update (UDISKS_LINUX_VOLUME_GROUP (object->iface_volume_group), info, &needs_polling); */ |
| 783 |
|
| 784 |
for (BDLVMLVdata **lvs_p=lvs; *lvs_p; lvs_p++) |
| 785 |
{ |
| 786 |
UDisksLinuxLogicalVolumeObject *volume; |
| 787 |
BDLVMLVdata *lv_info = *lvs_p; |
| 788 |
BDLVMLVdata *meta_lv_info = NULL; |
| 789 |
const gchar *lv_name = lv_info->lv_name; |
| 790 |
BDLVMVDOPooldata *vdo_info = NULL; |
| 791 |
|
| 792 |
if (lv_info->metadata_lv && *(lv_info->metadata_lv) != '\0') |
| 793 |
/* this is not cheap to do, but not many LVs have a metadata LV */ |
| 794 |
for (BDLVMLVdata **lvs_np=lvs; !meta_lv_info && *lvs_np; lvs_np++) |
| 795 |
if (cmp_int_lv_name ((*lvs_np)->lv_name, lv_info->metadata_lv)) |
| 796 |
meta_lv_info = *lvs_np; |
| 797 |
|
| 798 |
if (lv_info->pool_lv && g_strcmp0 (lv_info->segtype, "vdo") == 0) |
| 799 |
{ |
| 800 |
vdo_info = bd_lvm_vdo_info (lv_info->vg_name, lv_info->pool_lv, &error); |
| 801 |
if (!vdo_info) |
| 802 |
{ |
| 803 |
udisks_warning ("Failed to get information about VDO volume %s: %s", |
| 804 |
lv_info->lv_name, error->message); |
| 805 |
g_clear_error (&error); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
update_operations (object, lv_name, lv_info, &needs_polling); |
| 810 |
volume = g_hash_table_lookup (object->logical_volumes, lv_name); |
| 811 |
if (volume) |
| 812 |
udisks_linux_logical_volume_object_update (volume, lv_info, meta_lv_info, lvs, vdo_info, &needs_polling); |
| 813 |
} |
| 814 |
|
| 815 |
lv_list_free (lvs); |
| 816 |
g_object_unref (object); |
| 817 |
} |
| 818 |
|
| 819 |
static void poll_now (UDisksLinuxVolumeGroupObject *object); |
| 820 |
|
| 821 |
static gboolean |
| 822 |
poll_in_main_thread (gpointer user_data) |
| 823 |
{ |
| 824 |
UDisksLinuxVolumeGroupObject *object = user_data; |
| 825 |
|
| 826 |
if (object->poll_timeout_id) |
| 827 |
object->poll_requested = TRUE; |
| 828 |
else |
| 829 |
poll_now (object); |
| 830 |
|
| 831 |
g_object_unref (object); |
| 832 |
return FALSE; |
| 833 |
} |
| 834 |
|
| 835 |
static gboolean |
| 836 |
poll_timeout (gpointer user_data) |
| 837 |
{ |
| 838 |
UDisksLinuxVolumeGroupObject *object = user_data; |
| 839 |
|
| 840 |
object->poll_timeout_id = 0; |
| 841 |
if (object->poll_requested) |
| 842 |
{ |
| 843 |
object->poll_requested = FALSE; |
| 844 |
poll_now (object); |
| 845 |
} |
| 846 |
|
| 847 |
g_object_unref (object); |
| 848 |
return FALSE; |
| 849 |
} |
| 850 |
|
| 851 |
static void |
| 852 |
poll_now (UDisksLinuxVolumeGroupObject *object) |
| 853 |
{ |
| 854 |
gchar *vg_name = g_strdup (udisks_linux_volume_group_object_get_name (object)); |
| 855 |
GTask *task = NULL; |
| 856 |
|
| 857 |
object->poll_timeout_id = g_timeout_add (5000, poll_timeout, g_object_ref (object)); |
| 858 |
|
| 859 |
/* starting a new poll -> increment the epoch */ |
| 860 |
object->poll_epoch++; |
| 861 |
|
| 862 |
/* the callback (poll_vg_update) is called in the default main loop (context) */ |
| 863 |
task = g_task_new (g_object_ref (object), NULL /* cancellable */, |
| 864 |
poll_vg_update, GUINT_TO_POINTER (object->poll_epoch) /* callback_data */); |
| 865 |
g_task_set_task_data (task, vg_name, g_free); |
| 866 |
|
| 867 |
/* holds a reference to 'task' until it is finished */ |
| 868 |
g_task_run_in_thread (task, (GTaskThreadFunc) lvs_task_func); |
| 869 |
|
| 870 |
g_object_unref (task); |
| 871 |
} |
| 872 |
|
| 873 |
void |
| 874 |
udisks_linux_volume_group_object_poll (UDisksLinuxVolumeGroupObject *object) |
| 875 |
{ |
| 876 |
g_idle_add (poll_in_main_thread, g_object_ref (object)); |
| 877 |
} |
| 878 |
|
| 879 |
void |
| 880 |
udisks_linux_volume_group_object_destroy (UDisksLinuxVolumeGroupObject *object) |
| 881 |
{ |
| 882 |
UDisksDaemon *daemon; |
| 883 |
GHashTableIter volume_iter; |
| 884 |
gpointer key, value; |
| 885 |
|
| 886 |
daemon = udisks_module_get_daemon (UDISKS_MODULE (object->module)); |
| 887 |
|
| 888 |
g_hash_table_iter_init (&volume_iter, object->logical_volumes); |
| 889 |
while (g_hash_table_iter_next (&volume_iter, &key, &value)) |
| 890 |
{ |
| 891 |
UDisksLinuxLogicalVolumeObject *volume = value; |
| 892 |
g_dbus_object_manager_server_unexport (udisks_daemon_get_object_manager (daemon), |
| 893 |
g_dbus_object_get_object_path (G_DBUS_OBJECT (volume))); |
| 894 |
} |
| 895 |
|
| 896 |
if (object->iface_volume_group != NULL) |
| 897 |
{ |
| 898 |
g_dbus_object_skeleton_remove_interface (G_DBUS_OBJECT_SKELETON (object), |
| 899 |
G_DBUS_INTERFACE_SKELETON (object->iface_volume_group)); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
UDisksLinuxLogicalVolumeObject * |
| 904 |
udisks_linux_volume_group_object_find_logical_volume_object (UDisksLinuxVolumeGroupObject *object, |
| 905 |
const gchar *name) |
| 906 |
{ |
| 907 |
return g_hash_table_lookup (object->logical_volumes, name); |
| 908 |
} |
| 909 |
|
| 910 |
/** |
| 911 |
* udisks_linux_volume_group_object_get_name: |
| 912 |
* @object: A #UDisksLinuxVolumeGroupObject. |
| 913 |
* |
| 914 |
* Gets the name for @object. |
| 915 |
* |
| 916 |
* Returns: (transfer none): The name for object. Do not free, the string belongs to @object. |
| 917 |
*/ |
| 918 |
const gchar * |
| 919 |
udisks_linux_volume_group_object_get_name (UDisksLinuxVolumeGroupObject *object) |
| 920 |
{ |
| 921 |
g_return_val_if_fail (UDISKS_IS_LINUX_VOLUME_GROUP_OBJECT (object), NULL); |
| 922 |
return object->name; |
| 923 |
} |