]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/loopdev.c
setproctitle: fix out of boundary access
[thirdparty/util-linux.git] / lib / loopdev.c
1
2 /*
3 * No copyright is claimed. This code is in the public domain; do with
4 * it what you wish.
5 *
6 * Written by Karel Zak <kzak@redhat.com>
7 *
8 * -- based on mount/losetup.c
9 *
10 * Simple library for work with loop devices.
11 *
12 * - requires kernel 2.6.x
13 * - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
14 * - reads info by ioctl
15 * - supports *unlimited* number of loop devices
16 * - supports /dev/loop<N> as well as /dev/loop/<N>
17 * - minimize overhead (fd, loopinfo, ... are shared for all operations)
18 * - setup (associate device and backing file)
19 * - delete (dis-associate file)
20 * - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
21 * - extendible
22 */
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33 #include <inttypes.h>
34 #include <dirent.h>
35
36 #include "linux_version.h"
37 #include "c.h"
38 #include "sysfs.h"
39 #include "pathnames.h"
40 #include "loopdev.h"
41 #include "canonicalize.h"
42 #include "blkdev.h"
43 #include "debug.h"
44
45 /*
46 * Debug stuff (based on include/debug.h)
47 */
48 static UL_DEBUG_DEFINE_MASK(loopdev);
49 UL_DEBUG_DEFINE_MASKNAMES(loopdev) = UL_DEBUG_EMPTY_MASKNAMES;
50
51 #define LOOPDEV_DEBUG_INIT (1 << 1)
52 #define LOOPDEV_DEBUG_CXT (1 << 2)
53 #define LOOPDEV_DEBUG_ITER (1 << 3)
54 #define LOOPDEV_DEBUG_SETUP (1 << 4)
55 #define SFDISKPROG_DEBUG_ALL 0xFFFF
56
57 #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
58 #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
59
60 static void loopdev_init_debug(void)
61 {
62 if (loopdev_debug_mask)
63 return;
64 __UL_INIT_DEBUG(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG);
65 }
66
67 /*
68 * see loopcxt_init()
69 */
70 #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
71 #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
72 && !loopcxt_ioctl_enabled(_lc)
73
74 /*
75 * @lc: context
76 * @device: device name, absolute device path or NULL to reset the current setting
77 *
78 * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
79 * names ("loop<N>") are converted to the path (/dev/loop<N> or to
80 * /dev/loop/<N>)
81 *
82 * This sets the device name, but does not check if the device exists!
83 *
84 * Returns: <0 on error, 0 on success
85 */
86 int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
87 {
88 if (!lc)
89 return -EINVAL;
90
91 if (lc->fd >= 0) {
92 close(lc->fd);
93 DBG(CXT, ul_debugobj(lc, "closing old open fd"));
94 }
95 lc->fd = -1;
96 lc->mode = 0;
97 lc->has_info = 0;
98 lc->info_failed = 0;
99 *lc->device = '\0';
100 memset(&lc->info, 0, sizeof(lc->info));
101
102 /* set new */
103 if (device) {
104 if (*device != '/') {
105 const char *dir = _PATH_DEV;
106
107 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
108 if (lc->flags & LOOPDEV_FL_DEVSUBDIR) {
109 if (strlen(device) < 5)
110 return -1;
111 device += 4;
112 dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */
113 }
114 snprintf(lc->device, sizeof(lc->device), "%s%s",
115 dir, device);
116 } else {
117 strncpy(lc->device, device, sizeof(lc->device));
118 lc->device[sizeof(lc->device) - 1] = '\0';
119 }
120 DBG(CXT, ul_debugobj(lc, "%s name assigned", device));
121 }
122
123 sysfs_deinit(&lc->sysfs);
124 return 0;
125 }
126
127 int loopcxt_has_device(struct loopdev_cxt *lc)
128 {
129 return lc && *lc->device;
130 }
131
132 /*
133 * @lc: context
134 * @flags: LOOPDEV_FL_* flags
135 *
136 * Initialize loop handler.
137 *
138 * We have two sets of the flags:
139 *
140 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
141 *
142 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
143 *
144 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
145 * syscall to open loop device. By default is the device open read-only.
146 *
147 * The expection is loopcxt_setup_device(), where the device is open read-write
148 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
149 *
150 * Returns: <0 on error, 0 on success.
151 */
152 int loopcxt_init(struct loopdev_cxt *lc, int flags)
153 {
154 int rc;
155 struct stat st;
156 struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY;
157
158 if (!lc)
159 return -EINVAL;
160
161 loopdev_init_debug();
162 DBG(CXT, ul_debugobj(lc, "initialize context"));
163
164 memcpy(lc, &dummy, sizeof(dummy));
165 lc->flags = flags;
166
167 rc = loopcxt_set_device(lc, NULL);
168 if (rc)
169 return rc;
170
171 if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) {
172 lc->flags |= LOOPDEV_FL_NOSYSFS;
173 lc->flags &= ~LOOPDEV_FL_NOIOCTL;
174 DBG(CXT, ul_debugobj(lc, "init: disable /sys usage"));
175 }
176
177 if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
178 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
179 /*
180 * Use only sysfs for basic information about loop devices
181 */
182 lc->flags |= LOOPDEV_FL_NOIOCTL;
183 DBG(CXT, ul_debugobj(lc, "init: ignore ioctls"));
184 }
185
186 if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) {
187 lc->flags |= LOOPDEV_FL_CONTROL;
188 DBG(CXT, ul_debugobj(lc, "init: loop-control detected "));
189 }
190
191 return 0;
192 }
193
194 /*
195 * @lc: context
196 *
197 * Deinitialize loop context
198 */
199 void loopcxt_deinit(struct loopdev_cxt *lc)
200 {
201 int errsv = errno;
202
203 if (!lc)
204 return;
205
206 DBG(CXT, ul_debugobj(lc, "de-initialize"));
207
208 free(lc->filename);
209 lc->filename = NULL;
210
211 ignore_result( loopcxt_set_device(lc, NULL) );
212 loopcxt_deinit_iterator(lc);
213
214 errno = errsv;
215 }
216
217 /*
218 * @lc: context
219 *
220 * Returns newly allocated device path.
221 */
222 char *loopcxt_strdup_device(struct loopdev_cxt *lc)
223 {
224 if (!lc || !*lc->device)
225 return NULL;
226 return strdup(lc->device);
227 }
228
229 /*
230 * @lc: context
231 *
232 * Returns pointer device name in the @lc struct.
233 */
234 const char *loopcxt_get_device(struct loopdev_cxt *lc)
235 {
236 return lc && *lc->device ? lc->device : NULL;
237 }
238
239 /*
240 * @lc: context
241 *
242 * Returns pointer to the sysfs context (see lib/sysfs.c)
243 */
244 struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)
245 {
246 if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS))
247 return NULL;
248
249 if (!lc->sysfs.devno) {
250 dev_t devno = sysfs_devname_to_devno(lc->device, NULL);
251 if (!devno) {
252 DBG(CXT, ul_debugobj(lc, "sysfs: failed devname to devno"));
253 return NULL;
254 }
255 if (sysfs_init(&lc->sysfs, devno, NULL)) {
256 DBG(CXT, ul_debugobj(lc, "sysfs: init failed"));
257 return NULL;
258 }
259 }
260
261 return &lc->sysfs;
262 }
263
264 /*
265 * @lc: context
266 *
267 * Returns: file descriptor to the open loop device or <0 on error. The mode
268 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
269 * read-only.
270 */
271 int loopcxt_get_fd(struct loopdev_cxt *lc)
272 {
273 if (!lc || !*lc->device)
274 return -EINVAL;
275
276 if (lc->fd < 0) {
277 lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY;
278 lc->fd = open(lc->device, lc->mode | O_CLOEXEC);
279 DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device,
280 lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro"));
281 }
282 return lc->fd;
283 }
284
285 int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode)
286 {
287 if (!lc)
288 return -EINVAL;
289
290 lc->fd = fd;
291 lc->mode = mode;
292 return 0;
293 }
294
295 /*
296 * @lc: context
297 * @flags: LOOPITER_FL_* flags
298 *
299 * Iterator allows to scan list of the free or used loop devices.
300 *
301 * Returns: <0 on error, 0 on success
302 */
303 int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags)
304 {
305 struct loopdev_iter *iter;
306 struct stat st;
307
308 if (!lc)
309 return -EINVAL;
310
311
312 iter = &lc->iter;
313 DBG(ITER, ul_debugobj(iter, "initialize"));
314
315 /* always zeroize
316 */
317 memset(iter, 0, sizeof(*iter));
318 iter->ncur = -1;
319 iter->flags = flags;
320 iter->default_check = 1;
321
322 if (!lc->extra_check) {
323 /*
324 * Check for /dev/loop/<N> subdirectory
325 */
326 if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) &&
327 stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode))
328 lc->flags |= LOOPDEV_FL_DEVSUBDIR;
329
330 lc->extra_check = 1;
331 }
332 return 0;
333 }
334
335 /*
336 * @lc: context
337 *
338 * Returns: <0 on error, 0 on success
339 */
340 int loopcxt_deinit_iterator(struct loopdev_cxt *lc)
341 {
342 struct loopdev_iter *iter;
343
344 if (!lc)
345 return -EINVAL;
346
347 iter = &lc->iter;
348 DBG(ITER, ul_debugobj(iter, "de-initialize"));
349
350 free(iter->minors);
351 if (iter->proc)
352 fclose(iter->proc);
353 if (iter->sysblock)
354 closedir(iter->sysblock);
355
356 memset(iter, 0, sizeof(*iter));
357 return 0;
358 }
359
360 /*
361 * Same as loopcxt_set_device, but also checks if the device is
362 * associeted with any file.
363 *
364 * Returns: <0 on error, 0 on success, 1 device does not match with
365 * LOOPITER_FL_{USED,FREE} flags.
366 */
367 static int loopiter_set_device(struct loopdev_cxt *lc, const char *device)
368 {
369 int rc = loopcxt_set_device(lc, device);
370 int used;
371
372 if (rc)
373 return rc;
374
375 if (!(lc->iter.flags & LOOPITER_FL_USED) &&
376 !(lc->iter.flags & LOOPITER_FL_FREE))
377 return 0; /* caller does not care about device status */
378
379 if (!is_loopdev(lc->device)) {
380 DBG(ITER, ul_debugobj(&lc->iter, "%s does not exist", lc->device));
381 return -errno;
382 }
383
384 DBG(ITER, ul_debugobj(&lc->iter, "%s exist", lc->device));
385
386 used = loopcxt_get_offset(lc, NULL) == 0;
387
388 if ((lc->iter.flags & LOOPITER_FL_USED) && used)
389 return 0;
390
391 if ((lc->iter.flags & LOOPITER_FL_FREE) && !used)
392 return 0;
393
394 DBG(ITER, ul_debugobj(&lc->iter, "failed to use %s device", lc->device));
395
396 ignore_result( loopcxt_set_device(lc, NULL) );
397 return 1;
398 }
399
400 static int cmpnum(const void *p1, const void *p2)
401 {
402 return (((* (int *) p1) > (* (int *) p2)) -
403 ((* (int *) p1) < (* (int *) p2)));
404 }
405
406 /*
407 * The classic scandir() is more expensive and less portable.
408 * We needn't full loop device names -- loop numbers (loop<N>)
409 * are enough.
410 */
411 static int loop_scandir(const char *dirname, int **ary, int hasprefix)
412 {
413 DIR *dir;
414 struct dirent *d;
415 unsigned int n, count = 0, arylen = 0;
416
417 if (!dirname || !ary)
418 return 0;
419
420 DBG(ITER, ul_debug("scan dir: %s", dirname));
421
422 dir = opendir(dirname);
423 if (!dir)
424 return 0;
425 free(*ary);
426 *ary = NULL;
427
428 while((d = readdir(dir))) {
429 #ifdef _DIRENT_HAVE_D_TYPE
430 if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN &&
431 d->d_type != DT_LNK)
432 continue;
433 #endif
434 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
435 continue;
436
437 if (hasprefix) {
438 /* /dev/loop<N> */
439 if (sscanf(d->d_name, "loop%u", &n) != 1)
440 continue;
441 } else {
442 /* /dev/loop/<N> */
443 char *end = NULL;
444
445 errno = 0;
446 n = strtol(d->d_name, &end, 10);
447 if (d->d_name == end || (end && *end) || errno)
448 continue;
449 }
450 if (n < LOOPDEV_DEFAULT_NNODES)
451 continue; /* ignore loop<0..7> */
452
453 if (count + 1 > arylen) {
454 int *tmp;
455
456 arylen += 1;
457
458 tmp = realloc(*ary, arylen * sizeof(int));
459 if (!tmp) {
460 free(*ary);
461 *ary = NULL;
462 closedir(dir);
463 return -1;
464 }
465 *ary = tmp;
466 }
467 if (*ary)
468 (*ary)[count++] = n;
469 }
470 if (count && *ary)
471 qsort(*ary, count, sizeof(int), cmpnum);
472
473 closedir(dir);
474 return count;
475 }
476
477 /*
478 * Set the next *used* loop device according to /proc/partitions.
479 *
480 * Loop devices smaller than 512 bytes are invisible for this function.
481 */
482 static int loopcxt_next_from_proc(struct loopdev_cxt *lc)
483 {
484 struct loopdev_iter *iter = &lc->iter;
485 char buf[BUFSIZ];
486
487 DBG(ITER, ul_debugobj(iter, "scan /proc/partitions"));
488
489 if (!iter->proc)
490 iter->proc = fopen(_PATH_PROC_PARTITIONS, "r" UL_CLOEXECSTR);
491 if (!iter->proc)
492 return 1;
493
494 while (fgets(buf, sizeof(buf), iter->proc)) {
495 unsigned int m;
496 char name[128 + 1];
497
498
499 if (sscanf(buf, " %u %*s %*s %128[^\n ]",
500 &m, name) != 2 || m != LOOPDEV_MAJOR)
501 continue;
502
503 DBG(ITER, ul_debugobj(iter, "checking %s", name));
504
505 if (loopiter_set_device(lc, name) == 0)
506 return 0;
507 }
508
509 return 1;
510 }
511
512 /*
513 * Set the next *used* loop device according to
514 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
515 *
516 * This is preferred method.
517 */
518 static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc)
519 {
520 struct loopdev_iter *iter = &lc->iter;
521 struct dirent *d;
522 int fd;
523
524 DBG(ITER, ul_debugobj(iter, "scanning /sys/block"));
525
526 if (!iter->sysblock)
527 iter->sysblock = opendir(_PATH_SYS_BLOCK);
528
529 if (!iter->sysblock)
530 return 1;
531
532 fd = dirfd(iter->sysblock);
533
534 while ((d = readdir(iter->sysblock))) {
535 char name[NAME_MAX + 18 + 1];
536 struct stat st;
537
538 DBG(ITER, ul_debugobj(iter, "check %s", d->d_name));
539
540 if (strcmp(d->d_name, ".") == 0
541 || strcmp(d->d_name, "..") == 0
542 || strncmp(d->d_name, "loop", 4) != 0)
543 continue;
544
545 snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
546 if (fstatat(fd, name, &st, 0) != 0)
547 continue;
548
549 if (loopiter_set_device(lc, d->d_name) == 0)
550 return 0;
551 }
552
553 return 1;
554 }
555
556 /*
557 * @lc: context, has to initialized by loopcxt_init_iterator()
558 *
559 * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
560 * about the current loop device are available by
561 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
562 */
563 int loopcxt_next(struct loopdev_cxt *lc)
564 {
565 struct loopdev_iter *iter;
566
567 if (!lc)
568 return -EINVAL;
569
570
571 iter = &lc->iter;
572 if (iter->done)
573 return 1;
574
575 DBG(ITER, ul_debugobj(iter, "next"));
576
577 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
578 */
579 if (iter->flags & LOOPITER_FL_USED) {
580 int rc;
581
582 if (loopcxt_sysfs_available(lc))
583 rc = loopcxt_next_from_sysfs(lc);
584 else
585 rc = loopcxt_next_from_proc(lc);
586 if (rc == 0)
587 return 0;
588 goto done;
589 }
590
591 /* B) Classic way, try first eight loop devices (default number
592 * of loop devices). This is enough for 99% of all cases.
593 */
594 if (iter->default_check) {
595 DBG(ITER, ul_debugobj(iter, "next: default check"));
596 for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES;
597 iter->ncur++) {
598 char name[16];
599 snprintf(name, sizeof(name), "loop%d", iter->ncur);
600
601 if (loopiter_set_device(lc, name) == 0)
602 return 0;
603 }
604 iter->default_check = 0;
605 }
606
607 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
608 */
609 if (!iter->minors) {
610 DBG(ITER, ul_debugobj(iter, "next: scanning /dev"));
611 iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ?
612 loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) :
613 loop_scandir(_PATH_DEV, &iter->minors, 1);
614 iter->ncur = -1;
615 }
616 for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) {
617 char name[16];
618 snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]);
619
620 if (loopiter_set_device(lc, name) == 0)
621 return 0;
622 }
623 done:
624 loopcxt_deinit_iterator(lc);
625 return 1;
626 }
627
628 /*
629 * @device: path to device
630 */
631 int is_loopdev(const char *device)
632 {
633 struct stat st;
634
635 if (device && stat(device, &st) == 0 &&
636 S_ISBLK(st.st_mode) &&
637 major(st.st_rdev) == LOOPDEV_MAJOR)
638 return 1;
639
640 errno = ENODEV;
641 return 0;
642 }
643
644 /*
645 * @lc: context
646 *
647 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
648 */
649 struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc)
650 {
651 int fd;
652
653 if (!lc || lc->info_failed) {
654 errno = EINVAL;
655 return NULL;
656 }
657 errno = 0;
658 if (lc->has_info)
659 return &lc->info;
660
661 fd = loopcxt_get_fd(lc);
662 if (fd < 0)
663 return NULL;
664
665 if (ioctl(fd, LOOP_GET_STATUS64, &lc->info) == 0) {
666 lc->has_info = 1;
667 lc->info_failed = 0;
668 DBG(CXT, ul_debugobj(lc, "reading loop_info64 OK"));
669 return &lc->info;
670 }
671
672 lc->info_failed = 1;
673 DBG(CXT, ul_debugobj(lc, "reading loop_info64 FAILED"));
674
675 return NULL;
676 }
677
678 /*
679 * @lc: context
680 *
681 * Returns (allocated) string with path to the file assicieted
682 * with the current loop device.
683 */
684 char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
685 {
686 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
687 char *res = NULL;
688
689 if (sysfs)
690 /*
691 * This is always preffered, the loop_info64
692 * has too small buffer for the filename.
693 */
694 res = sysfs_strdup(sysfs, "loop/backing_file");
695
696 if (!res && loopcxt_ioctl_enabled(lc)) {
697 struct loop_info64 *lo = loopcxt_get_info(lc);
698
699 if (lo) {
700 lo->lo_file_name[LO_NAME_SIZE - 2] = '*';
701 lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
702 res = strdup((char *) lo->lo_file_name);
703 }
704 }
705
706 DBG(CXT, ul_debugobj(lc, "get_backing_file [%s]", res));
707 return res;
708 }
709
710 /*
711 * @lc: context
712 * @offset: returns offset number for the given device
713 *
714 * Returns: <0 on error, 0 on success
715 */
716 int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset)
717 {
718 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
719 int rc = -EINVAL;
720
721 if (sysfs)
722 rc = sysfs_read_u64(sysfs, "loop/offset", offset);
723
724 if (rc && loopcxt_ioctl_enabled(lc)) {
725 struct loop_info64 *lo = loopcxt_get_info(lc);
726 if (lo) {
727 if (offset)
728 *offset = lo->lo_offset;
729 rc = 0;
730 } else
731 rc = -errno;
732 }
733
734 DBG(CXT, ul_debugobj(lc, "get_offset [rc=%d]", rc));
735 return rc;
736 }
737
738 /*
739 * @lc: context
740 * @sizelimit: returns size limit for the given device
741 *
742 * Returns: <0 on error, 0 on success
743 */
744 int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size)
745 {
746 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
747 int rc = -EINVAL;
748
749 if (sysfs)
750 rc = sysfs_read_u64(sysfs, "loop/sizelimit", size);
751
752 if (rc && loopcxt_ioctl_enabled(lc)) {
753 struct loop_info64 *lo = loopcxt_get_info(lc);
754 if (lo) {
755 if (size)
756 *size = lo->lo_sizelimit;
757 rc = 0;
758 } else
759 rc = -errno;
760 }
761
762 DBG(CXT, ul_debugobj(lc, "get_sizelimit [rc=%d]", rc));
763 return rc;
764 }
765
766 /*
767 * @lc: context
768 * @devno: returns encryption type
769 *
770 * Cryptoloop is DEPRECATED!
771 *
772 * Returns: <0 on error, 0 on success
773 */
774 int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type)
775 {
776 struct loop_info64 *lo = loopcxt_get_info(lc);
777 int rc;
778
779 /* not provided by sysfs */
780 if (lo) {
781 if (type)
782 *type = lo->lo_encrypt_type;
783 rc = 0;
784 } else
785 rc = -errno;
786
787 DBG(CXT, ul_debugobj(lc, "get_encrypt_type [rc=%d]", rc));
788 return rc;
789 }
790
791 /*
792 * @lc: context
793 * @devno: returns crypt name
794 *
795 * Cryptoloop is DEPRECATED!
796 *
797 * Returns: <0 on error, 0 on success
798 */
799 const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc)
800 {
801 struct loop_info64 *lo = loopcxt_get_info(lc);
802
803 if (lo)
804 return (char *) lo->lo_crypt_name;
805
806 DBG(CXT, ul_debugobj(lc, "get_crypt_name failed"));
807 return NULL;
808 }
809
810 /*
811 * @lc: context
812 * @devno: returns backing file devno
813 *
814 * Returns: <0 on error, 0 on success
815 */
816 int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno)
817 {
818 struct loop_info64 *lo = loopcxt_get_info(lc);
819 int rc;
820
821 if (lo) {
822 if (devno)
823 *devno = lo->lo_device;
824 rc = 0;
825 } else
826 rc = -errno;
827
828 DBG(CXT, ul_debugobj(lc, "get_backing_devno [rc=%d]", rc));
829 return rc;
830 }
831
832 /*
833 * @lc: context
834 * @ino: returns backing file inode
835 *
836 * Returns: <0 on error, 0 on success
837 */
838 int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino)
839 {
840 struct loop_info64 *lo = loopcxt_get_info(lc);
841 int rc;
842
843 if (lo) {
844 if (ino)
845 *ino = lo->lo_inode;
846 rc = 0;
847 } else
848 rc = -errno;
849
850 DBG(CXT, ul_debugobj(lc, "get_backing_inode [rc=%d]", rc));
851 return rc;
852 }
853
854 /*
855 * Check if the kernel supports partitioned loop devices.
856 *
857 * Notes:
858 * - kernels < 3.2 support partitioned loop devices and PT scanning
859 * only if max_part= module parameter is non-zero
860 *
861 * - kernels >= 3.2 always support partitioned loop devices
862 *
863 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
864 *
865 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
866 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
867 * by default.
868 *
869 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
870 */
871 int loopmod_supports_partscan(void)
872 {
873 int rc, ret = 0;
874 FILE *f;
875
876 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
877 return 1;
878
879 f = fopen("/sys/module/loop/parameters/max_part", "r" UL_CLOEXECSTR);
880 if (!f)
881 return 0;
882 rc = fscanf(f, "%d", &ret);
883 fclose(f);
884 return rc == 1 ? ret : 0;
885 }
886
887 /*
888 * @lc: context
889 *
890 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
891 * scanning is enabled for all loop devices.
892 */
893 int loopcxt_is_partscan(struct loopdev_cxt *lc)
894 {
895 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
896
897 if (sysfs) {
898 /* kernel >= 3.2 */
899 int fl;
900 if (sysfs_read_int(sysfs, "loop/partscan", &fl) == 0)
901 return fl;
902 }
903
904 /* old kernels (including kernels without loopN/loop/<flags> directory */
905 return loopmod_supports_partscan();
906 }
907
908 /*
909 * @lc: context
910 *
911 * Returns: 1 if the autoclear flags is set.
912 */
913 int loopcxt_is_autoclear(struct loopdev_cxt *lc)
914 {
915 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
916
917 if (sysfs) {
918 int fl;
919 if (sysfs_read_int(sysfs, "loop/autoclear", &fl) == 0)
920 return fl;
921 }
922
923 if (loopcxt_ioctl_enabled(lc)) {
924 struct loop_info64 *lo = loopcxt_get_info(lc);
925 if (lo)
926 return lo->lo_flags & LO_FLAGS_AUTOCLEAR;
927 }
928 return 0;
929 }
930
931 /*
932 * @lc: context
933 *
934 * Returns: 1 if the readonly flags is set.
935 */
936 int loopcxt_is_readonly(struct loopdev_cxt *lc)
937 {
938 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
939
940 if (sysfs) {
941 int fl;
942 if (sysfs_read_int(sysfs, "ro", &fl) == 0)
943 return fl;
944 }
945
946 if (loopcxt_ioctl_enabled(lc)) {
947 struct loop_info64 *lo = loopcxt_get_info(lc);
948 if (lo)
949 return lo->lo_flags & LO_FLAGS_READ_ONLY;
950 }
951 return 0;
952 }
953
954 /*
955 * @lc: context
956 *
957 * Returns: 1 if the dio flags is set.
958 */
959 int loopcxt_is_dio(struct loopdev_cxt *lc)
960 {
961 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
962
963 if (sysfs) {
964 int fl;
965 if (sysfs_read_int(sysfs, "loop/dio", &fl) == 0)
966 return fl;
967 }
968 if (loopcxt_ioctl_enabled(lc)) {
969 struct loop_info64 *lo = loopcxt_get_info(lc);
970 if (lo)
971 return lo->lo_flags & LO_FLAGS_DIRECT_IO;
972 }
973 return 0;
974 }
975
976 /*
977 * @lc: context
978 * @st: backing file stat or NULL
979 * @backing_file: filename
980 * @offset: offset (use LOOPDEV_FL_OFFSET if specified)
981 * @sizelimit: size limit (use LOOPDEV_FL_SIZELIMIT if specified)
982 * @flags: LOOPDEV_FL_{OFFSET,SIZELIMIT}
983 *
984 * Returns 1 if the current @lc loopdev is associated with the given backing
985 * file. Note that the preferred way is to use devno and inode number rather
986 * than filename. The @backing_file filename is poor solution usable in case
987 * that you don't have rights to call stat().
988 *
989 * LOOPDEV_FL_SIZELIMIT requires LOOPDEV_FL_OFFSET being set as well.
990 *
991 * Don't forget that old kernels provide very restricted (in size) backing
992 * filename by LOOP_GET_STAT64 ioctl only.
993 */
994 int loopcxt_is_used(struct loopdev_cxt *lc,
995 struct stat *st,
996 const char *backing_file,
997 uint64_t offset,
998 uint64_t sizelimit,
999 int flags)
1000 {
1001 ino_t ino;
1002 dev_t dev;
1003
1004 if (!lc)
1005 return 0;
1006
1007 DBG(CXT, ul_debugobj(lc, "checking %s vs. %s",
1008 loopcxt_get_device(lc),
1009 backing_file));
1010
1011 if (st && loopcxt_get_backing_inode(lc, &ino) == 0 &&
1012 loopcxt_get_backing_devno(lc, &dev) == 0) {
1013
1014 if (ino == st->st_ino && dev == st->st_dev)
1015 goto found;
1016
1017 /* don't use filename if we have devno and inode */
1018 return 0;
1019 }
1020
1021 /* poor man's solution */
1022 if (backing_file) {
1023 char *name = loopcxt_get_backing_file(lc);
1024 int rc = name && strcmp(name, backing_file) == 0;
1025
1026 free(name);
1027 if (rc)
1028 goto found;
1029 }
1030
1031 return 0;
1032 found:
1033 if (flags & LOOPDEV_FL_OFFSET) {
1034 uint64_t off;
1035
1036 int rc = loopcxt_get_offset(lc, &off) == 0 && off == offset;
1037
1038 if (rc && flags & LOOPDEV_FL_SIZELIMIT) {
1039 uint64_t sz;
1040
1041 return loopcxt_get_sizelimit(lc, &sz) == 0 && sz == sizelimit;
1042 } else
1043 return rc;
1044 }
1045 return 1;
1046 }
1047
1048 /*
1049 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1050 */
1051 int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset)
1052 {
1053 if (!lc)
1054 return -EINVAL;
1055 lc->info.lo_offset = offset;
1056
1057 DBG(CXT, ul_debugobj(lc, "set offset=%jd", offset));
1058 return 0;
1059 }
1060
1061 /*
1062 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1063 */
1064 int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit)
1065 {
1066 if (!lc)
1067 return -EINVAL;
1068 lc->info.lo_sizelimit = sizelimit;
1069
1070 DBG(CXT, ul_debugobj(lc, "set sizelimit=%jd", sizelimit));
1071 return 0;
1072 }
1073
1074 /*
1075 * @lc: context
1076 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1077 *
1078 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1079 *
1080 * Returns: 0 on success, <0 on error.
1081 */
1082 int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags)
1083 {
1084 if (!lc)
1085 return -EINVAL;
1086 lc->info.lo_flags = flags;
1087
1088 DBG(CXT, ul_debugobj(lc, "set flags=%u", (unsigned) flags));
1089 return 0;
1090 }
1091
1092 /*
1093 * @lc: context
1094 * @filename: backing file path (the path will be canonicalized)
1095 *
1096 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1097 *
1098 * Returns: 0 on success, <0 on error.
1099 */
1100 int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename)
1101 {
1102 if (!lc)
1103 return -EINVAL;
1104
1105 lc->filename = canonicalize_path(filename);
1106 if (!lc->filename)
1107 return -errno;
1108
1109 strncpy((char *)lc->info.lo_file_name, lc->filename, LO_NAME_SIZE);
1110 lc->info.lo_file_name[LO_NAME_SIZE- 1] = '\0';
1111
1112 DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->info.lo_file_name));
1113 return 0;
1114 }
1115
1116 /*
1117 * In kernels prior to v3.9, if the offset or sizelimit options
1118 * are used, the block device's size won't be synced automatically.
1119 * blockdev --getsize64 and filesystems will use the backing
1120 * file size until the block device has been re-opened or the
1121 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1122 *
1123 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1124 * the open file descriptor to the mount system call, we need to use
1125 * the ioctl. Calling losetup directly doesn't have this problem since
1126 * it closes the device when it exits and whatever consumes the device
1127 * next will re-open it, causing the resync.
1128 */
1129 static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
1130 {
1131 uint64_t size, expected_size;
1132 int dev_fd;
1133 struct stat st;
1134
1135 if (!lc->info.lo_offset && !lc->info.lo_sizelimit)
1136 return 0;
1137
1138 if (fstat(file_fd, &st)) {
1139 DBG(CXT, ul_debugobj(lc, "failed to fstat backing file"));
1140 return -errno;
1141 }
1142 if (S_ISBLK(st.st_mode)) {
1143 if (blkdev_get_size(file_fd,
1144 (unsigned long long *) &expected_size)) {
1145 DBG(CXT, ul_debugobj(lc, "failed to determine device size"));
1146 return -errno;
1147 }
1148 } else
1149 expected_size = st.st_size;
1150
1151 if (expected_size == 0 || expected_size <= lc->info.lo_offset) {
1152 DBG(CXT, ul_debugobj(lc, "failed to determine expected size"));
1153 return 0; /* ignore this error */
1154 }
1155
1156 if (lc->info.lo_offset > 0)
1157 expected_size -= lc->info.lo_offset;
1158
1159 if (lc->info.lo_sizelimit > 0 && lc->info.lo_sizelimit < expected_size)
1160 expected_size = lc->info.lo_sizelimit;
1161
1162 dev_fd = loopcxt_get_fd(lc);
1163 if (dev_fd < 0) {
1164 DBG(CXT, ul_debugobj(lc, "failed to get loop FD"));
1165 return -errno;
1166 }
1167
1168 if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) {
1169 DBG(CXT, ul_debugobj(lc, "failed to determine loopdev size"));
1170 return -errno;
1171 }
1172
1173 /* It's block device, so, align to 512-byte sectors */
1174 if (expected_size % 512) {
1175 DBG(CXT, ul_debugobj(lc, "expected size misaligned to 512-byte sectors"));
1176 expected_size = (expected_size >> 9) << 9;
1177 }
1178
1179 if (expected_size != size) {
1180 DBG(CXT, ul_debugobj(lc, "warning: loopdev and expected "
1181 "size mismatch (%ju/%ju)",
1182 size, expected_size));
1183
1184 if (loopcxt_set_capacity(lc)) {
1185 /* ioctl not available */
1186 if (errno == ENOTTY || errno == EINVAL)
1187 errno = ERANGE;
1188 return -errno;
1189 }
1190
1191 if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
1192 return -errno;
1193
1194 if (expected_size != size) {
1195 errno = ERANGE;
1196 DBG(CXT, ul_debugobj(lc, "failed to set loopdev size, "
1197 "size: %ju, expected: %ju",
1198 size, expected_size));
1199 return -errno;
1200 }
1201 }
1202
1203 return 0;
1204 }
1205
1206 /*
1207 * @lc: context
1208 *
1209 * Associate the current device (see loopcxt_{set,get}_device()) with
1210 * a file (see loopcxt_set_backing_file()).
1211 *
1212 * The device is initialized read-write by default. If you want read-only
1213 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1214 * flags are ignored and modified according to LO_FLAGS_*.
1215 *
1216 * If the device is already open by loopcxt_get_fd() then this setup device
1217 * function will re-open the device to fix read/write mode.
1218 *
1219 * The device is also initialized read-only if the backing file is not
1220 * possible to open read-write (e.g. read-only FS).
1221 *
1222 * Returns: <0 on error, 0 on success.
1223 */
1224 int loopcxt_setup_device(struct loopdev_cxt *lc)
1225 {
1226 int file_fd, dev_fd, mode = O_RDWR, rc = -1, cnt = 0;
1227 int errsv = 0;
1228
1229 if (!lc || !*lc->device || !lc->filename)
1230 return -EINVAL;
1231
1232 DBG(SETUP, ul_debugobj(lc, "device setup requested"));
1233
1234 /*
1235 * Open backing file and device
1236 */
1237 if (lc->info.lo_flags & LO_FLAGS_READ_ONLY)
1238 mode = O_RDONLY;
1239
1240 if ((file_fd = open(lc->filename, mode | O_CLOEXEC)) < 0) {
1241 if (mode != O_RDONLY && (errno == EROFS || errno == EACCES))
1242 file_fd = open(lc->filename, mode = O_RDONLY);
1243
1244 if (file_fd < 0) {
1245 DBG(SETUP, ul_debugobj(lc, "open backing file failed: %m"));
1246 return -errno;
1247 }
1248 }
1249 DBG(SETUP, ul_debugobj(lc, "backing file open: OK"));
1250
1251 if (lc->fd != -1 && lc->mode != mode) {
1252 DBG(SETUP, ul_debugobj(lc, "closing already open device (mode mismatch)"));
1253 close(lc->fd);
1254 lc->fd = -1;
1255 lc->mode = 0;
1256 }
1257
1258 if (mode == O_RDONLY) {
1259 lc->flags |= LOOPDEV_FL_RDONLY; /* open() mode */
1260 lc->info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */
1261 } else {
1262 lc->flags |= LOOPDEV_FL_RDWR; /* open() mode */
1263 lc->info.lo_flags &= ~LO_FLAGS_READ_ONLY;
1264 lc->flags &= ~LOOPDEV_FL_RDONLY;
1265 }
1266
1267 do {
1268 errno = 0;
1269 dev_fd = loopcxt_get_fd(lc);
1270 if (dev_fd >= 0 || lc->control_ok == 0)
1271 break;
1272 if (errno != EACCES && errno != ENOENT)
1273 break;
1274 /* We have permissions to open /dev/loop-control, but open
1275 * /dev/loopN failed with EACCES, it's probably because udevd
1276 * does not applied chown yet. Let's wait a moment. */
1277 xusleep(25000);
1278 } while (cnt++ < 16);
1279
1280 if (dev_fd < 0) {
1281 rc = -errno;
1282 goto err;
1283 }
1284
1285 DBG(SETUP, ul_debugobj(lc, "device open: OK"));
1286
1287 /*
1288 * Set FD
1289 */
1290 if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
1291 rc = -errno;
1292 errsv = errno;
1293 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD failed: %m"));
1294 goto err;
1295 }
1296
1297 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD: OK"));
1298
1299 if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) {
1300 rc = -errno;
1301 errsv = errno;
1302 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
1303 goto err;
1304 }
1305
1306 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64: OK"));
1307
1308 if ((rc = loopcxt_check_size(lc, file_fd)))
1309 goto err;
1310
1311 close(file_fd);
1312
1313 memset(&lc->info, 0, sizeof(lc->info));
1314 lc->has_info = 0;
1315 lc->info_failed = 0;
1316
1317 DBG(SETUP, ul_debugobj(lc, "success [rc=0]"));
1318 return 0;
1319 err:
1320 if (file_fd >= 0)
1321 close(file_fd);
1322 if (dev_fd >= 0 && rc != -EBUSY)
1323 ioctl(dev_fd, LOOP_CLR_FD, 0);
1324 if (errsv)
1325 errno = errsv;
1326
1327 DBG(SETUP, ul_debugobj(lc, "failed [rc=%d]", rc));
1328 return rc;
1329 }
1330
1331 /*
1332 * @lc: context
1333 *
1334 * Update status of the current device (see loopcxt_{set,get}_device()).
1335 *
1336 * Note that once initialized, kernel accepts only selected changes:
1337 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1338 * For more see linux/drivers/block/loop.c:loop_set_status()
1339 *
1340 * Returns: <0 on error, 0 on success.
1341 */
1342 int loopcxt_set_status(struct loopdev_cxt *lc)
1343 {
1344 int dev_fd, rc = -1;
1345
1346 errno = 0;
1347 dev_fd = loopcxt_get_fd(lc);
1348
1349 if (dev_fd < 0) {
1350 rc = -errno;
1351 return rc;
1352 }
1353 DBG(SETUP, ul_debugobj(lc, "device open: OK"));
1354
1355 if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) {
1356 rc = -errno;
1357 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
1358 return rc;
1359 }
1360
1361 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64: OK"));
1362 return 0;
1363 }
1364
1365 int loopcxt_set_capacity(struct loopdev_cxt *lc)
1366 {
1367 int fd = loopcxt_get_fd(lc);
1368
1369 if (fd < 0)
1370 return -EINVAL;
1371
1372 /* Kernels prior to v2.6.30 don't support this ioctl */
1373 if (ioctl(fd, LOOP_SET_CAPACITY, 0) < 0) {
1374 int rc = -errno;
1375 DBG(CXT, ul_debugobj(lc, "LOOP_SET_CAPACITY failed: %m"));
1376 return rc;
1377 }
1378
1379 DBG(CXT, ul_debugobj(lc, "capacity set"));
1380 return 0;
1381 }
1382
1383 int loopcxt_set_dio(struct loopdev_cxt *lc, unsigned long use_dio)
1384 {
1385 int fd = loopcxt_get_fd(lc);
1386
1387 if (fd < 0)
1388 return -EINVAL;
1389
1390 /* Kernels prior to v4.4 don't support this ioctl */
1391 if (ioctl(fd, LOOP_SET_DIRECT_IO, use_dio) < 0) {
1392 int rc = -errno;
1393 DBG(CXT, ul_debugobj(lc, "LOOP_SET_DIRECT_IO failed: %m"));
1394 return rc;
1395 }
1396
1397 DBG(CXT, ul_debugobj(lc, "direct io set"));
1398 return 0;
1399 }
1400
1401 int loopcxt_delete_device(struct loopdev_cxt *lc)
1402 {
1403 int fd = loopcxt_get_fd(lc);
1404
1405 if (fd < 0)
1406 return -EINVAL;
1407
1408 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1409 DBG(CXT, ul_debugobj(lc, "LOOP_CLR_FD failed: %m"));
1410 return -errno;
1411 }
1412
1413 DBG(CXT, ul_debugobj(lc, "device removed"));
1414 return 0;
1415 }
1416
1417 int loopcxt_add_device(struct loopdev_cxt *lc)
1418 {
1419 int rc = -EINVAL;
1420 int ctl, nr = -1;
1421 const char *p, *dev = loopcxt_get_device(lc);
1422
1423 if (!dev)
1424 goto done;
1425
1426 if (!(lc->flags & LOOPDEV_FL_CONTROL)) {
1427 rc = -ENOSYS;
1428 goto done;
1429 }
1430
1431 p = strrchr(dev, '/');
1432 if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1)
1433 || nr < 0)
1434 goto done;
1435
1436 ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1437 if (ctl >= 0) {
1438 DBG(CXT, ul_debugobj(lc, "add_device %d", nr));
1439 rc = ioctl(ctl, LOOP_CTL_ADD, nr);
1440 close(ctl);
1441 }
1442 lc->control_ok = rc >= 0 ? 1 : 0;
1443 done:
1444 DBG(CXT, ul_debugobj(lc, "add_device done [rc=%d]", rc));
1445 return rc;
1446 }
1447
1448 /*
1449 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1450 * kernels we have to check all loop devices to found unused one.
1451 *
1452 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1453 */
1454 int loopcxt_find_unused(struct loopdev_cxt *lc)
1455 {
1456 int rc = -1;
1457
1458 DBG(CXT, ul_debugobj(lc, "find_unused requested"));
1459
1460 if (lc->flags & LOOPDEV_FL_CONTROL) {
1461 int ctl;
1462
1463 DBG(CXT, ul_debugobj(lc, "using loop-control"));
1464
1465 ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1466 if (ctl >= 0)
1467 rc = ioctl(ctl, LOOP_CTL_GET_FREE);
1468 if (rc >= 0) {
1469 char name[16];
1470 snprintf(name, sizeof(name), "loop%d", rc);
1471
1472 rc = loopiter_set_device(lc, name);
1473 }
1474 lc->control_ok = ctl >= 0 && rc == 0 ? 1 : 0;
1475 if (ctl >= 0)
1476 close(ctl);
1477 DBG(CXT, ul_debugobj(lc, "find_unused by loop-control [rc=%d]", rc));
1478 }
1479
1480 if (rc < 0) {
1481 DBG(CXT, ul_debugobj(lc, "using loop scan"));
1482 rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE);
1483 if (rc)
1484 return rc;
1485
1486 rc = loopcxt_next(lc);
1487 loopcxt_deinit_iterator(lc);
1488 DBG(CXT, ul_debugobj(lc, "find_unused by scan [rc=%d]", rc));
1489 }
1490 return rc;
1491 }
1492
1493
1494
1495 /*
1496 * Return: TRUE/FALSE
1497 */
1498 int loopdev_is_autoclear(const char *device)
1499 {
1500 struct loopdev_cxt lc;
1501 int rc;
1502
1503 if (!device)
1504 return 0;
1505
1506 rc = loopcxt_init(&lc, 0);
1507 if (!rc)
1508 rc = loopcxt_set_device(&lc, device);
1509 if (!rc)
1510 rc = loopcxt_is_autoclear(&lc);
1511
1512 loopcxt_deinit(&lc);
1513 return rc;
1514 }
1515
1516 char *loopdev_get_backing_file(const char *device)
1517 {
1518 struct loopdev_cxt lc;
1519 char *res = NULL;
1520
1521 if (!device)
1522 return NULL;
1523 if (loopcxt_init(&lc, 0))
1524 return NULL;
1525 if (loopcxt_set_device(&lc, device) == 0)
1526 res = loopcxt_get_backing_file(&lc);
1527
1528 loopcxt_deinit(&lc);
1529 return res;
1530 }
1531
1532 /*
1533 * Returns: TRUE/FALSE
1534 */
1535 int loopdev_is_used(const char *device, const char *filename,
1536 uint64_t offset, uint64_t sizelimit, int flags)
1537 {
1538 struct loopdev_cxt lc;
1539 struct stat st;
1540 int rc = 0;
1541
1542 if (!device || !filename)
1543 return 0;
1544
1545 rc = loopcxt_init(&lc, 0);
1546 if (!rc)
1547 rc = loopcxt_set_device(&lc, device);
1548 if (rc)
1549 return rc;
1550
1551 rc = !stat(filename, &st);
1552 rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, sizelimit, flags);
1553
1554 loopcxt_deinit(&lc);
1555 return rc;
1556 }
1557
1558 int loopdev_delete(const char *device)
1559 {
1560 struct loopdev_cxt lc;
1561 int rc;
1562
1563 if (!device)
1564 return -EINVAL;
1565
1566 rc = loopcxt_init(&lc, 0);
1567 if (!rc)
1568 rc = loopcxt_set_device(&lc, device);
1569 if (!rc)
1570 rc = loopcxt_delete_device(&lc);
1571 loopcxt_deinit(&lc);
1572 return rc;
1573 }
1574
1575 /*
1576 * Returns: 0 = success, < 0 error, 1 not found
1577 */
1578 int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename,
1579 uint64_t offset, uint64_t sizelimit, int flags)
1580 {
1581 int rc, hasst;
1582 struct stat st;
1583
1584 if (!filename)
1585 return -EINVAL;
1586
1587 hasst = !stat(filename, &st);
1588
1589 rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1590 if (rc)
1591 return rc;
1592
1593 while ((rc = loopcxt_next(lc)) == 0) {
1594
1595 if (loopcxt_is_used(lc, hasst ? &st : NULL,
1596 filename, offset, sizelimit, flags))
1597 break;
1598 }
1599
1600 loopcxt_deinit_iterator(lc);
1601 return rc;
1602 }
1603
1604 /*
1605 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1606 */
1607 int loopcxt_find_overlap(struct loopdev_cxt *lc, const char *filename,
1608 uint64_t offset, uint64_t sizelimit)
1609 {
1610 int rc, hasst;
1611 struct stat st;
1612
1613 if (!filename)
1614 return -EINVAL;
1615
1616 DBG(CXT, ul_debugobj(lc, "find_overlap requested"));
1617 hasst = !stat(filename, &st);
1618
1619 rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1620 if (rc)
1621 return rc;
1622
1623 while ((rc = loopcxt_next(lc)) == 0) {
1624 uint64_t lc_sizelimit, lc_offset;
1625
1626 rc = loopcxt_is_used(lc, hasst ? &st : NULL,
1627 filename, offset, sizelimit, 0);
1628 if (!rc)
1629 continue; /* unused */
1630 if (rc < 0)
1631 break; /* error */
1632
1633 DBG(CXT, ul_debugobj(lc, "found %s backed by %s",
1634 loopcxt_get_device(lc), filename));
1635
1636 rc = loopcxt_get_offset(lc, &lc_offset);
1637 if (rc) {
1638 DBG(CXT, ul_debugobj(lc, "failed to get offset for device %s",
1639 loopcxt_get_device(lc)));
1640 break;
1641 }
1642 rc = loopcxt_get_sizelimit(lc, &lc_sizelimit);
1643 if (rc) {
1644 DBG(CXT, ul_debugobj(lc, "failed to get sizelimit for device %s",
1645 loopcxt_get_device(lc)));
1646 break;
1647 }
1648
1649 /* full match */
1650 if (lc_sizelimit == sizelimit && lc_offset == offset) {
1651 DBG(CXT, ul_debugobj(lc, "overlapping loop device %s (full match)",
1652 loopcxt_get_device(lc)));
1653 rc = 2;
1654 goto found;
1655 }
1656
1657 /* overlap */
1658 if (lc_sizelimit != 0 && offset >= lc_offset + lc_sizelimit)
1659 continue;
1660 if (sizelimit != 0 && offset + sizelimit <= lc_offset)
1661 continue;
1662
1663 DBG(CXT, ul_debugobj(lc, "overlapping loop device %s",
1664 loopcxt_get_device(lc)));
1665 rc = 1;
1666 goto found;
1667 }
1668
1669 if (rc == 1)
1670 rc = 0; /* not found */
1671 found:
1672 loopcxt_deinit_iterator(lc);
1673 DBG(CXT, ul_debugobj(lc, "find_overlap done [rc=%d]", rc));
1674 return rc;
1675 }
1676
1677 /*
1678 * Returns allocated string with device name
1679 */
1680 char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, uint64_t sizelimit, int flags)
1681 {
1682 struct loopdev_cxt lc;
1683 char *res = NULL;
1684
1685 if (!filename)
1686 return NULL;
1687
1688 if (loopcxt_init(&lc, 0))
1689 return NULL;
1690 if (loopcxt_find_by_backing_file(&lc, filename, offset, sizelimit, flags) == 0)
1691 res = loopcxt_strdup_device(&lc);
1692 loopcxt_deinit(&lc);
1693
1694 return res;
1695 }
1696
1697 /*
1698 * Returns number of loop devices associated with @file, if only one loop
1699 * device is associeted with the given @filename and @loopdev is not NULL then
1700 * @loopdev returns name of the device.
1701 */
1702 int loopdev_count_by_backing_file(const char *filename, char **loopdev)
1703 {
1704 struct loopdev_cxt lc;
1705 int count = 0, rc;
1706
1707 if (!filename)
1708 return -1;
1709
1710 rc = loopcxt_init(&lc, 0);
1711 if (rc)
1712 return rc;
1713 if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED))
1714 return -1;
1715
1716 while(loopcxt_next(&lc) == 0) {
1717 char *backing = loopcxt_get_backing_file(&lc);
1718
1719 if (!backing || strcmp(backing, filename)) {
1720 free(backing);
1721 continue;
1722 }
1723
1724 free(backing);
1725 if (loopdev && count == 0)
1726 *loopdev = loopcxt_strdup_device(&lc);
1727 count++;
1728 }
1729
1730 loopcxt_deinit(&lc);
1731
1732 if (loopdev && count > 1) {
1733 free(*loopdev);
1734 *loopdev = NULL;
1735 }
1736 return count;
1737 }
1738