]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-gpt.c
6a2f749e2e6d4dc523d0f042ce5deb0985674546
[thirdparty/mdadm.git] / super-gpt.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 * 'gpt' is a pseudo metadata type for devices which have a
28 * GPT partition table.
29 *
30 * Obviously arrays cannot be created or assembled for this type.
31 * It is used to allow a new bare device to have an partition table
32 * added so the member partitions can then be included in other
33 * arrays as relevant.
34 *
35 * The meaning operations are:
36 * examine_super, but not brief_examine_super or export_examine
37 * load_super
38 * store_super
39 */
40
41 #include "mdadm.h"
42 #include "part.h"
43
44 static void free_gpt(struct supertype *st)
45 {
46 free(st->sb);
47 st->sb = NULL;
48 }
49
50 #ifndef MDASSEMBLE
51 static void examine_gpt(struct supertype *st, char *homehost)
52 {
53 struct GPT *gpt = st->sb + 512;
54 struct GPT_part_entry *gpe = st->sb + 1024;
55 unsigned int i;
56
57 printf(" GPT Magic : %llx\n", (unsigned long long)__le64_to_cpu(gpt->magic));
58 printf(" GPT Revision : %ld\n", (long)__le32_to_cpu(gpt->revision));
59 for (i = 0; i < __le32_to_cpu(gpt->part_cnt); i++) {
60 printf(" Partition[%02d] : %12llu sectors at %12llu\n",
61 i,
62 (unsigned long long)__le64_to_cpu(gpe[i].starting_lba),
63 (unsigned long long)__le64_to_cpu(gpe[i].ending_lba)-
64 (unsigned long long)__le64_to_cpu(gpe[i].starting_lba)
65 +1
66 );
67 }
68 }
69 #endif /* MDASSEMBLE */
70
71 static int load_gpt(struct supertype *st, int fd, char *devname)
72 {
73 struct MBR *super;
74 struct GPT *gpt_head;
75 int to_read;
76
77 free_gpt(st);
78
79 if (posix_memalign((void**)&super, 4096, 32*512) != 0) {
80 pr_err("%s could not allocate superblock\n",
81 __func__);
82 return 1;
83 }
84
85 lseek(fd, 0, 0);
86 if (read(fd, super, sizeof(*super)) != sizeof(*super)) {
87 no_read:
88 if (devname)
89 pr_err("Cannot read partition table on %s\n",
90 devname);
91 free(super);
92 return 1;
93 }
94
95 if (super->magic != MBR_SIGNATURE_MAGIC ||
96 super->parts[0].part_type != MBR_GPT_PARTITION_TYPE) {
97 not_found:
98 if (devname)
99 pr_err("No partition table found on %s\n",
100 devname);
101 free(super);
102 return 1;
103 }
104 /* Seem to have GPT, load the header */
105 gpt_head = (struct GPT*)(super+1);
106 if (read(fd, gpt_head, sizeof(*gpt_head)) != sizeof(*gpt_head))
107 goto no_read;
108 if (gpt_head->magic != GPT_SIGNATURE_MAGIC)
109 goto not_found;
110 if (__le32_to_cpu(gpt_head->part_cnt) >= 128)
111 goto not_found;
112
113 to_read = __le32_to_cpu(gpt_head->part_cnt) * sizeof(struct GPT_part_entry);
114 to_read = ((to_read+511)/512) * 512;
115 if (read(fd, gpt_head+1, to_read) != to_read)
116 goto no_read;
117
118 st->sb = super;
119
120 if (st->ss == NULL) {
121 st->ss = &gpt;
122 st->minor_version = 0;
123 st->max_devs = 1;
124 st->info = NULL;
125 }
126 return 0;
127 }
128
129 static int store_gpt(struct supertype *st, int fd)
130 {
131 /* FIXME should I save the boot loader */
132 /* need to write two copies! */
133 /* FIXME allow for blocks != 512 bytes
134 *etc
135 */
136 struct MBR *super = st->sb;
137 struct GPT *gpt;
138 int to_write;
139
140 gpt = (struct GPT*)(super+1);
141
142 to_write = __le32_to_cpu(gpt->part_cnt) * sizeof(struct GPT_part_entry);
143 to_write = ((to_write+511)/512) * 512;
144
145 lseek(fd, 0, 0);
146 if (write(fd, st->sb, to_write) != to_write)
147 return 4;
148
149 fsync(fd);
150 ioctl(fd, BLKRRPART, 0);
151 return 0;
152 }
153
154 static void getinfo_gpt(struct supertype *st, struct mdinfo *info, char *map)
155 {
156 struct GPT *gpt = st->sb + 512;
157 struct GPT_part_entry *gpe = st->sb + 1024;
158 unsigned int i;
159
160 memset(&info->array, 0, sizeof(info->array));
161 memset(&info->disk, 0, sizeof(info->disk));
162 strcpy(info->text_version, "gpt");
163 strcpy(info->name, "gpt");
164 info->component_size = 0;
165
166 for (i = 0; i < __le32_to_cpu(gpt->part_cnt); i++) {
167 unsigned long long last =
168 (unsigned long long)__le64_to_cpu(gpe[i].ending_lba);
169 if (last > info->component_size)
170 info->component_size = last;
171 }
172 }
173
174 static struct supertype *match_metadata_desc(char *arg)
175 {
176 struct supertype *st = xmalloc(sizeof(*st));
177
178 if (!st)
179 return st;
180 if (strcmp(arg, "gpt") != 0) {
181 free(st);
182 return NULL;
183 }
184
185 st->ss = &gpt;
186 st->info = NULL;
187 st->minor_version = 0;
188 st->max_devs = 1;
189 st->sb = NULL;
190 return st;
191 }
192
193 #ifndef MDASSEMBLE
194 static int validate_geometry(struct supertype *st, int level,
195 int layout, int raiddisks,
196 int *chunk, unsigned long long size,
197 unsigned long long data_offset,
198 char *subdev, unsigned long long *freesize,
199 int verbose)
200 {
201 pr_err("gpt metadata cannot be used this way\n");
202 return 0;
203 }
204 #endif
205
206 struct superswitch gpt = {
207 #ifndef MDASSEMBLE
208 .examine_super = examine_gpt,
209 .validate_geometry = validate_geometry,
210 #endif
211 .match_metadata_desc = match_metadata_desc,
212 .load_super = load_gpt,
213 .store_super = store_gpt,
214 .getinfo_super = getinfo_gpt,
215 .free_super = free_gpt,
216 .name = "gpt",
217 };