1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 ProFUSION embedded systems
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.
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.
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/>.
25 #include <sys/mount.h>
28 #include <linux/loop.h>
29 #include <linux/dm-ioctl.h>
33 #include "mount-setup.h"
37 typedef struct MountPoint {
40 LIST_FIELDS (struct MountPoint, mount_point);
43 static void mount_point_free(MountPoint **head, MountPoint *m) {
47 LIST_REMOVE(MountPoint, mount_point, *head, m);
53 static void mount_points_list_free(MountPoint **head) {
57 mount_point_free(head, *head);
60 static int mount_points_list_get(MountPoint **head) {
61 FILE *proc_self_mountinfo;
68 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
77 if ((k = fscanf(proc_self_mountinfo,
78 "%*s " /* (1) mount id */
79 "%*s " /* (2) parent id */
80 "%*s " /* (3) major:minor */
82 "%ms " /* (5) mount point */
83 "%*s" /* (6) mount options */
84 "%*[^-]" /* (7) optional fields */
85 "- " /* (8) separator */
86 "%*s " /* (9) file system type */
87 "%*s" /* (10) mount source */
88 "%*s" /* (11) mount options 2 */
89 "%*[^\n]", /* some rubbish at the end */
94 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
108 if (mount_point_is_api(p) || mount_point_ignore(p)) {
113 if (!(m = new0(MountPoint, 1))) {
120 LIST_PREPEND(MountPoint, mount_point, *head, m);
126 fclose(proc_self_mountinfo);
131 static int swap_list_get(MountPoint **head) {
138 if (!(proc_swaps = fopen("/proc/swaps", "re")))
141 (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
145 char *dev = NULL, *d;
148 if ((k = fscanf(proc_swaps,
149 "%ms " /* device/file */
150 "%*s " /* type of swap */
151 "%*s " /* swap size */
153 "%*s\n", /* priority */
159 log_warning("Failed to parse /proc/swaps:%u.", i);
165 if (endswith(dev, "(deleted)")) {
178 if (!(swap = new0(MountPoint, 1))) {
185 LIST_PREPEND(MountPoint, mount_point, *head, swap);
196 static int loopback_list_get(MountPoint **head) {
199 struct udev_enumerate *e = NULL;
200 struct udev_list_entry *item = NULL, *first = NULL;
204 if (!(udev = udev_new())) {
209 if (!(e = udev_enumerate_new(udev))) {
214 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
215 udev_enumerate_add_match_sysname(e, "loop*") < 0) {
220 if (udev_enumerate_scan_devices(e) < 0) {
225 first = udev_enumerate_get_list_entry(e);
226 udev_list_entry_foreach(item, first) {
228 struct udev_device *d;
232 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
237 if (!(dn = udev_device_get_devnode(d))) {
238 udev_device_unref(d);
243 udev_device_unref(d);
250 if (!(lb = new0(MountPoint, 1))) {
257 LIST_PREPEND(MountPoint, mount_point, *head, lb);
264 udev_enumerate_unref(e);
272 static int dm_list_get(MountPoint **head) {
275 struct udev_enumerate *e = NULL;
276 struct udev_list_entry *item = NULL, *first = NULL;
280 if (!(udev = udev_new())) {
285 if (!(e = udev_enumerate_new(udev))) {
290 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
291 udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
296 if (udev_enumerate_scan_devices(e) < 0) {
301 first = udev_enumerate_get_list_entry(e);
303 udev_list_entry_foreach(item, first) {
305 struct udev_device *d;
310 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
315 devnum = udev_device_get_devnum(d);
316 dn = udev_device_get_devnode(d);
318 if (major(devnum) == 0 || !dn) {
319 udev_device_unref(d);
324 udev_device_unref(d);
331 if (!(m = new(MountPoint, 1))) {
339 LIST_PREPEND(MountPoint, mount_point, *head, m);
346 udev_enumerate_unref(e);
354 static int delete_loopback(const char *device) {
357 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
360 r = ioctl(fd, LOOP_CLR_FD, 0);
361 close_nointr_nofail(fd);
366 /* ENXIO: not bound, so no error */
373 static int delete_dm(dev_t devnum) {
377 assert(major(devnum) != 0);
379 if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0)
383 dm.version[0] = DM_VERSION_MAJOR;
384 dm.version[1] = DM_VERSION_MINOR;
385 dm.version[2] = DM_VERSION_PATCHLEVEL;
387 dm.data_size = sizeof(dm);
390 r = ioctl(fd, DM_DEV_REMOVE, &dm);
391 close_nointr_nofail(fd);
393 return r >= 0 ? 0 : -errno;
396 static int mount_points_list_umount(MountPoint **head, bool *changed) {
402 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
403 if (streq(m->path, "/")) {
408 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
409 if (umount2(m->path, MNT_FORCE) == 0) {
413 mount_point_free(head, m);
415 log_warning("Could not unmount %s: %m", m->path);
423 static int mount_points_list_remount_read_only(MountPoint **head, bool *changed) {
429 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
431 /* Trying to remount read-only */
432 if (mount(NULL, m->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0) {
436 mount_point_free(head, m);
438 log_warning("Could not remount as read-only %s: %m", m->path);
446 static int swap_points_list_off(MountPoint **head, bool *changed) {
452 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
453 if (swapoff(m->path) == 0) {
457 mount_point_free(head, m);
459 log_warning("Could not deactivate swap %s: %m", m->path);
467 static int loopback_points_list_detach(MountPoint **head, bool *changed) {
473 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
476 if ((r = delete_loopback(m->path)) >= 0) {
478 if (r > 0 && changed)
481 mount_point_free(head, m);
483 log_warning("Could not delete loopback %s: %m", m->path);
491 static int dm_points_list_detach(MountPoint **head, bool *changed) {
497 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
500 if ((r = delete_dm(m->devnum)) >= 0) {
502 if (r > 0 && changed)
505 mount_point_free(head, m);
507 log_warning("Could not delete dm %s: %m", m->path);
515 int umount_all(bool *changed) {
517 LIST_HEAD(MountPoint, mp_list_head);
519 LIST_HEAD_INIT(MountPoint, mp_list_head);
521 r = mount_points_list_get(&mp_list_head);
525 r = mount_points_list_umount(&mp_list_head, changed);
529 r = mount_points_list_remount_read_only(&mp_list_head, changed);
532 mount_points_list_free(&mp_list_head);
537 int swapoff_all(bool *changed) {
539 LIST_HEAD(MountPoint, swap_list_head);
541 LIST_HEAD_INIT(MountPoint, swap_list_head);
543 r = swap_list_get(&swap_list_head);
547 r = swap_points_list_off(&swap_list_head, changed);
550 mount_points_list_free(&swap_list_head);
555 int loopback_detach_all(bool *changed) {
557 LIST_HEAD(MountPoint, loopback_list_head);
559 LIST_HEAD_INIT(MountPoint, loopback_list_head);
561 r = loopback_list_get(&loopback_list_head);
565 r = loopback_points_list_detach(&loopback_list_head, changed);
568 mount_points_list_free(&loopback_list_head);
573 int dm_detach_all(bool *changed) {
575 LIST_HEAD(MountPoint, dm_list_head);
577 LIST_HEAD_INIT(MountPoint, dm_list_head);
579 r = dm_list_get(&dm_list_head);
583 r = dm_points_list_detach(&dm_list_head, changed);
586 mount_points_list_free(&dm_list_head);