chiark / gitweb /
598002ec4f445290d0106eb63a397bdddfe3f2f4
[elogind.git] / extras / volume_id / volume_id / linux_raid.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <asm/types.h>
36
37 #include "volume_id.h"
38 #include "logging.h"
39 #include "util.h"
40 #include "linux_raid.h"
41
42 struct mdp_super_block {
43         __u32   md_magic;
44         __u32   major_version;
45         __u32   minor_version;
46         __u32   patch_version;
47         __u32   gvalid_words;
48         __u32   set_uuid0;
49         __u32   ctime;
50         __u32   level;
51         __u32   size;
52         __u32   nr_disks;
53         __u32   raid_disks;
54         __u32   md_minor;
55         __u32   not_persistent;
56         __u32   set_uuid1;
57         __u32   set_uuid2;
58         __u32   set_uuid3;
59 } __attribute__((packed)) *mdp;
60
61 #define MD_RESERVED_BYTES               0x10000
62 #define MD_MAGIC                        0xa92b4efc
63
64 int volume_id_probe_linux_raid(struct volume_id *id, __u64 off, __u64 size)
65 {
66         const __u8 *buf;
67         __u64 sboff;
68         __u8 uuid[16];
69
70         dbg("probing at offset 0x%llx, size 0x%llx",
71             (unsigned long long) off, (unsigned long long) size);
72
73         if (size < 0x10000)
74                 return -1;
75
76         sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
77         buf = volume_id_get_buffer(id, off + sboff, 0x800);
78         if (buf == NULL)
79                 return -1;
80
81         mdp = (struct mdp_super_block *) buf;
82
83         if (le32_to_cpu(mdp->md_magic) != MD_MAGIC)
84                 return -1;
85
86         memcpy(uuid, &mdp->set_uuid0, 4);
87         memcpy(&uuid[4], &mdp->set_uuid1, 12);
88         volume_id_set_uuid(id, uuid, UUID_DCE);
89
90         snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
91                  le32_to_cpu(mdp->major_version),
92                  le32_to_cpu(mdp->minor_version),
93                  le32_to_cpu(mdp->patch_version));
94
95         dbg("found raid signature");
96         volume_id_set_usage(id, VOLUME_ID_RAID);
97         id->type = "linux_raid_member";
98
99         return 0;
100 }