]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/umount.c
umount: umount, until all umounts failed
[thirdparty/systemd.git] / src / umount.c
CommitLineData
e3478379
FF
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 ProFUSION embedded systems
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <fcntl.h>
24#include <string.h>
25#include <sys/mount.h>
26#include <sys/swap.h>
27#include <unistd.h>
28#include <linux/loop.h>
d48141ba 29#include <linux/dm-ioctl.h>
e3478379
FF
30#include <libudev.h>
31
32#include "list.h"
33#include "mount-setup.h"
34#include "umount.h"
35#include "util.h"
36
37typedef struct MountPoint {
38 char *path;
2d9a3397 39 dev_t devnum;
f3accc08 40 bool skip_ro;
e3478379
FF
41 LIST_FIELDS (struct MountPoint, mount_point);
42} MountPoint;
43
12aad1d0
LP
44static void mount_point_free(MountPoint **head, MountPoint *m) {
45 assert(head);
46 assert(m);
e3478379 47
12aad1d0
LP
48 LIST_REMOVE(MountPoint, mount_point, *head, m);
49
50 free(m->path);
51 free(m);
e3478379
FF
52}
53
12aad1d0
LP
54static void mount_points_list_free(MountPoint **head) {
55 assert(head);
56
57 while (*head)
58 mount_point_free(head, *head);
e3478379
FF
59}
60
12aad1d0 61static int mount_points_list_get(MountPoint **head) {
e3478379
FF
62 FILE *proc_self_mountinfo;
63 char *path, *p;
64 unsigned int i;
65 int r;
66
12aad1d0
LP
67 assert(head);
68
e3478379
FF
69 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
70 return -errno;
71
72 for (i = 1;; i++) {
73 int k;
12aad1d0 74 MountPoint *m;
f3accc08
LP
75 char *root;
76 bool skip_ro;
e3478379
FF
77
78 path = p = NULL;
79
80 if ((k = fscanf(proc_self_mountinfo,
81 "%*s " /* (1) mount id */
82 "%*s " /* (2) parent id */
83 "%*s " /* (3) major:minor */
f3accc08 84 "%ms " /* (4) root */
e3478379
FF
85 "%ms " /* (5) mount point */
86 "%*s" /* (6) mount options */
87 "%*[^-]" /* (7) optional fields */
88 "- " /* (8) separator */
89 "%*s " /* (9) file system type */
90 "%*s" /* (10) mount source */
91 "%*s" /* (11) mount options 2 */
92 "%*[^\n]", /* some rubbish at the end */
f3accc08
LP
93 &root,
94 &path)) != 2) {
e3478379
FF
95 if (k == EOF)
96 break;
97
98 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
99
100 free(path);
101 continue;
102 }
103
f3accc08
LP
104 /* If we encounter a bind mount, don't try to remount
105 * the source dir too early */
d72238fc 106 skip_ro = !streq(root, "/");
f3accc08
LP
107 free(root);
108
2054a5b8
LP
109 p = cunescape(path);
110 free(path);
e3478379 111
2054a5b8 112 if (!p) {
e3478379
FF
113 r = -ENOMEM;
114 goto finish;
115 }
116
57f2a956 117 if (mount_point_is_api(p) || mount_point_ignore(p)) {
2054a5b8
LP
118 free(p);
119 continue;
120 }
121
12aad1d0 122 if (!(m = new0(MountPoint, 1))) {
2054a5b8 123 free(p);
e3478379
FF
124 r = -ENOMEM;
125 goto finish;
126 }
e3478379 127
12aad1d0 128 m->path = p;
f3accc08 129 m->skip_ro = skip_ro;
12aad1d0 130 LIST_PREPEND(MountPoint, mount_point, *head, m);
e3478379
FF
131 }
132
133 r = 0;
134
135finish:
136 fclose(proc_self_mountinfo);
137
e3478379
FF
138 return r;
139}
140
12aad1d0 141static int swap_list_get(MountPoint **head) {
e3478379
FF
142 FILE *proc_swaps;
143 unsigned int i;
144 int r;
145
12aad1d0
LP
146 assert(head);
147
e3478379 148 if (!(proc_swaps = fopen("/proc/swaps", "re")))
dee87d61 149 return (errno == ENOENT) ? 0 : -errno;
e3478379
FF
150
151 (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
152
153 for (i = 2;; i++) {
154 MountPoint *swap;
155 char *dev = NULL, *d;
156 int k;
157
158 if ((k = fscanf(proc_swaps,
159 "%ms " /* device/file */
160 "%*s " /* type of swap */
161 "%*s " /* swap size */
162 "%*s " /* used */
163 "%*s\n", /* priority */
164 &dev)) != 1) {
165
166 if (k == EOF)
167 break;
168
169 log_warning("Failed to parse /proc/swaps:%u.", i);
170
171 free(dev);
172 continue;
173 }
174
175 if (endswith(dev, "(deleted)")) {
176 free(dev);
177 continue;
178 }
179
180 d = cunescape(dev);
181 free(dev);
182
183 if (!d) {
184 r = -ENOMEM;
185 goto finish;
186 }
187
2d9a3397 188 if (!(swap = new0(MountPoint, 1))) {
e3478379
FF
189 free(d);
190 r = -ENOMEM;
191 goto finish;
192 }
193
2d9a3397 194 swap->path = d;
12aad1d0 195 LIST_PREPEND(MountPoint, mount_point, *head, swap);
e3478379
FF
196 }
197
198 r = 0;
199
200finish:
201 fclose(proc_swaps);
202
203 return r;
204}
205
12aad1d0 206static int loopback_list_get(MountPoint **head) {
e3478379
FF
207 int r;
208 struct udev *udev;
209 struct udev_enumerate *e = NULL;
210 struct udev_list_entry *item = NULL, *first = NULL;
211
12aad1d0
LP
212 assert(head);
213
e3478379
FF
214 if (!(udev = udev_new())) {
215 r = -ENOMEM;
216 goto finish;
217 }
218
219 if (!(e = udev_enumerate_new(udev))) {
220 r = -ENOMEM;
221 goto finish;
222 }
223
d48141ba
LP
224 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
225 udev_enumerate_add_match_sysname(e, "loop*") < 0) {
e3478379
FF
226 r = -EIO;
227 goto finish;
228 }
229
230 if (udev_enumerate_scan_devices(e) < 0) {
231 r = -EIO;
232 goto finish;
233 }
234
235 first = udev_enumerate_get_list_entry(e);
e3478379
FF
236 udev_list_entry_foreach(item, first) {
237 MountPoint *lb;
b854a7e7 238 struct udev_device *d;
e3478379 239 char *loop;
b854a7e7 240 const char *dn;
e3478379 241
b854a7e7 242 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
e3478379
FF
243 r = -ENOMEM;
244 goto finish;
245 }
246
2d9a3397 247 if (!(dn = udev_device_get_devnode(d))) {
b854a7e7 248 udev_device_unref(d);
2d9a3397
LP
249 continue;
250 }
b854a7e7 251
2d9a3397
LP
252 loop = strdup(dn);
253 udev_device_unref(d);
254
255 if (!loop) {
256 r = -ENOMEM;
257 goto finish;
258 }
b854a7e7 259
2d9a3397 260 if (!(lb = new0(MountPoint, 1))) {
e3478379
FF
261 free(loop);
262 r = -ENOMEM;
263 goto finish;
264 }
265
2d9a3397 266 lb->path = loop;
12aad1d0 267 LIST_PREPEND(MountPoint, mount_point, *head, lb);
e3478379
FF
268 }
269
270 r = 0;
271
272finish:
273 if (e)
274 udev_enumerate_unref(e);
275
b854a7e7
LP
276 if (udev)
277 udev_unref(udev);
278
e3478379
FF
279 return r;
280}
281
12aad1d0 282static int dm_list_get(MountPoint **head) {
d48141ba
LP
283 int r;
284 struct udev *udev;
285 struct udev_enumerate *e = NULL;
286 struct udev_list_entry *item = NULL, *first = NULL;
287
12aad1d0
LP
288 assert(head);
289
d48141ba
LP
290 if (!(udev = udev_new())) {
291 r = -ENOMEM;
292 goto finish;
293 }
294
295 if (!(e = udev_enumerate_new(udev))) {
296 r = -ENOMEM;
297 goto finish;
298 }
299
300 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
301 udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
302 r = -EIO;
303 goto finish;
304 }
305
306 if (udev_enumerate_scan_devices(e) < 0) {
307 r = -EIO;
308 goto finish;
309 }
310
311 first = udev_enumerate_get_list_entry(e);
312
313 udev_list_entry_foreach(item, first) {
2d9a3397 314 MountPoint *m;
d48141ba 315 struct udev_device *d;
2d9a3397
LP
316 dev_t devnum;
317 char *node;
318 const char *dn;
d48141ba
LP
319
320 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
321 r = -ENOMEM;
322 goto finish;
323 }
324
2d9a3397
LP
325 devnum = udev_device_get_devnum(d);
326 dn = udev_device_get_devnode(d);
d48141ba 327
2d9a3397
LP
328 if (major(devnum) == 0 || !dn) {
329 udev_device_unref(d);
330 continue;
d48141ba
LP
331 }
332
2d9a3397 333 node = strdup(dn);
d48141ba
LP
334 udev_device_unref(d);
335
2d9a3397
LP
336 if (!node) {
337 r = -ENOMEM;
338 goto finish;
339 }
d48141ba 340
2d9a3397
LP
341 if (!(m = new(MountPoint, 1))) {
342 free(node);
d48141ba
LP
343 r = -ENOMEM;
344 goto finish;
345 }
346
2d9a3397
LP
347 m->path = node;
348 m->devnum = devnum;
12aad1d0 349 LIST_PREPEND(MountPoint, mount_point, *head, m);
d48141ba
LP
350 }
351
352 r = 0;
353
354finish:
355 if (e)
356 udev_enumerate_unref(e);
357
358 if (udev)
359 udev_unref(udev);
360
361 return r;
362}
363
e3478379
FF
364static int delete_loopback(const char *device) {
365 int fd, r;
366
ce726252 367 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
c4f8bd1a 368 return errno == ENOENT ? 0 : -errno;
e3478379 369
ce726252 370 r = ioctl(fd, LOOP_CLR_FD, 0);
143b4e9b 371 close_nointr_nofail(fd);
e3478379 372
12aad1d0
LP
373 if (r >= 0)
374 return 1;
375
ce726252 376 /* ENXIO: not bound, so no error */
12aad1d0
LP
377 if (errno == ENXIO)
378 return 0;
379
380 return -errno;
e3478379
FF
381}
382
2d9a3397 383static int delete_dm(dev_t devnum) {
d48141ba
LP
384 int fd, r;
385 struct dm_ioctl dm;
386
2d9a3397 387 assert(major(devnum) != 0);
d48141ba
LP
388
389 if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0)
390 return -errno;
391
392 zero(dm);
393 dm.version[0] = DM_VERSION_MAJOR;
394 dm.version[1] = DM_VERSION_MINOR;
395 dm.version[2] = DM_VERSION_PATCHLEVEL;
396
397 dm.data_size = sizeof(dm);
2d9a3397 398 dm.dev = devnum;
d48141ba
LP
399
400 r = ioctl(fd, DM_DEV_REMOVE, &dm);
401 close_nointr_nofail(fd);
402
403 return r >= 0 ? 0 : -errno;
404}
405
12aad1d0
LP
406static int mount_points_list_umount(MountPoint **head, bool *changed) {
407 MountPoint *m, *n;
408 int n_failed = 0;
e3478379 409
12aad1d0
LP
410 assert(head);
411
412 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
9279749b
MB
413 if (streq(m->path, "/")) {
414 n_failed++;
e3478379 415 continue;
9279749b 416 }
e3478379
FF
417
418 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
12aad1d0 419 if (umount2(m->path, MNT_FORCE) == 0) {
0415a104 420 log_info("Unmounted %s.", m->path);
12aad1d0
LP
421 if (changed)
422 *changed = true;
423
424 mount_point_free(head, m);
425 } else {
426 log_warning("Could not unmount %s: %m", m->path);
427 n_failed++;
e3478379
FF
428 }
429 }
430
12aad1d0 431 return n_failed;
e3478379
FF
432}
433
12aad1d0
LP
434static int mount_points_list_remount_read_only(MountPoint **head, bool *changed) {
435 MountPoint *m, *n;
436 int n_failed = 0;
437
438 assert(head);
439
440 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
e3478379 441
f3accc08
LP
442 if (m->skip_ro) {
443 n_failed++;
444 continue;
445 }
446
e3478379 447 /* Trying to remount read-only */
12aad1d0
LP
448 if (mount(NULL, m->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0) {
449 if (changed)
450 *changed = true;
451
452 mount_point_free(head, m);
453 } else {
454 log_warning("Could not remount as read-only %s: %m", m->path);
455 n_failed++;
e3478379
FF
456 }
457 }
458
12aad1d0 459 return n_failed;
e3478379
FF
460}
461
12aad1d0
LP
462static int swap_points_list_off(MountPoint **head, bool *changed) {
463 MountPoint *m, *n;
464 int n_failed = 0;
465
466 assert(head);
e3478379 467
12aad1d0
LP
468 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
469 if (swapoff(m->path) == 0) {
470 if (changed)
471 *changed = true;
472
473 mount_point_free(head, m);
474 } else {
475 log_warning("Could not deactivate swap %s: %m", m->path);
476 n_failed++;
e3478379
FF
477 }
478 }
479
12aad1d0 480 return n_failed;
e3478379
FF
481}
482
12aad1d0
LP
483static int loopback_points_list_detach(MountPoint **head, bool *changed) {
484 MountPoint *m, *n;
7fc942b2
LP
485 int n_failed = 0, k;
486 struct stat root_st;
12aad1d0
LP
487
488 assert(head);
489
7fc942b2
LP
490 k = lstat("/", &root_st);
491
12aad1d0
LP
492 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
493 int r;
7fc942b2
LP
494 struct stat loopback_st;
495
496 if (k >= 0 &&
497 major(root_st.st_dev) != 0 &&
498 lstat(m->path, &loopback_st) >= 0 &&
499 root_st.st_dev == loopback_st.st_rdev) {
500 n_failed ++;
501 continue;
502 }
e3478379 503
12aad1d0
LP
504 if ((r = delete_loopback(m->path)) >= 0) {
505
506 if (r > 0 && changed)
507 *changed = true;
508
509 mount_point_free(head, m);
510 } else {
511 log_warning("Could not delete loopback %s: %m", m->path);
512 n_failed++;
e3478379
FF
513 }
514 }
515
12aad1d0 516 return n_failed;
e3478379
FF
517}
518
12aad1d0
LP
519static int dm_points_list_detach(MountPoint **head, bool *changed) {
520 MountPoint *m, *n;
7fc942b2
LP
521 int n_failed = 0, k;
522 struct stat root_st;
12aad1d0
LP
523
524 assert(head);
525
7fc942b2
LP
526 k = lstat("/", &root_st);
527
12aad1d0
LP
528 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
529 int r;
530
7fc942b2
LP
531 if (k >= 0 &&
532 major(root_st.st_dev) != 0 &&
533 root_st.st_dev == m->devnum) {
534 n_failed ++;
535 continue;
536 }
537
12aad1d0
LP
538 if ((r = delete_dm(m->devnum)) >= 0) {
539
540 if (r > 0 && changed)
541 *changed = true;
d48141ba 542
12aad1d0
LP
543 mount_point_free(head, m);
544 } else {
545 log_warning("Could not delete dm %s: %m", m->path);
546 n_failed++;
d48141ba
LP
547 }
548 }
549
12aad1d0 550 return n_failed;
d48141ba
LP
551}
552
12aad1d0 553int umount_all(bool *changed) {
e3478379 554 int r;
6f7f51f7
HH
555 bool umount_changed;
556
e3478379
FF
557 LIST_HEAD(MountPoint, mp_list_head);
558
559 LIST_HEAD_INIT(MountPoint, mp_list_head);
560
561 r = mount_points_list_get(&mp_list_head);
562 if (r < 0)
563 goto end;
564
6f7f51f7
HH
565 /* retry umount, until nothing can be umounted anymore */
566 do {
567 umount_changed = false;
568 r = mount_points_list_umount(&mp_list_head, &umount_changed);
569 if (umount_changed)
570 *changed = true;
571 } while(umount_changed);
e3478379
FF
572 if (r <= 0)
573 goto end;
574
12aad1d0 575 r = mount_points_list_remount_read_only(&mp_list_head, changed);
e3478379
FF
576
577 end:
578 mount_points_list_free(&mp_list_head);
579
580 return r;
581}
582
12aad1d0 583int swapoff_all(bool *changed) {
e3478379
FF
584 int r;
585 LIST_HEAD(MountPoint, swap_list_head);
586
587 LIST_HEAD_INIT(MountPoint, swap_list_head);
588
589 r = swap_list_get(&swap_list_head);
590 if (r < 0)
591 goto end;
592
12aad1d0 593 r = swap_points_list_off(&swap_list_head, changed);
e3478379
FF
594
595 end:
596 mount_points_list_free(&swap_list_head);
597
598 return r;
599}
600
12aad1d0 601int loopback_detach_all(bool *changed) {
e3478379
FF
602 int r;
603 LIST_HEAD(MountPoint, loopback_list_head);
604
605 LIST_HEAD_INIT(MountPoint, loopback_list_head);
606
607 r = loopback_list_get(&loopback_list_head);
608 if (r < 0)
609 goto end;
610
12aad1d0 611 r = loopback_points_list_detach(&loopback_list_head, changed);
e3478379
FF
612
613 end:
614 mount_points_list_free(&loopback_list_head);
615
616 return r;
617}
d48141ba 618
12aad1d0 619int dm_detach_all(bool *changed) {
d48141ba
LP
620 int r;
621 LIST_HEAD(MountPoint, dm_list_head);
622
623 LIST_HEAD_INIT(MountPoint, dm_list_head);
624
625 r = dm_list_get(&dm_list_head);
626 if (r < 0)
627 goto end;
628
12aad1d0 629 r = dm_points_list_detach(&dm_list_head, changed);
d48141ba
LP
630
631 end:
632 mount_points_list_free(&dm_list_head);
633
634 return r;
635}