chiark / gitweb /
volume_id: replace __packed__ by PACKED macro
[elogind.git] / extras / volume_id / libvolume_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 program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "logging.h"
28 #include "util.h"
29
30 struct mdp_super_block {
31         uint32_t        md_magic;
32         uint32_t        major_version;
33         uint32_t        minor_version;
34         uint32_t        patch_version;
35         uint32_t        gvalid_words;
36         uint32_t        set_uuid0;
37         uint32_t        ctime;
38         uint32_t        level;
39         uint32_t        size;
40         uint32_t        nr_disks;
41         uint32_t        raid_disks;
42         uint32_t        md_minor;
43         uint32_t        not_persistent;
44         uint32_t        set_uuid1;
45         uint32_t        set_uuid2;
46         uint32_t        set_uuid3;
47 } PACKED *mdp;
48
49 #define MD_RESERVED_BYTES               0x10000
50 #define MD_MAGIC                        0xa92b4efc
51
52 int volume_id_probe_linux_raid(struct volume_id *id, uint64_t off, uint64_t size)
53 {
54         const uint8_t *buf;
55         uint64_t sboff;
56         uint8_t uuid[16];
57
58         dbg("probing at offset 0x%llx, size 0x%llx",
59             (unsigned long long) off, (unsigned long long) size);
60
61         if (size < 0x10000)
62                 return -1;
63
64         sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
65         buf = volume_id_get_buffer(id, off + sboff, 0x800);
66         if (buf == NULL)
67                 return -1;
68
69         mdp = (struct mdp_super_block *) buf;
70
71         if (le32_to_cpu(mdp->md_magic) != MD_MAGIC)
72                 return -1;
73
74         memcpy(uuid, &mdp->set_uuid0, 4);
75         memcpy(&uuid[4], &mdp->set_uuid1, 12);
76         volume_id_set_uuid(id, uuid, UUID_DCE);
77
78         snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
79                  le32_to_cpu(mdp->major_version),
80                  le32_to_cpu(mdp->minor_version),
81                  le32_to_cpu(mdp->patch_version));
82
83         dbg("found raid signature");
84         volume_id_set_usage(id, VOLUME_ID_RAID);
85         id->type = "linux_raid_member";
86
87         return 0;
88 }