]>
Commit | Line | Data |
---|---|---|
0592faeb 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 | * '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 | ||
0592faeb N |
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 | } | |
0592faeb N |
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; | |
41b06495 | 74 | unsigned int sector_size; |
0592faeb N |
75 | |
76 | free_gpt(st); | |
77 | ||
a7322ae1 | 78 | if (posix_memalign((void**)&super, 4096, 32*512) != 0) { |
1ade5cc1 | 79 | pr_err("could not allocate superblock\n"); |
0592faeb N |
80 | return 1; |
81 | } | |
82 | ||
41b06495 MD |
83 | if (!get_dev_sector_size(fd, devname, §or_size)) { |
84 | free(super); | |
85 | return 1; | |
86 | } | |
87 | ||
0592faeb N |
88 | lseek(fd, 0, 0); |
89 | if (read(fd, super, sizeof(*super)) != sizeof(*super)) { | |
90 | no_read: | |
91 | if (devname) | |
e7b84f9d | 92 | pr_err("Cannot read partition table on %s\n", |
0592faeb N |
93 | devname); |
94 | free(super); | |
95 | return 1; | |
96 | } | |
1011e834 | 97 | |
0592faeb N |
98 | if (super->magic != MBR_SIGNATURE_MAGIC || |
99 | super->parts[0].part_type != MBR_GPT_PARTITION_TYPE) { | |
100 | not_found: | |
101 | if (devname) | |
e7b84f9d | 102 | pr_err("No partition table found on %s\n", |
0592faeb N |
103 | devname); |
104 | free(super); | |
105 | return 1; | |
106 | } | |
41b06495 MD |
107 | /* Set offset to second block (GPT header) */ |
108 | lseek(fd, sector_size, SEEK_SET); | |
0592faeb N |
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; | |
41b06495 MD |
120 | /* Set offset to third block (GPT entries) */ |
121 | lseek(fd, sector_size*2, SEEK_SET); | |
0592faeb N |
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 | ||
a5d85af7 | 161 | static void getinfo_gpt(struct supertype *st, struct mdinfo *info, char *map) |
0592faeb N |
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 | { | |
503975b9 | 183 | struct supertype *st = xmalloc(sizeof(*st)); |
0592faeb N |
184 | |
185 | if (!st) | |
186 | return st; | |
c9aaf5ef JS |
187 | if (strcmp(arg, "gpt") != 0) { |
188 | free(st); | |
0592faeb | 189 | return NULL; |
c9aaf5ef | 190 | } |
0592faeb N |
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 | ||
0592faeb N |
200 | static int validate_geometry(struct supertype *st, int level, |
201 | int layout, int raiddisks, | |
c21e737b | 202 | int *chunk, unsigned long long size, |
af4348dd | 203 | unsigned long long data_offset, |
0592faeb | 204 | char *subdev, unsigned long long *freesize, |
5308f117 | 205 | int consistency_policy, int verbose) |
0592faeb | 206 | { |
e7b84f9d | 207 | pr_err("gpt metadata cannot be used this way\n"); |
0592faeb N |
208 | return 0; |
209 | } | |
0592faeb N |
210 | |
211 | struct superswitch gpt = { | |
0592faeb | 212 | .examine_super = examine_gpt, |
0592faeb N |
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 | }; |