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.2/libparted/arch/linux.c =================================================================== --- parted-3.2.orig/libparted/arch/linux.c +++ parted-3.2/libparted/arch/linux.c @@ -295,6 +295,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) @@ -442,6 +445,22 @@ _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; +} + #ifdef ENABLE_DEVICE_MAPPER static int _dm_task_run_wait (struct dm_task *task, uint32_t cookie) @@ -1603,7 +1622,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 > 0) { ioctl (fd, BLKFLSBUF); retry: @@ -1634,7 +1653,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 @@ -1672,6 +1691,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); @@ -1907,6 +1930,7 @@ _write_lastoddsector (PedDevice* dev, co PED_ASSERT(dev != NULL); PED_ASSERT(buffer != NULL); + _ensure_read_write (dev); arch_specific = LINUX_SPECIFIC (dev); @@ -1955,6 +1979,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. @@ -2480,6 +2507,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.2/libparted/arch/linux.h =================================================================== --- parted-3.2.orig/libparted/arch/linux.h +++ parted-3.2/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.2/libparted/labels/dasd.c =================================================================== --- parted-3.2.orig/libparted/labels/dasd.c +++ parted-3.2/libparted/labels/dasd.c @@ -639,6 +639,10 @@ dasd_write (const PedDisk* disk) if (disk_specific->format_type == 1) 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); fdasd_get_geometry(disk->dev, &anchor, arch_specific->fd);