923a60
From e7e3e1d230c15079a3d1480c47076ffd89f1de63 Mon Sep 17 00:00:00 2001
923a60
From: Michal Sekletar <msekletar@users.noreply.github.com>
923a60
Date: Mon, 16 Oct 2017 16:15:05 +0200
923a60
Subject: [PATCH] mount: make sure we unmount tmpfs mounts before we deactivate
923a60
 swaps (#7076)
923a60
923a60
In the past we introduced this property just for tmp.mount. However on
923a60
todays systems usually there are many more tmpfs mounts. Most notably
923a60
mounts backing XDG_RUNTIME_DIR for each user.
923a60
923a60
Let's generalize what we already have for tmp.mount and implement the
923a60
ordering After=swap.target for all tmpfs based mounts.
923a60
923a60
(cherry picked from commit fab35afabf01a5dea651187a1ccb5ae7cd778f9d)
923a60
923a60
Conflicts:
923a60
	src/core/mount.h
923a60
923a60
Resolves: #1437518
923a60
---
923a60
 src/core/dbus-mount.c | 10 +---------
923a60
 src/core/mount.c      | 24 ++++++++++++++++++++++++
923a60
 src/core/mount.h      |  1 +
923a60
 units/tmp.mount       |  1 -
923a60
 4 files changed, 26 insertions(+), 10 deletions(-)
923a60
923a60
diff --git a/src/core/dbus-mount.c b/src/core/dbus-mount.c
923a60
index 53fe4edc34..04beba631b 100644
923a60
--- a/src/core/dbus-mount.c
923a60
+++ b/src/core/dbus-mount.c
923a60
@@ -90,20 +90,12 @@ static int property_get_type(
923a60
                 sd_bus_error *error) {
923a60
 
923a60
         Mount *m = userdata;
923a60
-        const char *d;
923a60
 
923a60
         assert(bus);
923a60
         assert(reply);
923a60
         assert(m);
923a60
 
923a60
-        if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype)
923a60
-                d = m->parameters_proc_self_mountinfo.fstype;
923a60
-        else if (m->from_fragment && m->parameters_fragment.fstype)
923a60
-                d = m->parameters_fragment.fstype;
923a60
-        else
923a60
-                d = "";
923a60
-
923a60
-        return sd_bus_message_append(reply, "s", d);
923a60
+        return sd_bus_message_append(reply, "s", mount_get_fstype(m));
923a60
 }
923a60
 
923a60
 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, mount_result, MountResult);
923a60
diff --git a/src/core/mount.c b/src/core/mount.c
923a60
index 7ca7f5a258..a6d93b8691 100644
923a60
--- a/src/core/mount.c
923a60
+++ b/src/core/mount.c
923a60
@@ -119,6 +119,21 @@ static bool needs_quota(const MountParameters *p) {
923a60
                                  "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
923a60
 }
923a60
 
923a60
+const char *mount_get_fstype(const Mount *m) {
923a60
+        const char *type = NULL;
923a60
+
923a60
+        assert(m);
923a60
+
923a60
+        if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype)
923a60
+                type = m->parameters_proc_self_mountinfo.fstype;
923a60
+        else if (m->from_fragment && m->parameters_fragment.fstype)
923a60
+                type = m->parameters_fragment.fstype;
923a60
+        else
923a60
+                type = "";
923a60
+
923a60
+        return type;
923a60
+}
923a60
+
923a60
 static void mount_init(Unit *u) {
923a60
         Mount *m = MOUNT(u);
923a60
 
923a60
@@ -236,6 +251,7 @@ _pure_ static MountParameters* get_mount_parameters(Mount *m) {
923a60
 
923a60
 static int mount_add_mount_links(Mount *m) {
923a60
         _cleanup_free_ char *parent = NULL;
923a60
+        const char *fstype;
923a60
         MountParameters *pm;
923a60
         Unit *other;
923a60
         Iterator i;
923a60
@@ -292,6 +308,14 @@ static int mount_add_mount_links(Mount *m) {
923a60
                 }
923a60
         }
923a60
 
923a60
+        /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
923a60
+        fstype = mount_get_fstype(m);
923a60
+        if (streq(fstype, "tmpfs")) {
923a60
+                r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET, NULL, true);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+        }
923a60
+
923a60
         return 0;
923a60
 }
923a60
 
923a60
diff --git a/src/core/mount.h b/src/core/mount.h
923a60
index d6987e6fa1..353222000f 100644
923a60
--- a/src/core/mount.h
923a60
+++ b/src/core/mount.h
923a60
@@ -130,3 +130,4 @@ const char* mount_result_to_string(MountResult i) _const_;
923a60
 MountResult mount_result_from_string(const char *s) _pure_;
923a60
 
923a60
 void warn_if_dir_nonempty(const char *unit, const char* where);
923a60
+const char *mount_get_fstype(const Mount *m);
923a60
diff --git a/units/tmp.mount b/units/tmp.mount
923a60
index 8c53a87052..af0cf4a551 100644
923a60
--- a/units/tmp.mount
923a60
+++ b/units/tmp.mount
923a60
@@ -13,7 +13,6 @@ ConditionPathIsSymbolicLink=!/tmp
923a60
 DefaultDependencies=no
923a60
 Conflicts=umount.target
923a60
 Before=local-fs.target umount.target
923a60
-After=swap.target
923a60
 
923a60
 [Mount]
923a60
 What=tmpfs