From 22d7b6137a3b362b6230af23dc1009f4ed87a208 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Wed, 3 Mar 2010 10:44:12 +0000 Subject: [PATCH 1/2] 2010-03-03 Colin Watson * util/hostdisk.c (struct grub_util_biosdisk_data): New structure. (grub_util_biosdisk_open): Initialise disk->data. (struct linux_partition_cache): New structure. (linux_find_partition): Cache partition start positions; these are expensive to compute on every read and write. (open_device): Cache open file descriptor in disk->data, so that we don't have to reopen it and flush the buffer cache for consecutive operations on the same device. (grub_util_biosdisk_close): New function. (grub_util_biosdisk_dev): Set `close' member. * conf/common.rmk (grub_probe_SOURCES): Add kern/list.c. * conf/i386-efi.rmk (grub_setup_SOURCES): Likewise. * conf/i386-pc.rmk (grub_setup_SOURCES): Likewise. * conf/sparc64-ieee1275.rmk (grub_setup_SOURCES): Likewise. * conf/x86_64-efi.rmk (grub_setup_SOURCES): Likewise. --- ChangeLog.hostdisk-speedup | 18 +++++++ conf/common.rmk | 2 +- conf/i386-efi.rmk | 2 +- conf/i386-pc.rmk | 3 +- conf/sparc64-ieee1275.rmk | 3 +- conf/x86_64-efi.rmk | 2 +- util/hostdisk.c | 104 ++++++++++++++++++++++++++++++++----- 7 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 ChangeLog.hostdisk-speedup diff --git a/ChangeLog.hostdisk-speedup b/ChangeLog.hostdisk-speedup new file mode 100644 index 000000000..ef48978a4 --- /dev/null +++ b/ChangeLog.hostdisk-speedup @@ -0,0 +1,18 @@ +2010-03-03 Colin Watson + + * util/hostdisk.c (struct grub_util_biosdisk_data): New structure. + (grub_util_biosdisk_open): Initialise disk->data. + (struct linux_partition_cache): New structure. + (linux_find_partition): Cache partition start positions; these are + expensive to compute on every read and write. + (open_device): Cache open file descriptor in disk->data, so that we + don't have to reopen it and flush the buffer cache for consecutive + operations on the same device. + (grub_util_biosdisk_close): New function. + (grub_util_biosdisk_dev): Set `close' member. + + * conf/common.rmk (grub_probe_SOURCES): Add kern/list.c. + * conf/i386-efi.rmk (grub_setup_SOURCES): Likewise. + * conf/i386-pc.rmk (grub_setup_SOURCES): Likewise. + * conf/sparc64-ieee1275.rmk (grub_setup_SOURCES): Likewise. + * conf/x86_64-efi.rmk (grub_setup_SOURCES): Likewise. diff --git a/conf/common.rmk b/conf/common.rmk index 7effa8af3..8f63e2db8 100644 --- a/conf/common.rmk +++ b/conf/common.rmk @@ -24,7 +24,7 @@ util/grub-probe.c_DEPENDENCIES = grub_probe_init.h grub_probe_SOURCES = gnulib/progname.c util/grub-probe.c \ util/hostdisk.c util/misc.c util/getroot.c \ kern/device.c kern/disk.c kern/err.c kern/misc.c \ - kern/parser.c kern/partition.c kern/file.c \ + kern/parser.c kern/partition.c kern/file.c kern/list.c \ \ fs/affs.c fs/cpio.c fs/fat.c fs/ext2.c fs/hfs.c \ fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index c03abb429..8f159e9c0 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -21,7 +21,7 @@ util/i386/efi/grub-mkimage.c_DEPENDENCIES = Makefile # kern/err.c kern/misc.c fs/fat.c fs/ext2.c fs/xfs.c fs/affs.c \ # fs/sfs.c kern/parser.c kern/partition.c partmap/msdos.c \ # fs/ufs.c fs/ufs2.c fs/minix.c fs/hfs.c fs/jfs.c fs/hfsplus.c kern/file.c \ -# kern/fs.c kern/env.c fs/fshelp.c +# kern/fs.c kern/env.c kern/list.c fs/fshelp.c # Scripts. sbin_SCRIPTS = grub-install diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 580bfea0a..610f4791b 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -96,7 +96,8 @@ grub_setup_SOURCES = gnulib/progname.c \ util/i386/pc/grub-setup.c util/hostdisk.c \ util/misc.c util/getroot.c kern/device.c kern/disk.c \ kern/err.c kern/misc.c kern/parser.c kern/partition.c \ - kern/file.c kern/fs.c kern/env.c fs/fshelp.c \ + kern/file.c kern/fs.c kern/env.c kern/list.c \ + fs/fshelp.c \ \ fs/affs.c fs/cpio.c fs/ext2.c fs/fat.c fs/hfs.c \ fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ diff --git a/conf/sparc64-ieee1275.rmk b/conf/sparc64-ieee1275.rmk index befc7dce5..e8bdccb2d 100644 --- a/conf/sparc64-ieee1275.rmk +++ b/conf/sparc64-ieee1275.rmk @@ -70,7 +70,8 @@ util/sparc64/ieee1275/grub-setup.c_DEPENDENCIES = grub_setup_init.h grub_setup_SOURCES = util/sparc64/ieee1275/grub-setup.c util/hostdisk.c \ util/misc.c util/getroot.c kern/device.c kern/disk.c \ kern/err.c kern/misc.c kern/parser.c kern/partition.c \ - kern/file.c kern/fs.c kern/env.c fs/fshelp.c \ + kern/file.c kern/fs.c kern/env.c kern/list.c \ + fs/fshelp.c \ \ fs/affs.c fs/cpio.c fs/ext2.c fs/fat.c fs/hfs.c \ fs/hfsplus.c fs/iso9660.c fs/udf.c fs/jfs.c fs/minix.c \ diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index d5c3c24cb..e17777e0a 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -20,7 +20,7 @@ grub_mkimage_SOURCES = gnulib/progname.c util/i386/efi/grub-mkimage.c \ # kern/err.c kern/misc.c fs/fat.c fs/ext2.c fs/xfs.c fs/affs.c \ # fs/sfs.c kern/parser.c kern/partition.c partmap/msdos.c \ # fs/ufs.c fs/ufs2.c fs/minix.c fs/hfs.c fs/jfs.c fs/hfsplus.c kern/file.c \ -# kern/fs.c kern/env.c fs/fshelp.c +# kern/fs.c kern/env.c kern/list.c fs/fshelp.c # Scripts. sbin_SCRIPTS = grub-install diff --git a/util/hostdisk.c b/util/hostdisk.c index 98d3d53ae..9ece36885 100644 --- a/util/hostdisk.c +++ b/util/hostdisk.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -103,6 +104,12 @@ struct char *device; } map[256]; +struct grub_util_biosdisk_data +{ + char *dev; + int fd; +}; + #ifdef __linux__ /* Check if we have devfs support. */ static int @@ -165,6 +172,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) { int drive; struct stat st; + struct grub_util_biosdisk_data *data; drive = find_grub_drive (name); if (drive < 0) @@ -173,6 +181,9 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) disk->has_partitions = 1; disk->id = drive; + disk->data = data = xmalloc (sizeof (struct grub_util_biosdisk_data)); + data->dev = NULL; + data->fd = -1; /* Get the size. */ #if defined(__MINGW32__) @@ -254,6 +265,17 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) } #ifdef __linux__ +/* Cache of partition start sectors for each disk. */ +struct linux_partition_cache +{ + struct linux_partition_cache *next; + char *dev; + unsigned long start; + int partno; +}; + +struct linux_partition_cache *linux_partition_cache_list; + static int linux_find_partition (char *dev, unsigned long sector) { @@ -262,6 +284,7 @@ linux_find_partition (char *dev, unsigned long sector) char *p; int i; char real_dev[PATH_MAX]; + struct linux_partition_cache *cache; strcpy(real_dev, dev); @@ -281,6 +304,16 @@ linux_find_partition (char *dev, unsigned long sector) format = "%d"; } + for (cache = linux_partition_cache_list; cache; cache = cache->next) + { + if (strcmp (cache->dev, dev) == 0 && cache->start == sector) + { + sprintf (p, format, cache->partno); + strcpy (dev, real_dev); + return 1; + } + } + for (i = 1; i < 10000; i++) { int fd; @@ -301,6 +334,15 @@ linux_find_partition (char *dev, unsigned long sector) if (hdg.start == sector) { + struct linux_partition_cache *new_cache_item; + + new_cache_item = xmalloc (sizeof *new_cache_item); + new_cache_item->dev = xstrdup (dev); + new_cache_item->start = hdg.start; + new_cache_item->partno = i; + grub_list_push (GRUB_AS_LIST_P (&linux_partition_cache_list), + GRUB_AS_LIST (new_cache_item)); + strcpy (dev, real_dev); return 1; } @@ -314,6 +356,7 @@ static int open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags) { int fd; + struct grub_util_biosdisk_data *data = disk->data; #ifdef O_LARGEFILE flags |= O_LARGEFILE; @@ -340,18 +383,30 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags) && strncmp (map[disk->id].device, "/dev/", 5) == 0) is_partition = linux_find_partition (dev, disk->partition->start); - /* Open the partition. */ - grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev); - fd = open (dev, flags); - if (fd < 0) + if (data->dev && strcmp (data->dev, dev) == 0) { - grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", dev); - return -1; + grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev); + fd = data->fd; } + else + { + /* Open the partition. */ + grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev); + fd = open (dev, flags); + if (fd < 0) + { + grub_error (GRUB_ERR_BAD_DEVICE, "cannot open `%s'", dev); + return -1; + } - /* Flush the buffer cache to the physical disk. - XXX: This also empties the buffer cache. */ - ioctl (fd, BLKFLSBUF, 0); + /* Flush the buffer cache to the physical disk. + XXX: This also empties the buffer cache. */ + ioctl (fd, BLKFLSBUF, 0); + + free (data->dev); + data->dev = xstrdup (dev); + data->fd = fd; + } if (is_partition) sector -= disk->partition->start; @@ -375,7 +430,21 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags) } #endif - fd = open (map[disk->id].device, flags); + if (data->dev && strcmp (data->dev, map[disk->id].device) == 0) + { + grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev); + fd = data->fd; + } + else + { + fd = open (map[disk->id].device, flags); + if (fd >= 0) + { + free (data->dev); + data->dev = xstrdup (map[disk->id].device); + data->fd = fd; + } + } #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) if (! (sysctl_oldflags & 0x10) @@ -535,7 +604,6 @@ grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector, != (ssize_t) (size << GRUB_DISK_SECTOR_BITS)) grub_error (GRUB_ERR_READ_ERROR, "cannot read from `%s'", map[disk->id].device); - close (fd); return grub_errno; } @@ -570,17 +638,27 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector, != (ssize_t) (size << GRUB_DISK_SECTOR_BITS)) grub_error (GRUB_ERR_WRITE_ERROR, "cannot write to `%s'", map[disk->id].device); - close (fd); return grub_errno; } +static void +grub_util_biosdisk_close (struct grub_disk *disk) +{ + struct grub_util_biosdisk_data *data = disk->data; + + free (data->dev); + if (data->fd != -1) + close (data->fd); + free (data); +} + static struct grub_disk_dev grub_util_biosdisk_dev = { .name = "biosdisk", .id = GRUB_DISK_DEVICE_BIOSDISK_ID, .iterate = grub_util_biosdisk_iterate, .open = grub_util_biosdisk_open, - .close = 0, + .close = grub_util_biosdisk_close, .read = grub_util_biosdisk_read, .write = grub_util_biosdisk_write, .next = 0 From c6293875f961046e9d961db56fbf6082a5d955cc Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Wed, 3 Mar 2010 20:03:50 +0000 Subject: [PATCH 2/2] reopen device when the requested access mode changes --- util/hostdisk.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/util/hostdisk.c b/util/hostdisk.c index 9ece36885..b8f3129f0 100644 --- a/util/hostdisk.c +++ b/util/hostdisk.c @@ -107,6 +107,7 @@ struct struct grub_util_biosdisk_data { char *dev; + int access_mode; int fd; }; @@ -183,6 +184,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) disk->id = drive; disk->data = data = xmalloc (sizeof (struct grub_util_biosdisk_data)); data->dev = NULL; + data->access_mode = 0; data->fd = -1; /* Get the size. */ @@ -383,13 +385,18 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags) && strncmp (map[disk->id].device, "/dev/", 5) == 0) is_partition = linux_find_partition (dev, disk->partition->start); - if (data->dev && strcmp (data->dev, dev) == 0) + if (data->dev && strcmp (data->dev, dev) == 0 && + data->access_mode == (flags & O_ACCMODE)) { grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev); fd = data->fd; } else { + free (data->dev); + if (data->fd != -1) + close (data->fd); + /* Open the partition. */ grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev); fd = open (dev, flags); @@ -403,8 +410,8 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags) XXX: This also empties the buffer cache. */ ioctl (fd, BLKFLSBUF, 0); - free (data->dev); data->dev = xstrdup (dev); + data->access_mode = (flags & O_ACCMODE); data->fd = fd; } @@ -430,18 +437,23 @@ open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags) } #endif - if (data->dev && strcmp (data->dev, map[disk->id].device) == 0) + if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 && + data->access_mode == (flags & O_ACCMODE)) { grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev); fd = data->fd; } else { + free (data->dev); + if (data->fd != -1) + close (data->fd); + fd = open (map[disk->id].device, flags); if (fd >= 0) { - free (data->dev); data->dev = xstrdup (map[disk->id].device); + data->access_mode = (flags & O_ACCMODE); data->fd = fd; } }