neil / rpms / udisks2

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