chiark / gitweb /
usb_id: use libudev
[elogind.git] / extras / volume_id / lib / 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 program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <byteswap.h>
31
32 #include "libvolume_id.h"
33 #include "libvolume_id-private.h"
34
35 struct mdp0_super_block {
36         uint32_t        md_magic;
37         uint32_t        major_version;
38         uint32_t        minor_version;
39         uint32_t        patch_version;
40         uint32_t        gvalid_words;
41         uint32_t        set_uuid0;
42         uint32_t        ctime;
43         uint32_t        level;
44         uint32_t        size;
45         uint32_t        nr_disks;
46         uint32_t        raid_disks;
47         uint32_t        md_minor;
48         uint32_t        not_persistent;
49         uint32_t        set_uuid1;
50         uint32_t        set_uuid2;
51         uint32_t        set_uuid3;
52 } PACKED;
53
54 struct mdp1_super_block {
55         uint32_t        magic;
56         uint32_t        major_version;
57         uint32_t        feature_map;
58         uint32_t        pad0;
59         uint8_t         set_uuid[16];
60         uint8_t         set_name[32];
61 } PACKED;
62
63 #define MD_RESERVED_BYTES               0x10000
64 #define MD_SB_MAGIC                     0xa92b4efc
65
66 static int volume_id_probe_linux_raid0(struct volume_id *id, uint64_t off, uint64_t size)
67 {
68         const uint8_t *buf;
69         struct mdp0_super_block *mdp0;
70         union {
71                 uint32_t ints[4];
72                 uint8_t bytes[16];
73         } uuid;
74
75         info("probing at offset 0x%llx, size 0x%llx\n",
76             (unsigned long long) off, (unsigned long long) size);
77         if (size < 0x10000)
78                 return -1;
79
80         buf = volume_id_get_buffer(id, off, 0x800);
81         if (buf == NULL)
82                 return -1;
83         mdp0 = (struct mdp0_super_block *) buf;
84
85         if (le32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
86                 uuid.ints[0] = bswap_32(mdp0->set_uuid0);
87                 if (le32_to_cpu(mdp0->minor_version >= 90)) {
88                         uuid.ints[1] = bswap_32(mdp0->set_uuid1);
89                         uuid.ints[2] = bswap_32(mdp0->set_uuid2);
90                         uuid.ints[3] = bswap_32(mdp0->set_uuid3);
91                 } else {
92                         uuid.ints[1] = 0;
93                         uuid.ints[2] = 0;
94                         uuid.ints[3] = 0;
95                 }
96                 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
97                 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
98                          le32_to_cpu(mdp0->major_version),
99                          le32_to_cpu(mdp0->minor_version),
100                          le32_to_cpu(mdp0->patch_version));
101         } else if (be32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
102                 uuid.ints[0] = mdp0->set_uuid0;
103                 if (be32_to_cpu(mdp0->minor_version >= 90)) {
104                         uuid.ints[1] = mdp0->set_uuid1;
105                         uuid.ints[2] = mdp0->set_uuid2;
106                         uuid.ints[3] = mdp0->set_uuid3;
107                 } else {
108                         uuid.ints[1] = 0;
109                         uuid.ints[2] = 0;
110                         uuid.ints[3] = 0;
111                 }
112                 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
113                 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
114                          be32_to_cpu(mdp0->major_version),
115                          be32_to_cpu(mdp0->minor_version),
116                          be32_to_cpu(mdp0->patch_version));
117         } else
118                 return -1;
119
120         volume_id_set_usage(id, VOLUME_ID_RAID);
121         id->type = "linux_raid_member";
122         return 0;
123 }
124
125 static int volume_id_probe_linux_raid1(struct volume_id *id, uint64_t off, uint64_t size)
126 {
127         const uint8_t *buf;
128         struct mdp1_super_block *mdp1;
129
130         info("probing at offset 0x%llx, size 0x%llx\n",
131             (unsigned long long) off, (unsigned long long) size);
132
133         buf = volume_id_get_buffer(id, off, 0x800);
134         if (buf == NULL)
135                 return -1;
136         mdp1 = (struct mdp1_super_block *) buf;
137
138         if (le32_to_cpu(mdp1->magic) != MD_SB_MAGIC)
139                 return -1;
140
141         if (le32_to_cpu(mdp1->major_version) != 1)
142                 return -1;
143
144         volume_id_set_uuid(id, mdp1->set_uuid, 0, UUID_MD);
145         volume_id_set_label_raw(id, mdp1->set_name, 32);
146         volume_id_set_label_string(id, mdp1->set_name, 32);
147         volume_id_set_usage(id, VOLUME_ID_RAID);
148         id->type = "linux_raid_member";
149         return 0;
150 }
151
152 int volume_id_probe_linux_raid(struct volume_id *id, uint64_t off, uint64_t size)
153 {
154         uint64_t sboff;
155
156         if (size > MD_RESERVED_BYTES) {
157                 /* version 0 at the end of the device */
158                 sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
159                 if (volume_id_probe_linux_raid0(id, off + sboff, size) == 0)
160                         return 0;
161
162                 /* version 1.0 at the end of the device */
163                 sboff = (size & ~(0x1000 - 1)) - 0x2000;
164                 if (volume_id_probe_linux_raid1(id, off + sboff, size) == 0) {
165                         strcpy(id->type_version, "1.0");
166                         return 0;
167                 }
168         }
169
170         /* version 1.1 at the start of the device */
171         if (volume_id_probe_linux_raid1(id, off, size) == 0) {
172                 strcpy(id->type_version, "1.1");
173                 return 0;
174         }
175
176         /* version 1.2 at 4k offset from the start */
177         if (volume_id_probe_linux_raid1(id, off + 0x1000, size) == 0) {
178                 strcpy(id->type_version, "1.2");
179                 return 0;
180         }
181
182         return -1;
183 }