Debian Codegraph @

modules/btrfs/udiskslinuxfilesystembtrfs.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2015 Peter Hatina <[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 #include "config.h"
21
22 #include <glib/gi18n.h>
23
24 #include <src/udisksdaemon.h>
25 #include <src/udisksdaemonutil.h>
26 #include <src/udiskslinuxblockobject.h>
27 #include <src/udiskslinuxdevice.h>
28 #include <src/udiskslogging.h>
29 #include <src/udisksmodule.h>
30 #include <src/udisksmoduleobject.h>
31 #include <blockdev/btrfs.h>
32
33 #include "udiskslinuxmodulebtrfs.h"
34 #include "udiskslinuxfilesystembtrfs.h"
35 #include "udisksbtrfsutil.h"
36
37 /**
38 * SECTION:udiskslinuxfilesystembtrfs
39 * @title: UDisksLinuxFilesystemBTRFS
40 * @short_description: Linux implementation of #UDisksFilesystemBTRFS
41 *
42 * This type provides an implementation of #UDisksFilesystemBTRFS interface
43 * on Linux.
44 */
45
46 /**
47 * UDisksLinuxFilesystemBTRFS:
48 *
49 * The #UDisksLinuxFilesystemBTRFS structure contains only private data and
50 * should be only accessed using provided API.
51 */
52 struct _UDisksLinuxFilesystemBTRFS {
53 UDisksFilesystemBTRFSSkeleton parent_instance;
54
55 UDisksLinuxModuleBTRFS *module;
56 UDisksLinuxBlockObject *block_object;
57 };
58
59 struct _UDisksLinuxFilesystemBTRFSClass {
60 UDisksFilesystemBTRFSSkeletonClass parent_class;
61 };
62
63 typedef gboolean (*btrfs_subvolume_func)(const gchar *mount_point, const gchar *name, const BDExtraArg **extra, GError **error);
64 typedef gboolean (*btrfs_device_func)(const gchar *mountpoint, const gchar *device, const BDExtraArg **extra, GError **error);
65
66 enum
67 {
68 PROP_0,
69 PROP_MODULE,
70 PROP_BLOCK_OBJECT,
71 N_PROPERTIES
72 };
73
74 static void udisks_linux_filesystem_btrfs_iface_init (UDisksFilesystemBTRFSIface *iface);
75 static void udisks_linux_filesystem_btrfs_module_object_iface_init (UDisksModuleObjectIface *iface);
76
77 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxFilesystemBTRFS, udisks_linux_filesystem_btrfs, UDISKS_TYPE_FILESYSTEM_BTRFS_SKELETON,
78 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_FILESYSTEM_BTRFS, udisks_linux_filesystem_btrfs_iface_init)
79 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_MODULE_OBJECT, udisks_linux_filesystem_btrfs_module_object_iface_init));
80
81 static void
82 udisks_linux_filesystem_btrfs_get_property (GObject *object,
83 guint property_id,
84 GValue *value,
85 GParamSpec *pspec)
86 {
87 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (object);
88
89 switch (property_id)
90 {
91 case PROP_MODULE:
92 g_value_set_object (value, UDISKS_MODULE (udisks_linux_filesystem_btrfs_get_module (l_fs_btrfs)));
93 break;
94
95 case PROP_BLOCK_OBJECT:
96 g_value_set_object (value, l_fs_btrfs->block_object);
97 break;
98
99 default:
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
101 break;
102 }
103 }
104
105 static void
106 udisks_linux_filesystem_btrfs_set_property (GObject *object,
107 guint property_id,
108 const GValue *value,
109 GParamSpec *pspec)
110 {
111 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (object);
112
113 switch (property_id)
114 {
115 case PROP_MODULE:
116 g_assert (l_fs_btrfs->module == NULL);
117 l_fs_btrfs->module = UDISKS_LINUX_MODULE_BTRFS (g_value_dup_object (value));
118 break;
119
120 case PROP_BLOCK_OBJECT:
121 g_assert (l_fs_btrfs->block_object == NULL);
122 /* we don't take reference to block_object */
123 l_fs_btrfs->block_object = g_value_get_object (value);
124 break;
125
126 default:
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
128 break;
129 }
130 }
131
132 static void
133 udisks_linux_filesystem_btrfs_finalize (GObject *object)
134 {
135 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (object);
136
137 /* we don't take reference to block_object */
138 g_object_unref (l_fs_btrfs->module);
139
140 if (G_OBJECT_CLASS (udisks_linux_filesystem_btrfs_parent_class))
141 G_OBJECT_CLASS (udisks_linux_filesystem_btrfs_parent_class)->finalize (object);
142 }
143
144 static void
145 udisks_linux_filesystem_btrfs_class_init (UDisksLinuxFilesystemBTRFSClass *klass)
146 {
147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
148
149 gobject_class->get_property = udisks_linux_filesystem_btrfs_get_property;
150 gobject_class->set_property = udisks_linux_filesystem_btrfs_set_property;
151 gobject_class->finalize = udisks_linux_filesystem_btrfs_finalize;
152
153 /**
154 * UDisksLinuxFilesystemBTRFS:module:
155 *
156 * The #UDisksModule for the object.
157 */
158 g_object_class_install_property (gobject_class,
159 PROP_MODULE,
160 g_param_spec_object ("module",
161 "Module",
162 "The module for the object",
163 UDISKS_TYPE_MODULE,
164 G_PARAM_READABLE |
165 G_PARAM_WRITABLE |
166 G_PARAM_CONSTRUCT_ONLY |
167 G_PARAM_STATIC_STRINGS));
168 /**
169 * UDisksLinuxFilesystemBTRFS:blockobject:
170 *
171 * The #UDisksLinuxBlockObject for the object.
172 */
173 g_object_class_install_property (gobject_class,
174 PROP_BLOCK_OBJECT,
175 g_param_spec_object ("blockobject",
176 "Block object",
177 "The block object for the interface",
178 UDISKS_TYPE_LINUX_BLOCK_OBJECT,
179 G_PARAM_READABLE |
180 G_PARAM_WRITABLE |
181 G_PARAM_CONSTRUCT_ONLY |
182 G_PARAM_STATIC_STRINGS));
183 }
184
185 static void
186 udisks_linux_filesystem_btrfs_init (UDisksLinuxFilesystemBTRFS *l_fs_btrfs)
187 {
188 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (l_fs_btrfs),
189 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
190 }
191
192 /**
193 * udisks_linux_filesystem_btrfs_new:
194 * @module: A #UDisksLinuxModuleBTRFS.
195 * @block_object: A #UDisksLinuxBlockObject.
196 *
197 * Creates a new #UDisksLinuxFilesystemBTRFS instance.
198 *
199 * Returns: A new #UDisksLinuxFilesystemBTRFS. Free with g_object_unref().
200 */
201 UDisksLinuxFilesystemBTRFS *
202 udisks_linux_filesystem_btrfs_new (UDisksLinuxModuleBTRFS *module,
203 UDisksLinuxBlockObject *block_object)
204 {
205 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_BTRFS (module), NULL);
206 g_return_val_if_fail (UDISKS_IS_LINUX_BLOCK_OBJECT (block_object), NULL);
207 return UDISKS_LINUX_FILESYSTEM_BTRFS (g_object_new (UDISKS_TYPE_LINUX_FILESYSTEM_BTRFS,
208 "module", UDISKS_MODULE (module),
209 "blockobject", block_object,
210 NULL));
211 }
212
213 /**
214 * udisks_linux_filesystem_btrfs_get_module:
215 * @fs: A #UDisksLinuxFilesystemBTRFS.
216 *
217 * Gets the module used by @l_fs_btrfs.
218 *
219 * Returns: A #UDisksLinuxModuleBTRFS. Do not free, the object is owned by @l_fs_btrfs.
220 */
221 UDisksLinuxModuleBTRFS *
222 udisks_linux_filesystem_btrfs_get_module (UDisksLinuxFilesystemBTRFS *l_fs_btrfs)
223 {
224 g_return_val_if_fail (UDISKS_IS_LINUX_FILESYSTEM_BTRFS (l_fs_btrfs), NULL);
225 return l_fs_btrfs->module;
226 }
227
228 /**
229 * udisks_linux_filesystem_btrfs_update:
230 * @l_fs_btrfs: A #UDisksLinuxFilesystemBTRFS.
231 * @object: The enclosing #UDisksLlinuxDriveObject instance.
232 *
233 * Updates the interface.
234 *
235 * Returns: %TRUE if the configuration has changed, %FALSE otherwise.
236 */
237 gboolean
238 udisks_linux_filesystem_btrfs_update (UDisksLinuxFilesystemBTRFS *l_fs_btrfs,
239 UDisksLinuxBlockObject *object)
240 {
241 UDisksFilesystemBTRFS *fs_btrfs = UDISKS_FILESYSTEM_BTRFS (l_fs_btrfs);
242 BDBtrfsFilesystemInfo *btrfs_info = NULL;
243 GError *error = NULL;
244 gchar *dev_file = NULL;
245 gboolean rval = FALSE;
246
247 g_return_val_if_fail (UDISKS_IS_LINUX_FILESYSTEM_BTRFS (fs_btrfs), FALSE);
248 g_return_val_if_fail (UDISKS_IS_LINUX_BLOCK_OBJECT (object), FALSE);
249
250 dev_file = udisks_linux_block_object_get_device_file (object);
251 if (! dev_file)
252 {
253 rval = FALSE;
254 goto out;
255 }
256
257 btrfs_info = bd_btrfs_filesystem_info (dev_file, &error);
258
259 if (! btrfs_info)
260 {
261 udisks_critical ("Can't get BTRFS filesystem info for %s", dev_file);
262 rval = FALSE;
263 goto out;
264 }
265
266 /* Update the interface */
267 udisks_filesystem_btrfs_set_label (fs_btrfs, btrfs_info->label);
268 udisks_filesystem_btrfs_set_uuid (fs_btrfs, btrfs_info->uuid);
269 udisks_filesystem_btrfs_set_num_devices (fs_btrfs, btrfs_info->num_devices);
270 udisks_filesystem_btrfs_set_used (fs_btrfs, btrfs_info->used);
271
272 out:
273 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (fs_btrfs));
274 if (btrfs_info)
275 bd_btrfs_filesystem_info_free (btrfs_info);
276 if (error)
277 g_clear_error (&error);
278 g_free (dev_file);
279
280 return rval;
281 }
282
283 /**
284 * udisks_filesystem_btrfs_get_first_mount_point:
285 *
286 * @fs_btrfs: A #UDisksFilesystemBTRFS.
287 *
288 * Returns: the first mount_point for the given BTRFS volume. Free with g_free().
289 */
290 static gchar *
291 udisks_filesystem_btrfs_get_first_mount_point (UDisksFilesystemBTRFS *fs_btrfs,
292 GError **error)
293 {
294 UDisksObject *object;
295 UDisksFilesystem *fs;
296 const gchar * const *mount_points;
297 gchar *mount_point = NULL;
298
299 g_return_val_if_fail (UDISKS_IS_FILESYSTEM_BTRFS (fs_btrfs), NULL);
300
301 /* Get enclosing object for this interface. */
302 object = udisks_daemon_util_dup_object (fs_btrfs, error);
303 g_return_val_if_fail (object, NULL);
304
305 /* Get UDisksFilesystem. */
306 fs = udisks_object_peek_filesystem (object);
307 if (fs != NULL)
308 {
309 mount_points = udisks_filesystem_get_mount_points (fs);
310 if (mount_points != NULL && *mount_points != NULL)
311 mount_point = g_strdup (*mount_points);
312 }
313
314 g_object_unref (object);
315
316 if (mount_point == NULL)
317 {
318 g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_NOT_MOUNTED,
319 "Volume not mounted");
320 return NULL;
321 }
322 return mount_point;
323 }
324
325 static gboolean
326 handle_set_label (UDisksFilesystemBTRFS *fs_btrfs,
327 GDBusMethodInvocation *invocation,
328 const gchar *arg_label,
329 GVariant *arg_options)
330 {
331 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
332 UDisksLinuxBlockObject *object = NULL;
333 UDisksDaemon *daemon;
334 GError *error = NULL;
335 gchar *dev_file = NULL;
336
337 object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
338 if (! object)
339 {
340 g_dbus_method_invocation_take_error (invocation, error);
341 goto out;
342 }
343
344 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
345
346 /* Policy check. */
347 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
348 UDISKS_OBJECT (object),
349 BTRFS_POLICY_ACTION_ID,
350 arg_options,
351 N_("Authentication is required to change label for BTRFS volume"),
352 invocation);
353
354 /* Get the device filename (eg.: /dev/sda1) */
355 dev_file = udisks_linux_block_object_get_device_file (object);
356 if (! dev_file)
357 {
358 g_dbus_method_invocation_return_error_literal (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
359 "Cannot find the device file");
360 goto out;
361 }
362
363 /* Change the label. */
364 if (! bd_btrfs_change_label (dev_file, arg_label, &error))
365 {
366 g_dbus_method_invocation_take_error (invocation, error);
367 goto out;
368 }
369
370 udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
371
372 /* Complete DBus call. */
373 udisks_filesystem_btrfs_complete_set_label (fs_btrfs, invocation);
374 out:
375 g_clear_object (&object);
376 g_free (dev_file);
377 return TRUE;
378 }
379
380 static gboolean
381 btrfs_subvolume_perform_action (UDisksFilesystemBTRFS *fs_btrfs,
382 GDBusMethodInvocation *invocation,
383 btrfs_subvolume_func subvolume_action,
384 const gchar *arg_name,
385 GVariant *arg_options,
386 const gchar *polkit_message)
387 {
388 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
389 UDisksLinuxBlockObject *object = NULL;
390 UDisksDaemon *daemon;
391 GError *error = NULL;
392 gchar *mount_point = NULL;
393
394 object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
395 if (! object)
396 {
397 g_dbus_method_invocation_take_error (invocation, error);
398 goto out;
399 }
400
401 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
402
403 /* Policy check. */
404 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
405 UDISKS_OBJECT (object),
406 BTRFS_POLICY_ACTION_ID,
407 arg_options,
408 N_(polkit_message),
409 invocation);
410
411 /* Do we have a valid subvolume name? */
412 if (! *arg_name)
413 {
414 g_dbus_method_invocation_return_error (invocation,
415 UDISKS_ERROR,
416 UDISKS_ERROR_FAILED,
417 "Invalid subvolume name");
418 goto out;
419 }
420
421 /* Get the mount point for this volume. */
422 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
423 if (! mount_point)
424 {
425 g_dbus_method_invocation_take_error (invocation, error);
426 goto out;
427 }
428
429 /* Add/remove the subvolume. */
430 if (! subvolume_action (mount_point, arg_name, NULL, &error))
431 {
432 g_dbus_method_invocation_take_error (invocation, error);
433 goto out;
434 }
435
436 udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
437
438 /* Complete DBus call. */
439 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
440
441 out:
442 /* Release the resources */
443 g_clear_object (&object);
444 g_free (mount_point);
445
446 /* Indicate that we handled the method invocation */
447 return TRUE;
448 }
449
450 static gboolean
451 btrfs_device_perform_action (UDisksFilesystemBTRFS *fs_btrfs,
452 GDBusMethodInvocation *invocation,
453 btrfs_device_func device_action,
454 const gchar *arg_device,
455 GVariant *arg_options)
456 {
457 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
458 UDisksLinuxBlockObject *object = NULL;
459 GError *error = NULL;
460 gchar *mount_point = NULL;
461 const gchar *device;
462 UDisksDaemon *daemon;
463 UDisksObject *new_device_object = NULL;
464 UDisksBlock *new_device_block = NULL;
465
466 object = udisks_daemon_util_dup_object (fs_btrfs, &error);
467 if (! object)
468 {
469 g_dbus_method_invocation_take_error (invocation, error);
470 goto out;
471 }
472
473 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
474
475 /* Policy check. */
476 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
477 UDISKS_OBJECT (object),
478 BTRFS_POLICY_ACTION_ID,
479 arg_options,
480 N_("Authentication is required to add the device to the volume"),
481 invocation);
482
483 /* Get the mount point for this volume. */
484 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
485 if (! mount_point)
486 {
487 g_dbus_method_invocation_take_error (invocation, error);
488 goto out;
489 }
490
491 new_device_object = udisks_daemon_find_object (daemon, arg_device);
492 if (new_device_object == NULL)
493 {
494 g_dbus_method_invocation_return_error (invocation,
495 UDISKS_ERROR,
496 UDISKS_ERROR_FAILED,
497 "Invalid object path %s",
498 arg_device);
499 goto out;
500 }
501
502 new_device_block = udisks_object_get_block (new_device_object);
503 if (new_device_block == NULL)
504 {
505 g_dbus_method_invocation_return_error (invocation,
506 UDISKS_ERROR,
507 UDISKS_ERROR_FAILED,
508 "Object path %s is not a block device",
509 arg_device);
510 goto out;
511 }
512
513 device = udisks_block_get_device (new_device_block);
514
515 /* Add/remove the device to/from the volume. */
516 if (! device_action (mount_point, device, NULL, &error))
517 {
518 g_dbus_method_invocation_take_error (invocation, error);
519 goto out;
520 }
521
522 /* Trigger uevent on the filesystem and on the added/removed device */
523 udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
524 udisks_daemon_util_trigger_uevent_sync (daemon, device, NULL, UDISKS_DEFAULT_WAIT_TIMEOUT);
525
526 /* Complete DBus call. */
527 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
528
529 out:
530 /* Release the resources */
531 g_clear_object (&object);
532 g_clear_object (&new_device_object);
533 g_clear_object (&new_device_block);
534 g_free (mount_point);
535
536 /* Indicate that we handled the method invocation. */
537 return TRUE;
538 }
539
540 static gboolean
541 handle_add_device (UDisksFilesystemBTRFS *fs_btrfs,
542 GDBusMethodInvocation *invocation,
543 const gchar *arg_device,
544 GVariant *arg_options)
545 {
546 return btrfs_device_perform_action (fs_btrfs,
547 invocation,
548 bd_btrfs_add_device,
549 arg_device,
550 arg_options);
551 }
552
553 static gboolean
554 handle_remove_device (UDisksFilesystemBTRFS *fs_btrfs,
555 GDBusMethodInvocation *invocation,
556 const gchar *arg_device,
557 GVariant *arg_options)
558 {
559 return btrfs_device_perform_action (fs_btrfs,
560 invocation,
561 bd_btrfs_remove_device,
562 arg_device,
563 arg_options);
564 }
565
566 static gboolean
567 handle_create_subvolume (UDisksFilesystemBTRFS *fs_btrfs,
568 GDBusMethodInvocation *invocation,
569 const gchar *arg_name,
570 GVariant *arg_options)
571 {
572 return btrfs_subvolume_perform_action (fs_btrfs,
573 invocation,
574 bd_btrfs_create_subvolume,
575 arg_name,
576 arg_options,
577 "Authentication is required to add a new subvolume for the given BTRFS volume");
578 }
579
580 static gboolean
581 handle_remove_subvolume (UDisksFilesystemBTRFS *fs_btrfs,
582 GDBusMethodInvocation *invocation,
583 const gchar *arg_name,
584 GVariant *arg_options)
585 {
586 return btrfs_subvolume_perform_action (fs_btrfs,
587 invocation,
588 bd_btrfs_delete_subvolume,
589 arg_name,
590 arg_options,
591 "Authentication is required to remove the subvolume for the given BTRFS volume");
592 }
593
594 static gboolean
595 handle_get_subvolumes (UDisksFilesystemBTRFS *fs_btrfs,
596 GDBusMethodInvocation *invocation,
597 gboolean arg_snapshots_only,
598 GVariant *arg_options)
599 {
600 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
601 UDisksLinuxBlockObject *object = NULL;
602 UDisksDaemon *daemon;
603 BDBtrfsSubvolumeInfo **subvolumes_info = NULL;
604 GVariant *subvolumes = NULL;
605 GError *error = NULL;
606 gchar *mount_point = NULL;
607 gint subvolumes_cnt = 0;
608
609 object = udisks_daemon_util_dup_object (fs_btrfs, &error);
610 if (! object)
611 {
612 g_dbus_method_invocation_take_error (invocation, error);
613 goto out;
614 }
615
616 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
617
618 /* Policy check. */
619 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
620 UDISKS_OBJECT (object),
621 BTRFS_POLICY_ACTION_ID,
622 arg_options,
623 N_("Authentication is required to get BTRFS subvolumes"),
624 invocation);
625
626 /* Get the mount point for this volume. */
627 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
628 if (! mount_point)
629 {
630 g_dbus_method_invocation_take_error (invocation, error);
631 goto out;
632 }
633
634 /* Get subvolume infos. */
635 subvolumes_info = bd_btrfs_list_subvolumes (mount_point,
636 arg_snapshots_only,
637 &error);
638
639 if (! subvolumes_info && error)
640 {
641 g_dbus_method_invocation_take_error (invocation, error);
642 goto out;
643 }
644
645 subvolumes = btrfs_subvolumes_to_gvariant (subvolumes_info, &subvolumes_cnt);
646
647 /* Complete DBus call. */
648 udisks_filesystem_btrfs_complete_get_subvolumes (fs_btrfs,
649 invocation,
650 subvolumes,
651 subvolumes_cnt);
652
653 out:
654 /* Release the resources */
655 g_clear_object (&object);
656 btrfs_free_subvolumes_info (subvolumes_info);
657 g_free (mount_point);
658
659 /* Indicate that we handled the method invocation */
660 return TRUE;
661 }
662
663 static gboolean
664 handle_get_default_subvolume_id (UDisksFilesystemBTRFS *fs_btrfs,
665 GDBusMethodInvocation *invocation,
666 GVariant *arg_options)
667 {
668 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
669 UDisksLinuxBlockObject *object = NULL;
670 GError *error = NULL;
671 gchar *mount_point = NULL;
672 guint64 default_id;
673
674 object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
675 if (! object)
676 {
677 g_dbus_method_invocation_take_error (invocation, error);
678 goto out;
679 }
680
681 /* Get the mount point for this volume. */
682 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
683 if (! mount_point)
684 {
685 g_dbus_method_invocation_take_error (invocation, error);
686 goto out;
687 }
688
689 default_id = bd_btrfs_get_default_subvolume_id (mount_point, &error);
690 if (! default_id && error)
691 {
692 g_dbus_method_invocation_take_error (invocation, error);
693 goto out;
694 }
695
696 /* Complete DBus call. */
697 udisks_filesystem_btrfs_complete_get_default_subvolume_id (fs_btrfs, invocation, default_id);
698
699 out:
700 /* Release the resources */
701 g_clear_object (&object);
702 g_free (mount_point);
703
704 /* Indicate that we handled the method invocation */
705 return TRUE;
706 }
707
708 static gboolean
709 handle_set_default_subvolume_id (UDisksFilesystemBTRFS *fs_btrfs,
710 GDBusMethodInvocation *invocation,
711 guint arg_id,
712 GVariant *arg_options)
713 {
714 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
715 UDisksLinuxBlockObject *object = NULL;
716 GError *error = NULL;
717 UDisksDaemon *daemon;
718 gchar *mount_point = NULL;
719
720 object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
721 if (! object)
722 {
723 g_dbus_method_invocation_take_error (invocation, error);
724 goto out;
725 }
726
727 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
728
729 /* Policy check. */
730 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
731 UDISKS_OBJECT (object),
732 BTRFS_POLICY_ACTION_ID,
733 arg_options,
734 N_("Authentication is required to set the default BTRFS subvolume"),
735 invocation);
736
737 /* Get the mount point for this volume. */
738 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
739 if (! mount_point)
740 {
741 g_dbus_method_invocation_take_error (invocation, error);
742 goto out;
743 }
744
745 if (! bd_btrfs_set_default_subvolume (mount_point, arg_id, NULL, &error))
746 {
747 g_dbus_method_invocation_take_error (invocation, error);
748 goto out;
749 }
750
751 /* Complete DBus call. */
752 udisks_filesystem_btrfs_complete_set_default_subvolume_id (fs_btrfs, invocation);
753
754 out:
755 /* Release the resources */
756 g_clear_object (&object);
757 g_free (mount_point);
758
759 /* Indicate that we handled the method invocation */
760 return TRUE;
761 }
762
763 static gboolean
764 handle_create_snapshot (UDisksFilesystemBTRFS *fs_btrfs,
765 GDBusMethodInvocation *invocation,
766 const gchar *arg_source,
767 const gchar *arg_dest,
768 gboolean arg_ro,
769 GVariant *arg_options)
770 {
771 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
772 UDisksLinuxBlockObject *object = NULL;
773 UDisksDaemon *daemon;
774 GError *error = NULL;
775 gchar *source = NULL;
776 gchar *dest = NULL;
777 gchar *mount_point = NULL;
778
779 object = udisks_daemon_util_dup_object (fs_btrfs, &error);
780 if (! object)
781 {
782 g_dbus_method_invocation_take_error (invocation, error);
783 goto out;
784 }
785
786 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
787
788 /* Policy check. */
789 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
790 UDISKS_OBJECT (object),
791 BTRFS_POLICY_ACTION_ID,
792 arg_options,
793 N_("Authentication is required to create a new snapshot"),
794 invocation);
795
796 /* Prefix source and destination directories with mount point; so that user
797 * doesn't need to always type in a full path.
798 */
799 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
800 if (! mount_point)
801 {
802 g_dbus_method_invocation_take_error (invocation, error);
803 goto out;
804 }
805 source = g_build_path (G_DIR_SEPARATOR_S, mount_point, arg_source, NULL);
806 dest = g_build_path (G_DIR_SEPARATOR_S, mount_point, arg_dest, NULL);
807
808 /* Create the snapshot. */
809 if (! bd_btrfs_create_snapshot (source, dest, arg_ro, NULL, &error))
810 {
811 g_dbus_method_invocation_take_error (invocation, error);
812 goto out;
813 }
814
815 /* Complete DBus call. */
816 udisks_filesystem_btrfs_complete_create_snapshot (fs_btrfs, invocation);
817
818 out:
819 /* Release the resources */
820 g_clear_object (&object);
821 g_free (source);
822 g_free (dest);
823 g_free (mount_point);
824
825 /* Indicate that we handled the method invocation */
826 return TRUE;
827 }
828
829 static gboolean
830 handle_repair (UDisksFilesystemBTRFS *fs_btrfs,
831 GDBusMethodInvocation *invocation,
832 GVariant *arg_options)
833 {
834 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
835 UDisksLinuxBlockObject *object = NULL;
836 UDisksDaemon *daemon;
837 GError *error = NULL;
838 gchar *dev_file = NULL;
839
840 object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
841 if (! object)
842 {
843 g_dbus_method_invocation_take_error (invocation, error);
844 goto out;
845 }
846
847 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
848
849 /* Policy check. */
850 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
851 UDISKS_OBJECT (object),
852 BTRFS_POLICY_ACTION_ID,
853 arg_options,
854 N_("Authentication is required to check and repair the volume"),
855 invocation);
856
857 /* Get the device filename (eg.: /dev/sda1) */
858 dev_file = udisks_linux_block_object_get_device_file (object);
859 if (! dev_file)
860 {
861 g_dbus_method_invocation_return_error_literal (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
862 "Cannot find the device file");
863 goto out;
864 }
865
866 /* Check and repair. */
867 if (! bd_btrfs_repair (dev_file, NULL, &error))
868 {
869 g_dbus_method_invocation_take_error (invocation, error);
870 goto out;
871 }
872
873 udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
874
875 /* Complete DBus call. */
876 udisks_filesystem_btrfs_complete_repair (fs_btrfs, invocation);
877
878 out:
879 g_clear_object (&object);
880 g_free (dev_file);
881
882 /* Indicate that we handled the method invocation */
883 return TRUE;
884 }
885
886 static gboolean
887 handle_resize (UDisksFilesystemBTRFS *fs_btrfs,
888 GDBusMethodInvocation *invocation,
889 guint64 arg_size,
890 GVariant *arg_options)
891 {
892 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
893 UDisksLinuxBlockObject *object = NULL;
894 UDisksDaemon *daemon;
895 GError *error = NULL;
896 gchar *mount_point = NULL;
897
898 object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
899 if (! object)
900 {
901 g_dbus_method_invocation_take_error (invocation, error);
902 goto out;
903 }
904
905 daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
906
907 /* Policy check. */
908 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
909 UDISKS_OBJECT (object),
910 BTRFS_POLICY_ACTION_ID,
911 arg_options,
912 N_("Authentication is required to resize the volume"),
913 invocation);
914
915 /* Get the mount point for this volume. */
916 mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
917 if (! mount_point)
918 {
919 g_dbus_method_invocation_take_error (invocation, error);
920 goto out;
921 }
922
923 /* Resize the volume. */
924 if (! bd_btrfs_resize (mount_point, arg_size, NULL, &error))
925 {
926 g_dbus_method_invocation_take_error (invocation, error);
927 goto out;
928 }
929
930 udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
931
932 /* Complete DBus call. */
933 udisks_filesystem_btrfs_complete_resize (fs_btrfs, invocation);
934
935 out:
936 g_clear_object (&object);
937 g_free (mount_point);
938
939 /* Indicate that we handled the method invocation */
940 return TRUE;
941 }
942
943 static void
944 udisks_linux_filesystem_btrfs_iface_init (UDisksFilesystemBTRFSIface *iface)
945 {
946 iface->handle_set_label = handle_set_label;
947 iface->handle_add_device = handle_add_device;
948 iface->handle_remove_device = handle_remove_device;
949 iface->handle_create_subvolume = handle_create_subvolume;
950 iface->handle_remove_subvolume = handle_remove_subvolume;
951 iface->handle_get_subvolumes = handle_get_subvolumes;
952 iface->handle_get_default_subvolume_id = handle_get_default_subvolume_id;
953 iface->handle_set_default_subvolume_id = handle_set_default_subvolume_id;
954 iface->handle_create_snapshot = handle_create_snapshot;
955 iface->handle_repair = handle_repair;
956 iface->handle_resize = handle_resize;
957 }
958
959 /* -------------------------------------------------------------------------- */
960
961 static gboolean
962 udisks_linux_filesystem_btrfs_module_object_process_uevent (UDisksModuleObject *module_object,
963 const gchar *action,
964 UDisksLinuxDevice *device,
965 gboolean *keep)
966 {
967 UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (module_object);
968 const gchar *fs_type = NULL;
969
970 g_return_val_if_fail (UDISKS_IS_LINUX_FILESYSTEM_BTRFS (module_object), FALSE);
971
972 if (device == NULL)
973 return FALSE;
974
975 /* Check filesystem type from udev property. */
976 fs_type = g_udev_device_get_property (device->udev_device, "ID_FS_TYPE");
977 *keep = g_strcmp0 (fs_type, "btrfs") == 0;
978 if (*keep)
979 {
980 udisks_linux_filesystem_btrfs_update (l_fs_btrfs, l_fs_btrfs->block_object);
981 }
982
983 return TRUE;
984 }
985
986 static void
987 udisks_linux_filesystem_btrfs_module_object_iface_init (UDisksModuleObjectIface *iface)
988 {
989 iface->process_uevent = udisks_linux_filesystem_btrfs_module_object_process_uevent;
990 }