2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/queue.h>
35 #include <bootstrap.h>
41 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
43 # define DEBUG(fmt, args...)
55 struct disk_devdesc *dev;
61 const struct devsw *d_dev;
68 STAILQ_ENTRY(dentry) entry;
74 static STAILQ_HEAD(, dentry) opened_disks =
75 STAILQ_HEAD_INITIALIZER(opened_disks);
78 disk_lookup(struct disk_devdesc *dev)
84 STAILQ_FOREACH(entry, &opened_disks, entry) {
85 if (entry->d_dev != dev->d_dev ||
86 entry->d_unit != dev->d_unit)
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);
105 disk_insert(struct disk_devdesc *dev)
107 struct dentry *entry;
109 entry = (struct dentry *)malloc(sizeof(struct dentry));
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;
120 entry->d_offset = dev->d_offset;
124 STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
125 DEBUG("%s cached", disk_fmtdev(dev));
129 COMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
133 command_dcachestat(int argc, char *argv[])
135 struct disk_devdesc dev;
136 struct dentry *entry;
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);
148 #endif /* DISK_DEBUG */
150 /* Convert size to a human-readable number. */
152 display_size(uint64_t size, u_int sectorsize)
157 size = size * sectorsize / 1024;
159 if (size >= 10485760000LL) {
162 } else if (size >= 10240000) {
165 } else if (size >= 10000) {
169 sprintf(buf, "%ld%cB", (long)size, unit);
174 ptblread(void *d, void *buf, size_t blocks, off_t offset)
176 struct disk_devdesc *dev;
177 struct open_disk *od;
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));
187 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
189 struct print_args *pa, bsd;
190 struct open_disk *od;
191 struct ptable *table;
195 pa = (struct print_args *)arg;
196 od = (struct open_disk *)pa->dev->d_opendata;
197 sprintf(line, " %s%s: %s", pa->prefix, pname,
198 parttype2str(part->type));
200 sprintf(line, "%-*s%s", PWIDTH, line,
201 display_size(part->end - part->start + 1,
204 if (pager_output(line))
207 if (part->type == PART_FREEBSD) {
208 /* Open slice with BSD label */
209 pa->dev->d_offset = part->start;
210 table = ptable_open(pa->dev, part->end - part->start + 1,
211 od->sectorsize, ptblread);
214 sprintf(line, " %s%s", pa->prefix, pname);
217 bsd.verbose = pa->verbose;
218 res = ptable_iterate(table, &bsd, ptable_print);
227 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
229 struct open_disk *od;
230 struct print_args pa;
232 /* Disk should be opened */
233 od = (struct open_disk *)dev->d_opendata;
236 pa.verbose = verbose;
237 return (ptable_iterate(od->table, &pa, ptable_print));
241 disk_read(struct disk_devdesc *dev, void *buf, off_t offset, u_int blocks)
243 struct open_disk *od;
246 od = (struct open_disk *)dev->d_opendata;
247 ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
248 blocks * od->sectorsize, buf, NULL);
254 disk_write(struct disk_devdesc *dev, void *buf, off_t offset, u_int blocks)
256 struct open_disk *od;
259 od = (struct open_disk *)dev->d_opendata;
260 ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
261 blocks * od->sectorsize, buf, NULL);
267 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *buf)
270 if (dev->d_dev->dv_ioctl)
271 return ((*dev->d_dev->dv_ioctl)(dev->d_opendata, cmd, buf));
277 disk_open(struct disk_devdesc *dev, off_t mediasize, u_int sectorsize,
280 struct open_disk *od;
281 struct ptable *table;
282 struct ptable_entry part;
283 int rc, slice, partition;
286 if ((flags & DISK_F_NOCACHE) == 0) {
287 rc = disk_lookup(dev);
292 * While we are reading disk metadata, make sure we do it relative
293 * to the start of the disk
297 slice = dev->d_slice;
298 partition = dev->d_partition;
301 * This entire disk was already opened and there is no
302 * need to allocate new open_disk structure and open the
303 * main partition table.
305 od = (struct open_disk *)dev->d_opendata;
306 DEBUG("%s unit %d, slice %d, partition %d => %p (cached)",
307 disk_fmtdev(dev), dev->d_unit, dev->d_slice,
308 dev->d_partition, od);
311 od = (struct open_disk *)malloc(sizeof(struct open_disk));
316 dev->d_opendata = od;
319 od->mediasize = mediasize;
320 od->sectorsize = sectorsize;
322 DEBUG("%s unit %d, slice %d, partition %d => %p",
323 disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
325 /* Determine disk layout. */
326 od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
328 if (od->table == NULL) {
329 DEBUG("Can't read partition table");
335 if (ptable_gettype(od->table) == PTABLE_BSD &&
337 /* It doesn't matter what value has d_slice */
338 rc = ptable_getpart(od->table, &part, partition);
340 dev->d_offset = part.start;
341 } else if (slice >= 0) {
342 /* Try to get information about partition */
344 rc = ptable_getbestpart(od->table, &part);
346 rc = ptable_getpart(od->table, &part, slice);
347 if (rc != 0) /* Partition doesn't exist */
349 dev->d_offset = part.start;
351 if (ptable_gettype(od->table) == PTABLE_GPT) {
353 goto out; /* Nothing more to do */
354 } else if (partition == 255) {
356 * When we try to open GPT partition, but partition
357 * table isn't GPT, reset d_partition value to -1
358 * and try to autodetect appropriate value.
363 * If d_partition < 0 and we are looking at a BSD slice,
364 * then try to read BSD label, otherwise return the
367 if (partition == -1 &&
368 part.type != PART_FREEBSD)
370 /* Try to read BSD label */
371 table = ptable_open(dev, part.end - part.start + 1,
372 od->sectorsize, ptblread);
374 DEBUG("Can't read BSD label");
379 * If slice contains BSD label and d_partition < 0, then
380 * assume the 'a' partition. Otherwise just return the
381 * whole MBR slice, because it can contain ZFS.
384 if (ptable_gettype(table) != PTABLE_BSD)
388 rc = ptable_getpart(table, &part, partition);
391 dev->d_offset += part.start;
399 if (od->table != NULL)
400 ptable_close(od->table);
403 DEBUG("%s could not open", disk_fmtdev(dev));
405 if ((flags & DISK_F_NOCACHE) == 0)
407 /* Save the slice and partition number to the dev */
408 dev->d_slice = slice;
409 dev->d_partition = partition;
410 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
411 (long long)dev->d_offset, od);
417 disk_close(struct disk_devdesc *dev)
419 struct open_disk *od;
421 od = (struct open_disk *)dev->d_opendata;
422 DEBUG("%s closed => %p [%d]", disk_fmtdev(dev), od, od->rcnt);
423 if (od->flags & DISK_F_NOCACHE) {
424 ptable_close(od->table);
431 disk_cleanup(const struct devsw *d_dev)
434 struct disk_devdesc dev;
436 struct dentry *entry, *tmp;
438 STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
439 if (entry->d_dev != d_dev)
443 dev.d_dev = (struct devsw *)entry->d_dev;
444 dev.d_unit = entry->d_unit;
445 dev.d_slice = entry->d_slice;
446 dev.d_partition = entry->d_partition;
447 DEBUG("%s was freed => %p [%d]", disk_fmtdev(&dev),
448 entry->od, entry->od->rcnt);
450 STAILQ_REMOVE(&opened_disks, entry, dentry, entry);
451 if (entry->od->rcnt < 1) {
452 if (entry->od->table != NULL)
453 ptable_close(entry->od->table);
461 disk_fmtdev(struct disk_devdesc *dev)
463 static char buf[128];
466 cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
467 if (dev->d_slice >= 0) {
468 #ifdef LOADER_GPT_SUPPORT
469 if (dev->d_partition == 255) {
470 sprintf(cp, "p%d:", dev->d_slice);
474 #ifdef LOADER_MBR_SUPPORT
475 cp += sprintf(cp, "s%d", dev->d_slice);
478 if (dev->d_partition >= 0)
479 cp += sprintf(cp, "%c", dev->d_partition + 'a');
485 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
487 int unit, slice, partition;
492 unit = slice = partition = -1;
493 if (*np != '\0' && *np != ':') {
494 unit = strtol(np, &cp, 10);
497 #ifdef LOADER_GPT_SUPPORT
500 slice = strtol(np, &cp, 10);
503 /* we don't support nested partitions on GPT */
504 if (*cp != '\0' && *cp != ':')
509 #ifdef LOADER_MBR_SUPPORT
512 slice = strtol(np, &cp, 10);
517 if (*cp != '\0' && *cp != ':') {
518 partition = *cp - 'a';
526 if (*cp != '\0' && *cp != ':')
529 dev->d_slice = slice;
530 dev->d_partition = partition;
532 *path = (*cp == '\0') ? cp: cp + 1;