From 378c18e5f024ac5a8aef4cb40d7c9aa9633d144c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 16:30:35 +0200 Subject: Adding upstream version 2.38.1. Signed-off-by: Daniel Baumann --- libblkid/src/topology/sysfs.c | 124 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 libblkid/src/topology/sysfs.c (limited to 'libblkid/src/topology/sysfs.c') diff --git a/libblkid/src/topology/sysfs.c b/libblkid/src/topology/sysfs.c new file mode 100644 index 0000000..745cd11 --- /dev/null +++ b/libblkid/src/topology/sysfs.c @@ -0,0 +1,124 @@ +/* + * sysfs based topology -- gathers topology information from Linux sysfs + * + * Copyright (C) 2009 Karel Zak + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + * + * For more information see Linux kernel Documentation/ABI/testing/sysfs-block. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sysfs.h" +#include "topology.h" + +/* + * Sysfs topology values (since 2.6.31, May 2009). + */ +static struct topology_val { + + /* /sys/dev/block/:/ */ + const char *attr; + + /* functions to set probing result */ + int (*set_ulong)(blkid_probe, unsigned long); + int (*set_int)(blkid_probe, int); + +} topology_vals[] = { + { "alignment_offset", NULL, blkid_topology_set_alignment_offset }, + { "queue/minimum_io_size", blkid_topology_set_minimum_io_size }, + { "queue/optimal_io_size", blkid_topology_set_optimal_io_size }, + { "queue/physical_block_size", blkid_topology_set_physical_sector_size }, + { "queue/dax", blkid_topology_set_dax }, +}; + +static int probe_sysfs_tp(blkid_probe pr, + const struct blkid_idmag *mag __attribute__((__unused__))) +{ + dev_t dev; + int rc, set_parent = 1; + struct path_cxt *pc; + size_t i, count = 0; + + dev = blkid_probe_get_devno(pr); + if (!dev) + return 1; + pc = ul_new_sysfs_path(dev, NULL, NULL); + if (!pc) + return 1; + + rc = 1; /* nothing (default) */ + + for (i = 0; i < ARRAY_SIZE(topology_vals); i++) { + struct topology_val *val = &topology_vals[i]; + int ok = ul_path_access(pc, F_OK, val->attr) == 0; + + rc = 1; /* nothing */ + + if (!ok && set_parent) { + dev_t disk = blkid_probe_get_wholedisk_devno(pr); + set_parent = 0; + + /* + * Read attributes from "disk" if the current device is + * a partition. Note that sysfs ul_path_* API is able + * to redirect requests to attributes if parent is set. + */ + if (disk && disk != dev) { + struct path_cxt *parent = ul_new_sysfs_path(disk, NULL, NULL); + if (!parent) + goto done; + + sysfs_blkdev_set_parent(pc, parent); + ul_unref_path(parent); + + /* try it again */ + ok = ul_path_access(pc, F_OK, val->attr) == 0; + } + } + if (!ok) + continue; /* attribute does not exist */ + + if (val->set_ulong) { + uint64_t data; + + if (ul_path_read_u64(pc, &data, val->attr) != 0) + continue; + rc = val->set_ulong(pr, (unsigned long) data); + + } else if (val->set_int) { + int64_t data; + + if (ul_path_read_s64(pc, &data, val->attr) != 0) + continue; + rc = val->set_int(pr, (int) data); + } + + if (rc < 0) + goto done; /* error */ + if (rc == 0) + count++; + } + +done: + ul_unref_path(pc); /* unref pc and parent */ + if (count) + return 0; /* success */ + return rc; /* error or nothing */ +} + +const struct blkid_idinfo sysfs_tp_idinfo = +{ + .name = "sysfs", + .probefunc = probe_sysfs_tp, + .magics = BLKID_NONE_MAGIC +}; + -- cgit v1.2.3