chiark / gitweb /
man: fix typos
[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%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
76         if (size < 0x10000)
77                 return -1;
78
79         buf = volume_id_get_buffer(id, off, 0x800);
80         if (buf == NULL)
81                 return -1;
82         mdp0 = (struct mdp0_super_block *) buf;
83
84         if (le32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
85                 uuid.ints[0] = bswap_32(mdp0->set_uuid0);
86                 if (le32_to_cpu(mdp0->minor_version >= 90)) {
87                         uuid.ints[1] = bswap_32(mdp0->set_uuid1);
88                         uuid.ints[2] = bswap_32(mdp0->set_uuid2);
89                         uuid.ints[3] = bswap_32(mdp0->set_uuid3);
90                 } else {
91                         uuid.ints[1] = 0;
92                         uuid.ints[2] = 0;
93                         uuid.ints[3] = 0;
94                 }
95                 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
96                 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
97                          le32_to_cpu(mdp0->major_version),
98                          le32_to_cpu(mdp0->minor_version),
99                          le32_to_cpu(mdp0->patch_version));
100         } else if (be32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
101                 uuid.ints[0] = mdp0->set_uuid0;
102                 if (be32_to_cpu(mdp0->minor_version >= 90)) {
103                         uuid.ints[1] = mdp0->set_uuid1;
104                         uuid.ints[2] = mdp0->set_uuid2;
105                         uuid.ints[3] = mdp0->set_uuid3;
106                 } else {
107                         uuid.ints[1] = 0;
108                         uuid.ints[2] = 0;
109                         uuid.ints[3] = 0;
110                 }
111                 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
112                 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
113                          be32_to_cpu(mdp0->major_version),
114                          be32_to_cpu(mdp0->minor_version),
115                          be32_to_cpu(mdp0->patch_version));
116         } else
117                 return -1;
118
119         volume_id_set_usage(id, VOLUME_ID_RAID);
120         id->type = "linux_raid_member";
121         return 0;
122 }
123
124 static int volume_id_probe_linux_raid1(struct volume_id *id, uint64_t off, uint64_t size)
125 {
126         const uint8_t *buf;
127         struct mdp1_super_block *mdp1;
128
129         info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
130
131         buf = volume_id_get_buffer(id, off, 0x800);
132         if (buf == NULL)
133                 return -1;
134         mdp1 = (struct mdp1_super_block *) buf;
135
136         if (le32_to_cpu(mdp1->magic) != MD_SB_MAGIC)
137                 return -1;
138
139         if (le32_to_cpu(mdp1->major_version) != 1)
140                 return -1;
141
142         volume_id_set_uuid(id, mdp1->set_uuid, 0, UUID_MD);
143         volume_id_set_label_raw(id, mdp1->set_name, 32);
144         volume_id_set_label_string(id, mdp1->set_name, 32);
145         volume_id_set_usage(id, VOLUME_ID_RAID);
146         id->type = "linux_raid_member";
147         return 0;
148 }
149
150 int volume_id_probe_linux_raid(struct volume_id *id, uint64_t off, uint64_t size)
151 {
152         uint64_t sboff;
153
154         if (size > MD_RESERVED_BYTES) {
155                 /* version 0 at the end of the device */
156                 sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
157                 if (volume_id_probe_linux_raid0(id, off + sboff, size) == 0)
158                         return 0;
159
160                 /* version 1.0 at the end of the device */
161                 sboff = (size & ~(0x1000 - 1)) - 0x2000;
162                 if (volume_id_probe_linux_raid1(id, off + sboff, size) == 0) {
163                         strcpy(id->type_version, "1.0");
164                         return 0;
165                 }
166         }
167
168         /* version 1.1 at the start of the device */
169         if (volume_id_probe_linux_raid1(id, off, size) == 0) {
170                 strcpy(id->type_version, "1.1");
171                 return 0;
172         }
173
174         /* version 1.2 at 4k offset from the start */
175         if (volume_id_probe_linux_raid1(id, off + 0x1000, size) == 0) {
176                 strcpy(id->type_version, "1.2");
177                 return 0;
178         }
179
180         return -1;
181 }