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
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2015 Peter Hatina <phatina@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <glib/gi18n.h>
#include <src/udisksdaemon.h>
#include <src/udisksdaemonutil.h>
#include <src/udiskslinuxblockobject.h>
#include <src/udiskslinuxdevice.h>
#include <src/udiskslogging.h>
#include <src/udisksmodule.h>
#include <src/udisksmoduleobject.h>
#include <blockdev/btrfs.h>
#include "udiskslinuxmodulebtrfs.h"
#include "udiskslinuxfilesystembtrfs.h"
#include "udisksbtrfsutil.h"
/**
* SECTION:udiskslinuxfilesystembtrfs
* @title: UDisksLinuxFilesystemBTRFS
* @short_description: Linux implementation of #UDisksFilesystemBTRFS
*
* This type provides an implementation of #UDisksFilesystemBTRFS interface
* on Linux.
*/
/**
* UDisksLinuxFilesystemBTRFS:
*
* The #UDisksLinuxFilesystemBTRFS structure contains only private data and
* should be only accessed using provided API.
*/
struct _UDisksLinuxFilesystemBTRFS {
UDisksFilesystemBTRFSSkeleton parent_instance;
UDisksLinuxModuleBTRFS *module;
UDisksLinuxBlockObject *block_object;
};
struct _UDisksLinuxFilesystemBTRFSClass {
UDisksFilesystemBTRFSSkeletonClass parent_class;
};
typedef gboolean (*btrfs_subvolume_func)(const gchar *mount_point, const gchar *name, const BDExtraArg **extra, GError **error);
typedef gboolean (*btrfs_device_func)(const gchar *mountpoint, const gchar *device, const BDExtraArg **extra, GError **error);
enum
{
PROP_0,
PROP_MODULE,
PROP_BLOCK_OBJECT,
N_PROPERTIES
};
static void udisks_linux_filesystem_btrfs_iface_init (UDisksFilesystemBTRFSIface *iface);
static void udisks_linux_filesystem_btrfs_module_object_iface_init (UDisksModuleObjectIface *iface);
G_DEFINE_TYPE_WITH_CODE (UDisksLinuxFilesystemBTRFS, udisks_linux_filesystem_btrfs, UDISKS_TYPE_FILESYSTEM_BTRFS_SKELETON,
G_IMPLEMENT_INTERFACE (UDISKS_TYPE_FILESYSTEM_BTRFS, udisks_linux_filesystem_btrfs_iface_init)
G_IMPLEMENT_INTERFACE (UDISKS_TYPE_MODULE_OBJECT, udisks_linux_filesystem_btrfs_module_object_iface_init));
static void
udisks_linux_filesystem_btrfs_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (object);
switch (property_id)
{
case PROP_MODULE:
g_value_set_object (value, UDISKS_MODULE (udisks_linux_filesystem_btrfs_get_module (l_fs_btrfs)));
break;
case PROP_BLOCK_OBJECT:
g_value_set_object (value, l_fs_btrfs->block_object);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
udisks_linux_filesystem_btrfs_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (object);
switch (property_id)
{
case PROP_MODULE:
g_assert (l_fs_btrfs->module == NULL);
l_fs_btrfs->module = UDISKS_LINUX_MODULE_BTRFS (g_value_dup_object (value));
break;
case PROP_BLOCK_OBJECT:
g_assert (l_fs_btrfs->block_object == NULL);
/* we don't take reference to block_object */
l_fs_btrfs->block_object = g_value_get_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
udisks_linux_filesystem_btrfs_finalize (GObject *object)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (object);
/* we don't take reference to block_object */
g_object_unref (l_fs_btrfs->module);
if (G_OBJECT_CLASS (udisks_linux_filesystem_btrfs_parent_class))
G_OBJECT_CLASS (udisks_linux_filesystem_btrfs_parent_class)->finalize (object);
}
static void
udisks_linux_filesystem_btrfs_class_init (UDisksLinuxFilesystemBTRFSClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->get_property = udisks_linux_filesystem_btrfs_get_property;
gobject_class->set_property = udisks_linux_filesystem_btrfs_set_property;
gobject_class->finalize = udisks_linux_filesystem_btrfs_finalize;
/**
* UDisksLinuxFilesystemBTRFS:module:
*
* The #UDisksModule for the object.
*/
g_object_class_install_property (gobject_class,
PROP_MODULE,
g_param_spec_object ("module",
"Module",
"The module for the object",
UDISKS_TYPE_MODULE,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/**
* UDisksLinuxFilesystemBTRFS:blockobject:
*
* The #UDisksLinuxBlockObject for the object.
*/
g_object_class_install_property (gobject_class,
PROP_BLOCK_OBJECT,
g_param_spec_object ("blockobject",
"Block object",
"The block object for the interface",
UDISKS_TYPE_LINUX_BLOCK_OBJECT,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}
static void
udisks_linux_filesystem_btrfs_init (UDisksLinuxFilesystemBTRFS *l_fs_btrfs)
{
g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (l_fs_btrfs),
G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
}
/**
* udisks_linux_filesystem_btrfs_new:
* @module: A #UDisksLinuxModuleBTRFS.
* @block_object: A #UDisksLinuxBlockObject.
*
* Creates a new #UDisksLinuxFilesystemBTRFS instance.
*
* Returns: A new #UDisksLinuxFilesystemBTRFS. Free with g_object_unref().
*/
UDisksLinuxFilesystemBTRFS *
udisks_linux_filesystem_btrfs_new (UDisksLinuxModuleBTRFS *module,
UDisksLinuxBlockObject *block_object)
{
g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_BTRFS (module), NULL);
g_return_val_if_fail (UDISKS_IS_LINUX_BLOCK_OBJECT (block_object), NULL);
return UDISKS_LINUX_FILESYSTEM_BTRFS (g_object_new (UDISKS_TYPE_LINUX_FILESYSTEM_BTRFS,
"module", UDISKS_MODULE (module),
"blockobject", block_object,
NULL));
}
/**
* udisks_linux_filesystem_btrfs_get_module:
* @fs: A #UDisksLinuxFilesystemBTRFS.
*
* Gets the module used by @l_fs_btrfs.
*
* Returns: A #UDisksLinuxModuleBTRFS. Do not free, the object is owned by @l_fs_btrfs.
*/
UDisksLinuxModuleBTRFS *
udisks_linux_filesystem_btrfs_get_module (UDisksLinuxFilesystemBTRFS *l_fs_btrfs)
{
g_return_val_if_fail (UDISKS_IS_LINUX_FILESYSTEM_BTRFS (l_fs_btrfs), NULL);
return l_fs_btrfs->module;
}
/**
* udisks_linux_filesystem_btrfs_update:
* @l_fs_btrfs: A #UDisksLinuxFilesystemBTRFS.
* @object: The enclosing #UDisksLlinuxDriveObject instance.
*
* Updates the interface.
*
* Returns: %TRUE if the configuration has changed, %FALSE otherwise.
*/
gboolean
udisks_linux_filesystem_btrfs_update (UDisksLinuxFilesystemBTRFS *l_fs_btrfs,
UDisksLinuxBlockObject *object)
{
UDisksFilesystemBTRFS *fs_btrfs = UDISKS_FILESYSTEM_BTRFS (l_fs_btrfs);
BDBtrfsFilesystemInfo *btrfs_info = NULL;
GError *error = NULL;
gchar *dev_file = NULL;
gboolean rval = FALSE;
g_return_val_if_fail (UDISKS_IS_LINUX_FILESYSTEM_BTRFS (fs_btrfs), FALSE);
g_return_val_if_fail (UDISKS_IS_LINUX_BLOCK_OBJECT (object), FALSE);
dev_file = udisks_linux_block_object_get_device_file (object);
if (! dev_file)
{
rval = FALSE;
goto out;
}
btrfs_info = bd_btrfs_filesystem_info (dev_file, &error);
if (! btrfs_info)
{
udisks_critical ("Can't get BTRFS filesystem info for %s", dev_file);
rval = FALSE;
goto out;
}
/* Update the interface */
udisks_filesystem_btrfs_set_label (fs_btrfs, btrfs_info->label);
udisks_filesystem_btrfs_set_uuid (fs_btrfs, btrfs_info->uuid);
udisks_filesystem_btrfs_set_num_devices (fs_btrfs, btrfs_info->num_devices);
udisks_filesystem_btrfs_set_used (fs_btrfs, btrfs_info->used);
out:
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (fs_btrfs));
if (btrfs_info)
bd_btrfs_filesystem_info_free (btrfs_info);
if (error)
g_clear_error (&error);
g_free (dev_file);
return rval;
}
/**
* udisks_filesystem_btrfs_get_first_mount_point:
*
* @fs_btrfs: A #UDisksFilesystemBTRFS.
*
* Returns: the first mount_point for the given BTRFS volume. Free with g_free().
*/
static gchar *
udisks_filesystem_btrfs_get_first_mount_point (UDisksFilesystemBTRFS *fs_btrfs,
GError **error)
{
UDisksObject *object;
UDisksFilesystem *fs;
const gchar * const *mount_points;
gchar *mount_point = NULL;
g_return_val_if_fail (UDISKS_IS_FILESYSTEM_BTRFS (fs_btrfs), NULL);
/* Get enclosing object for this interface. */
object = udisks_daemon_util_dup_object (fs_btrfs, error);
g_return_val_if_fail (object, NULL);
/* Get UDisksFilesystem. */
fs = udisks_object_peek_filesystem (object);
if (fs != NULL)
{
mount_points = udisks_filesystem_get_mount_points (fs);
if (mount_points != NULL && *mount_points != NULL)
mount_point = g_strdup (*mount_points);
}
g_object_unref (object);
if (mount_point == NULL)
{
g_set_error_literal (error, UDISKS_ERROR, UDISKS_ERROR_NOT_MOUNTED,
"Volume not mounted");
return NULL;
}
return mount_point;
}
static gboolean
handle_set_label (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
const gchar *arg_label,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
UDisksDaemon *daemon;
GError *error = NULL;
gchar *dev_file = NULL;
object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to change label for BTRFS volume"),
invocation);
/* Get the device filename (eg.: /dev/sda1) */
dev_file = udisks_linux_block_object_get_device_file (object);
if (! dev_file)
{
g_dbus_method_invocation_return_error_literal (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Cannot find the device file");
goto out;
}
/* Change the label. */
if (! bd_btrfs_change_label (dev_file, arg_label, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_set_label (fs_btrfs, invocation);
out:
g_clear_object (&object);
g_free (dev_file);
return TRUE;
}
static gboolean
btrfs_subvolume_perform_action (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
btrfs_subvolume_func subvolume_action,
const gchar *arg_name,
GVariant *arg_options,
const gchar *polkit_message)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
UDisksDaemon *daemon;
GError *error = NULL;
gchar *mount_point = NULL;
object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_(polkit_message),
invocation);
/* Do we have a valid subvolume name? */
if (! *arg_name)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Invalid subvolume name");
goto out;
}
/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Add/remove the subvolume. */
if (! subvolume_action (mount_point, arg_name, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
/* Complete DBus call. */
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
out:
/* Release the resources */
g_clear_object (&object);
g_free (mount_point);
/* Indicate that we handled the method invocation */
return TRUE;
}
static gboolean
btrfs_device_perform_action (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
btrfs_device_func device_action,
const gchar *arg_device,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
GError *error = NULL;
gchar *mount_point = NULL;
const gchar *device;
UDisksDaemon *daemon;
UDisksObject *new_device_object = NULL;
UDisksBlock *new_device_block = NULL;
object = udisks_daemon_util_dup_object (fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to add the device to the volume"),
invocation);
/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
new_device_object = udisks_daemon_find_object (daemon, arg_device);
if (new_device_object == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Invalid object path %s",
arg_device);
goto out;
}
new_device_block = udisks_object_get_block (new_device_object);
if (new_device_block == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Object path %s is not a block device",
arg_device);
goto out;
}
device = udisks_block_get_device (new_device_block);
/* Add/remove the device to/from the volume. */
if (! device_action (mount_point, device, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Trigger uevent on the filesystem and on the added/removed device */
udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
udisks_daemon_util_trigger_uevent_sync (daemon, device, NULL, UDISKS_DEFAULT_WAIT_TIMEOUT);
/* Complete DBus call. */
g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
out:
/* Release the resources */
g_clear_object (&object);
g_clear_object (&new_device_object);
g_clear_object (&new_device_block);
g_free (mount_point);
/* Indicate that we handled the method invocation. */
return TRUE;
}
static gboolean
handle_add_device (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
const gchar *arg_device,
GVariant *arg_options)
{
return btrfs_device_perform_action (fs_btrfs,
invocation,
bd_btrfs_add_device,
arg_device,
arg_options);
}
static gboolean
handle_remove_device (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
const gchar *arg_device,
GVariant *arg_options)
{
return btrfs_device_perform_action (fs_btrfs,
invocation,
bd_btrfs_remove_device,
arg_device,
arg_options);
}
static gboolean
handle_create_subvolume (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
const gchar *arg_name,
GVariant *arg_options)
{
return btrfs_subvolume_perform_action (fs_btrfs,
invocation,
bd_btrfs_create_subvolume,
arg_name,
arg_options,
"Authentication is required to add a new subvolume for the given BTRFS volume");
}
static gboolean
handle_remove_subvolume (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
const gchar *arg_name,
GVariant *arg_options)
{
return btrfs_subvolume_perform_action (fs_btrfs,
invocation,
bd_btrfs_delete_subvolume,
arg_name,
arg_options,
"Authentication is required to remove the subvolume for the given BTRFS volume");
}
static gboolean
handle_get_subvolumes (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
gboolean arg_snapshots_only,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
UDisksDaemon *daemon;
BDBtrfsSubvolumeInfo **subvolumes_info = NULL;
GVariant *subvolumes = NULL;
GError *error = NULL;
gchar *mount_point = NULL;
gint subvolumes_cnt = 0;
object = udisks_daemon_util_dup_object (fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to get BTRFS subvolumes"),
invocation);
/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Get subvolume infos. */
subvolumes_info = bd_btrfs_list_subvolumes (mount_point,
arg_snapshots_only,
&error);
if (! subvolumes_info && error)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
subvolumes = btrfs_subvolumes_to_gvariant (subvolumes_info, &subvolumes_cnt);
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_get_subvolumes (fs_btrfs,
invocation,
subvolumes,
subvolumes_cnt);
out:
/* Release the resources */
g_clear_object (&object);
btrfs_free_subvolumes_info (subvolumes_info);
g_free (mount_point);
/* Indicate that we handled the method invocation */
return TRUE;
}
static gboolean
handle_get_default_subvolume_id (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
GError *error = NULL;
gchar *mount_point = NULL;
guint64 default_id;
object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
default_id = bd_btrfs_get_default_subvolume_id (mount_point, &error);
if (! default_id && error)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_get_default_subvolume_id (fs_btrfs, invocation, default_id);
out:
/* Release the resources */
g_clear_object (&object);
g_free (mount_point);
/* Indicate that we handled the method invocation */
return TRUE;
}
static gboolean
handle_set_default_subvolume_id (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
guint arg_id,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
GError *error = NULL;
UDisksDaemon *daemon;
gchar *mount_point = NULL;
object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to set the default BTRFS subvolume"),
invocation);
/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
if (! bd_btrfs_set_default_subvolume (mount_point, arg_id, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_set_default_subvolume_id (fs_btrfs, invocation);
out:
/* Release the resources */
g_clear_object (&object);
g_free (mount_point);
/* Indicate that we handled the method invocation */
return TRUE;
}
static gboolean
handle_create_snapshot (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
const gchar *arg_source,
const gchar *arg_dest,
gboolean arg_ro,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
UDisksDaemon *daemon;
GError *error = NULL;
gchar *source = NULL;
gchar *dest = NULL;
gchar *mount_point = NULL;
object = udisks_daemon_util_dup_object (fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to create a new snapshot"),
invocation);
/* Prefix source and destination directories with mount point; so that user
* doesn't need to always type in a full path.
*/
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
source = g_build_path (G_DIR_SEPARATOR_S, mount_point, arg_source, NULL);
dest = g_build_path (G_DIR_SEPARATOR_S, mount_point, arg_dest, NULL);
/* Create the snapshot. */
if (! bd_btrfs_create_snapshot (source, dest, arg_ro, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_create_snapshot (fs_btrfs, invocation);
out:
/* Release the resources */
g_clear_object (&object);
g_free (source);
g_free (dest);
g_free (mount_point);
/* Indicate that we handled the method invocation */
return TRUE;
}
static gboolean
handle_repair (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
UDisksDaemon *daemon;
GError *error = NULL;
gchar *dev_file = NULL;
object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to check and repair the volume"),
invocation);
/* Get the device filename (eg.: /dev/sda1) */
dev_file = udisks_linux_block_object_get_device_file (object);
if (! dev_file)
{
g_dbus_method_invocation_return_error_literal (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
"Cannot find the device file");
goto out;
}
/* Check and repair. */
if (! bd_btrfs_repair (dev_file, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_repair (fs_btrfs, invocation);
out:
g_clear_object (&object);
g_free (dev_file);
/* Indicate that we handled the method invocation */
return TRUE;
}
static gboolean
handle_resize (UDisksFilesystemBTRFS *fs_btrfs,
GDBusMethodInvocation *invocation,
guint64 arg_size,
GVariant *arg_options)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (fs_btrfs);
UDisksLinuxBlockObject *object = NULL;
UDisksDaemon *daemon;
GError *error = NULL;
gchar *mount_point = NULL;
object = udisks_daemon_util_dup_object (l_fs_btrfs, &error);
if (! object)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_module_get_daemon (UDISKS_MODULE (l_fs_btrfs->module));
/* Policy check. */
UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
UDISKS_OBJECT (object),
BTRFS_POLICY_ACTION_ID,
arg_options,
N_("Authentication is required to resize the volume"),
invocation);
/* Get the mount point for this volume. */
mount_point = udisks_filesystem_btrfs_get_first_mount_point (fs_btrfs, &error);
if (! mount_point)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* Resize the volume. */
if (! bd_btrfs_resize (mount_point, arg_size, NULL, &error))
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
udisks_linux_block_object_trigger_uevent_sync (object, UDISKS_DEFAULT_WAIT_TIMEOUT);
/* Complete DBus call. */
udisks_filesystem_btrfs_complete_resize (fs_btrfs, invocation);
out:
g_clear_object (&object);
g_free (mount_point);
/* Indicate that we handled the method invocation */
return TRUE;
}
static void
udisks_linux_filesystem_btrfs_iface_init (UDisksFilesystemBTRFSIface *iface)
{
iface->handle_set_label = handle_set_label;
iface->handle_add_device = handle_add_device;
iface->handle_remove_device = handle_remove_device;
iface->handle_create_subvolume = handle_create_subvolume;
iface->handle_remove_subvolume = handle_remove_subvolume;
iface->handle_get_subvolumes = handle_get_subvolumes;
iface->handle_get_default_subvolume_id = handle_get_default_subvolume_id;
iface->handle_set_default_subvolume_id = handle_set_default_subvolume_id;
iface->handle_create_snapshot = handle_create_snapshot;
iface->handle_repair = handle_repair;
iface->handle_resize = handle_resize;
}
/* -------------------------------------------------------------------------- */
static gboolean
udisks_linux_filesystem_btrfs_module_object_process_uevent (UDisksModuleObject *module_object,
const gchar *action,
UDisksLinuxDevice *device,
gboolean *keep)
{
UDisksLinuxFilesystemBTRFS *l_fs_btrfs = UDISKS_LINUX_FILESYSTEM_BTRFS (module_object);
const gchar *fs_type = NULL;
g_return_val_if_fail (UDISKS_IS_LINUX_FILESYSTEM_BTRFS (module_object), FALSE);
if (device == NULL)
return FALSE;
/* Check filesystem type from udev property. */
fs_type = g_udev_device_get_property (device->udev_device, "ID_FS_TYPE");
*keep = g_strcmp0 (fs_type, "btrfs") == 0;
if (*keep)
{
udisks_linux_filesystem_btrfs_update (l_fs_btrfs, l_fs_btrfs->block_object);
}
return TRUE;
}
static void
udisks_linux_filesystem_btrfs_module_object_iface_init (UDisksModuleObjectIface *iface)
{
iface->process_uevent = udisks_linux_filesystem_btrfs_module_object_process_uevent;
}
|