]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-mbr.c
mdadm: load default sysfs attributes after assemblation
[thirdparty/mdadm.git] / super-mbr.c
CommitLineData
0f22b998
N
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
45static void free_mbr(struct supertype *st)
46{
47 free(st->sb);
48 st->sb = NULL;
49}
50
0f22b998
N
51static void examine_mbr(struct supertype *st, char *homehost)
52{
53 struct MBR *sb = st->sb;
54 int i;
55
56 printf(" MBR Magic : %04x\n", sb->magic);
57 for (i = 0; i < MBR_PARTITIONS; i++)
8e2bca51
JC
58 /*
59 * Have to make every access through sb rather than using a
60 * pointer to the partition table (or an entry), since the
61 * entries are not properly aligned.
62 */
0f22b998
N
63 if (sb->parts[i].blocks_num)
64 printf("Partition[%d] : %12lu sectors at %12lu (type %02x)\n",
65 i,
66 (unsigned long)__le32_to_cpu(sb->parts[i].blocks_num),
67 (unsigned long)__le32_to_cpu(sb->parts[i].first_sect_lba),
68 sb->parts[i].part_type);
69
70}
71
0f22b998
N
72static int load_super_mbr(struct supertype *st, int fd, char *devname)
73{
74 /* try to read an mbr
75 * Return
76 * 0 on success
77 * 1 cannot get record
78 * 2 record is meaningless
79 */
80 struct MBR *super;
81
82 free_mbr(st);
83
0f22b998 84 if (posix_memalign((void**)&super, 512, 512) != 0) {
1ade5cc1 85 pr_err("could not allocate superblock\n");
0f22b998
N
86 return 1;
87 }
88
0f22b998
N
89 lseek(fd, 0, 0);
90 if (read(fd, super, sizeof(*super)) != sizeof(*super)) {
91 if (devname)
e7b84f9d 92 pr_err("Cannot read partition table on %s\n",
0f22b998
N
93 devname);
94 free(super);
95 return 1;
96 }
1011e834 97
0f22b998
N
98 if (super->magic != MBR_SIGNATURE_MAGIC) {
99 if (devname)
e7b84f9d 100 pr_err("No partition table found on %s\n",
0f22b998
N
101 devname);
102 free(super);
103 return 1;
104 }
105
106 st->sb = super;
107
108 if (st->ss == NULL) {
109 st->ss = &mbr;
110 st->minor_version = 0;
111 st->max_devs = 1;
112 st->info = NULL;
113 }
114 return 0;
115}
116
117static int store_mbr(struct supertype *st, int fd)
118{
119 struct MBR *old, *super;
120
121 if (posix_memalign((void**)&old, 512, 512) != 0) {
1ade5cc1 122 pr_err("could not allocate superblock\n");
0f22b998
N
123 return 1;
124 }
125
0f22b998
N
126 lseek(fd, 0, 0);
127 if (read(fd, old, sizeof(*old)) != sizeof(*old)) {
128 free(old);
129 return 1;
130 }
131
132 super = st->sb;
133 memcpy(super->pad, old->pad, sizeof(super->pad));
134 free(old);
135 lseek(fd, 0, 0);
136 if (write(fd, super, sizeof(*super)) != sizeof(*super))
137 return 4;
138 fsync(fd);
139 ioctl(fd, BLKRRPART, 0);
140 return 0;
141}
142
a5d85af7 143static void getinfo_mbr(struct supertype *st, struct mdinfo *info, char *map)
0f22b998
N
144{
145 struct MBR *sb = st->sb;
146 int i;
147
148 memset(&info->array, 0, sizeof(info->array));
149 memset(&info->disk, 0, sizeof(info->disk));
150 strcpy(info->text_version, "mbr");
151 strcpy(info->name, "mbr");
152 info->component_size = 0;
153
154 for (i = 0; i < MBR_PARTITIONS ; i++)
8e2bca51
JC
155 /*
156 * Have to make every access through sb rather than using a
157 * pointer to the partition table (or an entry), since the
158 * entries are not properly aligned.
159 */
0f22b998 160 if (sb->parts[i].blocks_num) {
1011e834 161 unsigned long last =
0f22b998
N
162 (unsigned long)__le32_to_cpu(sb->parts[i].blocks_num)
163 + (unsigned long)__le32_to_cpu(sb->parts[i].first_sect_lba);
164 if (last > info->component_size)
165 info->component_size = last;
166 }
167
168}
169
170static struct supertype *match_metadata_desc(char *arg)
171{
62f5838f 172 struct supertype *st;
0f22b998 173
0f22b998
N
174 if (strcmp(arg, "mbr") != 0)
175 return NULL;
176
503975b9 177 st = xmalloc(sizeof(*st));
0f22b998
N
178 st->ss = &mbr;
179 st->info = NULL;
180 st->minor_version = 0;
181 st->max_devs = 1;
182 st->sb = NULL;
183 return st;
184}
185
0f22b998
N
186static int validate_geometry(struct supertype *st, int level,
187 int layout, int raiddisks,
c21e737b 188 int *chunk, unsigned long long size,
af4348dd 189 unsigned long long data_offset,
0f22b998 190 char *subdev, unsigned long long *freesize,
5308f117 191 int consistency_policy, int verbose)
0f22b998 192{
e7b84f9d 193 pr_err("mbr metadata cannot be used this way\n");
0f22b998
N
194 return 0;
195}
0f22b998
N
196
197struct superswitch mbr = {
0f22b998 198 .examine_super = examine_mbr,
0f22b998
N
199 .validate_geometry = validate_geometry,
200 .match_metadata_desc = match_metadata_desc,
201 .load_super = load_super_mbr,
202 .store_super = store_mbr,
203 .getinfo_super = getinfo_mbr,
204 .free_super = free_mbr,
205 .name = "mbr",
206};