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