xref: /NextBSD/sys/boot/common/disk.c (revision 4557fabb34e865d7f40be64b39c9e34fa41dbb60)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/disk.h>
32 #include <sys/queue.h>
33 #include <stand.h>
34 #include <stdarg.h>
35 #include <bootstrap.h>
36 #include <part.h>
37 
38 #include "disk.h"
39 
40 #ifdef DISK_DEBUG
41 # define DEBUG(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
42 #else
43 # define DEBUG(fmt, args...)
44 #endif
45 
46 struct open_disk {
47 	struct ptable		*table;
48 	off_t			mediasize;
49 	u_int			sectorsize;
50 	u_int			flags;
51 	int			rcnt;
52 };
53 
54 struct print_args {
55 	struct disk_devdesc	*dev;
56 	const char		*prefix;
57 	int			verbose;
58 };
59 
60 struct dentry {
61 	const struct devsw	*d_dev;
62 	int			d_unit;
63 	int			d_slice;
64 	int			d_partition;
65 
66 	struct open_disk	*od;
67 	off_t			d_offset;
68 	STAILQ_ENTRY(dentry)	entry;
69 #ifdef DISK_DEBUG
70 	uint32_t		count;
71 #endif
72 };
73 
74 static STAILQ_HEAD(, dentry) opened_disks =
75     STAILQ_HEAD_INITIALIZER(opened_disks);
76 
77 static int
disk_lookup(struct disk_devdesc * dev)78 disk_lookup(struct disk_devdesc *dev)
79 {
80 	struct dentry *entry;
81 	int rc;
82 
83 	rc = ENOENT;
84 	STAILQ_FOREACH(entry, &opened_disks, entry) {
85 		if (entry->d_dev != dev->d_dev ||
86 		    entry->d_unit != dev->d_unit)
87 			continue;
88 		dev->d_opendata = entry->od;
89 		if (entry->d_slice == dev->d_slice &&
90 		    entry->d_partition == dev->d_partition) {
91 			dev->d_offset = entry->d_offset;
92 			DEBUG("%s offset %lld", disk_fmtdev(dev),
93 			    (long long)dev->d_offset);
94 #ifdef DISK_DEBUG
95 			entry->count++;
96 #endif
97 			return (0);
98 		}
99 		rc = EAGAIN;
100 	}
101 	return (rc);
102 }
103 
104 static void
disk_insert(struct disk_devdesc * dev)105 disk_insert(struct disk_devdesc *dev)
106 {
107 	struct dentry *entry;
108 
109 	entry = (struct dentry *)malloc(sizeof(struct dentry));
110 	if (entry == NULL) {
111 		DEBUG("no memory");
112 		return;
113 	}
114 	entry->d_dev = dev->d_dev;
115 	entry->d_unit = dev->d_unit;
116 	entry->d_slice = dev->d_slice;
117 	entry->d_partition = dev->d_partition;
118 	entry->od = (struct open_disk *)dev->d_opendata;
119 	entry->od->rcnt++;
120 	entry->d_offset = dev->d_offset;
121 #ifdef DISK_DEBUG
122 	entry->count = 1;
123 #endif
124 	STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
125 	DEBUG("%s cached", disk_fmtdev(dev));
126 }
127 
128 #ifdef DISK_DEBUG
129 COMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
130     command_dcachestat);
131 
132 static int
command_dcachestat(int argc,char * argv[])133 command_dcachestat(int argc, char *argv[])
134 {
135 	struct disk_devdesc dev;
136 	struct dentry *entry;
137 
138 	STAILQ_FOREACH(entry, &opened_disks, entry) {
139 		dev.d_dev = (struct devsw *)entry->d_dev;
140 		dev.d_unit = entry->d_unit;
141 		dev.d_slice = entry->d_slice;
142 		dev.d_partition = entry->d_partition;
143 		printf("%s %d => %p [%d]\n", disk_fmtdev(&dev), entry->count,
144 		    entry->od, entry->od->rcnt);
145 	}
146 	return (CMD_OK);
147 }
148 #endif /* DISK_DEBUG */
149 
150 /* Convert size to a human-readable number. */
151 static char *
display_size(uint64_t size,u_int sectorsize)152 display_size(uint64_t size, u_int sectorsize)
153 {
154 	static char buf[80];
155 	char unit;
156 
157 	size = size * sectorsize / 1024;
158 	unit = 'K';
159 	if (size >= 10485760000LL) {
160 		size /= 1073741824;
161 		unit = 'T';
162 	} else if (size >= 10240000) {
163 		size /= 1048576;
164 		unit = 'G';
165 	} else if (size >= 10000) {
166 		size /= 1024;
167 		unit = 'M';
168 	}
169 	sprintf(buf, "%ld%cB", (long)size, unit);
170 	return (buf);
171 }
172 
173 static int
ptblread(void * d,void * buf,size_t blocks,off_t offset)174 ptblread(void *d, void *buf, size_t blocks, off_t offset)
175 {
176 	struct disk_devdesc *dev;
177 	struct open_disk *od;
178 
179 	dev = (struct disk_devdesc *)d;
180 	od = (struct open_disk *)dev->d_opendata;
181 	return (dev->d_dev->dv_strategy(dev, F_READ, offset,
182 	    blocks * od->sectorsize, (char *)buf, NULL));
183 }
184 
185 #define	PWIDTH	35
186 static void
ptable_print(void * arg,const char * pname,const struct ptable_entry * part)187 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
188 {
189 	struct print_args *pa, bsd;
190 	struct open_disk *od;
191 	struct ptable *table;
192 	char line[80];
193 
194 	pa = (struct print_args *)arg;
195 	od = (struct open_disk *)pa->dev->d_opendata;
196 	sprintf(line, "  %s%s: %s", pa->prefix, pname,
197 	    parttype2str(part->type));
198 	if (pa->verbose)
199 		sprintf(line, "%-*s%s", PWIDTH, line,
200 		    display_size(part->end - part->start + 1,
201 		    od->sectorsize));
202 	strcat(line, "\n");
203 	pager_output(line);
204 	if (part->type == PART_FREEBSD) {
205 		/* Open slice with BSD label */
206 		pa->dev->d_offset = part->start;
207 		table = ptable_open(pa->dev, part->end - part->start + 1,
208 		    od->sectorsize, ptblread);
209 		if (table == NULL)
210 			return;
211 		sprintf(line, "  %s%s", pa->prefix, pname);
212 		bsd.dev = pa->dev;
213 		bsd.prefix = line;
214 		bsd.verbose = pa->verbose;
215 		ptable_iterate(table, &bsd, ptable_print);
216 		ptable_close(table);
217 	}
218 }
219 #undef PWIDTH
220 
221 void
disk_print(struct disk_devdesc * dev,char * prefix,int verbose)222 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
223 {
224 	struct open_disk *od;
225 	struct print_args pa;
226 
227 	/* Disk should be opened */
228 	od = (struct open_disk *)dev->d_opendata;
229 	pa.dev = dev;
230 	pa.prefix = prefix;
231 	pa.verbose = verbose;
232 	ptable_iterate(od->table, &pa, ptable_print);
233 }
234 
235 int
disk_read(struct disk_devdesc * dev,void * buf,off_t offset,u_int blocks)236 disk_read(struct disk_devdesc *dev, void *buf, off_t offset, u_int blocks)
237 {
238 	struct open_disk *od;
239 	int ret;
240 
241 	od = (struct open_disk *)dev->d_opendata;
242 	ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
243 	    blocks * od->sectorsize, buf, NULL);
244 
245 	return (ret);
246 }
247 
248 int
disk_write(struct disk_devdesc * dev,void * buf,off_t offset,u_int blocks)249 disk_write(struct disk_devdesc *dev, void *buf, off_t offset, u_int blocks)
250 {
251 	struct open_disk *od;
252 	int ret;
253 
254 	od = (struct open_disk *)dev->d_opendata;
255 	ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
256 	    blocks * od->sectorsize, buf, NULL);
257 
258 	return (ret);
259 }
260 
261 int
disk_ioctl(struct disk_devdesc * dev,u_long cmd,void * buf)262 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *buf)
263 {
264 
265 	if (dev->d_dev->dv_ioctl)
266 		return ((*dev->d_dev->dv_ioctl)(dev->d_opendata, cmd, buf));
267 
268 	return (ENXIO);
269 }
270 
271 int
disk_open(struct disk_devdesc * dev,off_t mediasize,u_int sectorsize,u_int flags)272 disk_open(struct disk_devdesc *dev, off_t mediasize, u_int sectorsize,
273     u_int flags)
274 {
275 	struct open_disk *od;
276 	struct ptable *table;
277 	struct ptable_entry part;
278 	int rc, slice, partition;
279 
280 	rc = 0;
281 	if ((flags & DISK_F_NOCACHE) == 0) {
282 		rc = disk_lookup(dev);
283 		if (rc == 0)
284 			return (0);
285 	}
286 	/*
287 	 * While we are reading disk metadata, make sure we do it relative
288 	 * to the start of the disk
289 	 */
290 	dev->d_offset = 0;
291 	table = NULL;
292 	slice = dev->d_slice;
293 	partition = dev->d_partition;
294 	if (rc == EAGAIN) {
295 		/*
296 		 * This entire disk was already opened and there is no
297 		 * need to allocate new open_disk structure and open the
298 		 * main partition table.
299 		 */
300 		od = (struct open_disk *)dev->d_opendata;
301 		DEBUG("%s unit %d, slice %d, partition %d => %p (cached)",
302 		    disk_fmtdev(dev), dev->d_unit, dev->d_slice,
303 		    dev->d_partition, od);
304 		goto opened;
305 	} else {
306 		od = (struct open_disk *)malloc(sizeof(struct open_disk));
307 		if (od == NULL) {
308 			DEBUG("no memory");
309 			return (ENOMEM);
310 		}
311 		dev->d_opendata = od;
312 		od->rcnt = 0;
313 	}
314 	od->mediasize = mediasize;
315 	od->sectorsize = sectorsize;
316 	od->flags = flags;
317 	DEBUG("%s unit %d, slice %d, partition %d => %p",
318 	    disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
319 
320 	/* Determine disk layout. */
321 	od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
322 	    ptblread);
323 	if (od->table == NULL) {
324 		DEBUG("Can't read partition table");
325 		rc = ENXIO;
326 		goto out;
327 	}
328 opened:
329 	rc = 0;
330 	if (ptable_gettype(od->table) == PTABLE_BSD &&
331 	    partition >= 0) {
332 		/* It doesn't matter what value has d_slice */
333 		rc = ptable_getpart(od->table, &part, partition);
334 		if (rc == 0)
335 			dev->d_offset = part.start;
336 	} else if (slice >= 0) {
337 		/* Try to get information about partition */
338 		if (slice == 0)
339 			rc = ptable_getbestpart(od->table, &part);
340 		else
341 			rc = ptable_getpart(od->table, &part, slice);
342 		if (rc != 0) /* Partition doesn't exist */
343 			goto out;
344 		dev->d_offset = part.start;
345 		slice = part.index;
346 		if (ptable_gettype(od->table) == PTABLE_GPT) {
347 			partition = 255;
348 			goto out; /* Nothing more to do */
349 		} else if (partition == 255) {
350 			/*
351 			 * When we try to open GPT partition, but partition
352 			 * table isn't GPT, reset d_partition value to -1
353 			 * and try to autodetect appropriate value.
354 			 */
355 			partition = -1;
356 		}
357 		/*
358 		 * If d_partition < 0 and we are looking at a BSD slice,
359 		 * then try to read BSD label, otherwise return the
360 		 * whole MBR slice.
361 		 */
362 		if (partition == -1 &&
363 		    part.type != PART_FREEBSD)
364 			goto out;
365 		/* Try to read BSD label */
366 		table = ptable_open(dev, part.end - part.start + 1,
367 		    od->sectorsize, ptblread);
368 		if (table == NULL) {
369 			DEBUG("Can't read BSD label");
370 			rc = ENXIO;
371 			goto out;
372 		}
373 		/*
374 		 * If slice contains BSD label and d_partition < 0, then
375 		 * assume the 'a' partition. Otherwise just return the
376 		 * whole MBR slice, because it can contain ZFS.
377 		 */
378 		if (partition < 0) {
379 			if (ptable_gettype(table) != PTABLE_BSD)
380 				goto out;
381 			partition = 0;
382 		}
383 		rc = ptable_getpart(table, &part, partition);
384 		if (rc != 0)
385 			goto out;
386 		dev->d_offset += part.start;
387 	}
388 out:
389 	if (table != NULL)
390 		ptable_close(table);
391 
392 	if (rc != 0) {
393 		if (od->rcnt < 1) {
394 			if (od->table != NULL)
395 				ptable_close(od->table);
396 			free(od);
397 		}
398 		DEBUG("%s could not open", disk_fmtdev(dev));
399 	} else {
400 		if ((flags & DISK_F_NOCACHE) == 0)
401 			disk_insert(dev);
402 		/* Save the slice and partition number to the dev */
403 		dev->d_slice = slice;
404 		dev->d_partition = partition;
405 		DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
406 		    (long long)dev->d_offset, od);
407 	}
408 	return (rc);
409 }
410 
411 int
disk_close(struct disk_devdesc * dev)412 disk_close(struct disk_devdesc *dev)
413 {
414 	struct open_disk *od;
415 
416 	od = (struct open_disk *)dev->d_opendata;
417 	DEBUG("%s closed => %p [%d]", disk_fmtdev(dev), od, od->rcnt);
418 	if (od->flags & DISK_F_NOCACHE) {
419 		ptable_close(od->table);
420 		free(od);
421 	}
422 	return (0);
423 }
424 
425 void
disk_cleanup(const struct devsw * d_dev)426 disk_cleanup(const struct devsw *d_dev)
427 {
428 #ifdef DISK_DEBUG
429 	struct disk_devdesc dev;
430 #endif
431 	struct dentry *entry, *tmp;
432 
433 	STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
434 		if (entry->d_dev != d_dev)
435 			continue;
436 		entry->od->rcnt--;
437 #ifdef DISK_DEBUG
438 		dev.d_dev = (struct devsw *)entry->d_dev;
439 		dev.d_unit = entry->d_unit;
440 		dev.d_slice = entry->d_slice;
441 		dev.d_partition = entry->d_partition;
442 		DEBUG("%s was freed => %p [%d]", disk_fmtdev(&dev),
443 		    entry->od, entry->od->rcnt);
444 #endif
445 		STAILQ_REMOVE(&opened_disks, entry, dentry, entry);
446 		if (entry->od->rcnt < 1) {
447 			if (entry->od->table != NULL)
448 				ptable_close(entry->od->table);
449 			free(entry->od);
450 		}
451 		free(entry);
452 	}
453 }
454 
455 char*
disk_fmtdev(struct disk_devdesc * dev)456 disk_fmtdev(struct disk_devdesc *dev)
457 {
458 	static char buf[128];
459 	char *cp;
460 
461 	cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
462 	if (dev->d_slice >= 0) {
463 #ifdef LOADER_GPT_SUPPORT
464 		if (dev->d_partition == 255) {
465 			sprintf(cp, "p%d:", dev->d_slice);
466 			return (buf);
467 		} else
468 #endif
469 #ifdef LOADER_MBR_SUPPORT
470 			cp += sprintf(cp, "s%d", dev->d_slice);
471 #endif
472 	}
473 	if (dev->d_partition >= 0)
474 		cp += sprintf(cp, "%c", dev->d_partition + 'a');
475 	strcat(cp, ":");
476 	return (buf);
477 }
478 
479 int
disk_parsedev(struct disk_devdesc * dev,const char * devspec,const char ** path)480 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
481 {
482 	int unit, slice, partition;
483 	const char *np;
484 	char *cp;
485 
486 	np = devspec;
487 	unit = slice = partition = -1;
488 	if (*np != '\0' && *np != ':') {
489 		unit = strtol(np, &cp, 10);
490 		if (cp == np)
491 			return (EUNIT);
492 #ifdef LOADER_GPT_SUPPORT
493 		if (*cp == 'p') {
494 			np = cp + 1;
495 			slice = strtol(np, &cp, 10);
496 			if (np == cp)
497 				return (ESLICE);
498 			/* we don't support nested partitions on GPT */
499 			if (*cp != '\0' && *cp != ':')
500 				return (EINVAL);
501 			partition = 255;
502 		} else
503 #endif
504 #ifdef LOADER_MBR_SUPPORT
505 		if (*cp == 's') {
506 			np = cp + 1;
507 			slice = strtol(np, &cp, 10);
508 			if (np == cp)
509 				return (ESLICE);
510 		}
511 #endif
512 		if (*cp != '\0' && *cp != ':') {
513 			partition = *cp - 'a';
514 			if (partition < 0)
515 				return (EPART);
516 			cp++;
517 		}
518 	} else
519 		return (EINVAL);
520 
521 	if (*cp != '\0' && *cp != ':')
522 		return (EINVAL);
523 	dev->d_unit = unit;
524 	dev->d_slice = slice;
525 	dev->d_partition = partition;
526 	if (path != NULL)
527 		*path = (*cp == '\0') ? cp: cp + 1;
528 	return (0);
529 }
530