Debian Codegraph @

src/plugins/loop.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <unistd.h>
22 #include <glob.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <linux/loop.h>
27 #include <errno.h>
28 #include <blockdev/utils.h>
29 #include "loop.h"
30
31 #ifndef LOOP_SET_BLOCK_SIZE
32 #define LOOP_SET_BLOCK_SIZE 0x4C09
33 #endif
34
35 /**
36 * SECTION: loop
37 * @short_description: plugin for operations with loop devices
38 * @title: Loop
39 * @include: loop.h
40 *
41 * A plugin for operations with loop devices. All sizes passed
42 * in/out to/from the functions are in bytes.
43 */
44
45 static GMutex loop_control_lock;
46
47 /**
48 * bd_loop_error_quark: (skip)
49 */
50 GQuark bd_loop_error_quark (void)
51 {
52 return g_quark_from_static_string ("g-bd-loop-error-quark");
53 }
54
55 /**
56 * bd_loop_init:
57 *
58 * Initializes the plugin. **This function is called automatically by the
59 * library's initialization functions.**
60 *
61 */
62 gboolean bd_loop_init (void) {
63 /* nothing to do here */
64 return TRUE;
65 };
66
67 /**
68 * bd_loop_close:
69 *
70 * Cleans up after the plugin. **This function is called automatically by the
71 * library's functions that unload it.**
72 *
73 */
74 void bd_loop_close (void) {
75 /* nothing to do here */
76 }
77
78
79 /**
80 * bd_loop_is_tech_avail:
81 * @tech: the queried tech
82 * @mode: a bit mask of queried modes of operation (#BDLoopTechMode) for @tech
83 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
84 *
85 * Returns: whether the @tech-@mode combination is available -- supported by the
86 * plugin implementation and having all the runtime dependencies available
87 */
88 gboolean bd_loop_is_tech_avail (BDLoopTech tech G_GNUC_UNUSED, guint64 mode G_GNUC_UNUSED, GError **error G_GNUC_UNUSED) {
89 /* all combinations are supported by this implementation of the plugin */
90 return TRUE;
91 }
92
93 void bd_loop_info_free (BDLoopInfo *info) {
94 if (info == NULL)
95 return;
96
97 g_free (info->backing_file);
98 g_free (info);
99 }
100
101 BDLoopInfo* bd_loop_info_copy (BDLoopInfo *info) {
102 if (info == NULL)
103 return NULL;
104
105 BDLoopInfo *new_info = g_new0 (BDLoopInfo, 1);
106
107 new_info->backing_file = g_strdup (info->backing_file);
108 new_info->offset = info->offset;
109 new_info->autoclear = info->autoclear;
110 new_info->direct_io = info->direct_io;
111 new_info->part_scan = info->part_scan;
112 new_info->read_only = info->read_only;
113
114 return new_info;
115 }
116
117 static gchar* _loop_get_backing_file (const gchar *dev_name, GError **error) {
118 gchar *sys_path = g_strdup_printf ("/sys/class/block/%s/loop/backing_file", dev_name);
119 gchar *ret = NULL;
120 gboolean success = FALSE;
121
122 if (access (sys_path, R_OK) != 0) {
123 g_free (sys_path);
124 return NULL;
125 }
126
127 success = g_file_get_contents (sys_path, &ret, NULL, error);
128 if (!success) {
129 /* error is already populated */
130 g_free (sys_path);
131 return NULL;
132 }
133
134 g_free (sys_path);
135 return g_strstrip (ret);
136 }
137
138 /**
139 * bd_loop_info:
140 * @loop: name of the loop device to get information about (e.g. "loop0")
141 * @error: (out) (optional): place to store error (if any)
142 *
143 * Returns: (transfer full): information about the @loop device or %NULL in case of error
144 *
145 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_QUERY
146 */
147 BDLoopInfo* bd_loop_info (const gchar *loop, GError **error) {
148 BDLoopInfo *info = NULL;
149 g_autofree gchar *dev_loop = NULL;
150 gint fd = -1;
151 struct loop_info64 li64;
152 GError *l_error = NULL;
153
154 if (!g_str_has_prefix (loop, "/dev/"))
155 dev_loop = g_strdup_printf ("/dev/%s", loop);
156
157 fd = open (dev_loop ? dev_loop : loop, O_RDONLY);
158 if (fd < 0) {
159 g_set_error (error, BD_LOOP_ERROR, BD_LOOP_ERROR_DEVICE,
160 "Failed to open device %s: %m", loop);
161 return NULL;
162 }
163
164 memset (&li64, 0, sizeof (li64));
165 if (ioctl (fd, LOOP_GET_STATUS64, &li64) < 0) {
166 g_set_error (error, BD_LOOP_ERROR,
167 errno == ENXIO ? BD_LOOP_ERROR_DEVICE : BD_LOOP_ERROR_FAIL,
168 "Failed to get status of the device %s: %m", loop);
169 close (fd);
170 return NULL;
171 }
172
173 close (fd);
174
175 info = g_new0 (BDLoopInfo, 1);
176 info->offset = li64.lo_offset;
177 if ((li64.lo_flags & LO_FLAGS_AUTOCLEAR) != 0)
178 info->autoclear = TRUE;
179 if ((li64.lo_flags & LO_FLAGS_DIRECT_IO) != 0)
180 info->direct_io = TRUE;
181 if ((li64.lo_flags & LO_FLAGS_PARTSCAN) != 0)
182 info->part_scan = TRUE;
183 if ((li64.lo_flags & LO_FLAGS_READ_ONLY) != 0)
184 info->read_only = TRUE;
185
186 info->backing_file = _loop_get_backing_file (loop, &l_error);
187 if (l_error) {
188 bd_loop_info_free (info);
189 g_set_error (error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
190 "Failed to get backing file of the device %s: %s", loop, l_error->message);
191 g_clear_error (&l_error);
192 return NULL;
193 }
194
195 return info;
196 }
197
198 /**
199 * bd_loop_get_loop_name:
200 * @file: path of the backing file to get loop name for
201 * @error: (out) (optional): place to store error (if any)
202 *
203 * Returns: (transfer full): name of the loop device associated with the given
204 * @file or %NULL if failed to determine
205 *
206 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_QUERY
207 */
208 gchar* bd_loop_get_loop_name (const gchar *file, GError **error G_GNUC_UNUSED) {
209 glob_t globbuf;
210 gchar **path_p;
211 gboolean success = FALSE;
212 gchar *content = NULL;
213 gboolean found = FALSE;
214 gchar **parts;
215 gchar *ret;
216
217 if (glob ("/sys/block/loop*/loop/backing_file", GLOB_NOSORT, NULL, &globbuf) != 0) {
218 return NULL;
219 }
220
221 for (path_p = globbuf.gl_pathv; *path_p && !found; path_p++) {
222 success = g_file_get_contents (*path_p, &content, NULL, NULL);
223 if (!success)
224 continue;
225
226 g_strstrip (content);
227 found = (g_strcmp0 (content, file) == 0);
228 g_free (content);
229 }
230
231 if (!found) {
232 globfree (&globbuf);
233 return NULL;
234 }
235
236 parts = g_strsplit (*(path_p - 1), "/", 5);
237 ret = g_strdup (parts[3]);
238 g_strfreev (parts);
239
240 globfree (&globbuf);
241 return ret;
242 }
243
244 /**
245 * bd_loop_setup:
246 * @file: file to setup as a loop device
247 * @offset: offset of the start of the device (in @file)
248 * @size: maximum size of the device (or 0 to leave unspecified)
249 * @read_only: whether to setup as read-only (%TRUE) or read-write (%FALSE)
250 * @part_scan: whether to enforce partition scan on the newly created device or not
251 * @sector_size: logical sector size for the loop device in bytes (or 0 for default)
252 * @loop_name: (optional) (out): if not %NULL, it is used to store the name of the loop device
253 * @error: (out) (optional): place to store error (if any)
254 *
255 * Returns: whether the @file was successfully setup as a loop device or not
256 *
257 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_CREATE
258 */
259 gboolean bd_loop_setup (const gchar *file, guint64 offset, guint64 size, gboolean read_only, gboolean part_scan, guint64 sector_size, const gchar **loop_name, GError **error) {
260 gint fd = -1;
261 gboolean ret = FALSE;
262
263 /* open as RDWR so that @read_only determines whether the device is
264 read-only or not */
265 fd = open (file, O_RDWR);
266 if (fd < 0) {
267 g_set_error (error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
268 "Failed to open the backing file '%s': %m", file);
269 return FALSE;
270 }
271
272 ret = bd_loop_setup_from_fd (fd, offset, size, read_only, part_scan, sector_size, loop_name, error);
273 close (fd);
274 return ret;
275 }
276
277 /**
278 * bd_loop_setup_from_fd:
279 * @fd: file descriptor for a file to setup as a new loop device
280 * @offset: offset of the start of the device (in file given by @fd)
281 * @size: maximum size of the device (or 0 to leave unspecified)
282 * @read_only: whether to setup as read-only (%TRUE) or read-write (%FALSE)
283 * @part_scan: whether to enforce partition scan on the newly created device or not
284 * @sector_size: logical sector size for the loop device in bytes (or 0 for default)
285 * @loop_name: (optional) (out): if not %NULL, it is used to store the name of the loop device
286 * @error: (out) (optional): place to store error (if any)
287 *
288 * Returns: whether a new loop device was successfully setup for @fd or not
289 *
290 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_CREATE
291 */
292 gboolean bd_loop_setup_from_fd (gint fd, guint64 offset, guint64 size, gboolean read_only, gboolean part_scan, guint64 sector_size, const gchar **loop_name, GError **error) {
293 gint loop_control_fd = -1;
294 gint loop_number = -1;
295 gchar *loop_device = NULL;
296 gint loop_fd = -1;
297 struct loop_info64 li64;
298 guint64 progress_id = 0;
299 gint status = 0;
300 guint n_try = 0;
301 GError *l_error = NULL;
302
303 progress_id = bd_utils_report_started ("Started setting up loop device");
304
305 loop_control_fd = open ("/dev/loop-control", O_RDWR);
306 if (loop_control_fd == -1) {
307 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
308 "Failed to open the loop-control device: %m");
309 bd_utils_report_finished (progress_id, l_error->message);
310 g_propagate_error (error, l_error);
311 return FALSE;
312 }
313
314 /* XXX: serialize access to loop-control (seems to be required, but it's not
315 documented anywhere) */
316 g_mutex_lock (&loop_control_lock);
317 loop_number = ioctl (loop_control_fd, LOOP_CTL_GET_FREE);
318 g_mutex_unlock (&loop_control_lock);
319 close (loop_control_fd);
320 if (loop_number < 0) {
321 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
322 "Failed to open the loop-control device: %m");
323 bd_utils_report_finished (progress_id, l_error->message);
324 g_propagate_error (error, l_error);
325 return FALSE;
326 }
327
328 bd_utils_report_progress (progress_id, 33, "Got free loop device");
329
330 loop_device = g_strdup_printf ("/dev/loop%d", loop_number);
331 loop_fd = open (loop_device, read_only ? O_RDONLY : O_RDWR);
332 if (loop_fd == -1) {
333 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
334 "Failed to open the %s device: %m", loop_device);
335 g_free (loop_device);
336 bd_utils_report_finished (progress_id, l_error->message);
337 g_propagate_error (error, l_error);
338 return FALSE;
339 }
340
341 memset (&li64, '\0', sizeof (li64));
342 if (read_only)
343 li64.lo_flags |= LO_FLAGS_READ_ONLY;
344 if (part_scan)
345 li64.lo_flags |= LO_FLAGS_PARTSCAN;
346 if (offset > 0)
347 li64.lo_offset = offset;
348 if (size > 0)
349 li64.lo_sizelimit = size;
350 if (ioctl (loop_fd, LOOP_SET_FD, fd) < 0) {
351 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_DEVICE,
352 "Failed to associate the %s device with the file descriptor: %m", loop_device);
353 g_free (loop_device);
354 close (loop_fd);
355 bd_utils_report_finished (progress_id, l_error->message);
356 g_propagate_error (error, l_error);
357 return FALSE;
358 }
359
360 bd_utils_report_progress (progress_id, 66, "Associated the loop device");
361
362 /* we may need to try multiple times with some delays in case the device is
363 busy at the very moment */
364 for (n_try=10, status=-1; (status != 0) && (n_try > 0); n_try--) {
365 status = ioctl (loop_fd, LOOP_SET_STATUS64, &li64);
366 if (status < 0 && errno == EAGAIN)
367 g_usleep (100 * 1000); /* microseconds */
368 else
369 break;
370 }
371
372 if (status != 0) {
373 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
374 "Failed to set status for the %s device: %m", loop_device);
375 if (ioctl (loop_fd, LOOP_CLR_FD) < 0)
376 bd_utils_log_format (BD_UTILS_LOG_WARNING,
377 "Failed to clear the %s device: %m", loop_device);
378 g_free (loop_device);
379 close (loop_fd);
380 bd_utils_report_finished (progress_id, l_error->message);
381 g_propagate_error (error, l_error);
382 return FALSE;
383 }
384
385 if (sector_size > 0) {
386 for (n_try=10, status=-1; (status != 0) && (n_try > 0); n_try--) {
387 status = ioctl (loop_fd, LOOP_SET_BLOCK_SIZE, (unsigned long) sector_size);
388 if (status < 0 && errno == EAGAIN)
389 g_usleep (100 * 1000); /* microseconds */
390 else
391 break;
392 }
393
394 if (status != 0) {
395 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
396 "Failed to set sector size for the %s device: %m", loop_device);
397 if (ioctl (loop_fd, LOOP_CLR_FD) < 0)
398 bd_utils_log_format (BD_UTILS_LOG_WARNING,
399 "Failed to clear the %s device: %m", loop_device);
400 g_free (loop_device);
401 close (loop_fd);
402 bd_utils_report_finished (progress_id, l_error->message);
403 g_propagate_error (error, l_error);
404 return FALSE;
405 }
406 }
407
408 if (loop_name)
409 *loop_name = g_strdup_printf ("loop%d", loop_number);
410
411 g_free (loop_device);
412 close (loop_fd);
413 bd_utils_report_finished (progress_id, "Completed");
414 return TRUE;
415 }
416
417 /**
418 * bd_loop_teardown:
419 * @loop: path or name of the loop device to tear down
420 * @error: (out) (optional): place to store error (if any)
421 *
422 * Returns: whether the @loop device was successfully torn down or not
423 *
424 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_DESTROY
425 */
426 gboolean bd_loop_teardown (const gchar *loop, GError **error) {
427 gchar *dev_loop = NULL;
428 gint loop_fd = -1;
429 guint64 progress_id = 0;
430 GError *l_error = NULL;
431
432 progress_id = bd_utils_report_started ("Started tearing down loop device");
433
434 if (!g_str_has_prefix (loop, "/dev/"))
435 dev_loop = g_strdup_printf ("/dev/%s", loop);
436
437 loop_fd = open (dev_loop ? dev_loop : loop, O_RDONLY);
438 g_free (dev_loop);
439 if (loop_fd == -1) {
440 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
441 "Failed to open the %s device: %m", loop);
442 bd_utils_report_finished (progress_id, l_error->message);
443 g_propagate_error (error, l_error);
444 return FALSE;
445 }
446
447 if (ioctl (loop_fd, LOOP_CLR_FD) < 0) {
448 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
449 "Failed to detach the backing file from the %s device: %m", loop);
450 close (loop_fd);
451 bd_utils_report_finished (progress_id, l_error->message);
452 g_propagate_error (error, l_error);
453 return FALSE;
454 }
455
456 close (loop_fd);
457 bd_utils_report_finished (progress_id, "Completed");
458
459 return TRUE;
460 }
461
462 /**
463 * bd_loop_set_autoclear:
464 * @loop: path or name of the loop device
465 * @autoclear: whether to set or unset the autoclear flag
466 * @error: (out) (optional): place to store error (if any)
467 *
468 * Returns: whether the autoclear flag was successfully set on the @loop device or not
469 *
470 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_MODIFY
471 */
472 gboolean bd_loop_set_autoclear (const gchar *loop, gboolean autoclear, GError **error) {
473 gchar *dev_loop = NULL;
474 gint fd = -1;
475 struct loop_info64 li64;
476 guint64 progress_id = 0;
477 gchar *msg = NULL;
478 GError *l_error = NULL;
479
480 if (!g_str_has_prefix (loop, "/dev/"))
481 dev_loop = g_strdup_printf ("/dev/%s", loop);
482
483 msg = g_strdup_printf ("Started setting up the autoclear flag on the %s device",
484 dev_loop ? dev_loop : loop);
485 progress_id = bd_utils_report_started (msg);
486 g_free (msg);
487
488 fd = open (dev_loop ? dev_loop : loop, O_RDWR);
489 g_free (dev_loop);
490 if (fd < 0) {
491 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_DEVICE,
492 "Failed to open device %s: %m", loop);
493 bd_utils_report_finished (progress_id, l_error->message);
494 g_propagate_error (error, l_error);
495 return FALSE;
496 }
497
498 memset (&li64, 0, sizeof (li64));
499 if (ioctl (fd, LOOP_GET_STATUS64, &li64) < 0) {
500 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
501 "Failed to get status of the device %s: %m", loop);
502 close (fd);
503 bd_utils_report_finished (progress_id, l_error->message);
504 g_propagate_error (error, l_error);
505 return FALSE;
506 }
507
508 if (autoclear)
509 li64.lo_flags |= LO_FLAGS_AUTOCLEAR;
510 else
511 li64.lo_flags &= (~LO_FLAGS_AUTOCLEAR);
512
513 if (ioctl (fd, LOOP_SET_STATUS64, &li64) < 0) {
514 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
515 "Failed to set status of the device %s: %m", loop);
516 close (fd);
517 bd_utils_report_finished (progress_id, l_error->message);
518 g_propagate_error (error, l_error);
519 return FALSE;
520 }
521
522 close (fd);
523 bd_utils_report_finished (progress_id, "Completed");
524 return TRUE;
525 }
526
527 /**
528 * bd_loop_set_capacity:
529 * @loop: path or name of the loop device
530 * @error: (out) (optional): place to store error (if any)
531 *
532 * Force the loop driver to reread the size of the file associated with the
533 * specified @loop device.
534 *
535 * Returns: whether the LOOP_SET_CAPACITY ioctl was successfully issued or not.
536 *
537 * Tech category: %BD_LOOP_TECH_LOOP-%BD_LOOP_TECH_MODE_MODIFY
538 */
539 gboolean bd_loop_set_capacity (const gchar *loop, GError **error) {
540 gchar *dev_loop = NULL;
541 gint fd = -1;
542 guint64 progress_id = 0;
543 gchar *msg = NULL;
544 guint n_try = 0;
545 gint status = 0;
546 GError *l_error = NULL;
547
548 if (!g_str_has_prefix (loop, "/dev/"))
549 dev_loop = g_strdup_printf ("/dev/%s", loop);
550
551 msg = g_strdup_printf ("Started setting up capacity on the %s device",
552 dev_loop ? dev_loop : loop);
553 progress_id = bd_utils_report_started (msg);
554 g_free (msg);
555
556 fd = open (dev_loop ? dev_loop : loop, O_RDWR);
557 g_free (dev_loop);
558 if (fd < 0) {
559 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_DEVICE,
560 "Failed to open device %s: %m", loop);
561 bd_utils_report_finished (progress_id, l_error->message);
562 g_propagate_error (error, l_error);
563 return FALSE;
564 }
565
566 for (n_try=10, status=-1; (status != 0) && (n_try > 0); n_try--) {
567 status = ioctl (fd, LOOP_SET_CAPACITY, 0);
568 if (status < 0 && errno == EAGAIN)
569 g_usleep (100 * 1000); /* microseconds */
570 else
571 break;
572 }
573
574 if (status != 0) {
575 g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_FAIL,
576 "Failed to set capacity of the device %s: %m", loop);
577 close (fd);
578 bd_utils_report_finished (progress_id, l_error->message);
579 g_propagate_error (error, l_error);
580 return FALSE;
581 }
582
583 close (fd);
584 bd_utils_report_finished (progress_id, "Completed");
585 return TRUE;
586 }