valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone
803fb7
From c38c0e05767c5fd526368b63cebcdd5617332940 Mon Sep 17 00:00:00 2001
803fb7
From: Michael Olbrich <m.olbrich@pengutronix.de>
803fb7
Date: Tue, 14 Apr 2015 22:01:48 +0200
803fb7
Subject: [PATCH] automount: add expire support
803fb7
803fb7
(cherry picked from commit deb0a77cf0b409141c4b116ae30becb3d878e1ad)
803fb7
Resolves: #1354410
803fb7
---
de8967
 man/systemd.automount.xml             |   8 +
803fb7
 man/systemd.mount.xml                 |   9 ++
de8967
 src/core/automount.c                  | 224 ++++++++++++++++++++++++--
803fb7
 src/core/automount.h                  |   6 +-
803fb7
 src/core/dbus-automount.c             |   1 +
803fb7
 src/core/load-fragment-gperf.gperf.m4 |   1 +
803fb7
 src/core/mount.c                      |  20 +--
de8967
 src/fstab-generator/fstab-generator.c |  28 ++++
803fb7
 8 files changed, 269 insertions(+), 28 deletions(-)
803fb7
803fb7
diff --git a/man/systemd.automount.xml b/man/systemd.automount.xml
803fb7
index b5b5885cd..9561590c5 100644
803fb7
--- a/man/systemd.automount.xml
803fb7
+++ b/man/systemd.automount.xml
803fb7
@@ -135,6 +135,14 @@
803fb7
         creating these directories. Takes an access mode in octal
803fb7
         notation. Defaults to 0755.</para></listitem>
803fb7
       </varlistentry>
803fb7
+      <varlistentry>
803fb7
+        <term><varname>TimeoutIdleSec=</varname></term>
803fb7
+        <listitem><para>Configures an idleness timeout. Once the mount has been
803fb7
+        idle for the specified time, systemd will attempt to unmount. Takes a
803fb7
+        unit-less value in seconds, or a time span value such as "5min 20s".
803fb7
+        Pass 0 to disable the timeout logic. The timeout is disabled by
803fb7
+        default.</para></listitem>
803fb7
+      </varlistentry>
803fb7
     </variablelist>
803fb7
   </refsect1>
803fb7
 
803fb7
diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml
803fb7
index 8e652e133..04ed1e1cf 100644
803fb7
--- a/man/systemd.mount.xml
803fb7
+++ b/man/systemd.mount.xml
803fb7
@@ -177,6 +177,15 @@
803fb7
         for details.</para></listitem>
803fb7
       </varlistentry>
803fb7
 
803fb7
+      <varlistentry>
803fb7
+        <term><option>x-systemd.idle-timeout=</option></term>
803fb7
+
803fb7
+        <listitem><para>Configures the idleness timeout of the
803fb7
+        automount unit. See <varname>TimeoutIdleSec=</varname> in
803fb7
+        <citerefentry><refentrytitle>systemd.automount</refentrytitle><manvolnum>5</manvolnum></citerefentry>
803fb7
+        for details.</para></listitem>
803fb7
+      </varlistentry>
803fb7
+
803fb7
       <varlistentry>
803fb7
         <term><option>x-systemd.device-timeout=</option></term>
803fb7
 
803fb7
diff --git a/src/core/automount.c b/src/core/automount.c
803fb7
index b391f6198..4e066613d 100644
803fb7
--- a/src/core/automount.c
803fb7
+++ b/src/core/automount.c
803fb7
@@ -42,6 +42,7 @@
803fb7
 #include "dbus-automount.h"
803fb7
 #include "bus-util.h"
803fb7
 #include "bus-error.h"
803fb7
+#include "async.h"
803fb7
 
803fb7
 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
803fb7
         [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
803fb7
@@ -50,6 +51,22 @@ static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
803fb7
         [AUTOMOUNT_FAILED] = UNIT_FAILED
803fb7
 };
803fb7
 
803fb7
+struct expire_data {
803fb7
+        int dev_autofs_fd;
803fb7
+        int ioctl_fd;
803fb7
+};
803fb7
+
803fb7
+static inline void expire_data_free(struct expire_data *data) {
803fb7
+        if (!data)
803fb7
+                return;
803fb7
+
803fb7
+        safe_close(data->dev_autofs_fd);
803fb7
+        safe_close(data->ioctl_fd);
803fb7
+        free(data);
803fb7
+}
803fb7
+
803fb7
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data*, expire_data_free);
803fb7
+
803fb7
 static int open_dev_autofs(Manager *m);
803fb7
 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata);
803fb7
 
803fb7
@@ -81,13 +98,16 @@ static void repeat_unmount(const char *path) {
803fb7
         }
803fb7
 }
803fb7
 
803fb7
+static int automount_send_ready(Automount *a, Set *tokens, int status);
803fb7
+
803fb7
 static void unmount_autofs(Automount *a) {
803fb7
         assert(a);
803fb7
 
803fb7
         if (a->pipe_fd < 0)
803fb7
                 return;
803fb7
 
803fb7
-        automount_send_ready(a, -EHOSTDOWN);
803fb7
+        automount_send_ready(a, a->tokens, -EHOSTDOWN);
803fb7
+        automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
803fb7
 
803fb7
         a->pipe_event_source = sd_event_source_unref(a->pipe_event_source);
803fb7
         a->pipe_fd = safe_close(a->pipe_fd);
803fb7
@@ -112,6 +132,10 @@ static void automount_done(Unit *u) {
803fb7
 
803fb7
         set_free(a->tokens);
803fb7
         a->tokens = NULL;
803fb7
+        set_free(a->expire_tokens);
803fb7
+        a->expire_tokens = NULL;
803fb7
+
803fb7
+        a->expire_event_source = sd_event_source_unref(a->expire_event_source);
803fb7
 }
803fb7
 
803fb7
 static int automount_add_mount_links(Automount *a) {
803fb7
@@ -265,6 +289,7 @@ static int automount_coldplug(Unit *u, Hashmap *deferred_work) {
803fb7
 }
803fb7
 
803fb7
 static void automount_dump(Unit *u, FILE *f, const char *prefix) {
803fb7
+        char time_string[FORMAT_TIMESPAN_MAX];
803fb7
         Automount *a = AUTOMOUNT(u);
803fb7
 
803fb7
         assert(a);
803fb7
@@ -273,11 +298,13 @@ static void automount_dump(Unit *u, FILE *f, const char *prefix) {
803fb7
                 "%sAutomount State: %s\n"
803fb7
                 "%sResult: %s\n"
803fb7
                 "%sWhere: %s\n"
803fb7
-                "%sDirectoryMode: %04o\n",
803fb7
+                "%sDirectoryMode: %04o\n"
803fb7
+                "%sTimeoutIdleUSec: %s\n",
803fb7
                 prefix, automount_state_to_string(a->state),
803fb7
                 prefix, automount_result_to_string(a->result),
803fb7
                 prefix, a->where,
803fb7
-                prefix, a->directory_mode);
803fb7
+                prefix, a->directory_mode,
803fb7
+                prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, a->timeout_idle_usec, USEC_PER_SEC));
803fb7
 }
803fb7
 
803fb7
 static void automount_enter_dead(Automount *a, AutomountResult f) {
803fb7
@@ -367,7 +394,7 @@ static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
-static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
803fb7
+static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, usec_t usec) {
803fb7
         struct autofs_dev_ioctl param;
803fb7
 
803fb7
         assert(dev_autofs_fd >= 0);
803fb7
@@ -375,7 +402,9 @@ static int autofs_set_timeout(int dev_autofs_fd, int ioctl_fd, time_t sec) {
803fb7
 
803fb7
         init_autofs_dev_ioctl(¶m;;
803fb7
         param.ioctlfd = ioctl_fd;
803fb7
-        param.timeout.timeout = sec;
803fb7
+
803fb7
+        /* Convert to seconds, rounding up. */
803fb7
+        param.timeout.timeout = (usec + USEC_PER_SEC - 1) / USEC_PER_SEC;
803fb7
 
803fb7
         if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_TIMEOUT, &param) < 0)
803fb7
                 return -errno;
803fb7
@@ -404,7 +433,7 @@ static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, in
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
-int automount_send_ready(Automount *a, int status) {
803fb7
+static int automount_send_ready(Automount *a, Set *tokens, int status) {
803fb7
         _cleanup_close_ int ioctl_fd = -1;
803fb7
         unsigned token;
803fb7
         int r;
803fb7
@@ -412,7 +441,7 @@ int automount_send_ready(Automount *a, int status) {
803fb7
         assert(a);
803fb7
         assert(status <= 0);
803fb7
 
803fb7
-        if (set_isempty(a->tokens))
803fb7
+        if (set_isempty(tokens))
803fb7
                 return 0;
803fb7
 
803fb7
         ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
803fb7
@@ -427,7 +456,7 @@ int automount_send_ready(Automount *a, int status) {
803fb7
         r = 0;
803fb7
 
803fb7
         /* Autofs thankfully does not hand out 0 as a token */
803fb7
-        while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
803fb7
+        while ((token = PTR_TO_UINT(set_steal_first(tokens)))) {
803fb7
                 int k;
803fb7
 
803fb7
                 /* Autofs fun fact II:
803fb7
@@ -446,6 +475,55 @@ int automount_send_ready(Automount *a, int status) {
803fb7
         return r;
803fb7
 }
803fb7
 
803fb7
+int automount_update_mount(Automount *a, MountState old_state, MountState state) {
803fb7
+        _cleanup_close_ int ioctl_fd = -1;
803fb7
+
803fb7
+        assert(a);
803fb7
+
803fb7
+        switch (state) {
803fb7
+        case MOUNT_MOUNTED:
803fb7
+        case MOUNT_REMOUNTING:
803fb7
+                automount_send_ready(a, a->tokens, 0);
803fb7
+                break;
803fb7
+         case MOUNT_DEAD:
803fb7
+         case MOUNT_UNMOUNTING:
803fb7
+         case MOUNT_MOUNTING_SIGTERM:
803fb7
+         case MOUNT_MOUNTING_SIGKILL:
803fb7
+         case MOUNT_REMOUNTING_SIGTERM:
803fb7
+         case MOUNT_REMOUNTING_SIGKILL:
803fb7
+         case MOUNT_UNMOUNTING_SIGTERM:
803fb7
+         case MOUNT_UNMOUNTING_SIGKILL:
803fb7
+         case MOUNT_FAILED:
803fb7
+                if (old_state != state)
803fb7
+                        automount_send_ready(a, a->tokens, -ENODEV);
803fb7
+                break;
803fb7
+        default:
803fb7
+                break;
803fb7
+        }
803fb7
+
803fb7
+        switch (state) {
803fb7
+        case MOUNT_DEAD:
803fb7
+                automount_send_ready(a, a->expire_tokens, 0);
803fb7
+                break;
803fb7
+         case MOUNT_MOUNTING:
803fb7
+         case MOUNT_MOUNTING_DONE:
803fb7
+         case MOUNT_MOUNTING_SIGTERM:
803fb7
+         case MOUNT_MOUNTING_SIGKILL:
803fb7
+         case MOUNT_REMOUNTING_SIGTERM:
803fb7
+         case MOUNT_REMOUNTING_SIGKILL:
803fb7
+         case MOUNT_UNMOUNTING_SIGTERM:
803fb7
+         case MOUNT_UNMOUNTING_SIGKILL:
803fb7
+         case MOUNT_FAILED:
803fb7
+                if (old_state != state)
803fb7
+                        automount_send_ready(a, a->expire_tokens, -ENODEV);
803fb7
+                break;
803fb7
+        default:
803fb7
+                break;
803fb7
+        }
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 static void automount_enter_waiting(Automount *a) {
803fb7
         _cleanup_close_ int ioctl_fd = -1;
803fb7
         int p[2] = { -1, -1 };
803fb7
@@ -505,7 +583,7 @@ static void automount_enter_waiting(Automount *a) {
803fb7
         if (r < 0)
803fb7
                 goto fail;
803fb7
 
803fb7
-        r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300);
803fb7
+        r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, a->timeout_idle_usec);
803fb7
         if (r < 0)
803fb7
                 goto fail;
803fb7
 
803fb7
@@ -537,6 +615,83 @@ fail:
803fb7
         automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
803fb7
 }
803fb7
 
803fb7
+static void *expire_thread(void *p) {
803fb7
+        struct autofs_dev_ioctl param;
803fb7
+        _cleanup_(expire_data_freep) struct expire_data *data = (struct expire_data*)p;
803fb7
+        int r;
803fb7
+
803fb7
+        assert(data->dev_autofs_fd >= 0);
803fb7
+        assert(data->ioctl_fd >= 0);
803fb7
+
803fb7
+        init_autofs_dev_ioctl(¶m;;
803fb7
+        param.ioctlfd = data->ioctl_fd;
803fb7
+
803fb7
+        do {
803fb7
+                r = ioctl(data->dev_autofs_fd, AUTOFS_DEV_IOCTL_EXPIRE, ¶m;;
803fb7
+        } while (r >= 0);
803fb7
+
803fb7
+        if (errno != EAGAIN)
803fb7
+                log_warning_errno(errno, "Failed to expire automount, ignoring: %m");
803fb7
+
803fb7
+        return NULL;
803fb7
+}
803fb7
+
803fb7
+static int automount_start_expire(Automount *a);
803fb7
+
803fb7
+static int automount_dispatch_expire(sd_event_source *source, usec_t usec, void *userdata) {
803fb7
+        Automount *a = AUTOMOUNT(userdata);
803fb7
+        _cleanup_(expire_data_freep) struct expire_data *data = NULL;
803fb7
+        int r;
803fb7
+
803fb7
+        assert(a);
803fb7
+        assert(source == a->expire_event_source);
803fb7
+
803fb7
+        data = new0(struct expire_data, 1);
803fb7
+        if (!data)
803fb7
+                return log_oom();
803fb7
+
803fb7
+        data->ioctl_fd = -1;
803fb7
+
803fb7
+        data->dev_autofs_fd = fcntl(UNIT(a)->manager->dev_autofs_fd, F_DUPFD_CLOEXEC, 3);
803fb7
+        if (data->dev_autofs_fd < 0)
803fb7
+                return log_unit_error_errno(UNIT(a)->id, errno, "Failed to duplicate autofs fd: %m");
803fb7
+
803fb7
+        data->ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
803fb7
+        if (data->ioctl_fd < 0)
803fb7
+                return log_unit_error_errno(UNIT(a)->id, data->ioctl_fd, "Couldn't open autofs ioctl fd: %m");
803fb7
+
803fb7
+        r = asynchronous_job(expire_thread, data);
803fb7
+        if (r < 0)
803fb7
+                return log_unit_error_errno(UNIT(a)->id, r, "Failed to start expire job: %m");
803fb7
+
803fb7
+        data = NULL;
803fb7
+
803fb7
+        return automount_start_expire(a);
803fb7
+}
803fb7
+
803fb7
+static int automount_start_expire(Automount *a) {
803fb7
+        int r;
803fb7
+        usec_t timeout;
803fb7
+
803fb7
+        assert(a);
803fb7
+
803fb7
+        timeout = now(CLOCK_MONOTONIC) + MAX(a->timeout_idle_usec/10, USEC_PER_SEC);
803fb7
+
803fb7
+        if (a->expire_event_source) {
803fb7
+                r = sd_event_source_set_time(a->expire_event_source, timeout);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+
803fb7
+                return sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_ONESHOT);
803fb7
+        }
803fb7
+
803fb7
+        return sd_event_add_time(
803fb7
+                        UNIT(a)->manager->event,
803fb7
+                        &a->expire_event_source,
803fb7
+                        CLOCK_MONOTONIC, timeout, 0,
803fb7
+                        automount_dispatch_expire, a);
803fb7
+}
803fb7
+
803fb7
 static void automount_enter_runnning(Automount *a) {
803fb7
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
         struct stat st;
803fb7
@@ -549,7 +704,8 @@ static void automount_enter_runnning(Automount *a) {
803fb7
         if (unit_stop_pending(UNIT(a))) {
803fb7
                 log_unit_debug(UNIT(a)->id,
803fb7
                                "Suppressing automount request on %s since unit stop is scheduled.", UNIT(a)->id);
803fb7
-                automount_send_ready(a, -EHOSTDOWN);
803fb7
+                automount_send_ready(a, a->tokens, -EHOSTDOWN);
803fb7
+                automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
803fb7
                 return;
803fb7
         }
803fb7
 
803fb7
@@ -576,6 +732,10 @@ static void automount_enter_runnning(Automount *a) {
803fb7
                 }
803fb7
         }
803fb7
 
803fb7
+        r = automount_start_expire(a);
803fb7
+        if (r < 0)
803fb7
+                log_unit_warning_errno(UNIT(a)->id, r, "Failed to start expiration timer, ignoring: %m");
803fb7
+
803fb7
         automount_set_state(a, AUTOMOUNT_RUNNING);
803fb7
         return;
803fb7
 
803fb7
@@ -629,6 +789,8 @@ static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
803fb7
 
803fb7
         SET_FOREACH(p, a->tokens, i)
803fb7
                 unit_serialize_item_format(u, f, "token", "%u", PTR_TO_UINT(p));
803fb7
+        SET_FOREACH(p, a->expire_tokens, i)
803fb7
+                unit_serialize_item_format(u, f, "expire-token", "%u", PTR_TO_UINT(p));
803fb7
 
803fb7
         if (a->pipe_fd >= 0) {
803fb7
                 int copy;
803fb7
@@ -688,6 +850,22 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu
803fb7
                         if (r < 0)
803fb7
                                 return r;
803fb7
                 }
803fb7
+        } else if (streq(key, "expire-token")) {
803fb7
+                unsigned token;
803fb7
+
803fb7
+                if (safe_atou(value, &token) < 0)
803fb7
+                        log_unit_debug(u->id, "Failed to parse token value %s", value);
803fb7
+                else {
803fb7
+                        r = set_ensure_allocated(&a->expire_tokens, NULL);
803fb7
+                        if (r < 0) {
803fb7
+                                log_oom();
803fb7
+                                return 0;
803fb7
+                        }
803fb7
+
803fb7
+                        r = set_put(a->expire_tokens, UINT_TO_PTR(token));
803fb7
+                        if (r < 0)
803fb7
+                                log_unit_error_errno(u->id, r, "Failed to add expire token to set: %m");
803fb7
+                }
803fb7
         } else if (streq(key, "pipe-fd")) {
803fb7
                 int fd;
803fb7
 
803fb7
@@ -725,6 +903,7 @@ static bool automount_check_gc(Unit *u) {
803fb7
 }
803fb7
 
803fb7
 static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
803fb7
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
         union autofs_v5_packet_union packet;
803fb7
         Automount *a = AUTOMOUNT(userdata);
803fb7
         ssize_t l;
803fb7
@@ -777,6 +956,31 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo
803fb7
                 automount_enter_runnning(a);
803fb7
                 break;
803fb7
 
803fb7
+        case autofs_ptype_expire_direct:
803fb7
+                log_unit_debug(UNIT(a)->id, "Got direct umount request on %s", a->where);
803fb7
+
803fb7
+                (void) sd_event_source_set_enabled(a->expire_event_source, SD_EVENT_OFF);
803fb7
+
803fb7
+                r = set_ensure_allocated(&a->expire_tokens, NULL);
803fb7
+                if (r < 0) {
803fb7
+                        log_unit_error(UNIT(a)->id, "Failed to allocate token set.");
803fb7
+                        goto fail;
803fb7
+                }
803fb7
+
803fb7
+                r = set_put(a->expire_tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
803fb7
+                if (r < 0) {
803fb7
+                        log_unit_error_errno(UNIT(a)->id, r, "Failed to remember token: %m");
803fb7
+                        goto fail;
803fb7
+                }
803fb7
+                r = manager_add_job(UNIT(a)->manager, JOB_STOP, UNIT_TRIGGER(UNIT(a)), JOB_REPLACE, true, &error, NULL);
803fb7
+                if (r < 0) {
803fb7
+                        log_unit_warning(UNIT(a)->id,
803fb7
+                                         "%s failed to queue umount startup job: %s",
803fb7
+                                         UNIT(a)->id, bus_error_message(&error, r));
803fb7
+                        goto fail;
803fb7
+                }
803fb7
+                break;
803fb7
+
803fb7
         default:
803fb7
                 log_unit_error(UNIT(a)->id, "Received unknown automount request %i", packet.hdr.type);
803fb7
                 break;
803fb7
diff --git a/src/core/automount.h b/src/core/automount.h
803fb7
index 60f552238..2a50fef68 100644
803fb7
--- a/src/core/automount.h
803fb7
+++ b/src/core/automount.h
803fb7
@@ -47,6 +47,7 @@ struct Automount {
803fb7
         AutomountState state, deserialized_state;
803fb7
 
803fb7
         char *where;
803fb7
+        usec_t timeout_idle_usec;
803fb7
 
803fb7
         int pipe_fd;
803fb7
         sd_event_source *pipe_event_source;
803fb7
@@ -54,13 +55,16 @@ struct Automount {
803fb7
         dev_t dev_id;
803fb7
 
803fb7
         Set *tokens;
803fb7
+        Set *expire_tokens;
803fb7
+
803fb7
+        sd_event_source *expire_event_source;
803fb7
 
803fb7
         AutomountResult result;
803fb7
 };
803fb7
 
803fb7
 extern const UnitVTable automount_vtable;
803fb7
 
803fb7
-int automount_send_ready(Automount *a, int status);
803fb7
+int automount_update_mount(Automount *a, MountState old_state, MountState state);
803fb7
 
803fb7
 const char* automount_state_to_string(AutomountState i) _const_;
803fb7
 AutomountState automount_state_from_string(const char *s) _pure_;
803fb7
diff --git a/src/core/dbus-automount.c b/src/core/dbus-automount.c
803fb7
index b2a510ad0..c62ad8242 100644
803fb7
--- a/src/core/dbus-automount.c
803fb7
+++ b/src/core/dbus-automount.c
803fb7
@@ -32,5 +32,6 @@ const sd_bus_vtable bus_automount_vtable[] = {
803fb7
         SD_BUS_PROPERTY("Where", "s", NULL, offsetof(Automount, where), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_PROPERTY("DirectoryMode", "u", bus_property_get_mode, offsetof(Automount, directory_mode), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Automount, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
803fb7
+        SD_BUS_PROPERTY("TimeoutIdleUSec", "t", bus_property_get_usec, offsetof(Automount, timeout_idle_usec), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_VTABLE_END
803fb7
 };
803fb7
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
803fb7
index f3a6e13d9..c866a9cd0 100644
803fb7
--- a/src/core/load-fragment-gperf.gperf.m4
803fb7
+++ b/src/core/load-fragment-gperf.gperf.m4
803fb7
@@ -318,6 +318,7 @@ KILL_CONTEXT_CONFIG_ITEMS(Mount)m4_dnl
803fb7
 m4_dnl
803fb7
 Automount.Where,                 config_parse_path,                  0,                             offsetof(Automount, where)
803fb7
 Automount.DirectoryMode,         config_parse_mode,                  0,                             offsetof(Automount, directory_mode)
803fb7
+Automount.TimeoutIdleSec,        config_parse_sec,                   0,                             offsetof(Automount, timeout_idle_usec)
803fb7
 m4_dnl
803fb7
 Swap.What,                       config_parse_path,                  0,                             offsetof(Swap, parameters_fragment.what)
803fb7
 Swap.Priority,                   config_parse_int,                   0,                             offsetof(Swap, parameters_fragment.priority)
803fb7
diff --git a/src/core/mount.c b/src/core/mount.c
803fb7
index 3fbdb7daf..7ca7f5a25 100644
803fb7
--- a/src/core/mount.c
803fb7
+++ b/src/core/mount.c
803fb7
@@ -552,7 +552,7 @@ static int mount_load(Unit *u) {
803fb7
         return mount_verify(m);
803fb7
 }
803fb7
 
803fb7
-static int mount_notify_automount(Mount *m, int status) {
803fb7
+static int mount_notify_automount(Mount *m, MountState old_state, MountState state) {
803fb7
         Unit *p;
803fb7
         int r;
803fb7
         Iterator i;
803fb7
@@ -561,7 +561,7 @@ static int mount_notify_automount(Mount *m, int status) {
803fb7
 
803fb7
         SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
803fb7
                 if (p->type == UNIT_AUTOMOUNT) {
803fb7
-                         r = automount_send_ready(AUTOMOUNT(p), status);
803fb7
+                         r = automount_update_mount(AUTOMOUNT(p), old_state, state);
803fb7
                          if (r < 0)
803fb7
                                  return r;
803fb7
                 }
803fb7
@@ -592,21 +592,7 @@ static void mount_set_state(Mount *m, MountState state) {
803fb7
                 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
803fb7
         }
803fb7
 
803fb7
-        if (state == MOUNT_MOUNTED ||
803fb7
-            state == MOUNT_REMOUNTING)
803fb7
-                mount_notify_automount(m, 0);
803fb7
-        else if (state == MOUNT_DEAD ||
803fb7
-                 state == MOUNT_UNMOUNTING ||
803fb7
-                 state == MOUNT_MOUNTING_SIGTERM ||
803fb7
-                 state == MOUNT_MOUNTING_SIGKILL ||
803fb7
-                 state == MOUNT_REMOUNTING_SIGTERM ||
803fb7
-                 state == MOUNT_REMOUNTING_SIGKILL ||
803fb7
-                 state == MOUNT_UNMOUNTING_SIGTERM ||
803fb7
-                 state == MOUNT_UNMOUNTING_SIGKILL ||
803fb7
-                 state == MOUNT_FAILED) {
803fb7
-                if (state != old_state)
803fb7
-                        mount_notify_automount(m, -ENODEV);
803fb7
-        }
803fb7
+        mount_notify_automount(m, old_state, state);
803fb7
 
803fb7
         if (state != old_state)
803fb7
                 log_unit_debug(UNIT(m)->id,
803fb7
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
803fb7
index 029eb1638..a943393b0 100644
803fb7
--- a/src/fstab-generator/fstab-generator.c
803fb7
+++ b/src/fstab-generator/fstab-generator.c
803fb7
@@ -213,6 +213,30 @@ static int write_requires_mounts_for(FILE *f, const char *opts) {
803fb7
 
803fb7
         return 0;
803fb7
 }
803fb7
+
803fb7
+static int write_idle_timeout(FILE *f, const char *where, const char *opts, char **filtered) {
803fb7
+        _cleanup_free_ char *timeout = NULL;
803fb7
+        char timespan[FORMAT_TIMESPAN_MAX];
803fb7
+        usec_t u;
803fb7
+        int r;
803fb7
+
803fb7
+        r = fstab_filter_options(opts, "x-systemd.idle-timeout\0", NULL, &timeout, filtered);
803fb7
+        if (r < 0)
803fb7
+                return log_warning_errno(r, "Failed to parse options: %m");
803fb7
+        if (r == 0)
803fb7
+                return 0;
803fb7
+
803fb7
+        r = parse_sec(timeout, &u);
803fb7
+        if (r < 0) {
803fb7
+                log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
803fb7
+                return 0;
803fb7
+        }
803fb7
+
803fb7
+        fprintf(f, "TimeoutIdleSec=%s\n", format_timespan(timespan, sizeof(timespan), u, 0));
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 static int add_mount(
803fb7
                 const char *what,
803fb7
                 const char *where,
803fb7
@@ -374,6 +398,10 @@ static int add_mount(
803fb7
                         "Where=%s\n",
803fb7
                         where);
803fb7
 
803fb7
+                r = write_idle_timeout(f, where, opts, &filtered);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+
803fb7
                 fflush(f);
803fb7
                 if (ferror(f))
803fb7
                         return log_error_errno(errno, "Failed to write unit file %s: %m", automount_unit);