From: Petr Uzel Date: Thu, 26 May 2016 09:28:21 +0200 Subject: libparted: open the device RO and lazily switch to RW only if necessary References: bsc#979275 Patch-mainline: not yet Avoid useless udev events triggered by opening the device RW for purely read-only operations. Author: Michael Matz [sparschauer: Use _flush_cache() also for reads, don't switch to RW in _flush_cache() to avoid a possible recursion (bnc#989751), open partitions in _flush_cache() O_RDONLY instead of O_WRONLY] Signed-off-by: Sebastian Parschauer --- libparted/arch/linux.c | 33 ++++++++++++++++++++++++++++++--- libparted/arch/linux.h | 1 + 2 files changed, 31 insertions(+), 3 deletions(-) Index: parted-3.3/libparted/arch/linux.c =================================================================== --- parted-3.3.orig/libparted/arch/linux.c +++ parted-3.3/libparted/arch/linux.c @@ -298,6 +298,9 @@ static unsigned int _device_get_partitio 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) @@ -451,6 +454,22 @@ _is_blkext_major (int major) return _major_type_in_devices (major, "blkext"); } +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; +} + #ifdef ENABLE_DEVICE_MAPPER static int _dm_task_run_wait (struct dm_task *task, uint32_t cookie) @@ -1678,7 +1697,7 @@ _flush_cache (PedDevice* dev) if (!name) break; if (!_partition_is_mounted_by_path (name)) { - fd = open (name, WR_MODE, 0); + fd = open (name, RD_MODE, 0); if (fd > -1) { ioctl (fd, BLKFLSBUF); retry: @@ -1709,7 +1728,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 @@ -1747,6 +1766,10 @@ 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); @@ -1984,6 +2007,7 @@ _write_lastoddsector (PedDevice* dev, co PED_ASSERT(dev != NULL); PED_ASSERT(buffer != NULL); + _ensure_read_write (dev); arch_specific = LINUX_SPECIFIC (dev); @@ -2032,6 +2056,9 @@ linux_write (PedDevice* dev, const void* return 1; } + _ensure_read_write (dev); + if (count == 0) + return 1; /* We just ensured the device is open RW (see call from dasd_write()) */ 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. @@ -2570,6 +2597,7 @@ _blkpg_part_command (PedDevice* dev, str 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); Index: parted-3.3/libparted/arch/linux.h =================================================================== --- parted-3.3.orig/libparted/arch/linux.h +++ parted-3.3/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; Index: parted-3.3/libparted/labels/dasd.c =================================================================== --- parted-3.3.orig/libparted/labels/dasd.c +++ parted-3.3/libparted/labels/dasd.c @@ -637,6 +637,10 @@ dasd_write (const PedDisk* disk) return 1; } + /* Ensure the device is open RW */ + char dummy[10]; /* to avoid assertion in ped_device_write */ + ped_device_write(disk->dev, &dummy, 0, 0); + /* initialize the anchor */ fdasd_initialize_anchor(&anchor); if (fdasd_get_geometry(disk->dev, &anchor, arch_specific->fd) == 0)