disklabel.c revision 1.9 1 /* $NetBSD: disklabel.c,v 1.9 2019/07/21 11:56:20 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 {
169 int fd;
170 char diskpath[MAXPATHLEN];
171 uint flags;
172
173 if (run_program(RUN_SILENT | RUN_ERROR_OK,
174 "disklabel -r %s", disk) != 0)
175 return NULL;
176
177 /* read partitions */
178
179 struct disklabel_disk_partitions *parts = calloc(1, sizeof(*parts));
180 if (parts == NULL)
181 return NULL;
182
183 fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
184 if (fd == -1) {
185 free(parts);
186 return NULL;
187 }
188
189 /*
190 * We should actually try to read the label inside the start/len
191 * boundary, but for simplicity just rely on the kernel and
192 * instead verify a FS_UNUSED partition at RAW_PART-1 (if
193 * RAW_PART > 'c') is within the given limits.
194 */
195 if (ioctl(fd, DIOCGDINFO, &parts->l) < 0) {
196 free(parts);
197 close(fd);
198 return NULL;
199 }
200 #if RAW_PART > 2
201 if (parts->l.d_partitions[RAW_PART-1].p_fstype == FS_UNUSED) {
202 daddr_t dlstart = parts->l.d_partitions[RAW_PART-1].p_offset;
203 daddr_t dlend = start +
204 parts->l.d_partitions[RAW_PART-1].p_size;
205
206 if (dlstart < start && dlend > (start+len)) {
207 assert(false);
208 free(parts);
209 close(fd);
210 return NULL;
211 }
212 }
213 #endif
214
215 if (len > disklabel_parts.size_limit)
216 len = disklabel_parts.size_limit;
217 parts->dp.pscheme = &disklabel_parts;
218 parts->dp.disk = disk;
219 parts->dp.disk_start = start;
220 parts->dp.disk_size = parts->dp.free_space = len;
221 disklabel_init_default_alignment(parts, 0);
222
223 for (int part = 0; part < parts->l.d_npartitions; part++) {
224 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
225 && parts->l.d_partitions[part].p_size == 0)
226 continue;
227
228 parts->dp.num_part++;
229 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED)
230 continue;
231
232 flags = 0;
233 if (parts->l.d_partitions[part].p_fstype == FS_MSDOS)
234 flags = GLM_MAYBE_FAT32;
235 else if (parts->l.d_partitions[part].p_fstype == FS_BSDFFS)
236 flags = GLM_LIKELY_FFS;
237 if (flags != 0) {
238 uint fs_type, fs_sub_type;
239 const char *lm = get_last_mounted(fd,
240 parts->l.d_partitions[part].p_offset,
241 &fs_type, &fs_sub_type, flags);
242 if (lm != NULL && *lm != 0) {
243 strlcpy(parts->last_mounted[part], lm,
244 sizeof(parts->last_mounted[part]));
245 if (parts->l.d_partitions[part].p_fstype ==
246 fs_type)
247 parts->fs_sub_type[part] = fs_sub_type;
248 }
249 }
250
251 if (parts->l.d_partitions[part].p_size > parts->dp.free_space)
252 parts->dp.free_space = 0;
253 else
254 parts->dp.free_space -=
255 parts->l.d_partitions[part].p_size;
256 }
257 close(fd);
258
259 return &parts->dp;
260 }
261
262 static bool
263 disklabel_write_to_disk(struct disk_partitions *arg)
264 {
265 struct disklabel_disk_partitions *parts =
266 (struct disklabel_disk_partitions*)arg;
267 FILE *f;
268 char fname[PATH_MAX], packname[sizeof(parts->l.d_packname)+1];
269 int i, rv = 0;
270 const char *disk = parts->dp.disk, *s;
271 const struct partition *lp;
272 char *d;
273 size_t n;
274
275 assert(parts->l.d_secsize != 0);
276 assert(parts->l.d_nsectors != 0);
277 assert(parts->l.d_ntracks != 0);
278 assert(parts->l.d_ncylinders != 0);
279 assert(parts->l.d_secpercyl != 0);
280
281 sprintf(fname, "/tmp/disklabel.%u", getpid());
282 f = fopen(fname, "w");
283 if (f == NULL)
284 return false;
285
286 /* make sure we have a 0 terminated packname */
287 strlcpy(packname, parts->l.d_packname, sizeof packname);
288
289 /* fill typename with disk name prefix, if not already set */
290 if (strlen(parts->l.d_typename) == 0) {
291 for (n = 0, d = parts->l.d_typename, s = disk;
292 *s && n < sizeof(parts->l.d_typename); d++, s++, n++) {
293 if (isdigit((unsigned char)*s))
294 break;
295 *d = *s;
296 }
297 }
298 parts->l.d_typename[sizeof(parts->l.d_typename)-1] = 0;
299
300 /* we need a valid disk type name, so enforce an arbitrary if
301 * above did not yield a usable one */
302 if (strlen(parts->l.d_typename) == 0)
303 strncpy(parts->l.d_typename, "SCSI",
304 sizeof(parts->l.d_typename));
305
306 lp = parts->l.d_partitions;
307 scripting_fprintf(NULL, "cat <<EOF >%s\n", fname);
308 scripting_fprintf(f, "%s|NetBSD installation generated:\\\n",
309 parts->l.d_typename);
310 scripting_fprintf(f, "\t:nc#%d:nt#%d:ns#%d:\\\n",
311 parts->l.d_ncylinders, parts->l.d_ntracks, parts->l.d_nsectors);
312 scripting_fprintf(f, "\t:sc#%d:su#%" PRIu32 ":\\\n",
313 parts->l.d_secpercyl, lp[RAW_PART].p_offset+lp[RAW_PART].p_size);
314 scripting_fprintf(f, "\t:se#%d:\\\n", parts->l.d_secsize);
315
316 for (i = 0; i < parts->l.d_npartitions; i++) {
317 scripting_fprintf(f, "\t:p%c#%" PRIu32 ":o%c#%" PRIu32
318 ":t%c=%s:", 'a'+i, (uint32_t)lp[i].p_size,
319 'a'+i, (uint32_t)lp[i].p_offset, 'a'+i,
320 getfslabelname(lp[i].p_fstype, 0));
321 if (lp[i].p_fstype == FS_BSDLFS ||
322 lp[i].p_fstype == FS_BSDFFS)
323 scripting_fprintf (f, "b%c#%" PRIu32 ":f%c#%" PRIu32
324 ":", 'a'+i,
325 (uint32_t)(lp[i].p_fsize *
326 lp[i].p_frag),
327 'a'+i, (uint32_t)lp[i].p_fsize);
328
329 if (i < parts->l.d_npartitions - 1)
330 scripting_fprintf(f, "\\\n");
331 else
332 scripting_fprintf(f, "\n");
333 }
334 scripting_fprintf(NULL, "EOF\n");
335
336 fclose(f);
337
338 /*
339 * Label a disk using an MD-specific string DISKLABEL_CMD for
340 * to invoke disklabel.
341 * if MD code does not define DISKLABEL_CMD, this is a no-op.
342 *
343 * i386 port uses "/sbin/disklabel -w -r", just like i386
344 * miniroot scripts, though this may leave a bogus incore label.
345 *
346 * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w"
347 * to get incore to ondisk inode translation for the Sun proms.
348 */
349 #ifdef DISKLABEL_CMD
350 /* disklabel the disk */
351 rv = run_program(RUN_DISPLAY, "%s -f %s %s %s %s",
352 DISKLABEL_CMD, fname, disk, parts->l.d_typename, packname);
353 #endif
354
355 unlink(fname);
356
357 return rv == 0;
358 }
359
360 static bool
361 disklabel_delete_all(struct disk_partitions *arg)
362 {
363 struct disklabel_disk_partitions *parts =
364 (struct disklabel_disk_partitions*)arg;
365 daddr_t total_size = parts->l.d_partitions[RAW_PART].p_size;
366
367 memset(&parts->l.d_partitions, 0, sizeof(parts->l.d_partitions));
368 parts->dp.num_part = 0;
369
370 #if RAW_PART > 2
371 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED;
372 parts->l.d_partitions[RAW_PART-1].p_offset = parts->dp.disk_start;
373 parts->l.d_partitions[RAW_PART-1].p_size = parts->dp.disk_size;
374 parts->dp.num_part++;
375 #endif
376 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED;
377 parts->l.d_partitions[RAW_PART].p_offset = 0;
378 parts->l.d_partitions[RAW_PART].p_size = total_size;
379 parts->dp.num_part++;
380
381 parts->l.d_npartitions = RAW_PART+1;
382 return true;
383 }
384
385 static bool
386 disklabel_delete(struct disk_partitions *arg, part_id id,
387 const char **err_msg)
388 {
389 struct disklabel_disk_partitions *parts =
390 (struct disklabel_disk_partitions*)arg;
391 part_id ndx;
392
393 ndx = 0;
394 for (int part = 0; part < parts->l.d_npartitions; part++) {
395 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
396 && parts->l.d_partitions[part].p_size == 0)
397 continue;
398
399 if (ndx == id) {
400 if (part == RAW_PART
401 #if RAW_PART > 2
402 || part == RAW_PART-1
403 #endif
404 ) {
405 if (err_msg)
406 *err_msg = msg_string(
407 MSG_part_not_deletable);
408 return false;
409 }
410 parts->l.d_partitions[part].p_size = 0;
411 parts->l.d_partitions[part].p_offset = 0;
412 parts->l.d_partitions[part].p_fstype = FS_UNUSED;
413 parts->dp.num_part--;
414 return true;
415 }
416 ndx++;
417 }
418
419 if (err_msg)
420 *err_msg = INTERNAL_ERROR;
421 return false;
422 }
423
424 static bool
425 disklabel_delete_range(struct disk_partitions *arg, daddr_t r_start,
426 daddr_t r_size)
427 {
428 struct disklabel_disk_partitions *parts =
429 (struct disklabel_disk_partitions*)arg;
430
431 for (int part = 0; part < parts->l.d_npartitions; part++) {
432 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
433 && parts->l.d_partitions[part].p_size == 0)
434 continue;
435
436 if (part == RAW_PART)
437 continue;
438
439 daddr_t start = parts->l.d_partitions[part].p_offset;
440 daddr_t end = start + parts->l.d_partitions[part].p_size;
441
442 #if RAW_PART > 2
443 if (part == RAW_PART - 1 && start == r_start &&
444 r_start + r_size == end)
445 continue;
446 #endif
447
448 if ((start >= r_start && start <= r_start+r_size) ||
449 (end >= r_start && end <= r_start+r_size)) {
450 if (parts->dp.num_part > 1)
451 parts->dp.num_part--;
452 parts->dp.free_space +=
453 parts->l.d_partitions[part].p_size;
454 parts->l.d_partitions[part].p_fstype = FS_UNUSED;
455 parts->l.d_partitions[part].p_size = 0;
456 }
457 }
458
459 return true;
460 }
461
462 static void
463 dl_init_types(void)
464 {
465 for (size_t i = 0; i < __arraycount(dl_types); i++) {
466 if (fstypenames[i] == NULL)
467 break;
468 dl_types[i].short_desc =
469 dl_types[i].description = getfslabelname(i, 0);
470 enum part_type pt;
471 switch (i) {
472 case FS_UNUSED: pt = PT_undef; break;
473 case FS_BSDFFS: pt = PT_root; break;
474 case FS_SWAP: pt = PT_swap; break;
475 case FS_MSDOS: pt = PT_FAT; break;
476 default: pt = PT_unknown; break;
477 }
478 dl_types[i].generic_ptype = pt;
479 }
480 }
481
482 static uint8_t
483 dl_part_type_from_generic(const struct part_type_desc *gent)
484 {
485
486 if (dl_types[0].description == NULL)
487 dl_init_types();
488 for (size_t i = 0; i < __arraycount(dl_types); i++)
489 if (gent == &dl_types[i])
490 return (uint8_t)i;
491
492 for (size_t i = 0; i < dl_custom_ptype_count; i++)
493 if (gent == &dl_custom_ptypes[i].desc)
494 return dl_custom_ptypes[i].type;
495
496 return 0;
497 }
498
499 static size_t
500 disklabel_type_count(void)
501 {
502 return __arraycount(dl_types) + dl_custom_ptype_count;
503 }
504
505 static const struct part_type_desc *
506 disklabel_get_type(size_t ndx)
507 {
508 if (dl_types[0].description == NULL)
509 dl_init_types();
510
511 if (ndx < __arraycount(dl_types))
512 return &dl_types[ndx];
513
514 ndx -= __arraycount(dl_types);
515 if (ndx >= dl_custom_ptype_count)
516 return NULL;
517
518 return &dl_custom_ptypes[ndx].desc;
519 }
520
521 static const struct part_type_desc *
522 disklabel_find_type(uint type, bool create_if_unknown)
523 {
524 if (dl_types[0].description == NULL)
525 dl_init_types();
526
527 if (type < __arraycount(dl_types))
528 return &dl_types[type];
529
530 for (size_t i = 0; i < dl_custom_ptype_count; i++)
531 if (dl_custom_ptypes[i].type == type)
532 return &dl_custom_ptypes[i].desc;
533
534 if (create_if_unknown) {
535 struct dl_custom_ptype *nt;
536
537 nt = realloc(dl_custom_ptypes, dl_custom_ptype_count+1);
538 if (nt == NULL)
539 return NULL;
540 dl_custom_ptypes = nt;
541 nt = dl_custom_ptypes + dl_custom_ptype_count;
542 dl_custom_ptype_count++;
543 memset(nt, 0, sizeof(*nt));
544 nt->type = type;
545 snprintf(nt->short_desc, sizeof(nt->short_desc), "%u", type);
546 nt->short_desc[sizeof(nt->short_desc)-1] = 0;
547 snprintf(nt->description, sizeof(nt->description),
548 "%s (%u)", msg_string(MSG_custom_type), type);
549 nt->description[sizeof(nt->description)-1] = 0;
550 nt->desc.generic_ptype = PT_unknown;
551 nt->desc.short_desc = nt->short_desc;
552 nt->desc.description = nt->description;
553 return &nt->desc;
554 }
555
556 return NULL;
557 }
558
559 static const struct part_type_desc *
560 disklabel_create_custom_part_type(const char *custom, const char **err_msg)
561 {
562 char *endp;
563 unsigned long fstype;
564
565 fstype = strtoul(custom, &endp, 10);
566 if (*endp != 0) {
567 if (err_msg)
568 *err_msg = msg_string(MSG_dl_type_invalid);
569 return NULL;
570 }
571
572 return disklabel_find_type(fstype, true);
573 }
574
575 static const struct part_type_desc *
576 disklabel_get_fs_part_type(unsigned fstype, unsigned subtype)
577 {
578 return disklabel_find_type(fstype, false);
579 }
580
581 static const struct part_type_desc *
582 disklabel_get_generic_type(enum part_type pt)
583 {
584 size_t nt;
585
586 if (dl_types[0].description == NULL)
587 dl_init_types();
588
589 switch (pt) {
590 case PT_root: nt = FS_BSDFFS; break;
591 case PT_swap: nt = FS_SWAP; break;
592 case PT_FAT:
593 case PT_EFI_SYSTEM:
594 nt = FS_MSDOS; break;
595 default: nt = FS_UNUSED; break;
596 }
597
598 return disklabel_get_type(nt);
599 }
600
601 static bool
602 disklabel_get_part_info(const struct disk_partitions *arg, part_id id,
603 struct disk_part_info *info)
604 {
605 const struct disklabel_disk_partitions *parts =
606 (const struct disklabel_disk_partitions*)arg;
607 part_id ndx;
608
609 if (dl_types[0].description == NULL)
610 dl_init_types();
611
612 ndx = 0;
613 for (int part = 0; part < parts->l.d_npartitions; part++) {
614 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
615 && parts->l.d_partitions[part].p_size == 0)
616 continue;
617
618 if (ndx == id) {
619 memset(info, 0, sizeof(*info));
620 info->start = parts->l.d_partitions[part].p_offset;
621 info->size = parts->l.d_partitions[part].p_size;
622 info->nat_type = disklabel_find_type(
623 parts->l.d_partitions[part].p_fstype, true);
624 if (parts->last_mounted[part][0] != 0)
625 info->last_mounted = parts->last_mounted[part];
626 info->fs_type = parts->l.d_partitions[part].p_fstype;
627 info->fs_sub_type = parts->fs_sub_type[part];
628 if (part == RAW_PART &&
629 parts->l.d_partitions[part].p_fstype == FS_UNUSED)
630 info->flags |=
631 PTI_PSCHEME_INTERNAL|PTI_RAW_PART;
632 #if RAW_PART > 2
633 if (part == (RAW_PART-1) &&
634 parts->l.d_partitions[part].p_fstype == FS_UNUSED)
635 info->flags |=
636 PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK;
637 #endif
638 return true;
639 }
640
641 ndx++;
642 if (ndx > parts->dp.num_part || ndx > id)
643 break;
644 }
645
646 return false;
647 }
648
649 static bool
650 disklabel_set_part_info(struct disk_partitions *arg, part_id id,
651 const struct disk_part_info *info, const char **err_msg)
652 {
653 struct disklabel_disk_partitions *parts =
654 (struct disklabel_disk_partitions*)arg;
655 part_id ndx;
656
657 if (dl_types[0].description == NULL)
658 dl_init_types();
659
660 ndx = 0;
661 for (int part = 0; part < parts->l.d_npartitions; part++) {
662 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED
663 && parts->l.d_partitions[part].p_size == 0)
664 continue;
665
666 if (ndx == id) {
667 parts->l.d_partitions[part].p_offset = info->start;
668 parts->l.d_partitions[part].p_size = info->size;
669 parts->l.d_partitions[part].p_fstype =
670 dl_part_type_from_generic(info->nat_type);
671 if (info->last_mounted != NULL &&
672 info->last_mounted != parts->last_mounted[part])
673 strlcpy(parts->last_mounted[part],
674 info->last_mounted,
675 sizeof(parts->last_mounted[part]));
676 assert(info->fs_type == 0 || info->fs_type ==
677 parts->l.d_partitions[part].p_fstype);
678 if (info->fs_sub_type != 0)
679 parts->fs_sub_type[part] = info->fs_sub_type;
680 return true;
681 }
682
683 ndx++;
684 if (ndx > parts->dp.num_part || ndx > id)
685 break;
686 }
687
688 return false;
689 }
690
691 static size_t
692 disklabel_get_free_spaces_internal(const struct
693 disklabel_disk_partitions *parts,
694 struct disk_part_free_space *result, size_t max_num_result,
695 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
696 {
697 size_t cnt = 0, i;
698 daddr_t s, e, from, size, end_of_disk;
699
700 if (start < parts->dp.disk_start)
701 start = parts->dp.disk_start;
702 if (min_space_size < 1)
703 min_space_size = 1;
704 if (align > 1 && (start % align) != 0)
705 start = max(roundup(start, align), align);
706 end_of_disk = parts->dp.disk_start + parts->dp.disk_size;
707 from = start;
708 while (from < end_of_disk && cnt < max_num_result) {
709 again:
710 size = parts->dp.disk_start + parts->dp.disk_size - from;
711 start = from;
712 for (i = 0; i < parts->l.d_npartitions; i++) {
713 if (i == RAW_PART)
714 continue;
715 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED)
716 continue;
717
718 s = parts->l.d_partitions[i].p_offset;
719 e = parts->l.d_partitions[i].p_size + s;
720 if (s == ignore)
721 continue;
722 if (e < from)
723 continue;
724 if (s <= from && e > from) {
725 if (e - 1 >= end_of_disk)
726 return cnt;
727
728 from = e + 1;
729 if (align > 1) {
730 from = max(roundup(from, align), align);
731 if (from >= end_of_disk) {
732 size = 0;
733 break;
734 }
735 }
736 goto again;
737 }
738 if (s > from && s - from < size) {
739 size = s - from;
740 }
741 }
742 if (size >= min_space_size) {
743 result->start = start;
744 result->size = size;
745 result++;
746 cnt++;
747 }
748 from += size + 1;
749 if (align > 1)
750 from = max(roundup(from, align), align);
751 }
752
753 return cnt;
754 }
755
756 static bool
757 disklabel_can_add_partition(const struct disk_partitions *arg)
758 {
759 const struct disklabel_disk_partitions *parts =
760 (const struct disklabel_disk_partitions*)arg;
761 struct disk_part_free_space space;
762 int i;
763
764 if (dl_maxpart == 0)
765 dl_maxpart = getmaxpartitions();
766 if (parts->dp.free_space < parts->ptn_alignment)
767 return false;
768 if (parts->dp.num_part >= dl_maxpart)
769 return false;
770 if (disklabel_get_free_spaces_internal(parts, &space, 1,
771 parts->ptn_alignment, parts->ptn_alignment, 0, -1) < 1)
772 return false;
773
774 for (i = 0; i < parts->l.d_npartitions; i++) {
775 if (i == RAW_PART)
776 continue;
777 #if RAW_PART > 2
778 if (i == RAW_PART-1)
779 continue;
780 #endif
781 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED)
782 return true;
783 }
784 return false;
785 }
786
787 static bool
788 disklabel_get_disk_pack_name(const struct disk_partitions *arg,
789 char *buf, size_t len)
790 {
791 const struct disklabel_disk_partitions *parts =
792 (const struct disklabel_disk_partitions*)arg;
793
794 strlcpy(buf, parts->l.d_packname, min(len,
795 sizeof(parts->l.d_packname)+1));
796 return true;
797 }
798
799 static bool
800 disklabel_set_disk_pack_name(struct disk_partitions *arg, const char *pack)
801 {
802 struct disklabel_disk_partitions *parts =
803 (struct disklabel_disk_partitions*)arg;
804
805 strncpy(parts->l.d_packname, pack, sizeof(parts->l.d_packname));
806 return true;
807 }
808
809 static bool
810 disklabel_get_part_device(const struct disk_partitions *arg,
811 part_id ptn, char *devname, size_t max_devname_len, int *part,
812 enum dev_name_usage which_name, bool with_path)
813 {
814
815 if (part != 0)
816 *part = ptn;
817
818 switch (which_name) {
819 case parent_device_only:
820 strlcpy(devname, arg->disk, max_devname_len);
821 return true;
822 case logical_name:
823 case plain_name:
824 if (with_path)
825 snprintf(devname, max_devname_len, _PATH_DEV "%s%c",
826 arg->disk, (char)ptn + 'a');
827 else
828 snprintf(devname, max_devname_len, "%s%c",
829 arg->disk, (char)ptn + 'a');
830 return true;
831 case raw_dev_name:
832 if (with_path)
833 snprintf(devname, max_devname_len, _PATH_DEV "r%s%c",
834 arg->disk, (char)ptn + 'a');
835 else
836 snprintf(devname, max_devname_len, "r%s%c",
837 arg->disk, (char)ptn + 'a');
838 return true;
839 }
840
841 return false;
842 }
843
844 static part_id
845 disklabel_add_partition(struct disk_partitions *arg,
846 const struct disk_part_info *info, const char **err_msg)
847 {
848 struct disklabel_disk_partitions *parts =
849 (struct disklabel_disk_partitions*)arg;
850 int i, part = -1;
851 part_id new_id;
852 struct disk_part_free_space space;
853 struct disk_part_info data = *info;
854
855 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 1,
856 info->start, -1) < 1) {
857 if (err_msg)
858 *err_msg = msg_string(MSG_No_free_space);
859 return NO_PART;
860 }
861 if (data.size > space.size)
862 data.size = space.size;
863 daddr_t dend = data.start+data.size;
864 if (space.start > data.start)
865 data.start = space.start;
866 if (space.start + space.size < dend)
867 data.size = space.start+space.size-data.start;
868
869 if (dl_maxpart == 0)
870 dl_maxpart = getmaxpartitions();
871
872 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) {
873 if (parts->l.d_partitions[i].p_size > 0)
874 new_id++;
875 if (info->nat_type->generic_ptype != PT_root &&
876 info->nat_type->generic_ptype != PT_swap && i < RAW_PART)
877 continue;
878 if (i == 0 && info->nat_type->generic_ptype != PT_root)
879 continue;
880 if (i == 1 && info->nat_type->generic_ptype != PT_swap)
881 continue;
882 if (i == RAW_PART)
883 continue;
884 #if RAW_PART > 2
885 if (i == RAW_PART-1)
886 continue;
887 #endif
888 if (parts->l.d_partitions[i].p_size > 0)
889 continue;
890 part = i;
891 break;
892 }
893
894 if (part < 0) {
895 if (parts->l.d_npartitions >= dl_maxpart) {
896 if (err_msg)
897 *err_msg =
898 msg_string(MSG_err_too_many_partitions);
899 return NO_PART;
900 }
901
902 part = parts->l.d_npartitions++;
903 }
904 parts->l.d_partitions[part].p_offset = data.start;
905 parts->l.d_partitions[part].p_size = data.size;
906 parts->l.d_partitions[part].p_fstype =
907 dl_part_type_from_generic(info->nat_type);
908 if (info->last_mounted && info->last_mounted[0])
909 strlcpy(parts->last_mounted[part], info->last_mounted,
910 sizeof(parts->last_mounted[part]));
911 else
912 parts->last_mounted[part][0] = 0;
913 parts->fs_sub_type[part] = info->fs_sub_type;
914 parts->dp.num_part++;
915 if (data.size <= parts->dp.free_space)
916 parts->dp.free_space -= data.size;
917 else
918 parts->dp.free_space = 0;
919
920 return new_id;
921 }
922
923 static part_id
924 disklabel_add_outer_partition(struct disk_partitions *arg,
925 const struct disk_part_info *info, const char **err_msg)
926 {
927 struct disklabel_disk_partitions *parts =
928 (struct disklabel_disk_partitions*)arg;
929 int i, part = -1;
930 part_id new_id;
931
932 if (dl_maxpart == 0)
933 dl_maxpart = getmaxpartitions();
934
935 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) {
936 if (parts->l.d_partitions[i].p_size > 0)
937 new_id++;
938 if (info->nat_type->generic_ptype != PT_root &&
939 info->nat_type->generic_ptype != PT_swap && i < RAW_PART)
940 continue;
941 if (i == 0 && info->nat_type->generic_ptype != PT_root)
942 continue;
943 if (i == 1 && info->nat_type->generic_ptype != PT_swap)
944 continue;
945 if (i == RAW_PART)
946 continue;
947 #if RAW_PART > 2
948 if (i == RAW_PART-1)
949 continue;
950 #endif
951 if (parts->l.d_partitions[i].p_size > 0)
952 continue;
953 part = i;
954 break;
955 }
956
957 if (part < 0) {
958 if (parts->l.d_npartitions >= dl_maxpart) {
959 if (err_msg)
960 *err_msg =
961 msg_string(MSG_err_too_many_partitions);
962 return NO_PART;
963 }
964
965 part = parts->l.d_npartitions++;
966 }
967 parts->l.d_partitions[part].p_offset = info->start;
968 parts->l.d_partitions[part].p_size = info->size;
969 parts->l.d_partitions[part].p_fstype =
970 dl_part_type_from_generic(info->nat_type);
971 if (info->last_mounted && info->last_mounted[0])
972 strlcpy(parts->last_mounted[part], info->last_mounted,
973 sizeof(parts->last_mounted[part]));
974 else
975 parts->last_mounted[part][0] = 0;
976 parts->fs_sub_type[part] = info->fs_sub_type;
977 parts->dp.num_part++;
978
979 return new_id;
980 }
981
982 static size_t
983 disklabel_get_free_spaces(const struct disk_partitions *arg,
984 struct disk_part_free_space *result, size_t max_num_result,
985 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
986 {
987 const struct disklabel_disk_partitions *parts =
988 (const struct disklabel_disk_partitions*)arg;
989
990 return disklabel_get_free_spaces_internal(parts, result,
991 max_num_result, min_space_size, align, start, ignore);
992 }
993
994 static daddr_t
995 disklabel_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
996 {
997 const struct disklabel_disk_partitions *parts =
998 (const struct disklabel_disk_partitions*)arg;
999 struct disk_part_free_space space;
1000
1001 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 0,
1002 start, start) == 1)
1003 return space.size;
1004
1005 return 0;
1006 }
1007
1008 static daddr_t
1009 disklabel_get_alignment(const struct disk_partitions *arg)
1010 {
1011 const struct disklabel_disk_partitions *parts =
1012 (const struct disklabel_disk_partitions*)arg;
1013
1014 return parts->ptn_alignment;
1015 }
1016
1017 static void
1018 disklabel_free(struct disk_partitions *arg)
1019 {
1020
1021 assert(arg != NULL);
1022 free(arg);
1023 }
1024
1025 const struct disk_partitioning_scheme
1026 disklabel_parts = {
1027 .name = MSG_parttype_disklabel,
1028 .short_name = MSG_parttype_disklabel_short,
1029 .new_type_prompt = MSG_dl_get_custom_fstype,
1030 .size_limit = (daddr_t)UINT32_MAX,
1031 .write_to_disk = disklabel_write_to_disk,
1032 .read_from_disk = disklabel_parts_read,
1033 .create_new_for_disk = disklabel_parts_new,
1034 .change_disk_geom = disklabel_change_geom,
1035 .get_disk_pack_name = disklabel_get_disk_pack_name,
1036 .set_disk_pack_name = disklabel_set_disk_pack_name,
1037 .delete_all_partitions = disklabel_delete_all,
1038 .delete_partitions_in_range = disklabel_delete_range,
1039 .delete_partition = disklabel_delete,
1040 .get_part_types_count = disklabel_type_count,
1041 .get_part_type = disklabel_get_type,
1042 .get_generic_part_type = disklabel_get_generic_type,
1043 .get_fs_part_type = disklabel_get_fs_part_type,
1044 .create_custom_part_type = disklabel_create_custom_part_type,
1045 .get_part_alignment = disklabel_get_alignment,
1046 .get_part_info = disklabel_get_part_info,
1047 .can_add_partition = disklabel_can_add_partition,
1048 .set_part_info = disklabel_set_part_info,
1049 .add_partition = disklabel_add_partition,
1050 .add_outer_partition = disklabel_add_outer_partition,
1051 .max_free_space_at = disklabel_max_free_space_at,
1052 .get_free_spaces = disklabel_get_free_spaces,
1053 .get_part_device = disklabel_get_part_device,
1054 .free = disklabel_free,
1055 };
1056