]> git.neil.brown.name Git - mdadm.git/blob - super-mbr.c
Fix some issues found by clang
[mdadm.git] / super-mbr.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2010 Neil Brown <neilb@suse.de>
5  *
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *    Author: Neil Brown
22  *    Email: <neil@brown.name>
23  *
24  */
25
26 /*
27  * 'mbr' is a pseudo metadata type for devices which have a
28  * partition table in the Master Boot Record (mbr) also known
29  * as a dos partition table.
30  *
31  * Obviously arrays cannot be created or assembled for this type.
32  * It is used to allow a new bare device to have an partition table
33  * added so the member partitions can then be included in other
34  * arrays as relevant.
35  *
36  * The meaning operations are:
37  * examine_super, but not brief_examine_super or export_examine
38  * load_super
39  * store_super
40  */
41
42 #include "mdadm.h"
43 #include "part.h"
44
45 static void free_mbr(struct supertype *st)
46 {
47         free(st->sb);
48         st->sb = NULL;
49 }
50
51 #ifndef MDASSEMBLE
52
53 static void examine_mbr(struct supertype *st, char *homehost)
54 {
55         struct MBR *sb = st->sb;
56         int i;
57
58         printf("   MBR Magic : %04x\n", sb->magic);
59         for (i = 0; i < MBR_PARTITIONS; i++)
60                 if (sb->parts[i].blocks_num)
61                         printf("Partition[%d] : %12lu sectors at %12lu (type %02x)\n",
62                                i,
63                                (unsigned long)__le32_to_cpu(sb->parts[i].blocks_num),
64                                (unsigned long)__le32_to_cpu(sb->parts[i].first_sect_lba),
65                                sb->parts[i].part_type);
66
67 }
68
69 #endif /*MDASSEMBLE */
70
71 static int load_super_mbr(struct supertype *st, int fd, char *devname)
72 {
73         /* try to read an mbr
74          * Return
75          *  0 on success
76          *  1 cannot get record
77          *  2 record is meaningless
78          */
79         struct MBR *super;
80
81         free_mbr(st);
82
83         if (posix_memalign((void**)&super, 512, 512) != 0) {
84                 pr_err("could not allocate superblock\n");
85                 return 1;
86         }
87
88         lseek(fd, 0, 0);
89         if (read(fd, super, sizeof(*super)) != sizeof(*super)) {
90                 if (devname)
91                         pr_err("Cannot read partition table on %s\n",
92                                 devname);
93                 free(super);
94                 return 1;
95         }
96
97         if (super->magic != MBR_SIGNATURE_MAGIC) {
98                 if (devname)
99                         pr_err("No partition table found on %s\n",
100                                 devname);
101                 free(super);
102                 return 1;
103         }
104
105         st->sb = super;
106
107         if (st->ss == NULL) {
108                 st->ss = &mbr;
109                 st->minor_version = 0;
110                 st->max_devs = 1;
111                 st->info = NULL;
112         }
113         return 0;
114 }
115
116 static int store_mbr(struct supertype *st, int fd)
117 {
118         struct MBR *old, *super;
119
120         if (posix_memalign((void**)&old, 512, 512) != 0) {
121                 pr_err("could not allocate superblock\n");
122                 return 1;
123         }
124
125         lseek(fd, 0, 0);
126         if (read(fd, old, sizeof(*old)) != sizeof(*old)) {
127                 free(old);
128                 return 1;
129         }
130
131         super = st->sb;
132         memcpy(super->pad, old->pad, sizeof(super->pad));
133         free(old);
134         lseek(fd, 0, 0);
135         if (write(fd, super, sizeof(*super)) != sizeof(*super))
136                 return 4;
137         fsync(fd);
138         ioctl(fd, BLKRRPART, 0);
139         return 0;
140 }
141
142 static void getinfo_mbr(struct supertype *st, struct mdinfo *info, char *map)
143 {
144         struct MBR *sb = st->sb;
145         int i;
146
147         memset(&info->array, 0, sizeof(info->array));
148         memset(&info->disk, 0, sizeof(info->disk));
149         strcpy(info->text_version, "mbr");
150         strcpy(info->name, "mbr");
151         info->component_size = 0;
152
153         for (i = 0; i < MBR_PARTITIONS ; i++)
154                 if (sb->parts[i].blocks_num) {
155                         unsigned long last =
156                                 (unsigned long)__le32_to_cpu(sb->parts[i].blocks_num)
157                                 + (unsigned long)__le32_to_cpu(sb->parts[i].first_sect_lba);
158                         if (last > info->component_size)
159                                 info->component_size = last;
160                 }
161
162 }
163
164 static struct supertype *match_metadata_desc(char *arg)
165 {
166         struct supertype *st;
167
168         if (strcmp(arg, "mbr") != 0)
169                 return NULL;
170
171         st = xmalloc(sizeof(*st));
172         st->ss = &mbr;
173         st->info = NULL;
174         st->minor_version = 0;
175         st->max_devs = 1;
176         st->sb = NULL;
177         return st;
178 }
179
180 #ifndef MDASSEMBLE
181 static int validate_geometry(struct supertype *st, int level,
182                              int layout, int raiddisks,
183                              int *chunk, unsigned long long size,
184                              unsigned long long data_offset,
185                              char *subdev, unsigned long long *freesize,
186                              int verbose)
187 {
188         pr_err("mbr metadata cannot be used this way\n");
189         return 0;
190 }
191 #endif
192
193 struct superswitch mbr = {
194 #ifndef MDASSEMBLE
195         .examine_super = examine_mbr,
196         .validate_geometry = validate_geometry,
197 #endif
198         .match_metadata_desc = match_metadata_desc,
199         .load_super = load_super_mbr,
200         .store_super = store_mbr,
201         .getinfo_super = getinfo_mbr,
202         .free_super = free_mbr,
203         .name = "mbr",
204 };