2 * sysfs based topology -- gathers topology information from Linux sysfs
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
9 * For more information see Linux kernel Documentation/ABI/testing/sysfs-block.
15 #include <sys/types.h>
24 * Sysfs topology values (since 2.6.31, May 2009).
26 static struct topology_val {
28 /* /sys/dev/block/<maj>:<min>/<ATTR> */
31 /* functions to set probing resut */
32 int (*set_ulong)(blkid_probe, unsigned long);
33 int (*set_int)(blkid_probe, int);
36 { "alignment_offset", NULL, blkid_topology_set_alignment_offset },
37 { "queue/minimum_io_size", blkid_topology_set_minimum_io_size },
38 { "queue/optimal_io_size", blkid_topology_set_optimal_io_size },
39 { "queue/physical_block_size", blkid_topology_set_physical_sector_size },
42 static int probe_sysfs_tp(blkid_probe pr,
43 const struct blkid_idmag *mag __attribute__((__unused__)))
47 struct sysfs_cxt sysfs, parent;
50 dev = blkid_probe_get_devno(pr);
51 if (!dev || sysfs_init(&sysfs, dev, NULL) != 0)
54 rc = 1; /* nothing (default) */
56 for (i = 0; i < ARRAY_SIZE(topology_vals); i++) {
57 struct topology_val *val = &topology_vals[i];
58 int ok = sysfs_has_attribute(&sysfs, val->attr);
65 * Read atrributes from "disk" if the current
66 * device is a partition.
68 disk = blkid_probe_get_wholedisk_devno(pr);
69 if (disk && disk != dev) {
70 if (sysfs_init(&parent, disk, NULL) != 0)
73 sysfs.parent = &parent;
74 ok = sysfs_has_attribute(&sysfs,
79 continue; /* attrinute does not exist */
85 if (sysfs_read_u64(&sysfs, val->attr, &data) != 0)
87 rc = val->set_ulong(pr, (unsigned long) data);
89 } else if (val->set_int) {
92 if (sysfs_read_s64(&sysfs, val->attr, &data) != 0)
94 rc = val->set_int(pr, (int) data);
98 goto done; /* error */
104 sysfs_deinit(&sysfs);
106 sysfs_deinit(&parent);
108 return 0; /* success */
109 return rc; /* error or nothing */
112 const struct blkid_idinfo sysfs_tp_idinfo =
115 .probefunc = probe_sysfs_tp,
116 .magics = BLKID_NONE_MAGIC