disklabel.c revision 1.13 1 /* $NetBSD: disklabel.c,v 1.13 2019/08/14 13:58:00 martin Exp $ */
2
3 /*
4 * Copyright 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include "defs.h"
31 #include "md.h"
32 #include <assert.h>
33 #include <util.h>
34 #include <paths.h>
35 #include <sys/ioctl.h>
36 #include <sys/param.h>
37
38 const struct disk_partitioning_scheme disklabel_parts;
39
40 /*************** disklabel ******************************************/
41 /* a disklabel based disk_partitions interface */
42 struct disklabel_disk_partitions {
43 struct disk_partitions dp;
44 struct disklabel l;
45 daddr_t ptn_alignment;
46 char last_mounted[MAXPARTITIONS][MOUNTLEN];
47 uint fs_sub_type[MAXPARTITIONS];
48 };
49
50 /*
51 * Maximum number of disklabel partitions the current kernel supports
52 */
53 size_t dl_maxpart;
54
55 /* index into this arrray is the type code */
56 static struct part_type_desc dl_types[__arraycount(fstypenames)-1];
57
58 struct dl_custom_ptype {
59 unsigned int type;
60 char short_desc[6], description[30];
61 struct part_type_desc desc;
62 };
63 struct dl_custom_ptype * dl_custom_ptypes;
64 size_t dl_custom_ptype_count;
65
66 static uint8_t dl_part_type_from_generic(const struct part_type_desc*);
67
68 static void
69 disklabel_init_default_alignment(struct disklabel_disk_partitions *parts,
70 uint track)
71 {
72 if (track == 0)
73 track = MEG / 512;
74
75 if (dl_maxpart == 0)
76 dl_maxpart = getmaxpartitions();
77
78 #ifdef MD_DISKLABEL_SET_ALIGN_PRE
79 if (MD_DISKLABEL_SET_ALIGN_PRE(parts->ptn_alignment, track))
80 return;
81 #endif
82 /* Use 1MB alignemnt for large (>128GB) disks */
83 if (parts->dp.disk_size > HUGE_DISK_SIZE) {
84 parts->ptn_alignment = 2048;
85 } else if (parts->dp.disk_size > TINY_DISK_SIZE) {
86 parts->ptn_alignment = 64;
87 } else {
88 parts->ptn_alignment = 1;
89 }
90 #ifdef MD_DISKLABEL_SET_ALIGN_POST
91 MD_DISKLABEL_SET_ALIGN_POST(parts->ptn_alignment, track);
92 #endif
93 }
94
95 static bool
96 disklabel_change_geom(struct disk_partitions *arg, int ncyl, int nhead,
97 int nsec)
98 {
99 struct disklabel_disk_partitions *parts =
100 (struct disklabel_disk_partitions*)arg;
101
102 assert(parts->l.d_secsize != 0);
103 assert(parts->l.d_nsectors != 0);
104 assert(parts->l.d_ntracks != 0);
105 assert(parts->l.d_ncylinders != 0);
106 assert(parts->l.d_secpercyl != 0);
107
108 disklabel_init_default_alignment(parts, nhead * nsec);
109 if (ncyl*nhead*nsec <= TINY_DISK_SIZE)
110 set_default_sizemult(1);
111 else
112 set_default_sizemult(MEG/512);
113
114 return true;
115 }
116
117 static struct disk_partitions *
118 disklabel_parts_new(const char *dev, daddr_t start, daddr_t len,
119 daddr_t total_size, bool is_boot_drive)
120 {
121 struct disklabel_disk_partitions *parts;
122 struct disk_geom geo;
123
124 if (!get_disk_geom(dev, &geo))
125 return NULL;
126
127 parts = calloc(1, sizeof(*parts));
128 if (parts == NULL)
129 return NULL;
130
131 if (len > disklabel_parts.size_limit)
132 len = disklabel_parts.size_limit;
133 if (total_size > disklabel_parts.size_limit)
134 total_size = disklabel_parts.size_limit;
135
136 parts->l.d_ncylinders = geo.dg_ncylinders;
137 parts->l.d_ntracks = geo.dg_ntracks;
138 parts->l.d_nsectors = geo.dg_nsectors;
139 parts->l.d_secsize = geo.dg_secsize;
140 parts->l.d_secpercyl = geo.dg_nsectors * geo.dg_ntracks;
141
142 parts->dp.pscheme = &disklabel_parts;
143 parts->dp.disk = dev;
144 parts->dp.disk_start = start;
145 parts->dp.disk_size = parts->dp.free_space = len;
146 disklabel_init_default_alignment(parts, parts->l.d_secpercyl);
147
148 strncpy(parts->l.d_packname, "fictious", sizeof parts->l.d_packname);
149
150 #if RAW_PART > 2
151 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED;
152 parts->l.d_partitions[RAW_PART-1].p_offset = start;
153 parts->l.d_partitions[RAW_PART-1].p_size = len;
154 parts->dp.num_part++;
155 #endif
156 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED;
157 parts->l.d_partitions[RAW_PART].p_offset = 0;
158 parts->l.d_partitions[RAW_PART].p_size = total_size;
159 parts->dp.num_part++;
160
161 parts->l.d_npartitions = RAW_PART+1;
162
163 return &parts->dp;
164 }
165
166 static struct disk_partitions *
167 disklabel_parts_read(const char *disk, daddr_t start, daddr_t len,
168 const struct disk_partitioning_scheme *scheme)
169 {
170 int fd;
171 char diskpath[MAXPATHLEN];
172 uint flags;
173
174 if (run_program(RUN_SILENT | RUN_ERROR_OK,
175 "disklabel -r %s", disk) != 0)
176 return NULL;
177
178 /* read partitions */
179
180 struct disklabel_disk_partitions *parts = calloc(1, sizeof(*parts));
181 if (parts == NULL)
182 return NULL;
183
184 fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
185 if (fd == -1) {
186 free(parts);
187 return NULL;
188 }
189
190 /*
191 * We should actually try to read the label inside the start/len
192 * boundary, but for simplicity just rely on the kernel and
193 * instead verify a FS_UNUSED partition at RAW_PART-1 (if
194 * RAW_PART > 'c') is within the given limits.
195 */
196 if (ioctl(fd, DIOCGDINFO, &parts->l) < 0) {
197 free(parts);
198 close(fd);
199 return NULL;
200 }
201 #if RAW_PART > 2
202 if (parts->l.d_partitions[RAW_PART-1].p_fstype == FS_UNUSED) {
203 daddr_t dlstart = parts->l.d_partitions[RAW_PART-1].p_offset;
204 daddr_t dlend = start +
205 parts->l.d_partitions[RAW_PART-1].p_size;
206
207 if (dlstart < start && dlend > (start+len)) {
208 assert(false);
209 free(parts);
210 close(fd);
211 return NULL;
212 }
213 }
214 #endif
215
216 if (len > disklabel_parts.size_limit)
217 len = disklabel_parts.size_limit;
218 parts->dp.pscheme = scheme;
219 parts->dp.disk = disk;
220 parts->dp.disk_start = start;
221 parts->dp.disk_size = parts->dp.free_space = len;
222 disklabel_init_default_alignment(parts, 0);
223
224 for (int part = 0; part < parts->l.d_npartitions; part++) {
225 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
226 && parts->l.d_partitions[part].p_size == 0)
227 continue;
228
229 parts->dp.num_part++;
230 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED)
231 continue;
232
233 flags = 0;
234 if (parts->l.d_partitions[part].p_fstype == FS_MSDOS)
235 flags = GLM_MAYBE_FAT32;
236 else if (parts->l.d_partitions[part].p_fstype == FS_BSDFFS)
237 flags = GLM_LIKELY_FFS;
238 if (flags != 0) {
239 uint fs_type, fs_sub_type;
240 const char *lm = get_last_mounted(fd,
241 parts->l.d_partitions[part].p_offset,
242 &fs_type, &fs_sub_type, flags);
243 if (lm != NULL && *lm != 0) {
244 strlcpy(parts->last_mounted[part], lm,
245 sizeof(parts->last_mounted[part]));
246 if (parts->l.d_partitions[part].p_fstype ==
247 fs_type)
248 parts->fs_sub_type[part] = fs_sub_type;
249 canonicalize_last_mounted(
250 parts->last_mounted[part]);
251 }
252 }
253
254 if (parts->l.d_partitions[part].p_size > parts->dp.free_space)
255 parts->dp.free_space = 0;
256 else
257 parts->dp.free_space -=
258 parts->l.d_partitions[part].p_size;
259 }
260 close(fd);
261
262 return &parts->dp;
263 }
264
265 static bool
266 disklabel_write_to_disk(struct disk_partitions *arg)
267 {
268 struct disklabel_disk_partitions *parts =
269 (struct disklabel_disk_partitions*)arg;
270 FILE *f;
271 char fname[PATH_MAX], packname[sizeof(parts->l.d_packname)+1];
272 int i, rv = 0;
273 const char *disk = parts->dp.disk, *s;
274 const struct partition *lp;
275 char *d;
276 size_t n;
277
278 assert(parts->l.d_secsize != 0);
279 assert(parts->l.d_nsectors != 0);
280 assert(parts->l.d_ntracks != 0);
281 assert(parts->l.d_ncylinders != 0);
282 assert(parts->l.d_secpercyl != 0);
283
284 sprintf(fname, "/tmp/disklabel.%u", getpid());
285 f = fopen(fname, "w");
286 if (f == NULL)
287 return false;
288
289 /* make sure we have a 0 terminated packname */
290 strlcpy(packname, parts->l.d_packname, sizeof packname);
291
292 /* fill typename with disk name prefix, if not already set */
293 if (strlen(parts->l.d_typename) == 0) {
294 for (n = 0, d = parts->l.d_typename, s = disk;
295 *s && n < sizeof(parts->l.d_typename); d++, s++, n++) {
296 if (isdigit((unsigned char)*s))
297 break;
298 *d = *s;
299 }
300 }
301 parts->l.d_typename[sizeof(parts->l.d_typename)-1] = 0;
302
303 /* we need a valid disk type name, so enforce an arbitrary if
304 * above did not yield a usable one */
305 if (strlen(parts->l.d_typename) == 0)
306 strncpy(parts->l.d_typename, "SCSI",
307 sizeof(parts->l.d_typename));
308
309 lp = parts->l.d_partitions;
310 scripting_fprintf(NULL, "cat <<EOF >%s\n", fname);
311 scripting_fprintf(f, "%s|NetBSD installation generated:\\\n",
312 parts->l.d_typename);
313 scripting_fprintf(f, "\t:nc#%d:nt#%d:ns#%d:\\\n",
314 parts->l.d_ncylinders, parts->l.d_ntracks, parts->l.d_nsectors);
315 scripting_fprintf(f, "\t:sc#%d:su#%" PRIu32 ":\\\n",
316 parts->l.d_secpercyl, lp[RAW_PART].p_offset+lp[RAW_PART].p_size);
317 scripting_fprintf(f, "\t:se#%d:\\\n", parts->l.d_secsize);
318
319 for (i = 0; i < parts->l.d_npartitions; i++) {
320 scripting_fprintf(f, "\t:p%c#%" PRIu32 ":o%c#%" PRIu32
321 ":t%c=%s:", 'a'+i, (uint32_t)lp[i].p_size,
322 'a'+i, (uint32_t)lp[i].p_offset, 'a'+i,
323 getfslabelname(lp[i].p_fstype, 0));
324 if (lp[i].p_fstype == FS_BSDLFS ||
325 lp[i].p_fstype == FS_BSDFFS)
326 scripting_fprintf (f, "b%c#%" PRIu32 ":f%c#%" PRIu32
327 ":", 'a'+i,
328 (uint32_t)(lp[i].p_fsize *
329 lp[i].p_frag),
330 'a'+i, (uint32_t)lp[i].p_fsize);
331
332 if (i < parts->l.d_npartitions - 1)
333 scripting_fprintf(f, "\\\n");
334 else
335 scripting_fprintf(f, "\n");
336 }
337 scripting_fprintf(NULL, "EOF\n");
338
339 fclose(f);
340
341 /*
342 * Label a disk using an MD-specific string DISKLABEL_CMD for
343 * to invoke disklabel.
344 * if MD code does not define DISKLABEL_CMD, this is a no-op.
345 *
346 * i386 port uses "/sbin/disklabel -w -r", just like i386
347 * miniroot scripts, though this may leave a bogus incore label.
348 *
349 * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w"
350 * to get incore to ondisk inode translation for the Sun proms.
351 */
352 #ifdef DISKLABEL_CMD
353 /* disklabel the disk */
354 rv = run_program(RUN_DISPLAY, "%s -f %s %s %s %s",
355 DISKLABEL_CMD, fname, disk, parts->l.d_typename, packname);
356 #endif
357
358 unlink(fname);
359
360 return rv == 0;
361 }
362
363 static bool
364 disklabel_delete_all(struct disk_partitions *arg)
365 {
366 struct disklabel_disk_partitions *parts =
367 (struct disklabel_disk_partitions*)arg;
368 daddr_t total_size = parts->l.d_partitions[RAW_PART].p_size;
369
370 memset(&parts->l.d_partitions, 0, sizeof(parts->l.d_partitions));
371 parts->dp.num_part = 0;
372
373 #if RAW_PART > 2
374 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED;
375 parts->l.d_partitions[RAW_PART-1].p_offset = parts->dp.disk_start;
376 parts->l.d_partitions[RAW_PART-1].p_size = parts->dp.disk_size;
377 parts->dp.num_part++;
378 #endif
379 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED;
380 parts->l.d_partitions[RAW_PART].p_offset = 0;
381 parts->l.d_partitions[RAW_PART].p_size = total_size;
382 parts->dp.num_part++;
383
384 parts->l.d_npartitions = RAW_PART+1;
385 return true;
386 }
387
388 static bool
389 disklabel_delete(struct disk_partitions *arg, part_id id,
390 const char **err_msg)
391 {
392 struct disklabel_disk_partitions *parts =
393 (struct disklabel_disk_partitions*)arg;
394 part_id ndx;
395
396 ndx = 0;
397 for (int part = 0; part < parts->l.d_npartitions; part++) {
398 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
399 && parts->l.d_partitions[part].p_size == 0)
400 continue;
401
402 if (ndx == id) {
403 if (part == RAW_PART
404 #if RAW_PART > 2
405 || part == RAW_PART-1
406 #endif
407 ) {
408 if (err_msg)
409 *err_msg = msg_string(
410 MSG_part_not_deletable);
411 return false;
412 }
413 parts->l.d_partitions[part].p_size = 0;
414 parts->l.d_partitions[part].p_offset = 0;
415 parts->l.d_partitions[part].p_fstype = FS_UNUSED;
416 parts->dp.num_part--;
417 return true;
418 }
419 ndx++;
420 }
421
422 if (err_msg)
423 *err_msg = INTERNAL_ERROR;
424 return false;
425 }
426
427 static bool
428 disklabel_delete_range(struct disk_partitions *arg, daddr_t r_start,
429 daddr_t r_size)
430 {
431 struct disklabel_disk_partitions *parts =
432 (struct disklabel_disk_partitions*)arg;
433
434 for (int part = 0; part < parts->l.d_npartitions; part++) {
435 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
436 && parts->l.d_partitions[part].p_size == 0)
437 continue;
438
439 if (part == RAW_PART)
440 continue;
441
442 daddr_t start = parts->l.d_partitions[part].p_offset;
443 daddr_t end = start + parts->l.d_partitions[part].p_size;
444
445 #if RAW_PART > 2
446 if (part == RAW_PART - 1 && start == r_start &&
447 r_start + r_size == end)
448 continue;
449 #endif
450
451 if ((start >= r_start && start <= r_start+r_size) ||
452 (end >= r_start && end <= r_start+r_size)) {
453 if (parts->dp.num_part > 1)
454 parts->dp.num_part--;
455 parts->dp.free_space +=
456 parts->l.d_partitions[part].p_size;
457 parts->l.d_partitions[part].p_fstype = FS_UNUSED;
458 parts->l.d_partitions[part].p_size = 0;
459 }
460 }
461
462 return true;
463 }
464
465 static void
466 dl_init_types(void)
467 {
468 for (size_t i = 0; i < __arraycount(dl_types); i++) {
469 if (fstypenames[i] == NULL)
470 break;
471 dl_types[i].short_desc =
472 dl_types[i].description = getfslabelname(i, 0);
473 enum part_type pt;
474 switch (i) {
475 case FS_UNUSED: pt = PT_undef; break;
476 case FS_BSDFFS: pt = PT_root; break;
477 case FS_SWAP: pt = PT_swap; break;
478 case FS_MSDOS: pt = PT_FAT; break;
479 default: pt = PT_unknown; break;
480 }
481 dl_types[i].generic_ptype = pt;
482 }
483 }
484
485 static uint8_t
486 dl_part_type_from_generic(const struct part_type_desc *gent)
487 {
488
489 if (dl_types[0].description == NULL)
490 dl_init_types();
491 for (size_t i = 0; i < __arraycount(dl_types); i++)
492 if (gent == &dl_types[i])
493 return (uint8_t)i;
494
495 for (size_t i = 0; i < dl_custom_ptype_count; i++)
496 if (gent == &dl_custom_ptypes[i].desc)
497 return dl_custom_ptypes[i].type;
498
499 return 0;
500 }
501
502 static size_t
503 disklabel_type_count(void)
504 {
505 return __arraycount(dl_types) + dl_custom_ptype_count;
506 }
507
508 static const struct part_type_desc *
509 disklabel_get_type(size_t ndx)
510 {
511 if (dl_types[0].description == NULL)
512 dl_init_types();
513
514 if (ndx < __arraycount(dl_types))
515 return &dl_types[ndx];
516
517 ndx -= __arraycount(dl_types);
518 if (ndx >= dl_custom_ptype_count)
519 return NULL;
520
521 return &dl_custom_ptypes[ndx].desc;
522 }
523
524 static const struct part_type_desc *
525 disklabel_find_type(uint type, bool create_if_unknown)
526 {
527 if (dl_types[0].description == NULL)
528 dl_init_types();
529
530 if (type < __arraycount(dl_types))
531 return &dl_types[type];
532
533 for (size_t i = 0; i < dl_custom_ptype_count; i++)
534 if (dl_custom_ptypes[i].type == type)
535 return &dl_custom_ptypes[i].desc;
536
537 if (create_if_unknown) {
538 struct dl_custom_ptype *nt;
539
540 nt = realloc(dl_custom_ptypes, dl_custom_ptype_count+1);
541 if (nt == NULL)
542 return NULL;
543 dl_custom_ptypes = nt;
544 nt = dl_custom_ptypes + dl_custom_ptype_count;
545 dl_custom_ptype_count++;
546 memset(nt, 0, sizeof(*nt));
547 nt->type = type;
548 snprintf(nt->short_desc, sizeof(nt->short_desc), "%u", type);
549 nt->short_desc[sizeof(nt->short_desc)-1] = 0;
550 snprintf(nt->description, sizeof(nt->description),
551 "%s (%u)", msg_string(MSG_custom_type), type);
552 nt->description[sizeof(nt->description)-1] = 0;
553 nt->desc.generic_ptype = PT_unknown;
554 nt->desc.short_desc = nt->short_desc;
555 nt->desc.description = nt->description;
556 return &nt->desc;
557 }
558
559 return NULL;
560 }
561
562 static const struct part_type_desc *
563 disklabel_create_custom_part_type(const char *custom, const char **err_msg)
564 {
565 char *endp;
566 unsigned long fstype;
567
568 fstype = strtoul(custom, &endp, 10);
569 if (*endp != 0) {
570 if (err_msg)
571 *err_msg = msg_string(MSG_dl_type_invalid);
572 return NULL;
573 }
574
575 return disklabel_find_type(fstype, true);
576 }
577
578 static const struct part_type_desc *
579 disklabel_get_fs_part_type(unsigned fstype, unsigned subtype)
580 {
581 return disklabel_find_type(fstype, false);
582 }
583
584 static const struct part_type_desc *
585 disklabel_get_generic_type(enum part_type pt)
586 {
587 size_t nt;
588
589 if (dl_types[0].description == NULL)
590 dl_init_types();
591
592 switch (pt) {
593 case PT_root: nt = FS_BSDFFS; break;
594 case PT_swap: nt = FS_SWAP; break;
595 case PT_FAT:
596 case PT_EFI_SYSTEM:
597 nt = FS_MSDOS; break;
598 default: nt = FS_UNUSED; break;
599 }
600
601 return disklabel_get_type(nt);
602 }
603
604 static bool
605 disklabel_get_part_info(const struct disk_partitions *arg, part_id id,
606 struct disk_part_info *info)
607 {
608 const struct disklabel_disk_partitions *parts =
609 (const struct disklabel_disk_partitions*)arg;
610 part_id ndx;
611
612 if (dl_types[0].description == NULL)
613 dl_init_types();
614
615 ndx = 0;
616 for (int part = 0; part < parts->l.d_npartitions; part++) {
617 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
618 && parts->l.d_partitions[part].p_size == 0)
619 continue;
620
621 if (ndx == id) {
622 memset(info, 0, sizeof(*info));
623 info->start = parts->l.d_partitions[part].p_offset;
624 info->size = parts->l.d_partitions[part].p_size;
625 info->nat_type = disklabel_find_type(
626 parts->l.d_partitions[part].p_fstype, true);
627 if (parts->last_mounted[part][0] != 0)
628 info->last_mounted = parts->last_mounted[part];
629 info->fs_type = parts->l.d_partitions[part].p_fstype;
630 info->fs_sub_type = parts->fs_sub_type[part];
631 if (part == RAW_PART &&
632 parts->l.d_partitions[part].p_fstype == FS_UNUSED)
633 info->flags |=
634 PTI_PSCHEME_INTERNAL|PTI_RAW_PART;
635 #if RAW_PART > 2
636 if (part == (RAW_PART-1) &&
637 parts->l.d_partitions[part].p_fstype == FS_UNUSED)
638 info->flags |=
639 PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK;
640 #endif
641 return true;
642 }
643
644 ndx++;
645 if (ndx > parts->dp.num_part || ndx > id)
646 break;
647 }
648
649 return false;
650 }
651
652 static bool
653 disklabel_set_part_info(struct disk_partitions *arg, part_id id,
654 const struct disk_part_info *info, const char **err_msg)
655 {
656 struct disklabel_disk_partitions *parts =
657 (struct disklabel_disk_partitions*)arg;
658 part_id ndx;
659
660 if (dl_types[0].description == NULL)
661 dl_init_types();
662
663 ndx = 0;
664 for (int part = 0; part < parts->l.d_npartitions; part++) {
665 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
666 && parts->l.d_partitions[part].p_size == 0)
667 continue;
668
669 if (ndx == id) {
670 parts->l.d_partitions[part].p_offset = info->start;
671 parts->l.d_partitions[part].p_size = info->size;
672 parts->l.d_partitions[part].p_fstype =
673 dl_part_type_from_generic(info->nat_type);
674 if (info->last_mounted != NULL &&
675 info->last_mounted != parts->last_mounted[part])
676 strlcpy(parts->last_mounted[part],
677 info->last_mounted,
678 sizeof(parts->last_mounted[part]));
679 assert(info->fs_type == 0 || info->fs_type ==
680 parts->l.d_partitions[part].p_fstype);
681 if (info->fs_sub_type != 0)
682 parts->fs_sub_type[part] = info->fs_sub_type;
683 return true;
684 }
685
686 ndx++;
687 if (ndx > parts->dp.num_part || ndx > id)
688 break;
689 }
690
691 return false;
692 }
693
694 static size_t
695 disklabel_get_free_spaces_internal(const struct
696 disklabel_disk_partitions *parts,
697 struct disk_part_free_space *result, size_t max_num_result,
698 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
699 {
700 size_t cnt = 0, i;
701 daddr_t s, e, from, size, end_of_disk;
702
703 if (start < parts->dp.disk_start)
704 start = parts->dp.disk_start;
705 if (min_space_size < 1)
706 min_space_size = 1;
707 if (align > 1 && (start % align) != 0)
708 start = max(roundup(start, align), align);
709 end_of_disk = parts->dp.disk_start + parts->dp.disk_size;
710 from = start;
711 while (from < end_of_disk && cnt < max_num_result) {
712 again:
713 size = parts->dp.disk_start + parts->dp.disk_size - from;
714 start = from;
715 for (i = 0; i < parts->l.d_npartitions; i++) {
716 if (i == RAW_PART)
717 continue;
718 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED)
719 continue;
720
721 s = parts->l.d_partitions[i].p_offset;
722 e = parts->l.d_partitions[i].p_size + s;
723 if (s == ignore)
724 continue;
725 if (e < from)
726 continue;
727 if (s <= from && e > from) {
728 if (e - 1 >= end_of_disk)
729 return cnt;
730
731 from = e + 1;
732 if (align > 1) {
733 from = max(roundup(from, align), align);
734 if (from >= end_of_disk) {
735 size = 0;
736 break;
737 }
738 }
739 goto again;
740 }
741 if (s > from && s - from < size) {
742 size = s - from;
743 }
744 }
745 if (size >= min_space_size) {
746 result->start = start;
747 result->size = size;
748 result++;
749 cnt++;
750 }
751 from += size + 1;
752 if (align > 1)
753 from = max(roundup(from, align), align);
754 }
755
756 return cnt;
757 }
758
759 static bool
760 disklabel_can_add_partition(const struct disk_partitions *arg)
761 {
762 const struct disklabel_disk_partitions *parts =
763 (const struct disklabel_disk_partitions*)arg;
764 struct disk_part_free_space space;
765 int i;
766
767 if (dl_maxpart == 0)
768 dl_maxpart = getmaxpartitions();
769 if (parts->dp.free_space < parts->ptn_alignment)
770 return false;
771 if (parts->dp.num_part >= dl_maxpart)
772 return false;
773 if (disklabel_get_free_spaces_internal(parts, &space, 1,
774 parts->ptn_alignment, parts->ptn_alignment, 0, -1) < 1)
775 return false;
776
777 for (i = 0; i < parts->l.d_npartitions; i++) {
778 if (i == RAW_PART)
779 continue;
780 #if RAW_PART > 2
781 if (i == RAW_PART-1)
782 continue;
783 #endif
784 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED)
785 return true;
786 }
787 return false;
788 }
789
790 static bool
791 disklabel_get_disk_pack_name(const struct disk_partitions *arg,
792 char *buf, size_t len)
793 {
794 const struct disklabel_disk_partitions *parts =
795 (const struct disklabel_disk_partitions*)arg;
796
797 strlcpy(buf, parts->l.d_packname, min(len,
798 sizeof(parts->l.d_packname)+1));
799 return true;
800 }
801
802 static bool
803 disklabel_set_disk_pack_name(struct disk_partitions *arg, const char *pack)
804 {
805 struct disklabel_disk_partitions *parts =
806 (struct disklabel_disk_partitions*)arg;
807
808 strncpy(parts->l.d_packname, pack, sizeof(parts->l.d_packname));
809 return true;
810 }
811
812 static bool
813 disklabel_get_part_device(const struct disk_partitions *arg,
814 part_id ptn, char *devname, size_t max_devname_len, int *part,
815 enum dev_name_usage which_name, bool with_path)
816 {
817 const struct disklabel_disk_partitions *parts =
818 (const struct disklabel_disk_partitions*)arg;
819 part_id id;
820 int part_index;
821 char pname;
822
823 if (ptn >= parts->l.d_npartitions)
824 return false;
825
826 for (id = part_index = 0; id < ptn &&
827 part_index < parts->l.d_npartitions; part_index++)
828 if (parts->l.d_partitions[part_index].p_fstype != FS_UNUSED ||
829 parts->l.d_partitions[part_index].p_size != 0)
830 id++;
831
832 if (part != 0)
833 *part = part_index;
834
835 pname = 'a'+ part_index;
836
837 switch (which_name) {
838 case parent_device_only:
839 strlcpy(devname, arg->disk, max_devname_len);
840 return true;
841 case logical_name:
842 case plain_name:
843 if (with_path)
844 snprintf(devname, max_devname_len, _PATH_DEV "%s%c",
845 arg->disk, pname);
846 else
847 snprintf(devname, max_devname_len, "%s%c",
848 arg->disk, pname);
849 return true;
850 case raw_dev_name:
851 if (with_path)
852 snprintf(devname, max_devname_len, _PATH_DEV "r%s%c",
853 arg->disk, pname);
854 else
855 snprintf(devname, max_devname_len, "r%s%c",
856 arg->disk, pname);
857 return true;
858 }
859
860 return false;
861 }
862
863 static part_id
864 disklabel_add_partition(struct disk_partitions *arg,
865 const struct disk_part_info *info, const char **err_msg)
866 {
867 struct disklabel_disk_partitions *parts =
868 (struct disklabel_disk_partitions*)arg;
869 int i, part = -1;
870 part_id new_id;
871 struct disk_part_free_space space;
872 struct disk_part_info data = *info;
873
874 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 1,
875 info->start, -1) < 1) {
876 if (err_msg)
877 *err_msg = msg_string(MSG_No_free_space);
878 return NO_PART;
879 }
880 if (data.size > space.size)
881 data.size = space.size;
882 daddr_t dend = data.start+data.size;
883 if (space.start > data.start)
884 data.start = space.start;
885 if (space.start + space.size < dend)
886 data.size = space.start+space.size-data.start;
887
888 if (dl_maxpart == 0)
889 dl_maxpart = getmaxpartitions();
890
891 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) {
892 if (parts->l.d_partitions[i].p_size > 0)
893 new_id++;
894 if (info->nat_type->generic_ptype != PT_root &&
895 info->nat_type->generic_ptype != PT_swap && i < RAW_PART)
896 continue;
897 if (i == 0 && info->nat_type->generic_ptype != PT_root)
898 continue;
899 if (i == 1 && info->nat_type->generic_ptype != PT_swap)
900 continue;
901 if (i == RAW_PART)
902 continue;
903 #if RAW_PART > 2
904 if (i == RAW_PART-1)
905 continue;
906 #endif
907 if (parts->l.d_partitions[i].p_size > 0)
908 continue;
909 part = i;
910 break;
911 }
912
913 if (part < 0) {
914 if (parts->l.d_npartitions >= dl_maxpart) {
915 if (err_msg)
916 *err_msg =
917 msg_string(MSG_err_too_many_partitions);
918 return NO_PART;
919 }
920
921 part = parts->l.d_npartitions++;
922 }
923 parts->l.d_partitions[part].p_offset = data.start;
924 parts->l.d_partitions[part].p_size = data.size;
925 parts->l.d_partitions[part].p_fstype =
926 dl_part_type_from_generic(info->nat_type);
927 if (info->last_mounted && info->last_mounted[0])
928 strlcpy(parts->last_mounted[part], info->last_mounted,
929 sizeof(parts->last_mounted[part]));
930 else
931 parts->last_mounted[part][0] = 0;
932 parts->fs_sub_type[part] = info->fs_sub_type;
933 parts->dp.num_part++;
934 if (data.size <= parts->dp.free_space)
935 parts->dp.free_space -= data.size;
936 else
937 parts->dp.free_space = 0;
938
939 return new_id;
940 }
941
942 static part_id
943 disklabel_add_outer_partition(struct disk_partitions *arg,
944 const struct disk_part_info *info, const char **err_msg)
945 {
946 struct disklabel_disk_partitions *parts =
947 (struct disklabel_disk_partitions*)arg;
948 int i, part = -1;
949 part_id new_id;
950
951 if (dl_maxpart == 0)
952 dl_maxpart = getmaxpartitions();
953
954 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) {
955 if (parts->l.d_partitions[i].p_size > 0)
956 new_id++;
957 if (info->nat_type->generic_ptype != PT_root &&
958 info->nat_type->generic_ptype != PT_swap && i < RAW_PART)
959 continue;
960 if (i == 0 && info->nat_type->generic_ptype != PT_root)
961 continue;
962 if (i == 1 && info->nat_type->generic_ptype != PT_swap)
963 continue;
964 if (i == RAW_PART)
965 continue;
966 #if RAW_PART > 2
967 if (i == RAW_PART-1)
968 continue;
969 #endif
970 if (parts->l.d_partitions[i].p_size > 0)
971 continue;
972 part = i;
973 break;
974 }
975
976 if (part < 0) {
977 if (parts->l.d_npartitions >= dl_maxpart) {
978 if (err_msg)
979 *err_msg =
980 msg_string(MSG_err_too_many_partitions);
981 return NO_PART;
982 }
983
984 part = parts->l.d_npartitions++;
985 }
986 parts->l.d_partitions[part].p_offset = info->start;
987 parts->l.d_partitions[part].p_size = info->size;
988 parts->l.d_partitions[part].p_fstype =
989 dl_part_type_from_generic(info->nat_type);
990 if (info->last_mounted && info->last_mounted[0])
991 strlcpy(parts->last_mounted[part], info->last_mounted,
992 sizeof(parts->last_mounted[part]));
993 else
994 parts->last_mounted[part][0] = 0;
995 parts->fs_sub_type[part] = info->fs_sub_type;
996 parts->dp.num_part++;
997
998 return new_id;
999 }
1000
1001 static size_t
1002 disklabel_get_free_spaces(const struct disk_partitions *arg,
1003 struct disk_part_free_space *result, size_t max_num_result,
1004 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
1005 {
1006 const struct disklabel_disk_partitions *parts =
1007 (const struct disklabel_disk_partitions*)arg;
1008
1009 return disklabel_get_free_spaces_internal(parts, result,
1010 max_num_result, min_space_size, align, start, ignore);
1011 }
1012
1013 static daddr_t
1014 disklabel_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
1015 {
1016 const struct disklabel_disk_partitions *parts =
1017 (const struct disklabel_disk_partitions*)arg;
1018 struct disk_part_free_space space;
1019
1020 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 0,
1021 start, start) == 1)
1022 return space.size;
1023
1024 return 0;
1025 }
1026
1027 static daddr_t
1028 disklabel_get_alignment(const struct disk_partitions *arg)
1029 {
1030 const struct disklabel_disk_partitions *parts =
1031 (const struct disklabel_disk_partitions*)arg;
1032
1033 return parts->ptn_alignment;
1034 }
1035
1036 static part_id
1037 disklabel_find_by_name(struct disk_partitions *arg, const char *name)
1038 {
1039 const struct disklabel_disk_partitions *parts =
1040 (const struct disklabel_disk_partitions*)arg;
1041 char *sl, part;
1042 ptrdiff_t n;
1043 part_id pno, id, i;
1044
1045 sl = strrchr(name, '/');
1046 if (sl == NULL)
1047 return NO_PART;
1048 n = sl - name;
1049 if (strncmp(name, parts->l.d_packname, n) != 0)
1050 return NO_PART;
1051 part = name[n+1];
1052 if (part < 'a')
1053 return NO_PART;
1054 pno = part - 'a';
1055 if (pno >= parts->l.d_npartitions)
1056 return NO_PART;
1057 if (parts->l.d_partitions[pno].p_fstype == FS_UNUSED)
1058 return NO_PART;
1059 for (id = 0, i = 0; i < pno; i++)
1060 if (parts->l.d_partitions[i].p_fstype != FS_UNUSED ||
1061 parts->l.d_partitions[i].p_size != 0)
1062 id++;
1063 return id;
1064 }
1065
1066 static void
1067 disklabel_free(struct disk_partitions *arg)
1068 {
1069
1070 assert(arg != NULL);
1071 free(arg);
1072 }
1073
1074 const struct disk_partitioning_scheme
1075 disklabel_parts = {
1076 .name = MSG_parttype_disklabel,
1077 .short_name = MSG_parttype_disklabel_short,
1078 .new_type_prompt = MSG_dl_get_custom_fstype,
1079 .size_limit = (daddr_t)UINT32_MAX,
1080 .write_to_disk = disklabel_write_to_disk,
1081 .read_from_disk = disklabel_parts_read,
1082 .create_new_for_disk = disklabel_parts_new,
1083 .change_disk_geom = disklabel_change_geom,
1084 .find_by_name = disklabel_find_by_name,
1085 .get_disk_pack_name = disklabel_get_disk_pack_name,
1086 .set_disk_pack_name = disklabel_set_disk_pack_name,
1087 .delete_all_partitions = disklabel_delete_all,
1088 .delete_partitions_in_range = disklabel_delete_range,
1089 .delete_partition = disklabel_delete,
1090 .get_part_types_count = disklabel_type_count,
1091 .get_part_type = disklabel_get_type,
1092 .get_generic_part_type = disklabel_get_generic_type,
1093 .get_fs_part_type = disklabel_get_fs_part_type,
1094 .create_custom_part_type = disklabel_create_custom_part_type,
1095 .get_part_alignment = disklabel_get_alignment,
1096 .get_part_info = disklabel_get_part_info,
1097 .can_add_partition = disklabel_can_add_partition,
1098 .set_part_info = disklabel_set_part_info,
1099 .add_partition = disklabel_add_partition,
1100 .add_outer_partition = disklabel_add_outer_partition,
1101 .max_free_space_at = disklabel_max_free_space_at,
1102 .get_free_spaces = disklabel_get_free_spaces,
1103 .get_part_device = disklabel_get_part_device,
1104 .free = disklabel_free,
1105 };
1106