Coverage Report

Created: 2026-09-13 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/util-linux/libblkid/src/topology/sysfs.c
Line
Count
Source
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 const struct topology_val {
27
28
  /* /sys/dev/block/<maj>:<min>/<ATTR> */
29
  const char * const attr;
30
31
  /* functions to set probing result */
32
  int (*set_ulong)(blkid_probe, unsigned long);
33
  int (*set_int)(blkid_probe, int);
34
  int (*set_u64)(blkid_probe, uint64_t);
35
36
} topology_vals[] = {
37
  { "alignment_offset", NULL, blkid_topology_set_alignment_offset },
38
  { "queue/minimum_io_size", blkid_topology_set_minimum_io_size },
39
  { "queue/optimal_io_size", blkid_topology_set_optimal_io_size },
40
  { "queue/physical_block_size", blkid_topology_set_physical_sector_size },
41
  { "queue/dax", blkid_topology_set_dax },
42
  { "diskseq", .set_u64 = blkid_topology_set_diskseq },
43
};
44
45
static int probe_sysfs_tp(blkid_probe pr,
46
    const struct blkid_idmag *mag __attribute__((__unused__)))
47
0
{
48
0
  dev_t dev;
49
0
  int rc, set_parent = 1;
50
0
  struct path_cxt *pc;
51
0
  size_t i, count = 0;
52
53
0
  dev = blkid_probe_get_devno(pr);
54
0
  if (!dev)
55
0
    return 1;
56
0
  pc = ul_new_sysfs_path(dev, NULL, NULL);
57
0
  if (!pc)
58
0
    return 1;
59
0
  ul_path_refer_vfs(pc, pr->vfs);
60
61
0
  rc = 1;   /* nothing (default) */
62
63
0
  for (i = 0; i < ARRAY_SIZE(topology_vals); i++) {
64
0
    const struct topology_val *val = &topology_vals[i];
65
0
    int ok = ul_path_access(pc, F_OK, val->attr) == 0;
66
67
0
    rc = 1; /* nothing */
68
69
0
    if (!ok && set_parent) {
70
0
      dev_t disk = blkid_probe_get_wholedisk_devno(pr);
71
0
      set_parent = 0;
72
73
      /*
74
       * Read attributes from "disk" if the current device is
75
       * a partition. Note that sysfs ul_path_* API is able
76
       * to redirect requests to attributes if parent is set.
77
       */
78
0
      if (disk && disk != dev) {
79
0
        struct path_cxt *parent = ul_new_sysfs_path(disk, NULL, NULL);
80
0
        if (!parent)
81
0
          goto done;
82
83
0
        sysfs_blkdev_set_parent(pc, parent);
84
0
        ul_unref_path(parent);
85
86
        /* try it again */
87
0
        ok = ul_path_access(pc, F_OK, val->attr) == 0;
88
0
      }
89
0
    }
90
0
    if (!ok)
91
0
      continue; /* attribute does not exist */
92
93
0
    if (val->set_ulong) {
94
0
      uint64_t data;
95
96
0
      if (ul_path_read_u64(pc, &data, val->attr) != 0)
97
0
        continue;
98
0
      rc = val->set_ulong(pr, (unsigned long) data);
99
100
0
    } else if (val->set_int) {
101
0
      int64_t data;
102
103
0
      if (ul_path_read_s64(pc, &data, val->attr) != 0)
104
0
        continue;
105
0
      rc = val->set_int(pr, (int) data);
106
0
    } else if (val->set_u64) {
107
0
      uint64_t data;
108
109
0
      if (ul_path_read_u64(pc, &data, val->attr) != 0)
110
0
        continue;
111
0
      rc = val->set_u64(pr, data);
112
0
    }
113
114
0
    if (rc < 0)
115
0
      goto done; /* error */
116
0
    if (rc == 0)
117
0
      count++;
118
0
  }
119
120
0
done:
121
0
  ul_unref_path(pc);    /* unref pc and parent */
122
0
  if (count)
123
0
    return 0;   /* success */
124
0
  return rc;     /* error or nothing */
125
0
}
126
127
const struct blkid_idinfo sysfs_tp_idinfo =
128
{
129
  .name   = "sysfs",
130
  .probefunc  = probe_sysfs_tp,
131
  .magics   = BLKID_NONE_MAGIC
132
};
133