neil / rpms / udisks2

Forked from rpms/udisks2 a year ago
Clone
a19a3f
From 50b7a2125f77f23901eff12c76efb80881236513 Mon Sep 17 00:00:00 2001
a19a3f
From: Marius Vollmer <mvollmer@redhat.com>
a19a3f
Date: Thu, 23 Nov 2017 11:40:50 +0200
a19a3f
Subject: [PATCH] Add 'no-discard' option to formatting methods
a19a3f
a19a3f
It unfortunately seems to be necessary to use this option in certain
a19a3f
situations.  See https://bugzilla.redhat.com/show_bug.cgi?id=1516041
a19a3f
for example.
a19a3f
a19a3f
(cherry picked from commit 26ac7429b5ef9b5024e54e6eca25b685c0666eb5)
a19a3f
a19a3f
Conflicts:
a19a3f
    src/udiskslinuxblock.c
a19a3f
a19a3f
Signed-off-by: Vratislav Podzimek <vpodzime@redhat.com>
a19a3f
---
a19a3f
 data/org.freedesktop.UDisks2.xml |  4 ++++
a19a3f
 src/tests/integration-test       | 14 +++++++-----
a19a3f
 src/udiskslinuxblock.c           | 48 ++++++++++++++++++++++++++++------------
a19a3f
 src/udiskslinuxfsinfo.c          | 31 ++++++++++++++++++--------
a19a3f
 src/udiskslinuxfsinfo.h          |  1 +
a19a3f
 5 files changed, 70 insertions(+), 28 deletions(-)
a19a3f
a19a3f
diff --git a/data/org.freedesktop.UDisks2.xml b/data/org.freedesktop.UDisks2.xml
a19a3f
index fa53a72..1f71ca1 100644
a19a3f
--- a/data/org.freedesktop.UDisks2.xml
a19a3f
+++ b/data/org.freedesktop.UDisks2.xml
a19a3f
@@ -1404,6 +1404,10 @@
a19a3f
         this allows a deeper check of the parameters even when
a19a3f
         <parameter>no-block</parameter> is %TRUE.
a19a3f
 
a19a3f
+        If the option <parameter>no-discard</parameter> is set to
a19a3f
+        %TRUE then Udisks tells the formatting utility not to issue
a19a3f
+        BLKDISCARD ioctls.
a19a3f
+
a19a3f
         If the option <parameter>config-items</parameter> is set, it
a19a3f
         should be an array of configuration items suitable for
a19a3f
         org.freedesktop.UDisks2.Block.AddConfigurationItem.  They will
a19a3f
diff --git a/src/tests/integration-test b/src/tests/integration-test
a19a3f
index f863578..0edbb6a 100755
a19a3f
--- a/src/tests/integration-test
a19a3f
+++ b/src/tests/integration-test
a19a3f
@@ -825,6 +825,8 @@ class FS(UDisksTestCase):
a19a3f
             self._do_udisks_check(fs_type, 'test%stst' % fs_type)
a19a3f
             # also test fs_create with an empty label
a19a3f
             self._do_udisks_check(fs_type, '')
a19a3f
+            # also test fs_create with the no-discard option
a19a3f
+            self._do_udisks_check(fs_type, '', True)
a19a3f
 
a19a3f
     def _do_cli_check(self, fs_type, label=None):
a19a3f
         """udisks correctly picks up file system changes from command line tools"""
a19a3f
@@ -890,15 +892,17 @@ class FS(UDisksTestCase):
a19a3f
         subprocess.call(['umount', mount_a])
a19a3f
         self.assertProperty(fs, 'mount-points', [])
a19a3f
 
a19a3f
-    def _do_udisks_check(self, fs_type, label=None):
a19a3f
+    def _do_udisks_check(self, fs_type, label=None, no_discard=None):
a19a3f
         """udisks API correctly changes file system"""
a19a3f
 
a19a3f
+
a19a3f
         # create fs
a19a3f
+        options = { }
a19a3f
         if label is not None:
a19a3f
-            options = GLib.Variant('a{sv}', {'label': GLib.Variant('s', label)})
a19a3f
-        else:
a19a3f
-            options = no_options
a19a3f
-        self.fs_create(None, fs_type, options)
a19a3f
+            options['label'] = GLib.Variant('s', label);
a19a3f
+        if no_discard is not None:
a19a3f
+            options['no-discard'] = GLib.Variant('b', no_discard);
a19a3f
+        self.fs_create(None, fs_type, GLib.Variant('a{sv}', options))
a19a3f
 
a19a3f
         # properties
a19a3f
         b_id = self.blkid()
a19a3f
diff --git a/src/udiskslinuxblock.c b/src/udiskslinuxblock.c
a19a3f
index 3d56577..0de5422 100644
a19a3f
--- a/src/udiskslinuxblock.c
a19a3f
+++ b/src/udiskslinuxblock.c
a19a3f
@@ -2656,6 +2656,27 @@ add_blocksize (gchar        **command,
a19a3f
   return TRUE;
a19a3f
 }
a19a3f
 
a19a3f
+static gchar *
a19a3f
+build_command (const gchar *template,
a19a3f
+               const gchar *device,
a19a3f
+               const gchar *label,
a19a3f
+               const gchar *options,
a19a3f
+               GError     **error)
a19a3f
+{
a19a3f
+  gchar *tmp, *tmp2, *command;
a19a3f
+  tmp = udisks_daemon_util_subst_str_and_escape (template, "$DEVICE", device);
a19a3f
+  tmp2 = udisks_daemon_util_subst_str_and_escape (tmp, "$LABEL", label != NULL ? label : "");
a19a3f
+  command = udisks_daemon_util_subst_str (tmp2, "$OPTIONS", options != NULL ? options : "");
a19a3f
+  g_free (tmp);
a19a3f
+  g_free (tmp2);
a19a3f
+  if (strstr (command, "$BLOCKSIZE") && ! add_blocksize (&command, device, error))
a19a3f
+    {
a19a3f
+      g_free (command);
a19a3f
+      return NULL;
a19a3f
+    }
a19a3f
+
a19a3f
+  return command;
a19a3f
+}
a19a3f
 
a19a3f
 void
a19a3f
 udisks_linux_block_handle_format (UDisksBlock             *block,
a19a3f
@@ -2679,8 +2700,8 @@ udisks_linux_block_handle_format (UDisksBlock             *block,
a19a3f
   const gchar *action_id;
a19a3f
   const gchar *message;
a19a3f
   const FSInfo *fs_info;
a19a3f
+  const gchar *command_options = NULL;
a19a3f
   gchar *command = NULL;
a19a3f
-  gchar *tmp;
a19a3f
   gchar *error_message;
a19a3f
   GError *error;
a19a3f
   int status;
a19a3f
@@ -2700,6 +2721,7 @@ udisks_linux_block_handle_format (UDisksBlock             *block,
a19a3f
   const gchar *partition_type = NULL;
a19a3f
   GVariant *config_items = NULL;
a19a3f
   gboolean teardown_flag = FALSE;
a19a3f
+  gboolean no_discard_flag = FALSE;
a19a3f
   BDPartTableType part_table_type = BD_PART_TABLE_UNDEF;
a19a3f
 
a19a3f
   error = NULL;
a19a3f
@@ -2723,6 +2745,7 @@ udisks_linux_block_handle_format (UDisksBlock             *block,
a19a3f
   g_variant_lookup (options, "dry-run-first", "b", &dry_run_first);
a19a3f
   g_variant_lookup (options, "config-items", "@a(sa{sv})", &config_items);
a19a3f
   g_variant_lookup (options, "tear-down", "b", &teardown_flag);
a19a3f
+  g_variant_lookup (options, "no-discard", "b", &no_discard_flag);
a19a3f
 
a19a3f
   partition = udisks_object_get_partition (object);
a19a3f
   if (partition != NULL)
a19a3f
@@ -2886,21 +2909,20 @@ udisks_linux_block_handle_format (UDisksBlock             *block,
a19a3f
       goto out;
a19a3f
     }
a19a3f
 
a19a3f
+  if (no_discard_flag && fs_info->option_no_discard)
a19a3f
+    command_options = fs_info->option_no_discard;
a19a3f
+
a19a3f
   /* If requested, check whether the ultimate filesystem creation
a19a3f
      will succeed before actually getting to work.
a19a3f
   */
a19a3f
   if (dry_run_first && fs_info->command_validate_create_fs)
a19a3f
     {
a19a3f
       const gchar *device = udisks_block_get_device (block);
a19a3f
-      tmp = udisks_daemon_util_subst_str_and_escape (fs_info->command_validate_create_fs, "$DEVICE",
a19a3f
-                                                     device);
a19a3f
-      command = udisks_daemon_util_subst_str_and_escape (tmp, "$LABEL", label != NULL ? label : "");
a19a3f
-      g_free (tmp);
a19a3f
-      if (strstr (command, "$BLOCKSIZE") && ! add_blocksize (&command, device, &error))
a19a3f
-        {
a19a3f
-          handle_format_failure (invocation, error);
a19a3f
-          goto out;
a19a3f
-        }
a19a3f
+      command = build_command (fs_info->command_validate_create_fs, device, label, command_options, &error);
a19a3f
+      if (command == NULL) {
a19a3f
+            handle_format_failure (invocation, error);
a19a3f
+            goto out;
a19a3f
+      }
a19a3f
 
a19a3f
       if (!udisks_daemon_launch_spawned_job_sync (daemon,
a19a3f
                                                     object,
a19a3f
@@ -3081,10 +3103,8 @@ udisks_linux_block_handle_format (UDisksBlock             *block,
a19a3f
       {
a19a3f
         /* Build and run mkfs shell command */
a19a3f
         const gchar *device = udisks_block_get_device (block_to_mkfs);
a19a3f
-        tmp = udisks_daemon_util_subst_str_and_escape (fs_info->command_create_fs, "$DEVICE", device);
a19a3f
-        command = udisks_daemon_util_subst_str_and_escape (tmp, "$LABEL", label != NULL ? label : "");
a19a3f
-        g_free (tmp);
a19a3f
-        if (strstr (command, "$BLOCKSIZE") && ! add_blocksize (&command, device, &error))
a19a3f
+        command = build_command (fs_info->command_create_fs, device, label, command_options, &error);
a19a3f
+        if (command == NULL)
a19a3f
           {
a19a3f
             handle_format_failure (invocation, error);
a19a3f
             goto out;
a19a3f
diff --git a/src/udiskslinuxfsinfo.c b/src/udiskslinuxfsinfo.c
a19a3f
index b7834c0..1556af3 100644
a19a3f
--- a/src/udiskslinuxfsinfo.c
a19a3f
+++ b/src/udiskslinuxfsinfo.c
a19a3f
@@ -70,8 +70,9 @@ const FSInfo _fs_info[] =
a19a3f
       NULL,
a19a3f
       TRUE,  /* supports_online_label_rename */
a19a3f
       TRUE,  /* supports_owners */
a19a3f
-      "mkfs.ext2 -F -L $LABEL $DEVICE",
a19a3f
-      "mkfs.ext2 -n -F -L $LABEL $DEVICE",
a19a3f
+      "mkfs.ext2 -F -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "mkfs.ext2 -n -F -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "-E nodiscard", /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_EXT3,
a19a3f
@@ -79,8 +80,9 @@ const FSInfo _fs_info[] =
a19a3f
       NULL,
a19a3f
       TRUE,  /* supports_online_label_rename */
a19a3f
       TRUE,  /* supports_owners */
a19a3f
-      "mkfs.ext3 -F -L $LABEL $DEVICE",
a19a3f
-      "mkfs.ext3 -n -F -L $LABEL $DEVICE",
a19a3f
+      "mkfs.ext3 -F -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "mkfs.ext3 -n -F -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "-E nodiscard", /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_EXT4,
a19a3f
@@ -88,8 +90,9 @@ const FSInfo _fs_info[] =
a19a3f
       NULL,
a19a3f
       TRUE,  /* supports_online_label_rename */
a19a3f
       TRUE,  /* supports_owners */
a19a3f
-      "mkfs.ext4 -F -L $LABEL $DEVICE",
a19a3f
-      "mkfs.ext4 -n -F -L $LABEL $DEVICE",
a19a3f
+      "mkfs.ext4 -F -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "mkfs.ext4 -n -F -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "-E nodiscard", /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_VFAT,
a19a3f
@@ -99,6 +102,7 @@ const FSInfo _fs_info[] =
a19a3f
       FALSE, /* supports_owners */
a19a3f
       "mkfs.vfat -I -n $LABEL $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL, /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_NTFS,
a19a3f
@@ -108,6 +112,7 @@ const FSInfo _fs_info[] =
a19a3f
       FALSE, /* supports_owners */
a19a3f
       "mkntfs -f -F -L $LABEL $DEVICE",
a19a3f
       "mkntfs -n -f -F -L $LABEL $DEVICE",
a19a3f
+      NULL, /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_EXFAT,
a19a3f
@@ -117,6 +122,7 @@ const FSInfo _fs_info[] =
a19a3f
       FALSE, /* supports_owners */
a19a3f
       "mkexfatfs -n $LABEL $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL, /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_XFS,
a19a3f
@@ -124,8 +130,9 @@ const FSInfo _fs_info[] =
a19a3f
       "xfs_admin -L -- $DEVICE",
a19a3f
       FALSE, /* supports_online_label_rename */
a19a3f
       TRUE,  /* supports_owners */
a19a3f
-      "mkfs.xfs -f -L $LABEL $DEVICE",
a19a3f
-      "mkfs.xfs -N -f -L $LABEL $DEVICE"
a19a3f
+      "mkfs.xfs -f -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "mkfs.xfs -N -f -L $LABEL $OPTIONS $DEVICE",
a19a3f
+      "-K" /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_REISERFS,
a19a3f
@@ -135,6 +142,7 @@ const FSInfo _fs_info[] =
a19a3f
       TRUE,  /* supports_owners */
a19a3f
       "mkfs.reiserfs -q -l $LABEL $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL, /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_NILFS2,
a19a3f
@@ -144,6 +152,7 @@ const FSInfo _fs_info[] =
a19a3f
       TRUE,  /* supports_owners */
a19a3f
       "mkfs.nilfs2 -L $LABEL $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL, /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_BTRFS,
a19a3f
@@ -151,8 +160,9 @@ const FSInfo _fs_info[] =
a19a3f
       NULL,
a19a3f
       FALSE, /* supports_online_label_rename */
a19a3f
       TRUE,  /* supports_owners */
a19a3f
-      "mkfs.btrfs -L $LABEL $DEVICE",
a19a3f
+      "mkfs.btrfs -L $LABEL $OPTIONS $DEVICE",
a19a3f
       NULL,
a19a3f
+      "-K", /* option_no_discard */
a19a3f
     },
a19a3f
     {
a19a3f
       FS_MINIX,
a19a3f
@@ -162,6 +172,7 @@ const FSInfo _fs_info[] =
a19a3f
       FALSE, /* supports_owners */
a19a3f
       "mkfs.minix $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL,
a19a3f
     },
a19a3f
     {
a19a3f
       FS_UDF,
a19a3f
@@ -171,6 +182,7 @@ const FSInfo _fs_info[] =
a19a3f
       TRUE,  /* supports_owners */
a19a3f
       "mkudffs --utf8 --media-type=hd --udfrev=0x201 --blocksize=$BLOCKSIZE --vid $LABEL --lvid $LABEL $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL,
a19a3f
     },
a19a3f
     {
a19a3f
       FS_F2FS,
a19a3f
@@ -180,6 +192,7 @@ const FSInfo _fs_info[] =
a19a3f
       TRUE,  /* supports_owners */
a19a3f
       "mkfs.f2fs -l $LABEL $DEVICE",
a19a3f
       NULL,
a19a3f
+      NULL,
a19a3f
     },
a19a3f
     /* swap space */
a19a3f
     {
a19a3f
diff --git a/src/udiskslinuxfsinfo.h b/src/udiskslinuxfsinfo.h
a19a3f
index 2daec10..6a7a541 100644
a19a3f
--- a/src/udiskslinuxfsinfo.h
a19a3f
+++ b/src/udiskslinuxfsinfo.h
a19a3f
@@ -35,6 +35,7 @@ typedef struct
a19a3f
   gboolean     supports_owners;
a19a3f
   const gchar *command_create_fs;  /* should have $DEVICE and $LABEL */
a19a3f
   const gchar *command_validate_create_fs;  /* should have $DEVICE and $LABEL */
a19a3f
+  const gchar *option_no_discard;
a19a3f
 } FSInfo;
a19a3f
 
a19a3f
 const FSInfo  *get_fs_info (const gchar *fstype);
a19a3f
-- 
a19a3f
2.9.5
a19a3f