HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/mount.c

93,351 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: c05df5d10cff84a1ed7414c2a7c520c6e0845fdb

Lines 1–1000 of 2,581

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
 
#include "sd-messages.h"
 
#include "alloc-util.h"
#include "chase.h"
#include "dbus-mount.h"
#include "dbus-unit.h"
#include "device.h"
#include "errno-util.h"
#include "exec-credential.h"
#include "exit-status.h"
#include "fd-util.h"
#include "format-util.h"
#include "fstab-util.h"
#include "glyph-util.h"
#include "initrd-util.h"
#include "libmount-util.h"
#include "log.h"
#include "manager.h"
#include "mkdir.h"
#include "mount-util.h"
#include "mount.h"
#include "mount-setup.h"
#include "mountpoint-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "serialize.h"
#include "set.h"
#include "special.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "unit.h"
#include "unit-name.h"
#include "utf8.h"
 
#define RETRY_UMOUNT_MAX 32
 
static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
        [MOUNT_DEAD]               = UNIT_INACTIVE,
        [MOUNT_MOUNTING]           = UNIT_ACTIVATING,
        [MOUNT_MOUNTING_DONE]      = UNIT_ACTIVATING,
        [MOUNT_MOUNTED]            = UNIT_ACTIVE,
        [MOUNT_REMOUNTING]         = UNIT_RELOADING,
        [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
        [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
        [MOUNT_UNMOUNTING]         = UNIT_DEACTIVATING,
        [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
        [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
        [MOUNT_FAILED]             = UNIT_FAILED,
        [MOUNT_CLEANING]           = UNIT_MAINTENANCE,
};
 
static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
#if HAVE_LIBMOUNT
static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
#endif
static void mount_enter_dead(Mount *m, MountResult f, bool flush_result);
static void mount_enter_mounted(Mount *m, MountResult f);
static void mount_cycle_clear(Mount *m);
static int mount_process_proc_self_mountinfo(Manager *m);
 
static bool MOUNT_STATE_WITH_PROCESS(MountState state) {
        return IN_SET(state,
                      MOUNT_MOUNTING,
                      MOUNT_MOUNTING_DONE,
                      MOUNT_REMOUNTING,
                      MOUNT_REMOUNTING_SIGTERM,
                      MOUNT_REMOUNTING_SIGKILL,
                      MOUNT_UNMOUNTING,
                      MOUNT_UNMOUNTING_SIGTERM,
                      MOUNT_UNMOUNTING_SIGKILL,
                      MOUNT_CLEANING);
}
 
static MountParameters* get_mount_parameters_fragment(Mount *m) {
        assert(m);
 
        if (m->from_fragment)
                return &m->parameters_fragment;
 
        return NULL;
}
 
static MountParameters* get_mount_parameters(Mount *m) {
        assert(m);
 
        if (m->from_proc_self_mountinfo)
                return &m->parameters_proc_self_mountinfo;
 
        return get_mount_parameters_fragment(m);
}
 
static bool mount_is_network(const MountParameters *p) {
        assert(p);
 
        if (fstab_test_option(p->options, "_netdev\0"))
                return true;
 
        if (p->fstype && fstype_is_network(p->fstype))
                return true;
 
        return false;
}
 
static bool mount_is_nofail(const Mount *m) {
        assert(m);
 
        if (!m->from_fragment)
                return false;
 
        return fstab_test_yes_no_option(m->parameters_fragment.options, "nofail\0" "fail\0");
}
 
static bool mount_is_loop(const MountParameters *p) {
        assert(p);
 
        if (fstab_test_option(p->options, "loop\0"))
                return true;
 
        return false;
}
 
static bool mount_is_bind(const MountParameters *p) {
        assert(p);
        return fstab_is_bind(p->options, p->fstype);
}
 
static int mount_is_bound_to_device(Mount *m) {
        _cleanup_free_ char *value = NULL;
        const MountParameters *p;
        int r;
 
        assert(m);
 
        /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do
         * this by checking for the x-systemd.device-bound= mount option. If it is enabled we use BindsTo=,
         * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see
         * below. */
 
        p = get_mount_parameters(m);
        if (!p)
                return false;
 
        r = fstab_filter_options(p->options, "x-systemd.device-bound\0", NULL, &value, NULL, NULL);
        if (r < 0)
                return r;
        if (r == 0)
                return -EIDRM; /* If unspecified at all, return recognizable error */
 
        if (isempty(value))
                return true;
 
        return parse_boolean(value);
}
 
static bool mount_propagate_stop(Mount *m) {
        int r;
 
        assert(m);
 
        r = mount_is_bound_to_device(m);
        if (r >= 0)
                /* If x-systemd.device-bound=no is explicitly requested by user, don't try to set StopPropagatedFrom=.
                 * Also don't bother if true, since with BindsTo= the stop propagation is implicit. */
                return false;
        if (r != -EIDRM)
                log_debug_errno(r, "Failed to get x-systemd.device-bound= option, ignoring: %m");
 
        return m->from_fragment; /* let's propagate stop whenever this is an explicitly configured unit,
                                  * otherwise let's not bother. */
}
 
static void mount_init(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
 
        assert(u->load_state == UNIT_STUB);
 
        m->timeout_usec = u->manager->defaults.timeout_start_usec;
 
        m->exec_context.std_output = u->manager->defaults.std_output;
        m->exec_context.std_error = u->manager->defaults.std_error;
 
        m->directory_mode = 0755;
 
        /* We need to make sure that /usr/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.same_pgrp = true;
 
        m->control_pid = PIDREF_NULL;
        m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
 
        u->ignore_on_isolate = true;
}
 
static int mount_arm_timer(Mount *m, bool relative, usec_t usec) {
        assert(m);
 
        return unit_arm_timer(UNIT(m), &m->timer_event_source, relative, usec, mount_dispatch_timer);
}
 
static void mount_unwatch_control_pid(Mount *m) {
        assert(m);
        unit_unwatch_pidref_done(UNIT(m), &m->control_pid);
}
 
static void mount_parameters_done(MountParameters *p) {
        assert(p);
 
        p->what = mfree(p->what);
        p->options = mfree(p->options);
        p->fstype = mfree(p->fstype);
}
 
static void mount_done(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
 
        m->where = mfree(m->where);
 
        mount_parameters_done(&m->parameters_proc_self_mountinfo);
        mount_parameters_done(&m->parameters_fragment);
 
        m->exec_runtime = exec_runtime_free(m->exec_runtime);
 
        exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
        m->control_command = NULL;
 
        mount_unwatch_control_pid(m);
 
        m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
}
 
static int mount_add_mount_dependencies(Mount *m) {
        MountParameters *pm;
        int r;
 
        assert(m);
 
        if (!path_equal(m->where, "/")) {
                _cleanup_free_ char *parent = NULL;
 
                /* Adds in links to other mount points that might lie further up in the hierarchy */
 
                r = path_extract_directory(m->where, &parent);
                if (r < 0)
                        return r;
 
                r = unit_add_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT, UNIT_MOUNT_REQUIRES);
                if (r < 0)
                        return r;
        }
 
        /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
         * or a loop mount) to be available. */
        pm = get_mount_parameters_fragment(m);
        if (pm && pm->what &&
            path_is_absolute(pm->what) &&
            (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) {
 
                r = unit_add_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_REQUIRES);
                if (r < 0)
                        return r;
        }
 
        /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
        for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; ++t) {
                Unit *other;
                Set *s = manager_get_units_needing_mounts_for(UNIT(m)->manager, m->where, t);
 
                SET_FOREACH(other, s) {
                        if (other->load_state != UNIT_LOADED)
                                continue;
 
                        if (other == UNIT(m))
                                continue;
 
                        r = unit_add_dependency(
                                        other,
                                        UNIT_AFTER,
                                        UNIT(m),
                                        /* add_reference= */ true,
                                        UNIT_DEPENDENCY_PATH);
                        if (r < 0)
                                return r;
 
                        if (UNIT(m)->fragment_path) {
                                /* If we have fragment configuration, then make this dependency required/wanted */
                                r = unit_add_dependency(
                                                other,
                                                unit_mount_dependency_type_to_dependency_type(t),
                                                UNIT(m),
                                                /* add_reference= */ true,
                                                UNIT_DEPENDENCY_PATH);
                                if (r < 0)
                                        return r;
                        }
                }
        }
 
        return 0;
}
 
static int mount_add_device_dependencies(Mount *m) {
        UnitDependencyMask mask;
        MountParameters *p;
        UnitDependency dep;
        int r;
 
        assert(m);
 
        log_unit_trace(UNIT(m), "Processing implicit device dependencies");
 
        p = get_mount_parameters(m);
        if (!p) {
                log_unit_trace(UNIT(m), "Missing mount parameters, skipping implicit device dependencies");
                return 0;
        }
 
        if (!p->what) {
                log_unit_trace(UNIT(m), "Missing mount source, skipping implicit device dependencies");
                return 0;
        }
 
        if (mount_is_bind(p)) {
                log_unit_trace(UNIT(m), "Mount unit is a bind mount, skipping implicit device dependencies");
                return 0;
        }
 
        if (!is_device_path(p->what)) {
                log_unit_trace(UNIT(m), "Mount source is not a device path, skipping implicit device dependencies");
                return 0;
        }
 
        /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for
         * the root file system specified on the kernel command line. Ignore it here. */
        if (PATH_IN_SET(p->what, "/dev/root", "/dev/nfs")) {
                log_unit_trace(UNIT(m), "Mount source is in /dev/root or /dev/nfs, skipping implicit device dependencies");
                return 0;
        }
 
        if (path_equal(m->where, "/")) {
                log_unit_trace(UNIT(m), "Mount destination is '/', skipping implicit device dependencies");
                return 0;
        }
 
        /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to
         * races when mounts are established by other tools with different backing devices than what we
         * maintain. The user can still force this to be a BindsTo= dependency with an appropriate option (or
         * udev property) so the mount units are automatically stopped when the device disappears
         * suddenly. */
        dep = mount_is_bound_to_device(m) > 0 ? UNIT_BINDS_TO : UNIT_REQUIRES;
 
        /* We always use 'what' from /proc/self/mountinfo if mounted */
        mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO : UNIT_DEPENDENCY_MOUNT_FILE;
 
        r = unit_add_node_dependency(UNIT(m), p->what, dep, mask);
        if (r < 0)
                return r;
        if (r > 0)
                log_unit_trace(UNIT(m), "Added %s dependency on %s", unit_dependency_to_string(dep), p->what);
 
        if (mount_propagate_stop(m)) {
                r = unit_add_node_dependency(UNIT(m), p->what, UNIT_STOP_PROPAGATED_FROM, mask);
                if (r < 0)
                        return r;
                if (r > 0)
                        log_unit_trace(UNIT(m), "Added %s dependency on %s",
                                       unit_dependency_to_string(UNIT_STOP_PROPAGATED_FROM), p->what);
        }
 
        r = unit_add_blockdev_dependency(UNIT(m), p->what, mask);
        if (r > 0)
                log_unit_trace(UNIT(m), "Added %s dependency on %s", unit_dependency_to_string(UNIT_AFTER), p->what);
 
        return 0;
}
 
static bool mount_is_extrinsic(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
        MountParameters *p;
 
        /* Returns true for all units that are "magic" and should be excluded from the usual
         * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
         * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
         * ourselves but it's fine if the user operates on them with us. */
 
        /* We only automatically manage mounts if we are in system mode */
        if (MANAGER_IS_USER(u->manager))
                return true;
 
        p = get_mount_parameters(m);
        if (p && fstab_is_extrinsic(m->where, p->options))
                return true;
 
        return false;
}
 
static int mount_add_default_ordering_dependencies(Mount *m, MountParameters *p, UnitDependencyMask mask) {
        const char *after, *before, *e;
        int r;
 
        assert(m);
 
        e = path_startswith(m->where, "/sysroot");
        if (e && in_initrd()) {
                /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
                 * it's not technically part of the basic initrd filesystem itself, and so
                 * shouldn't inherit the default Before=local-fs.target dependency. However,
                 * these mounts still need to start after local-fs-pre.target, as a sync point
                 * for things like systemd-hibernate-resume.service that should start before
                 * any mounts. */
 
                after = SPECIAL_LOCAL_FS_PRE_TARGET;
                before = isempty(e) ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_INITRD_FS_TARGET;
 
        } else if (in_initrd() && path_startswith(m->where, "/sysusr/usr")) {
                after = SPECIAL_LOCAL_FS_PRE_TARGET;
                before = SPECIAL_INITRD_USR_FS_TARGET;
 
        } else if (mount_is_network(p)) {
                after = SPECIAL_REMOTE_FS_PRE_TARGET;
                before = SPECIAL_REMOTE_FS_TARGET;
 
        } else {
                after = SPECIAL_LOCAL_FS_PRE_TARGET;
                before = SPECIAL_LOCAL_FS_TARGET;
        }
 
        if (before && !mount_is_nofail(m)) {
                r = unit_add_dependency_by_name(UNIT(m), UNIT_BEFORE, before, /* add_reference= */ true, mask);
                if (r < 0)
                        return r;
        }
 
        if (after) {
                r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, /* add_reference= */ true, mask);
                if (r < 0)
                        return r;
        }
 
        r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET,
                                              /* add_reference= */ true, mask);
        if (r < 0)
                return r;
 
        /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
        if (streq_ptr(p->fstype, "tmpfs")) {
                r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET,
                                                /* add_reference= */ true, mask);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int mount_add_default_network_dependencies(Mount *m, MountParameters *p, UnitDependencyMask mask) {
        int r;
 
        assert(m);
 
        if (!mount_is_network(p))
                return 0;
 
        /* We order ourselves after network.target. This is primarily useful at shutdown: services that take
         * down the network should order themselves before network.target, so that they are shut down only
         * after this mount unit is stopped. */
 
        r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET,
                                        /* add_reference= */ true, mask);
        if (r < 0)
                return r;
 
        /* We pull in network-online.target, and order ourselves after it. This is useful at start-up to
         * actively pull in tools that want to be started before we start mounting network file systems, and
         * whose purpose it is to delay this until the network is "up". */
 
        return unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET,
                                                 /* add_reference= */ true, mask);
}
 
static int mount_add_default_dependencies(Mount *m) {
        UnitDependencyMask mask;
        MountParameters *p;
        int r;
 
        assert(m);
 
        if (!UNIT(m)->default_dependencies)
                return 0;
 
        /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
         * guaranteed to stay mounted the whole time, since our system is on it.  Also, don't
         * bother with anything mounted below virtual file systems, it's also going to be virtual,
         * and hence not worth the effort. */
        if (mount_is_extrinsic(UNIT(m)))
                return 0;
 
        p = get_mount_parameters(m);
        if (!p)
                return 0;
 
        mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO : UNIT_DEPENDENCY_MOUNT_FILE;
 
        r = mount_add_default_ordering_dependencies(m, p, mask);
        if (r < 0)
                return r;
 
        r = mount_add_default_network_dependencies(m, p, mask);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int mount_verify(Mount *m) {
        _cleanup_free_ char *e = NULL;
        MountParameters *p;
        int r;
 
        assert(m);
        assert(UNIT(m)->load_state == UNIT_LOADED);
 
        if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual)
                return -ENOENT;
 
        r = unit_name_from_path(m->where, ".mount", &e);
        if (r < 0)
                return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m");
 
        if (!unit_has_name(UNIT(m), e))
                return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing.");
 
        if (mount_point_is_api(m->where) || mount_point_ignore(m->where))
                return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC),
                                            "Cannot create mount unit for API file system '%s'. Refusing.", m->where);
 
        if (mount_point_is_credentials(UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME], m->where))
                return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC),
                                            "Cannot create mount unit for credential mount '%s'. Refusing.", m->where);
 
        p = get_mount_parameters_fragment(m);
        if (p && !p->what && !UNIT(m)->perpetual)
                return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "What= setting is missing. Refusing.");
 
        return 0;
}
 
static int mount_add_non_exec_dependencies(Mount *m) {
        int r;
 
        assert(m);
 
        /* We may be called due to this mount appearing in /proc/self/mountinfo, hence we clear all existing
         * dependencies that were initialized from the unit file but whose final value really depends on the
         * content of /proc/self/mountinfo. Some (such as m->where) might have become stale now. */
        unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO | UNIT_DEPENDENCY_MOUNT_FILE);
 
        if (!m->where)
                return 0;
 
        /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
         * resulting from the ExecContext and such. */
 
        r = mount_add_device_dependencies(m);
        if (r < 0)
                return r;
 
        r = mount_add_mount_dependencies(m);
        if (r < 0)
                return r;
 
        r = mount_add_default_dependencies(m);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int mount_add_extras(Mount *m) {
        Unit *u = UNIT(ASSERT_PTR(m));
        int r;
 
        /* Note: this call might be called after we already have been loaded once (and even when it has already been
         * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
         * to run with an already set up unit. */
 
        if (u->fragment_path)
                m->from_fragment = true;
 
        if (!m->where) {
                r = unit_name_to_path(u->id, &m->where);
                if (r == -ENAMETOOLONG)
                        log_unit_error_errno(u, r, "Failed to derive mount point path from unit name, because unit name is hashed. "
                                                   "Set \"Where=\" in the unit file explicitly.");
                if (r < 0)
                        return r;
        }
 
        path_simplify(m->where);
 
        if (!u->description) {
                _cleanup_free_ char *w = mount_get_where_escaped(m);
                if (!w)
                        return log_oom();
 
                r = unit_set_description(u, w);
                if (r < 0)
                        return r;
        }
 
        r = unit_patch_contexts(u);
        if (r < 0)
                return r;
 
        r = unit_add_exec_dependencies(u, &m->exec_context);
        if (r < 0)
                return r;
 
        r = unit_set_default_slice(u);
        if (r < 0)
                return r;
 
        r = mount_add_non_exec_dependencies(m);
        if (r < 0)
                return r;
 
        return 0;
}
 
static void mount_load_root_mount(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
 
        if (!unit_has_name(u, SPECIAL_ROOT_MOUNT))
                return;
 
        u->perpetual = true;
        u->default_dependencies = false;
 
        /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
        m->exec_context.std_output = EXEC_OUTPUT_NULL;
        m->exec_context.std_input = EXEC_INPUT_NULL;
 
        if (!u->description)
                u->description = strdup("Root Mount");
}
 
static int mount_load(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
        int r;
 
        assert(u->load_state == UNIT_STUB);
 
        mount_load_root_mount(u);
 
        bool from_kernel = m->from_proc_self_mountinfo || u->perpetual;
 
        r = unit_load_fragment_and_dropin(u, /* fragment_required= */ !from_kernel);
 
        /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
         * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
         * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
         * 'slice' field for a unit once it is active. */
        if (u->load_state == UNIT_LOADED || from_kernel)
                RET_GATHER(r, mount_add_extras(m));
 
        if (r < 0)
                return r;
 
        if (u->load_state != UNIT_LOADED)
                return 0;
 
        return mount_verify(m);
}
 
static void mount_set_state(Mount *m, MountState state) {
        MountState old_state;
 
        assert(m);
 
        if (m->state != state)
                bus_unit_send_pending_change_signal(UNIT(m), false);
 
        old_state = m->state;
        m->state = state;
 
        if (!MOUNT_STATE_WITH_PROCESS(state)) {
                m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
                mount_unwatch_control_pid(m);
                m->control_command = NULL;
                m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
        }
 
        if (state != old_state)
                log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
 
        unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
}
 
static int mount_coldplug(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
        int r;
 
        assert(m->state == MOUNT_DEAD);
 
        if (m->deserialized_state == m->state)
                return 0;
 
        if (pidref_is_set(&m->control_pid) &&
            pidref_is_unwaited(&m->control_pid) > 0 &&
            MOUNT_STATE_WITH_PROCESS(m->deserialized_state)) {
 
                r = unit_watch_pidref(UNIT(m), &m->control_pid, /* exclusive= */ false);
                if (r < 0)
                        return r;
 
                r = mount_arm_timer(m, /* relative= */ false, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
                if (r < 0)
                        return r;
        }
 
        if (!IN_SET(m->deserialized_state, MOUNT_DEAD, MOUNT_FAILED))
                (void) unit_setup_exec_runtime(u);
 
        mount_set_state(m, m->deserialized_state);
        return 0;
}
 
static void mount_catchup(Unit *u) {
        Mount *m = ASSERT_PTR(MOUNT(u));
 
        /* Adjust the deserialized state. See comments in mount_process_proc_self_mountinfo(). */
        if (m->from_proc_self_mountinfo)
                switch (m->state) {
                case MOUNT_DEAD:
                case MOUNT_FAILED:
                        assert(!pidref_is_set(&m->control_pid));
                        (void) unit_acquire_invocation_id(u);
                        mount_cycle_clear(m);
                        mount_enter_mounted(m, MOUNT_SUCCESS);
                        break;
                case MOUNT_MOUNTING:
                        assert(pidref_is_set(&m->control_pid));
                        mount_set_state(m, MOUNT_MOUNTING_DONE);
                        break;
                default:
                        ;
                }
        else
                switch (m->state) {
                case MOUNT_MOUNTING_DONE:
                        assert(pidref_is_set(&m->control_pid));
                        mount_set_state(m, MOUNT_MOUNTING);
                        break;
                case MOUNT_MOUNTED:
                        assert(!pidref_is_set(&m->control_pid));
                        mount_enter_dead(m, MOUNT_SUCCESS, /* flush_result= */ false);
                        break;
                default:
                        ;
                }
}
 
static void mount_dump(Unit *u, FILE *f, const char *prefix) {
        Mount *m = ASSERT_PTR(MOUNT(u));
        MountParameters *p;
        const char *prefix2;
 
        assert(f);
 
        prefix = strempty(prefix);
        prefix2 = strjoina(prefix, "\t");
 
        p = get_mount_parameters(m);
 
        fprintf(f,
                "%sMount State: %s\n"
                "%sResult: %s\n"
                "%sClean Result: %s\n"
                "%sWhere: %s\n"
                "%sWhat: %s\n"
                "%sFile System Type: %s\n"
                "%sOptions: %s\n"
                "%sFrom /proc/self/mountinfo: %s\n"
                "%sFrom fragment: %s\n"
                "%sExtrinsic: %s\n"
                "%sDirectoryMode: %04o\n"
                "%sSloppyOptions: %s\n"
                "%sLazyUnmount: %s\n"
                "%sForceUnmount: %s\n"
                "%sReadWriteOnly: %s\n"
                "%sTimeoutSec: %s\n",
                prefix, mount_state_to_string(m->state),
                prefix, mount_result_to_string(m->result),
                prefix, mount_result_to_string(m->clean_result),
                prefix, m->where,
                prefix, p ? strna(p->what) : "n/a",
                prefix, p ? strna(p->fstype) : "n/a",
                prefix, p ? strna(p->options) : "n/a",
                prefix, yes_no(m->from_proc_self_mountinfo),
                prefix, yes_no(m->from_fragment),
                prefix, yes_no(mount_is_extrinsic(u)),
                prefix, m->directory_mode,
                prefix, yes_no(m->sloppy_options),
                prefix, yes_no(m->lazy_unmount),
                prefix, yes_no(m->force_unmount),
                prefix, yes_no(m->read_write_only),
                prefix, FORMAT_TIMESPAN(m->timeout_usec, USEC_PER_SEC));
 
        if (pidref_is_set(&m->control_pid))
                fprintf(f,
                        "%sControl PID: "PID_FMT"\n",
                        prefix, m->control_pid.pid);
 
        exec_context_dump(&m->exec_context, f, prefix);
        kill_context_dump(&m->kill_context, f, prefix);
        cgroup_context_dump(UNIT(m), f, prefix);
 
        for (MountExecCommand c = 0; c < _MOUNT_EXEC_COMMAND_MAX; c++) {
                if (!m->exec_command[c].argv)
                        continue;
 
                fprintf(f, "%s%s %s:\n",
                        prefix, glyph(GLYPH_ARROW_RIGHT), mount_exec_command_to_string(c));
 
                exec_command_dump(m->exec_command + c, f, prefix2);
        }
}
 
static ExecFlags mount_exec_flags(MountState state) {
        ExecFlags flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN;
 
        assert(IN_SET(state, MOUNT_MOUNTING, MOUNT_REMOUNTING, MOUNT_UNMOUNTING));
 
        if (IN_SET(state, MOUNT_MOUNTING, MOUNT_REMOUNTING))
                flags |= EXEC_SETUP_CREDENTIALS;
 
        return flags;
}
 
static int mount_spawn(Mount *m, ExecCommand *c, ExecFlags flags, PidRef *ret_pid) {
        _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT(flags);
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
        int r;
 
        assert(m);
        assert(c);
        assert(ret_pid);
 
        r = unit_prepare_exec(UNIT(m));
        if (r < 0)
                return r;
 
        r = mount_arm_timer(m, /* relative= */ true, m->timeout_usec);
        if (r < 0)
                return r;
 
        r = unit_set_exec_params(UNIT(m), &exec_params);
        if (r < 0)
                return r;
 
        /* Assume the label inherited from systemd as the fallback */
        exec_params.fallback_smack_process_label = NULL;
 
        r = exec_spawn(UNIT(m),
                       c,
                       &m->exec_context,
                       &exec_params,
                       m->exec_runtime,
                       &m->cgroup_context,
                       &pidref);
        if (r < 0)
                return r;
 
        r = unit_watch_pidref(UNIT(m), &pidref, /* exclusive= */ true);
        if (r < 0)
                return r;
 
        *ret_pid = TAKE_PIDREF(pidref);
        return 0;
}
 
static void mount_enter_dead(Mount *m, MountResult f, bool flush_result) {
        assert(m);
 
        if (m->result == MOUNT_SUCCESS || f == MOUNT_FAILURE_START_LIMIT_HIT || flush_result)
                m->result = f;
 
        unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
        unit_warn_leftover_processes(UNIT(m), /* start= */ false);
 
        mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
 
        m->exec_runtime = exec_runtime_destroy(m->exec_runtime);
 
        unit_destroy_runtime_data(UNIT(m), &m->exec_context, /* destroy_runtime_dir= */ true);
 
        unit_unref_uid_gid(UNIT(m), true);
 
        /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from
         * .mount unit. */
        (void) mount_add_non_exec_dependencies(m);
}
 
static void mount_enter_mounted(Mount *m, MountResult f) {
        assert(m);
 
        if (m->result == MOUNT_SUCCESS)
                m->result = f;
 
        /* Refer to service_set_state() handling of SERVICE_EXITED state for rationale. In particular
         * for mount units let's not leave cred mounts around, otherwise the actual number of mounts would be
         * somewhat doubled.
         *
         * Note that this effectively means fresh creds are used during remount, but that's mostly what
         * users would expect anyways. */
        unit_destroy_runtime_data(UNIT(m), &m->exec_context, /* destroy_runtime_dir= */ false);
 
        mount_set_state(m, MOUNT_MOUNTED);
}
 
static void mount_enter_dead_or_mounted(Mount *m, MountResult f, bool flush_result) {
        assert(m);
 
        /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point.
         * We use this whenever we executed an operation, so that our internal state reflects what
         * the kernel says again, after all ultimately we just mirror the kernel's internal state on this.
         *
         * Note that flush_result only applies to mount_enter_dead(), since that's when the result gets
         * turned into unit end state. */
 
        if (m->from_proc_self_mountinfo)
                mount_enter_mounted(m, f);
        else
                mount_enter_dead(m, f, flush_result);
}
 
static int state_to_kill_operation(MountState state) {
        switch (state) {
 
        case MOUNT_REMOUNTING_SIGTERM:
                return KILL_RESTART;
 
        case MOUNT_UNMOUNTING_SIGTERM:
                return KILL_TERMINATE;
 
        case MOUNT_REMOUNTING_SIGKILL:
        case MOUNT_UNMOUNTING_SIGKILL:
                return KILL_KILL;
 
        default:
                return _KILL_OPERATION_INVALID;
        }
}
 
static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
        int r;
 
        assert(m);
 
        if (m->result == MOUNT_SUCCESS)
                m->result = f;
 
        r = unit_kill_context(UNIT(m), state_to_kill_operation(state));
        if (r < 0) {
                log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
                goto fail;
        }
 
        if (r > 0) {
                r = mount_arm_timer(m, /* relative= */ true, m->timeout_usec);
                if (r < 0) {
                        log_unit_warning_errno(UNIT(m), r, "Failed to install timer: %m");
                        goto fail;
                }
 
                mount_set_state(m, state);
        } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
                mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
        else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
                mount_enter_mounted(m, MOUNT_SUCCESS);
        else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
                mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
        else
                mount_enter_dead_or_mounted(m, MOUNT_SUCCESS, /* flush_result= */ false);
 
        return;
 
fail:
        mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES, /* flush_result= */ false);