Debian Codegraph @

src/shutdown/detach-loopback.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2010 ProFUSION embedded systems
4 ***/
5
6 #include <linux/loop.h>
7 #include <sys/ioctl.h>
8
9 #if HAVE_VALGRIND_MEMCHECK_H
10 #include <valgrind/memcheck.h>
11 #endif
12
13 #include "sd-device.h"
14
15 #include "alloc-util.h"
16 #include "blockdev-util.h"
17 #include "detach-loopback.h"
18 #include "device-util.h"
19 #include "errno-util.h"
20 #include "fd-util.h"
21 #include "list.h"
22 #include "shutdown.h"
23
24 typedef struct LoopbackDevice {
25 char *path;
26 dev_t devnum;
27 LIST_FIELDS(struct LoopbackDevice, loopback_device);
28 } LoopbackDevice;
29
30 static void loopback_device_free(LoopbackDevice **head, LoopbackDevice *m) {
31 assert(head);
32 assert(m);
33
34 LIST_REMOVE(loopback_device, *head, m);
35
36 free(m->path);
37 free(m);
38 }
39
40 static void loopback_device_list_free(LoopbackDevice **head) {
41 assert(head);
42
43 while (*head)
44 loopback_device_free(head, *head);
45 }
46
47 static int loopback_list_get(LoopbackDevice **head) {
48 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
49 int r;
50
51 assert(head);
52
53 r = sd_device_enumerator_new(&e);
54 if (r < 0)
55 return r;
56
57 r = sd_device_enumerator_allow_uninitialized(e);
58 if (r < 0)
59 return r;
60
61 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
62 if (r < 0)
63 return r;
64
65 r = sd_device_enumerator_add_match_sysname(e, "loop*");
66 if (r < 0)
67 return r;
68
69 r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", NULL, true);
70 if (r < 0)
71 return r;
72
73 FOREACH_DEVICE(e, d) {
74 _cleanup_free_ char *p = NULL;
75 const char *dn;
76 LoopbackDevice *lb;
77 dev_t devnum;
78
79 if (sd_device_get_devnum(d, &devnum) < 0 ||
80 sd_device_get_devname(d, &dn) < 0)
81 continue;
82
83 p = strdup(dn);
84 if (!p)
85 return -ENOMEM;
86
87 lb = new(LoopbackDevice, 1);
88 if (!lb)
89 return -ENOMEM;
90
91 *lb = (LoopbackDevice) {
92 .path = TAKE_PTR(p),
93 .devnum = devnum,
94 };
95
96 LIST_PREPEND(loopback_device, *head, lb);
97 }
98
99 return 0;
100 }
101
102 static int delete_loopback(const char *device) {
103 _cleanup_close_ int fd = -EBADF;
104 struct loop_info64 info;
105
106 assert(device);
107
108 fd = open(device, O_RDONLY|O_CLOEXEC);
109 if (fd < 0) {
110 if (ERRNO_IS_DEVICE_ABSENT(errno)) {
111 log_debug_errno(errno, "Tried to open loopback device '%s', but device disappeared by now, ignoring: %m", device);
112 return 0;
113 }
114
115 return log_debug_errno(errno, "Failed to open loopback device '%s': %m", device);
116 }
117
118 /* Loopback block devices don't sync in-flight blocks when we clear the fd, hence sync explicitly
119 * first */
120 (void) sync_with_progress(fd);
121
122 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
123 if (errno == ENXIO) /* Nothing bound, didn't do anything */
124 return 0;
125
126 if (errno != EBUSY)
127 return log_debug_errno(errno, "Failed to clear loopback device %s: %m", device);
128
129 if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
130 if (errno == ENXIO) /* What? Suddenly detached after all? That's fine by us then. */
131 return 1;
132
133 log_debug_errno(errno, "Failed to invoke LOOP_GET_STATUS64 on loopback device %s, ignoring: %m", device);
134 return -EBUSY; /* propagate original error */
135 }
136
137 #if HAVE_VALGRIND_MEMCHECK_H
138 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
139 #endif
140
141 if (FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR)) /* someone else already set LO_FLAGS_AUTOCLEAR for us? fine by us */
142 return -EBUSY; /* propagate original error */
143
144 info.lo_flags |= LO_FLAGS_AUTOCLEAR;
145 if (ioctl(fd, LOOP_SET_STATUS64, &info) < 0) {
146 if (errno == ENXIO) /* Suddenly detached after all? Fine by us */
147 return 1;
148
149 log_debug_errno(errno, "Failed to set LO_FLAGS_AUTOCLEAR flag for loop device %s, ignoring: %m", device);
150 } else
151 log_debug("Successfully set LO_FLAGS_AUTOCLEAR flag for loop device %s.", device);
152
153 return -EBUSY;
154 }
155
156 if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
157 /* If the LOOP_CLR_FD above succeeded we'll see ENXIO here. */
158 if (errno == ENXIO)
159 log_debug("Successfully detached loopback device %s.", device);
160 else
161 log_debug_errno(errno, "Failed to invoke LOOP_GET_STATUS64 on loopback device %s, ignoring: %m", device); /* the LOOP_CLR_FD at least worked, let's hope for the best */
162
163 return 1;
164 }
165
166 #if HAVE_VALGRIND_MEMCHECK_H
167 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
168 #endif
169
170 /* Linux makes LOOP_CLR_FD succeed whenever LO_FLAGS_AUTOCLEAR is set without actually doing
171 * anything. Very confusing. Let's hence not claim we did anything in this case. */
172 if (FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR))
173 log_debug("Successfully called LOOP_CLR_FD on a loopback device %s with autoclear set, which is a NOP.", device);
174 else
175 log_debug("Weird, LOOP_CLR_FD succeeded but the device is still attached on %s.", device);
176
177 return -EBUSY; /* Nothing changed, the device is still attached, hence it apparently is still busy */
178 }
179
180 static int loopback_points_list_detach(LoopbackDevice **head, bool *changed, bool last_try) {
181 int n_failed = 0, r;
182 dev_t rootdev = 0, usrdev = 0;
183
184 assert(head);
185 assert(changed);
186
187 (void) get_block_device_harder("/", &rootdev);
188 (void) block_get_whole_disk(rootdev, &rootdev);
189
190 (void) get_block_device_harder("/usr", &usrdev);
191 (void) block_get_whole_disk(usrdev, &usrdev);
192
193 LIST_FOREACH(loopback_device, m, *head) {
194 if ((major(rootdev) != 0 && rootdev == m->devnum) ||
195 (major(usrdev) != 0 && usrdev == m->devnum)) {
196 log_debug("Not detaching loopback device %s that backs the OS itself, skipping.", m->path);
197 n_failed++;
198 continue;
199 }
200
201 log_info("Detaching loopback %s.", m->path);
202 r = delete_loopback(m->path);
203 if (r < 0) {
204 log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Could not detach loopback %s: %m", m->path);
205 n_failed++;
206 continue;
207 }
208 if (r > 0)
209 *changed = true;
210
211 loopback_device_free(head, m);
212 }
213
214 return n_failed;
215 }
216
217 int loopback_detach_all(bool *changed, bool last_try) {
218 _cleanup_(loopback_device_list_free) LIST_HEAD(LoopbackDevice, loopback_list_head);
219 int r;
220
221 assert(changed);
222
223 LIST_HEAD_INIT(loopback_list_head);
224
225 r = loopback_list_get(&loopback_list_head);
226 if (r < 0)
227 return r;
228
229 return loopback_points_list_detach(&loopback_list_head, changed, last_try);
230 }