Debian Codegraph @

src/shared/blockdev-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <sys/sysmacros.h>
5
6 #include "shared-forward.h"
7 #include "stdio-util.h"
8
9 #define SYS_BLOCK_PATH_MAX(suffix) \
10 (STRLEN("/sys/dev/block/") + DECIMAL_STR_MAX(dev_t) + 1 + DECIMAL_STR_MAX(dev_t) + STRLEN(suffix))
11 #define xsprintf_sys_block_path(buf, suffix, devno) \
12 xsprintf(buf, "/sys/dev/block/%u:%u%s", major(devno), minor(devno), suffix ?: "")
13
14 typedef enum BlockDeviceLookupFlags {
15 BLOCK_DEVICE_LOOKUP_WHOLE_DISK = 1 << 0, /* whole block device, e.g. sda, nvme0n1, or loop0. */
16 BLOCK_DEVICE_LOOKUP_BACKING = 1 << 1, /* fd may be regular file or directory on file system, in
17 * which case backing block device is determined. */
18 BLOCK_DEVICE_LOOKUP_ORIGINATING = 1 << 2, /* Try to find the underlying layer device for stacked
19 * block device, e.g. LUKS-style DM. */
20 } BlockDeviceLookupFlags;
21
22 int block_device_new_from_fd(int fd, BlockDeviceLookupFlags flags, sd_device **ret);
23 int block_device_new_from_path(const char *path, BlockDeviceLookupFlags flags, sd_device **ret);
24
25 int block_device_is_whole_disk(sd_device *dev);
26 int block_device_get_whole_disk(sd_device *dev, sd_device **ret);
27 int block_device_get_originating(sd_device *dev, sd_device **ret, bool recursive);
28 int block_get_whole_disk(dev_t d, dev_t *ret);
29 int block_get_originating(dev_t d, dev_t *ret, bool recursive);
30
31 int get_block_device_fd(int fd, dev_t *ret);
32 int get_block_device(const char *path, dev_t *ret);
33
34 int get_block_device_harder_fd(int fd, dev_t *ret);
35 int get_block_device_harder(const char *path, dev_t *ret);
36
37 int lock_whole_block_device(dev_t devt, int open_flags, int operation);
38
39 int blockdev_partscan_enabled(sd_device *d);
40 int blockdev_partscan_enabled_fd(int fd);
41
42 int fd_is_encrypted(int fd);
43 int path_is_encrypted(const char *path);
44
45 int fd_get_whole_disk(int fd, bool backing, dev_t *ret);
46 int path_get_whole_disk(const char *path, bool backing, dev_t *ret);
47
48 int block_device_add_partition(int fd, int nr, uint64_t start, uint64_t size);
49 int block_device_remove_partition(int fd, int nr);
50 int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size);
51 int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret);
52 int block_device_remove_all_partitions(sd_device *dev, int fd);
53
54 int blockdev_get_sector_size(int fd, uint32_t *ret);
55 int blockdev_get_device_size(int fd, uint64_t *ret);
56
57 int blockdev_get_root(int level, dev_t *ret);
58
59 int partition_node_of(const char *node, unsigned nr, char **ret);