1 /* $NetBSD: dev-md.c,v 1.1.1.2 2009/12/02 00:26:33 haad Exp $ */
4 * Copyright (C) 2004 Luca Berra
5 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* Lifted from <linux/raid/md_p.h> because of difficulty including it */
27 #define MD_SB_MAGIC 0xa92b4efc
28 #define MD_RESERVED_BYTES (64 * 1024ULL)
29 #define MD_RESERVED_SECTORS (MD_RESERVED_BYTES / 512)
30 #define MD_NEW_SIZE_SECTORS(x) ((x & ~(MD_RESERVED_SECTORS - 1)) \
31 - MD_RESERVED_SECTORS)
33 static int _dev_has_md_magic(struct device *dev, uint64_t sb_offset)
37 /* Version 1 is little endian; version 0.90.0 is machine endian */
38 if (dev_read(dev, sb_offset, sizeof(uint32_t), &md_magic) &&
39 ((md_magic == xlate32(MD_SB_MAGIC)) ||
40 (md_magic == MD_SB_MAGIC)))
47 * Calculate the position of the superblock.
48 * It is always aligned to a 4K boundary and
49 * depending on minor_version, it can be:
50 * 0: At least 8K, but less than 12K, from end of device
51 * 1: At start of device
52 * 2: 4K from start of device.
56 MD_MINOR_V0 = MD_MINOR_VERSION_MIN,
59 MD_MINOR_VERSION_MAX = MD_MINOR_V2
62 static uint64_t _v1_sb_offset(uint64_t size, md_minor_version_t minor_version)
64 uint64_t uninitialized_var(sb_offset);
66 switch(minor_version) {
68 sb_offset = (size - 8 * 2) & ~(4 * 2 - 1ULL);
77 sb_offset <<= SECTOR_SHIFT;
85 int dev_is_md(struct device *dev, uint64_t *sb)
88 md_minor_version_t minor;
89 uint64_t size, sb_offset;
91 if (!dev_get_size(dev, &size)) {
96 if (size < MD_RESERVED_SECTORS * 2)
104 /* Check if it is an md component device. */
106 sb_offset = MD_NEW_SIZE_SECTORS(size) << SECTOR_SHIFT;
107 if (_dev_has_md_magic(dev, sb_offset))
110 minor = MD_MINOR_VERSION_MIN;
111 /* Version 1, try v1.0 -> v1.2 */
113 sb_offset = _v1_sb_offset(size, minor);
114 if (_dev_has_md_magic(dev, sb_offset))
116 } while (++minor <= MD_MINOR_VERSION_MAX);
130 static int _md_sysfs_attribute_snprintf(char *path, size_t size,
131 const char *sysfs_dir,
132 struct device *blkdev,
133 const char *attribute)
136 dev_t dev = blkdev->dev;
139 if (!sysfs_dir || !*sysfs_dir)
142 if (MAJOR(dev) == blkext_major()) {
143 /* lookup parent MD device from blkext partition */
144 if (!get_primary_dev(sysfs_dir, blkdev, &dev))
148 if (MAJOR(dev) != md_major())
151 ret = dm_snprintf(path, size, "%s/dev/block/%d:%d/md/%s", sysfs_dir,
152 (int)MAJOR(dev), (int)MINOR(dev), attribute);
154 log_error("dm_snprintf md %s failed", attribute);
158 if (stat(path, &info) == -1) {
159 if (errno != ENOENT) {
160 log_sys_error("stat", path);
163 /* old sysfs structure */
164 ret = dm_snprintf(path, size, "%s/block/md%d/md/%s",
165 sysfs_dir, (int)MINOR(dev), attribute);
167 log_error("dm_snprintf old md %s failed", attribute);
175 static int _md_sysfs_attribute_scanf(const char *sysfs_dir,
177 const char *attribute_name,
178 const char *attribute_fmt,
179 void *attribute_value)
181 char path[PATH_MAX+1], buffer[64];
185 if (_md_sysfs_attribute_snprintf(path, PATH_MAX, sysfs_dir,
186 dev, attribute_name) < 0)
189 if (!(fp = fopen(path, "r"))) {
190 log_sys_error("fopen", path);
194 if (!fgets(buffer, sizeof(buffer), fp)) {
195 log_sys_error("fgets", path);
199 if ((ret = sscanf(buffer, attribute_fmt, attribute_value)) != 1) {
200 log_error("%s sysfs attr %s not in expected format: %s",
201 dev_name(dev), attribute_name, buffer);
207 log_sys_error("fclose", path);
213 * Retrieve chunk size from md device using sysfs.
215 static unsigned long dev_md_chunk_size(const char *sysfs_dir,
218 const char *attribute = "chunk_size";
219 unsigned long chunk_size_bytes = 0UL;
221 if (_md_sysfs_attribute_scanf(sysfs_dir, dev, attribute,
222 "%lu", &chunk_size_bytes) != 1)
225 log_very_verbose("Device %s %s is %lu bytes.",
226 dev_name(dev), attribute, chunk_size_bytes);
228 return chunk_size_bytes >> SECTOR_SHIFT;
232 * Retrieve level from md device using sysfs.
234 static int dev_md_level(const char *sysfs_dir, struct device *dev)
236 const char *attribute = "level";
239 if (_md_sysfs_attribute_scanf(sysfs_dir, dev, attribute,
240 "raid%d", &level) != 1)
243 log_very_verbose("Device %s %s is raid%d.",
244 dev_name(dev), attribute, level);
250 * Retrieve raid_disks from md device using sysfs.
252 static int dev_md_raid_disks(const char *sysfs_dir, struct device *dev)
254 const char *attribute = "raid_disks";
257 if (_md_sysfs_attribute_scanf(sysfs_dir, dev, attribute,
258 "%d", &raid_disks) != 1)
261 log_very_verbose("Device %s %s is %d.",
262 dev_name(dev), attribute, raid_disks);
268 * Calculate stripe width of md device using its sysfs files.
270 unsigned long dev_md_stripe_width(const char *sysfs_dir, struct device *dev)
272 unsigned long chunk_size_sectors = 0UL;
273 unsigned long stripe_width_sectors = 0UL;
274 int level, raid_disks, data_disks;
276 chunk_size_sectors = dev_md_chunk_size(sysfs_dir, dev);
277 if (!chunk_size_sectors)
280 level = dev_md_level(sysfs_dir, dev);
284 raid_disks = dev_md_raid_disks(sysfs_dir, dev);
288 /* The raid level governs the number of data disks. */
291 /* striped md does not have any parity disks */
292 data_disks = raid_disks;
296 /* mirrored md effectively has 1 data disk */
301 /* both raid 4 and 5 have a single parity disk */
302 data_disks = raid_disks - 1;
305 /* raid 6 has 2 parity disks */
306 data_disks = raid_disks - 2;
309 log_error("Device %s has an unknown md raid level: %d",
310 dev_name(dev), level);
314 stripe_width_sectors = chunk_size_sectors * data_disks;
316 log_very_verbose("Device %s stripe-width is %lu bytes.",
318 stripe_width_sectors << SECTOR_SHIFT);
320 return stripe_width_sectors;
325 int dev_is_md(struct device *dev __attribute((unused)),
326 uint64_t *sb __attribute((unused)))
331 unsigned long dev_md_stripe_width(const char *sysfs_dir __attribute((unused)),
332 struct device *dev __attribute((unused)))