tree-wide: use devpath_from_devnum() and device_open_from_devnum()

Fixes #24465.
This commit is contained in:
Yu Watanabe
2022-08-28 10:10:50 +09:00
parent 42899bcee5
commit ca8228295e
12 changed files with 68 additions and 159 deletions
+3 -7
View File
@@ -295,9 +295,8 @@ static int swap_verify(Swap *s) {
}
static int swap_load_devnode(Swap *s) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_free_ char *p = NULL;
struct stat st;
const char *p;
int r;
assert(s);
@@ -305,16 +304,13 @@ static int swap_load_devnode(Swap *s) {
if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
return 0;
r = sd_device_new_from_stat_rdev(&d, &st);
r = devpath_from_devnum(S_IFBLK, st.st_rdev, &p);
if (r < 0) {
log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
"Failed to allocate device for swap %s: %m", s->what);
"Failed to get device node for swap %s: %m", s->what);
return 0;
}
if (sd_device_get_devname(d, &p) < 0)
return 0;
return swap_set_devnode(s, p);
}
+7 -12
View File
@@ -14,6 +14,8 @@
#include "blockdev-util.h"
#include "chase-symlinks.h"
#include "copy.h"
#include "device-util.h"
#include "devnum-util.h"
#include "dissect-image.h"
#include "env-util.h"
#include "fd-util.h"
@@ -859,11 +861,9 @@ static int action_copy(DissectedImage *m, LoopDevice *d) {
static int action_umount(const char *path) {
_cleanup_close_ int fd = -1;
_cleanup_free_ char *canonical = NULL;
dev_t devno;
const char *devname;
_cleanup_free_ char *canonical = NULL, *devname = NULL;
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
dev_t devno;
int r, k;
fd = chase_symlinks_and_open(path, NULL, 0, O_DIRECTORY, &canonical);
@@ -882,15 +882,10 @@ static int action_umount(const char *path) {
if (r < 0)
return log_error_errno(r, "Failed to find backing block device for '%s': %m", canonical);
r = sd_device_new_from_devnum(&device, 'b', devno);
r = devpath_from_devnum(S_IFBLK, devno, &devname);
if (r < 0)
return log_error_errno(r, "Failed to create sd-device object for block device %u:%u: %m",
major(devno), minor(devno));
r = sd_device_get_devname(device, &devname);
if (r < 0)
return log_error_errno(r, "Failed to get devname of block device %u:%u: %m",
major(devno), minor(devno));
return log_error_errno(r, "Failed to get devname of block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
r = loop_device_open(devname, 0, &d);
if (r < 0)
+3 -8
View File
@@ -19,6 +19,7 @@
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "chattr-util.h"
#include "device-util.h"
#include "devnum-util.h"
#include "dm-util.h"
#include "env-util.h"
@@ -3121,20 +3122,14 @@ int home_resize_luks(
log_info("Operating on partition device %s, using parent device.", ip);
r = device_path_make_major_minor(st.st_mode, parent, &whole_disk);
opened_image_fd = r = device_open_from_devnum(st.st_mode, parent, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK, &whole_disk);
if (r < 0)
return log_error_errno(r, "Failed to derive whole disk path for %s: %m", ip);
opened_image_fd = open(whole_disk, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (opened_image_fd < 0)
return log_error_errno(errno, "Failed to open whole block device %s: %m", whole_disk);
return log_error_errno(r, "Failed to open whole block device for %s: %m", ip);
image_fd = opened_image_fd;
if (fstat(image_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat whole block device %s: %m", whole_disk);
if (!S_ISBLK(st.st_mode))
return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Whole block device %s is not actually a block device, refusing.", whole_disk);
} else
log_info("Operating on whole block device %s.", ip);
+2 -1
View File
@@ -8,6 +8,7 @@
#include "bus-util.h"
#include "cap-list.h"
#include "cpu-set-util.h"
#include "device-util.h"
#include "devnum-util.h"
#include "env-util.h"
#include "format-util.h"
@@ -899,7 +900,7 @@ static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags
}
/* Suppress a couple of implicit device nodes */
r = device_path_make_canonical(node->mode, makedev(node->major, node->minor), &path);
r = devpath_from_devnum(node->mode, makedev(node->major, node->minor), &path);
if (r < 0)
json_log(e, flags|JSON_DEBUG, 0, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor);
else {
+12 -46
View File
@@ -34,8 +34,7 @@ static bool arg_dry_run = false;
#if HAVE_LIBCRYPTSETUP
static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
_cleanup_(sd_device_unrefp) sd_device *main_dev = NULL, *dev = NULL;
const char *devpath, *main_devpath;
_cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
_cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
_cleanup_close_ int main_devfd = -1;
uint64_t size;
@@ -45,33 +44,21 @@ static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_
if (r < 0)
return log_error_errno(r, "Cannot resize LUKS device: %m");
r = sd_device_new_from_devnum(&main_dev, 'b', main_devno);
main_devfd = r = device_open_from_devnum(S_IFBLK, main_devno, O_RDONLY|O_CLOEXEC, &main_devpath);
if (r < 0)
return log_error_errno(r, "Failed to create main sd-device for block device " DEVNUM_FORMAT_STR ": %m",
return log_error_errno(r, "Failed to open main block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(main_devno));
r = sd_device_get_devname(main_dev, &main_devpath);
if (r < 0)
return log_device_error_errno(main_dev, r, "Failed to get main devpath: %m");
main_devfd = sd_device_open(main_dev, O_RDONLY|O_CLOEXEC);
if (main_devfd < 0)
return log_device_error_errno(main_dev, main_devfd, "Failed to open block device \"%s\": %m",
main_devpath);
if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m",
main_devpath);
log_debug("%s is %"PRIu64" bytes", main_devpath, size);
r = sd_device_new_from_devnum(&dev, 'b', devno);
if (r < 0)
return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
r = sd_device_get_devname(dev, &devpath);
r = devpath_from_devnum(S_IFBLK, devno, &devpath);
if (r < 0)
return log_device_error_errno(dev, r, "Failed to get devpath: %m");
return log_error_errno(r, "Failed to get devpath of " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
r = sym_crypt_init(&cd, devpath);
if (r < 0)
@@ -105,9 +92,7 @@ static int maybe_resize_underlying_device(
const char *mountpath,
dev_t main_devno) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_free_ char *fstype = NULL;
const char *devpath;
_cleanup_free_ char *devpath = NULL, *fstype = NULL;
dev_t devno;
int r;
@@ -132,15 +117,11 @@ static int maybe_resize_underlying_device(
if (devno == main_devno)
return 0;
r = sd_device_new_from_devnum(&dev, 'b', devno);
r = devpath_from_devnum(S_IFBLK, devno, &devpath);
if (r < 0)
return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
return log_error_errno(r, "Failed to get devpath for block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
r = sd_device_get_devname(dev, &devpath);
if (r < 0)
return log_device_error_errno(dev, r, "Failed to get devpath: %m");
r = probe_filesystem(devpath, &fstype);
if (r == -EUCLEAN)
return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath);
@@ -224,11 +205,9 @@ static int parse_argv(int argc, char *argv[]) {
}
static int run(int argc, char *argv[]) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int mountfd = -1, devfd = -1;
const char *devpath;
_cleanup_free_ char *devpath = NULL;
uint64_t size, newsize;
struct stat st;
dev_t devno;
int r;
@@ -260,24 +239,11 @@ static int run(int argc, char *argv[]) {
if (r < 0)
log_warning_errno(r, "Unable to resize underlying device of \"%s\", proceeding anyway: %m", arg_target);
r = sd_device_new_from_devnum(&dev, 'b', devno);
devfd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC, &devpath);
if (r < 0)
return log_error_errno(r, "Failed to create sd-device for block device " DEVNUM_FORMAT_STR ": %m",
return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
r = sd_device_get_devname(dev, &devpath);
if (r < 0)
return log_device_error_errno(dev, r, "Failed to get devpath: %m");
devfd = sd_device_open(dev, O_RDONLY|O_CLOEXEC);
if (devfd < 0)
return log_device_error_errno(dev, devfd, "Failed to open block device \"%s\": %m", devpath);
if (fstat(devfd, &st) < 0)
return log_error_errno(r, "Failed to stat() device %s: %m", devpath);
if (!S_ISBLK(st.st_mode))
return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Backing device of file system is not a block device, refusing.");
if (ioctl(devfd, BLKGETSIZE64, &size) != 0)
return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath);
+16 -48
View File
@@ -24,6 +24,7 @@
#include "conf-parser.h"
#include "cryptsetup-util.h"
#include "def.h"
#include "device-util.h"
#include "devnum-util.h"
#include "dirent-util.h"
#include "efivars.h"
@@ -3673,9 +3674,9 @@ static int resolve_copy_blocks_auto_candidate(
sd_id128_t *ret_uuid) {
_cleanup_(blkid_free_probep) blkid_probe b = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int fd = -1;
const char *pttype, *t, *p;
_cleanup_free_ char *p = NULL;
const char *pttype, *t;
sd_id128_t pt_parsed, u;
blkid_partition pp;
dev_t whole_devno;
@@ -3702,19 +3703,10 @@ static int resolve_copy_blocks_auto_candidate(
major(partition_devno), minor(partition_devno),
major(restrict_devno), minor(restrict_devno));
r = sd_device_new_from_devnum(&dev, 'b', whole_devno);
fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
if (r < 0)
return log_error_errno(r, "Failed to create sd-device for block device %u:%u: %m",
major(whole_devno), minor(whole_devno));
r = sd_device_get_devname(dev, &p);
if (r < 0)
return log_error_errno(r, "Failed to get name of block device %u:%u: %m",
major(whole_devno), minor(whole_devno));
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (fd < 0)
return log_error_errno(fd, "Failed to open block device %s: %m", p);
return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(whole_devno));
b = blkid_new_probe();
if (!b)
@@ -3996,29 +3988,16 @@ static int context_open_copy_block_paths(
"Copying from block device node is not permitted in --image=/--root= mode, refusing.");
} else if (p->copy_blocks_auto) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *devname;
dev_t devno;
r = resolve_copy_blocks_auto(p->type_uuid, root, restrict_devno, &devno, &uuid);
if (r < 0)
return r;
r = sd_device_new_from_devnum(&dev, 'b', devno);
source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
if (r < 0)
return log_error_errno(r, "Failed to create sd-device object for device %u:%u: %m", major(devno), minor(devno));
r = sd_device_get_devname(dev, &devname);
if (r < 0)
return log_error_errno(r, "Failed to get device name of %u:%u: %m", major(devno), minor(devno));
opened = strdup(devname);
if (!opened)
return log_oom();
source_fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (source_fd < 0)
return log_error_errno(source_fd, "Failed to open automatically determined source block copy device '%s': %m", opened);
return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devno));
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
@@ -4026,38 +4005,27 @@ static int context_open_copy_block_paths(
continue;
if (S_ISDIR(st.st_mode)) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *bdev;
_cleanup_free_ char *bdev = NULL;
dev_t devt;
/* If the file is a directory, automatically find the backing block device */
if (major(st.st_dev) != 0)
r = sd_device_new_from_devnum(&dev, 'b', st.st_dev);
devt = st.st_dev;
else {
dev_t devt;
/* Special support for btrfs */
r = btrfs_get_block_device_fd(source_fd, &devt);
if (r == -EUCLEAN)
return btrfs_log_dev_root(LOG_ERR, r, opened);
if (r < 0)
return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
r = sd_device_new_from_devnum(&dev, 'b', devt);
}
if (r < 0)
return log_error_errno(r, "Failed to create sd-device object for block device backing '%s': %m", opened);
r = sd_device_get_devname(dev, &bdev);
if (r < 0)
return log_error_errno(r, "Failed to get device name for block device backing '%s': %m", opened);
safe_close(source_fd);
source_fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (source_fd < 0)
return log_error_errno(source_fd, "Failed to open block device '%s': %m", bdev);
source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
if (r < 0)
return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
if (fstat(source_fd, &st) < 0)
return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
@@ -4548,7 +4516,7 @@ static int acquire_root_devno(
if (r < 0)
log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
r = device_path_make_canonical(S_IFBLK, devno, ret);
r = devpath_from_devnum(S_IFBLK, devno, ret);
if (r < 0)
return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
+1 -6
View File
@@ -221,7 +221,6 @@ int get_block_device_harder(const char *path, dev_t *ret) {
}
int lock_whole_block_device(dev_t devt, int operation) {
_cleanup_free_ char *whole_node = NULL;
_cleanup_close_ int lock_fd = -1;
dev_t whole_devt;
int r;
@@ -232,14 +231,10 @@ int lock_whole_block_device(dev_t devt, int operation) {
if (r < 0)
return r;
r = device_path_make_major_minor(S_IFBLK, whole_devt, &whole_node);
lock_fd = r = device_open_from_devnum(S_IFBLK, whole_devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, NULL);
if (r < 0)
return r;
lock_fd = open(whole_node, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (lock_fd < 0)
return -errno;
if (flock(lock_fd, operation) < 0)
return -errno;
+16 -19
View File
@@ -45,9 +45,9 @@ static int verify_esp_blkid(
const char *v;
int r;
r = device_path_make_major_minor(S_IFBLK, devid, &node);
r = devpath_from_devnum(S_IFBLK, devid, &node);
if (r < 0)
return log_error_errno(r, "Failed to format major/minor device path: %m");
return log_error_errno(r, "Failed to get device path for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid));
errno = 0;
b = blkid_new_probe_from_filename(node);
@@ -151,21 +151,20 @@ static int verify_esp_udev(
sd_id128_t *ret_uuid) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_free_ char *node = NULL;
sd_id128_t uuid = SD_ID128_NULL;
uint64_t pstart = 0, psize = 0;
uint32_t part = 0;
const char *v;
const char *node, *v;
int r;
r = device_path_make_major_minor(S_IFBLK, devid, &node);
if (r < 0)
return log_error_errno(r, "Failed to format major/minor device path: %m");
r = sd_device_new_from_devnum(&d, 'b', devid);
if (r < 0)
return log_error_errno(r, "Failed to get device from device number: %m");
r = sd_device_get_devname(d, &node);
if (r < 0)
return log_error_errno(r, "Failed to get device node: %m");
r = sd_device_get_property_value(d, "ID_FS_TYPE", &v);
if (r < 0)
return log_error_errno(r, "Failed to get device property: %m");
@@ -509,10 +508,10 @@ static int verify_xbootldr_blkid(
const char *type, *v;
int r;
r = device_path_make_major_minor(S_IFBLK, devid, &node);
r = devpath_from_devnum(S_IFBLK, devid, &node);
if (r < 0)
return log_error_errno(r, "Failed to format block device path for %u:%u: %m",
major(devid), minor(devid));
return log_error_errno(r, "Failed to get block device path for " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devid));
errno = 0;
b = blkid_new_probe_from_filename(node);
@@ -583,19 +582,17 @@ static int verify_xbootldr_udev(
sd_id128_t *ret_uuid) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_free_ char *node = NULL;
sd_id128_t uuid = SD_ID128_NULL;
const char *type, *v;
const char *node, *type, *v;
int r;
r = device_path_make_major_minor(S_IFBLK, devid, &node);
if (r < 0)
return log_error_errno(r, "Failed to format block device path for %u:%u: %m",
major(devid), minor(devid));
r = sd_device_new_from_devnum(&d, 'b', devid);
if (r < 0)
return log_error_errno(r, "%s: Failed to get block device: %m", node);
return log_error_errno(r, "Failed to get block device for " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(devid));
r = sd_device_get_devname(d, &node);
if (r < 0)
return log_error_errno(r, "Failed to get device node: %m");
r = sd_device_get_property_value(d, "ID_PART_ENTRY_SCHEME", &type);
if (r < 0)
+2 -6
View File
@@ -806,7 +806,7 @@ int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) {
static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
char sysfs[STRLEN("/sys/dev/block/:/partition") + 2*DECIMAL_STR_MAX(dev_t) + 1];
_cleanup_free_ char *whole = NULL, *buffer = NULL;
_cleanup_free_ char *buffer = NULL;
uint64_t current_offset, current_size, partno;
_cleanup_close_ int whole_fd = -1;
struct stat st;
@@ -865,14 +865,10 @@ static int resize_partition(int partition_fd, uint64_t offset, uint64_t size) {
if (r < 0)
return r;
r = device_path_make_major_minor(S_IFBLK, devno, &whole);
whole_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, NULL);
if (r < 0)
return r;
whole_fd = open(whole, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (whole_fd < 0)
return -errno;
struct blkpg_partition bp = {
.pno = partno,
.start = offset == UINT64_MAX ? current_offset : offset,
+2 -2
View File
@@ -5,7 +5,7 @@
#include "alloc-util.h"
#include "blockdev-util.h"
#include "devnum-util.h"
#include "device-util.h"
#include "quota-util.h"
int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr) {
@@ -15,7 +15,7 @@ int quotactl_devnum(int cmd, dev_t devnum, int id, void *addr) {
/* Like quotactl() but takes a dev_t instead of a path to a device node, and fixes caddr_t → void*,
* like we should, today */
r = device_path_make_major_minor(S_IFBLK, devnum, &devnode);
r = devpath_from_devnum(S_IFBLK, devnum, &devnode);
if (r < 0)
return r;
+2 -2
View File
@@ -7,7 +7,7 @@
#include "alloc-util.h"
#include "blockdev-util.h"
#include "chase-symlinks.h"
#include "devnum-util.h"
#include "device-util.h"
#include "dirent-util.h"
#include "env-util.h"
#include "fd-util.h"
@@ -608,7 +608,7 @@ int resource_resolve_path(
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"File system is not placed on a partition block device, cannot determine whole block device backing root file system.");
r = device_path_make_canonical(S_IFBLK, d, &p);
r = devpath_from_devnum(S_IFBLK, d, &p);
if (r < 0)
return r;
+2 -2
View File
@@ -7,7 +7,7 @@
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "devnum-util.h"
#include "device-util.h"
#include "fd-util.h"
#include "fdset.h"
#include "main-func.h"
@@ -321,7 +321,7 @@ int lock_main(int argc, char *argv[], void *userdata) {
for (size_t i = 0; i < n_devnos; i++) {
_cleanup_free_ char *node = NULL;
r = device_path_make_canonical(S_IFBLK, devnos[i], &node);
r = devpath_from_devnum(S_IFBLK, devnos[i], &node);
if (r < 0)
return log_error_errno(r, "Failed to format block device path: %m");