]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/mount.c
core/mount: if umount(8) fails but mount disappeared, assume success
[thirdparty/systemd.git] / src / core / mount.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
5cb5a6ff 3#include <errno.h>
4f5dd394 4#include <signal.h>
b08d03ff 5#include <stdio.h>
ef734fd6 6#include <sys/epoll.h>
5cb5a6ff 7
20ad4cfd 8#include "sd-messages.h"
4f5dd394 9
b5efdb8a 10#include "alloc-util.h"
4139c1b2 11#include "dbus-mount.h"
6fcbec6f 12#include "dbus-unit.h"
57b7a260 13#include "device.h"
9a57c629 14#include "exit-status.h"
f97b34a6 15#include "format-util.h"
218cfe23 16#include "fs-util.h"
4f5dd394 17#include "fstab-util.h"
baa6a42d 18#include "initrd-util.h"
fb36b133 19#include "libmount-util.h"
4f5dd394
LP
20#include "log.h"
21#include "manager.h"
35cd0ba5 22#include "mkdir-label.h"
4f5dd394
LP
23#include "mount-setup.h"
24#include "mount.h"
049af8ad 25#include "mountpoint-util.h"
6bedfcbb 26#include "parse-util.h"
4f5dd394 27#include "path-util.h"
7b3e062c 28#include "process-util.h"
d68c645b 29#include "serialize.h"
4f5dd394 30#include "special.h"
218cfe23 31#include "stat-util.h"
8b43440b 32#include "string-table.h"
b5efdb8a 33#include "string-util.h"
4f5dd394
LP
34#include "strv.h"
35#include "unit-name.h"
36#include "unit.h"
8dbab37d 37#include "utf8.h"
5cb5a6ff 38
7d54a03a
LP
39#define RETRY_UMOUNT_MAX 32
40
f50e0a01
LP
41static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
42 [MOUNT_DEAD] = UNIT_INACTIVE,
43 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
b6ba0c16 44 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVATING,
f50e0a01 45 [MOUNT_MOUNTED] = UNIT_ACTIVE,
032ff4af 46 [MOUNT_REMOUNTING] = UNIT_RELOADING,
f50e0a01 47 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
032ff4af
LP
48 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
49 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
e537352b
LP
50 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
51 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
17e9d53d
YW
52 [MOUNT_FAILED] = UNIT_FAILED,
53 [MOUNT_CLEANING] = UNIT_MAINTENANCE,
f50e0a01 54};
5cb5a6ff 55
718db961
LP
56static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
57static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
01400460
YW
58static void mount_enter_dead(Mount *m, MountResult f);
59static void mount_enter_mounted(Mount *m, MountResult f);
60static void mount_cycle_clear(Mount *m);
35080486 61static int mount_process_proc_self_mountinfo(Manager *m);
718db961 62
c634f3d2
LP
63static bool MOUNT_STATE_WITH_PROCESS(MountState state) {
64 return IN_SET(state,
65 MOUNT_MOUNTING,
66 MOUNT_MOUNTING_DONE,
67 MOUNT_REMOUNTING,
68 MOUNT_REMOUNTING_SIGTERM,
69 MOUNT_REMOUNTING_SIGKILL,
70 MOUNT_UNMOUNTING,
71 MOUNT_UNMOUNTING_SIGTERM,
17e9d53d
YW
72 MOUNT_UNMOUNTING_SIGKILL,
73 MOUNT_CLEANING);
c634f3d2
LP
74}
75
47cd17ea
LP
76static MountParameters* get_mount_parameters_fragment(Mount *m) {
77 assert(m);
78
79 if (m->from_fragment)
80 return &m->parameters_fragment;
81
82 return NULL;
83}
84
85static MountParameters* get_mount_parameters(Mount *m) {
86 assert(m);
87
88 if (m->from_proc_self_mountinfo)
89 return &m->parameters_proc_self_mountinfo;
90
91 return get_mount_parameters_fragment(m);
92}
93
7121cbcf
LP
94static bool mount_is_network(const MountParameters *p) {
95 assert(p);
96
97 if (fstab_test_option(p->options, "_netdev\0"))
fc676b00
ZJS
98 return true;
99
7121cbcf 100 if (p->fstype && fstype_is_network(p->fstype))
fc676b00
ZJS
101 return true;
102
103 return false;
104}
105
5a7c4f4f
FB
106static bool mount_is_nofail(const Mount *m) {
107 assert(m);
108
109 if (!m->from_fragment)
110 return false;
111
112 return fstab_test_yes_no_option(m->parameters_fragment.options, "nofail\0" "fail\0");
113}
114
d3bd0986
MK
115static bool mount_is_loop(const MountParameters *p) {
116 assert(p);
117
118 if (fstab_test_option(p->options, "loop\0"))
119 return true;
120
121 return false;
122}
123
e6a7b9f4 124static bool mount_is_bind(const MountParameters *p) {
fc676b00 125 assert(p);
35df78cd 126 return fstab_is_bind(p->options, p->fstype);
fc676b00
ZJS
127}
128
707ecf14
MY
129static int mount_is_bound_to_device(Mount *m) {
130 _cleanup_free_ char *value = NULL;
ebc8968b 131 const MountParameters *p;
707ecf14 132 int r;
ebc8968b 133
47cd17ea
LP
134 assert(m);
135
136 /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do
707ecf14 137 * this by checking for the x-systemd.device-bound= mount option. If it is enabled we use BindsTo=,
47cd17ea
LP
138 * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see
139 * below. */
140
141 p = get_mount_parameters(m);
142 if (!p)
143 return false;
ebc8968b 144
707ecf14
MY
145 r = fstab_filter_options(p->options, "x-systemd.device-bound\0", NULL, &value, NULL, NULL);
146 if (r < 0)
147 return r;
148 if (r == 0)
149 return -EIDRM; /* If unspecified at all, return recognizable error */
150
151 if (isempty(value))
152 return true;
153
154 return parse_boolean(value);
ebc8968b
FB
155}
156
47cd17ea 157static bool mount_propagate_stop(Mount *m) {
707ecf14
MY
158 int r;
159
47cd17ea
LP
160 assert(m);
161
707ecf14
MY
162 r = mount_is_bound_to_device(m);
163 if (r >= 0)
164 /* If x-systemd.device-bound=no is explicitly requested by user, don't try to set StopPropagatedFrom=.
165 * Also don't bother if true, since with BindsTo= the stop propagation is implicit. */
47cd17ea 166 return false;
707ecf14
MY
167 if (r != -EIDRM)
168 log_debug_errno(r, "Failed to get x-systemd.device-bound= option, ignoring: %m");
47cd17ea
LP
169
170 return m->from_fragment; /* let's propagate stop whenever this is an explicitly configured unit,
171 * otherwise let's not bother. */
172}
173
06e457b1 174static bool mount_needs_quota(const MountParameters *p) {
fc676b00
ZJS
175 assert(p);
176
d72f4a38 177 if (p->fstype && !fstype_needs_quota(p->fstype))
fc676b00
ZJS
178 return false;
179
180 if (mount_is_bind(p))
181 return false;
182
d15d0333
ZJS
183 return fstab_test_option(p->options,
184 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
fc676b00
ZJS
185}
186
a16e1123
LP
187static void mount_init(Unit *u) {
188 Mount *m = MOUNT(u);
5cb5a6ff 189
c73f413d 190 assert(m);
a16e1123 191 assert(u);
ac155bb8 192 assert(u->load_state == UNIT_STUB);
a16e1123 193
c9e120e0 194 m->timeout_usec = u->manager->defaults.timeout_start_usec;
5804e1b6 195
c9e120e0
LP
196 m->exec_context.std_output = u->manager->defaults.std_output;
197 m->exec_context.std_error = u->manager->defaults.std_error;
5804e1b6 198
3e5235b0
LP
199 m->directory_mode = 0755;
200
f00929ad
DJL
201 /* We need to make sure that /usr/bin/mount is always called
202 * in the same process group as us, so that the autofs kernel
a16e1123
LP
203 * side doesn't send us another mount request while we are
204 * already trying to comply its last one. */
74922904 205 m->exec_context.same_pgrp = true;
8d567588 206
360f384f 207 m->control_pid = PIDREF_NULL;
a16e1123 208 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
c8f4d764 209
5bcb0f2b 210 u->ignore_on_isolate = true;
8d567588
LP
211}
212
e9276800 213static int mount_arm_timer(Mount *m, bool relative, usec_t usec) {
718db961
LP
214 assert(m);
215
e9276800 216 return unit_arm_timer(UNIT(m), &m->timer_event_source, relative, usec, mount_dispatch_timer);
718db961
LP
217}
218
a16e1123 219static void mount_unwatch_control_pid(Mount *m) {
5e94833f 220 assert(m);
ea1e0bf1 221 unit_unwatch_pidref_done(UNIT(m), &m->control_pid);
5e94833f
LP
222}
223
e537352b
LP
224static void mount_parameters_done(MountParameters *p) {
225 assert(p);
226
a26592cf
LP
227 p->what = mfree(p->what);
228 p->options = mfree(p->options);
229 p->fstype = mfree(p->fstype);
e537352b
LP
230}
231
87f0e418 232static void mount_done(Unit *u) {
ef734fd6 233 Mount *m = MOUNT(u);
034c6ed7 234
ef734fd6 235 assert(m);
034c6ed7 236
a1e58e8e 237 m->where = mfree(m->where);
f50e0a01 238
e537352b
LP
239 mount_parameters_done(&m->parameters_proc_self_mountinfo);
240 mount_parameters_done(&m->parameters_fragment);
ef734fd6 241
28135da3 242 m->exec_runtime = exec_runtime_free(m->exec_runtime);
9cc54544 243
e537352b
LP
244 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
245 m->control_command = NULL;
f50e0a01 246
a16e1123 247 mount_unwatch_control_pid(m);
f50e0a01 248
5dcadb4c 249 m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
f50e0a01
LP
250}
251
9ddaa3e4 252static int update_parameters_proc_self_mountinfo(
b7bbf890
ZJS
253 Mount *m,
254 const char *what,
255 const char *options,
256 const char *fstype) {
257
258 MountParameters *p;
259 int r, q, w;
260
261 p = &m->parameters_proc_self_mountinfo;
262
bcf58ff5
YW
263 r = free_and_strdup(&p->what, what);
264 if (r < 0)
265 return r;
aaf7b0e4 266
bcf58ff5
YW
267 q = free_and_strdup(&p->options, options);
268 if (q < 0)
269 return q;
b7bbf890
ZJS
270
271 w = free_and_strdup(&p->fstype, fstype);
272 if (w < 0)
273 return w;
274
275 return r > 0 || q > 0 || w > 0;
276}
277
eef85c4a 278static int mount_add_mount_dependencies(Mount *m) {
5c78d8e2 279 MountParameters *pm;
b08d03ff
LP
280 int r;
281
6e2ef85b 282 assert(m);
b08d03ff 283
a57f7e2c 284 if (!path_equal(m->where, "/")) {
eef85c4a
LP
285 _cleanup_free_ char *parent = NULL;
286
287 /* Adds in links to other mount points that might lie further up in the hierarchy */
5f311f8c 288
45519d13
LP
289 r = path_extract_directory(m->where, &parent);
290 if (r < 0)
291 return r;
01f78473 292
9e615fa3 293 r = unit_add_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT, UNIT_MOUNT_REQUIRES);
4f0eedb7 294 if (r < 0)
01f78473 295 return r;
4f0eedb7 296 }
01f78473 297
eef85c4a
LP
298 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
299 * or a loop mount) to be available. */
a57f7e2c 300 pm = get_mount_parameters_fragment(m);
fc676b00
ZJS
301 if (pm && pm->what &&
302 path_is_absolute(pm->what) &&
d3bd0986 303 (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) {
fc676b00 304
9e615fa3 305 r = unit_add_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_REQUIRES);
4f0eedb7 306 if (r < 0)
6e2ef85b 307 return r;
4f0eedb7 308 }
b08d03ff 309
eef85c4a 310 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
9e615fa3
LB
311 for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; ++t) {
312 Unit *other;
313 Set *s = manager_get_units_needing_mounts_for(UNIT(m)->manager, m->where, t);
314
315 SET_FOREACH(other, s) {
316 if (other->load_state != UNIT_LOADED)
317 continue;
318
319 if (other == UNIT(m))
320 continue;
321
322 r = unit_add_dependency(
323 other,
324 UNIT_AFTER,
325 UNIT(m),
326 /* add_reference= */ true,
327 UNIT_DEPENDENCY_PATH);
a57f7e2c
LP
328 if (r < 0)
329 return r;
9e615fa3
LB
330
331 if (UNIT(m)->fragment_path) {
332 /* If we have fragment configuration, then make this dependency required/wanted */
333 r = unit_add_dependency(
334 other,
335 unit_mount_dependency_type_to_dependency_type(t),
336 UNIT(m),
337 /* add_reference= */ true,
338 UNIT_DEPENDENCY_PATH);
339 if (r < 0)
340 return r;
341 }
a57f7e2c 342 }
7c8fa05c
LP
343 }
344
345 return 0;
346}
347
eef85c4a 348static int mount_add_device_dependencies(Mount *m) {
87c734ee 349 UnitDependencyMask mask;
eef85c4a 350 MountParameters *p;
ebc8968b 351 UnitDependency dep;
9fff8a1f 352 int r;
173a8d04
LP
353
354 assert(m);
355
556b6f4e
DDM
356 log_unit_trace(UNIT(m), "Processing implicit device dependencies");
357
06e97888 358 p = get_mount_parameters(m);
556b6f4e
DDM
359 if (!p) {
360 log_unit_trace(UNIT(m), "Missing mount parameters, skipping implicit device dependencies");
173a8d04 361 return 0;
556b6f4e 362 }
173a8d04 363
556b6f4e
DDM
364 if (!p->what) {
365 log_unit_trace(UNIT(m), "Missing mount source, skipping implicit device dependencies");
173a8d04 366 return 0;
556b6f4e 367 }
5c78d8e2 368
556b6f4e
DDM
369 if (mount_is_bind(p)) {
370 log_unit_trace(UNIT(m), "Mount unit is a bind mount, skipping implicit device dependencies");
dd144c63 371 return 0;
556b6f4e 372 }
dd144c63 373
556b6f4e
DDM
374 if (!is_device_path(p->what)) {
375 log_unit_trace(UNIT(m), "Mount source is not a device path, skipping implicit device dependencies");
dd144c63 376 return 0;
556b6f4e 377 }
dd144c63 378
5de0acf4
LP
379 /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for
380 * the root file system specified on the kernel command line. Ignore it here. */
556b6f4e
DDM
381 if (PATH_IN_SET(p->what, "/dev/root", "/dev/nfs")) {
382 log_unit_trace(UNIT(m), "Mount source is in /dev/root or /dev/nfs, skipping implicit device dependencies");
7ba2711d 383 return 0;
556b6f4e 384 }
7ba2711d 385
556b6f4e
DDM
386 if (path_equal(m->where, "/")) {
387 log_unit_trace(UNIT(m), "Mount destination is '/', skipping implicit device dependencies");
dd144c63 388 return 0;
556b6f4e 389 }
dd144c63 390
44b0d1fd 391 /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to
47cd17ea
LP
392 * races when mounts are established by other tools with different backing devices than what we
393 * maintain. The user can still force this to be a BindsTo= dependency with an appropriate option (or
394 * udev property) so the mount units are automatically stopped when the device disappears
44b0d1fd 395 * suddenly. */
707ecf14 396 dep = mount_is_bound_to_device(m) > 0 ? UNIT_BINDS_TO : UNIT_REQUIRES;
ebc8968b 397
87c734ee
FB
398 /* We always use 'what' from /proc/self/mountinfo if mounted */
399 mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO : UNIT_DEPENDENCY_MOUNT_FILE;
400
401 r = unit_add_node_dependency(UNIT(m), p->what, dep, mask);
dd144c63
LP
402 if (r < 0)
403 return r;
556b6f4e
DDM
404 if (r > 0)
405 log_unit_trace(UNIT(m), "Added %s dependency on %s", unit_dependency_to_string(dep), p->what);
406
47cd17ea 407 if (mount_propagate_stop(m)) {
87c734ee 408 r = unit_add_node_dependency(UNIT(m), p->what, UNIT_STOP_PROPAGATED_FROM, mask);
47cd17ea
LP
409 if (r < 0)
410 return r;
556b6f4e
DDM
411 if (r > 0)
412 log_unit_trace(UNIT(m), "Added %s dependency on %s",
413 unit_dependency_to_string(UNIT_STOP_PROPAGATED_FROM), p->what);
47cd17ea 414 }
9fff8a1f 415
87c734ee 416 r = unit_add_blockdev_dependency(UNIT(m), p->what, mask);
556b6f4e
DDM
417 if (r > 0)
418 log_unit_trace(UNIT(m), "Added %s dependency on %s", unit_dependency_to_string(UNIT_AFTER), p->what);
419
49267b1b 420 return 0;
173a8d04
LP
421}
422
eef85c4a 423static int mount_add_quota_dependencies(Mount *m) {
6b1dc2bd 424 MountParameters *p;
eef85c4a 425 int r;
6b1dc2bd
LP
426
427 assert(m);
428
463d0d15 429 if (!MANAGER_IS_SYSTEM(UNIT(m)->manager))
6b1dc2bd
LP
430 return 0;
431
432 p = get_mount_parameters_fragment(m);
433 if (!p)
434 return 0;
435
06e457b1 436 if (!mount_needs_quota(p))
6b1dc2bd
LP
437 return 0;
438
c5d82021
FB
439 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE,
440 /* add_reference= */ true, UNIT_DEPENDENCY_FILE);
6b1dc2bd
LP
441 if (r < 0)
442 return r;
443
c5d82021
FB
444 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE,
445 /* add_reference= */true, UNIT_DEPENDENCY_FILE);
6b1dc2bd
LP
446 if (r < 0)
447 return r;
448
449 return 0;
450}
451
39c79477 452static bool mount_is_extrinsic(Unit *u) {
88ac30a1 453 MountParameters *p;
39c79477 454 Mount *m = MOUNT(u);
ad2706db 455 assert(m);
88ac30a1 456
bc9e5a4c
FB
457 /* Returns true for all units that are "magic" and should be excluded from the usual
458 * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally
459 * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them
460 * ourselves but it's fine if the user operates on them with us. */
ad2706db 461
bc9e5a4c 462 /* We only automatically manage mounts if we are in system mode */
39c79477 463 if (MANAGER_IS_USER(u->manager))
b8e5776d
LP
464 return true;
465
88ac30a1 466 p = get_mount_parameters(m);
bc9e5a4c 467 if (p && fstab_is_extrinsic(m->where, p->options))
ad2706db 468 return true;
88ac30a1 469
ad2706db 470 return false;
88ac30a1
TG
471}
472
92e64e9a
LP
473static bool mount_is_credentials(Mount *m) {
474 const char *e;
475
476 assert(m);
477
478 /* Returns true if this is a credentials mount. We don't want automatic dependencies on credential
479 * mounts, since they are managed by us for even the earliest services, and we never want anything to
480 * be ordered before them hence. */
481
482 e = path_startswith(m->where, UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
483 if (!e)
484 return false;
485
486 return !isempty(path_startswith(e, "credentials"));
487}
488
87c734ee 489static int mount_add_default_ordering_dependencies(Mount *m, MountParameters *p, UnitDependencyMask mask) {
83cdc870 490 const char *after, *before, *e;
d54bab90 491 int r;
2edd4434
LP
492
493 assert(m);
494
83cdc870
FB
495 e = path_startswith(m->where, "/sysroot");
496 if (e && in_initrd()) {
497 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW,
498 * it's not technically part of the basic initrd filesystem itself, and so
6e017b19
WF
499 * shouldn't inherit the default Before=local-fs.target dependency. However,
500 * these mounts still need to start after local-fs-pre.target, as a sync point
760e99bb 501 * for things like systemd-hibernate-resume.service that should start before
6e017b19 502 * any mounts. */
83cdc870 503
6e017b19 504 after = SPECIAL_LOCAL_FS_PRE_TARGET;
83cdc870
FB
505 before = isempty(e) ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_INITRD_FS_TARGET;
506
dbfc0960 507 } else if (in_initrd() && path_startswith(m->where, "/sysusr/usr")) {
6e017b19 508 after = SPECIAL_LOCAL_FS_PRE_TARGET;
dbfc0960
YW
509 before = SPECIAL_INITRD_USR_FS_TARGET;
510
92e64e9a
LP
511 } else if (mount_is_credentials(m))
512 after = before = NULL;
513
514 else if (mount_is_network(p)) {
ea0ec5ce 515 after = SPECIAL_REMOTE_FS_PRE_TARGET;
d54bab90 516 before = SPECIAL_REMOTE_FS_TARGET;
2ec15c4f 517
d54bab90 518 } else {
ea0ec5ce 519 after = SPECIAL_LOCAL_FS_PRE_TARGET;
d54bab90
LP
520 before = SPECIAL_LOCAL_FS_TARGET;
521 }
522
92e64e9a 523 if (before && !mount_is_nofail(m)) {
87c734ee 524 r = unit_add_dependency_by_name(UNIT(m), UNIT_BEFORE, before, /* add_reference= */ true, mask);
8c8203db
YW
525 if (r < 0)
526 return r;
527 }
ea0ec5ce 528
92e64e9a
LP
529 if (after) {
530 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, /* add_reference= */ true, mask);
531 if (r < 0)
532 return r;
533 }
534
535 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET,
536 /* add_reference= */ true, mask);
6e017b19
WF
537 if (r < 0)
538 return r;
e8d2f6cd 539
92e64e9a
LP
540 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
541 if (streq_ptr(p->fstype, "tmpfs") && !mount_is_credentials(m)) {
542 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET,
543 /* add_reference= */ true, mask);
544 if (r < 0)
545 return r;
546 }
547
548 return 0;
61154cf9
FB
549}
550
75b09529
LP
551static int mount_add_default_network_dependencies(Mount *m, MountParameters *p, UnitDependencyMask mask) {
552 int r;
553
554 assert(m);
555
556 if (!mount_is_network(p))
557 return 0;
558
559 /* We order ourselves after network.target. This is primarily useful at shutdown: services that take
560 * down the network should order themselves before network.target, so that they are shut down only
561 * after this mount unit is stopped. */
562
563 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET,
564 /* add_reference= */ true, mask);
6e017b19
WF
565 if (r < 0)
566 return r;
e8d2f6cd 567
75b09529
LP
568 /* We pull in network-online.target, and order ourselves after it. This is useful at start-up to
569 * actively pull in tools that want to be started before we start mounting network file systems, and
570 * whose purpose it is to delay this until the network is "up". */
571
572 return unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET,
87c734ee 573 /* add_reference= */ true, mask);
61154cf9
FB
574}
575
2edd4434 576static int mount_add_default_dependencies(Mount *m) {
87c734ee 577 UnitDependencyMask mask;
9ddc4a26 578 MountParameters *p;
d54bab90 579 int r;
2edd4434
LP
580
581 assert(m);
582
4c9ea260
LP
583 if (!UNIT(m)->default_dependencies)
584 return 0;
585
61154cf9
FB
586 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are
587 * guaranteed to stay mounted the whole time, since our system is on it. Also, don't
588 * bother with anything mounted below virtual file systems, it's also going to be virtual,
589 * and hence not worth the effort. */
39c79477 590 if (mount_is_extrinsic(UNIT(m)))
6b1dc2bd 591 return 0;
2edd4434 592
874d3404
LP
593 p = get_mount_parameters(m);
594 if (!p)
6b1dc2bd
LP
595 return 0;
596
87c734ee
FB
597 mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO : UNIT_DEPENDENCY_MOUNT_FILE;
598
599 r = mount_add_default_ordering_dependencies(m, p, mask);
ad2706db
LP
600 if (r < 0)
601 return r;
eef85c4a 602
75b09529
LP
603 r = mount_add_default_network_dependencies(m, p, mask);
604 if (r < 0)
605 return r;
3e3852b3 606
84214541 607 return 0;
2edd4434
LP
608}
609
8d567588 610static int mount_verify(Mount *m) {
a57f7e2c 611 _cleanup_free_ char *e = NULL;
b294b79f 612 MountParameters *p;
7410616c 613 int r;
a57f7e2c 614
8d567588 615 assert(m);
75193d41 616 assert(UNIT(m)->load_state == UNIT_LOADED);
8d567588 617
1df96fcb 618 if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual)
8cbef760
LP
619 return -ENOENT;
620
7410616c
LP
621 r = unit_name_from_path(m->where, ".mount", &e);
622 if (r < 0)
f2341e0a 623 return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m");
8d567588 624
d85ff944
YW
625 if (!unit_has_name(UNIT(m), e))
626 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing.");
8d567588 627
d85ff944
YW
628 if (mount_point_is_api(m->where) || mount_point_ignore(m->where))
629 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Cannot create mount unit for API file system %s. Refusing.", m->where);
33ff02c9 630
b294b79f 631 p = get_mount_parameters_fragment(m);
0879fbd6
LP
632 if (p && !p->what && !UNIT(m)->perpetual)
633 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC),
634 "What= setting is missing. Refusing.");
4e85aff4 635
d85ff944
YW
636 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP)
637 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
4d0e5dbd 638
8d567588
LP
639 return 0;
640}
641
bf7eedbf
LP
642static int mount_add_non_exec_dependencies(Mount *m) {
643 int r;
49267b1b 644
bf7eedbf
LP
645 assert(m);
646
9c77ffc7
FB
647 /* We may be called due to this mount appearing in /proc/self/mountinfo, hence we clear all existing
648 * dependencies that were initialized from the unit file but whose final value really depends on the
649 * content of /proc/self/mountinfo. Some (such as m->where) might have become stale now. */
87c734ee 650 unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO | UNIT_DEPENDENCY_MOUNT_FILE);
49267b1b
YW
651
652 if (!m->where)
653 return 0;
654
bf7eedbf
LP
655 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
656 * resulting from the ExecContext and such. */
657
658 r = mount_add_device_dependencies(m);
659 if (r < 0)
660 return r;
661
662 r = mount_add_mount_dependencies(m);
663 if (r < 0)
664 return r;
665
666 r = mount_add_quota_dependencies(m);
667 if (r < 0)
668 return r;
669
670 r = mount_add_default_dependencies(m);
671 if (r < 0)
672 return r;
673
674 return 0;
675}
676
1a4ac875
MS
677static int mount_add_extras(Mount *m) {
678 Unit *u = UNIT(m);
e537352b
LP
679 int r;
680
e821075a
LP
681 assert(m);
682
1f736476
LP
683 /* Note: this call might be called after we already have been loaded once (and even when it has already been
684 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
685 * to run with an already set up unit. */
686
e821075a 687 if (u->fragment_path)
1a4ac875 688 m->from_fragment = true;
e537352b 689
1a4ac875 690 if (!m->where) {
7410616c 691 r = unit_name_to_path(u->id, &m->where);
1d0727e7
MS
692 if (r == -ENAMETOOLONG)
693 log_unit_error_errno(u, r, "Failed to derive mount point path from unit name, because unit name is hashed. "
694 "Set \"Where=\" in the unit file explicitly.");
7410616c
LP
695 if (r < 0)
696 return r;
1a4ac875 697 }
a16e1123 698
4ff361cc 699 path_simplify(m->where);
e537352b 700
e821075a 701 if (!u->description) {
1a4ac875
MS
702 r = unit_set_description(u, m->where);
703 if (r < 0)
173a8d04 704 return r;
1a4ac875 705 }
6e2ef85b 706
598459ce
LP
707 r = unit_patch_contexts(u);
708 if (r < 0)
709 return r;
4e67ddd6 710
598459ce 711 r = unit_add_exec_dependencies(u, &m->exec_context);
a016b922
LP
712 if (r < 0)
713 return r;
714
d79200e2 715 r = unit_set_default_slice(u);
1a4ac875
MS
716 if (r < 0)
717 return r;
718
bf7eedbf 719 r = mount_add_non_exec_dependencies(m);
4c9ea260
LP
720 if (r < 0)
721 return r;
598459ce 722
1a4ac875
MS
723 return 0;
724}
725
75193d41 726static void mount_load_root_mount(Unit *u) {
11222d0f
LP
727 assert(u);
728
729 if (!unit_has_name(u, SPECIAL_ROOT_MOUNT))
75193d41 730 return;
11222d0f
LP
731
732 u->perpetual = true;
733 u->default_dependencies = false;
734
735 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
736 MOUNT(u)->exec_context.std_output = EXEC_OUTPUT_NULL;
737 MOUNT(u)->exec_context.std_input = EXEC_INPUT_NULL;
738
739 if (!u->description)
740 u->description = strdup("Root Mount");
11222d0f
LP
741}
742
1a4ac875
MS
743static int mount_load(Unit *u) {
744 Mount *m = MOUNT(u);
75193d41 745 int r, q = 0;
1a4ac875 746
c73f413d 747 assert(m);
1a4ac875
MS
748 assert(u);
749 assert(u->load_state == UNIT_STUB);
750
75193d41 751 mount_load_root_mount(u);
11222d0f 752
c3620770 753 bool fragment_optional = m->from_proc_self_mountinfo || u->perpetual;
75193d41 754 r = unit_load_fragment_and_dropin(u, !fragment_optional);
780ae022
LP
755
756 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
757 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
758 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
759 * 'slice' field for a unit once it is active. */
760 if (u->load_state == UNIT_LOADED || m->from_proc_self_mountinfo || u->perpetual)
75193d41 761 q = mount_add_extras(m);
780ae022 762
1a4ac875
MS
763 if (r < 0)
764 return r;
780ae022
LP
765 if (q < 0)
766 return q;
75193d41
ZJS
767 if (u->load_state != UNIT_LOADED)
768 return 0;
e537352b 769
8d567588 770 return mount_verify(m);
e537352b
LP
771}
772
773static void mount_set_state(Mount *m, MountState state) {
774 MountState old_state;
775 assert(m);
776
6fcbec6f
LP
777 if (m->state != state)
778 bus_unit_send_pending_change_signal(UNIT(m), false);
779
e537352b
LP
780 old_state = m->state;
781 m->state = state;
782
c634f3d2 783 if (!MOUNT_STATE_WITH_PROCESS(state)) {
5dcadb4c 784 m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
a16e1123 785 mount_unwatch_control_pid(m);
e537352b 786 m->control_command = NULL;
a16e1123 787 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
e537352b
LP
788 }
789
790 if (state != old_state)
f2341e0a 791 log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
e537352b 792
96b09de5 793 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
e537352b
LP
794}
795
be847e82 796static int mount_coldplug(Unit *u) {
e537352b 797 Mount *m = MOUNT(u);
a16e1123 798 int r;
e537352b
LP
799
800 assert(m);
801 assert(m->state == MOUNT_DEAD);
802
01400460 803 if (m->deserialized_state == m->state)
5bcb0f2b 804 return 0;
e537352b 805
360f384f 806 if (pidref_is_set(&m->control_pid) &&
4d9f092b 807 pidref_is_unwaited(&m->control_pid) > 0 &&
01400460 808 MOUNT_STATE_WITH_PROCESS(m->deserialized_state)) {
5bcb0f2b 809
495e75ed 810 r = unit_watch_pidref(UNIT(m), &m->control_pid, /* exclusive= */ false);
5bcb0f2b
LP
811 if (r < 0)
812 return r;
e537352b 813
e9276800 814 r = mount_arm_timer(m, /* relative= */ false, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
5bcb0f2b
LP
815 if (r < 0)
816 return r;
a16e1123 817 }
e537352b 818
9cc54544 819 if (!IN_SET(m->deserialized_state, MOUNT_DEAD, MOUNT_FAILED)) {
e8a565cb 820 (void) unit_setup_exec_runtime(u);
9cc54544
LP
821 (void) unit_setup_cgroup_runtime(u);
822 }
29206d46 823
01400460 824 mount_set_state(m, m->deserialized_state);
e537352b 825 return 0;
e537352b
LP
826}
827
01400460
YW
828static void mount_catchup(Unit *u) {
829 Mount *m = MOUNT(ASSERT_PTR(u));
830
831 assert(m);
832
833 /* Adjust the deserialized state. See comments in mount_process_proc_self_mountinfo(). */
834 if (m->from_proc_self_mountinfo)
835 switch (m->state) {
836 case MOUNT_DEAD:
837 case MOUNT_FAILED:
360f384f 838 assert(!pidref_is_set(&m->control_pid));
039f4284 839 (void) unit_acquire_invocation_id(u);
01400460
YW
840 mount_cycle_clear(m);
841 mount_enter_mounted(m, MOUNT_SUCCESS);
842 break;
843 case MOUNT_MOUNTING:
360f384f 844 assert(pidref_is_set(&m->control_pid));
01400460
YW
845 mount_set_state(m, MOUNT_MOUNTING_DONE);
846 break;
847 default:
848 break;
849 }
850 else
851 switch (m->state) {
852 case MOUNT_MOUNTING_DONE:
360f384f 853 assert(pidref_is_set(&m->control_pid));
01400460
YW
854 mount_set_state(m, MOUNT_MOUNTING);
855 break;
856 case MOUNT_MOUNTED:
360f384f 857 assert(!pidref_is_set(&m->control_pid));
01400460
YW
858 mount_enter_dead(m, MOUNT_SUCCESS);
859 break;
860 default:
861 break;
862 }
863}
864
e537352b
LP
865static void mount_dump(Unit *u, FILE *f, const char *prefix) {
866 Mount *m = MOUNT(u);
867 MountParameters *p;
868
869 assert(m);
870 assert(f);
871
cb39ed3f 872 p = get_mount_parameters(m);
e537352b
LP
873
874 fprintf(f,
875 "%sMount State: %s\n"
81a5c6d0 876 "%sResult: %s\n"
17e9d53d 877 "%sClean Result: %s\n"
e537352b
LP
878 "%sWhere: %s\n"
879 "%sWhat: %s\n"
880 "%sFile System Type: %s\n"
881 "%sOptions: %s\n"
e537352b
LP
882 "%sFrom /proc/self/mountinfo: %s\n"
883 "%sFrom fragment: %s\n"
ad2706db 884 "%sExtrinsic: %s\n"
e520950a 885 "%sDirectoryMode: %04o\n"
49915de2 886 "%sSloppyOptions: %s\n"
4f8d40a9 887 "%sLazyUnmount: %s\n"
91899792 888 "%sForceUnmount: %s\n"
c600357b 889 "%sReadWriteOnly: %s\n"
ac8956ef 890 "%sTimeoutSec: %s\n",
a16e1123 891 prefix, mount_state_to_string(m->state),
81a5c6d0 892 prefix, mount_result_to_string(m->result),
17e9d53d 893 prefix, mount_result_to_string(m->clean_result),
e537352b 894 prefix, m->where,
1e4fc9b1
HH
895 prefix, p ? strna(p->what) : "n/a",
896 prefix, p ? strna(p->fstype) : "n/a",
897 prefix, p ? strna(p->options) : "n/a",
e537352b
LP
898 prefix, yes_no(m->from_proc_self_mountinfo),
899 prefix, yes_no(m->from_fragment),
39c79477 900 prefix, yes_no(mount_is_extrinsic(u)),
e520950a 901 prefix, m->directory_mode,
49915de2 902 prefix, yes_no(m->sloppy_options),
4f8d40a9 903 prefix, yes_no(m->lazy_unmount),
91899792 904 prefix, yes_no(m->force_unmount),
c600357b 905 prefix, yes_no(m->read_write_only),
5291f26d 906 prefix, FORMAT_TIMESPAN(m->timeout_usec, USEC_PER_SEC));
e537352b 907
360f384f 908 if (pidref_is_set(&m->control_pid))
e537352b 909 fprintf(f,
ccd06097 910 "%sControl PID: "PID_FMT"\n",
360f384f 911 prefix, m->control_pid.pid);
e537352b
LP
912
913 exec_context_dump(&m->exec_context, f, prefix);
4819ff03 914 kill_context_dump(&m->kill_context, f, prefix);
bc0623df 915 cgroup_context_dump(UNIT(m), f, prefix);
e537352b
LP
916}
917
360f384f 918static int mount_spawn(Mount *m, ExecCommand *c, PidRef *ret_pid) {
3c7416b6 919
fba173ff 920 _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT(
b646fc32 921 EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN);
360f384f 922 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
3c7416b6 923 int r;
a16e1123
LP
924
925 assert(m);
926 assert(c);
ef25552e 927 assert(ret_pid);
a16e1123 928
3c7416b6 929 r = unit_prepare_exec(UNIT(m));
29206d46
LP
930 if (r < 0)
931 return r;
932
e9276800 933 r = mount_arm_timer(m, /* relative= */ true, m->timeout_usec);
36697dc0 934 if (r < 0)
36c16a7c 935 return r;
a16e1123 936
1ad6e8b3
LP
937 r = unit_set_exec_params(UNIT(m), &exec_params);
938 if (r < 0)
939 return r;
9fa95f85 940
f2341e0a
LP
941 r = exec_spawn(UNIT(m),
942 c,
4ad49000 943 &m->exec_context,
9fa95f85 944 &exec_params,
613b411c 945 m->exec_runtime,
6bb00842 946 &m->cgroup_context,
556d2bc4 947 &pidref);
4ad49000 948 if (r < 0)
36c16a7c 949 return r;
a16e1123 950
495e75ed 951 r = unit_watch_pidref(UNIT(m), &pidref, /* exclusive= */ true);
360f384f
LP
952 if (r < 0)
953 return r;
954
955 *ret_pid = TAKE_PIDREF(pidref);
a16e1123 956 return 0;
a16e1123
LP
957}
958
9d2f5178 959static void mount_enter_dead(Mount *m, MountResult f) {
e537352b
LP
960 assert(m);
961
a0fef983 962 if (m->result == MOUNT_SUCCESS)
9d2f5178 963 m->result = f;
e537352b 964
aac99f30 965 unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
4c425434
LP
966 unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_stop);
967
29206d46
LP
968 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
969
28135da3 970 m->exec_runtime = exec_runtime_destroy(m->exec_runtime);
613b411c 971
bb0c0d6f 972 unit_destroy_runtime_data(UNIT(m), &m->exec_context);
e66cf1a3 973
00d9ef85
LP
974 unit_unref_uid_gid(UNIT(m), true);
975
49267b1b
YW
976 /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from
977 * .mount unit. */
978 (void) mount_add_non_exec_dependencies(m);
e537352b
LP
979}
980
9d2f5178 981static void mount_enter_mounted(Mount *m, MountResult f) {
80876c20
LP
982 assert(m);
983
a0fef983 984 if (m->result == MOUNT_SUCCESS)
9d2f5178 985 m->result = f;
80876c20
LP
986
987 mount_set_state(m, MOUNT_MOUNTED);
988}
989
22af0e58
LP
990static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
991 assert(m);
992
993 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
994 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
995 * ultimately we just mirror the kernel's internal state on this. */
996
997 if (m->from_proc_self_mountinfo)
998 mount_enter_mounted(m, f);
999 else
1000 mount_enter_dead(m, f);
1001}
1002
1003static int state_to_kill_operation(MountState state) {
1004 switch (state) {
1005
1006 case MOUNT_REMOUNTING_SIGTERM:
a232ebcc
ZJS
1007 return KILL_RESTART;
1008
22af0e58
LP
1009 case MOUNT_UNMOUNTING_SIGTERM:
1010 return KILL_TERMINATE;
1011
1012 case MOUNT_REMOUNTING_SIGKILL:
1013 case MOUNT_UNMOUNTING_SIGKILL:
1014 return KILL_KILL;
1015
1016 default:
1017 return _KILL_OPERATION_INVALID;
1018 }
1019}
1020
9d2f5178 1021static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
e537352b
LP
1022 int r;
1023
1024 assert(m);
1025
a0fef983 1026 if (m->result == MOUNT_SUCCESS)
9d2f5178 1027 m->result = f;
e537352b 1028
b826e317 1029 r = unit_kill_context(UNIT(m), state_to_kill_operation(state));
d30eb0b5
LP
1030 if (r < 0) {
1031 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
cd2086fe 1032 goto fail;
d30eb0b5 1033 }
e537352b 1034
cd2086fe 1035 if (r > 0) {
e9276800 1036 r = mount_arm_timer(m, /* relative= */ true, m->timeout_usec);
d30eb0b5
LP
1037 if (r < 0) {
1038 log_unit_warning_errno(UNIT(m), r, "Failed to install timer: %m");
80876c20 1039 goto fail;
d30eb0b5 1040 }
e537352b 1041
80876c20 1042 mount_set_state(m, state);
22af0e58 1043 } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 1044 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
22af0e58 1045 else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
9d2f5178 1046 mount_enter_mounted(m, MOUNT_SUCCESS);
22af0e58 1047 else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 1048 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
80876c20 1049 else
22af0e58 1050 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1051
1052 return;
1053
1054fail:
22af0e58 1055 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
5261ba90
TT
1056}
1057
d30eb0b5
LP
1058static int mount_set_umount_command(Mount *m, ExecCommand *c) {
1059 int r;
1060
1061 assert(m);
1062 assert(c);
1063
1064 r = exec_command_set(c, UMOUNT_PATH, m->where, "-c", NULL);
1065 if (r < 0)
1066 return r;
1067
1068 if (m->lazy_unmount) {
1069 r = exec_command_append(c, "-l", NULL);
1070 if (r < 0)
1071 return r;
1072 }
1073
1074 if (m->force_unmount) {
1075 r = exec_command_append(c, "-f", NULL);
1076 if (r < 0)
1077 return r;
1078 }
1079
1080 return 0;
1081}
1082
9d2f5178 1083static void mount_enter_unmounting(Mount *m) {
e537352b
LP
1084 int r;
1085
1086 assert(m);
1087
7d54a03a
LP
1088 /* Start counting our attempts */
1089 if (!IN_SET(m->state,
1090 MOUNT_UNMOUNTING,
1091 MOUNT_UNMOUNTING_SIGTERM,
e323d2e4 1092 MOUNT_UNMOUNTING_SIGKILL))
7d54a03a
LP
1093 m->n_retry_umount = 0;
1094
a16e1123
LP
1095 m->control_command_id = MOUNT_EXEC_UNMOUNT;
1096 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
e537352b 1097
d30eb0b5
LP
1098 r = mount_set_umount_command(m, m->control_command);
1099 if (r < 0) {
1100 log_unit_warning_errno(UNIT(m), r, "Failed to prepare umount command line: %m");
e537352b 1101 goto fail;
d30eb0b5 1102 }
e537352b 1103
a16e1123 1104 mount_unwatch_control_pid(m);
5e94833f 1105
7d54a03a 1106 r = mount_spawn(m, m->control_command, &m->control_pid);
d30eb0b5
LP
1107 if (r < 0) {
1108 log_unit_warning_errno(UNIT(m), r, "Failed to spawn 'umount' task: %m");
e537352b 1109 goto fail;
d30eb0b5 1110 }
e537352b
LP
1111
1112 mount_set_state(m, MOUNT_UNMOUNTING);
e323d2e4 1113
e537352b
LP
1114 return;
1115
1116fail:
22af0e58 1117 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1118}
1119
d30eb0b5
LP
1120static int mount_set_mount_command(Mount *m, ExecCommand *c, const MountParameters *p) {
1121 int r;
1122
1123 assert(m);
1124 assert(c);
1125 assert(p);
1126
1127 r = exec_command_set(c, MOUNT_PATH, p->what, m->where, NULL);
1128 if (r < 0)
1129 return r;
1130
1131 if (m->sloppy_options) {
1132 r = exec_command_append(c, "-s", NULL);
1133 if (r < 0)
1134 return r;
1135 }
1136
1137 if (m->read_write_only) {
1138 r = exec_command_append(c, "-w", NULL);
1139 if (r < 0)
1140 return r;
1141 }
1142
1143 if (p->fstype) {
1144 r = exec_command_append(c, "-t", p->fstype, NULL);
1145 if (r < 0)
1146 return r;
1147 }
1148
1149 _cleanup_free_ char *opts = NULL;
1150 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, NULL, &opts);
1151 if (r < 0)
1152 return r;
1153
1154 if (!isempty(opts)) {
1155 r = exec_command_append(c, "-o", opts, NULL);
1156 if (r < 0)
1157 return r;
1158 }
1159
1160 return 0;
1161}
1162
8d567588 1163static void mount_enter_mounting(Mount *m) {
e537352b 1164 int r;
cb39ed3f 1165 MountParameters *p;
218cfe23 1166 bool source_is_dir = true;
e537352b
LP
1167
1168 assert(m);
1169
25cd4964 1170 r = unit_fail_if_noncanonical(UNIT(m), m->where);
f2341e0a
LP
1171 if (r < 0)
1172 goto fail;
1173
218cfe23
DT
1174 p = get_mount_parameters_fragment(m);
1175 if (p && mount_is_bind(p)) {
1176 r = is_dir(p->what, /* follow = */ true);
1177 if (r < 0 && r != -ENOENT)
1178 log_unit_info_errno(UNIT(m), r, "Failed to determine type of bind mount source '%s', ignoring: %m", p->what);
1179 else if (r == 0)
1180 source_is_dir = false;
1181 }
3e5235b0 1182
218cfe23 1183 if (source_is_dir)
ce427d0e 1184 r = mkdir_p_label(m->where, m->directory_mode);
218cfe23 1185 else
ce427d0e
DDM
1186 r = touch_file(m->where, /* parents = */ true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
1187 if (r < 0 && r != -EEXIST)
1188 log_unit_warning_errno(UNIT(m), r, "Failed to create mount point '%s', ignoring: %m", m->where);
218cfe23 1189
6c75eff6 1190 /* If we are asked to create an OverlayFS, create the upper/work directories if they are missing */
9614dd54 1191 if (p && streq_ptr(p->fstype, "overlay")) {
6c75eff6
LB
1192 _cleanup_strv_free_ char **dirs = NULL;
1193
1194 r = fstab_filter_options(
1195 p->options,
1196 "upperdir\0workdir\0",
1197 /* ret_namefound= */ NULL,
1198 /* ret_value= */ NULL,
1199 &dirs,
1200 /* ret_filtered= */ NULL);
1201 if (r < 0)
1202 log_unit_warning_errno(
1203 UNIT(m),
1204 r,
1205 "Failed to determine upper directory for OverlayFS, ignoring: %m");
1206 else
1207 STRV_FOREACH(d, dirs) {
1208 r = mkdir_p_label(*d, m->directory_mode);
1209 if (r < 0 && r != -EEXIST)
1210 log_unit_warning_errno(
1211 UNIT(m),
1212 r,
1213 "Failed to create overlay directory '%s', ignoring: %m",
1214 *d);
1215 }
1216 }
1217
218cfe23
DT
1218 if (source_is_dir)
1219 unit_warn_if_dir_nonempty(UNIT(m), m->where);
4c425434 1220 unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_start);
a4634b21
LP
1221
1222 m->control_command_id = MOUNT_EXEC_MOUNT;
1223 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
1224
cb39ed3f 1225 /* Create the source directory for bind-mounts if needed */
5dc60faa
AP
1226 if (p && mount_is_bind(p)) {
1227 r = mkdir_p_label(p->what, m->directory_mode);
237ecfee 1228 /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is
574febda
YW
1229 * totally OK, in case the user wants us to overmount a non-directory inode. Also -EROFS can be
1230 * returned on read-only filesystem. Moreover, -EACCES (and also maybe -EPERM?) may be returned
1231 * when the path is on NFS. See issue #24120. All such errors will be logged in the debug level. */
e5e6b7c2 1232 if (r < 0 && r != -EEXIST)
574febda
YW
1233 log_unit_full_errno(UNIT(m),
1234 (r == -EROFS || ERRNO_IS_PRIVILEGE(r)) ? LOG_DEBUG : LOG_WARNING,
1235 r, "Failed to make bind mount source '%s', ignoring: %m", p->what);
5dc60faa 1236 }
5261ba90 1237
b294b79f 1238 if (p) {
d30eb0b5
LP
1239 r = mount_set_mount_command(m, m->control_command, p);
1240 if (r < 0) {
1241 log_unit_warning_errno(UNIT(m), r, "Failed to prepare mount command line: %m");
17a1c597 1242 goto fail;
d30eb0b5
LP
1243 }
1244 } else {
1245 r = log_unit_warning_errno(UNIT(m), SYNTHETIC_ERRNO(ENOENT), "No mount parameters to operate on.");
e537352b 1246 goto fail;
d30eb0b5 1247 }
e537352b 1248
a16e1123 1249 mount_unwatch_control_pid(m);
5e94833f 1250
257f1d8e 1251 r = mount_spawn(m, m->control_command, &m->control_pid);
d30eb0b5
LP
1252 if (r < 0) {
1253 log_unit_warning_errno(UNIT(m), r, "Failed to spawn 'mount' task: %m");
e537352b 1254 goto fail;
d30eb0b5 1255 }
e537352b
LP
1256
1257 mount_set_state(m, MOUNT_MOUNTING);
e537352b
LP
1258 return;
1259
1260fail:
22af0e58 1261 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1262}
1263
850b7410
LP
1264static void mount_set_reload_result(Mount *m, MountResult result) {
1265 assert(m);
1266
1267 /* Only store the first error we encounter */
1268 if (m->reload_result != MOUNT_SUCCESS)
1269 return;
1270
1271 m->reload_result = result;
1272}
1273
9d2f5178 1274static void mount_enter_remounting(Mount *m) {
e537352b 1275 int r;
b294b79f 1276 MountParameters *p;
e537352b
LP
1277
1278 assert(m);
1279
850b7410
LP
1280 /* Reset reload result when we are about to start a new remount operation */
1281 m->reload_result = MOUNT_SUCCESS;
1282
a16e1123
LP
1283 m->control_command_id = MOUNT_EXEC_REMOUNT;
1284 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
e537352b 1285
b294b79f
MO
1286 p = get_mount_parameters_fragment(m);
1287 if (p) {
e537352b
LP
1288 const char *o;
1289
b294b79f
MO
1290 if (p->options)
1291 o = strjoina("remount,", p->options);
718db961 1292 else
e537352b
LP
1293 o = "remount";
1294
f00929ad 1295 r = exec_command_set(m->control_command, MOUNT_PATH,
b294b79f 1296 p->what, m->where,
e86b3761 1297 "-o", o, NULL);
e86b3761
ZJS
1298 if (r >= 0 && m->sloppy_options)
1299 r = exec_command_append(m->control_command, "-s", NULL);
c600357b
MH
1300 if (r >= 0 && m->read_write_only)
1301 r = exec_command_append(m->control_command, "-w", NULL);
b294b79f
MO
1302 if (r >= 0 && p->fstype)
1303 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
d30eb0b5
LP
1304 if (r < 0) {
1305 log_unit_warning_errno(UNIT(m), r, "Failed to prepare remount command line: %m");
1306 goto fail;
1307 }
1308
1309 } else {
1310 r = log_unit_warning_errno(UNIT(m), SYNTHETIC_ERRNO(ENOENT), "No mount parameters to operate on.");
e537352b 1311 goto fail;
d30eb0b5 1312 }
e537352b 1313
a16e1123 1314 mount_unwatch_control_pid(m);
5e94833f 1315
718db961 1316 r = mount_spawn(m, m->control_command, &m->control_pid);
d30eb0b5
LP
1317 if (r < 0) {
1318 log_unit_warning_errno(UNIT(m), r, "Failed to spawn 'remount' task: %m");
e537352b 1319 goto fail;
d30eb0b5 1320 }
e537352b
LP
1321
1322 mount_set_state(m, MOUNT_REMOUNTING);
e537352b
LP
1323 return;
1324
1325fail:
850b7410 1326 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
22af0e58 1327 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1328}
1329
7eba1463
LP
1330static void mount_cycle_clear(Mount *m) {
1331 assert(m);
1332
1333 /* Clear all state we shall forget for this new cycle */
1334
1335 m->result = MOUNT_SUCCESS;
1336 m->reload_result = MOUNT_SUCCESS;
1337 exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
9cc54544
LP
1338
1339 if (m->cgroup_runtime)
1340 m->cgroup_runtime->reset_accounting = true;
7eba1463
LP
1341}
1342
e537352b
LP
1343static int mount_start(Unit *u) {
1344 Mount *m = MOUNT(u);
07299350 1345 int r;
e537352b
LP
1346
1347 assert(m);
1348
1349 /* We cannot fulfill this request right now, try again later
1350 * please! */
f2aed307
LP
1351 if (IN_SET(m->state,
1352 MOUNT_UNMOUNTING,
1353 MOUNT_UNMOUNTING_SIGTERM,
17e9d53d
YW
1354 MOUNT_UNMOUNTING_SIGKILL,
1355 MOUNT_CLEANING))
e537352b
LP
1356 return -EAGAIN;
1357
1358 /* Already on it! */
db39a627 1359 if (IN_SET(m->state, MOUNT_MOUNTING, MOUNT_MOUNTING_DONE))
e537352b
LP
1360 return 0;
1361
f2aed307 1362 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
e537352b 1363
4b58153d
LP
1364 r = unit_acquire_invocation_id(u);
1365 if (r < 0)
1366 return r;
1367
7eba1463 1368 mount_cycle_clear(m);
8d567588 1369 mount_enter_mounting(m);
7eba1463 1370
82a2b6bb 1371 return 1;
e537352b
LP
1372}
1373
1374static int mount_stop(Unit *u) {
1375 Mount *m = MOUNT(u);
1376
1377 assert(m);
1378
1e122561
YW
1379 /* When we directly call umount() for a path, then the state of the corresponding mount unit may be
1380 * outdated. Let's re-read mountinfo now and update the state. */
1381 if (m->invalidated_state)
1382 (void) mount_process_proc_self_mountinfo(u->manager);
1383
22af0e58
LP
1384 switch (m->state) {
1385
1386 case MOUNT_UNMOUNTING:
1387 case MOUNT_UNMOUNTING_SIGKILL:
1388 case MOUNT_UNMOUNTING_SIGTERM:
1389 /* Already on it */
e537352b
LP
1390 return 0;
1391
22af0e58
LP
1392 case MOUNT_MOUNTING:
1393 case MOUNT_MOUNTING_DONE:
1394 case MOUNT_REMOUNTING:
1395 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1396 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1397 return 0;
e537352b 1398
22af0e58
LP
1399 case MOUNT_REMOUNTING_SIGTERM:
1400 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1401 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1402 return 0;
1403
1404 case MOUNT_REMOUNTING_SIGKILL:
1405 /* as above */
1406 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1407 return 0;
1408
1409 case MOUNT_MOUNTED:
1410 mount_enter_unmounting(m);
1411 return 1;
1412
17e9d53d
YW
1413 case MOUNT_CLEANING:
1414 /* If we are currently cleaning, then abort it, brutally. */
1415 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
1416 return 0;
1417
1e122561
YW
1418 case MOUNT_DEAD:
1419 case MOUNT_FAILED:
1420 /* The mount has just been unmounted by somebody else. */
1421 return 0;
1422
22af0e58 1423 default:
04499a70 1424 assert_not_reached();
22af0e58 1425 }
e537352b
LP
1426}
1427
1428static int mount_reload(Unit *u) {
1429 Mount *m = MOUNT(u);
1430
1431 assert(m);
e537352b
LP
1432 assert(m->state == MOUNT_MOUNTED);
1433
9d2f5178 1434 mount_enter_remounting(m);
22af0e58 1435
2d018ae2 1436 return 1;
e537352b
LP
1437}
1438
a16e1123
LP
1439static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1440 Mount *m = MOUNT(u);
1441
1442 assert(m);
1443 assert(f);
1444 assert(fds);
1445
d68c645b
LP
1446 (void) serialize_item(f, "state", mount_state_to_string(m->state));
1447 (void) serialize_item(f, "result", mount_result_to_string(m->result));
1448 (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
2c09fb81 1449 (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
2a7451dc 1450 (void) serialize_pidref(f, fds, "control-pid", &m->control_pid);
a16e1123
LP
1451
1452 if (m->control_command_id >= 0)
d68c645b 1453 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
a16e1123
LP
1454
1455 return 0;
1456}
1457
1458static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1459 Mount *m = MOUNT(u);
2c09fb81 1460 int r;
a16e1123 1461
c73f413d 1462 assert(m);
a16e1123
LP
1463 assert(u);
1464 assert(key);
1465 assert(value);
1466 assert(fds);
1467
1468 if (streq(key, "state")) {
1469 MountState state;
1470
7fb1d980
YW
1471 state = mount_state_from_string(value);
1472 if (state < 0)
1473 log_unit_debug_errno(u, state, "Failed to parse state value: %s", value);
a16e1123
LP
1474 else
1475 m->deserialized_state = state;
2c09fb81 1476
9d2f5178
LP
1477 } else if (streq(key, "result")) {
1478 MountResult f;
a16e1123 1479
9d2f5178
LP
1480 f = mount_result_from_string(value);
1481 if (f < 0)
7fb1d980 1482 log_unit_debug_errno(u, f, "Failed to parse result value: %s", value);
9d2f5178
LP
1483 else if (f != MOUNT_SUCCESS)
1484 m->result = f;
1485
1486 } else if (streq(key, "reload-result")) {
1487 MountResult f;
1488
1489 f = mount_result_from_string(value);
1490 if (f < 0)
7fb1d980 1491 log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value);
9d2f5178
LP
1492 else if (f != MOUNT_SUCCESS)
1493 m->reload_result = f;
a16e1123 1494
2c09fb81
LP
1495 } else if (streq(key, "n-retry-umount")) {
1496
1497 r = safe_atou(value, &m->n_retry_umount);
1498 if (r < 0)
7fb1d980 1499 log_unit_debug_errno(u, r, "Failed to parse n-retry-umount value: %s", value);
2c09fb81 1500
a16e1123 1501 } else if (streq(key, "control-pid")) {
a16e1123 1502
360f384f 1503 pidref_done(&m->control_pid);
2a7451dc 1504 (void) deserialize_pidref(fds, value, &m->control_pid);
3f459cd9 1505
a16e1123
LP
1506 } else if (streq(key, "control-command")) {
1507 MountExecCommand id;
1508
f2341e0a
LP
1509 id = mount_exec_command_from_string(value);
1510 if (id < 0)
7fb1d980 1511 log_unit_debug_errno(u, id, "Failed to parse exec-command value: %s", value);
a16e1123
LP
1512 else {
1513 m->control_command_id = id;
1514 m->control_command = m->exec_command + id;
1515 }
a16e1123 1516 } else
f2341e0a 1517 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
1518
1519 return 0;
1520}
1521
d1e8e8b5 1522static UnitActiveState mount_active_state(Unit *u) {
e537352b
LP
1523 assert(u);
1524
1525 return state_translation_table[MOUNT(u)->state];
1526}
1527
d1e8e8b5 1528static const char *mount_sub_state_to_string(Unit *u) {
10a94420
LP
1529 assert(u);
1530
a16e1123 1531 return mount_state_to_string(MOUNT(u)->state);
10a94420
LP
1532}
1533
d1e8e8b5 1534static bool mount_may_gc(Unit *u) {
701cc384
LP
1535 Mount *m = MOUNT(u);
1536
1537 assert(m);
1538
f2f725e5
ZJS
1539 if (m->from_proc_self_mountinfo)
1540 return false;
1541
1542 return true;
701cc384
LP
1543}
1544
e537352b
LP
1545static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1546 Mount *m = MOUNT(u);
9d2f5178 1547 MountResult f;
e537352b
LP
1548
1549 assert(m);
1550 assert(pid >= 0);
1551
360f384f 1552 if (pid != m->control_pid.pid)
8c47c732 1553 return;
e537352b 1554
35080486
LP
1555 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1556 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1557 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1558 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1559 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1560 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1561 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1562 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1563 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1564 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1565 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1566 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1567 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1568 * /proc/self/mountinfo changes before our mount/umount exits. */
1569 (void) mount_process_proc_self_mountinfo(u->manager);
1570
360f384f 1571 pidref_done(&m->control_pid);
e537352b 1572
1f0958f6 1573 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
9d2f5178
LP
1574 f = MOUNT_SUCCESS;
1575 else if (code == CLD_EXITED)
1576 f = MOUNT_FAILURE_EXIT_CODE;
1577 else if (code == CLD_KILLED)
1578 f = MOUNT_FAILURE_SIGNAL;
1579 else if (code == CLD_DUMPED)
1580 f = MOUNT_FAILURE_CORE_DUMP;
1581 else
04499a70 1582 assert_not_reached();
9d2f5178 1583
850b7410
LP
1584 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1585 mount_set_reload_result(m, f);
8e94bb62
MY
1586 else if (m->result == MOUNT_SUCCESS && !IN_SET(m->state, MOUNT_MOUNTING, MOUNT_UNMOUNTING))
1587 /* MOUNT_MOUNTING and MOUNT_UNMOUNTING states need to be patched, see below. */
9d2f5178 1588 m->result = f;
8c47c732 1589
a16e1123 1590 if (m->control_command) {
6ea832a2 1591 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
9d2f5178 1592
a16e1123
LP
1593 m->control_command = NULL;
1594 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1595 }
1596
91bbd9b7 1597 unit_log_process_exit(
5cc2cd1c 1598 u,
91bbd9b7
LP
1599 "Mount process",
1600 mount_exec_command_to_string(m->control_command_id),
5cc2cd1c 1601 f == MOUNT_SUCCESS,
91bbd9b7 1602 code, status);
e537352b 1603
006aabbd
AJ
1604 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1605 * before we process the SIGCHLD for the mount command. */
e537352b
LP
1606
1607 switch (m->state) {
1608
1609 case MOUNT_MOUNTING:
8e94bb62 1610 /* Our mount point has not appeared in mountinfo. Something went wrong. */
e537352b 1611
006aabbd 1612 if (f == MOUNT_SUCCESS) {
8e94bb62
MY
1613 /* Either /bin/mount has an unexpected definition of success, or someone raced us
1614 * and we lost. */
006aabbd
AJ
1615 log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
1616 f = MOUNT_FAILURE_PROTOCOL;
1617 }
1618 mount_enter_dead(m, f);
1619 break;
1620
1621 case MOUNT_MOUNTING_DONE:
1622 mount_enter_mounted(m, f);
e537352b
LP
1623 break;
1624
e2f3b44c 1625 case MOUNT_REMOUNTING:
e2f3b44c 1626 case MOUNT_REMOUNTING_SIGTERM:
22af0e58
LP
1627 case MOUNT_REMOUNTING_SIGKILL:
1628 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e2f3b44c
LP
1629 break;
1630
e537352b 1631 case MOUNT_UNMOUNTING:
3cc96856 1632 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
22af0e58 1633 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
57018361
AJ
1634 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1635 * but we will stop as soon as any one umount times out. */
22af0e58
LP
1636
1637 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1638 log_unit_debug(u, "Mount still present, trying again.");
1639 m->n_retry_umount++;
1640 mount_enter_unmounting(m);
1641 } else {
006aabbd 1642 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
22af0e58
LP
1643 mount_enter_mounted(m, f);
1644 }
8e94bb62
MY
1645 } else if (f == MOUNT_FAILURE_EXIT_CODE && !m->from_proc_self_mountinfo) {
1646 /* Hmm, umount process spawned by us failed, but the mount disappeared anyway?
1647 * Maybe someone else is trying to unmount at the same time. */
1648 log_unit_notice(u, "Mount disappeared even though umount process failed, continuing.");
1649 mount_enter_dead(m, MOUNT_SUCCESS);
22af0e58 1650 } else
3cc96856 1651 mount_enter_dead_or_mounted(m, f);
22af0e58 1652
e537352b 1653 break;
57018361 1654
57018361 1655 case MOUNT_UNMOUNTING_SIGTERM:
8e94bb62 1656 case MOUNT_UNMOUNTING_SIGKILL:
57018361
AJ
1657 mount_enter_dead_or_mounted(m, f);
1658 break;
e537352b 1659
17e9d53d
YW
1660 case MOUNT_CLEANING:
1661 if (m->clean_result == MOUNT_SUCCESS)
1662 m->clean_result = f;
1663
1664 mount_enter_dead(m, MOUNT_SUCCESS);
1665 break;
1666
e537352b 1667 default:
04499a70 1668 assert_not_reached();
e537352b 1669 }
c4e2ceae
LP
1670
1671 /* Notify clients about changed exit status */
1672 unit_add_to_dbus_queue(u);
e537352b
LP
1673}
1674
718db961
LP
1675static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1676 Mount *m = MOUNT(userdata);
e537352b
LP
1677
1678 assert(m);
718db961 1679 assert(m->timer_event_source == source);
e537352b
LP
1680
1681 switch (m->state) {
1682
1683 case MOUNT_MOUNTING:
1684 case MOUNT_MOUNTING_DONE:
22af0e58
LP
1685 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1686 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1687 break;
1688
1689 case MOUNT_REMOUNTING:
79aafbd1 1690 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
22af0e58
LP
1691 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1692 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
e537352b
LP
1693 break;
1694
22af0e58
LP
1695 case MOUNT_REMOUNTING_SIGTERM:
1696 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
e537352b 1697
4819ff03 1698 if (m->kill_context.send_sigkill) {
22af0e58
LP
1699 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1700 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
ba035df2 1701 } else {
22af0e58
LP
1702 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1703 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
ba035df2 1704 }
e537352b
LP
1705 break;
1706
22af0e58
LP
1707 case MOUNT_REMOUNTING_SIGKILL:
1708 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1709
22af0e58
LP
1710 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1711 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1712 break;
1713
1714 case MOUNT_UNMOUNTING:
79aafbd1 1715 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
22af0e58 1716 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1717 break;
1718
1719 case MOUNT_UNMOUNTING_SIGTERM:
4819ff03 1720 if (m->kill_context.send_sigkill) {
22af0e58 1721 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
9d2f5178 1722 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
ba035df2 1723 } else {
22af0e58
LP
1724 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1725 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1726 }
e537352b
LP
1727 break;
1728
e537352b 1729 case MOUNT_UNMOUNTING_SIGKILL:
22af0e58
LP
1730 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1731 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1732 break;
1733
17e9d53d
YW
1734 case MOUNT_CLEANING:
1735 log_unit_warning(UNIT(m), "Cleaning timed out. killing.");
1736
1737 if (m->clean_result == MOUNT_SUCCESS)
1738 m->clean_result = MOUNT_FAILURE_TIMEOUT;
1739
1740 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, 0);
1741 break;
1742
e537352b 1743 default:
04499a70 1744 assert_not_reached();
e537352b 1745 }
718db961
LP
1746
1747 return 0;
e537352b
LP
1748}
1749
03b8cfed 1750static int mount_setup_new_unit(
839ee058
LP
1751 Manager *m,
1752 const char *name,
03b8cfed
FB
1753 const char *what,
1754 const char *where,
1755 const char *options,
1756 const char *fstype,
ec88d1ea 1757 MountProcFlags *ret_flags,
839ee058 1758 Unit **ret) {
03b8cfed 1759
839ee058 1760 _cleanup_(unit_freep) Unit *u = NULL;
bbee24bc 1761 int r;
03b8cfed 1762
839ee058
LP
1763 assert(m);
1764 assert(name);
ec88d1ea 1765 assert(ret_flags);
839ee058
LP
1766 assert(ret);
1767
1768 r = unit_new_for_name(m, sizeof(Mount), name, &u);
1769 if (r < 0)
1770 return r;
03b8cfed 1771
e10fe042
LP
1772 r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
1773 if (r < 0)
1774 return r;
03b8cfed 1775
e10fe042
LP
1776 r = free_and_strdup(&MOUNT(u)->where, where);
1777 if (r < 0)
1778 return r;
03b8cfed 1779
9ddaa3e4 1780 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
bbee24bc
LP
1781 if (r < 0)
1782 return r;
03b8cfed 1783
09f4d843
ZJS
1784 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the
1785 * time we load the unit file for it (and thus add in extra deps right after) we know what source to
1786 * attributes the deps to. */
6d7e89b0
LP
1787 MOUNT(u)->from_proc_self_mountinfo = true;
1788
b9e6d23d
YW
1789 r = mount_add_non_exec_dependencies(MOUNT(u));
1790 if (r < 0)
1791 return r;
1792
09f4d843
ZJS
1793 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything
1794 * else is loaded in now. */
cfcd4318 1795 unit_add_to_load_queue(u);
26e35b16 1796
ec88d1ea 1797 *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
839ee058 1798 *ret = TAKE_PTR(u);
03b8cfed
FB
1799 return 0;
1800}
1801
1802static int mount_setup_existing_unit(
1803 Unit *u,
1804 const char *what,
1805 const char *where,
1806 const char *options,
1807 const char *fstype,
ec88d1ea 1808 MountProcFlags *ret_flags) {
03b8cfed 1809
bbee24bc 1810 int r;
03b8cfed
FB
1811
1812 assert(u);
4bb68f2f 1813 assert(ret_flags);
03b8cfed
FB
1814
1815 if (!MOUNT(u)->where) {
1816 MOUNT(u)->where = strdup(where);
1817 if (!MOUNT(u)->where)
1818 return -ENOMEM;
1819 }
1820
4bb68f2f
LP
1821 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1822 * for the current unit. Note that the flags field is reset on each iteration of reading
1823 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1824 * iteration and thus worthy of taking into account. */
1825 MountProcFlags flags =
1826 MOUNT(u)->proc_flags | MOUNT_PROC_IS_MOUNTED;
1827
9ddaa3e4 1828 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
bbee24bc
LP
1829 if (r < 0)
1830 return r;
ec88d1ea
LP
1831 if (r > 0)
1832 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed 1833
4bb68f2f
LP
1834 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1835 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1836 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1837 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1838 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1839 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1840 * actually seeing it established for the first time. */
1841 if (!MOUNT(u)->from_proc_self_mountinfo || MOUNT(u)->state == MOUNT_MOUNTING)
ec88d1ea 1842 flags |= MOUNT_PROC_JUST_MOUNTED;
d253a45e
YW
1843
1844 MOUNT(u)->from_proc_self_mountinfo = true;
03b8cfed 1845
f8064c4f
LP
1846 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
1847 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1848 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
03b8cfed
FB
1849 u->load_state = UNIT_LOADED;
1850 u->load_error = 0;
1851
ec88d1ea 1852 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed
FB
1853 }
1854
ec88d1ea 1855 if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
a3742204
LP
1856 /* If things changed, then make sure that all deps are regenerated. Let's
1857 * first remove all automatic deps, and then add in the new ones. */
bf7eedbf 1858 r = mount_add_non_exec_dependencies(MOUNT(u));
a3742204
LP
1859 if (r < 0)
1860 return r;
1861 }
03b8cfed 1862
ec88d1ea 1863 *ret_flags = flags;
03b8cfed
FB
1864 return 0;
1865}
1866
628c89cc 1867static int mount_setup_unit(
e537352b
LP
1868 Manager *m,
1869 const char *what,
1870 const char *where,
1871 const char *options,
1872 const char *fstype,
e537352b 1873 bool set_flags) {
057d9ab8 1874
03b8cfed 1875 _cleanup_free_ char *e = NULL;
ec88d1ea 1876 MountProcFlags flags;
057d9ab8
LP
1877 Unit *u;
1878 int r;
b08d03ff 1879
f50e0a01 1880 assert(m);
b08d03ff
LP
1881 assert(what);
1882 assert(where);
e537352b
LP
1883 assert(options);
1884 assert(fstype);
1885
e537352b
LP
1886 /* Ignore API mount points. They should never be referenced in
1887 * dependencies ever. */
33ff02c9 1888 if (mount_point_is_api(where) || mount_point_ignore(where))
57f2a956 1889 return 0;
b08d03ff 1890
8d567588
LP
1891 if (streq(fstype, "autofs"))
1892 return 0;
1893
4e85aff4
LP
1894 /* probably some kind of swap, ignore */
1895 if (!is_path(where))
b08d03ff
LP
1896 return 0;
1897
7410616c 1898 r = unit_name_from_path(where, ".mount", &e);
03e52e8c 1899 if (r < 0)
2c905207 1900 return log_struct_errno(
03e52e8c 1901 LOG_WARNING, r,
2c905207
LP
1902 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR,
1903 "MOUNT_POINT=%s", where,
03e52e8c
YW
1904 LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m",
1905 where));
b08d03ff 1906
7d17cfbc 1907 u = manager_get_unit(m, e);
839ee058 1908 if (u)
03b8cfed 1909 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
839ee058 1910 else
09f4d843
ZJS
1911 /* First time we see this mount point meaning that it's not been initiated by a mount unit
1912 * but rather by the sysadmin having called mount(8) directly. */
839ee058 1913 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
03b8cfed 1914 if (r < 0)
2c905207 1915 return log_warning_errno(r, "Failed to set up mount unit for '%s': %m", where);
ff5f34d0 1916
ec88d1ea
LP
1917 /* If the mount changed properties or state, let's notify our clients */
1918 if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
ff5f34d0 1919 unit_add_to_dbus_queue(u);
c1e1601e 1920
ec88d1ea
LP
1921 if (set_flags)
1922 MOUNT(u)->proc_flags = flags;
1923
b08d03ff 1924 return 0;
b08d03ff
LP
1925}
1926
ef734fd6 1927static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
13dcfe46
ZJS
1928 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1929 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
ba0d56f5 1930 int r;
b08d03ff
LP
1931
1932 assert(m);
1933
e2857b3d 1934 r = libmount_parse(NULL, NULL, &table, &iter);
5cca8def 1935 if (r < 0)
628c89cc 1936 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
e537352b 1937
5cca8def 1938 for (;;) {
95b862b0 1939 struct libmnt_fs *fs;
8d3ae2bd 1940 const char *device, *path, *options, *fstype;
b08d03ff 1941
13dcfe46
ZJS
1942 r = mnt_table_next_fs(table, iter, &fs);
1943 if (r == 1)
5cca8def 1944 break;
13dcfe46
ZJS
1945 if (r < 0)
1946 return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
5cca8def 1947
8d3ae2bd
CL
1948 device = mnt_fs_get_source(fs);
1949 path = mnt_fs_get_target(fs);
1950 options = mnt_fs_get_options(fs);
1951 fstype = mnt_fs_get_fstype(fs);
a2e0f3d3 1952
c0a7f8d3
DM
1953 if (!device || !path)
1954 continue;
1955
9d1b2b22 1956 device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
628c89cc 1957
9d1b2b22 1958 (void) mount_setup_unit(m, device, path, options, fstype, set_flags);
b08d03ff
LP
1959 }
1960
ba0d56f5 1961 return 0;
e537352b
LP
1962}
1963
1964static void mount_shutdown(Manager *m) {
1965 assert(m);
1966
5dcadb4c 1967 m->mount_event_source = sd_event_source_disable_unref(m->mount_event_source);
718db961 1968
d379d442
KZ
1969 mnt_unref_monitor(m->mount_monitor);
1970 m->mount_monitor = NULL;
b08d03ff
LP
1971}
1972
7a7821c8 1973static int mount_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1974 Mount *m = MOUNT(u);
7a7821c8 1975 usec_t t;
68db7a3b
ZJS
1976 int r;
1977
c73f413d
FS
1978 assert(m);
1979 assert(u);
1980
68db7a3b
ZJS
1981 if (!m->timer_event_source)
1982 return 0;
1983
7a7821c8 1984 r = sd_event_source_get_time(m->timer_event_source, &t);
68db7a3b
ZJS
1985 if (r < 0)
1986 return r;
7a7821c8
LP
1987 if (t == USEC_INFINITY)
1988 return 0;
68db7a3b 1989
7a7821c8 1990 *timeout = t;
68db7a3b
ZJS
1991 return 1;
1992}
1993
04eb582a 1994static void mount_enumerate_perpetual(Manager *m) {
11222d0f
LP
1995 Unit *u;
1996 int r;
1997
1998 assert(m);
1999
2000 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
2001 * unconditionally synthesize it here and mark it as perpetual. */
2002
2003 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
2004 if (!u) {
a581e45a 2005 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
04eb582a
LP
2006 if (r < 0) {
2007 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
2008 return;
2009 }
11222d0f
LP
2010 }
2011
2012 u->perpetual = true;
2013 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
2014
2015 unit_add_to_load_queue(u);
2016 unit_add_to_dbus_queue(u);
2017}
2018
2019static bool mount_is_mounted(Mount *m) {
2020 assert(m);
2021
ec88d1ea 2022 return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
11222d0f
LP
2023}
2024
edc027b4 2025static int mount_on_ratelimit_expire(sd_event_source *s, void *userdata) {
99534007 2026 Manager *m = ASSERT_PTR(userdata);
c29e6a95 2027 Job *j;
edc027b4 2028
c29e6a95
MS
2029 /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */
2030 HASHMAP_FOREACH(j, m->jobs) {
2031 if (j->unit->type != UNIT_MOUNT)
2032 continue;
2033
2034 job_add_to_run_queue(j);
2035 }
2036
b0c4b282
LP
2037 /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
2038 * let's make sure we dispatch them in the next iteration. */
2039 manager_trigger_run_queue(m);
edc027b4
MS
2040
2041 return 0;
2042}
2043
ba64af90 2044static void mount_enumerate(Manager *m) {
b08d03ff 2045 int r;
d379d442 2046
b08d03ff
LP
2047 assert(m);
2048
8d3ae2bd
CL
2049 mnt_init_debug(0);
2050
d379d442 2051 if (!m->mount_monitor) {
24a4542c 2052 unsigned mount_rate_limit_burst = 5;
d379d442 2053 int fd;
ef734fd6 2054
d379d442
KZ
2055 m->mount_monitor = mnt_new_monitor();
2056 if (!m->mount_monitor) {
ba64af90 2057 log_oom();
718db961 2058 goto fail;
d379d442 2059 }
29083707 2060
d379d442 2061 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
ba64af90
LP
2062 if (r < 0) {
2063 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
29083707 2064 goto fail;
ba64af90
LP
2065 }
2066
d379d442 2067 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
ba64af90
LP
2068 if (r < 0) {
2069 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
f7c1ad4f 2070 goto fail;
ba64af90 2071 }
90598531 2072
d379d442
KZ
2073 /* mnt_unref_monitor() will close the fd */
2074 fd = r = mnt_monitor_get_fd(m->mount_monitor);
ba64af90
LP
2075 if (r < 0) {
2076 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
f7c1ad4f 2077 goto fail;
ba64af90 2078 }
befb6d54 2079
d379d442 2080 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
ba64af90
LP
2081 if (r < 0) {
2082 log_error_errno(r, "Failed to watch mount file descriptor: %m");
befb6d54 2083 goto fail;
ba64af90 2084 }
befb6d54 2085
d42b61d2 2086 r = sd_event_source_set_priority(m->mount_event_source, EVENT_PRIORITY_MOUNT_TABLE);
ba64af90
LP
2087 if (r < 0) {
2088 log_error_errno(r, "Failed to adjust mount watch priority: %m");
befb6d54 2089 goto fail;
ba64af90 2090 }
7dfbe2e3 2091
24a4542c
LB
2092 /* Let users override the default (5 in 1s), as it stalls the boot sequence on busy systems. */
2093 const char *e = secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST");
2094 if (e) {
2095 r = safe_atou(e, &mount_rate_limit_burst);
2096 if (r < 0)
2097 log_debug("Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST, ignoring: %s", e);
2098 }
2099
2100 r = sd_event_source_set_ratelimit(m->mount_event_source, 1 * USEC_PER_SEC, mount_rate_limit_burst);
d586f642
MS
2101 if (r < 0) {
2102 log_error_errno(r, "Failed to enable rate limit for mount events: %m");
2103 goto fail;
2104 }
2105
edc027b4
MS
2106 r = sd_event_source_set_ratelimit_expire_callback(m->mount_event_source, mount_on_ratelimit_expire);
2107 if (r < 0) {
2108 log_error_errno(r, "Failed to enable rate limit for mount events: %m");
2109 goto fail;
2110 }
2111
d379d442 2112 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
befb6d54
CL
2113 }
2114
e62d8c39
ZJS
2115 r = mount_load_proc_self_mountinfo(m, false);
2116 if (r < 0)
b08d03ff
LP
2117 goto fail;
2118
ba64af90 2119 return;
b08d03ff
LP
2120
2121fail:
2122 mount_shutdown(m);
5cb5a6ff
LP
2123}
2124
fcd8e119
LP
2125static int drain_libmount(Manager *m) {
2126 bool rescan = false;
2127 int r;
2128
2129 assert(m);
2130
2131 /* Drain all events and verify that the event is valid.
2132 *
2133 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
2134 * may generate event which is irrelevant for us.
2135 *
2136 * error: r < 0; valid: r == 0, false positive: r == 1 */
2137 do {
2138 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
2139 if (r < 0)
2140 return log_error_errno(r, "Failed to drain libmount events: %m");
2141 if (r == 0)
2142 rescan = true;
2143 } while (r == 0);
2144
2145 return rescan;
2146}
2147
35080486 2148static int mount_process_proc_self_mountinfo(Manager *m) {
6688b72d 2149 _cleanup_set_free_ Set *around = NULL, *gone = NULL;
ec8126d7 2150 const char *what;
ef734fd6
LP
2151 int r;
2152
2153 assert(m);
ef734fd6 2154
fcd8e119
LP
2155 r = drain_libmount(m);
2156 if (r <= 0)
2157 return r;
ef734fd6 2158
4f0eedb7
ZJS
2159 r = mount_load_proc_self_mountinfo(m, true);
2160 if (r < 0) {
e537352b 2161 /* Reset flags, just in case, for later calls */
ec88d1ea
LP
2162 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
2163 MOUNT(u)->proc_flags = 0;
e537352b 2164
ec8126d7 2165 return 0;
ef734fd6
LP
2166 }
2167
2168 manager_dispatch_load_queue(m);
2169
595ed347
MS
2170 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
2171 Mount *mount = MOUNT(u);
ef734fd6 2172
1e122561
YW
2173 mount->invalidated_state = false;
2174
11222d0f 2175 if (!mount_is_mounted(mount)) {
e537352b 2176
1483892a 2177 /* A mount point is not around right now. It might be gone, or might never have
394763f6
LP
2178 * existed. */
2179
2180 if (mount->from_proc_self_mountinfo &&
6688b72d 2181 mount->parameters_proc_self_mountinfo.what)
394763f6 2182 /* Remember that this device might just have disappeared */
6688b72d 2183 if (set_put_strdup_full(&gone, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 2184 log_oom(); /* we don't care too much about OOM here... */
fcd8b266 2185
ef734fd6 2186 mount->from_proc_self_mountinfo = false;
9ddaa3e4 2187 assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0);
e537352b
LP
2188
2189 switch (mount->state) {
2190
2191 case MOUNT_MOUNTED:
7eba1463 2192 /* This has just been unmounted by somebody else, follow the state change. */
9d2f5178 2193 mount_enter_dead(mount, MOUNT_SUCCESS);
e537352b
LP
2194 break;
2195
2fa0bd7d
YW
2196 case MOUNT_MOUNTING_DONE:
2197 /* The mount command may add the corresponding proc mountinfo entry and
2198 * then remove it because of an internal error. E.g., fuse.sshfs seems
2199 * to do that when the connection fails. See #17617. To handle such the
2200 * case, let's once set the state back to mounting. Then, the unit can
8e94bb62 2201 * correctly enter the failed state later in mount_sigchld_event(). */
2fa0bd7d
YW
2202 mount_set_state(mount, MOUNT_MOUNTING);
2203 break;
2204
e537352b 2205 default:
e537352b 2206 break;
e537352b
LP
2207 }
2208
ec88d1ea 2209 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
e537352b 2210
fcd8b266 2211 /* A mount point was added or changed */
e537352b
LP
2212
2213 switch (mount->state) {
2214
2215 case MOUNT_DEAD:
fdf20a31 2216 case MOUNT_FAILED:
4b58153d
LP
2217
2218 /* This has just been mounted by somebody else, follow the state change, but let's
2219 * generate a new invocation ID for this implicitly and automatically. */
7eba1463
LP
2220 (void) unit_acquire_invocation_id(u);
2221 mount_cycle_clear(mount);
9d2f5178 2222 mount_enter_mounted(mount, MOUNT_SUCCESS);
e537352b
LP
2223 break;
2224
2225 case MOUNT_MOUNTING:
5bcb0f2b 2226 mount_set_state(mount, MOUNT_MOUNTING_DONE);
e537352b
LP
2227 break;
2228
2229 default:
1483892a
LP
2230 /* Nothing really changed, but let's issue an notification call nonetheless,
2231 * in case somebody is waiting for this. (e.g. file system ro/rw
2232 * remounts.) */
e537352b
LP
2233 mount_set_state(mount, mount->state);
2234 break;
2235 }
394763f6 2236 }
fcd8b266 2237
11222d0f 2238 if (mount_is_mounted(mount) &&
394763f6 2239 mount->from_proc_self_mountinfo &&
6688b72d 2240 mount->parameters_proc_self_mountinfo.what)
b6418dc9 2241 /* Track devices currently used */
6688b72d 2242 if (set_put_strdup_full(&around, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 2243 log_oom();
e537352b
LP
2244
2245 /* Reset the flags for later calls */
ec88d1ea 2246 mount->proc_flags = 0;
e537352b 2247 }
718db961 2248
90e74a66 2249 SET_FOREACH(what, gone) {
fcd8b266
LP
2250 if (set_contains(around, what))
2251 continue;
2252
2253 /* Let the device units know that the device is no longer mounted */
b1ba0ce8 2254 device_found_node(m, what, DEVICE_NOT_FOUND, DEVICE_FOUND_MOUNT);
fcd8b266 2255 }
ec8126d7
ZJS
2256
2257 return 0;
e537352b
LP
2258}
2259
35080486 2260static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
99534007 2261 Manager *m = ASSERT_PTR(userdata);
35080486 2262
35080486
LP
2263 assert(revents & EPOLLIN);
2264
2265 return mount_process_proc_self_mountinfo(m);
2266}
2267
1e122561
YW
2268int mount_invalidate_state_by_path(Manager *manager, const char *path) {
2269 _cleanup_free_ char *name = NULL;
2270 Unit *u;
2271 int r;
2272
2273 assert(manager);
2274 assert(path);
2275
2276 r = unit_name_from_path(path, ".mount", &name);
2277 if (r < 0)
2278 return log_debug_errno(r, "Failed to generate unit name from path \"%s\", ignoring: %m", path);
2279
2280 u = manager_get_unit(manager, name);
2281 if (!u)
2282 return -ENOENT;
2283
2284 MOUNT(u)->invalidated_state = true;
2285 return 0;
2286}
2287
fdf20a31 2288static void mount_reset_failed(Unit *u) {
5632e374
LP
2289 Mount *m = MOUNT(u);
2290
2291 assert(m);
2292
fdf20a31 2293 if (m->state == MOUNT_FAILED)
5632e374
LP
2294 mount_set_state(m, MOUNT_DEAD);
2295
9d2f5178
LP
2296 m->result = MOUNT_SUCCESS;
2297 m->reload_result = MOUNT_SUCCESS;
17e9d53d 2298 m->clean_result = MOUNT_SUCCESS;
5632e374
LP
2299}
2300
37eb258e
LP
2301static PidRef* mount_control_pid(Unit *u) {
2302 return &ASSERT_PTR(MOUNT(u))->control_pid;
291d565a
LP
2303}
2304
17e9d53d
YW
2305static int mount_clean(Unit *u, ExecCleanMask mask) {
2306 _cleanup_strv_free_ char **l = NULL;
2307 Mount *m = MOUNT(u);
2308 int r;
2309
2310 assert(m);
2311 assert(mask != 0);
2312
2313 if (m->state != MOUNT_DEAD)
2314 return -EBUSY;
2315
2316 r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l);
2317 if (r < 0)
2318 return r;
2319
2320 if (strv_isempty(l))
2321 return -EUNATCH;
2322
2323 mount_unwatch_control_pid(m);
2324 m->clean_result = MOUNT_SUCCESS;
2325 m->control_command = NULL;
2326 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
2327
e9276800 2328 r = mount_arm_timer(m, /* relative= */ true, m->exec_context.timeout_clean_usec);
d30eb0b5
LP
2329 if (r < 0) {
2330 log_unit_warning_errno(u, r, "Failed to install timer: %m");
17e9d53d 2331 goto fail;
d30eb0b5 2332 }
17e9d53d 2333
4775b55d 2334 r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid);
d30eb0b5
LP
2335 if (r < 0) {
2336 log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m");
360f384f 2337 goto fail;
d30eb0b5 2338 }
17e9d53d 2339
360f384f 2340 mount_set_state(m, MOUNT_CLEANING);
17e9d53d
YW
2341 return 0;
2342
2343fail:
17e9d53d 2344 m->clean_result = MOUNT_FAILURE_RESOURCES;
5dcadb4c 2345 m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
17e9d53d
YW
2346 return r;
2347}
2348
2349static int mount_can_clean(Unit *u, ExecCleanMask *ret) {
2350 Mount *m = MOUNT(u);
2351
2352 assert(m);
2353
2354 return exec_context_get_clean_mask(&m->exec_context, ret);
2355}
2356
705578c3 2357static int mount_can_start(Unit *u) {
9727f242
DDM
2358 Mount *m = MOUNT(u);
2359 int r;
2360
2361 assert(m);
2362
2363 r = unit_test_start_limit(u);
2364 if (r < 0) {
2365 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
2366 return r;
2367 }
2368
705578c3 2369 return 1;
9727f242
DDM
2370}
2371
47261967
LP
2372static int mount_subsystem_ratelimited(Manager *m) {
2373 assert(m);
2374
2375 if (!m->mount_event_source)
2376 return false;
2377
2378 return sd_event_source_is_ratelimited(m->mount_event_source);
2379}
2380
8dbab37d
DDM
2381char* mount_get_what_escaped(const Mount *m) {
2382 _cleanup_free_ char *escaped = NULL;
2383 const char *s = NULL;
2384
2385 assert(m);
2386
2387 if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
2388 s = m->parameters_proc_self_mountinfo.what;
2389 else if (m->from_fragment && m->parameters_fragment.what)
2390 s = m->parameters_fragment.what;
2391
2392 if (s) {
2393 escaped = utf8_escape_invalid(s);
2394 if (!escaped)
2395 return NULL;
2396 }
2397
2398 return escaped ? TAKE_PTR(escaped) : strdup("");
2399}
2400
2401char* mount_get_options_escaped(const Mount *m) {
2402 _cleanup_free_ char *escaped = NULL;
2403 const char *s = NULL;
2404
2405 assert(m);
2406
2407 if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options)
2408 s = m->parameters_proc_self_mountinfo.options;
2409 else if (m->from_fragment && m->parameters_fragment.options)
2410 s = m->parameters_fragment.options;
2411
2412 if (s) {
2413 escaped = utf8_escape_invalid(s);
2414 if (!escaped)
2415 return NULL;
2416 }
2417
2418 return escaped ? TAKE_PTR(escaped) : strdup("");
2419}
2420
2421const char* mount_get_fstype(const Mount *m) {
2422 assert(m);
2423
2424 if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype)
2425 return m->parameters_proc_self_mountinfo.fstype;
2426
2427 if (m->from_fragment && m->parameters_fragment.fstype)
2428 return m->parameters_fragment.fstype;
2429
2430 return NULL;
2431}
2432
a16e1123 2433static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
48d83e33 2434 [MOUNT_EXEC_MOUNT] = "ExecMount",
a16e1123
LP
2435 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
2436 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
2437};
2438
2439DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
2440
9d2f5178 2441static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
48d83e33
ZJS
2442 [MOUNT_SUCCESS] = "success",
2443 [MOUNT_FAILURE_RESOURCES] = "resources",
2444 [MOUNT_FAILURE_TIMEOUT] = "timeout",
2445 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
2446 [MOUNT_FAILURE_SIGNAL] = "signal",
2447 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
07299350 2448 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
48d83e33 2449 [MOUNT_FAILURE_PROTOCOL] = "protocol",
9d2f5178
LP
2450};
2451
2452DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
2453
87f0e418 2454const UnitVTable mount_vtable = {
7d17cfbc 2455 .object_size = sizeof(Mount),
718db961
LP
2456 .exec_context_offset = offsetof(Mount, exec_context),
2457 .cgroup_context_offset = offsetof(Mount, cgroup_context),
2458 .kill_context_offset = offsetof(Mount, kill_context),
613b411c 2459 .exec_runtime_offset = offsetof(Mount, exec_runtime),
9cc54544 2460 .cgroup_runtime_offset = offsetof(Mount, cgroup_runtime),
3ef63c31 2461
f975e971
LP
2462 .sections =
2463 "Unit\0"
2464 "Mount\0"
2465 "Install\0",
4ad49000 2466 .private_section = "Mount",
71645aca 2467
c80a9a33
LP
2468 .can_transient = true,
2469 .can_fail = true,
755021d4 2470 .exclude_from_switch_root_serialization = true,
c80a9a33 2471
e537352b
LP
2472 .init = mount_init,
2473 .load = mount_load,
034c6ed7 2474 .done = mount_done,
e537352b 2475
f50e0a01 2476 .coldplug = mount_coldplug,
01400460 2477 .catchup = mount_catchup,
f50e0a01 2478
034c6ed7 2479 .dump = mount_dump,
5cb5a6ff 2480
e537352b
LP
2481 .start = mount_start,
2482 .stop = mount_stop,
2483 .reload = mount_reload,
2484
17e9d53d
YW
2485 .clean = mount_clean,
2486 .can_clean = mount_can_clean,
8a0867d6 2487
a16e1123
LP
2488 .serialize = mount_serialize,
2489 .deserialize_item = mount_deserialize_item,
2490
f50e0a01 2491 .active_state = mount_active_state,
10a94420 2492 .sub_state_to_string = mount_sub_state_to_string,
b08d03ff 2493
52a12341
YW
2494 .will_restart = unit_will_restart_default,
2495
f2f725e5 2496 .may_gc = mount_may_gc,
39c79477 2497 .is_extrinsic = mount_is_extrinsic,
701cc384 2498
e537352b 2499 .sigchld_event = mount_sigchld_event,
e537352b 2500
fdf20a31 2501 .reset_failed = mount_reset_failed,
291d565a
LP
2502
2503 .control_pid = mount_control_pid,
5632e374 2504
74c964d3
LP
2505 .bus_set_property = bus_mount_set_property,
2506 .bus_commit_properties = bus_mount_commit_properties,
4139c1b2 2507
68db7a3b
ZJS
2508 .get_timeout = mount_get_timeout,
2509
04eb582a 2510 .enumerate_perpetual = mount_enumerate_perpetual,
f50e0a01 2511 .enumerate = mount_enumerate,
c6918296 2512 .shutdown = mount_shutdown,
47261967 2513 .subsystem_ratelimited = mount_subsystem_ratelimited,
c6918296
MS
2514
2515 .status_message_formats = {
2516 .starting_stopping = {
2517 [0] = "Mounting %s...",
2518 [1] = "Unmounting %s...",
2519 },
2520 .finished_start_job = {
2521 [JOB_DONE] = "Mounted %s.",
2522 [JOB_FAILED] = "Failed to mount %s.",
c6918296
MS
2523 [JOB_TIMEOUT] = "Timed out mounting %s.",
2524 },
2525 .finished_stop_job = {
2526 [JOB_DONE] = "Unmounted %s.",
2527 [JOB_FAILED] = "Failed unmounting %s.",
2528 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2529 },
2530 },
9727f242 2531
705578c3 2532 .can_start = mount_can_start,
b2bfd121
LP
2533
2534 .notify_plymouth = true,
5cb5a6ff 2535};