Debian Codegraph @

src/shutdown/detach-dm.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2010 ProFUSION embedded systems
4 ***/
5
6 #include <linux/dm-ioctl.h>
7 #include <sys/ioctl.h>
8
9 #include "sd-device.h"
10
11 #include "alloc-util.h"
12 #include "blockdev-util.h"
13 #include "detach-dm.h"
14 #include "device-util.h"
15 #include "devnum-util.h"
16 #include "errno-util.h"
17 #include "fd-util.h"
18 #include "list.h"
19 #include "shutdown.h"
20
21 typedef struct DeviceMapper {
22 char *path;
23 dev_t devnum;
24 LIST_FIELDS(struct DeviceMapper, device_mapper);
25 } DeviceMapper;
26
27 static void device_mapper_free(DeviceMapper **head, DeviceMapper *m) {
28 assert(head);
29 assert(m);
30
31 LIST_REMOVE(device_mapper, *head, m);
32
33 free(m->path);
34 free(m);
35 }
36
37 static void device_mapper_list_free(DeviceMapper **head) {
38 assert(head);
39
40 while (*head)
41 device_mapper_free(head, *head);
42 }
43
44 static int dm_list_get(DeviceMapper **head) {
45 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
46 int r;
47
48 assert(head);
49
50 r = sd_device_enumerator_new(&e);
51 if (r < 0)
52 return r;
53
54 r = sd_device_enumerator_allow_uninitialized(e);
55 if (r < 0)
56 return r;
57
58 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
59 if (r < 0)
60 return r;
61
62 r = sd_device_enumerator_add_match_sysname(e, "dm-*");
63 if (r < 0)
64 return r;
65
66 FOREACH_DEVICE(e, d) {
67 _cleanup_free_ char *p = NULL;
68 const char *dn;
69 DeviceMapper *m;
70 dev_t devnum;
71
72 if (sd_device_get_devnum(d, &devnum) < 0 ||
73 sd_device_get_devname(d, &dn) < 0)
74 continue;
75
76 p = strdup(dn);
77 if (!p)
78 return -ENOMEM;
79
80 m = new(DeviceMapper, 1);
81 if (!m)
82 return -ENOMEM;
83
84 *m = (DeviceMapper) {
85 .path = TAKE_PTR(p),
86 .devnum = devnum,
87 };
88
89 LIST_PREPEND(device_mapper, *head, m);
90 }
91
92 return 0;
93 }
94
95 static int delete_dm(DeviceMapper *m) {
96 _cleanup_close_ int fd = -EBADF;
97
98 assert(m);
99 assert(major(m->devnum) != 0);
100 assert(m->path);
101
102 fd = open(m->path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
103 if (fd < 0)
104 log_debug_errno(errno, "Failed to open DM block device %s for syncing, ignoring: %m", m->path);
105 else {
106 (void) sync_with_progress(fd);
107 fd = safe_close(fd);
108 }
109
110 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
111 if (fd < 0)
112 return log_debug_errno(errno, "Failed to open %s: %m", "/dev/mapper/control");
113
114 return RET_NERRNO(ioctl(fd, DM_DEV_REMOVE, &(struct dm_ioctl) {
115 .version = {
116 DM_VERSION_MAJOR,
117 DM_VERSION_MINOR,
118 DM_VERSION_PATCHLEVEL
119 },
120 .data_size = sizeof(struct dm_ioctl),
121 .dev = m->devnum,
122 }));
123 }
124
125 static int dm_points_list_detach(DeviceMapper **head, bool *changed, bool last_try) {
126 int n_failed = 0, r;
127 dev_t rootdev = 0, usrdev = 0;
128
129 assert(head);
130 assert(changed);
131
132 (void) get_block_device("/", &rootdev);
133 (void) get_block_device("/usr", &usrdev);
134
135 LIST_FOREACH(device_mapper, m, *head) {
136 if ((major(rootdev) != 0 && rootdev == m->devnum) ||
137 (major(usrdev) != 0 && usrdev == m->devnum)) {
138 log_debug("Not detaching DM %s that backs the OS itself, skipping.", m->path);
139 n_failed++;
140 continue;
141 }
142
143 log_info("Detaching DM %s (" DEVNUM_FORMAT_STR ").", m->path, DEVNUM_FORMAT_VAL(m->devnum));
144 r = delete_dm(m);
145 if (r < 0) {
146 log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not detach DM %s: %m", m->path);
147 n_failed++;
148 continue;
149 }
150
151 *changed = true;
152 device_mapper_free(head, m);
153 }
154
155 return n_failed;
156 }
157
158 int dm_detach_all(bool *changed, bool last_try) {
159 _cleanup_(device_mapper_list_free) LIST_HEAD(DeviceMapper, dm_list_head);
160 int r;
161
162 assert(changed);
163
164 LIST_HEAD_INIT(dm_list_head);
165
166 r = dm_list_get(&dm_list_head);
167 if (r < 0)
168 return r;
169
170 return dm_points_list_detach(&dm_list_head, changed, last_try);
171 }