• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14208534426

01 Apr 2025 06:15PM UTC coverage: 71.955% (+0.04%) from 71.916%
14208534426

push

github

web-flow
unit: don't bother determining unit install state for transient or perpetual units (#36504)

17 of 20 new or added lines in 1 file covered. (85.0%)

1170 existing lines in 45 files now uncovered.

296855 of 412557 relevant lines covered (71.95%)

653293.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.8
/src/shared/loop-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#if HAVE_VALGRIND_MEMCHECK_H
4
#include <valgrind/memcheck.h>
5
#endif
6

7
#include <errno.h>
8
#include <fcntl.h>
9
#include <linux/blkpg.h>
10
#include <linux/loop.h>
11
#include <sys/file.h>
12
#include <sys/ioctl.h>
13
#include <unistd.h>
14

15
#include "sd-device.h"
16

17
#include "alloc-util.h"
18
#include "blockdev-util.h"
19
#include "data-fd-util.h"
20
#include "device-util.h"
21
#include "devnum-util.h"
22
#include "dissect-image.h"
23
#include "env-util.h"
24
#include "errno-util.h"
25
#include "fd-util.h"
26
#include "fs-util.h"
27
#include "fileio.h"
28
#include "loop-util.h"
29
#include "missing_fs.h"
30
#include "parse-util.h"
31
#include "path-util.h"
32
#include "random-util.h"
33
#include "stat-util.h"
34
#include "stdio-util.h"
35
#include "string-util.h"
36
#include "tmpfile-util.h"
37

38
static void cleanup_clear_loop_close(int *fd) {
1,901✔
39
        if (*fd < 0)
1,901✔
40
                return;
41

42
        (void) ioctl(*fd, LOOP_CLR_FD);
×
43
        (void) safe_close(*fd);
×
44
}
45

46
static int loop_is_bound(int fd) {
1,901✔
47
        struct loop_info64 info;
1,901✔
48

49
        if (ioctl(ASSERT_FD(fd), LOOP_GET_STATUS64, &info) < 0) {
1,901✔
50
                if (errno == ENXIO)
1,901✔
51
                        return false; /* not bound! */
1,901✔
52

53
                return -errno;
×
54
        }
55

56
        return true; /* bound! */
57
}
58

59
static int open_lock_fd(int primary_fd, int operation) {
1,925✔
60
        _cleanup_close_ int lock_fd = -EBADF;
1,925✔
61

62
        assert(IN_SET(operation & ~LOCK_NB, LOCK_SH, LOCK_EX));
1,925✔
63

64
        lock_fd = fd_reopen(ASSERT_FD(primary_fd), O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
1,925✔
65
        if (lock_fd < 0)
1,925✔
66
                return lock_fd;
67

68
        if (flock(lock_fd, operation) < 0)
1,925✔
69
                return -errno;
×
70

71
        return TAKE_FD(lock_fd);
72
}
73

74
static int loop_configure_verify_direct_io(int fd, const struct loop_config *c) {
1,901✔
75
        assert(fd >= 0);
1,901✔
76
        assert(c);
1,901✔
77

78
        if (FLAGS_SET(c->info.lo_flags, LO_FLAGS_DIRECT_IO)) {
1,901✔
79
                struct loop_info64 info;
1,900✔
80

81
                if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0)
1,900✔
82
                        return log_debug_errno(errno, "Failed to issue LOOP_GET_STATUS64: %m");
×
83

84
#if HAVE_VALGRIND_MEMCHECK_H
85
                VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
86
#endif
87

88
                /* On older kernels (<= 5.3) it was necessary to set the block size of the loopback block
89
                 * device to the logical block size of the underlying file system. Since there was no nice
90
                 * way to query the value, we are not bothering to do this however. On newer kernels the
91
                 * block size is propagated automatically and does not require intervention from us. We'll
92
                 * check here if enabling direct IO worked, to make this easily debuggable however.
93
                 *
94
                 * (Should anyone really care and actually wants direct IO on old kernels: it might be worth
95
                 * enabling direct IO with iteratively larger block sizes until it eventually works.)
96
                 *
97
                 * On older kernels (e.g.: 5.10) when this is attempted on a file stored on a dm-crypt
98
                 * backed partition the kernel will start returning I/O errors when accessing the mounted
99
                 * loop device, so return a recognizable error that causes the operation to be started
100
                 * from scratch without the LO_FLAGS_DIRECT_IO flag. */
101
                if (!FLAGS_SET(info.lo_flags, LO_FLAGS_DIRECT_IO))
1,900✔
102
                        return log_debug_errno(
×
103
                                        SYNTHETIC_ERRNO(ENOANO),
104
                                        "Could not enable direct IO mode, retrying in buffered IO mode.");
105
        }
106

107
        return 0;
108
}
109

110
static int loop_configure_verify(int fd, const struct loop_config *c) {
1,901✔
111
        bool broken = false;
1,901✔
112
        int r;
1,901✔
113

114
        assert(fd >= 0);
1,901✔
115
        assert(c);
1,901✔
116

117
        if (c->block_size != 0) {
1,901✔
118
                uint32_t ssz;
1,901✔
119

120
                r = blockdev_get_sector_size(fd, &ssz);
1,901✔
121
                if (r < 0)
1,901✔
122
                        return r;
×
123

124
                if (ssz != c->block_size) {
1,901✔
125
                        log_debug("LOOP_CONFIGURE didn't honour requested block size %" PRIu32 ", got %" PRIu32 " instead. Ignoring.", c->block_size, ssz);
1,901✔
126
                        broken = true;
127
                }
128
        }
129

130
        if (c->info.lo_sizelimit != 0) {
1,901✔
131
                /* Kernel 5.8 vanilla doesn't properly propagate the size limit into the
132
                 * block device. If it's used, let's immediately check if it had the desired
133
                 * effect hence. And if not use classic LOOP_SET_STATUS64. */
134
                uint64_t z;
68✔
135

136
                r = blockdev_get_device_size(fd, &z);
68✔
137
                if (r < 0)
68✔
138
                        return r;
×
139

140
                if (z != c->info.lo_sizelimit) {
68✔
141
                        log_debug("LOOP_CONFIGURE is broken, doesn't honour .info.lo_sizelimit. Falling back to LOOP_SET_STATUS64.");
68✔
142
                        broken = true;
143
                }
144
        }
145

146
        if (FLAGS_SET(c->info.lo_flags, LO_FLAGS_PARTSCAN)) {
1,901✔
147
                /* Kernel 5.8 vanilla doesn't properly propagate the partition scanning flag
148
                 * into the block device. Let's hence verify if things work correctly here
149
                 * before returning. */
150

151
                r = blockdev_partscan_enabled_fd(fd);
1,759✔
152
                if (r < 0)
1,759✔
153
                        return r;
154
                if (r == 0) {
1,759✔
155
                        log_debug("LOOP_CONFIGURE is broken, doesn't honour LO_FLAGS_PARTSCAN. Falling back to LOOP_SET_STATUS64.");
×
156
                        broken = true;
157
                }
158
        }
159

160
        r = loop_configure_verify_direct_io(fd, c);
1,901✔
161
        if (r < 0)
1,901✔
162
                return r;
163

164
        return !broken;
1,901✔
165
}
166

167
static int loop_configure_fallback(int fd, const struct loop_config *c) {
×
168
        struct loop_info64 info_copy;
×
169
        int r;
×
170

171
        assert(fd >= 0);
×
172
        assert(c);
×
173

174
        /* Only some of the flags LOOP_CONFIGURE can set are also settable via LOOP_SET_STATUS64, hence mask
175
         * them out. */
176
        info_copy = c->info;
×
177
        info_copy.lo_flags &= LOOP_SET_STATUS_SETTABLE_FLAGS;
×
178

179
        /* Since kernel commit 5db470e229e22b7eda6e23b5566e532c96fb5bc3 (kernel v5.0) the LOOP_SET_STATUS64
180
         * ioctl can return EAGAIN in case we change the info.lo_offset field, if someone else is accessing the
181
         * block device while we try to reconfigure it. This is a pretty common case, since udev might
182
         * instantly start probing the device as soon as we attach an fd to it. Hence handle it in two ways:
183
         * first, let's take the BSD lock to ensure that udev will not step in between the point in
184
         * time where we attach the fd and where we reconfigure the device. Secondly, let's wait 50ms on
185
         * EAGAIN and retry. The former should be an efficient mechanism to avoid we have to wait 50ms
186
         * needlessly if we are just racing against udev. The latter is protection against all other cases,
187
         * i.e. peers that do not take the BSD lock. */
188

189
        for (unsigned n_attempts = 0;;) {
×
190
                if (ioctl(fd, LOOP_SET_STATUS64, &info_copy) >= 0)
×
191
                        break;
192

193
                if (errno != EAGAIN || ++n_attempts >= 64)
×
194
                        return log_debug_errno(errno, "Failed to configure loopback block device: %m");
×
195

196
                /* Sleep some random time, but at least 10ms, at most 250ms. Increase the delay the more
197
                 * failed attempts we see */
198
                (void) usleep_safe(UINT64_C(10) * USEC_PER_MSEC +
×
199
                              random_u64_range(UINT64_C(240) * USEC_PER_MSEC * n_attempts/64));
×
200
        }
201

202
        /* Work around a kernel bug, where changing offset/size of the loopback device doesn't correctly
203
         * invalidate the buffer cache. For details see:
204
         *
205
         *     https://android.googlesource.com/platform/system/apex/+/bef74542fbbb4cd629793f4efee8e0053b360570
206
         *
207
         * This was fixed in kernel 5.0, see:
208
         *
209
         *     https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5db470e229e22b7eda6e23b5566e532c96fb5bc3
210
         *
211
         * We'll run the work-around here in the legacy LOOP_SET_STATUS64 codepath. In the LOOP_CONFIGURE
212
         * codepath above it should not be necessary. */
213
        if (c->info.lo_offset != 0 || c->info.lo_sizelimit != 0)
×
214
                if (ioctl(fd, BLKFLSBUF, 0) < 0)
×
215
                        log_debug_errno(errno, "Failed to issue BLKFLSBUF ioctl, ignoring: %m");
×
216

217
        /* If a block size is requested then try to configure it. If that doesn't work, ignore errors, but
218
         * afterwards, let's validate what is in effect, and if it doesn't match what we want, fail */
219
        if (c->block_size != 0) {
×
220
                uint32_t ssz;
×
221

222
                if (ioctl(fd, LOOP_SET_BLOCK_SIZE, (unsigned long) c->block_size) < 0)
×
223
                        log_debug_errno(errno, "Failed to set sector size, ignoring: %m");
×
224

225
                r = blockdev_get_sector_size(fd, &ssz);
×
226
                if (r < 0)
×
227
                        return log_debug_errno(r, "Failed to read sector size: %m");
×
228
                if (ssz != c->block_size)
×
229
                        return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Sector size of loopback device doesn't match what we requested, refusing.");
×
230
        }
231

232
        /* LO_FLAGS_DIRECT_IO is a flags we need to configure via explicit ioctls. */
233
        if (FLAGS_SET(c->info.lo_flags, LO_FLAGS_DIRECT_IO))
×
234
                if (ioctl(fd, LOOP_SET_DIRECT_IO, 1UL) < 0)
×
235
                        log_debug_errno(errno, "Failed to enable direct IO mode, ignoring: %m");
×
236

237
        return loop_configure_verify_direct_io(fd, c);
×
238
}
239

240
static int loop_configure(
1,901✔
241
                int nr,
242
                int open_flags,
243
                int lock_op,
244
                const struct loop_config *c,
245
                LoopDevice **ret) {
246

247
        static bool loop_configure_broken = false;
1,901✔
248

249
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1,901✔
250
        _cleanup_(cleanup_clear_loop_close) int loop_with_fd = -EBADF; /* This must be declared before lock_fd. */
×
251
        _cleanup_close_ int fd = -EBADF, lock_fd = -EBADF;
3,802✔
252
        _cleanup_free_ char *node = NULL;
1,901✔
253
        uint64_t diskseq = 0;
1,901✔
254
        dev_t devno;
1,901✔
255
        int r;
1,901✔
256

257
        assert(nr >= 0);
1,901✔
258
        assert(c);
1,901✔
259
        assert(ret);
1,901✔
260

261
        if (asprintf(&node, "/dev/loop%i", nr) < 0)
1,901✔
262
                return log_oom_debug();
×
263

264
        r = sd_device_new_from_devname(&dev, node);
1,901✔
265
        if (r < 0)
1,901✔
266
                return log_debug_errno(r, "Failed to create sd_device object for \"%s\": %m", node);
×
267

268
        r = sd_device_get_devnum(dev, &devno);
1,901✔
269
        if (r < 0)
1,901✔
270
                return log_device_debug_errno(dev, r, "Failed to get devnum: %m");
×
271

272
        fd = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
1,901✔
273
        if (fd < 0)
1,901✔
274
                return log_device_debug_errno(dev, fd, "Failed to open device: %m");
×
275

276
        /* Let's lock the device before we do anything. We take the BSD lock on a second, separately opened
277
         * fd for the device. udev after all watches for close() events (specifically IN_CLOSE_WRITE) on
278
         * block devices to reprobe them, hence by having a separate fd we will later close() we can ensure
279
         * we trigger udev after everything is done. If we'd lock our own fd instead and keep it open for a
280
         * long time udev would possibly never run on it again, even though the fd is unlocked, simply
281
         * because we never close() it. It also has the nice benefit we can use the _cleanup_close_ logic to
282
         * automatically release the lock, after we are done. */
283
        lock_fd = open_lock_fd(fd, LOCK_EX);
1,901✔
284
        if (lock_fd < 0)
1,901✔
285
                return log_device_debug_errno(dev, lock_fd, "Failed to acquire lock: %m");
×
286

287
        log_device_debug(dev, "Acquired exclusive lock.");
1,927✔
288

289
        /* Let's see if backing file is really unattached. Someone may already attach a backing file without
290
         * taking BSD lock. */
291
        r = loop_is_bound(fd);
1,901✔
292
        if (r < 0)
1,901✔
293
                return log_device_debug_errno(dev, r, "Failed to check if the loopback block device is bound: %m");
×
294
        if (r > 0)
1,901✔
295
                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EBUSY),
×
296
                                              "The loopback block device is already bound, ignoring.");
297

298
        /* Let's see if the device is really detached, i.e. currently has no associated partition block
299
         * devices. On various kernels (such as 5.8) it is possible to have a loopback block device that
300
         * superficially is detached but still has partition block devices associated for it. Let's then
301
         * manually remove the partitions via BLKPG, and tell the caller we did that via EUCLEAN, so they try
302
         * again. */
303
        r = block_device_remove_all_partitions(dev, fd);
1,901✔
304
        if (r < 0)
1,901✔
305
                return log_device_debug_errno(dev, r, "Failed to remove partitions on the loopback block device: %m");
×
306
        if (r > 0)
1,901✔
307
                /* Removed all partitions. Let's report this to the caller, to try again, and count this as
308
                 * an attempt. */
309
                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EUCLEAN),
×
310
                                              "Removed partitions on the loopback block device.");
311

312
        if (!loop_configure_broken) {
1,901✔
313
                if (ioctl(fd, LOOP_CONFIGURE, c) < 0) {
1,901✔
314
                        /* Do fallback only if LOOP_CONFIGURE is not supported, propagate all other errors. */
315
                        if (!ERRNO_IS_IOCTL_NOT_SUPPORTED(errno))
×
316
                                return log_device_debug_errno(dev, errno, "ioctl(LOOP_CONFIGURE) failed: %m");
×
317

318
                        loop_configure_broken = true;
×
319
                } else {
320
                        loop_with_fd = TAKE_FD(fd);
1,901✔
321

322
                        r = loop_configure_verify(loop_with_fd, c);
1,901✔
323
                        if (r < 0)
1,901✔
324
                                return log_device_debug_errno(dev, r, "Failed to verify if loopback block device is correctly configured: %m");
×
325
                        if (r == 0) {
1,901✔
326
                                /* LOOP_CONFIGURE doesn't work. Remember that. */
327
                                loop_configure_broken = true;
×
328

329
                                /* We return EBUSY here instead of retrying immediately with LOOP_SET_FD,
330
                                 * because LOOP_CLR_FD is async: if the operation cannot be executed right
331
                                 * away it just sets the autoclear flag on the device. This means there's a
332
                                 * good chance we cannot actually reuse the loopback device right-away. Hence
333
                                 * let's assume it's busy, avoid the trouble and let the calling loop call us
334
                                 * again with a new, likely unused device. */
335
                                return -EBUSY;
×
336
                        }
337
                }
338
        }
339

340
        if (loop_configure_broken) {
1,901✔
341
                if (ioctl(fd, LOOP_SET_FD, c->fd) < 0)
×
342
                        return log_device_debug_errno(dev, errno, "ioctl(LOOP_SET_FD) failed: %m");
×
343

344
                loop_with_fd = TAKE_FD(fd);
×
345

346
                r = loop_configure_fallback(loop_with_fd, c);
×
347
                if (r < 0)
×
348
                        return r;
349
        }
350

351
        r = fd_get_diskseq(loop_with_fd, &diskseq);
1,901✔
352
        if (r < 0 && r != -EOPNOTSUPP)
1,901✔
353
                return log_device_debug_errno(dev, r, "Failed to get diskseq: %m");
×
354

355
        switch (lock_op & ~LOCK_NB) {
1,901✔
356
        case LOCK_EX: /* Already in effect */
357
                break;
358
        case LOCK_SH: /* Downgrade */
1,799✔
359
                if (flock(lock_fd, lock_op) < 0)
1,799✔
360
                        return log_device_debug_errno(dev, errno, "Failed to downgrade lock level: %m");
×
361
                break;
362
        case LOCK_UN: /* Release */
×
363
                lock_fd = safe_close(lock_fd);
×
364
                break;
365
        default:
×
366
                assert_not_reached();
×
367
        }
368

369
        uint64_t device_size;
1,901✔
370
        r = blockdev_get_device_size(loop_with_fd, &device_size);
1,901✔
371
        if (r < 0)
1,901✔
372
                return log_device_debug_errno(dev, r, "Failed to get loopback device size: %m");
×
373

374
        LoopDevice *d = new(LoopDevice, 1);
1,901✔
375
        if (!d)
1,901✔
376
                return log_oom_debug();
×
377

378
        *d = (LoopDevice) {
1,901✔
379
                .n_ref = 1,
380
                .fd = TAKE_FD(loop_with_fd),
1,901✔
381
                .lock_fd = TAKE_FD(lock_fd),
1,901✔
382
                .node = TAKE_PTR(node),
1,901✔
383
                .nr = nr,
384
                .devno = devno,
385
                .dev = TAKE_PTR(dev),
1,901✔
386
                .diskseq = diskseq,
387
                .sector_size = c->block_size,
1,901✔
388
                .device_size = device_size,
389
                .created = true,
390
        };
391

392
        *ret = TAKE_PTR(d);
1,901✔
393
        return 0;
1,901✔
394
}
395

396
static int loop_device_make_internal(
4,049✔
397
                const char *path,
398
                int fd,
399
                int open_flags,
400
                uint64_t offset,
401
                uint64_t size,
402
                uint32_t sector_size,
403
                uint32_t loop_flags,
404
                int lock_op,
405
                LoopDevice **ret) {
406

407
        _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
×
408
        _cleanup_close_ int reopened_fd = -EBADF, control = -EBADF;
8,098✔
409
        _cleanup_free_ char *backing_file = NULL;
4,049✔
410
        struct loop_config config;
4,049✔
411
        int r, f_flags;
4,049✔
412
        struct stat st;
4,049✔
413

414
        assert(ret);
4,049✔
415
        assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
4,049✔
416

417
        if (fstat(ASSERT_FD(fd), &st) < 0)
4,049✔
418
                return -errno;
×
419

420
        if (S_ISBLK(st.st_mode)) {
4,049✔
421
                if (offset == 0 && IN_SET(size, 0, UINT64_MAX))
2✔
422
                        /* If this is already a block device and we are supposed to cover the whole of it
423
                         * then store an fd to the original open device node — and do not actually create an
424
                         * unnecessary loopback device for it. */
425
                        return loop_device_open_from_fd(fd, open_flags, lock_op, ret);
×
426
        } else {
427
                r = stat_verify_regular(&st);
4,047✔
428
                if (r < 0)
4,047✔
429
                        return r;
430
        }
431

432
        if (path) {
1,921✔
433
                r = path_make_absolute_cwd(path, &backing_file);
1,818✔
434
                if (r < 0)
1,818✔
435
                        return r;
436

437
                path_simplify(backing_file);
1,818✔
438
        } else {
439
                r = fd_get_path(fd, &backing_file);
103✔
440
                if (r < 0)
103✔
441
                        return r;
442
        }
443

444
        f_flags = fcntl(fd, F_GETFL);
1,921✔
445
        if (f_flags < 0)
1,921✔
446
                return -errno;
×
447

448
        if (FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) != FLAGS_SET(f_flags, O_DIRECT)) {
1,921✔
449
                /* If LO_FLAGS_DIRECT_IO is requested, then make sure we have the fd open with O_DIRECT, as
450
                 * that's required. Conversely, if it's off require that O_DIRECT is off too (that's because
451
                 * new kernels will implicitly enable LO_FLAGS_DIRECT_IO if O_DIRECT is set).
452
                 *
453
                 * Our intention here is that LO_FLAGS_DIRECT_IO is the primary knob, and O_DIRECT derived
454
                 * from that automatically. */
455

456
                reopened_fd = fd_reopen(fd, (FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) ? O_DIRECT : 0)|O_CLOEXEC|O_NONBLOCK|open_flags);
102✔
457
                if (reopened_fd < 0) {
102✔
458
                        if (!FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO))
×
459
                                return log_debug_errno(reopened_fd, "Failed to reopen file descriptor without O_DIRECT: %m");
×
460

461
                        /* Some file systems might not support O_DIRECT, let's gracefully continue without it then. */
462
                        log_debug_errno(reopened_fd, "Failed to enable O_DIRECT for backing file descriptor for loopback device. Continuing without.");
×
463
                        loop_flags &= ~LO_FLAGS_DIRECT_IO;
×
464
                } else
465
                        fd = reopened_fd; /* From now on, operate on our new O_DIRECT fd */
466
        }
467

468
        control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
1,921✔
469
        if (control < 0)
1,921✔
470
                return -errno;
20✔
471

472
        if (sector_size == 0)
1,901✔
473
                /* If no sector size is specified, default to the classic default */
474
                sector_size = 512;
×
475
        else if (sector_size == UINT32_MAX) {
1,901✔
476

477
                if (S_ISBLK(st.st_mode))
1,799✔
478
                        /* If the sector size is specified as UINT32_MAX we'll propagate the sector size of
479
                         * the underlying block device. */
480
                        r = blockdev_get_sector_size(fd, &sector_size);
×
481
                else {
482
                        _cleanup_close_ int non_direct_io_fd = -EBADF;
4,049✔
483
                        int probe_fd;
1,799✔
484

485
                        assert(S_ISREG(st.st_mode));
1,799✔
486

487
                        /* If sector size is specified as UINT32_MAX, we'll try to probe the right sector
488
                         * size of the image in question by looking for the GPT partition header at various
489
                         * offsets. This of course only works if the image already has a disk label.
490
                         *
491
                         * So here we actually want to read the file contents ourselves. This is quite likely
492
                         * not going to work if we managed to enable O_DIRECT, because in such a case there
493
                         * are some pretty strict alignment requirements to offset, size and target, but
494
                         * there's no way to query what alignment specifically is actually required. Hence,
495
                         * let's avoid the mess, and temporarily open an fd without O_DIRECT for the probing
496
                         * logic. */
497

498
                        if (FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO)) {
1,799✔
499
                                non_direct_io_fd = fd_reopen(fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1,798✔
500
                                if (non_direct_io_fd < 0)
1,798✔
501
                                        return non_direct_io_fd;
×
502

503
                                probe_fd = non_direct_io_fd;
504
                        } else
505
                                probe_fd = fd;
506

507
                        r = probe_sector_size(probe_fd, &sector_size);
1,799✔
508
                }
509
                if (r < 0)
1,799✔
510
                        return r;
511
        }
512

513
        config = (struct loop_config) {
3,802✔
514
                .fd = fd,
515
                .block_size = sector_size,
516
                .info = {
517
                        /* Use the specified flags, but configure the read-only flag from the open flags, and force autoclear */
518
                        .lo_flags = (loop_flags & ~LO_FLAGS_READ_ONLY) | ((open_flags & O_ACCMODE) == O_RDONLY ? LO_FLAGS_READ_ONLY : 0) | LO_FLAGS_AUTOCLEAR,
1,901✔
519
                        .lo_offset = offset,
520
                        .lo_sizelimit = size == UINT64_MAX ? 0 : size,
1,901✔
521
                },
522
        };
523

524
        /* Loop around LOOP_CTL_GET_FREE, since at the moment we attempt to open the returned device it might
525
         * be gone already, taken by somebody else racing against us. */
526
        for (unsigned n_attempts = 0;;) {
1,901✔
527
                usec_t usec;
1,901✔
528
                int nr;
1,901✔
529

530
                /* Let's take a lock on the control device first. On a busy system, where many programs
531
                 * attempt to allocate a loopback device at the same time, we might otherwise keep looping
532
                 * around relatively heavy operations: asking for a free loopback device, then opening it,
533
                 * validating it, attaching something to it. Let's serialize this whole operation, to make
534
                 * unnecessary busywork less likely. Note that this is just something we do to optimize our
535
                 * own code (and whoever else decides to use LOCK_EX locks for this), taking this lock is not
536
                 * necessary, it just means it's less likely we have to iterate through this loop again and
537
                 * again if our own code races against our own code.
538
                 *
539
                 * Note: our lock protocol is to take the /dev/loop-control lock first, and the block device
540
                 * lock second, if both are taken, and always in this order, to avoid ABBA locking issues. */
541
                if (flock(control, LOCK_EX) < 0)
1,901✔
542
                        return -errno;
×
543

544
                nr = ioctl(control, LOOP_CTL_GET_FREE);
1,901✔
545
                if (nr < 0)
1,901✔
546
                        return -errno;
×
547

548
                r = loop_configure(nr, open_flags, lock_op, &config, &d);
1,901✔
549
                if (r >= 0)
1,901✔
550
                        break;
551

552
                /* -ENODEV or friends: Somebody might've gotten the same number from the kernel, used the
553
                 * device, and called LOOP_CTL_REMOVE on it. Let's retry with a new number.
554
                 * -EBUSY: a file descriptor is already bound to the loopback block device.
555
                 * -EUCLEAN: some left-over partition devices that were cleaned up.
556
                 * -ENOANO: we tried to use LO_FLAGS_DIRECT_IO but the kernel rejected it. */
557
                if (!ERRNO_IS_DEVICE_ABSENT(r) && !IN_SET(r, -EBUSY, -EUCLEAN, -ENOANO))
×
558
                        return r;
559

560
                /* OK, this didn't work, let's try again a bit later, but first release the lock on the
561
                 * control device */
562
                if (flock(control, LOCK_UN) < 0)
×
563
                        return -errno;
×
564

565
                if (++n_attempts >= 64) /* Give up eventually */
×
566
                        return -EBUSY;
567

568
                /* If we failed to enable direct IO mode, let's retry without it. We restart the process as
569
                 * on some combination of kernel version and storage filesystem, the kernel is very unhappy
570
                 * about a failed DIRECT_IO enablement and throws I/O errors. */
571
                if (r == -ENOANO && FLAGS_SET(config.info.lo_flags, LO_FLAGS_DIRECT_IO)) {
×
572
                        config.info.lo_flags &= ~LO_FLAGS_DIRECT_IO;
×
573
                        open_flags &= ~O_DIRECT;
×
574

575
                        int non_direct_io_fd = fd_reopen(config.fd, O_CLOEXEC|O_NONBLOCK|open_flags);
×
576
                        if (non_direct_io_fd < 0)
×
577
                                return log_debug_errno(
×
578
                                                non_direct_io_fd,
579
                                                "Failed to reopen file descriptor without O_DIRECT: %m");
580

581
                        safe_close(reopened_fd);
×
582
                        fd = config.fd = /* For cleanups */ reopened_fd = non_direct_io_fd;
×
583
                }
584

585
                /* Wait some random time, to make collision less likely. Let's pick a random time in the
586
                 * range 0ms…250ms, linearly scaled by the number of failed attempts. */
587
                usec = random_u64_range(UINT64_C(10) * USEC_PER_MSEC +
×
588
                                        UINT64_C(240) * USEC_PER_MSEC * n_attempts/64);
×
589
                log_debug("Trying again after %s.", FORMAT_TIMESPAN(usec, USEC_PER_MSEC));
×
590
                (void) usleep_safe(usec);
×
591
        }
592

593
        d->backing_file = TAKE_PTR(backing_file);
1,901✔
594
        d->backing_inode = st.st_ino;
1,901✔
595
        d->backing_devno = st.st_dev;
1,901✔
596

597
        log_debug("Successfully acquired %s, devno=%u:%u, nr=%i, diskseq=%" PRIu64,
1,901✔
598
                  d->node,
599
                  major(d->devno), minor(d->devno),
600
                  d->nr,
601
                  d->diskseq);
602

603
        *ret = TAKE_PTR(d);
1,901✔
604
        return 0;
1,901✔
605
}
606

607
static uint32_t loop_flags_mangle(uint32_t loop_flags) {
4,049✔
608
        int r;
4,049✔
609

610
        r = getenv_bool("SYSTEMD_LOOP_DIRECT_IO");
4,049✔
611
        if (r < 0 && r != -ENXIO)
4,049✔
612
                log_debug_errno(r, "Failed to parse $SYSTEMD_LOOP_DIRECT_IO, ignoring: %m");
×
613

614
        return UPDATE_FLAG(loop_flags, LO_FLAGS_DIRECT_IO, r != 0); /* Turn on LO_FLAGS_DIRECT_IO by default, unless explicitly configured to off. */
4,049✔
615
}
616

617
int loop_device_make(
102✔
618
                int fd,
619
                int open_flags,
620
                uint64_t offset,
621
                uint64_t size,
622
                uint32_t sector_size,
623
                uint32_t loop_flags,
624
                int lock_op,
625
                LoopDevice **ret) {
626

627
        assert(fd >= 0);
102✔
628
        assert(ret);
102✔
629

630
        return loop_device_make_internal(
102✔
631
                        NULL,
632
                        fd,
633
                        open_flags,
634
                        offset,
635
                        size,
636
                        sector_size,
637
                        loop_flags_mangle(loop_flags),
638
                        lock_op,
639
                        ret);
640
}
641

642
int loop_device_make_by_path_at(
3,947✔
643
                int dir_fd,
644
                const char *path,
645
                int open_flags,
646
                uint32_t sector_size,
647
                uint32_t loop_flags,
648
                int lock_op,
649
                LoopDevice **ret) {
650

651
        int r, basic_flags, direct_flags, rdwr_flags;
3,947✔
652
        _cleanup_close_ int fd = -EBADF;
3,947✔
653
        bool direct = false;
3,947✔
654

655
        assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
3,947✔
656
        assert(path);
3,947✔
657
        assert(ret);
3,947✔
658
        assert(open_flags < 0 || IN_SET(open_flags, O_RDWR, O_RDONLY));
3,947✔
659

660
        /* Passing < 0 as open_flags here means we'll try to open the device writable if we can, retrying
661
         * read-only if we cannot. */
662

663
        loop_flags = loop_flags_mangle(loop_flags);
3,947✔
664

665
        /* Let's open with O_DIRECT if we can. But not all file systems support that, hence fall back to
666
         * non-O_DIRECT mode automatically, if it fails. */
667

668
        basic_flags = O_CLOEXEC|O_NONBLOCK|O_NOCTTY;
3,947✔
669
        direct_flags = FLAGS_SET(loop_flags, LO_FLAGS_DIRECT_IO) ? O_DIRECT : 0;
3,947✔
670
        rdwr_flags = open_flags >= 0 ? open_flags : O_RDWR;
3,947✔
671

672
        fd = xopenat(dir_fd, path, basic_flags|direct_flags|rdwr_flags);
3,947✔
673
        if (fd < 0 && direct_flags != 0) /* If we had O_DIRECT on, and things failed with that, let's immediately try again without */
3,947✔
674
                fd = xopenat(dir_fd, path, basic_flags|rdwr_flags);
2,129✔
675
        else
676
                direct = direct_flags != 0;
×
677
        if (fd < 0) {
3,947✔
678
                r = fd;
1✔
679

680
                /* Retry read-only? */
681
                if (open_flags >= 0 || !(ERRNO_IS_PRIVILEGE(r) || r == -EROFS))
1✔
682
                        return r;
683

684
                fd = xopenat(dir_fd, path, basic_flags|direct_flags|O_RDONLY);
×
685
                if (fd < 0 && direct_flags != 0) /* as above */
×
686
                        fd = xopenat(dir_fd, path, basic_flags|O_RDONLY);
×
687
                else
688
                        direct = direct_flags != 0;
×
689
                if (fd < 0)
×
690
                        return r; /* Propagate original error */
691

692
                open_flags = O_RDONLY;
693
        } else if (open_flags < 0)
3,946✔
694
                open_flags = O_RDWR;
73✔
695

696
        log_debug("Opened '%s' in %s access mode%s, with O_DIRECT %s%s.",
15,608✔
697
                  path,
698
                  open_flags == O_RDWR ? "O_RDWR" : "O_RDONLY",
699
                  open_flags != rdwr_flags ? " (O_RDWR was requested but not allowed)" : "",
700
                  direct ? "enabled" : "disabled",
701
                  direct != (direct_flags != 0) ? " (O_DIRECT was requested but not supported)" : "");
702

703
        return loop_device_make_internal(
3,946✔
704
                        dir_fd == AT_FDCWD ? path : NULL,
705
                        fd,
706
                        open_flags,
707
                        /* offset = */ 0,
708
                        /* size = */ 0,
709
                        sector_size,
710
                        loop_flags,
711
                        lock_op,
712
                        ret);
713
}
714

715
int loop_device_make_by_path_memory(
1✔
716
                const char *path,
717
                int open_flags,
718
                uint32_t sector_size,
719
                uint32_t loop_flags,
720
                int lock_op,
721
                LoopDevice **ret) {
722

723
        _cleanup_close_ int fd = -EBADF, mfd = -EBADF;
1✔
724
        _cleanup_free_ char *fn = NULL;
1✔
725
        struct stat st;
1✔
726
        int r;
1✔
727

728
        assert(path);
1✔
729
        assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
1✔
730
        assert(ret);
1✔
731

732
        loop_flags &= ~LO_FLAGS_DIRECT_IO; /* memfds don't support O_DIRECT, hence LO_FLAGS_DIRECT_IO can't be used either */
1✔
733

734
        fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
1✔
735
        if (fd < 0)
1✔
736
                return -errno;
×
737

738
        if (fstat(fd, &st) < 0)
1✔
739
                return -errno;
×
740

741
        if (!S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode))
1✔
742
                return -EBADF;
743

744
        r = path_extract_filename(path, &fn);
1✔
745
        if (r < 0)
1✔
746
                return r;
747

748
        mfd = memfd_clone_fd(fd, fn, open_flags|O_CLOEXEC);
1✔
749
        if (mfd < 0)
1✔
750
                return mfd;
751

752
        fd = safe_close(fd); /* Let's close the original early */
1✔
753

754
        return loop_device_make_internal(NULL, mfd, open_flags, 0, 0, sector_size, loop_flags, lock_op, ret);
1✔
755
}
756

757
static LoopDevice* loop_device_free(LoopDevice *d) {
1,843✔
758
        _cleanup_close_ int control = -EBADF;
1,843✔
759
        int r;
1,843✔
760

761
        if (!d)
1,843✔
762
                return NULL;
763

764
        /* Release any lock we might have on the device first. We want to open+lock the /dev/loop-control
765
         * device below, but our lock protocol says that if both control and block device locks are taken,
766
         * the control lock needs to be taken first, the block device lock second — in order to avoid ABBA
767
         * locking issues. Moreover, we want to issue LOOP_CLR_FD on the block device further down, and that
768
         * would fail if we had another fd open to the device. */
769
        d->lock_fd = safe_close(d->lock_fd);
1,843✔
770

771
        /* Let's open the control device early, and lock it, so that we can release our block device and
772
         * delete it in a synchronized fashion, and allocators won't needlessly see the block device as free
773
         * while we are about to delete it. */
774
        if (!LOOP_DEVICE_IS_FOREIGN(d) && !d->relinquished) {
1,843✔
775
                control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
1,699✔
776
                if (control < 0)
1,699✔
777
                        log_debug_errno(errno, "Failed to open loop control device, cannot remove loop device '%s', ignoring: %m", strna(d->node));
×
778
                else if (flock(control, LOCK_EX) < 0)
1,699✔
779
                        log_debug_errno(errno, "Failed to lock loop control device, ignoring: %m");
×
780
        }
781

782
        /* Then let's release the loopback block device */
783
        if (d->fd >= 0) {
1,843✔
784
                /* Implicitly sync the device, since otherwise in-flight blocks might not get written */
785
                if (fsync(d->fd) < 0)
1,843✔
786
                        log_debug_errno(errno, "Failed to sync loop block device, ignoring: %m");
×
787

788
                if (!LOOP_DEVICE_IS_FOREIGN(d) && !d->relinquished) {
1,843✔
789
                        /* We are supposed to clear the loopback device. Let's do this synchronously: lock
790
                         * the device, manually remove all partitions and then clear it. This should ensure
791
                         * udev doesn't concurrently access the devices, and we can be reasonably sure that
792
                         * once we are done here the device is cleared and all its partition children
793
                         * removed. Note that we lock our primary device fd here (and not a separate locking
794
                         * fd, as we do during allocation, since we want to keep the lock all the way through
795
                         * the LOOP_CLR_FD, but that call would fail if we had more than one fd open.) */
796

797
                        if (flock(d->fd, LOCK_EX) < 0)
1,699✔
798
                                log_debug_errno(errno, "Failed to lock loop block device, ignoring: %m");
×
799

800
                        r = block_device_remove_all_partitions(d->dev, d->fd);
1,699✔
801
                        if (r < 0)
1,699✔
802
                                log_debug_errno(r, "Failed to remove partitions of loopback block device, ignoring: %m");
×
803

804
                        if (ioctl(d->fd, LOOP_CLR_FD) < 0)
1,699✔
805
                                log_debug_errno(errno, "Failed to clear loop device, ignoring: %m");
×
806
                }
807

808
                safe_close(d->fd);
1,843✔
809
        }
810

811
        /* Now that the block device is released, let's also try to remove it */
812
        if (control >= 0) {
1,843✔
813
                useconds_t delay = 5 * USEC_PER_MSEC;  /* A total delay of 5090 ms between 39 attempts,
814
                                                        * (4*5 + 5*10 + 5*20 + … + 3*640) = 5090. */
815

816
                for (unsigned attempt = 1;; attempt++) {
35✔
817
                        if (ioctl(control, LOOP_CTL_REMOVE, d->nr) >= 0)
1,734✔
818
                                break;
819
                        if (errno != EBUSY || attempt > 38) {
35✔
820
                                log_debug_errno(errno, "Failed to remove device %s: %m", strna(d->node));
×
821
                                break;
822
                        }
823
                        if (attempt % 5 == 0) {
35✔
UNCOV
824
                                log_debug("Device is still busy after %u attempts…", attempt);
×
UNCOV
825
                                delay *= 2;
×
826
                        }
827

828
                        (void) usleep_safe(delay);
35✔
829
                }
830
        }
831

832
        free(d->node);
1,843✔
833
        sd_device_unref(d->dev);
1,843✔
834
        free(d->backing_file);
1,843✔
835
        return mfree(d);
1,843✔
836
}
837

838
DEFINE_TRIVIAL_REF_UNREF_FUNC(LoopDevice, loop_device, loop_device_free);
5,678✔
839

840
void loop_device_relinquish(LoopDevice *d) {
141✔
841
        assert(d);
141✔
842

843
        /* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel
844
         * itself, using the loop device "auto-clear" logic we already turned on when creating the device. */
845

846
        d->relinquished = true;
141✔
847
}
141✔
848

849
void loop_device_unrelinquish(LoopDevice *d) {
20✔
850
        assert(d);
20✔
851
        d->relinquished = false;
20✔
852
}
20✔
853

854
int loop_device_open(
23✔
855
                sd_device *dev,
856
                int open_flags,
857
                int lock_op,
858
                LoopDevice **ret) {
859

860
        _cleanup_close_ int fd = -EBADF, lock_fd = -EBADF;
23✔
861
        _cleanup_free_ char *node = NULL, *backing_file = NULL;
23✔
862
        dev_t devnum, backing_devno = 0;
23✔
863
        struct loop_info64 info;
23✔
864
        ino_t backing_inode = 0;
23✔
865
        uint64_t diskseq = 0;
23✔
866
        LoopDevice *d;
23✔
867
        const char *s;
23✔
868
        int r, nr = -1;
23✔
869

870
        assert(dev);
23✔
871
        assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
23✔
872
        assert(ret);
23✔
873

874
        /* Even if fd is provided through the argument in loop_device_open_from_fd(), we reopen the inode
875
         * here, instead of keeping just a dup() clone of it around, since we want to ensure that the
876
         * O_DIRECT flag of the handle we keep is off, we have our own file index, and have the right
877
         * read/write mode in effect. */
878
        fd = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
23✔
879
        if (fd < 0)
23✔
880
                return fd;
881

882
        if ((lock_op & ~LOCK_NB) != LOCK_UN) {
23✔
883
                lock_fd = open_lock_fd(fd, lock_op);
23✔
884
                if (lock_fd < 0)
23✔
885
                        return lock_fd;
886
        }
887

888
        if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0) {
23✔
889
#if HAVE_VALGRIND_MEMCHECK_H
890
                /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
891
                VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
892
#endif
893
                nr = info.lo_number;
23✔
894

895
                if (sd_device_get_sysattr_value(dev, "loop/backing_file", &s) >= 0) {
23✔
896
                        backing_file = strdup(s);
13✔
897
                        if (!backing_file)
13✔
898
                                return -ENOMEM;
899
                }
900

901
                backing_devno = info.lo_device;
23✔
902
                backing_inode = info.lo_inode;
23✔
903
        }
904

905
        r = fd_get_diskseq(fd, &diskseq);
23✔
906
        if (r < 0 && r != -EOPNOTSUPP)
23✔
907
                return r;
908

909
        uint32_t sector_size;
23✔
910
        r = blockdev_get_sector_size(fd, &sector_size);
23✔
911
        if (r < 0)
23✔
912
                return r;
913

914
        uint64_t device_size;
23✔
915
        r = blockdev_get_device_size(fd, &device_size);
23✔
916
        if (r < 0)
23✔
917
                return r;
918

919
        r = sd_device_get_devnum(dev, &devnum);
23✔
920
        if (r < 0)
23✔
921
                return r;
922

923
        r = sd_device_get_devname(dev, &s);
23✔
924
        if (r < 0)
23✔
925
                return r;
926

927
        node = strdup(s);
23✔
928
        if (!node)
23✔
929
                return -ENOMEM;
930

931
        d = new(LoopDevice, 1);
23✔
932
        if (!d)
23✔
933
                return -ENOMEM;
934

935
        *d = (LoopDevice) {
46✔
936
                .n_ref = 1,
937
                .fd = TAKE_FD(fd),
23✔
938
                .lock_fd = TAKE_FD(lock_fd),
23✔
939
                .nr = nr,
940
                .node = TAKE_PTR(node),
23✔
941
                .dev = sd_device_ref(dev),
23✔
942
                .backing_file = TAKE_PTR(backing_file),
23✔
943
                .backing_inode = backing_inode,
944
                .backing_devno = backing_devno,
945
                .relinquished = true, /* It's not ours, don't try to destroy it when this object is freed */
946
                .devno = devnum,
947
                .diskseq = diskseq,
948
                .sector_size = sector_size,
949
                .device_size = device_size,
950
                .created = false,
951
        };
952

953
        *ret = d;
23✔
954
        return 0;
23✔
955
}
956

957
int loop_device_open_from_fd(
2✔
958
                int fd,
959
                int open_flags,
960
                int lock_op,
961
                LoopDevice **ret) {
962

963
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
2✔
964
        int r;
2✔
965

966
        r = block_device_new_from_fd(ASSERT_FD(fd), 0, &dev);
2✔
967
        if (r < 0)
2✔
968
                return r;
969

970
        return loop_device_open(dev, open_flags, lock_op, ret);
2✔
971
}
972

973
int loop_device_open_from_path(
×
974
                const char *path,
975
                int open_flags,
976
                int lock_op,
977
                LoopDevice **ret) {
978

979
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
×
980
        int r;
×
981

982
        assert(path);
×
983

984
        r = block_device_new_from_path(path, 0, &dev);
×
985
        if (r < 0)
×
986
                return r;
987

988
        return loop_device_open(dev, open_flags, lock_op, ret);
×
989
}
990

991
static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
×
992
        char sysfs[STRLEN("/sys/dev/block/:/partition") + 2*DECIMAL_STR_MAX(dev_t) + 1];
×
993
        _cleanup_free_ char *buffer = NULL;
×
994
        uint64_t current_offset, current_size, partno;
×
995
        _cleanup_close_ int whole_fd = -EBADF;
×
996
        struct stat st;
×
997
        dev_t devno;
×
998
        int r;
×
999

1000
        /* Resizes the partition the loopback device refer to (assuming it refers to one instead of an actual
1001
         * loopback device), and changes the offset, if needed. This is a fancy wrapper around
1002
         * BLKPG_RESIZE_PARTITION. */
1003

1004
        if (fstat(ASSERT_FD(partition_fd), &st) < 0)
×
1005
                return -errno;
×
1006

1007
        assert(S_ISBLK(st.st_mode));
×
1008

1009
        xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/partition", DEVNUM_FORMAT_VAL(st.st_rdev));
×
1010
        r = read_one_line_file(sysfs, &buffer);
×
1011
        if (r == -ENOENT) /* not a partition, cannot resize */
×
1012
                return -ENOTTY;
1013
        if (r < 0)
×
1014
                return r;
1015
        r = safe_atou64(buffer, &partno);
×
1016
        if (r < 0)
×
1017
                return r;
1018

1019
        xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/start", DEVNUM_FORMAT_VAL(st.st_rdev));
×
1020

1021
        buffer = mfree(buffer);
×
1022
        r = read_one_line_file(sysfs, &buffer);
×
1023
        if (r < 0)
×
1024
                return r;
1025
        r = safe_atou64(buffer, &current_offset);
×
1026
        if (r < 0)
×
1027
                return r;
1028
        if (current_offset > UINT64_MAX/512U)
×
1029
                return -EINVAL;
1030
        current_offset *= 512U;
×
1031

1032
        r = blockdev_get_device_size(partition_fd, &current_size);
×
1033
        if (r < 0)
×
1034
                return r;
1035

1036
        if (size == UINT64_MAX && offset == UINT64_MAX)
×
1037
                return 0;
1038
        if (current_size == size && current_offset == offset)
×
1039
                return 0;
1040

1041
        xsprintf(sysfs, "/sys/dev/block/" DEVNUM_FORMAT_STR "/../dev", DEVNUM_FORMAT_VAL(st.st_rdev));
×
1042

1043
        buffer = mfree(buffer);
×
1044
        r = read_one_line_file(sysfs, &buffer);
×
1045
        if (r < 0)
×
1046
                return r;
1047
        r = parse_devnum(buffer, &devno);
×
1048
        if (r < 0)
×
1049
                return r;
1050

1051
        whole_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL);
×
1052
        if (r < 0)
×
1053
                return r;
1054

1055
        return block_device_resize_partition(
×
1056
                        whole_fd,
1057
                        partno,
1058
                        offset == UINT64_MAX ? current_offset : offset,
1059
                        size == UINT64_MAX ? current_size : size);
1060
}
1061

1062
int loop_device_refresh_size(LoopDevice *d, uint64_t offset, uint64_t size) {
6✔
1063
        struct loop_info64 info;
6✔
1064

1065
        assert(d);
6✔
1066
        assert(d->fd >= 0);
6✔
1067

1068
        /* Changes the offset/start of the loop device relative to the beginning of the underlying file or
1069
         * block device. If this loop device actually refers to a partition and not a loopback device, we'll
1070
         * try to adjust the partition offsets instead.
1071
         *
1072
         * If either offset or size is UINT64_MAX we won't change that parameter. */
1073

1074
        if (d->nr < 0) /* not a loopback device */
6✔
1075
                return resize_partition(d->fd, offset, size);
×
1076

1077
        if (ioctl(d->fd, LOOP_GET_STATUS64, &info) < 0)
6✔
1078
                return -errno;
×
1079

1080
#if HAVE_VALGRIND_MEMCHECK_H
1081
        /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
1082
        VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
1083
#endif
1084

1085
        if (size == UINT64_MAX && offset == UINT64_MAX)
6✔
1086
                return 0;
1087
        if (info.lo_sizelimit == size && info.lo_offset == offset)
6✔
1088
                return 0;
1089

1090
        if (size != UINT64_MAX)
6✔
1091
                info.lo_sizelimit = size;
6✔
1092
        if (offset != UINT64_MAX)
6✔
1093
                info.lo_offset = offset;
×
1094

1095
        return RET_NERRNO(ioctl(d->fd, LOOP_SET_STATUS64, &info));
6✔
1096
}
1097

1098
int loop_device_flock(LoopDevice *d, int operation) {
128✔
1099
        assert(IN_SET(operation & ~LOCK_NB, LOCK_UN, LOCK_SH, LOCK_EX));
128✔
1100
        assert(d);
128✔
1101

1102
        /* When unlocking just close the lock fd */
1103
        if ((operation & ~LOCK_NB) == LOCK_UN) {
128✔
1104
                d->lock_fd = safe_close(d->lock_fd);
126✔
1105
                return 0;
126✔
1106
        }
1107

1108
        /* If we had no lock fd so far, create one and lock it right-away */
1109
        if (d->lock_fd < 0) {
2✔
1110
                d->lock_fd = open_lock_fd(ASSERT_FD(d->fd), operation);
1✔
1111
                if (d->lock_fd < 0)
1✔
1112
                        return d->lock_fd;
1113

1114
                return 0;
1✔
1115
        }
1116

1117
        /* Otherwise change the current lock mode on the existing fd */
1118
        return RET_NERRNO(flock(d->lock_fd, operation));
1✔
1119
}
1120

1121
int loop_device_sync(LoopDevice *d) {
56✔
1122
        assert(d);
56✔
1123

1124
        /* We also do this implicitly in loop_device_unref(). Doing this explicitly here has the benefit that
1125
         * we can check the return value though. */
1126

1127
        return RET_NERRNO(fsync(ASSERT_FD(d->fd)));
56✔
1128
}
1129

1130
int loop_device_set_autoclear(LoopDevice *d, bool autoclear) {
7✔
1131
        struct loop_info64 info;
7✔
1132

1133
        assert(d);
7✔
1134

1135
        if (ioctl(ASSERT_FD(d->fd), LOOP_GET_STATUS64, &info) < 0)
7✔
1136
                return -errno;
×
1137

1138
        if (autoclear == FLAGS_SET(info.lo_flags, LO_FLAGS_AUTOCLEAR))
7✔
1139
                return 0;
1140

1141
        SET_FLAG(info.lo_flags, LO_FLAGS_AUTOCLEAR, autoclear);
7✔
1142

1143
        if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0)
7✔
1144
                return -errno;
×
1145

1146
        return 1;
1147
}
1148

1149
int loop_device_set_filename(LoopDevice *d, const char *name) {
3✔
1150
        struct loop_info64 info;
3✔
1151

1152
        assert(d);
3✔
1153

1154
        /* Sets the .lo_file_name of the loopback device. This is supposed to contain the path to the file
1155
         * backing the block device, but is actually just a free-form string you can pass to the kernel. Most
1156
         * tools that actually care for the backing file path use the sysfs attribute file loop/backing_file
1157
         * which is a kernel generated string, subject to file system namespaces and such.
1158
         *
1159
         * .lo_file_name is useful since userspace can select it freely when creating a loopback block
1160
         * device, and we can use it for /dev/disk/by-loop-ref/ symlinks, and similar, so that apps can
1161
         * recognize their own loopback files. */
1162

1163
        if (name && strlen(name) >= sizeof(info.lo_file_name))
3✔
1164
                return -ENOBUFS;
3✔
1165

1166
        if (ioctl(ASSERT_FD(d->fd), LOOP_GET_STATUS64, &info) < 0)
3✔
1167
                return -errno;
×
1168

1169
        if (strneq((char*) info.lo_file_name, strempty(name), sizeof(info.lo_file_name)))
3✔
1170
                return 0;
1171

1172
        if (name) {
3✔
1173
                strncpy((char*) info.lo_file_name, name, sizeof(info.lo_file_name)-1);
3✔
1174
                info.lo_file_name[sizeof(info.lo_file_name)-1] = 0;
3✔
1175
        } else
1176
                memzero(info.lo_file_name, sizeof(info.lo_file_name));
×
1177

1178
        if (ioctl(d->fd, LOOP_SET_STATUS64, &info) < 0)
3✔
1179
                return -errno;
×
1180

1181
        return 1;
1182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc