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