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

systemd / systemd / 25834419893

13 May 2026 10:51PM UTC coverage: 72.584% (+0.07%) from 72.513%
25834419893

push

github

bluca
nsresourced: re-link GID delegation file after atomic UID file write

userns_registry_remove() restores a sub-delegated UID range by writing
the previous owner's data to u<UID>.delegate with WRITE_STRING_FILE_ATOMIC.
Atomic writes go via a temp file and rename, which replaces the directory
entry with a fresh inode and severs the hardlink to g<GID>.delegate. The
stale GID side then keeps pointing at the prior inode with outdated owner
and ancestor data, so subsequent lookups via GID return wrong results.

Re-create the hardlink after the atomic write so the two views stay in
sync, matching what userns_registry_store() already does after writing
a new delegation.

5 of 8 new or added lines in 1 file covered. (62.5%)

4652 existing lines in 93 files now uncovered.

328958 of 453210 relevant lines covered (72.58%)

1305869.89 hits per line

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

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

3
#include <linux/blkpg.h>
4
#include <linux/fs.h>
5
#include <sys/file.h>
6
#include <sys/ioctl.h>
7
#include <sys/stat.h>
8
#include <unistd.h>
9

10
#include "sd-device.h"
11

12
#include "alloc-util.h"
13
#include "blockdev-util.h"
14
#include "btrfs-util.h"
15
#include "device-private.h"
16
#include "device-util.h"
17
#include "devnum-util.h"
18
#include "dirent-util.h"
19
#include "errno-util.h"
20
#include "fd-util.h"
21
#include "fileio.h"
22
#include "fs-util.h"
23
#include "parse-util.h"
24
#include "path-util.h"
25
#include "string-util.h"
26

27
static int fd_get_devnum(int fd, BlockDeviceLookupFlags flags, dev_t *ret) {
4,129✔
28
        struct stat st;
4,129✔
29
        dev_t devnum;
4,129✔
30
        int r;
4,129✔
31

32
        assert(fd >= 0);
4,129✔
33
        assert(ret);
4,129✔
34

35
        if (fstat(fd, &st) < 0)
4,129✔
36
                return -errno;
×
37

38
        if (S_ISBLK(st.st_mode))
4,129✔
39
                devnum = st.st_rdev;
3,885✔
40
        else if (!FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_BACKING))
244✔
41
                return -ENOTBLK;
42
        else if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))
35✔
43
                return -ENOTBLK;
44
        else if (major(st.st_dev) != 0)
35✔
45
                devnum = st.st_dev;
35✔
46
        else {
47
                /* If major(st.st_dev) is zero, this might mean we are backed by btrfs, which needs special
48
                 * handing, to get the backing device node. */
49

50
                r = btrfs_get_block_device_fd(fd, &devnum);
×
51
                if (r == -ENOTTY) /* not btrfs */
×
52
                        return -ENOTBLK;
53
                if (r < 0)
×
54
                        return r;
55
        }
56

57
        *ret = devnum;
3,920✔
58
        return 0;
3,920✔
59
}
60

61
int block_device_is_whole_disk(sd_device *dev) {
126,405✔
62
        int r;
126,405✔
63

64
        assert(dev);
126,405✔
65

66
        r = device_in_subsystem(dev, "block");
126,405✔
67
        if (r < 0)
126,405✔
68
                return r;
126,405✔
69
        if (r == 0)
126,405✔
70
                return -ENOTBLK;
71

72
        return device_is_devtype(dev, "disk");
74,525✔
73
}
74

75
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
89,136✔
76
        int r;
89,136✔
77

78
        assert(dev);
89,136✔
79
        assert(ret);
89,136✔
80

81
        /* Do not unref returned sd_device object. */
82

83
        r = block_device_is_whole_disk(dev);
89,136✔
84
        if (r < 0)
89,136✔
85
                return r;
86
        if (r == 0) {
37,256✔
87
                r = sd_device_get_parent(dev, &dev);
23,566✔
88
                if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */
23,566✔
89
                        return -ENODEV;
90
                if (r < 0)
23,472✔
91
                        return r;
92

93
                r = block_device_is_whole_disk(dev);
23,472✔
94
                if (r < 0)
23,472✔
95
                        return r;
96
                if (r == 0)
23,472✔
97
                        return -ENXIO;
98
        }
99

100
        *ret = dev;
37,162✔
101
        return 0;
37,162✔
102
}
103

104
static int block_device_get_originating_one(sd_device *dev, sd_device **ret) {
3,592✔
105
        _cleanup_(sd_device_unrefp) sd_device *first_found = NULL;
3,592✔
106
        const char *suffix;
3,592✔
107
        dev_t devnum = 0;  /* avoid false maybe-uninitialized warning */
3,592✔
108

109
        /* For the specified block device tries to chase it through the layers, in case LUKS-style DM
110
         * stacking is used, trying to find the next underlying layer. */
111

112
        assert(dev);
3,592✔
113
        assert(ret);
3,592✔
114

115
        FOREACH_DEVICE_CHILD_WITH_SUFFIX(dev, child, suffix) {
7,198✔
116
                sd_device *child_whole_disk;
3,606✔
117
                dev_t n;
3,606✔
118

119
                if (!path_startswith(suffix, "slaves"))
3,606✔
120
                        continue;
3,606✔
121

122
                if (block_device_get_whole_disk(child, &child_whole_disk) < 0)
×
123
                        continue;
×
124

125
                if (sd_device_get_devnum(child_whole_disk, &n) < 0)
×
126
                        continue;
×
127

128
                if (!first_found) {
×
129
                        first_found = sd_device_ref(child);
×
130
                        devnum = n;
×
131
                        continue;
×
132
                }
133

134
                /* We found a device backed by multiple other devices. We don't really support automatic
135
                 * discovery on such setups, with the exception of dm-verity partitions. In this case there
136
                 * are two backing devices: the data partition and the hash partition. We are fine with such
137
                 * setups, however, only if both partitions are on the same physical device. Hence, let's
138
                 * verify this by iterating over every node in the 'slaves/' directory and comparing them with
139
                 * the first that gets returned by readdir(), to ensure they all point to the same device. */
140
                if (n != devnum)
×
141
                        return -ENOTUNIQ;
×
142
        }
143

144
        if (!first_found)
3,592✔
145
                return -ENOENT;
146

147
        *ret = TAKE_PTR(first_found);
×
148
        return 0;
×
149
}
150

151
int block_device_get_originating(sd_device *dev, sd_device **ret, bool recursive) {
3,592✔
152
        _cleanup_(sd_device_unrefp) sd_device *current = NULL;
3,592✔
153
        int r;
3,592✔
154

155
        assert(dev);
3,592✔
156
        assert(ret);
3,592✔
157

158
        if (!recursive)
3,592✔
159
                return block_device_get_originating_one(dev, ret);
10✔
160

161
        current = sd_device_ref(dev);
3,582✔
162

163
        for (;;) {
3,582✔
164
                sd_device *origin;
3,582✔
165

166
                r = block_device_get_originating_one(current, &origin);
3,582✔
167
                if (r == -ENOENT)
3,582✔
168
                        break;
169
                if (r < 0)
×
170
                        return r;
×
171

172
                sd_device_unref(current);
×
173
                current = origin;
×
174
        }
175

176
        if (current == dev)
3,582✔
177
                return -ENOENT;
178

179
        *ret = TAKE_PTR(current);
×
180

181
        return 0;
×
182
}
183

184
int block_device_new_from_fd(int fd, BlockDeviceLookupFlags flags, sd_device **ret) {
3,008✔
185
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
3,008✔
186
        dev_t devnum;
3,008✔
187
        int r;
3,008✔
188

189
        assert(fd >= 0);
3,008✔
190
        assert(ret);
3,008✔
191

192
        r = fd_get_devnum(fd, flags, &devnum);
3,008✔
193
        if (r < 0)
3,008✔
194
                return r;
195

196
        r = sd_device_new_from_devnum(&dev, 'b', devnum);
2,799✔
197
        if (r < 0)
2,799✔
198
                return r;
199

200
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_ORIGINATING)) {
2,799✔
201
                _cleanup_(sd_device_unrefp) sd_device *dev_origin = NULL;
1✔
202
                sd_device *dev_whole_disk;
1✔
203

204
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
1✔
205
                if (r < 0)
1✔
206
                        return r;
207

208
                r = block_device_get_originating(dev_whole_disk, &dev_origin, /* recursive= */ false);
1✔
209
                if (r >= 0)
1✔
210
                        device_unref_and_replace_new_ref(dev, dev_origin);
×
211
                else if (r != -ENOENT)
1✔
212
                        return r;
213
        }
214

215
        if (FLAGS_SET(flags, BLOCK_DEVICE_LOOKUP_WHOLE_DISK)) {
2,799✔
216
                sd_device *dev_whole_disk;
355✔
217

218
                r = block_device_get_whole_disk(dev, &dev_whole_disk);
355✔
219
                if (r < 0)
355✔
220
                        return r;
355✔
221

222
                *ret = sd_device_ref(dev_whole_disk);
355✔
223
                return 0;
355✔
224
        }
225

226
        *ret = sd_device_ref(dev);
2,444✔
227
        return 0;
2,444✔
228
}
229

230
int block_device_new_from_path(const char *path, BlockDeviceLookupFlags flags, sd_device **ret) {
3✔
231
        _cleanup_close_ int fd = -EBADF;
3✔
232

233
        assert(path);
3✔
234
        assert(ret);
3✔
235

236
        fd = open(path, O_CLOEXEC|O_PATH);
3✔
237
        if (fd < 0)
3✔
238
                return -errno;
2✔
239

240
        return block_device_new_from_fd(fd, flags, ret);
1✔
241
}
242

243
int block_get_whole_disk(dev_t d, dev_t *ret) {
4,662✔
244
        char p[SYS_BLOCK_PATH_MAX("/partition")];
4,662✔
245
        _cleanup_free_ char *s = NULL;
4,662✔
246
        dev_t devt;
4,662✔
247
        int r;
4,662✔
248

249
        assert(ret);
4,662✔
250

251
        if (major(d) == 0)
4,662✔
252
                return -ENODEV;
253

254
        /* If it has a queue this is good enough for us */
255
        xsprintf_sys_block_path(p, "/queue", d);
4,662✔
256
        if (access(p, F_OK) >= 0) {
4,662✔
257
                *ret = d;
1,104✔
258
                return 0;
1,104✔
259
        }
260
        if (errno != ENOENT)
3,558✔
261
                return -errno;
×
262

263
        /* If it is a partition find the originating device */
264
        xsprintf_sys_block_path(p, "/partition", d);
3,558✔
265
        if (access(p, F_OK) < 0)
3,558✔
266
                return -errno;
×
267

268
        /* Get parent dev_t */
269
        xsprintf_sys_block_path(p, "/../dev", d);
3,558✔
270
        r = read_one_line_file(p, &s);
3,558✔
271
        if (r < 0)
3,558✔
272
                return r;
273

274
        r = parse_devnum(s, &devt);
3,558✔
275
        if (r < 0)
3,558✔
276
                return r;
277

278
        /* Only return this if it is really good enough for us. */
279
        xsprintf_sys_block_path(p, "/queue", devt);
3,558✔
280
        if (access(p, F_OK) < 0)
3,558✔
281
                return -errno;
×
282

283
        *ret = devt;
3,558✔
284
        return 1;
3,558✔
285
}
286

287
int get_block_device_fd(int fd, dev_t *ret) {
3,888✔
288
        struct stat st;
3,888✔
289
        int r;
3,888✔
290

291
        assert(fd >= 0);
3,888✔
292
        assert(ret);
3,888✔
293

294
        /* Gets the block device directly backing a file system. If the block device is encrypted, returns
295
         * the device mapper block device. */
296

297
        if (fstat(fd, &st))
3,888✔
298
                return -errno;
×
299

300
        if (major(st.st_dev) != 0) {
3,888✔
301
                *ret = st.st_dev;
3,776✔
302
                return 1;
3,776✔
303
        }
304

305
        r = btrfs_get_block_device_fd(fd, ret);
112✔
306
        if (r != -ENOTTY) /* ENOTTY: not btrfs */
112✔
307
                return r;
308

309
        *ret = 0;
112✔
310
        return 0;
112✔
311
}
312

313
int get_block_device(const char *path, dev_t *ret) {
234✔
314
        _cleanup_close_ int fd = -EBADF;
234✔
315

316
        assert(path);
234✔
317
        assert(ret);
234✔
318

319
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
234✔
320
        if (fd < 0)
234✔
321
                return -errno;
×
322

323
        return get_block_device_fd(fd, ret);
234✔
324
}
325

326
int block_get_originating(dev_t dt, dev_t *ret, bool recursive) {
3,591✔
327
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL, *origin = NULL;
7,182✔
328
        int r;
3,591✔
329

330
        assert(ret);
3,591✔
331

332
        r = sd_device_new_from_devnum(&dev, 'b', dt);
3,591✔
333
        if (r < 0)
3,591✔
334
                return r;
335

336
        r = block_device_get_originating(dev, &origin, recursive);
3,591✔
337
        if (r < 0)
3,591✔
338
                return r;
339

340
        return sd_device_get_devnum(origin, ret);
×
341
}
342

343
int get_block_device_harder_fd(int fd, dev_t *ret) {
3,582✔
344
        int r;
3,582✔
345

346
        assert(fd >= 0);
3,582✔
347
        assert(ret);
3,582✔
348

349
        /* Gets the backing block device for a file system, and handles LUKS encrypted file systems, looking
350
         * for its underlying physical device, if there is one. */
351

352
        r = get_block_device_fd(fd, ret);
3,582✔
353
        if (r <= 0)
3,582✔
354
                return r;
355

356
        r = block_get_originating(*ret, ret, /* recursive= */ true);
3,582✔
357
        if (r < 0)
3,582✔
358
                log_debug_errno(r, "Failed to chase block device, ignoring: %m");
3,582✔
359

360
        return 1;
361
}
362

363
int get_block_device_harder(const char *path, dev_t *ret) {
3,529✔
364
        _cleanup_close_ int fd = -EBADF;
3,529✔
365

366
        assert(path);
3,529✔
367
        assert(ret);
3,529✔
368

369
        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
3,529✔
370
        if (fd < 0)
3,529✔
371
                return -errno;
×
372

373
        return get_block_device_harder_fd(fd, ret);
3,529✔
374
}
375

376
int lock_whole_block_device(dev_t devt, int open_flags, int operation) {
×
377
        _cleanup_close_ int lock_fd = -EBADF;
×
378
        dev_t whole_devt;
×
379
        int r;
×
380

381
        /* Let's get a BSD file lock on the whole block device, as per: https://systemd.io/BLOCK_DEVICE_LOCKING
382
         *
383
         * NB: it matters whether open_flags indicates open for write: only then will the eventual closing of
384
         * the fd trigger udev's partitioning rescanning of the device (as it watches for IN_CLOSE_WRITE),
385
         * hence make sure to pass the right value there. */
386

387
        r = block_get_whole_disk(devt, &whole_devt);
×
388
        if (r < 0)
×
389
                return r;
390

391
        lock_fd = device_open_from_devnum(S_IFBLK, whole_devt, open_flags|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL);
×
392
        if (lock_fd < 0)
×
393
                return lock_fd;
394

395
        if (flock(lock_fd, operation) < 0)
×
396
                return -errno;
×
397

398
        return TAKE_FD(lock_fd);
399
}
400

401
int blockdev_partscan_enabled(sd_device *dev) {
11,942✔
402
        unsigned capability;
11,942✔
403
        int r, ext_range;
11,942✔
404

405
        /* Checks if partition scanning is correctly enabled on the block device.
406
         *
407
         * The 'GENHD_FL_NO_PART_SCAN' flag was introduced by
408
         * https://github.com/torvalds/linux/commit/d27769ec3df1a8de9ca450d2dcd72d1ab259ba32 (v3.2).
409
         * But at that time, the flag is also effectively implied when 'minors' element of 'struct gendisk'
410
         * is 1, which can be check with 'ext_range' sysfs attribute. Explicit flag ('GENHD_FL_NO_PART_SCAN')
411
         * can be obtained from 'capability' sysattr.
412
         *
413
         * With https://github.com/torvalds/linux/commit/46e7eac647b34ed4106a8262f8bedbb90801fadd (v5.17),
414
         * the flag is renamed to GENHD_FL_NO_PART.
415
         *
416
         * With https://github.com/torvalds/linux/commit/1ebe2e5f9d68e94c524aba876f27b945669a7879 (v5.17),
417
         * we can check the flag from 'ext_range' sysfs attribute directly.
418
         *
419
         * With https://github.com/torvalds/linux/commit/430cc5d3ab4d0ba0bd011cfbb0035e46ba92920c (v5.17),
420
         * the value of GENHD_FL_NO_PART is changed from 0x0200 to 0x0004. 💣💣💣
421
         * Note, the new value was used by the GENHD_FL_MEDIA_CHANGE_NOTIFY flag, which was introduced by
422
         * 86ce18d7b7925bfd6b64c061828ca2a857ee83b8 (v2.6.22), and removed by
423
         * 9243c6f3e012a92dd900d97ef45efaf8a8edc448 (v5.7). If we believe the commit message of
424
         * e81cd5a983bb35dabd38ee472cf3fea1c63e0f23, the flag was never used. So, fortunately, we can use
425
         * both the new and old values safely.
426
         *
427
         * With https://github.com/torvalds/linux/commit/b9684a71fca793213378dd410cd11675d973eaa1 (v5.19),
428
         * another flag GD_SUPPRESS_PART_SCAN is introduced for loopback block device, and partition scanning
429
         * is done only when both GENHD_FL_NO_PART and GD_SUPPRESS_PART_SCAN are not set. Before the commit,
430
         * LO_FLAGS_PARTSCAN flag was directly tied with GENHD_FL_NO_PART. But with this change now it is
431
         * tied with GD_SUPPRESS_PART_SCAN. So, LO_FLAGS_PARTSCAN cannot be obtained from 'ext_range'
432
         * sysattr, which corresponds to GENHD_FL_NO_PART, and we need to read 'loop/partscan'. 💣💣💣
433
         *
434
         * With https://github.com/torvalds/linux/commit/73a166d9749230d598320fdae3b687cdc0e2e205 (v6.3),
435
         * the GD_SUPPRESS_PART_SCAN flag is also introduced for userspace block device (ublk). Though, not
436
         * sure if we should support the device...
437
         *
438
         * With https://github.com/torvalds/linux/commit/e81cd5a983bb35dabd38ee472cf3fea1c63e0f23 (v6.3),
439
         * the 'capability' sysfs attribute is deprecated, hence we cannot check flags from it. 💣💣💣
440
         *
441
         * With https://github.com/torvalds/linux/commit/a4217c6740dc64a3eb6815868a9260825e8c68c6 (v6.10,
442
         * backported to v6.6+), the partscan status is directly exposed as 'partscan' sysattr.
443
         *
444
         * To support both old and new kernels, we need to do the following:
445
         * 1) check 'partscan' sysfs attribute where the information is made directly available,
446
         * 2) check if the blockdev refers to a partition, where partscan is not supported,
447
         * 3) check 'loop/partscan' sysfs attribute for loopback block devices, and if '0' we can conclude
448
         *    partition scanning is disabled,
449
         * 4) check 'ext_range' sysfs attribute, and if '1' we can conclude partition scanning is disabled,
450
         * 5) otherwise check 'capability' sysfs attribute for ancient version. */
451

452
        assert(dev);
11,942✔
453

454
        r = device_in_subsystem(dev, "block");
11,942✔
455
        if (r < 0)
11,942✔
456
                return r;
11,942✔
457
        if (r == 0)
11,942✔
458
                return -ENOTBLK;
459

460
        /* For v6.10 or newer. */
461
        r = device_get_sysattr_bool(dev, "partscan");
11,942✔
462
        if (r != -ENOENT)
11,942✔
463
                return r;
464

465
        /* Partition block devices never have partition scanning on, there's no concept of sub-partitions for
466
         * partitions. */
467
        r = device_is_devtype(dev, "partition");
10✔
468
        if (r < 0)
10✔
469
                return r;
470
        if (r > 0)
10✔
471
                return false;
472

473
        /* For loopback block device, especially for v5.19 or newer. Even if this is enabled, we also need to
474
         * check GENHD_FL_NO_PART flag through 'ext_range' and 'capability' sysfs attributes below. */
475
        if (device_get_sysattr_bool(dev, "loop/partscan") == 0)
×
476
                return false;
477

478
        r = device_get_sysattr_int(dev, "ext_range", &ext_range);
×
479
        if (r == -ENOENT) /* If the ext_range file doesn't exist then we are most likely looking at a
×
480
                           * partition block device, not the whole block device. And that means we have no
481
                           * partition scanning on for it (we do for its parent, but not for the partition
482
                           * itself). */
483
                return false;
484
        if (r < 0)
×
485
                return r;
486

487
        if (ext_range <= 1) /* The value should be always positive, but the kernel uses '%d' for the
×
488
                             * attribute. Let's gracefully handle zero or negative. */
489
                return false;
490

491
        r = device_get_sysattr_unsigned_full(dev, "capability", 16, &capability);
×
492
        if (r == -ENOENT)
×
493
                return false;
494
        if (r < 0)
×
495
                return r;
496

497
#define GENHD_FL_NO_PART_OLD 0x0200
498
#define GENHD_FL_NO_PART_NEW 0x0004
499
        /* If one of the NO_PART flags is set, part scanning is definitely off. */
500
        if ((capability & (GENHD_FL_NO_PART_OLD | GENHD_FL_NO_PART_NEW)) != 0)
×
501
                return false;
×
502

503
        /* Otherwise, assume part scanning is on, we have no further checks available. Assume the best. */
504
        return true;
505
}
506

507
int blockdev_partscan_enabled_fd(int fd) {
337✔
508
        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
337✔
509
        int r;
337✔
510

511
        assert(fd >= 0);
337✔
512

513
        r = block_device_new_from_fd(fd, 0, &dev);
337✔
514
        if (r < 0)
337✔
515
                return r;
516

517
        return blockdev_partscan_enabled(dev);
128✔
518
}
519

520
static int blockdev_is_encrypted(const char *sysfs_path, unsigned depth_left) {
20✔
521
        _cleanup_free_ char *p = NULL, *uuids = NULL;
20✔
522
        _cleanup_closedir_ DIR *d = NULL;
20✔
523
        int r, found_encrypted = false;
20✔
524

525
        assert(sysfs_path);
20✔
526

527
        if (depth_left == 0)
20✔
528
                return -EINVAL;
529

530
        p = path_join(sysfs_path, "dm/uuid");
20✔
531
        if (!p)
20✔
532
                return -ENOMEM;
533

534
        r = read_one_line_file(p, &uuids);
20✔
535
        if (r != -ENOENT) {
20✔
536
                if (r < 0)
×
537
                        return r;
538

539
                /* The DM device's uuid attribute is prefixed with "CRYPT-" if this is a dm-crypt device. */
540
                if (startswith(uuids, "CRYPT-"))
×
541
                        return true;
542
        }
543

544
        /* Not a dm-crypt device itself. But maybe it is on top of one? Follow the links in the "slaves/"
545
         * subdir. */
546

547
        p = mfree(p);
20✔
548
        p = path_join(sysfs_path, "slaves");
20✔
549
        if (!p)
20✔
550
                return -ENOMEM;
551

552
        d = opendir(p);
20✔
553
        if (!d) {
20✔
554
                if (errno == ENOENT) /* Doesn't have underlying devices */
20✔
555
                        return false;
556

557
                return -errno;
×
558
        }
559

560
        for (;;) {
×
561
                _cleanup_free_ char *q = NULL;
×
562
                struct dirent *de;
×
563

564
                errno = 0;
×
565
                de = readdir_no_dot(d);
×
566
                if (!de) {
×
567
                        if (errno != 0)
×
568
                                return -errno;
×
569

570
                        break; /* No more underlying devices */
571
                }
572

573
                q = path_join(p, de->d_name);
×
574
                if (!q)
×
575
                        return -ENOMEM;
576

577
                r = blockdev_is_encrypted(q, depth_left - 1);
×
578
                if (r < 0)
×
579
                        return r;
580
                if (r == 0) /* we found one that is not encrypted? then propagate that immediately */
×
581
                        return false;
582

583
                found_encrypted = true;
×
584
        }
585

586
        return found_encrypted;
587
}
588

589
int fd_is_encrypted(int fd) {
19✔
590
        char p[SYS_BLOCK_PATH_MAX("")];
19✔
591
        dev_t devt;
19✔
592
        int r;
19✔
593

594
        r = get_block_device_fd(fd, &devt);
19✔
595
        if (r < 0)
19✔
596
                return r;
19✔
597
        if (r == 0) /* doesn't have a block device */
19✔
598
                return false;
599

600
        xsprintf_sys_block_path(p, NULL, devt);
17✔
601

602
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
17✔
603
}
604

605
int path_is_encrypted(const char *path) {
7✔
606
        char p[SYS_BLOCK_PATH_MAX("")];
7✔
607
        dev_t devt;
7✔
608
        int r;
7✔
609

610
        r = get_block_device(path, &devt);
7✔
611
        if (r < 0)
7✔
612
                return r;
7✔
613
        if (r == 0) /* doesn't have a block device */
7✔
614
                return false;
615

616
        xsprintf_sys_block_path(p, NULL, devt);
3✔
617

618
        return blockdev_is_encrypted(p, 10 /* safety net: maximum recursion depth */);
3✔
619
}
620

621
int fd_get_whole_disk(int fd, bool backing, dev_t *ret) {
1,121✔
622
        dev_t devt;
1,121✔
623
        int r;
1,121✔
624

625
        assert(fd >= 0);
1,121✔
626
        assert(ret);
1,121✔
627

628
        r = fd_get_devnum(fd, backing ? BLOCK_DEVICE_LOOKUP_BACKING : 0, &devt);
2,236✔
629
        if (r < 0)
1,121✔
630
                return r;
1,121✔
631

632
        return block_get_whole_disk(devt, ret);
1,121✔
633
}
634

635
int path_get_whole_disk(const char *path, bool backing, dev_t *ret) {
1,121✔
636
        _cleanup_close_ int fd = -EBADF;
1,121✔
637

638
        fd = open(path, O_CLOEXEC|O_PATH);
1,121✔
639
        if (fd < 0)
1,121✔
640
                return -errno;
×
641

642
        return fd_get_whole_disk(fd, backing, ret);
1,121✔
643
}
644

645
int block_device_add_partition(int fd, int nr, uint64_t start, uint64_t size) {
382✔
646

647
        assert(fd >= 0);
382✔
648
        assert(nr > 0);
382✔
649

650
        struct blkpg_partition bp = {
382✔
651
                .pno = nr,
652
                .start = start,
653
                .length = size,
654
        };
655

656
        struct blkpg_ioctl_arg ba = {
382✔
657
                .op = BLKPG_ADD_PARTITION,
658
                .data = &bp,
659
                .datalen = sizeof(bp),
660
        };
661

662
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
382✔
663
}
664

665
int block_device_remove_partition(int fd, int nr) {
182✔
666

667
        assert(fd >= 0);
182✔
668
        assert(nr > 0);
182✔
669

670
        struct blkpg_partition bp = {
182✔
671
                .pno = nr,
672
        };
673

674
        struct blkpg_ioctl_arg ba = {
182✔
675
                .op = BLKPG_DEL_PARTITION,
676
                .data = &bp,
677
                .datalen = sizeof(bp),
678
        };
679

680
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
182✔
681
}
682

UNCOV
683
int block_device_resize_partition(
×
684
                int fd,
685
                int nr,
686
                uint64_t start,
687
                uint64_t size) {
688

UNCOV
689
        assert(fd >= 0);
×
UNCOV
690
        assert(nr > 0);
×
691

UNCOV
692
        struct blkpg_partition bp = {
×
693
                .pno = nr,
694
                .start = start,
695
                .length = size,
696
        };
697

UNCOV
698
        struct blkpg_ioctl_arg ba = {
×
699
                .op = BLKPG_RESIZE_PARTITION,
700
                .data = &bp,
701
                .datalen = sizeof(bp),
702
        };
703

UNCOV
704
        return RET_NERRNO(ioctl(fd, BLKPG, &ba));
×
705
}
706

707
int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
9,107✔
708
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
9,107✔
709
        const char *s;
9,107✔
710
        int r;
9,107✔
711

712
        assert(dev);
9,107✔
713
        assert(ret);
9,107✔
714

715
        /* Refuse invocation on partition block device, insist on "whole" device */
716
        r = block_device_is_whole_disk(dev);
9,107✔
717
        if (r < 0)
9,107✔
718
                return r;
719
        if (r == 0)
9,107✔
720
                return -ENXIO; /* return a recognizable error */
721

722
        r = sd_device_enumerator_new(&e);
9,107✔
723
        if (r < 0)
9,107✔
724
                return r;
725

726
        r = sd_device_enumerator_allow_uninitialized(e);
9,107✔
727
        if (r < 0)
9,107✔
728
                return r;
729

730
        r = sd_device_enumerator_add_match_parent(e, dev);
9,107✔
731
        if (r < 0)
9,107✔
732
                return r;
733

734
        r = sd_device_get_sysname(dev, &s);
9,107✔
735
        if (r < 0)
9,107✔
736
                return r;
737

738
        /* Also add sysname check for safety. Hopefully, this also improves performance. */
739
        s = strjoina(s, "*");
45,535✔
740
        r = sd_device_enumerator_add_match_sysname(e, s);
9,107✔
741
        if (r < 0)
9,107✔
742
                return r;
743

744
        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true);
9,107✔
745
        if (r < 0)
9,107✔
746
                return r;
747

748
        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
9,107✔
749
        if (r < 0)
9,107✔
750
                return r;
751

752
        *ret = TAKE_PTR(e);
9,107✔
753
        return 0;
9,107✔
754
}
755

756
int block_device_remove_all_partitions(sd_device *dev, int fd) {
4,811✔
757
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
4,811✔
758
        _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
4,811✔
759
        _cleanup_close_ int fd_close = -EBADF;
4,811✔
760
        bool has_partitions = false;
4,811✔
761
        int r, k = 0;
4,811✔
762

763
        assert(dev || fd >= 0);
4,811✔
764

765
        if (!dev) {
4,811✔
UNCOV
766
                r = block_device_new_from_fd(fd, 0, &dev_unref);
×
UNCOV
767
                if (r < 0)
×
768
                        return r;
769

UNCOV
770
                dev = dev_unref;
×
771
        }
772

773
        r = partition_enumerator_new(dev, &e);
4,811✔
774
        if (r < 0)
4,811✔
775
                return r;
776

777
        if (fd < 0) {
4,811✔
UNCOV
778
                fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
×
UNCOV
779
                if (fd_close < 0)
×
780
                        return fd_close;
781

782
                fd = fd_close;
783
        }
784

785
        FOREACH_DEVICE(e, part) {
4,993✔
786
                const char *v, *devname;
182✔
787
                int nr;
182✔
788

789
                has_partitions = true;
182✔
790

791
                r = sd_device_get_devname(part, &devname);
182✔
792
                if (r < 0)
182✔
UNCOV
793
                        return r;
×
794

795
                r = sd_device_get_property_value(part, "PARTN", &v);
182✔
796
                if (r < 0)
182✔
797
                        return r;
798

799
                r = safe_atoi(v, &nr);
182✔
800
                if (r < 0)
182✔
801
                        return r;
802

803
                r = btrfs_forget_device(devname);
182✔
804
                if (r < 0 && r != -ENOENT)
182✔
UNCOV
805
                        log_debug_errno(r, "Failed to forget btrfs device %s, ignoring: %m", devname);
×
806

807
                r = block_device_remove_partition(fd, nr);
182✔
808
                if (r == -ENODEV) {
182✔
UNCOV
809
                        log_debug("Kernel removed partition %s before us, ignoring", devname);
×
UNCOV
810
                        continue;
×
811
                }
812
                if (r < 0) {
182✔
813
                        log_debug_errno(r, "Failed to remove partition %s: %m", devname);
×
UNCOV
814
                        k = k < 0 ? k : r;
×
UNCOV
815
                        continue;
×
816
                }
817

818
                log_debug("Removed partition %s", devname);
182✔
819
        }
820

821
        return k < 0 ? k : has_partitions;
4,811✔
822
}
823

824
int blockdev_get_sector_size(int fd, uint32_t *ret) {
9,020✔
825
        int ssz = 0;
9,020✔
826

827
        assert(fd >= 0);
9,020✔
828
        assert(ret);
9,020✔
829

830
        if (ioctl(fd, BLKSSZGET, &ssz) < 0)
9,020✔
UNCOV
831
                return -errno;
×
832
        if (ssz <= 0) /* make sure the field is initialized */
9,020✔
833
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Block device reported invalid sector size %i.", ssz);
×
834

835
        *ret = ssz;
9,020✔
836
        return 0;
9,020✔
837
}
838

839
int blockdev_get_device_size(int fd, uint64_t *ret) {
2,849✔
840
        uint64_t sz = 0;
2,849✔
841

842
        assert(fd >= 0);
2,849✔
843
        assert(ret);
2,849✔
844

845
        /* This is just a type-safe wrapper around BLKGETSIZE64 that gets us around having to include messy linux/fs.h in various clients */
846

847
        if (ioctl(fd, BLKGETSIZE64, &sz) < 0)
2,849✔
UNCOV
848
                return -errno;
×
849

850
        *ret = sz;
2,849✔
851
        return 0;
2,849✔
852
}
853

854
int blockdev_get_root(int level, dev_t *ret) {
3,529✔
855
        _cleanup_free_ char *p = NULL;
3,529✔
856
        dev_t devno;
3,529✔
857
        int r;
3,529✔
858

859
        /* Returns the device node backing the root file system. Traces through
860
         * dm-crypt/dm-verity/... Returns > 0 and the devno of the device on success. If there's no block
861
         * device (or multiple) returns 0 and a devno of 0. Failure otherwise.
862
         *
863
         * If the root mount has been replaced by some form of volatile file system (overlayfs), the original
864
         * root block device node is symlinked in /run/systemd/volatile-root. Let's read that here. */
865
        r = readlink_malloc("/run/systemd/volatile-root", &p);
3,529✔
866
        if (r == -ENOENT) { /* volatile-root not found */
3,529✔
867
                r = get_block_device_harder("/", &devno);
3,529✔
868
                if (r == -EUCLEAN)
3,529✔
UNCOV
869
                        return btrfs_log_dev_root(level, r, "root file system");
×
870
                if (r < 0)
3,529✔
UNCOV
871
                        return log_full_errno(level, r, "Failed to determine block device of root file system: %m");
×
872
                if (r == 0) { /* Not backed by a single block device. (Could be NFS or so, or could be multi-device RAID or so) */
3,529✔
UNCOV
873
                        r = get_block_device_harder("/usr", &devno);
×
UNCOV
874
                        if (r == -EUCLEAN)
×
UNCOV
875
                                return btrfs_log_dev_root(level, r, "/usr");
×
UNCOV
876
                        if (r < 0)
×
UNCOV
877
                                return log_full_errno(level, r, "Failed to determine block device of /usr/ file system: %m");
×
UNCOV
878
                        if (r == 0) { /* /usr/ not backed by single block device, either. */
×
UNCOV
879
                                log_debug("Neither root nor /usr/ file system are on a (single) block device.");
×
880

UNCOV
881
                                if (ret)
×
UNCOV
882
                                        *ret = 0;
×
883

884
                                return 0;
885
                        }
886
                }
UNCOV
887
        } else if (r < 0)
×
UNCOV
888
                return log_full_errno(level, r, "Failed to read symlink /run/systemd/volatile-root: %m");
×
889
        else {
UNCOV
890
                mode_t m;
×
891
                r = device_path_parse_major_minor(p, &m, &devno);
×
UNCOV
892
                if (r < 0)
×
893
                        return log_full_errno(level, r, "Failed to parse major/minor device node: %m");
×
894
                if (!S_ISBLK(m))
×
895
                        return log_full_errno(level, SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type.");
×
896
        }
897

898
        if (ret)
3,529✔
899
                *ret = devno;
3,529✔
900

901
        return 1;
902
}
903

904
int partition_node_of(const char *node, unsigned nr, char **ret) {
5,806✔
905
        _cleanup_free_ char *fn = NULL, *dn = NULL;
5,806✔
906
        int r;
5,806✔
907

908
        assert(node);
5,806✔
909
        assert(nr > 0);
5,806✔
910
        assert(ret);
5,806✔
911

912
        /* Given a device node path to a block device returns the device node path to the partition block
913
         * device of the specified partition */
914

915
        r = path_split_prefix_filename(node, &dn, &fn);
5,806✔
916
        if (r < 0)
5,806✔
917
                return r;
918
        if (r == O_DIRECTORY)
5,803✔
919
                return -EISDIR;
920

921
        size_t l = strlen(fn);
5,802✔
922
        assert(l > 0); /* underflow check for the subtraction below */
5,802✔
923

924
        bool need_p = ascii_isdigit(fn[l-1]); /* Last char a digit? */
5,802✔
925

926
        _cleanup_free_ char *subnode = NULL;
5,802✔
927
        if (asprintf(&subnode, "%s%s%u", fn, need_p ? "p" : "", nr) < 0)
10,757✔
928
                return -ENOMEM;
929

930
        if (dn) {
5,802✔
931
                _cleanup_free_ char *j = path_join(dn, subnode);
5,800✔
932
                if (!j)
5,800✔
UNCOV
933
                        return -ENOMEM;
×
934

935
                *ret = TAKE_PTR(j);
5,800✔
936
        } else
937
                *ret = TAKE_PTR(subnode);
2✔
938

939
        return 0;
940
}
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