Coverage Report

Created: 2026-04-11 06:29

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