loader: remove open_disk cache

As we provide the disk size verification and correction via disk_ioctl
and disk state provided by disk_open(), we can not share the partition
state in disk_devdesc structure. Also the sharing does make a lot of sense
with ufs, as only one partition is open at any given time, but zfs pools
do keep the disk devices open.

To make sure we do get the correct information about the open device,
just remove the cache.

Reviewed by:	allanjude, smh
Approved by:	allanjude (mentor)
Differential Revision:	https://reviews.freebsd.org/D9757
This commit is contained in:
Toomas Soome 2017-03-16 12:04:43 +00:00
parent 11f791c606
commit b91aad35da
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=315408
9 changed files with 63 additions and 241 deletions

View File

@ -48,8 +48,6 @@ struct open_disk {
uint64_t mediasize;
uint64_t entrysize;
u_int sectorsize;
u_int flags;
int rcnt;
};
struct print_args {
@ -58,96 +56,6 @@ struct print_args {
int verbose;
};
struct dentry {
const struct devsw *d_dev;
int d_unit;
int d_slice;
int d_partition;
struct open_disk *od;
uint64_t d_offset;
STAILQ_ENTRY(dentry) entry;
#ifdef DISK_DEBUG
uint32_t count;
#endif
};
static STAILQ_HEAD(, dentry) opened_disks =
STAILQ_HEAD_INITIALIZER(opened_disks);
static int
disk_lookup(struct disk_devdesc *dev)
{
struct dentry *entry;
int rc;
rc = ENOENT;
STAILQ_FOREACH(entry, &opened_disks, entry) {
if (entry->d_dev != dev->d_dev ||
entry->d_unit != dev->d_unit)
continue;
dev->d_opendata = entry->od;
if (entry->d_slice == dev->d_slice &&
entry->d_partition == dev->d_partition) {
dev->d_offset = entry->d_offset;
DEBUG("%s offset %lld", disk_fmtdev(dev),
(long long)dev->d_offset);
#ifdef DISK_DEBUG
entry->count++;
#endif
return (0);
}
rc = EAGAIN;
}
return (rc);
}
static void
disk_insert(struct disk_devdesc *dev)
{
struct dentry *entry;
entry = (struct dentry *)malloc(sizeof(struct dentry));
if (entry == NULL) {
DEBUG("no memory");
return;
}
entry->d_dev = dev->d_dev;
entry->d_unit = dev->d_unit;
entry->d_slice = dev->d_slice;
entry->d_partition = dev->d_partition;
entry->od = (struct open_disk *)dev->d_opendata;
entry->od->rcnt++;
entry->d_offset = dev->d_offset;
#ifdef DISK_DEBUG
entry->count = 1;
#endif
STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
DEBUG("%s cached", disk_fmtdev(dev));
}
#ifdef DISK_DEBUG
COMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
command_dcachestat);
static int
command_dcachestat(int argc, char *argv[])
{
struct disk_devdesc dev;
struct dentry *entry;
STAILQ_FOREACH(entry, &opened_disks, entry) {
dev.d_dev = (struct devsw *)entry->d_dev;
dev.d_unit = entry->d_unit;
dev.d_slice = entry->d_slice;
dev.d_partition = entry->d_partition;
printf("%s %d => %p [%d]\n", disk_fmtdev(&dev), entry->count,
entry->od, entry->od->rcnt);
}
return (CMD_OK);
}
#endif /* DISK_DEBUG */
/* Convert size to a human-readable number. */
static char *
display_size(uint64_t size, u_int sectorsize)
@ -187,6 +95,7 @@ ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
static int
ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
{
struct disk_devdesc dev;
struct print_args *pa, bsd;
struct open_disk *od;
struct ptable *table;
@ -207,17 +116,24 @@ ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
res = 0;
if (part->type == PART_FREEBSD) {
/* Open slice with BSD label */
pa->dev->d_offset = part->start;
table = ptable_open(pa->dev, part->end - part->start + 1,
od->sectorsize, ptblread);
if (table == NULL)
return 0;
sprintf(line, " %s%s", pa->prefix, pname);
bsd.dev = pa->dev;
bsd.prefix = line;
bsd.verbose = pa->verbose;
res = ptable_iterate(table, &bsd, ptable_print);
ptable_close(table);
dev.d_dev = pa->dev->d_dev;
dev.d_unit = pa->dev->d_unit;
dev.d_slice = part->index;
dev.d_partition = -1;
if (disk_open(&dev, part->end - part->start + 1,
od->sectorsize) == 0) {
table = ptable_open(&dev, part->end - part->start + 1,
od->sectorsize, ptblread);
if (table != NULL) {
sprintf(line, " %s%s", pa->prefix, pname);
bsd.dev = pa->dev;
bsd.prefix = line;
bsd.verbose = pa->verbose;
res = ptable_iterate(table, &bsd, ptable_print);
ptable_close(table);
}
disk_close(&dev);
}
}
return (res);
@ -290,8 +206,7 @@ disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
}
int
disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
u_int flags)
disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
{
struct open_disk *od;
struct ptable *table;
@ -299,11 +214,6 @@ disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
int rc, slice, partition;
rc = 0;
if ((flags & DISK_F_NOCACHE) == 0) {
rc = disk_lookup(dev);
if (rc == 0)
return (0);
}
/*
* While we are reading disk metadata, make sure we do it relative
* to the start of the disk
@ -312,30 +222,15 @@ disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
table = NULL;
slice = dev->d_slice;
partition = dev->d_partition;
if (rc == EAGAIN) {
/*
* This entire disk was already opened and there is no
* need to allocate new open_disk structure and open the
* main partition table.
*/
od = (struct open_disk *)dev->d_opendata;
DEBUG("%s unit %d, slice %d, partition %d => %p (cached)",
disk_fmtdev(dev), dev->d_unit, dev->d_slice,
dev->d_partition, od);
goto opened;
} else {
od = (struct open_disk *)malloc(sizeof(struct open_disk));
if (od == NULL) {
DEBUG("no memory");
return (ENOMEM);
}
dev->d_opendata = od;
od->rcnt = 0;
od->entrysize = 0;
od = (struct open_disk *)malloc(sizeof(struct open_disk));
if (od == NULL) {
DEBUG("no memory");
return (ENOMEM);
}
dev->d_opendata = od;
od->entrysize = 0;
od->mediasize = mediasize;
od->sectorsize = sectorsize;
od->flags = flags;
DEBUG("%s unit %d, slice %d, partition %d => %p",
disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
@ -355,8 +250,7 @@ disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
if (mediasize > od->mediasize) {
od->mediasize = mediasize;
}
opened:
rc = 0;
if (ptable_gettype(od->table) == PTABLE_BSD &&
partition >= 0) {
/* It doesn't matter what value has d_slice */
@ -424,15 +318,11 @@ disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize,
ptable_close(table);
if (rc != 0) {
if (od->rcnt < 1) {
if (od->table != NULL)
ptable_close(od->table);
free(od);
}
if (od->table != NULL)
ptable_close(od->table);
free(od);
DEBUG("%s could not open", disk_fmtdev(dev));
} else {
if ((flags & DISK_F_NOCACHE) == 0)
disk_insert(dev);
/* Save the slice and partition number to the dev */
dev->d_slice = slice;
dev->d_partition = partition;
@ -448,44 +338,12 @@ disk_close(struct disk_devdesc *dev)
struct open_disk *od;
od = (struct open_disk *)dev->d_opendata;
DEBUG("%s closed => %p [%d]", disk_fmtdev(dev), od, od->rcnt);
if (od->flags & DISK_F_NOCACHE) {
ptable_close(od->table);
free(od);
}
DEBUG("%s closed => %p", disk_fmtdev(dev), od);
ptable_close(od->table);
free(od);
return (0);
}
void
disk_cleanup(const struct devsw *d_dev)
{
#ifdef DISK_DEBUG
struct disk_devdesc dev;
#endif
struct dentry *entry, *tmp;
STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
if (entry->d_dev != d_dev)
continue;
entry->od->rcnt--;
#ifdef DISK_DEBUG
dev.d_dev = (struct devsw *)entry->d_dev;
dev.d_unit = entry->d_unit;
dev.d_slice = entry->d_slice;
dev.d_partition = entry->d_partition;
DEBUG("%s was freed => %p [%d]", disk_fmtdev(&dev),
entry->od, entry->od->rcnt);
#endif
STAILQ_REMOVE(&opened_disks, entry, dentry, entry);
if (entry->od->rcnt < 1) {
if (entry->od->table != NULL)
ptable_close(entry->od->table);
free(entry->od);
}
free(entry);
}
}
char*
disk_fmtdev(struct disk_devdesc *dev)
{

View File

@ -78,6 +78,9 @@
* the device's strategy method.
*/
#ifndef _DISK_H
#define _DISK_H
struct disk_devdesc
{
struct devsw *d_dev;
@ -97,23 +100,18 @@ enum disk_ioctl {
/*
* Parse disk metadata and initialise dev->d_offset.
*/
extern int disk_open(struct disk_devdesc *dev, uint64_t mediasize,
u_int sectorsize, u_int flags);
#define DISK_F_NOCACHE 0x0001 /* Do not use metadata caching */
extern int disk_close(struct disk_devdesc *dev);
extern void disk_cleanup(const struct devsw *d_dev);
extern int disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *buf);
extern int disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset,
u_int blocks);
extern int disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset,
u_int blocks);
extern int ptblread(void *d, void *buf, size_t blocks, uint64_t offset);
extern int disk_open(struct disk_devdesc *, uint64_t, u_int);
extern int disk_close(struct disk_devdesc *);
extern int disk_ioctl(struct disk_devdesc *, u_long, void *);
extern int disk_read(struct disk_devdesc *, void *, uint64_t, u_int);
extern int disk_write(struct disk_devdesc *, void *, uint64_t, u_int);
extern int ptblread(void *, void *, size_t, uint64_t);
/*
* Print information about slices on a disk.
*/
extern int disk_print(struct disk_devdesc *dev, char *prefix, int verbose);
extern char* disk_fmtdev(struct disk_devdesc *dev);
extern int disk_parsedev(struct disk_devdesc *dev, const char *devspec,
const char **path);
extern int disk_print(struct disk_devdesc *, char *, int);
extern char* disk_fmtdev(struct disk_devdesc *);
extern int disk_parsedev(struct disk_devdesc *, const char *, const char **);
#endif /* _DISK_H */

View File

@ -635,8 +635,7 @@ efipart_print_common(struct devsw *dev, pdinfo_list_t *pdlist, int verbose)
pd_dev.d_opendata = blkio;
ret = disk_open(&pd_dev, blkio->Media->BlockSize *
(blkio->Media->LastBlock + 1),
blkio->Media->BlockSize,
blkio->Media->RemovableMedia? DISK_F_NOCACHE: 0);
blkio->Media->BlockSize);
if (ret == 0) {
ret = disk_print(&pd_dev, line, verbose);
disk_close(&pd_dev);
@ -726,8 +725,7 @@ efipart_open(struct open_file *f, ...)
if (dev->d_dev->dv_type == DEVT_DISK) {
return (disk_open(dev,
blkio->Media->BlockSize * (blkio->Media->LastBlock + 1),
blkio->Media->BlockSize,
blkio->Media->RemovableMedia? DISK_F_NOCACHE: 0));
blkio->Media->BlockSize));
}
return (0);
}

View File

@ -137,7 +137,6 @@ static int bd_open(struct open_file *f, ...);
static int bd_close(struct open_file *f);
static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
static int bd_print(int verbose);
static void bd_cleanup(void);
#ifdef LOADER_GELI_SUPPORT
static enum isgeli {
@ -160,7 +159,7 @@ struct devsw biosdisk = {
bd_close,
bd_ioctl,
bd_print,
bd_cleanup
NULL
};
/*
@ -231,13 +230,6 @@ bd_init(void)
return(0);
}
static void
bd_cleanup(void)
{
disk_cleanup(&biosdisk);
}
/*
* Try to detect a device supported by the legacy int13 BIOS
*/
@ -364,9 +356,7 @@ bd_print(int verbose)
dev.d_partition = -1;
if (disk_open(&dev,
bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
bdinfo[i].bd_sectorsize,
(bdinfo[i].bd_flags & BD_FLOPPY) ?
DISK_F_NOCACHE: 0) == 0) {
bdinfo[i].bd_sectorsize) == 0) {
snprintf(line, sizeof(line), " disk%d", i);
ret = disk_print(&dev, line, verbose);
disk_close(&dev);
@ -421,8 +411,7 @@ bd_open(struct open_file *f, ...)
disk.d_partition = -1;
disk.d_offset = 0;
if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
BD(dev).bd_sectorsize, (BD(dev).bd_flags & BD_FLOPPY) ?
DISK_F_NOCACHE: 0) == 0) {
BD(dev).bd_sectorsize) == 0) {
if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
size /= BD(dev).bd_sectorsize;
@ -433,8 +422,7 @@ bd_open(struct open_file *f, ...)
}
err = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
BD(dev).bd_sectorsize, (BD(dev).bd_flags & BD_FLOPPY) ?
DISK_F_NOCACHE: 0);
BD(dev).bd_sectorsize);
#ifdef LOADER_GELI_SUPPORT
static char gelipw[GELI_PW_MAXLEN];
@ -954,8 +942,7 @@ bd_getdev(struct i386_devdesc *d)
if (biosdev == -1) /* not a BIOS device */
return(-1);
if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
BD(dev).bd_sectorsize,(BD(dev).bd_flags & BD_FLOPPY) ?
DISK_F_NOCACHE: 0) != 0) /* oops, not a viable device */
BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */
return (-1);
else
disk_close(dev);

View File

@ -44,7 +44,6 @@ __FBSDID("$FreeBSD$");
static int beri_cfi_disk_init(void);
static int beri_cfi_disk_open(struct open_file *, ...);
static int beri_cfi_disk_close(struct open_file *);
static void beri_cfi_disk_cleanup(void);
static int beri_cfi_disk_strategy(void *, int, daddr_t, size_t,
char *, size_t *);
static int beri_cfi_disk_print(int);
@ -58,7 +57,7 @@ struct devsw beri_cfi_disk = {
.dv_close = beri_cfi_disk_close,
.dv_ioctl = noioctl,
.dv_print = beri_cfi_disk_print,
.dv_cleanup = beri_cfi_disk_cleanup,
.dv_cleanup = NULL,
};
static int
@ -100,7 +99,7 @@ beri_cfi_disk_open(struct open_file *f, ...)
if (dev->d_unit != 0)
return (EIO);
return (disk_open(dev, cfi_get_mediasize(), cfi_get_sectorsize(), 0));
return (disk_open(dev, cfi_get_mediasize(), cfi_get_sectorsize()));
}
static int
@ -131,8 +130,7 @@ beri_cfi_disk_print(int verbose)
dev.d_unit = 0;
dev.d_slice = -1;
dev.d_partition = -1;
if (disk_open(&dev, cfi_get_mediasize(),
cfi_get_sectorsize(), 0) == 0) {
if (disk_open(&dev, cfi_get_mediasize(), cfi_get_sectorsize()) == 0) {
snprintf(line, sizeof(line), " cfi%d", 0);
ret = disk_print(&dev, line, verbose);
disk_close(&dev);
@ -140,10 +138,3 @@ beri_cfi_disk_print(int verbose)
return (ret);
}
static void
beri_cfi_disk_cleanup(void)
{
disk_cleanup(&beri_cfi_disk);
}

View File

@ -44,7 +44,6 @@ __FBSDID("$FreeBSD$");
static int beri_sdcard_disk_init(void);
static int beri_sdcard_disk_open(struct open_file *, ...);
static int beri_sdcard_disk_close(struct open_file *);
static void beri_sdcard_disk_cleanup(void);
static int beri_sdcard_disk_strategy(void *, int, daddr_t, size_t,
char *, size_t *);
static int beri_sdcard_disk_print(int);
@ -58,7 +57,7 @@ struct devsw beri_sdcard_disk = {
.dv_close = beri_sdcard_disk_close,
.dv_ioctl = noioctl,
.dv_print = beri_sdcard_disk_print,
.dv_cleanup = beri_sdcard_disk_cleanup,
.dv_cleanup = NULL,
};
static int
@ -106,7 +105,7 @@ beri_sdcard_disk_open(struct open_file *f, ...)
if (dev->d_unit != 0)
return (EIO);
return (disk_open(dev, altera_sdcard_get_mediasize(),
altera_sdcard_get_sectorsize(), 0));
altera_sdcard_get_sectorsize()));
}
static int
@ -138,17 +137,10 @@ beri_sdcard_disk_print(int verbose)
dev.d_slice = -1;
dev.d_partition = -1;
if (disk_open(&dev, altera_sdcard_get_mediasize(),
altera_sdcard_get_sectorsize(), 0) == 0) {
altera_sdcard_get_sectorsize()) == 0) {
snprintf(line, sizeof(line), " sdcard%d", 0);
ret = disk_print(&dev, line, verbose);
disk_close(&dev);
}
return (ret);
}
static void
beri_sdcard_disk_cleanup(void)
{
disk_cleanup(&beri_sdcard_disk);
}

View File

@ -139,7 +139,6 @@ stor_cleanup(void)
for (i = 0; i < stor_info_no; i++)
if (stor_info[i].opened > 0)
ub_dev_close(stor_info[i].handle);
disk_cleanup(&uboot_storage);
}
static int
@ -203,7 +202,7 @@ stor_opendev(struct disk_devdesc *dev)
SI(dev).opened++;
}
return (disk_open(dev, SI(dev).blocks * SI(dev).bsize,
SI(dev).bsize, 0));
SI(dev).bsize));
}
static int

View File

@ -116,7 +116,7 @@ umass_disk_open_sub(struct disk_devdesc *dev)
if (usb_msc_read_capacity(umass_uaa.device, 0, &nblock, &blocksize) != 0)
return (EINVAL);
return (disk_open(dev, ((uint64_t)nblock + 1) * (uint64_t)blocksize, blocksize, 0));
return (disk_open(dev, ((uint64_t)nblock + 1) * (uint64_t)blocksize, blocksize));
}
static int
@ -208,7 +208,6 @@ umass_disk_print(int verbose)
static void
umass_disk_cleanup(void)
{
disk_cleanup(&umass_disk);
usb_uninit();
}

View File

@ -141,7 +141,7 @@ userdisk_print(int verbose)
dev.d_slice = -1;
dev.d_partition = -1;
if (disk_open(&dev, ud_info[i].mediasize,
ud_info[i].sectorsize, 0) == 0) {
ud_info[i].sectorsize) == 0) {
snprintf(line, sizeof(line), " disk%d", i);
ret = disk_print(&dev, line, verbose);
disk_close(&dev);
@ -171,7 +171,7 @@ userdisk_open(struct open_file *f, ...)
if (ud_info[dev->d_unit].ud_bcache == NULL)
ud_info[dev->d_unit].ud_bcache = bcache_allocate();
return (disk_open(dev, ud_info[dev->d_unit].mediasize,
ud_info[dev->d_unit].sectorsize, 0));
ud_info[dev->d_unit].sectorsize));
}
static int