Coverage Report

Created: 2026-06-08 06:06

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
    file_fd = ul_open_no_symlinks(lc->filename, mode | flags, 0);
1402
0
  else
1403
0
    file_fd = open(lc->filename, mode | flags);
1404
1405
0
  if (file_fd < 0 && mode != O_RDONLY
1406
0
      && (errno == EROFS || errno == EACCES)) {
1407
0
    mode = O_RDONLY;
1408
0
    if (lc->flags & LOOPDEV_FL_NOFOLLOW)
1409
0
      file_fd = ul_open_no_symlinks(lc->filename, mode | flags, 0);
1410
0
    else
1411
0
      file_fd = open(lc->filename, mode | flags);
1412
0
  }
1413
0
  if (file_fd < 0) {
1414
0
    DBG_OBJ(SETUP, lc, ul_debug("open backing file failed: %m"));
1415
0
    return -errno;
1416
0
  }
1417
0
  DBG_OBJ(SETUP, lc, ul_debug("backing file open: OK"));
1418
1419
0
  if (mode == O_RDONLY)
1420
0
    lc->config.info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */
1421
0
  else
1422
0
    lc->config.info.lo_flags &= ~LO_FLAGS_READ_ONLY;
1423
1424
0
  do {
1425
0
    errno = 0;
1426
1427
0
    dev_fd = __loopcxt_get_fd(lc, mode);
1428
0
    if (dev_fd >= 0 || lc->control_ok == 0)
1429
0
      break;
1430
0
    if (errno != EACCES && errno != ENOENT)
1431
0
      break;
1432
    /* We have permissions to open /dev/loop-control, but open
1433
     * /dev/loopN failed with EACCES, it's probably because udevd
1434
     * does not applied chown yet. Let's wait a moment. */
1435
0
    xusleep(25000);
1436
0
  } while (cnt++ < 16);
1437
1438
0
  if (dev_fd < 0) {
1439
0
    rc = -errno;
1440
0
    goto err;
1441
0
  }
1442
1443
0
  DBG_OBJ(SETUP, lc, ul_debug("device open: OK"));
1444
1445
  /*
1446
   * Atomic way to configure all by one ioctl call
1447
   * -- since Linux v5.8-rc1, commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
1448
   */
1449
0
  lc->config.fd = file_fd;
1450
0
  if (lc->blocksize > 0)
1451
0
    lc->config.block_size = lc->blocksize;
1452
1453
0
  rc = repeat_on_eagain( ioctl(dev_fd, LOOP_CONFIGURE, &lc->config) );
1454
0
  if (rc != 0) {
1455
0
    errsv = errno;
1456
0
    if (errno != EINVAL && errno != ENOTTY && errno != ENOSYS) {
1457
0
      DBG_OBJ(SETUP, lc, ul_debug("LOOP_CONFIGURE failed: %m"));
1458
0
      goto err;
1459
0
    }
1460
0
    fallback = 1;
1461
0
  } else {
1462
0
    DBG_OBJ(SETUP, lc, ul_debug("LOOP_CONFIGURE: OK"));
1463
0
  }
1464
1465
  /*
1466
   * Old deprecated way; first assign backing file FD and then in the
1467
   * second step set loop device properties.
1468
   */
1469
0
  if (fallback) {
1470
0
    if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
1471
0
      rc = -errno;
1472
0
      errsv = errno;
1473
0
      DBG_OBJ(SETUP, lc, ul_debug("LOOP_SET_FD failed: %m"));
1474
0
      goto err;
1475
0
    }
1476
1477
0
    DBG_OBJ(SETUP, lc, ul_debug("LOOP_SET_FD: OK"));
1478
1479
0
    if (lc->blocksize > 0
1480
0
      && (rc = loopcxt_ioctl_blocksize(lc, lc->blocksize)) < 0) {
1481
0
      errsv = -rc;
1482
0
      goto err;
1483
0
    }
1484
1485
0
    if ((rc = loopcxt_ioctl_status(lc)) < 0) {
1486
0
      errsv = -rc;
1487
0
      goto err;
1488
0
    }
1489
0
  }
1490
1491
0
  if ((rc = loopcxt_check_size(lc, file_fd)))
1492
0
    goto err;
1493
1494
0
  close(file_fd);
1495
1496
0
  memset(&lc->config, 0, sizeof(lc->config));
1497
0
  lc->has_info = 0;
1498
0
  lc->info_failed = 0;
1499
1500
0
  DBG_OBJ(SETUP, lc, ul_debug("success [rc=0]"));
1501
0
  return 0;
1502
0
err:
1503
0
  if (file_fd >= 0)
1504
0
    close(file_fd);
1505
0
  if (dev_fd >= 0 && rc != -EBUSY)
1506
0
    ioctl(dev_fd, LOOP_CLR_FD, 0);
1507
0
  if (errsv)
1508
0
    errno = errsv;
1509
1510
0
  DBG_OBJ(SETUP, lc, ul_debug("failed [rc=%d]", rc));
1511
0
  return rc;
1512
0
}
1513
1514
1515
/*
1516
 * @lc: context
1517
 *
1518
 * Update status of the current device (see loopcxt_{set,get}_device()).
1519
 *
1520
 * Note that once initialized, kernel accepts only selected changes:
1521
 * LO_FLAGS_AUTOCLEAR and LO_FLAGS_PARTSCAN
1522
 * For more see linux/drivers/block/loop.c:loop_set_status()
1523
 *
1524
 * Returns: <0 on error, 0 on success.
1525
 */
1526
int loopcxt_ioctl_status(struct loopdev_cxt *lc)
1527
0
{
1528
0
  int dev_fd, rc;
1529
1530
0
  errno = 0;
1531
0
  dev_fd = loopcxt_get_fd(lc);
1532
1533
0
  if (dev_fd < 0)
1534
0
    return -errno;
1535
1536
0
  DBG_OBJ(SETUP, lc, ul_debug("calling LOOP_SET_STATUS64"));
1537
1538
0
  rc = repeat_on_eagain( ioctl(dev_fd, LOOP_SET_STATUS64, &lc->config.info) );
1539
0
  if (rc != 0) {
1540
0
    DBG_OBJ(SETUP, lc, ul_debug("LOOP_SET_STATUS64 failed: %m"));
1541
0
    return rc;
1542
0
  }
1543
1544
0
  DBG_OBJ(SETUP, lc, ul_debug("LOOP_SET_STATUS64: OK"));
1545
0
  return 0;
1546
0
}
1547
1548
int loopcxt_ioctl_capacity(struct loopdev_cxt *lc)
1549
0
{
1550
0
  int rc, fd = loopcxt_get_fd(lc);
1551
1552
0
  if (fd < 0)
1553
0
    return -EINVAL;
1554
1555
0
  DBG_OBJ(SETUP, lc, ul_debug("calling LOOP_SET_CAPACITY"));
1556
1557
  /* Kernels prior to v2.6.30 don't support this ioctl */
1558
0
  rc = repeat_on_eagain( ioctl(fd, LOOP_SET_CAPACITY, 0) );
1559
0
  if (rc != 0) {
1560
0
    DBG_OBJ(CXT, lc, ul_debug("LOOP_SET_CAPACITY failed: %m"));
1561
0
    return rc;
1562
0
  }
1563
1564
0
  DBG_OBJ(CXT, lc, ul_debug("capacity set"));
1565
0
  return 0;
1566
0
}
1567
1568
int loopcxt_ioctl_dio(struct loopdev_cxt *lc, unsigned long use_dio)
1569
0
{
1570
0
  int rc, fd = loopcxt_get_fd(lc);
1571
1572
0
  if (fd < 0)
1573
0
    return -EINVAL;
1574
1575
0
  DBG_OBJ(SETUP, lc, ul_debug("calling LOOP_SET_DIRECT_IO"));
1576
1577
  /* Kernels prior to v4.4 don't support this ioctl */
1578
0
  rc = repeat_on_eagain( ioctl(fd, LOOP_SET_DIRECT_IO, use_dio) );
1579
0
  if (rc != 0) {
1580
0
    DBG_OBJ(CXT, lc, ul_debug("LOOP_SET_DIRECT_IO failed: %m"));
1581
0
    return rc;
1582
0
  }
1583
1584
0
  DBG_OBJ(CXT, lc, ul_debug("direct io set"));
1585
0
  return 0;
1586
0
}
1587
1588
/*
1589
 * Kernel uses "unsigned long" as ioctl arg, but we use u64 for all sizes to
1590
 * keep loopdev internal API simple.
1591
 */
1592
int loopcxt_ioctl_blocksize(struct loopdev_cxt *lc, uint64_t blocksize)
1593
0
{
1594
0
  int rc, fd = loopcxt_get_fd(lc);
1595
1596
0
  if (fd < 0)
1597
0
    return -EINVAL;
1598
1599
0
  DBG_OBJ(SETUP, lc, ul_debug("calling LOOP_SET_BLOCK_SIZE"));
1600
1601
0
  rc = repeat_on_eagain(
1602
0
    ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) blocksize) );
1603
0
  if (rc != 0) {
1604
0
    DBG_OBJ(CXT, lc, ul_debug("LOOP_SET_BLOCK_SIZE failed: %m"));
1605
0
    return rc;
1606
0
  }
1607
1608
0
  DBG_OBJ(CXT, lc, ul_debug("logical block size set"));
1609
0
  return 0;
1610
0
}
1611
1612
/*
1613
 * @lc: context
1614
 * @nr: returns loop device number
1615
 *
1616
 * Extracts the loop device number from the device path.
1617
 * Supports both /dev/loop<N> and /dev/loop/<N> formats.
1618
 *
1619
 * Returns: 0 on success, <0 on error
1620
 */
1621
static int loopcxt_get_device_nr(struct loopdev_cxt *lc, int *nr)
1622
0
{
1623
0
  const char *p, *dev;
1624
0
  int rc = -EINVAL;
1625
1626
0
  errno = 0;
1627
0
  if (!lc || !nr)
1628
0
    return rc;
1629
1630
0
  dev = loopcxt_get_device(lc);
1631
0
  if (!dev)
1632
0
    goto done;
1633
1634
0
  p = strrchr(dev, '/');
1635
0
  if (!p)
1636
0
    goto done;
1637
1638
0
  if (sscanf(p, "/loop%d", nr) != 1 && sscanf(p, "/%d", nr) != 1)
1639
0
    goto done;
1640
1641
0
  if (*nr < 0)
1642
0
    goto done;
1643
0
  rc = 0;
1644
0
done:
1645
0
  if (rc && !errno)
1646
0
    errno = -rc;
1647
0
  DBG_OBJ(CXT, lc, ul_debug("get_device_nr [nr=%d]", *nr));
1648
0
  return rc;
1649
0
}
1650
1651
int loopcxt_detach_device(struct loopdev_cxt *lc)
1652
0
{
1653
0
  int rc, fd = loopcxt_get_fd(lc);
1654
1655
0
  if (fd < 0)
1656
0
    return -EINVAL;
1657
1658
0
  DBG_OBJ(SETUP, lc, ul_debug("calling LOOP_CLR_FD"));
1659
1660
0
  rc = repeat_on_eagain( ioctl(fd, LOOP_CLR_FD, 0) );
1661
0
  if (rc != 0) {
1662
0
    DBG_OBJ(CXT, lc, ul_debug("LOOP_CLR_FD failed: %m"));
1663
0
    return rc;
1664
0
  }
1665
1666
0
  DBG_OBJ(CXT, lc, ul_debug("device removed"));
1667
0
  return 0;
1668
0
}
1669
1670
int loopcxt_remove_device(struct loopdev_cxt *lc)
1671
0
{
1672
0
  int rc = -EINVAL;
1673
0
  int ctl, nr = -1;
1674
1675
0
  if (!(lc->flags & LOOPDEV_FL_CONTROL)) {
1676
0
    rc = -ENOSYS;
1677
0
    goto done;
1678
0
  }
1679
1680
0
  rc = loopcxt_get_device_nr(lc, &nr);
1681
0
  if (rc)
1682
0
    goto done;
1683
1684
0
  ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1685
0
  if (ctl >= 0) {
1686
0
    DBG_OBJ(CXT, lc, ul_debug("remove_device %d", nr));
1687
0
    rc = ioctl(ctl, LOOP_CTL_REMOVE, nr);
1688
0
    close(ctl);
1689
0
  }
1690
0
  lc->control_ok = rc >= 0 ? 1 : 0;
1691
0
done:
1692
0
  DBG_OBJ(CXT, lc, ul_debug("remove_device done [rc=%d]", rc));
1693
0
  return rc;
1694
0
}
1695
1696
int loopcxt_add_device(struct loopdev_cxt *lc)
1697
0
{
1698
0
  int rc = -EINVAL;
1699
0
  int ctl, nr = -1;
1700
1701
0
  if (!(lc->flags & LOOPDEV_FL_CONTROL)) {
1702
0
    rc = -ENOSYS;
1703
0
    goto done;
1704
0
  }
1705
1706
0
  rc = loopcxt_get_device_nr(lc, &nr);
1707
0
  if (rc)
1708
0
    goto done;
1709
1710
0
  ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1711
0
  if (ctl >= 0) {
1712
0
    DBG_OBJ(CXT, lc, ul_debug("add_device %d", nr));
1713
0
    rc = ioctl(ctl, LOOP_CTL_ADD, nr);
1714
0
    close(ctl);
1715
0
  }
1716
0
  lc->control_ok = rc >= 0 ? 1 : 0;
1717
0
done:
1718
0
  DBG_OBJ(CXT, lc, ul_debug("add_device done [rc=%d]", rc));
1719
0
  return rc;
1720
0
}
1721
1722
/*
1723
 * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older
1724
 * kernels we have to check all loop devices to found unused one.
1725
 *
1726
 * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484.
1727
 *
1728
 * Returns: 0 = success, < 0 error
1729
 */
1730
int loopcxt_find_unused(struct loopdev_cxt *lc)
1731
0
{
1732
0
  int rc = -1;
1733
1734
0
  DBG_OBJ(CXT, lc, ul_debug("find_unused requested"));
1735
1736
0
  if (lc->flags & LOOPDEV_FL_CONTROL) {
1737
0
    int ctl;
1738
1739
0
    DBG_OBJ(CXT, lc, ul_debug("using loop-control"));
1740
1741
0
    ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC);
1742
0
    if (ctl >= 0)
1743
0
      rc = ioctl(ctl, LOOP_CTL_GET_FREE);
1744
0
    else
1745
0
      rc = -errno;
1746
0
    if (rc >= 0) {
1747
0
      char name[16];
1748
0
      snprintf(name, sizeof(name), "loop%d", rc);
1749
1750
0
      rc = loopiter_set_device(lc, name);
1751
0
    }
1752
0
    lc->control_ok = ctl >= 0 && rc == 0 ? 1 : 0;
1753
0
    if (ctl >= 0)
1754
0
      close(ctl);
1755
0
    DBG_OBJ(CXT, lc, ul_debug("find_unused by loop-control [rc=%d]", rc));
1756
0
  }
1757
1758
0
  if (rc < 0 && rc != -EACCES) {
1759
0
    DBG_OBJ(CXT, lc, ul_debug("using loop scan"));
1760
0
    rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE);
1761
0
    if (rc)
1762
0
      return rc;
1763
1764
0
    rc = loopcxt_next(lc);
1765
0
    loopcxt_deinit_iterator(lc);
1766
0
    DBG_OBJ(CXT, lc, ul_debug("find_unused by scan [rc=%d]", rc));
1767
0
    if (rc)
1768
0
      return -ENOENT;
1769
0
  }
1770
0
  return rc;
1771
0
}
1772
1773
1774
1775
/*
1776
 * Return: TRUE/FALSE
1777
 */
1778
int loopdev_is_autoclear(const char *device)
1779
0
{
1780
0
  struct loopdev_cxt lc;
1781
0
  int rc;
1782
1783
0
  if (!device)
1784
0
    return 0;
1785
1786
0
  rc = loopcxt_init(&lc, 0);
1787
0
  if (!rc)
1788
0
    rc = loopcxt_set_device(&lc, device);
1789
0
  if (!rc)
1790
0
    rc = loopcxt_is_autoclear(&lc);
1791
1792
0
  loopcxt_deinit(&lc);
1793
0
  return rc;
1794
0
}
1795
1796
char *loopdev_get_backing_file(const char *device)
1797
0
{
1798
0
  struct loopdev_cxt lc;
1799
0
  char *res = NULL;
1800
1801
0
  if (!device)
1802
0
    return NULL;
1803
0
  if (loopcxt_init(&lc, 0))
1804
0
    return NULL;
1805
0
  if (loopcxt_set_device(&lc, device) == 0)
1806
0
    res = loopcxt_get_backing_file(&lc);
1807
1808
0
  loopcxt_deinit(&lc);
1809
0
  return res;
1810
0
}
1811
1812
/*
1813
 * Returns: TRUE/FALSE
1814
 */
1815
int loopdev_has_backing_file(const char *device)
1816
0
{
1817
0
  char *tmp = loopdev_get_backing_file(device);
1818
1819
0
  if (tmp) {
1820
0
    free(tmp);
1821
0
    return 1;
1822
0
  }
1823
0
  return 0;
1824
0
}
1825
1826
/*
1827
 * Returns: TRUE/FALSE
1828
 */
1829
int loopdev_is_used(const char *device, const char *filename,
1830
        uint64_t offset, uint64_t sizelimit, int flags)
1831
0
{
1832
0
  struct loopdev_cxt lc;
1833
0
  struct stat st;
1834
0
  int rc = 0;
1835
1836
0
  if (!device || !filename)
1837
0
    return 0;
1838
1839
0
  rc = loopcxt_init(&lc, 0);
1840
0
  if (!rc)
1841
0
    rc = loopcxt_set_device(&lc, device);
1842
0
  if (rc)
1843
0
    return rc;
1844
1845
0
  rc = !stat(filename, &st);
1846
0
  rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, sizelimit, flags);
1847
1848
0
  loopcxt_deinit(&lc);
1849
0
  return rc;
1850
0
}
1851
1852
/*
1853
 * Returns: 0 = success, < 0 error
1854
 */
1855
int loopdev_detach(const char *device)
1856
0
{
1857
0
  struct loopdev_cxt lc;
1858
0
  int rc;
1859
1860
0
  if (!device)
1861
0
    return -EINVAL;
1862
1863
0
  rc = loopcxt_init(&lc, 0);
1864
0
  if (!rc)
1865
0
    rc = loopcxt_set_device(&lc, device);
1866
0
  if (!rc)
1867
0
    rc = loopcxt_detach_device(&lc);
1868
0
  loopcxt_deinit(&lc);
1869
0
  return rc;
1870
0
}
1871
1872
/*
1873
 * Returns: 0 = success, < 0 error, 1 not found
1874
 */
1875
int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename,
1876
         uint64_t offset, uint64_t sizelimit, int flags)
1877
0
{
1878
0
  int rc, hasst;
1879
0
  struct stat st;
1880
1881
0
  if (!filename)
1882
0
    return -EINVAL;
1883
1884
0
  hasst = !stat(filename, &st);
1885
1886
0
  rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1887
0
  if (rc)
1888
0
    return rc;
1889
1890
0
  while ((rc = loopcxt_next(lc)) == 0) {
1891
1892
0
    if (loopcxt_is_used(lc, hasst ? &st : NULL,
1893
0
            filename, offset, sizelimit, flags))
1894
0
      break;
1895
0
  }
1896
1897
0
  loopcxt_deinit_iterator(lc);
1898
0
  return rc;
1899
0
}
1900
1901
/*
1902
 * Returns: 0 = not found, < 0 error, 1 found, 2 found full size and offset match
1903
 */
1904
int loopcxt_find_overlap(struct loopdev_cxt *lc, const char *filename,
1905
         uint64_t offset, uint64_t sizelimit)
1906
0
{
1907
0
  int rc, hasst;
1908
0
  struct stat st;
1909
1910
0
  if (!filename)
1911
0
    return -EINVAL;
1912
1913
0
  DBG_OBJ(CXT, lc, ul_debug("find_overlap requested"));
1914
0
  hasst = !stat(filename, &st);
1915
1916
0
  rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
1917
0
  if (rc)
1918
0
    return rc;
1919
1920
0
  while ((rc = loopcxt_next(lc)) == 0) {
1921
0
    uint64_t lc_sizelimit, lc_offset;
1922
1923
0
    rc = loopcxt_is_used(lc, hasst ? &st : NULL,
1924
0
             filename, offset, sizelimit, 0);
1925
    /*
1926
     * Either the loopdev is unused or we've got an error which can
1927
     * happen when we are racing with device autoclear. Just ignore
1928
     * this loopdev...
1929
     */
1930
0
    if (rc <= 0)
1931
0
      continue;
1932
1933
0
    DBG_OBJ(CXT, lc, ul_debug("found %s backed by %s",
1934
0
      loopcxt_get_device(lc), filename));
1935
1936
0
    rc = loopcxt_get_offset(lc, &lc_offset);
1937
0
    if (rc) {
1938
0
      DBG_OBJ(CXT, lc, ul_debug("failed to get offset for device %s",
1939
0
        loopcxt_get_device(lc)));
1940
0
      continue;
1941
0
    }
1942
0
    rc = loopcxt_get_sizelimit(lc, &lc_sizelimit);
1943
0
    if (rc) {
1944
0
      DBG_OBJ(CXT, lc, ul_debug("failed to get sizelimit for device %s",
1945
0
        loopcxt_get_device(lc)));
1946
0
      continue;
1947
0
    }
1948
1949
    /* full match */
1950
0
    if (lc_sizelimit == sizelimit && lc_offset == offset) {
1951
0
      DBG_OBJ(CXT, lc, ul_debug("overlapping loop device %s (full match)",
1952
0
            loopcxt_get_device(lc)));
1953
0
      rc = 2;
1954
0
      goto found;
1955
0
    }
1956
1957
    /* overlap */
1958
0
    if (lc_sizelimit != 0 && offset >= lc_offset + lc_sizelimit)
1959
0
      continue;
1960
0
    if (sizelimit != 0 && offset + sizelimit <= lc_offset)
1961
0
      continue;
1962
1963
0
    DBG_OBJ(CXT, lc, ul_debug("overlapping loop device %s",
1964
0
      loopcxt_get_device(lc)));
1965
0
      rc = 1;
1966
0
      goto found;
1967
0
  }
1968
1969
0
  if (rc == 1)
1970
0
    rc = 0; /* not found */
1971
0
found:
1972
0
  loopcxt_deinit_iterator(lc);
1973
0
  DBG_OBJ(CXT, lc, ul_debug("find_overlap done [rc=%d]", rc));
1974
0
  return rc;
1975
0
}
1976
1977
/*
1978
 * Returns allocated string with device name
1979
 */
1980
char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, uint64_t sizelimit, int flags)
1981
0
{
1982
0
  struct loopdev_cxt lc;
1983
0
  char *res = NULL;
1984
1985
0
  if (!filename)
1986
0
    return NULL;
1987
1988
0
  if (loopcxt_init(&lc, 0))
1989
0
    return NULL;
1990
0
  if (loopcxt_find_by_backing_file(&lc, filename, offset, sizelimit, flags) == 0)
1991
0
    res = loopcxt_strdup_device(&lc);
1992
0
  loopcxt_deinit(&lc);
1993
1994
0
  return res;
1995
0
}
1996
1997
/*
1998
 * Returns number of loop devices associated with @file, if only one loop
1999
 * device is associated with the given @filename and @loopdev is not NULL then
2000
 * @loopdev returns name of the device.
2001
 */
2002
int loopdev_count_by_backing_file(const char *filename, char **loopdev)
2003
0
{
2004
0
  struct loopdev_cxt lc;
2005
0
  int count = 0, rc;
2006
2007
0
  if (!filename)
2008
0
    return -1;
2009
2010
0
  rc = loopcxt_init(&lc, 0);
2011
0
  if (rc)
2012
0
    return rc;
2013
0
  if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED))
2014
0
    return -1;
2015
2016
0
  while(loopcxt_next(&lc) == 0) {
2017
0
    char *backing = loopcxt_get_backing_file(&lc);
2018
2019
0
    if (!backing || strcmp(backing, filename) != 0) {
2020
0
      free(backing);
2021
0
      continue;
2022
0
    }
2023
2024
0
    free(backing);
2025
0
    if (loopdev && count == 0)
2026
0
      *loopdev = loopcxt_strdup_device(&lc);
2027
0
    count++;
2028
0
  }
2029
2030
0
  loopcxt_deinit(&lc);
2031
2032
0
  if (loopdev && count > 1) {
2033
0
    free(*loopdev);
2034
    *loopdev = NULL;
2035
0
  }
2036
0
  return count;
2037
0
}
2038
2039
#ifdef TEST_PROGRAM_LOOPDEV
2040
int main(int argc, char *argv[])
2041
{
2042
  if (argc < 2)
2043
    goto usage;
2044
2045
  if (strcmp(argv[1], "--is-loopdev") == 0 && argc == 3)
2046
    printf("%s: %s\n", argv[2], is_loopdev(argv[2]) ? "OK" : "FAIL");
2047
  else
2048
    goto usage;
2049
2050
  return EXIT_SUCCESS;
2051
usage:
2052
  fprintf(stderr, "usage: %1$s --is-loopdev <dev>\n",
2053
      program_invocation_short_name);
2054
  return EXIT_FAILURE;
2055
}
2056
#endif
2057