]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - super-gpt.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2010 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neil@brown.name>
27 * 'gpt' is a pseudo metadata type for devices which have a
28 * GPT partition table.
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
35 * The meaning operations are:
36 * examine_super, but not brief_examine_super or export_examine
44 static void free_gpt(struct supertype
*st
)
51 static void examine_gpt(struct supertype
*st
, char *homehost
)
53 struct GPT
*gpt
= st
->sb
+ 512;
54 struct GPT_part_entry
*gpe
= st
->sb
+ 1024;
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",
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
)
69 #endif /* MDASSEMBLE */
71 static int load_gpt(struct supertype
*st
, int fd
, char *devname
)
79 if (posix_memalign((void**)&super
, 512, 32*512) != 0) {
80 fprintf(stderr
, Name
": %s could not allocate superblock\n",
85 ioctl(fd
, BLKFLSBUF
, 0); /* make sure we read current data */
88 if (read(fd
, super
, sizeof(*super
)) != sizeof(*super
)) {
91 fprintf(stderr
, Name
": Cannot read partition table on %s\n",
97 if (super
->magic
!= MBR_SIGNATURE_MAGIC
||
98 super
->parts
[0].part_type
!= MBR_GPT_PARTITION_TYPE
) {
101 fprintf(stderr
, Name
": No partition table found on %s\n",
106 /* Seem to have GPT, load the header */
107 gpt_head
= (struct GPT
*)(super
+1);
108 if (read(fd
, gpt_head
, sizeof(*gpt_head
)) != sizeof(*gpt_head
))
110 if (gpt_head
->magic
!= GPT_SIGNATURE_MAGIC
)
112 if (__le32_to_cpu(gpt_head
->part_cnt
) >= 128)
115 to_read
= __le32_to_cpu(gpt_head
->part_cnt
) * sizeof(struct GPT_part_entry
);
116 to_read
= ((to_read
+511)/512) * 512;
117 if (read(fd
, gpt_head
+1, to_read
) != to_read
)
122 if (st
->ss
== NULL
) {
124 st
->minor_version
= 0;
131 static int store_gpt(struct supertype
*st
, int fd
)
133 /* FIXME should I save the boot loader */
134 /* need to write two copies! */
135 /* FIXME allow for blocks != 512 bytes
138 struct MBR
*super
= st
->sb
;
142 gpt
= (struct GPT
*)(super
+1);
144 to_write
= __le32_to_cpu(gpt
->part_cnt
) * sizeof(struct GPT_part_entry
);
145 to_write
= ((to_write
+511)/512) * 512;
148 if (write(fd
, st
->sb
, to_write
) != to_write
)
152 ioctl(fd
, BLKRRPART
, 0);
156 static void getinfo_gpt(struct supertype
*st
, struct mdinfo
*info
, char *map
)
158 struct GPT
*gpt
= st
->sb
+ 512;
159 struct GPT_part_entry
*gpe
= st
->sb
+ 1024;
162 memset(&info
->array
, 0, sizeof(info
->array
));
163 memset(&info
->disk
, 0, sizeof(info
->disk
));
164 strcpy(info
->text_version
, "gpt");
165 strcpy(info
->name
, "gpt");
166 info
->component_size
= 0;
168 for (i
= 0; i
< __le32_to_cpu(gpt
->part_cnt
); i
++) {
169 unsigned long long last
=
170 (unsigned long long)__le64_to_cpu(gpe
[i
].ending_lba
);
171 if (last
> info
->component_size
)
172 info
->component_size
= last
;
176 static struct supertype
*match_metadata_desc(char *arg
)
178 struct supertype
*st
= malloc(sizeof(*st
));
182 if (strcmp(arg
, "gpt") != 0)
187 st
->minor_version
= 0;
194 static int validate_geometry(struct supertype
*st
, int level
,
195 int layout
, int raiddisks
,
196 int chunk
, unsigned long long size
,
197 char *subdev
, unsigned long long *freesize
,
200 fprintf(stderr
, Name
": gpt metadata cannot be used this way\n");
205 struct superswitch gpt
= {
207 .examine_super
= examine_gpt
,
209 .validate_geometry
= validate_geometry
,
210 .match_metadata_desc
= match_metadata_desc
,
211 .load_super
= load_gpt
,
212 .store_super
= store_gpt
,
213 .getinfo_super
= getinfo_gpt
,
214 .free_super
= free_gpt
,