~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/topology/sysfs.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "topology.h"
21
 
 
22
 
/*
23
 
 * Sysfs topology values (since 2.6.31, May 2009).
24
 
 */
25
 
static struct topology_val {
26
 
 
27
 
        /* /sys/dev/block/<maj>:<min>/<ATTR> */
28
 
        const char *attr;
29
 
 
30
 
        /* functions to set probing resut */
31
 
        int (*set_ulong)(blkid_probe, unsigned long);
32
 
        int (*set_int)(blkid_probe, int);
33
 
 
34
 
} topology_vals[] = {
35
 
        { "alignment_offset", NULL, blkid_topology_set_alignment_offset },
36
 
        { "queue/minimum_io_size", blkid_topology_set_minimum_io_size },
37
 
        { "queue/optimal_io_size", blkid_topology_set_optimal_io_size },
38
 
        { "queue/physical_block_size", blkid_topology_set_physical_sector_size },
39
 
};
40
 
 
41
 
static int probe_sysfs_tp(blkid_probe pr, const struct blkid_idmag *mag)
42
 
{
43
 
        dev_t dev, pri_dev = 0;
44
 
        int i, count = 0;
45
 
 
46
 
        dev = blkid_probe_get_devno(pr);
47
 
        if (!dev)
48
 
                goto nothing;           /* probably not a block device */
49
 
 
50
 
        for (i = 0; i < ARRAY_SIZE(topology_vals); i++) {
51
 
                struct topology_val *val = &topology_vals[i];
52
 
                dev_t attr_dev = dev;
53
 
                int rc = 1;
54
 
 
55
 
                if (!blkid_devno_has_attribute(dev, val->attr)) {
56
 
                        /* get attribute from partition's primary device */
57
 
                        if (!pri_dev &&
58
 
                            blkid_devno_to_wholedisk(dev, NULL, 0, &pri_dev))
59
 
                                continue;
60
 
                        attr_dev = pri_dev;
61
 
                }
62
 
 
63
 
                if (val->set_ulong) {
64
 
                        uint64_t data = 0;
65
 
 
66
 
                        if (blkid_devno_get_u64_attribute(attr_dev,
67
 
                                                        val->attr, &data))
68
 
                                continue;
69
 
                        rc = val->set_ulong(pr, (unsigned long) data);
70
 
 
71
 
                } else if (val->set_int) {
72
 
                        int64_t data = 0;
73
 
 
74
 
                        if (blkid_devno_get_s64_attribute(attr_dev,
75
 
                                                        val->attr, &data))
76
 
                                continue;
77
 
                        rc = val->set_int(pr, (int) data);
78
 
                }
79
 
 
80
 
                if (rc)
81
 
                        goto err;
82
 
                count++;
83
 
        }
84
 
 
85
 
        if (count)
86
 
                return 0;
87
 
nothing:
88
 
        return 1;
89
 
err:
90
 
        return -1;
91
 
}
92
 
 
93
 
const struct blkid_idinfo sysfs_tp_idinfo =
94
 
{
95
 
        .name           = "sysfs",
96
 
        .probefunc      = probe_sysfs_tp,
97
 
        .magics         = BLKID_NONE_MAGIC
98
 
};
99