disklabel.c revision 1.12 1 /* $NetBSD: disklabel.c,v 1.12 2019/08/14 13:02:23 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
818 if (part != 0)
819 *part = ptn;
820
821 switch (which_name) {
822 case parent_device_only:
823 strlcpy(devname, arg->disk, max_devname_len);
824 return true;
825 case logical_name:
826 case plain_name:
827 if (with_path)
828 snprintf(devname, max_devname_len, _PATH_DEV "%s%c",
829 arg->disk, (char)ptn + 'a');
830 else
831 snprintf(devname, max_devname_len, "%s%c",
832 arg->disk, (char)ptn + 'a');
833 return true;
834 case raw_dev_name:
835 if (with_path)
836 snprintf(devname, max_devname_len, _PATH_DEV "r%s%c",
837 arg->disk, (char)ptn + 'a');
838 else
839 snprintf(devname, max_devname_len, "r%s%c",
840 arg->disk, (char)ptn + 'a');
841 return true;
842 }
843
844 return false;
845 }
846
847 static part_id
848 disklabel_add_partition(struct disk_partitions *arg,
849 const struct disk_part_info *info, const char **err_msg)
850 {
851 struct disklabel_disk_partitions *parts =
852 (struct disklabel_disk_partitions*)arg;
853 int i, part = -1;
854 part_id new_id;
855 struct disk_part_free_space space;
856 struct disk_part_info data = *info;
857
858 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 1,
859 info->start, -1) < 1) {
860 if (err_msg)
861 *err_msg = msg_string(MSG_No_free_space);
862 return NO_PART;
863 }
864 if (data.size > space.size)
865 data.size = space.size;
866 daddr_t dend = data.start+data.size;
867 if (space.start > data.start)
868 data.start = space.start;
869 if (space.start + space.size < dend)
870 data.size = space.start+space.size-data.start;
871
872 if (dl_maxpart == 0)
873 dl_maxpart = getmaxpartitions();
874
875 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) {
876 if (parts->l.d_partitions[i].p_size > 0)
877 new_id++;
878 if (info->nat_type->generic_ptype != PT_root &&
879 info->nat_type->generic_ptype != PT_swap && i < RAW_PART)
880 continue;
881 if (i == 0 && info->nat_type->generic_ptype != PT_root)
882 continue;
883 if (i == 1 && info->nat_type->generic_ptype != PT_swap)
884 continue;
885 if (i == RAW_PART)
886 continue;
887 #if RAW_PART > 2
888 if (i == RAW_PART-1)
889 continue;
890 #endif
891 if (parts->l.d_partitions[i].p_size > 0)
892 continue;
893 part = i;
894 break;
895 }
896
897 if (part < 0) {
898 if (parts->l.d_npartitions >= dl_maxpart) {
899 if (err_msg)
900 *err_msg =
901 msg_string(MSG_err_too_many_partitions);
902 return NO_PART;
903 }
904
905 part = parts->l.d_npartitions++;
906 }
907 parts->l.d_partitions[part].p_offset = data.start;
908 parts->l.d_partitions[part].p_size = data.size;
909 parts->l.d_partitions[part].p_fstype =
910 dl_part_type_from_generic(info->nat_type);
911 if (info->last_mounted && info->last_mounted[0])
912 strlcpy(parts->last_mounted[part], info->last_mounted,
913 sizeof(parts->last_mounted[part]));
914 else
915 parts->last_mounted[part][0] = 0;
916 parts->fs_sub_type[part] = info->fs_sub_type;
917 parts->dp.num_part++;
918 if (data.size <= parts->dp.free_space)
919 parts->dp.free_space -= data.size;
920 else
921 parts->dp.free_space = 0;
922
923 return new_id;
924 }
925
926 static part_id
927 disklabel_add_outer_partition(struct disk_partitions *arg,
928 const struct disk_part_info *info, const char **err_msg)
929 {
930 struct disklabel_disk_partitions *parts =
931 (struct disklabel_disk_partitions*)arg;
932 int i, part = -1;
933 part_id new_id;
934
935 if (dl_maxpart == 0)
936 dl_maxpart = getmaxpartitions();
937
938 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) {
939 if (parts->l.d_partitions[i].p_size > 0)
940 new_id++;
941 if (info->nat_type->generic_ptype != PT_root &&
942 info->nat_type->generic_ptype != PT_swap && i < RAW_PART)
943 continue;
944 if (i == 0 && info->nat_type->generic_ptype != PT_root)
945 continue;
946 if (i == 1 && info->nat_type->generic_ptype != PT_swap)
947 continue;
948 if (i == RAW_PART)
949 continue;
950 #if RAW_PART > 2
951 if (i == RAW_PART-1)
952 continue;
953 #endif
954 if (parts->l.d_partitions[i].p_size > 0)
955 continue;
956 part = i;
957 break;
958 }
959
960 if (part < 0) {
961 if (parts->l.d_npartitions >= dl_maxpart) {
962 if (err_msg)
963 *err_msg =
964 msg_string(MSG_err_too_many_partitions);
965 return NO_PART;
966 }
967
968 part = parts->l.d_npartitions++;
969 }
970 parts->l.d_partitions[part].p_offset = info->start;
971 parts->l.d_partitions[part].p_size = info->size;
972 parts->l.d_partitions[part].p_fstype =
973 dl_part_type_from_generic(info->nat_type);
974 if (info->last_mounted && info->last_mounted[0])
975 strlcpy(parts->last_mounted[part], info->last_mounted,
976 sizeof(parts->last_mounted[part]));
977 else
978 parts->last_mounted[part][0] = 0;
979 parts->fs_sub_type[part] = info->fs_sub_type;
980 parts->dp.num_part++;
981
982 return new_id;
983 }
984
985 static size_t
986 disklabel_get_free_spaces(const struct disk_partitions *arg,
987 struct disk_part_free_space *result, size_t max_num_result,
988 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
989 {
990 const struct disklabel_disk_partitions *parts =
991 (const struct disklabel_disk_partitions*)arg;
992
993 return disklabel_get_free_spaces_internal(parts, result,
994 max_num_result, min_space_size, align, start, ignore);
995 }
996
997 static daddr_t
998 disklabel_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
999 {
1000 const struct disklabel_disk_partitions *parts =
1001 (const struct disklabel_disk_partitions*)arg;
1002 struct disk_part_free_space space;
1003
1004 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 0,
1005 start, start) == 1)
1006 return space.size;
1007
1008 return 0;
1009 }
1010
1011 static daddr_t
1012 disklabel_get_alignment(const struct disk_partitions *arg)
1013 {
1014 const struct disklabel_disk_partitions *parts =
1015 (const struct disklabel_disk_partitions*)arg;
1016
1017 return parts->ptn_alignment;
1018 }
1019
1020 static part_id
1021 disklabel_find_by_name(struct disk_partitions *arg, const char *name)
1022 {
1023 const struct disklabel_disk_partitions *parts =
1024 (const struct disklabel_disk_partitions*)arg;
1025 char *sl, part;
1026 ptrdiff_t n;
1027 part_id pno;
1028
1029 sl = strrchr(name, '/');
1030 if (sl == NULL)
1031 return NO_PART;
1032 n = sl - name;
1033 if (strncmp(name, parts->l.d_packname, n) != 0)
1034 return NO_PART;
1035 part = name[n+1];
1036 if (part < 'a')
1037 return NO_PART;
1038 pno = part - 'a';
1039 if (pno >= parts->l.d_npartitions)
1040 return NO_PART;
1041 if (parts->l.d_partitions[pno].p_fstype == FS_UNUSED)
1042 return NO_PART;
1043 return pno;
1044 }
1045
1046 static void
1047 disklabel_free(struct disk_partitions *arg)
1048 {
1049
1050 assert(arg != NULL);
1051 free(arg);
1052 }
1053
1054 const struct disk_partitioning_scheme
1055 disklabel_parts = {
1056 .name = MSG_parttype_disklabel,
1057 .short_name = MSG_parttype_disklabel_short,
1058 .new_type_prompt = MSG_dl_get_custom_fstype,
1059 .size_limit = (daddr_t)UINT32_MAX,
1060 .write_to_disk = disklabel_write_to_disk,
1061 .read_from_disk = disklabel_parts_read,
1062 .create_new_for_disk = disklabel_parts_new,
1063 .change_disk_geom = disklabel_change_geom,
1064 .find_by_name = disklabel_find_by_name,
1065 .get_disk_pack_name = disklabel_get_disk_pack_name,
1066 .set_disk_pack_name = disklabel_set_disk_pack_name,
1067 .delete_all_partitions = disklabel_delete_all,
1068 .delete_partitions_in_range = disklabel_delete_range,
1069 .delete_partition = disklabel_delete,
1070 .get_part_types_count = disklabel_type_count,
1071 .get_part_type = disklabel_get_type,
1072 .get_generic_part_type = disklabel_get_generic_type,
1073 .get_fs_part_type = disklabel_get_fs_part_type,
1074 .create_custom_part_type = disklabel_create_custom_part_type,
1075 .get_part_alignment = disklabel_get_alignment,
1076 .get_part_info = disklabel_get_part_info,
1077 .can_add_partition = disklabel_can_add_partition,
1078 .set_part_info = disklabel_set_part_info,
1079 .add_partition = disklabel_add_partition,
1080 .add_outer_partition = disklabel_add_outer_partition,
1081 .max_free_space_at = disklabel_max_free_space_at,
1082 .get_free_spaces = disklabel_get_free_spaces,
1083 .get_part_device = disklabel_get_part_device,
1084 .free = disklabel_free,
1085 };
1086