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