From b9420b8e618c8ad988c410e4289273ac962ec918 Mon Sep 17 00:00:00 2001 From: Petr Uzel Date: Thu, 26 May 2016 09:28:21 +0200 Subject: [PATCH] libparted: open the device RO and lazily switch to RW only if necessary Avoid useless udev events triggered by opening the device RW for purely read-only operations. References: https://bugzilla.novell.com/show_bug.cgi?id=979275 Author: Michael Matz --- libparted/arch/linux.c | 33 ++++++++++++++++++++++++++++++--- libparted/arch/linux.h | 1 + 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c index 326b956..0a50452 100644 --- a/libparted/arch/linux.c +++ b/libparted/arch/linux.c @@ -294,6 +294,9 @@ static unsigned int _device_get_partition_range(PedDevice const* dev); static int _device_open (PedDevice* dev, int flags); static int _device_open_ro (PedDevice* dev); static int _device_close (PedDevice* dev); +static int linux_close (PedDevice* dev); +static void _flush_cache (PedDevice* dev); + static int _read_fd (int fd, char **buf) @@ -441,6 +444,24 @@ _is_virtblk_major (int major) return _major_type_in_devices (major, "virtblk"); } +static void +_ensure_read_write (PedDevice *dev) +{ + PED_ASSERT (dev != NULL); + LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev); + + if (arch_specific->rw) + return; + + if (!linux_close(dev)) + return; + + if (!_device_open (dev, RW_MODE)) + return; + + _flush_cache (dev); +} + #ifdef ENABLE_DEVICE_MAPPER static int _dm_task_run_wait (struct dm_task *task, uint32_t cookie) @@ -1604,6 +1625,7 @@ _flush_cache (PedDevice* dev) if (dev->read_only) return; + _ensure_read_write (dev); dev->dirty = 0; ioctl (arch_specific->fd, BLKFLSBUF); @@ -1647,7 +1669,7 @@ _device_open_ro (PedDevice* dev) static int linux_open (PedDevice* dev) { - return _device_open (dev, RW_MODE); + return _device_open (dev, RD_MODE); } static int @@ -1685,10 +1707,12 @@ retry: } } else { dev->read_only = 0; + if (flags == WR_MODE || flags == RW_MODE) + arch_specific->rw = 1; + else + arch_specific->rw = 0; } - _flush_cache (dev); - return 1; } @@ -1920,6 +1944,7 @@ _write_lastoddsector (PedDevice* dev, const void* buffer) PED_ASSERT(dev != NULL); PED_ASSERT(buffer != NULL); + _ensure_read_write (dev); arch_specific = LINUX_SPECIFIC (dev); @@ -1968,6 +1993,7 @@ linux_write (PedDevice* dev, const void* buffer, PedSector start, return 1; } + _ensure_read_write (dev); if (_get_linux_version() < KERNEL_VERSION (2,6,0)) { /* Kludge. This is necessary to read/write the last block of an odd-sized disk, until Linux 2.5.x kernel fixes. @@ -2497,6 +2523,7 @@ _blkpg_part_command (PedDevice* dev, struct blkpg_partition* part, int op) LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev); struct blkpg_ioctl_arg ioctl_arg; + _ensure_read_write (dev); ioctl_arg.op = op; ioctl_arg.flags = 0; ioctl_arg.datalen = sizeof (struct blkpg_partition); diff --git a/libparted/arch/linux.h b/libparted/arch/linux.h index 95def1f..926f7e2 100644 --- a/libparted/arch/linux.h +++ b/libparted/arch/linux.h @@ -30,6 +30,7 @@ struct _LinuxSpecific { int fd; int major; int minor; + int rw; char* dmtype; /**< device map target type */ #if defined __s390__ || defined __s390x__ unsigned int real_sector_size; -- 1.8.4.5