Update to upstream util-linux 2.20.1
[framework/base/util-linux-ng.git] / libblkid / src / topology / sysfs.c
1 /*
2  * sysfs based topology -- gathers topology information from Linux sysfs
3  *
4  * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
5  *
6  * This file may be redistributed under the terms of the
7  * GNU Lesser General Public License.
8  *
9  * For more information see Linux kernel Documentation/ABI/testing/sysfs-block.
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <inttypes.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <errno.h>
19
20 #include "sysfs.h"
21 #include "topology.h"
22
23 /*
24  * Sysfs topology values (since 2.6.31, May 2009).
25  */
26 static struct topology_val {
27
28         /* /sys/dev/block/<maj>:<min>/<ATTR> */
29         const char *attr;
30
31         /* functions to set probing resut */
32         int (*set_ulong)(blkid_probe, unsigned long);
33         int (*set_int)(blkid_probe, int);
34
35 } topology_vals[] = {
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 },
40 };
41
42 static int probe_sysfs_tp(blkid_probe pr,
43                 const struct blkid_idmag *mag __attribute__((__unused__)))
44 {
45         dev_t dev, disk = 0;
46         int rc;
47         struct sysfs_cxt sysfs, parent;
48         size_t i, count = 0;
49
50         dev = blkid_probe_get_devno(pr);
51         if (!dev || sysfs_init(&sysfs, dev, NULL) != 0)
52                 return 1;
53
54         rc = 1;         /* nothing (default) */
55
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);
59
60                 rc = 1; /* nothing */
61
62                 if (!ok) {
63                         if (!disk) {
64                                 /*
65                                  * Read atrributes from "disk" if the current
66                                  * device is a partition.
67                                  */
68                                 disk = blkid_probe_get_wholedisk_devno(pr);
69                                 if (disk && disk != dev) {
70                                         if (sysfs_init(&parent, disk, NULL) != 0)
71                                                 goto done;
72
73                                         sysfs.parent = &parent;
74                                         ok = sysfs_has_attribute(&sysfs,
75                                                                  val->attr);
76                                 }
77                         }
78                         if (!ok)
79                                 continue;       /* attrinute does not exist */
80                 }
81
82                 if (val->set_ulong) {
83                         uint64_t data;
84
85                         if (sysfs_read_u64(&sysfs, val->attr, &data) != 0)
86                                 continue;
87                         rc = val->set_ulong(pr, (unsigned long) data);
88
89                 } else if (val->set_int) {
90                         int64_t data;
91
92                         if (sysfs_read_s64(&sysfs, val->attr, &data) != 0)
93                                 continue;
94                         rc = val->set_int(pr, (int) data);
95                 }
96
97                 if (rc < 0)
98                         goto done;      /* error */
99                 if (rc == 0)
100                         count++;
101         }
102
103 done:
104         sysfs_deinit(&sysfs);
105         if (disk)
106                 sysfs_deinit(&parent);
107         if (count)
108                 return 0;               /* success */
109         return rc;                      /* error or nothing */
110 }
111
112 const struct blkid_idinfo sysfs_tp_idinfo =
113 {
114         .name           = "sysfs",
115         .probefunc      = probe_sysfs_tp,
116         .magics         = BLKID_NONE_MAGIC
117 };
118