Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / lib / device / dev-md.c
blob4a7f9e8487b85840c175ddabba3800656cb2353a
1 /* $NetBSD$ */
3 /*
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
18 #include "lib.h"
19 #include "metadata.h"
20 #include "xlate.h"
21 #include "filter.h"
23 #ifdef linux
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)
35 uint32_t md_magic;
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)))
41 return 1;
43 return 0;
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.
54 typedef enum {
55 MD_MINOR_VERSION_MIN,
56 MD_MINOR_V0 = MD_MINOR_VERSION_MIN,
57 MD_MINOR_V1,
58 MD_MINOR_V2,
59 MD_MINOR_VERSION_MAX = MD_MINOR_V2
60 } md_minor_version_t;
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) {
67 case MD_MINOR_V0:
68 sb_offset = (size - 8 * 2) & ~(4 * 2 - 1ULL);
69 break;
70 case MD_MINOR_V1:
71 sb_offset = 0;
72 break;
73 case MD_MINOR_V2:
74 sb_offset = 4 * 2;
75 break;
77 sb_offset <<= SECTOR_SHIFT;
79 return sb_offset;
83 * Returns -1 on error
85 int dev_is_md(struct device *dev, uint64_t *sb)
87 int ret = 1;
88 md_minor_version_t minor;
89 uint64_t size, sb_offset;
91 if (!dev_get_size(dev, &size)) {
92 stack;
93 return -1;
96 if (size < MD_RESERVED_SECTORS * 2)
97 return 0;
99 if (!dev_open(dev)) {
100 stack;
101 return -1;
104 /* Check if it is an md component device. */
105 /* Version 0.90.0 */
106 sb_offset = MD_NEW_SIZE_SECTORS(size) << SECTOR_SHIFT;
107 if (_dev_has_md_magic(dev, sb_offset))
108 goto out;
110 minor = MD_MINOR_VERSION_MIN;
111 /* Version 1, try v1.0 -> v1.2 */
112 do {
113 sb_offset = _v1_sb_offset(size, minor);
114 if (_dev_has_md_magic(dev, sb_offset))
115 goto out;
116 } while (++minor <= MD_MINOR_VERSION_MAX);
118 ret = 0;
120 out:
121 if (!dev_close(dev))
122 stack;
124 if (ret && sb)
125 *sb = sb_offset;
127 return ret;
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)
135 struct stat info;
136 dev_t dev = blkdev->dev;
137 int ret = -1;
139 if (!sysfs_dir || !*sysfs_dir)
140 return ret;
142 if (MAJOR(dev) == blkext_major()) {
143 /* lookup parent MD device from blkext partition */
144 if (!get_primary_dev(sysfs_dir, blkdev, &dev))
145 return ret;
148 if (MAJOR(dev) != md_major())
149 return ret;
151 ret = dm_snprintf(path, size, "%s/dev/block/%d:%d/md/%s", sysfs_dir,
152 (int)MAJOR(dev), (int)MINOR(dev), attribute);
153 if (ret < 0) {
154 log_error("dm_snprintf md %s failed", attribute);
155 return ret;
158 if (stat(path, &info) == -1) {
159 if (errno != ENOENT) {
160 log_sys_error("stat", path);
161 return ret;
163 /* old sysfs structure */
164 ret = dm_snprintf(path, size, "%s/block/md%d/md/%s",
165 sysfs_dir, (int)MINOR(dev), attribute);
166 if (ret < 0) {
167 log_error("dm_snprintf old md %s failed", attribute);
168 return ret;
172 return ret;
175 static int _md_sysfs_attribute_scanf(const char *sysfs_dir,
176 struct device *dev,
177 const char *attribute_name,
178 const char *attribute_fmt,
179 void *attribute_value)
181 char path[PATH_MAX+1], buffer[64];
182 FILE *fp;
183 int ret = 0;
185 if (_md_sysfs_attribute_snprintf(path, PATH_MAX, sysfs_dir,
186 dev, attribute_name) < 0)
187 return ret;
189 if (!(fp = fopen(path, "r"))) {
190 log_sys_error("fopen", path);
191 return ret;
194 if (!fgets(buffer, sizeof(buffer), fp)) {
195 log_sys_error("fgets", path);
196 goto out;
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);
202 goto out;
205 out:
206 if (fclose(fp))
207 log_sys_error("fclose", path);
209 return ret;
213 * Retrieve chunk size from md device using sysfs.
215 static unsigned long dev_md_chunk_size(const char *sysfs_dir,
216 struct device *dev)
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)
223 return 0;
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";
237 int level = -1;
239 if (_md_sysfs_attribute_scanf(sysfs_dir, dev, attribute,
240 "raid%d", &level) != 1)
241 return -1;
243 log_very_verbose("Device %s %s is raid%d.",
244 dev_name(dev), attribute, level);
246 return 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";
255 int raid_disks = 0;
257 if (_md_sysfs_attribute_scanf(sysfs_dir, dev, attribute,
258 "%d", &raid_disks) != 1)
259 return 0;
261 log_very_verbose("Device %s %s is %d.",
262 dev_name(dev), attribute, raid_disks);
264 return 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)
278 return 0;
280 level = dev_md_level(sysfs_dir, dev);
281 if (level < 0)
282 return 0;
284 raid_disks = dev_md_raid_disks(sysfs_dir, dev);
285 if (!raid_disks)
286 return 0;
288 /* The raid level governs the number of data disks. */
289 switch (level) {
290 case 0:
291 /* striped md does not have any parity disks */
292 data_disks = raid_disks;
293 break;
294 case 1:
295 case 10:
296 /* mirrored md effectively has 1 data disk */
297 data_disks = 1;
298 break;
299 case 4:
300 case 5:
301 /* both raid 4 and 5 have a single parity disk */
302 data_disks = raid_disks - 1;
303 break;
304 case 6:
305 /* raid 6 has 2 parity disks */
306 data_disks = raid_disks - 2;
307 break;
308 default:
309 log_error("Device %s has an unknown md raid level: %d",
310 dev_name(dev), level);
311 return 0;
314 stripe_width_sectors = chunk_size_sectors * data_disks;
316 log_very_verbose("Device %s stripe-width is %lu bytes.",
317 dev_name(dev),
318 stripe_width_sectors << SECTOR_SHIFT);
320 return stripe_width_sectors;
323 #else
325 int dev_is_md(struct device *dev __attribute((unused)),
326 uint64_t *sb __attribute((unused)))
328 return 0;
331 unsigned long dev_md_stripe_width(const char *sysfs_dir __attribute((unused)),
332 struct device *dev __attribute((unused)))
334 return 0UL;
337 #endif