chiark / gitweb /
execute: fix bad jump
[elogind.git] / mount.c
diff --git a/mount.c b/mount.c
index 04edc6e10fad510722382a28b824029865d3f9c5..b53c7c0d8a291a169ffbd65f1f35436fdfe9e175 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -32,6 +32,9 @@
 #include "log.h"
 #include "strv.h"
 #include "mount-setup.h"
+#include "unit-name.h"
+#include "mount.h"
+#include "dbus-mount.h"
 
 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
         [MOUNT_DEAD] = UNIT_INACTIVE,
@@ -49,23 +52,27 @@ static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
         [MOUNT_MAINTAINANCE] = UNIT_INACTIVE,
 };
 
-static const char* const state_string_table[_MOUNT_STATE_MAX] = {
-        [MOUNT_DEAD] = "dead",
-        [MOUNT_MOUNTING] = "mounting",
-        [MOUNT_MOUNTING_DONE] = "mounting-done",
-        [MOUNT_MOUNTED] = "mounted",
-        [MOUNT_REMOUNTING] = "remounting",
-        [MOUNT_UNMOUNTING] = "unmounting",
-        [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
-        [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
-        [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
-        [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
-        [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
-        [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
-        [MOUNT_MAINTAINANCE] = "maintainance"
-};
+static void mount_init(Unit *u) {
+        Mount *m = MOUNT(u);
 
-static void service_unwatch_control_pid(Mount *m) {
+        assert(u);
+        assert(u->meta.load_state == UNIT_STUB);
+
+        m->timeout_usec = DEFAULT_TIMEOUT_USEC;
+        exec_context_init(&m->exec_context);
+
+        /* We need to make sure that /bin/mount is always called in
+         * the same process group as us, so that the autofs kernel
+         * side doesn't send us another mount request while we are
+         * already trying to comply its last one. */
+        m->exec_context.no_setsid = true;
+
+        m->timer_watch.type = WATCH_INVALID;
+
+        m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
+}
+
+static void mount_unwatch_control_pid(Mount *m) {
         assert(m);
 
         if (m->control_pid <= 0)
@@ -101,128 +108,92 @@ static void mount_done(Unit *u) {
         exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
         m->control_command = NULL;
 
-        service_unwatch_control_pid(m);
+        mount_unwatch_control_pid(m);
 
         unit_unwatch_timer(u, &m->timer_watch);
 }
 
-static void mount_init(Unit *u) {
-        Mount *m = MOUNT(u);
-
-        assert(u);
-        assert(u->meta.load_state == UNIT_STUB);
-
-        m->state = 0;
-        m->from_etc_fstab = false;
-        m->from_proc_self_mountinfo = false;
-        m->from_fragment = false;
-
-        m->is_mounted = false;
-        m->just_mounted = false;
-        m->just_changed = false;
-
-        m->timeout_usec = DEFAULT_TIMEOUT_USEC;
-
-        zero(m->exec_command);
-        exec_context_init(&m->exec_context);
-
-        m->kill_mode = 0;
-
-        m->control_pid = 0;
-        m->failure = false;
-
-        m->timer_watch.type = WATCH_INVALID;
-}
-
-static int mount_add_node_links(Mount *m) {
-        Unit *device;
-        char *e;
+static int mount_add_mount_links(Mount *m) {
+        Meta *other;
         int r;
-        const char *what;
 
         assert(m);
 
-        /* Adds in links to the device that this node is based on */
+        /* Adds in links to other mount points that might lie below or
+         * above us in the hierarchy */
 
-        if (m->parameters_fragment.what)
-                what = m->parameters_fragment.what;
-        else if (m->parameters_etc_fstab.what)
-                what = m->parameters_etc_fstab.what;
-        else
-                /* We observe kernel mounts only while they are live,
-                 * hence don't create any links for them */
-                return 0;
+        LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_MOUNT]) {
+                Mount *n = (Mount*) other;
 
-        if (!path_startswith(what, "/dev/"))
-                return 0;
+                if (n == m)
+                        continue;
 
-        if (!(e = unit_name_escape_path(what+1, ".device")))
-                return -ENOMEM;
+                if (n->meta.load_state != UNIT_LOADED)
+                        continue;
 
-        r = manager_load_unit(UNIT(m)->meta.manager, e, &device);
-        free(e);
+                if (path_startswith(m->where, n->where)) {
 
-        if (r < 0)
-                return r;
+                        if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
+                                return r;
 
-        if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, device)) < 0)
-                return r;
+                        if (n->from_etc_fstab || n->from_fragment)
+                                if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
+                                        return r;
 
-        if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, device)) < 0)
-                return r;
+                } else if (path_startswith(n->where, m->where)) {
 
-        if (UNIT(m)->meta.manager->running_as == MANAGER_INIT ||
-            UNIT(m)->meta.manager->running_as == MANAGER_SYSTEM)
-                if ((r = unit_add_dependency(device, UNIT_WANTS, UNIT(m))) < 0)
-                        return r;
+                        if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(n), true)) < 0)
+                                return r;
+
+                        if (m->from_etc_fstab || m->from_fragment)
+                                if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
+                                        return r;
+                }
+        }
 
         return 0;
 }
 
-static int mount_add_path_links(Mount *m) {
+static int mount_add_swap_links(Mount *m) {
         Meta *other;
         int r;
 
         assert(m);
 
-        /* Adds in link to other mount points, that might lie below or
-         * above us in the hierarchy */
-
-        LIST_FOREACH(units_per_type, other, UNIT(m)->meta.manager->units_per_type[UNIT_MOUNT]) {
-                Mount *n;
-
-                n = (Mount*) other;
+        LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_SWAP])
+                if ((r = swap_add_one_mount_link((Swap*) other, m)) < 0)
+                        return r;
 
-                if (n == m)
-                        continue;
+        return 0;
+}
 
-                if (m->meta.load_state != UNIT_LOADED)
-                        continue;
+static int mount_add_automount_links(Mount *m) {
+        Meta *other;
+        int r;
 
-                if (path_startswith(m->where, n->where)) {
+        assert(m);
 
-                        if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(other))) < 0)
-                                return r;
+        LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_AUTOMOUNT])
+                if ((r = automount_add_one_mount_link((Automount*) other, m)) < 0)
+                        return r;
 
-                        if (n->from_etc_fstab || n->from_fragment)
-                                if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(other))) < 0)
-                                        return r;
+        return 0;
+}
 
-                } else if (path_startswith(n->where, m->where)) {
+static int mount_add_socket_links(Mount *m) {
+        Meta *other;
+        int r;
 
-                        if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(other))) < 0)
-                                return r;
+        assert(m);
 
-                        if (m->from_etc_fstab || m->from_fragment)
-                                if ((r = unit_add_dependency(UNIT(other), UNIT_REQUIRES, UNIT(m))) < 0)
-                                        return r;
-                }
-        }
+        LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_SOCKET])
+                if ((r = socket_add_one_mount_link((Socket*) other, m)) < 0)
+                        return r;
 
         return 0;
 }
 
-static bool mount_test_option(const char *haystack, const char *needle) {
+static char* mount_test_option(const char *haystack, const char *needle) {
         struct mntent me;
 
         assert(needle);
@@ -236,14 +207,15 @@ static bool mount_test_option(const char *haystack, const char *needle) {
         zero(me);
         me.mnt_opts = (char*) haystack;
 
-        return !!hasmntopt(&me, needle);
+        return hasmntopt(&me, needle);
 }
 
 static int mount_add_target_links(Mount *m) {
         const char *target;
         MountParameters *p;
-        Unit *u;
+        Unit *tu;
         int r;
+        bool noauto, handle, automount;
 
         assert(m);
 
@@ -254,11 +226,9 @@ static int mount_add_target_links(Mount *m) {
         else
                 return 0;
 
-        if (!p->fstype)
-                return 0;
-
-        if (mount_test_option(p->options, MNTOPT_NOAUTO))
-                return 0;
+        noauto = !!mount_test_option(p->options, MNTOPT_NOAUTO);
+        handle = !!mount_test_option(p->options, "comment=systemd.mount");
+        automount = !!mount_test_option(p->options, "comment=systemd.automount");
 
         if (mount_test_option(p->options, "_netdev") ||
             fstype_is_network(p->fstype))
@@ -266,13 +236,55 @@ static int mount_add_target_links(Mount *m) {
         else
                 target = SPECIAL_LOCAL_FS_TARGET;
 
-        if ((r = manager_load_unit(UNIT(m)->meta.manager, target, &u)) < 0)
+        if ((r = manager_load_unit(UNIT(m)->meta.manager, target, NULL, &tu)) < 0)
                 return r;
 
-        if ((r = unit_add_dependency(u, UNIT_WANTS, UNIT(m))) < 0)
-                return r;
+        if (automount) {
+                Unit *am;
+
+                if ((r = unit_load_related_unit(UNIT(m), ".automount", &am)) < 0)
+                        return r;
+
+                if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(am), true)) < 0)
+                        return r;
+
+                return unit_add_dependency(UNIT(am), UNIT_BEFORE, tu, true);
+
+        } else {
+
+                if (!noauto && handle)
+                        if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(m), true)) < 0)
+                                return r;
+
+                return unit_add_dependency(UNIT(m), UNIT_BEFORE, tu, true);
+        }
+}
+
+static int mount_verify(Mount *m) {
+        bool b;
+        char *e;
+        assert(m);
 
-        return unit_add_dependency(UNIT(m), UNIT_BEFORE, u);
+        if (UNIT(m)->meta.load_state != UNIT_LOADED)
+                return 0;
+
+        if (!(e = unit_name_from_path(m->where, ".mount")))
+                return -ENOMEM;
+
+        b = unit_has_name(UNIT(m), e);
+        free(e);
+
+        if (!b) {
+                log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m)->meta.id);
+                return -EINVAL;
+        }
+
+        if (m->meta.fragment_path && !m->parameters_fragment.what) {
+                log_error("%s's What setting is missing. Refusing.", UNIT(m)->meta.id);
+                return -EBADMSG;
+        }
+
+        return 0;
 }
 
 static int mount_load(Unit *u) {
@@ -287,28 +299,66 @@ static int mount_load(Unit *u) {
 
         /* This is a new unit? Then let's add in some extras */
         if (u->meta.load_state == UNIT_LOADED) {
+                const char *what = NULL;
 
-                /* Minor validity checking */
-                if ((m->parameters_fragment.options || m->parameters_fragment.fstype) && !m->parameters_fragment.what)
-                        return -EBADMSG;
-
-                if (m->parameters_fragment.what)
+                if (m->meta.fragment_path)
                         m->from_fragment = true;
 
-                if ((r = mount_add_node_links(MOUNT(u))) < 0)
+                if (!m->where)
+                        if (!(m->where = unit_name_to_path(u->meta.id)))
+                                return -ENOMEM;
+
+                path_kill_slashes(m->where);
+
+                if (!m->meta.description)
+                        if ((r = unit_set_description(u, m->where)) < 0)
+                                return r;
+
+                if (m->from_fragment && m->parameters_fragment.what)
+                        what = m->parameters_fragment.what;
+                else if (m->from_etc_fstab && m->parameters_etc_fstab.what)
+                        what = m->parameters_etc_fstab.what;
+                else if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
+                        what = m->parameters_proc_self_mountinfo.what;
+
+                if (what)
+                        if ((r = unit_add_node_link(u, what,
+                                                    (u->meta.manager->running_as == MANAGER_INIT ||
+                                                     u->meta.manager->running_as == MANAGER_SYSTEM))) < 0)
+                                return r;
+
+                if ((r = mount_add_mount_links(m)) < 0)
                         return r;
 
-                if ((r = mount_add_path_links(MOUNT(u))) < 0)
+                if ((r = mount_add_socket_links(m)) < 0)
                         return r;
 
-                if ((r = mount_add_target_links(MOUNT(u))) < 0)
+                if ((r = mount_add_swap_links(m)) < 0)
+                        return r;
+
+                if ((r = mount_add_automount_links(m)) < 0)
+                        return r;
+
+                if ((r = mount_add_target_links(m)) < 0)
                         return r;
 
                 if ((r = unit_add_default_cgroup(u)) < 0)
                         return r;
         }
 
-        return 0;
+        return mount_verify(m);
+}
+
+static int mount_notify_automount(Mount *m, int status) {
+        Unit *p;
+        int r;
+
+        assert(m);
+
+        if ((r = unit_get_related_unit(UNIT(m), ".automount", &p)) < 0)
+                return r == -ENOENT ? 0 : r;
+
+        return automount_send_ready(AUTOMOUNT(p), status);
 }
 
 static void mount_set_state(Mount *m, MountState state) {
@@ -329,61 +379,74 @@ static void mount_set_state(Mount *m, MountState state) {
             state != MOUNT_REMOUNTING_SIGTERM &&
             state != MOUNT_REMOUNTING_SIGKILL) {
                 unit_unwatch_timer(UNIT(m), &m->timer_watch);
-                service_unwatch_control_pid(m);
+                mount_unwatch_control_pid(m);
                 m->control_command = NULL;
+                m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
         }
 
+        if (state == MOUNT_MOUNTED ||
+            state == MOUNT_REMOUNTING)
+                mount_notify_automount(m, 0);
+        else if (state == MOUNT_DEAD ||
+                 state == MOUNT_UNMOUNTING ||
+                 state == MOUNT_MOUNTING_SIGTERM ||
+                 state == MOUNT_MOUNTING_SIGKILL ||
+                 state == MOUNT_REMOUNTING_SIGTERM ||
+                 state == MOUNT_REMOUNTING_SIGKILL ||
+                 state == MOUNT_UNMOUNTING_SIGTERM ||
+                 state == MOUNT_UNMOUNTING_SIGKILL ||
+                 state == MOUNT_MAINTAINANCE)
+                mount_notify_automount(m, -ENODEV);
+
         if (state != old_state)
-                log_debug("%s changed %s → %s", unit_id(UNIT(m)), state_string_table[old_state], state_string_table[state]);
+                log_debug("%s changed %s -> %s",
+                          UNIT(m)->meta.id,
+                          mount_state_to_string(old_state),
+                          mount_state_to_string(state));
 
         unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]);
 }
 
 static int mount_coldplug(Unit *u) {
         Mount *m = MOUNT(u);
+        MountState new_state = MOUNT_DEAD;
+        int r;
 
         assert(m);
         assert(m->state == MOUNT_DEAD);
 
-        if (m->from_proc_self_mountinfo)
-                mount_set_state(m, MOUNT_MOUNTED);
+        if (m->deserialized_state != m->state)
+                new_state = m->deserialized_state;
+        else if (m->from_proc_self_mountinfo)
+                new_state = MOUNT_MOUNTED;
 
-        return 0;
-}
+        if (new_state != m->state) {
 
-static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
-        pid_t pid;
-        int r;
+                if (new_state == MOUNT_MOUNTING ||
+                    new_state == MOUNT_MOUNTING_DONE ||
+                    new_state == MOUNT_REMOUNTING ||
+                    new_state == MOUNT_UNMOUNTING ||
+                    new_state == MOUNT_MOUNTING_SIGTERM ||
+                    new_state == MOUNT_MOUNTING_SIGKILL ||
+                    new_state == MOUNT_UNMOUNTING_SIGTERM ||
+                    new_state == MOUNT_UNMOUNTING_SIGKILL ||
+                    new_state == MOUNT_REMOUNTING_SIGTERM ||
+                    new_state == MOUNT_REMOUNTING_SIGKILL) {
 
-        assert(m);
-        assert(c);
-        assert(_pid);
+                        if (m->control_pid <= 0)
+                                return -EBADMSG;
 
-        if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
-                goto fail;
-
-        if ((r = exec_spawn(c,
-                            &m->exec_context,
-                            NULL, 0,
-                            true,
-                            true,
-                            UNIT(m)->meta.manager->confirm_spawn,
-                            UNIT(m)->meta.cgroup_bondings,
-                            &pid)) < 0)
-                goto fail;
+                        if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0)
+                                return r;
 
-        if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
-                /* FIXME: we need to do something here */
-                goto fail;
+                        if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+                                return r;
+                }
 
-        *_pid = pid;
+                mount_set_state(m, new_state);
+        }
 
         return 0;
-
-fail:
-        unit_unwatch_timer(UNIT(m), &m->timer_watch);
-
-        return r;
 }
 
 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
@@ -410,7 +473,7 @@ static void mount_dump(Unit *u, FILE *f, const char *prefix) {
                 "%sFrom /proc/self/mountinfo: %s\n"
                 "%sFrom fragment: %s\n"
                 "%sKillMode: %s\n",
-                prefix, state_string_table[m->state],
+                prefix, mount_state_to_string(m->state),
                 prefix, m->where,
                 prefix, strna(p->what),
                 prefix, strna(p->fstype),
@@ -428,6 +491,43 @@ static void mount_dump(Unit *u, FILE *f, const char *prefix) {
         exec_context_dump(&m->exec_context, f, prefix);
 }
 
+static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
+        pid_t pid;
+        int r;
+
+        assert(m);
+        assert(c);
+        assert(_pid);
+
+        if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
+                goto fail;
+
+        if ((r = exec_spawn(c,
+                            NULL,
+                            &m->exec_context,
+                            NULL, 0,
+                            m->meta.manager->environment,
+                            true,
+                            true,
+                            UNIT(m)->meta.manager->confirm_spawn,
+                            UNIT(m)->meta.cgroup_bondings,
+                            &pid)) < 0)
+                goto fail;
+
+        if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
+                /* FIXME: we need to do something here */
+                goto fail;
+
+        *_pid = pid;
+
+        return 0;
+
+fail:
+        unit_unwatch_timer(UNIT(m), &m->timer_watch);
+
+        return r;
+}
+
 static void mount_enter_dead(Mount *m, bool success) {
         assert(m);
 
@@ -489,7 +589,7 @@ static void mount_enter_signal(Mount *m, MountState state, bool success) {
         return;
 
 fail:
-        log_warning("%s failed to kill processes: %s", unit_id(UNIT(m)), strerror(-r));
+        log_warning("%s failed to kill processes: %s", UNIT(m)->meta.id, strerror(-r));
 
         if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
                 mount_enter_mounted(m, false);
@@ -498,7 +598,6 @@ fail:
 }
 
 static void mount_enter_unmounting(Mount *m, bool success) {
-        ExecCommand *c;
         int r;
 
         assert(m);
@@ -506,18 +605,19 @@ static void mount_enter_unmounting(Mount *m, bool success) {
         if (!success)
                 m->failure = true;
 
-        m->control_command = c = m->exec_command + MOUNT_EXEC_UNMOUNT;
+        m->control_command_id = MOUNT_EXEC_UNMOUNT;
+        m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
 
         if ((r = exec_command_set(
-                             c,
+                             m->control_command,
                              "/bin/umount",
                              m->where,
                              NULL)) < 0)
                 goto fail;
 
-        service_unwatch_control_pid(m);
+        mount_unwatch_control_pid(m);
 
-        if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
+        if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
                 goto fail;
 
         mount_set_state(m, MOUNT_UNMOUNTING);
@@ -525,33 +625,30 @@ static void mount_enter_unmounting(Mount *m, bool success) {
         return;
 
 fail:
-        log_warning("%s failed to run umount exectuable: %s", unit_id(UNIT(m)), strerror(-r));
+        log_warning("%s failed to run umount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
         mount_enter_mounted(m, false);
 }
 
-static void mount_enter_mounting(Mount *m, bool success) {
-        ExecCommand *c;
+static void mount_enter_mounting(Mount *m) {
         int r;
 
         assert(m);
 
-        if (!success)
-                m->failure = true;
-
-        m->control_command = c = m->exec_command + MOUNT_EXEC_MOUNT;
+        m->control_command_id = MOUNT_EXEC_MOUNT;
+        m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
 
         if (m->from_fragment)
                 r = exec_command_set(
-                                c,
+                                m->control_command,
                                 "/bin/mount",
                                 m->parameters_fragment.what,
                                 m->where,
                                 "-t", m->parameters_fragment.fstype,
-                                "-o", m->parameters_fragment.options,
+                                m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
                                 NULL);
         else if (m->from_etc_fstab)
                 r = exec_command_set(
-                                c,
+                                m->control_command,
                                 "/bin/mount",
                                 m->where,
                                 NULL);
@@ -561,9 +658,9 @@ static void mount_enter_mounting(Mount *m, bool success) {
         if (r < 0)
                 goto fail;
 
-        service_unwatch_control_pid(m);
+        mount_unwatch_control_pid(m);
 
-        if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
+        if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
                 goto fail;
 
         mount_set_state(m, MOUNT_MOUNTING);
@@ -571,21 +668,17 @@ static void mount_enter_mounting(Mount *m, bool success) {
         return;
 
 fail:
-        log_warning("%s failed to run mount exectuable: %s", unit_id(UNIT(m)), strerror(-r));
+        log_warning("%s failed to run mount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
         mount_enter_dead(m, false);
 }
 
-static void mount_enter_mounting_done(Mount *m, bool success) {
+static void mount_enter_mounting_done(Mount *m) {
         assert(m);
 
-        if (!success)
-                m->failure = true;
-
         mount_set_state(m, MOUNT_MOUNTING_DONE);
 }
 
 static void mount_enter_remounting(Mount *m, bool success) {
-        ExecCommand *c;
         int r;
 
         assert(m);
@@ -593,7 +686,8 @@ static void mount_enter_remounting(Mount *m, bool success) {
         if (!success)
                 m->failure = true;
 
-        m->control_command = c = m->exec_command + MOUNT_EXEC_REMOUNT;
+        m->control_command_id = MOUNT_EXEC_REMOUNT;
+        m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
 
         if (m->from_fragment) {
                 char *buf = NULL;
@@ -610,7 +704,7 @@ static void mount_enter_remounting(Mount *m, bool success) {
                         o = "remount";
 
                 r = exec_command_set(
-                                c,
+                                m->control_command,
                                 "/bin/mount",
                                 m->parameters_fragment.what,
                                 m->where,
@@ -621,7 +715,7 @@ static void mount_enter_remounting(Mount *m, bool success) {
                 free(buf);
         } else if (m->from_etc_fstab)
                 r = exec_command_set(
-                                c,
+                                m->control_command,
                                 "/bin/mount",
                                 m->where,
                                 "-o", "remount",
@@ -634,9 +728,9 @@ static void mount_enter_remounting(Mount *m, bool success) {
                 goto fail;
         }
 
-        service_unwatch_control_pid(m);
+        mount_unwatch_control_pid(m);
 
-        if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
+        if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
                 goto fail;
 
         mount_set_state(m, MOUNT_REMOUNTING);
@@ -668,8 +762,7 @@ static int mount_start(Unit *u) {
         assert(m->state == MOUNT_DEAD || m->state == MOUNT_MAINTAINANCE);
 
         m->failure = false;
-
-        mount_enter_mounting(m, true);
+        mount_enter_mounting(m);
         return 0;
 }
 
@@ -714,12 +807,92 @@ static int mount_reload(Unit *u) {
         return 0;
 }
 
+static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
+        Mount *m = MOUNT(u);
+
+        assert(m);
+        assert(f);
+        assert(fds);
+
+        unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
+        unit_serialize_item(u, f, "failure", yes_no(m->failure));
+
+        if (m->control_pid > 0)
+                unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) m->control_pid);
+
+        if (m->control_command_id >= 0)
+                unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
+
+        return 0;
+}
+
+static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
+        Mount *m = MOUNT(u);
+        int r;
+
+        assert(u);
+        assert(key);
+        assert(value);
+        assert(fds);
+
+        if (streq(key, "state")) {
+                MountState state;
+
+                if ((state = mount_state_from_string(value)) < 0)
+                        log_debug("Failed to parse state value %s", value);
+                else
+                        m->deserialized_state = state;
+        } else if (streq(key, "failure")) {
+                int b;
+
+                if ((b = parse_boolean(value)) < 0)
+                        log_debug("Failed to parse failure value %s", value);
+                else
+                        m->failure = b || m->failure;
+
+        } else if (streq(key, "control-pid")) {
+                unsigned pid;
+
+                if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
+                        log_debug("Failed to parse control-pid value %s", value);
+                else
+                        m->control_pid = (pid_t) pid;
+        } else if (streq(key, "control-command")) {
+                MountExecCommand id;
+
+                if ((id = mount_exec_command_from_string(value)) < 0)
+                        log_debug("Failed to parse exec-command value %s", value);
+                else {
+                        m->control_command_id = id;
+                        m->control_command = m->exec_command + id;
+                }
+
+        } else
+                log_debug("Unknown serialization key '%s'", key);
+
+        return 0;
+}
+
 static UnitActiveState mount_active_state(Unit *u) {
         assert(u);
 
         return state_translation_table[MOUNT(u)->state];
 }
 
+static const char *mount_sub_state_to_string(Unit *u) {
+        assert(u);
+
+        return mount_state_to_string(MOUNT(u)->state);
+}
+
+static bool mount_check_gc(Unit *u) {
+        Mount *m = MOUNT(u);
+
+        assert(m);
+
+        return m->from_etc_fstab || m->from_proc_self_mountinfo;
+}
+
 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         Mount *m = MOUNT(u);
         bool success;
@@ -731,12 +904,15 @@ static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         m->failure = m->failure || !success;
 
         assert(m->control_pid == pid);
-        assert(m->control_command);
-
-        exec_status_fill(&m->control_command->exec_status, pid, code, status);
         m->control_pid = 0;
 
-        log_debug("%s control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
+        if (m->control_command) {
+                exec_status_fill(&m->control_command->exec_status, pid, code, status);
+                m->control_command = NULL;
+                m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
+        }
+
+        log_debug("%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
 
         /* Note that mount(8) returning and the kernel sending us a
          * mount table change event might happen out-of-order. If an
@@ -791,39 +967,39 @@ static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
 
         case MOUNT_MOUNTING:
         case MOUNT_MOUNTING_DONE:
-                log_warning("%s mounting timed out. Stopping.", unit_id(u));
+                log_warning("%s mounting timed out. Stopping.", u->meta.id);
                 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false);
                 break;
 
         case MOUNT_REMOUNTING:
-                log_warning("%s remounting timed out. Stopping.", unit_id(u));
+                log_warning("%s remounting timed out. Stopping.", u->meta.id);
                 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false);
                 break;
 
         case MOUNT_UNMOUNTING:
-                log_warning("%s unmounting timed out. Stopping.", unit_id(u));
+                log_warning("%s unmounting timed out. Stopping.", u->meta.id);
                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false);
                 break;
 
         case MOUNT_MOUNTING_SIGTERM:
-                log_warning("%s mounting timed out. Killing.", unit_id(u));
+                log_warning("%s mounting timed out. Killing.", u->meta.id);
                 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false);
                 break;
 
         case MOUNT_REMOUNTING_SIGTERM:
-                log_warning("%s remounting timed out. Killing.", unit_id(u));
+                log_warning("%s remounting timed out. Killing.", u->meta.id);
                 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false);
                 break;
 
         case MOUNT_UNMOUNTING_SIGTERM:
-                log_warning("%s unmounting timed out. Killing.", unit_id(u));
+                log_warning("%s unmounting timed out. Killing.", u->meta.id);
                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false);
                 break;
 
         case MOUNT_MOUNTING_SIGKILL:
         case MOUNT_REMOUNTING_SIGKILL:
         case MOUNT_UNMOUNTING_SIGKILL:
-                log_warning("%s mount process still around after SIGKILL. Ignoring.", unit_id(u));
+                log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id);
 
                 if (m->from_proc_self_mountinfo)
                         mount_enter_mounted(m, false);
@@ -848,7 +1024,7 @@ static int mount_add_one(
         Unit *u;
         bool delete;
         char *e, *w = NULL, *o = NULL, *f = NULL;
-        MountParameters *mp;
+        MountParameters *p;
 
         assert(m);
         assert(what);
@@ -863,16 +1039,14 @@ static int mount_add_one(
         if (mount_point_is_api(where))
                 return 0;
 
-        /* probably some kind of swap, which we don't cover for now */
-        if (where[0] != '/')
+        if (streq(fstype, "autofs"))
                 return 0;
 
-        if (streq(where, "/"))
-                e = strdup("-.mount");
-        else
-                e = unit_name_escape_path(where+1, ".mount");
+        /* probably some kind of swap, ignore */
+        if (!is_path(where))
+                return 0;
 
-        if (!e)
+        if (!(e = unit_name_from_path(where, ".mount")))
                 return -ENOMEM;
 
         if (!(u = manager_get_unit(m, e))) {
@@ -890,12 +1064,9 @@ static int mount_add_one(
                         goto fail;
 
                 if (!(MOUNT(u)->where = strdup(where))) {
-                            r = -ENOMEM;
-                            goto fail;
-                    }
-
-                if ((r = unit_set_description(u, where)) < 0)
+                        r = -ENOMEM;
                         goto fail;
+                }
 
                 unit_add_to_load_queue(u);
         } else {
@@ -911,30 +1082,28 @@ static int mount_add_one(
         }
 
         if (from_proc_self_mountinfo) {
-                mp = &MOUNT(u)->parameters_proc_self_mountinfo;
+                p = &MOUNT(u)->parameters_proc_self_mountinfo;
 
                 if (set_flags) {
                         MOUNT(u)->is_mounted = true;
                         MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
-                        MOUNT(u)->just_changed = !streq_ptr(MOUNT(u)->parameters_proc_self_mountinfo.options, o);
+                        MOUNT(u)->just_changed = !streq_ptr(p->options, o);
                 }
 
                 MOUNT(u)->from_proc_self_mountinfo = true;
-
         } else {
-                mp = &MOUNT(u)->parameters_etc_fstab;
-
+                p = &MOUNT(u)->parameters_etc_fstab;
                 MOUNT(u)->from_etc_fstab = true;
         }
 
-        free(mp->what);
-        mp->what = w;
+        free(p->what);
+        p->what = w;
 
-        free(mp->options);
-        mp->options = o;
+        free(p->options);
+        p->options = o;
 
-        free(mp->fstype);
-        mp->fstype = f;
+        free(p->fstype);
+        p->fstype = f;
 
         unit_add_to_dbus_queue(u);
 
@@ -948,7 +1117,7 @@ fail:
         if (delete && u)
                 unit_free(u);
 
-        return 0;
+        return r;
 }
 
 static char *fstab_node_to_udev_node(char *p) {
@@ -989,6 +1158,27 @@ static char *fstab_node_to_udev_node(char *p) {
         return strdup(p);
 }
 
+static int mount_find_pri(char *options) {
+        char *end, *pri;
+        unsigned long r;
+
+        if (!(pri = mount_test_option(options, "pri=")))
+                return 0;
+
+        pri += 4;
+
+        errno = 0;
+        r = strtoul(pri, &end, 10);
+
+        if (errno != 0)
+                return -errno;
+
+        if (end == pri || (*end != ',' && *end != 0))
+                return -EINVAL;
+
+        return (int) r;
+}
+
 static int mount_load_etc_fstab(Manager *m) {
         FILE *f;
         int r;
@@ -1020,7 +1210,21 @@ static int mount_load_etc_fstab(Manager *m) {
                 if (where[0] == '/')
                         path_kill_slashes(where);
 
-                r = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, false, false);
+                if (streq(me->mnt_type, "swap")) {
+                        int pri;
+
+                        if ((pri = mount_find_pri(me->mnt_opts)) < 0)
+                                r = pri;
+                        else
+                                r = swap_add_one(m,
+                                                 what,
+                                                 pri,
+                                                 !!mount_test_option(me->mnt_opts, MNTOPT_NOAUTO),
+                                                 !!mount_test_option(me->mnt_opts, "comment=systemd.swapon"),
+                                                 false);
+                } else
+                        r = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, false, false);
+
                 free(what);
                 free(where);
 
@@ -1105,8 +1309,10 @@ finish:
 static void mount_shutdown(Manager *m) {
         assert(m);
 
-        if (m->proc_self_mountinfo)
+        if (m->proc_self_mountinfo) {
                 fclose(m->proc_self_mountinfo);
+                m->proc_self_mountinfo = NULL;
+        }
 }
 
 static int mount_enumerate(Manager *m) {
@@ -1114,18 +1320,20 @@ static int mount_enumerate(Manager *m) {
         struct epoll_event ev;
         assert(m);
 
-        if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "r")))
-                return -errno;
+        if (!m->proc_self_mountinfo) {
+                if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
+                        return -errno;
 
-        m->mount_watch.type = WATCH_MOUNT;
-        m->mount_watch.fd = fileno(m->proc_self_mountinfo);
+                m->mount_watch.type = WATCH_MOUNT;
+                m->mount_watch.fd = fileno(m->proc_self_mountinfo);
 
-        zero(ev);
-        ev.events = EPOLLERR;
-        ev.data.ptr = &m->mount_watch;
+                zero(ev);
+                ev.events = EPOLLERR;
+                ev.data.ptr = &m->mount_watch;
 
-        if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
-                return -errno;
+                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
+                        return -errno;
+        }
 
         if ((r = mount_load_etc_fstab(m)) < 0)
                 goto fail;
@@ -1198,7 +1406,7 @@ void mount_fd_event(Manager *m, int events) {
                                 break;
 
                         case MOUNT_MOUNTING:
-                                mount_enter_mounting_done(mount, true);
+                                mount_enter_mounting_done(mount);
                                 break;
 
                         default:
@@ -1236,7 +1444,7 @@ int mount_path_is_mounted(Manager *m, const char* path) {
                 char *e, *slash;
                 Unit *u;
 
-                if (!(e = unit_name_escape_path(t+1, ".mount"))) {
+                if (!(e = unit_name_from_path(t, ".mount"))) {
                         r = -ENOMEM;
                         goto finish;
                 }
@@ -1268,10 +1476,38 @@ finish:
         return r;
 }
 
+static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
+        [MOUNT_DEAD] = "dead",
+        [MOUNT_MOUNTING] = "mounting",
+        [MOUNT_MOUNTING_DONE] = "mounting-done",
+        [MOUNT_MOUNTED] = "mounted",
+        [MOUNT_REMOUNTING] = "remounting",
+        [MOUNT_UNMOUNTING] = "unmounting",
+        [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
+        [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
+        [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
+        [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
+        [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
+        [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
+        [MOUNT_MAINTAINANCE] = "maintainance"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
+
+static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
+        [MOUNT_EXEC_MOUNT] = "ExecMount",
+        [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
+        [MOUNT_EXEC_REMOUNT] = "ExecRemount",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
+
 const UnitVTable mount_vtable = {
         .suffix = ".mount",
 
         .no_alias = true,
+        .no_instances = true,
+        .no_isolate = true,
 
         .init = mount_init,
         .load = mount_load,
@@ -1285,11 +1521,19 @@ const UnitVTable mount_vtable = {
         .stop = mount_stop,
         .reload = mount_reload,
 
+        .serialize = mount_serialize,
+        .deserialize_item = mount_deserialize_item,
+
         .active_state = mount_active_state,
+        .sub_state_to_string = mount_sub_state_to_string,
+
+        .check_gc = mount_check_gc,
 
         .sigchld_event = mount_sigchld_event,
         .timer_event = mount_timer_event,
 
+        .bus_message_handler = bus_mount_message_handler,
+
         .enumerate = mount_enumerate,
         .shutdown = mount_shutdown
 };