blob: 09b9bbf75d7217627e514a4125cd0714d08a51c9 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 *
7 * -- based on mount/losetup.c
8 *
9 * Simple library for work with loop devices.
10 *
11 * - requires kernel 2.6.x
12 * - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
13 * - reads info by ioctl
14 * - supports *unlimited* number of loop devices
15 * - supports /dev/loop<N> as well as /dev/loop/<N>
16 * - minimize overhead (fd, loopinfo, ... are shared for all operations)
17 * - setup (associate device and backing file)
18 * - delete (dis-associate file)
19 * - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
20 * - extendible
21 */
22#include <stdio.h>
23#include <stdint.h>
24#include <string.h>
25#include <ctype.h>
26#include <fcntl.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <sys/ioctl.h>
30#include <sys/stat.h>
31#include <sys/mman.h>
32#include <sys/sysmacros.h>
33#include <inttypes.h>
34#include <dirent.h>
35#include <linux/posix_types.h>
36
37#include "linux_version.h"
38#include "c.h"
39#include "sysfs.h"
40#include "pathnames.h"
41#include "loopdev.h"
42#include "canonicalize.h"
43#include "at.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050044#include "blkdev.h"
45#include "debug.h"
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050046
bigbiff7b4c7a62015-01-01 19:44:14 -050047/*
48 * Debug stuff (based on include/debug.h)
49 */
50UL_DEBUG_DEFINE_MASK(loopdev);
51UL_DEBUG_DEFINE_MASKNAMES(loopdev) = UL_DEBUG_EMPTY_MASKNAMES;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050052
bigbiff7b4c7a62015-01-01 19:44:14 -050053#define LOOPDEV_DEBUG_INIT (1 << 1)
54#define LOOPDEV_DEBUG_CXT (1 << 2)
55#define LOOPDEV_DEBUG_ITER (1 << 3)
56#define LOOPDEV_DEBUG_SETUP (1 << 4)
57#define SFDISKPROG_DEBUG_ALL 0xFFFF
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050058
bigbiff7b4c7a62015-01-01 19:44:14 -050059#define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
60#define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050061
bigbiff7b4c7a62015-01-01 19:44:14 -050062static void loopdev_init_debug(void)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050063{
bigbiff7b4c7a62015-01-01 19:44:14 -050064 if (loopdev_debug_mask)
65 return;
66 __UL_INIT_DEBUG(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050067}
68
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050069/*
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 *
bigbiff7b4c7a62015-01-01 19:44:14 -050084 * This sets the device name, but does not check if the device exists!
85 *
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050086 * Returns: <0 on error, 0 on success
87 */
88int 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);
bigbiff7b4c7a62015-01-01 19:44:14 -050095 DBG(CXT, ul_debugobj(lc, "closing old open fd"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050096 }
97 lc->fd = -1;
98 lc->mode = 0;
99 lc->has_info = 0;
100 lc->info_failed = 0;
101 *lc->device = '\0';
102 memset(&lc->info, 0, sizeof(lc->info));
103
104 /* set new */
105 if (device) {
106 if (*device != '/') {
107 const char *dir = _PATH_DEV;
108
109 /* compose device name for /dev/loop<n> or /dev/loop/<n> */
110 if (lc->flags & LOOPDEV_FL_DEVSUBDIR) {
111 if (strlen(device) < 5)
112 return -1;
113 device += 4;
114 dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */
115 }
116 snprintf(lc->device, sizeof(lc->device), "%s%s",
117 dir, device);
118 } else {
119 strncpy(lc->device, device, sizeof(lc->device));
120 lc->device[sizeof(lc->device) - 1] = '\0';
121 }
bigbiff7b4c7a62015-01-01 19:44:14 -0500122 DBG(CXT, ul_debugobj(lc, "%s name assigned", device));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500123 }
124
125 sysfs_deinit(&lc->sysfs);
126 return 0;
127}
128
129int loopcxt_has_device(struct loopdev_cxt *lc)
130{
131 return lc && *lc->device;
132}
133
134/*
135 * @lc: context
136 * @flags: LOOPDEV_FL_* flags
137 *
138 * Initilize loop handler.
139 *
140 * We have two sets of the flags:
141 *
142 * * LOOPDEV_FL_* flags control loopcxt_* API behavior
143 *
144 * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
145 *
146 * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
147 * syscall to open loop device. By default is the device open read-only.
148 *
149 * The expection is loopcxt_setup_device(), where the device is open read-write
150 * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
151 *
152 * Returns: <0 on error, 0 on success.
153 */
154int loopcxt_init(struct loopdev_cxt *lc, int flags)
155{
156 int rc;
157 struct stat st;
158 struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY;
159
160 if (!lc)
161 return -EINVAL;
162
bigbiff7b4c7a62015-01-01 19:44:14 -0500163 loopdev_init_debug();
164 DBG(CXT, ul_debugobj(lc, "initialize context"));
165
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500166 memcpy(lc, &dummy, sizeof(dummy));
167 lc->flags = flags;
168
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500169 rc = loopcxt_set_device(lc, NULL);
170 if (rc)
171 return rc;
172
173 if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) {
174 lc->flags |= LOOPDEV_FL_NOSYSFS;
175 lc->flags &= ~LOOPDEV_FL_NOIOCTL;
bigbiff7b4c7a62015-01-01 19:44:14 -0500176 DBG(CXT, ul_debugobj(lc, "init: disable /sys usage"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500177 }
178
179 if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
180 get_linux_version() >= KERNEL_VERSION(2,6,37)) {
181 /*
182 * Use only sysfs for basic information about loop devices
183 */
184 lc->flags |= LOOPDEV_FL_NOIOCTL;
bigbiff7b4c7a62015-01-01 19:44:14 -0500185 DBG(CXT, ul_debugobj(lc, "init: ignore ioctls"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500186 }
187
188 if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) {
189 lc->flags |= LOOPDEV_FL_CONTROL;
bigbiff7b4c7a62015-01-01 19:44:14 -0500190 DBG(CXT, ul_debugobj(lc, "init: loop-control detected "));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500191 }
192
193 return 0;
194}
195
196/*
197 * @lc: context
198 *
199 * Deinitialize loop context
200 */
201void loopcxt_deinit(struct loopdev_cxt *lc)
202{
203 int errsv = errno;
204
205 if (!lc)
206 return;
207
bigbiff7b4c7a62015-01-01 19:44:14 -0500208 DBG(CXT, ul_debugobj(lc, "de-initialize"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500209
210 free(lc->filename);
211 lc->filename = NULL;
212
213 ignore_result( loopcxt_set_device(lc, NULL) );
214 loopcxt_deinit_iterator(lc);
215
216 errno = errsv;
217}
218
219/*
220 * @lc: context
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500221 *
222 * Returns newly allocated device path.
223 */
224char *loopcxt_strdup_device(struct loopdev_cxt *lc)
225{
bigbiff7b4c7a62015-01-01 19:44:14 -0500226 if (!lc || !*lc->device)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500227 return NULL;
228 return strdup(lc->device);
229}
230
231/*
232 * @lc: context
233 *
234 * Returns pointer device name in the @lc struct.
235 */
236const char *loopcxt_get_device(struct loopdev_cxt *lc)
237{
bigbiff7b4c7a62015-01-01 19:44:14 -0500238 return lc && *lc->device ? lc->device : NULL;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500239}
240
241/*
242 * @lc: context
243 *
244 * Returns pointer to the sysfs context (see lib/sysfs.c)
245 */
246struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)
247{
248 if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS))
249 return NULL;
250
251 if (!lc->sysfs.devno) {
252 dev_t devno = sysfs_devname_to_devno(lc->device, NULL);
253 if (!devno) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500254 DBG(CXT, ul_debugobj(lc, "sysfs: failed devname to devno"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500255 return NULL;
256 }
257 if (sysfs_init(&lc->sysfs, devno, NULL)) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500258 DBG(CXT, ul_debugobj(lc, "sysfs: init failed"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500259 return NULL;
260 }
261 }
262
263 return &lc->sysfs;
264}
265
266/*
267 * @lc: context
268 *
269 * Returns: file descriptor to the open loop device or <0 on error. The mode
270 * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
271 * read-only.
272 */
273int loopcxt_get_fd(struct loopdev_cxt *lc)
274{
275 if (!lc || !*lc->device)
276 return -EINVAL;
277
278 if (lc->fd < 0) {
279 lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY;
bigbiff7b4c7a62015-01-01 19:44:14 -0500280 lc->fd = open(lc->device, lc->mode | O_CLOEXEC);
281 DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device,
282 lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500283 }
284 return lc->fd;
285}
286
287int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode)
288{
289 if (!lc)
290 return -EINVAL;
291
292 lc->fd = fd;
293 lc->mode = mode;
294 return 0;
295}
296
297/*
298 * @lc: context
299 * @flags: LOOPITER_FL_* flags
300 *
301 * Iterator allows to scan list of the free or used loop devices.
302 *
303 * Returns: <0 on error, 0 on success
304 */
305int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags)
306{
307 struct loopdev_iter *iter;
308 struct stat st;
309
310 if (!lc)
311 return -EINVAL;
312
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500313
314 iter = &lc->iter;
bigbiff7b4c7a62015-01-01 19:44:14 -0500315 DBG(ITER, ul_debugobj(iter, "initialize"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500316
317 /* always zeroize
318 */
319 memset(iter, 0, sizeof(*iter));
320 iter->ncur = -1;
321 iter->flags = flags;
322 iter->default_check = 1;
323
324 if (!lc->extra_check) {
325 /*
326 * Check for /dev/loop/<N> subdirectory
327 */
328 if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) &&
329 stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode))
330 lc->flags |= LOOPDEV_FL_DEVSUBDIR;
331
332 lc->extra_check = 1;
333 }
334 return 0;
335}
336
337/*
338 * @lc: context
339 *
340 * Returns: <0 on error, 0 on success
341 */
342int loopcxt_deinit_iterator(struct loopdev_cxt *lc)
343{
344 struct loopdev_iter *iter;
345
346 if (!lc)
347 return -EINVAL;
348
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500349 iter = &lc->iter;
bigbiff7b4c7a62015-01-01 19:44:14 -0500350 DBG(ITER, ul_debugobj(iter, "de-initialize"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500351
352 free(iter->minors);
353 if (iter->proc)
354 fclose(iter->proc);
355 if (iter->sysblock)
356 closedir(iter->sysblock);
357 iter->minors = NULL;
358 iter->proc = NULL;
359 iter->sysblock = NULL;
360 iter->done = 1;
361 return 0;
362}
363
364/*
365 * Same as loopcxt_set_device, but also checks if the device is
366 * associeted 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 */
371static 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
bigbiff7b4c7a62015-01-01 19:44:14 -0500383 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
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500390 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
bigbiff7b4c7a62015-01-01 19:44:14 -0500398 DBG(ITER, ul_debugobj(&lc->iter, "failed to use %s device", lc->device));
399
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500400 ignore_result( loopcxt_set_device(lc, NULL) );
401 return 1;
402}
403
404static int cmpnum(const void *p1, const void *p2)
405{
406 return (((* (int *) p1) > (* (int *) p2)) -
407 ((* (int *) p1) < (* (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 */
415static 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;
bigbiff7b4c7a62015-01-01 19:44:14 -0500423
424 DBG(ITER, ul_debug("scan dir: %s", dirname));
425
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500426 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
bigbiff7b4c7a62015-01-01 19:44:14 -0500449 errno = 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500450 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 closedir(dir);
466 return -1;
467 }
468 *ary = tmp;
469 }
470 if (*ary)
471 (*ary)[count++] = n;
472 }
473 if (count && *ary)
474 qsort(*ary, count, sizeof(int), cmpnum);
475
476 closedir(dir);
477 return count;
478}
479
480/*
481 * Set the next *used* loop device according to /proc/partitions.
482 *
483 * Loop devices smaller than 512 bytes are invisible for this function.
484 */
485static int loopcxt_next_from_proc(struct loopdev_cxt *lc)
486{
487 struct loopdev_iter *iter = &lc->iter;
488 char buf[BUFSIZ];
489
bigbiff7b4c7a62015-01-01 19:44:14 -0500490 DBG(ITER, ul_debugobj(iter, "scan /proc/partitions"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500491
492 if (!iter->proc)
493 iter->proc = fopen(_PATH_PROC_PARTITIONS, "r");
494 if (!iter->proc)
495 return 1;
496
497 while (fgets(buf, sizeof(buf), iter->proc)) {
498 unsigned int m;
499 char name[128 + 1];
500
501
502 if (sscanf(buf, " %u %*s %*s %128[^\n ]",
503 &m, name) != 2 || m != LOOPDEV_MAJOR)
504 continue;
505
bigbiff7b4c7a62015-01-01 19:44:14 -0500506 DBG(ITER, ul_debugobj(iter, "checking %s", name));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500507
508 if (loopiter_set_device(lc, name) == 0)
509 return 0;
510 }
511
512 return 1;
513}
514
515/*
516 * Set the next *used* loop device according to
517 * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
518 *
519 * This is preferred method.
520 */
521static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc)
522{
523 struct loopdev_iter *iter = &lc->iter;
524 struct dirent *d;
525 int fd;
526
bigbiff7b4c7a62015-01-01 19:44:14 -0500527 DBG(ITER, ul_debugobj(iter, "scanning /sys/block"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500528
529 if (!iter->sysblock)
530 iter->sysblock = opendir(_PATH_SYS_BLOCK);
531
532 if (!iter->sysblock)
533 return 1;
534
535 fd = dirfd(iter->sysblock);
536
537 while ((d = readdir(iter->sysblock))) {
538 char name[256];
539 struct stat st;
540
bigbiff7b4c7a62015-01-01 19:44:14 -0500541 DBG(ITER, ul_debugobj(iter, "check %s", d->d_name));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500542
543 if (strcmp(d->d_name, ".") == 0
544 || strcmp(d->d_name, "..") == 0
545 || strncmp(d->d_name, "loop", 4) != 0)
546 continue;
547
548 snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
549 if (fstat_at(fd, _PATH_SYS_BLOCK, name, &st, 0) != 0)
550 continue;
551
552 if (loopiter_set_device(lc, d->d_name) == 0)
553 return 0;
554 }
555
556 return 1;
557}
558
559/*
560 * @lc: context, has to initialized by loopcxt_init_iterator()
561 *
562 * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
563 * about the current loop device are available by
564 * loopcxt_get_{fd,backing_file,device,offset, ...} functions.
565 */
566int loopcxt_next(struct loopdev_cxt *lc)
567{
568 struct loopdev_iter *iter;
569
570 if (!lc)
571 return -EINVAL;
572
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500573
574 iter = &lc->iter;
575 if (iter->done)
576 return 1;
577
bigbiff7b4c7a62015-01-01 19:44:14 -0500578 DBG(ITER, ul_debugobj(iter, "next"));
579
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500580 /* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
581 */
582 if (iter->flags & LOOPITER_FL_USED) {
583 int rc;
584
585 if (loopcxt_sysfs_available(lc))
586 rc = loopcxt_next_from_sysfs(lc);
587 else
588 rc = loopcxt_next_from_proc(lc);
589 if (rc == 0)
590 return 0;
591 goto done;
592 }
593
594 /* B) Classic way, try first eight loop devices (default number
595 * of loop devices). This is enough for 99% of all cases.
596 */
597 if (iter->default_check) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500598 DBG(ITER, ul_debugobj(iter, "next: default check"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500599 for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES;
600 iter->ncur++) {
601 char name[16];
602 snprintf(name, sizeof(name), "loop%d", iter->ncur);
603
604 if (loopiter_set_device(lc, name) == 0)
605 return 0;
606 }
607 iter->default_check = 0;
608 }
609
610 /* C) the worst possibility, scan whole /dev or /dev/loop/<N>
611 */
612 if (!iter->minors) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500613 DBG(ITER, ul_debugobj(iter, "next: scanning /dev"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500614 iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ?
615 loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) :
616 loop_scandir(_PATH_DEV, &iter->minors, 1);
617 iter->ncur = -1;
618 }
619 for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) {
620 char name[16];
621 snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]);
622
623 if (loopiter_set_device(lc, name) == 0)
624 return 0;
625 }
626done:
627 loopcxt_deinit_iterator(lc);
628 return 1;
629}
630
631/*
632 * @device: path to device
633 */
634int is_loopdev(const char *device)
635{
636 struct stat st;
637
638 if (!device)
639 return 0;
640
641 return (stat(device, &st) == 0 &&
642 S_ISBLK(st.st_mode) &&
643 major(st.st_rdev) == LOOPDEV_MAJOR);
644}
645
646/*
647 * @lc: context
648 *
649 * Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
650 */
651struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc)
652{
653 int fd;
654
bigbiff7b4c7a62015-01-01 19:44:14 -0500655 if (!lc || lc->info_failed) {
656 errno = EINVAL;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500657 return NULL;
bigbiff7b4c7a62015-01-01 19:44:14 -0500658 }
659 errno = 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500660 if (lc->has_info)
661 return &lc->info;
662
663 fd = loopcxt_get_fd(lc);
664 if (fd < 0)
665 return NULL;
666
667 if (ioctl(fd, LOOP_GET_STATUS64, &lc->info) == 0) {
668 lc->has_info = 1;
669 lc->info_failed = 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500670 DBG(CXT, ul_debugobj(lc, "reading loop_info64 OK"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500671 return &lc->info;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500672 }
673
bigbiff7b4c7a62015-01-01 19:44:14 -0500674 lc->info_failed = 1;
675 DBG(CXT, ul_debugobj(lc, "reading loop_info64 FAILED"));
676
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500677 return NULL;
678}
679
680/*
681 * @lc: context
682 *
683 * Returns (allocated) string with path to the file assicieted
684 * with the current loop device.
685 */
686char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
687{
688 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
689 char *res = NULL;
690
691 if (sysfs)
692 /*
693 * This is always preffered, the loop_info64
694 * has too small buffer for the filename.
695 */
696 res = sysfs_strdup(sysfs, "loop/backing_file");
697
698 if (!res && loopcxt_ioctl_enabled(lc)) {
699 struct loop_info64 *lo = loopcxt_get_info(lc);
700
701 if (lo) {
702 lo->lo_file_name[LO_NAME_SIZE - 2] = '*';
703 lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
704 res = strdup((char *) lo->lo_file_name);
705 }
706 }
707
bigbiff7b4c7a62015-01-01 19:44:14 -0500708 DBG(CXT, ul_debugobj(lc, "get_backing_file [%s]", res));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500709 return res;
710}
711
712/*
713 * @lc: context
714 * @offset: returns offset number for the given device
715 *
716 * Returns: <0 on error, 0 on success
717 */
718int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset)
719{
720 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
721 int rc = -EINVAL;
722
723 if (sysfs)
724 rc = sysfs_read_u64(sysfs, "loop/offset", offset);
725
726 if (rc && loopcxt_ioctl_enabled(lc)) {
727 struct loop_info64 *lo = loopcxt_get_info(lc);
728 if (lo) {
729 if (offset)
730 *offset = lo->lo_offset;
731 rc = 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500732 } else
733 rc = -errno;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500734 }
735
bigbiff7b4c7a62015-01-01 19:44:14 -0500736 DBG(CXT, ul_debugobj(lc, "get_offset [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500737 return rc;
738}
739
740/*
741 * @lc: context
742 * @sizelimit: returns size limit for the given device
743 *
744 * Returns: <0 on error, 0 on success
745 */
746int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size)
747{
748 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
749 int rc = -EINVAL;
750
751 if (sysfs)
752 rc = sysfs_read_u64(sysfs, "loop/sizelimit", size);
753
754 if (rc && loopcxt_ioctl_enabled(lc)) {
755 struct loop_info64 *lo = loopcxt_get_info(lc);
756 if (lo) {
757 if (size)
758 *size = lo->lo_sizelimit;
759 rc = 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500760 } else
761 rc = -errno;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500762 }
763
bigbiff7b4c7a62015-01-01 19:44:14 -0500764 DBG(CXT, ul_debugobj(lc, "get_sizelimit [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500765 return rc;
766}
767
768/*
769 * @lc: context
770 * @devno: returns encryption type
771 *
772 * Cryptoloop is DEPRECATED!
773 *
774 * Returns: <0 on error, 0 on success
775 */
776int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type)
777{
778 struct loop_info64 *lo = loopcxt_get_info(lc);
bigbiff7b4c7a62015-01-01 19:44:14 -0500779 int rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500780
bigbiff7b4c7a62015-01-01 19:44:14 -0500781 /* not provided by sysfs */
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500782 if (lo) {
783 if (type)
784 *type = lo->lo_encrypt_type;
785 rc = 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500786 } else
787 rc = -errno;
788
789 DBG(CXT, ul_debugobj(lc, "get_encrypt_type [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500790 return rc;
791}
792
793/*
794 * @lc: context
795 * @devno: returns crypt name
796 *
797 * Cryptoloop is DEPRECATED!
798 *
799 * Returns: <0 on error, 0 on success
800 */
801const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc)
802{
803 struct loop_info64 *lo = loopcxt_get_info(lc);
804
805 if (lo)
806 return (char *) lo->lo_crypt_name;
807
bigbiff7b4c7a62015-01-01 19:44:14 -0500808 DBG(CXT, ul_debugobj(lc, "get_crypt_name failed"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500809 return NULL;
810}
811
812/*
813 * @lc: context
814 * @devno: returns backing file devno
815 *
816 * Returns: <0 on error, 0 on success
817 */
818int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno)
819{
820 struct loop_info64 *lo = loopcxt_get_info(lc);
bigbiff7b4c7a62015-01-01 19:44:14 -0500821 int rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500822
823 if (lo) {
824 if (devno)
825 *devno = lo->lo_device;
826 rc = 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500827 } else
828 rc = -errno;
829
830 DBG(CXT, ul_debugobj(lc, "get_backing_devno [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500831 return rc;
832}
833
834/*
835 * @lc: context
836 * @ino: returns backing file inode
837 *
838 * Returns: <0 on error, 0 on success
839 */
840int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino)
841{
842 struct loop_info64 *lo = loopcxt_get_info(lc);
bigbiff7b4c7a62015-01-01 19:44:14 -0500843 int rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500844
845 if (lo) {
846 if (ino)
847 *ino = lo->lo_inode;
848 rc = 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500849 } else
850 rc = -errno;
851
852 DBG(CXT, ul_debugobj(lc, "get_backing_inode [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500853 return rc;
854}
855
856/*
857 * Check if the kernel supports partitioned loop devices.
858 *
859 * Notes:
860 * - kernels < 3.2 support partitioned loop devices and PT scanning
861 * only if max_part= module paremeter is non-zero
862 *
863 * - kernels >= 3.2 always support partitioned loop devices
864 *
865 * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls
866 *
867 * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the
868 * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled
869 * by default.
870 *
871 * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98.
872 */
873int loopmod_supports_partscan(void)
874{
875 int rc, ret = 0;
876 FILE *f;
877
878 if (get_linux_version() >= KERNEL_VERSION(3,2,0))
879 return 1;
880
881 f = fopen("/sys/module/loop/parameters/max_part", "r");
882 if (!f)
883 return 0;
884 rc = fscanf(f, "%d", &ret);
885 fclose(f);
886 return rc == 1 ? ret : 0;
887}
888
889/*
890 * @lc: context
891 *
892 * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions
893 * scannig is enabled for all loop devices.
894 */
895int loopcxt_is_partscan(struct loopdev_cxt *lc)
896{
897 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
898
899 if (sysfs) {
900 /* kernel >= 3.2 */
901 int fl;
902 if (sysfs_read_int(sysfs, "loop/partscan", &fl) == 0)
903 return fl;
904 }
905
906 /* old kernels (including kernels without loopN/loop/<flags> directory */
907 return loopmod_supports_partscan();
908}
909
910/*
911 * @lc: context
912 *
913 * Returns: 1 if the autoclear flags is set.
914 */
915int loopcxt_is_autoclear(struct loopdev_cxt *lc)
916{
917 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
918
919 if (sysfs) {
920 int fl;
921 if (sysfs_read_int(sysfs, "loop/autoclear", &fl) == 0)
922 return fl;
923 }
924
925 if (loopcxt_ioctl_enabled(lc)) {
926 struct loop_info64 *lo = loopcxt_get_info(lc);
927 if (lo)
928 return lo->lo_flags & LO_FLAGS_AUTOCLEAR;
929 }
930 return 0;
931}
932
933/*
934 * @lc: context
935 *
936 * Returns: 1 if the readonly flags is set.
937 */
938int loopcxt_is_readonly(struct loopdev_cxt *lc)
939{
940 struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc);
941
942 if (sysfs) {
943 int fl;
944 if (sysfs_read_int(sysfs, "ro", &fl) == 0)
945 return fl;
946 }
947
948 if (loopcxt_ioctl_enabled(lc)) {
949 struct loop_info64 *lo = loopcxt_get_info(lc);
950 if (lo)
951 return lo->lo_flags & LO_FLAGS_READ_ONLY;
952 }
953 return 0;
954}
955
956/*
957 * @lc: context
958 * @st: backing file stat or NULL
959 * @backing_file: filename
960 * @offset: offset
961 * @flags: LOOPDEV_FL_OFFSET if @offset should not be ignored
962 *
963 * Returns 1 if the current @lc loopdev is associated with the given backing
964 * file. Note that the preferred way is to use devno and inode number rather
965 * than filename. The @backing_file filename is poor solution usable in case
966 * that you don't have rights to call stat().
967 *
968 * Don't forget that old kernels provide very restricted (in size) backing
969 * filename by LOOP_GET_STAT64 ioctl only.
970 */
971int loopcxt_is_used(struct loopdev_cxt *lc,
972 struct stat *st,
973 const char *backing_file,
974 uint64_t offset,
975 int flags)
976{
977 ino_t ino;
978 dev_t dev;
979
980 if (!lc)
981 return 0;
982
bigbiff7b4c7a62015-01-01 19:44:14 -0500983 DBG(CXT, ul_debugobj(lc, "checking %s vs. %s",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500984 loopcxt_get_device(lc),
985 backing_file));
986
987 if (st && loopcxt_get_backing_inode(lc, &ino) == 0 &&
988 loopcxt_get_backing_devno(lc, &dev) == 0) {
989
990 if (ino == st->st_ino && dev == st->st_dev)
991 goto found;
992
993 /* don't use filename if we have devno and inode */
994 return 0;
995 }
996
997 /* poor man's solution */
998 if (backing_file) {
999 char *name = loopcxt_get_backing_file(lc);
1000 int rc = name && strcmp(name, backing_file) == 0;
1001
1002 free(name);
1003 if (rc)
1004 goto found;
1005 }
1006
1007 return 0;
1008found:
1009 if (flags & LOOPDEV_FL_OFFSET) {
1010 uint64_t off;
1011
1012 return loopcxt_get_offset(lc, &off) == 0 && off == offset;
1013 }
1014 return 1;
1015}
1016
1017/*
1018 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1019 */
1020int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset)
1021{
1022 if (!lc)
1023 return -EINVAL;
1024 lc->info.lo_offset = offset;
1025
bigbiff7b4c7a62015-01-01 19:44:14 -05001026 DBG(CXT, ul_debugobj(lc, "set offset=%jd", offset));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001027 return 0;
1028}
1029
1030/*
1031 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1032 */
1033int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit)
1034{
1035 if (!lc)
1036 return -EINVAL;
1037 lc->info.lo_sizelimit = sizelimit;
1038
bigbiff7b4c7a62015-01-01 19:44:14 -05001039 DBG(CXT, ul_debugobj(lc, "set sizelimit=%jd", sizelimit));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001040 return 0;
1041}
1042
1043/*
1044 * @lc: context
1045 * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags
1046 *
1047 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1048 *
1049 * Returns: 0 on success, <0 on error.
1050 */
1051int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags)
1052{
1053 if (!lc)
1054 return -EINVAL;
1055 lc->info.lo_flags = flags;
1056
bigbiff7b4c7a62015-01-01 19:44:14 -05001057 DBG(CXT, ul_debugobj(lc, "set flags=%u", (unsigned) flags));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001058 return 0;
1059}
1060
1061/*
1062 * @lc: context
1063 * @filename: backing file path (the path will be canonicalized)
1064 *
1065 * The setting is removed by loopcxt_set_device() loopcxt_next()!
1066 *
1067 * Returns: 0 on success, <0 on error.
1068 */
1069int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename)
1070{
1071 if (!lc)
1072 return -EINVAL;
1073
1074 lc->filename = canonicalize_path(filename);
1075 if (!lc->filename)
1076 return -errno;
1077
1078 strncpy((char *)lc->info.lo_file_name, lc->filename, LO_NAME_SIZE);
1079 lc->info.lo_file_name[LO_NAME_SIZE- 1] = '\0';
1080
bigbiff7b4c7a62015-01-01 19:44:14 -05001081 DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->info.lo_file_name));
1082 return 0;
1083}
1084
1085/*
1086 * In kernels prior to v3.9, if the offset or sizelimit options
1087 * are used, the block device's size won't be synced automatically.
1088 * blockdev --getsize64 and filesystems will use the backing
1089 * file size until the block device has been re-opened or the
1090 * LOOP_SET_CAPACITY ioctl is called to sync the sizes.
1091 *
1092 * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes
1093 * the open file descriptor to the mount system call, we need to use
1094 * the ioctl. Calling losetup directly doesn't have this problem since
1095 * it closes the device when it exits and whatever consumes the device
1096 * next will re-open it, causing the resync.
1097 */
1098static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd)
1099{
1100 uint64_t size, expected_size;
1101 int dev_fd;
1102 struct stat st;
1103
1104 if (!lc->info.lo_offset && !lc->info.lo_sizelimit)
1105 return 0;
1106
1107 if (fstat(file_fd, &st)) {
1108 DBG(CXT, ul_debugobj(lc, "failed to fstat backing file"));
1109 return -errno;
1110 }
1111 if (S_ISBLK(st.st_mode)) {
1112 if (blkdev_get_size(file_fd,
1113 (unsigned long long *) &expected_size)) {
1114 DBG(CXT, ul_debugobj(lc, "failed to determine device size"));
1115 return -errno;
1116 }
1117 } else
1118 expected_size = st.st_size;
1119
1120 if (expected_size == 0 || expected_size <= lc->info.lo_offset) {
1121 DBG(CXT, ul_debugobj(lc, "failed to determine expected size"));
1122 return 0; /* ignore this error */
1123 }
1124
1125 if (lc->info.lo_offset > 0)
1126 expected_size -= lc->info.lo_offset;
1127
1128 if (lc->info.lo_sizelimit > 0 && lc->info.lo_sizelimit < expected_size)
1129 expected_size = lc->info.lo_sizelimit;
1130
1131 dev_fd = loopcxt_get_fd(lc);
1132 if (dev_fd < 0) {
1133 DBG(CXT, ul_debugobj(lc, "failed to get loop FD"));
1134 return -errno;
1135 }
1136
1137 if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) {
1138 DBG(CXT, ul_debugobj(lc, "failed to determine loopdev size"));
1139 return -errno;
1140 }
1141
1142 /* It's block device, so, align to 512-byte sectors */
1143 if (expected_size % 512) {
1144 DBG(CXT, ul_debugobj(lc, "expected size misaligned to 512-byte sectors"));
1145 expected_size = (expected_size >> 9) << 9;
1146 }
1147
1148 if (expected_size != size) {
1149 DBG(CXT, ul_debugobj(lc, "warning: loopdev and expected "
1150 "size dismatch (%ju/%ju)",
1151 size, expected_size));
1152
1153 if (loopcxt_set_capacity(lc)) {
1154 /* ioctl not available */
1155 if (errno == ENOTTY || errno == EINVAL)
1156 errno = ERANGE;
1157 return -errno;
1158 }
1159
1160 if (blkdev_get_size(dev_fd, (unsigned long long *) &size))
1161 return -errno;
1162
1163 if (expected_size != size) {
1164 errno = ERANGE;
1165 DBG(CXT, ul_debugobj(lc, "failed to set loopdev size, "
1166 "size: %ju, expected: %ju",
1167 size, expected_size));
1168 return -errno;
1169 }
1170 }
1171
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001172 return 0;
1173}
1174
1175/*
1176 * @cl: context
1177 *
1178 * Associate the current device (see loopcxt_{set,get}_device()) with
1179 * a file (see loopcxt_set_backing_file()).
1180 *
1181 * The device is initialized read-write by default. If you want read-only
1182 * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_*
1183 * flags are ignored and modified according to LO_FLAGS_*.
1184 *
1185 * If the device is already open by loopcxt_get_fd() then this setup device
1186 * function will re-open the device to fix read/write mode.
1187 *
1188 * The device is also initialized read-only if the backing file is not
1189 * possible to open read-write (e.g. read-only FS).
1190 *
1191 * Returns: <0 on error, 0 on success.
1192 */
1193int loopcxt_setup_device(struct loopdev_cxt *lc)
1194{
bigbiff7b4c7a62015-01-01 19:44:14 -05001195 int file_fd, dev_fd, mode = O_RDWR, rc = -1, cnt = 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001196
1197 if (!lc || !*lc->device || !lc->filename)
1198 return -EINVAL;
1199
bigbiff7b4c7a62015-01-01 19:44:14 -05001200 DBG(SETUP, ul_debugobj(lc, "device setup requested"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001201
1202 /*
1203 * Open backing file and device
1204 */
1205 if (lc->info.lo_flags & LO_FLAGS_READ_ONLY)
1206 mode = O_RDONLY;
1207
bigbiff7b4c7a62015-01-01 19:44:14 -05001208 if ((file_fd = open(lc->filename, mode | O_CLOEXEC)) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001209 if (mode != O_RDONLY && (errno == EROFS || errno == EACCES))
1210 file_fd = open(lc->filename, mode = O_RDONLY);
1211
1212 if (file_fd < 0) {
bigbiff7b4c7a62015-01-01 19:44:14 -05001213 DBG(SETUP, ul_debugobj(lc, "open backing file failed: %m"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001214 return -errno;
1215 }
1216 }
bigbiff7b4c7a62015-01-01 19:44:14 -05001217 DBG(SETUP, ul_debugobj(lc, "backing file open: OK"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001218
1219 if (lc->fd != -1 && lc->mode != mode) {
bigbiff7b4c7a62015-01-01 19:44:14 -05001220 DBG(SETUP, ul_debugobj(lc, "closing already open device (mode mismatch)"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001221 close(lc->fd);
1222 lc->fd = -1;
1223 lc->mode = 0;
1224 }
1225
1226 if (mode == O_RDONLY) {
1227 lc->flags |= LOOPDEV_FL_RDONLY; /* open() mode */
1228 lc->info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */
1229 } else {
1230 lc->flags |= LOOPDEV_FL_RDWR; /* open() mode */
1231 lc->info.lo_flags &= ~LO_FLAGS_READ_ONLY;
1232 lc->flags &= ~LOOPDEV_FL_RDONLY;
1233 }
1234
bigbiff7b4c7a62015-01-01 19:44:14 -05001235 do {
1236 errno = 0;
1237 dev_fd = loopcxt_get_fd(lc);
1238 if (dev_fd >= 0 || lc->control_ok == 0)
1239 break;
1240 if (errno != EACCES && errno != ENOENT)
1241 break;
1242 /* We have permissions to open /dev/loop-control, but open
1243 * /dev/loopN failed with EACCES, it's probably because udevd
1244 * does not applied chown yet. Let's wait a moment. */
1245 usleep(25000);
1246 } while (cnt++ < 16);
1247
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001248 if (dev_fd < 0) {
1249 rc = -errno;
1250 goto err;
1251 }
1252
bigbiff7b4c7a62015-01-01 19:44:14 -05001253 DBG(SETUP, ul_debugobj(lc, "device open: OK"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001254
1255 /*
1256 * Set FD
1257 */
1258 if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
1259 rc = -errno;
bigbiff7b4c7a62015-01-01 19:44:14 -05001260 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD failed: %m"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001261 goto err;
1262 }
1263
bigbiff7b4c7a62015-01-01 19:44:14 -05001264 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD: OK"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001265
1266 if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) {
bigbiff7b4c7a62015-01-01 19:44:14 -05001267 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001268 goto err;
1269 }
1270
bigbiff7b4c7a62015-01-01 19:44:14 -05001271 DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64: OK"));
1272
1273 if ((rc = loopcxt_check_size(lc, file_fd)))
1274 goto err;
1275
1276 close(file_fd);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001277
1278 memset(&lc->info, 0, sizeof(lc->info));
1279 lc->has_info = 0;
1280 lc->info_failed = 0;
1281
bigbiff7b4c7a62015-01-01 19:44:14 -05001282 DBG(SETUP, ul_debugobj(lc, "success [rc=0]"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001283 return 0;
1284err:
1285 if (file_fd >= 0)
1286 close(file_fd);
bigbiff7b4c7a62015-01-01 19:44:14 -05001287 if (dev_fd >= 0 && rc != -EBUSY)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001288 ioctl(dev_fd, LOOP_CLR_FD, 0);
1289
bigbiff7b4c7a62015-01-01 19:44:14 -05001290 DBG(SETUP, ul_debugobj(lc, "failed [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001291 return rc;
1292}
1293
bigbiff7b4c7a62015-01-01 19:44:14 -05001294int loopcxt_set_capacity(struct loopdev_cxt *lc)
1295{
1296 int fd = loopcxt_get_fd(lc);
1297
1298 if (fd < 0)
1299 return -EINVAL;
1300
1301 /* Kernels prior to v2.6.30 don't support this ioctl */
1302 if (ioctl(fd, LOOP_SET_CAPACITY, 0) < 0) {
1303 int rc = -errno;
1304 DBG(CXT, ul_debugobj(lc, "LOOP_SET_CAPACITY failed: %m"));
1305 return rc;
1306 }
1307
1308 DBG(CXT, ul_debugobj(lc, "capacity set"));
1309 return 0;
1310}
1311
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001312int loopcxt_delete_device(struct loopdev_cxt *lc)
1313{
1314 int fd = loopcxt_get_fd(lc);
1315
1316 if (fd < 0)
1317 return -EINVAL;
1318
1319 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
bigbiff7b4c7a62015-01-01 19:44:14 -05001320 DBG(CXT, ul_debugobj(lc, "LOOP_CLR_FD failed: %m"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001321 return -errno;
1322 }
1323
bigbiff7b4c7a62015-01-01 19:44:14 -05001324 DBG(CXT, ul_debugobj(lc, "device removed"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001325 return 0;
1326}
1327
bigbiff7b4c7a62015-01-01 19:44:14 -05001328int loopcxt_add_device(struct loopdev_cxt *lc)
1329{
1330 int rc = -EINVAL;
1331 int ctl, nr = -1;
1332 const char *p, *dev = loopcxt_get_device(lc);
1333
1334 if (!dev)
1335 goto done;
1336
1337 if (!(lc->flags & LOOPDEV_FL_CONTROL)) {
1338 rc = -ENOSYS;
1339 goto done;
1340 }
1341
1342 p = strrchr(dev, '/');
1343 if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1)
1344 || nr < 0)
1345 goto done;
1346
1347 ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1348 if (ctl >= 0) {
1349 DBG(CXT, ul_debugobj(lc, "add_device %d", nr));
1350 rc = ioctl(ctl, LOOP_CTL_ADD, nr);
1351 close(ctl);
1352 }
1353 lc->control_ok = rc >= 0 ? 1 : 0;
1354done:
1355 DBG(CXT, ul_debugobj(lc, "add_device done [rc=%d]", rc));
1356 return rc;
1357}
1358
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001359/*
1360 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1361 * kernels we have to check all loop devices to found unused one.
1362 *
1363 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1364 */
1365int loopcxt_find_unused(struct loopdev_cxt *lc)
1366{
1367 int rc = -1;
1368
bigbiff7b4c7a62015-01-01 19:44:14 -05001369 DBG(CXT, ul_debugobj(lc, "find_unused requested"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001370
1371 if (lc->flags & LOOPDEV_FL_CONTROL) {
bigbiff7b4c7a62015-01-01 19:44:14 -05001372 int ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001373
1374 if (ctl >= 0)
1375 rc = ioctl(ctl, LOOP_CTL_GET_FREE);
1376 if (rc >= 0) {
1377 char name[16];
1378 snprintf(name, sizeof(name), "loop%d", rc);
1379
1380 rc = loopiter_set_device(lc, name);
1381 }
bigbiff7b4c7a62015-01-01 19:44:14 -05001382 lc->control_ok = ctl >= 0 && rc == 0 ? 1 : 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001383 if (ctl >= 0)
1384 close(ctl);
bigbiff7b4c7a62015-01-01 19:44:14 -05001385 DBG(CXT, ul_debugobj(lc, "find_unused by loop-control [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001386 }
1387
1388 if (rc < 0) {
1389 rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE);
1390 if (rc)
1391 return rc;
1392
1393 rc = loopcxt_next(lc);
1394 loopcxt_deinit_iterator(lc);
bigbiff7b4c7a62015-01-01 19:44:14 -05001395 DBG(CXT, ul_debugobj(lc, "find_unused by scan [rc=%d]", rc));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001396 }
1397 return rc;
1398}
1399
1400
1401
1402/*
1403 * Return: TRUE/FALSE
1404 */
1405int loopdev_is_autoclear(const char *device)
1406{
1407 struct loopdev_cxt lc;
1408 int rc;
1409
1410 if (!device)
1411 return 0;
1412
1413 rc = loopcxt_init(&lc, 0);
1414 if (!rc)
1415 rc = loopcxt_set_device(&lc, device);
1416 if (!rc)
1417 rc = loopcxt_is_autoclear(&lc);
1418
1419 loopcxt_deinit(&lc);
1420 return rc;
1421}
1422
1423char *loopdev_get_backing_file(const char *device)
1424{
1425 struct loopdev_cxt lc;
1426 char *res = NULL;
1427
1428 if (!device)
1429 return NULL;
1430 if (loopcxt_init(&lc, 0))
1431 return NULL;
1432 if (loopcxt_set_device(&lc, device) == 0)
1433 res = loopcxt_get_backing_file(&lc);
1434
1435 loopcxt_deinit(&lc);
1436 return res;
1437}
1438
1439/*
1440 * Returns: TRUE/FALSE
1441 */
1442int loopdev_is_used(const char *device, const char *filename,
1443 uint64_t offset, int flags)
1444{
1445 struct loopdev_cxt lc;
1446 struct stat st;
1447 int rc = 0;
1448
1449 if (!device || !filename)
1450 return 0;
1451
1452 rc = loopcxt_init(&lc, 0);
1453 if (!rc)
1454 rc = loopcxt_set_device(&lc, device);
1455 if (rc)
1456 return rc;
1457
1458 rc = !stat(filename, &st);
1459 rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, flags);
1460
1461 loopcxt_deinit(&lc);
1462 return rc;
1463}
1464
1465int loopdev_delete(const char *device)
1466{
1467 struct loopdev_cxt lc;
1468 int rc;
1469
1470 if (!device)
1471 return -EINVAL;
1472
1473 rc = loopcxt_init(&lc, 0);
1474 if (!rc)
1475 rc = loopcxt_set_device(&lc, device);
1476 if (!rc)
1477 rc = loopcxt_delete_device(&lc);
1478 loopcxt_deinit(&lc);
1479 return rc;
1480}
1481
1482/*
1483 * Returns: 0 = success, < 0 error, 1 not found
1484 */
1485int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename,
1486 uint64_t offset, int flags)
1487{
1488 int rc, hasst;
1489 struct stat st;
1490
1491 if (!filename)
1492 return -EINVAL;
1493
1494 hasst = !stat(filename, &st);
1495
1496 rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1497 if (rc)
1498 return rc;
1499
1500 while ((rc = loopcxt_next(lc)) == 0) {
1501
1502 if (loopcxt_is_used(lc, hasst ? &st : NULL,
1503 filename, offset, flags))
1504 break;
1505 }
1506
1507 loopcxt_deinit_iterator(lc);
1508 return rc;
1509}
1510
1511/*
1512 * Returns allocated string with device name
1513 */
1514char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int flags)
1515{
1516 struct loopdev_cxt lc;
1517 char *res = NULL;
1518
1519 if (!filename)
1520 return NULL;
1521
1522 if (loopcxt_init(&lc, 0))
1523 return NULL;
1524 if (loopcxt_find_by_backing_file(&lc, filename, offset, flags) == 0)
1525 res = loopcxt_strdup_device(&lc);
1526 loopcxt_deinit(&lc);
1527
1528 return res;
1529}
1530
1531/*
1532 * Returns number of loop devices associated with @file, if only one loop
1533 * device is associeted with the given @filename and @loopdev is not NULL then
1534 * @loopdev returns name of the device.
1535 */
1536int loopdev_count_by_backing_file(const char *filename, char **loopdev)
1537{
1538 struct loopdev_cxt lc;
1539 int count = 0, rc;
1540
1541 if (!filename)
1542 return -1;
1543
1544 rc = loopcxt_init(&lc, 0);
1545 if (rc)
1546 return rc;
1547 if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED))
1548 return -1;
1549
1550 while(loopcxt_next(&lc) == 0) {
1551 char *backing = loopcxt_get_backing_file(&lc);
1552
1553 if (!backing || strcmp(backing, filename)) {
1554 free(backing);
1555 continue;
1556 }
1557
1558 free(backing);
1559 if (loopdev && count == 0)
1560 *loopdev = loopcxt_strdup_device(&lc);
1561 count++;
1562 }
1563
1564 loopcxt_deinit(&lc);
1565
1566 if (loopdev && count > 1) {
1567 free(*loopdev);
1568 *loopdev = NULL;
1569 }
1570 return count;
1571}
1572