Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | From d6e15a60e84c1511523aa81272b7db7a6ec441d0 Mon Sep 17 00:00:00 2001 |
| 2 | From: Ovidiu Panait <ovidiu.panait@windriver.com> |
| 3 | Date: Tue, 26 Sep 2017 08:04:58 +0000 |
| 4 | Subject: [PATCH] libparted: Use read only when probing devices on linux |
| 5 | (#1245144) |
| 6 | |
| 7 | When a device is opened for RW closing it can trigger other actions, |
| 8 | like udev scanning it for partition changes. Use read only for the |
| 9 | init_* methods and RW for actual changes to the device. |
| 10 | |
| 11 | This adds _device_open which takes mode flags as an argument and turns |
| 12 | linux_open into a wrapper for it with RW_MODE. |
| 13 | |
| 14 | _device_open_ro is added to open the device with RD_MODE and increment |
| 15 | the open_counter. This is used in the init_* functions. |
| 16 | |
| 17 | _device_close is a wrapper around linux_close that decrements the |
| 18 | open_counter and is used in the init_* functions. |
| 19 | |
| 20 | All of these changes are self-contained with no external API changes. |
| 21 | The only visible change in behavior is that when a new PedDevice is |
| 22 | created the device is opened in RO_MODE instead of RW_MODE. |
| 23 | |
| 24 | Resolves: rhbz#1245144 |
| 25 | |
| 26 | Upstream-Status: Backport |
| 27 | |
| 28 | Author: Brian C. Lane <bcl@redhat.com> |
| 29 | Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com> |
| 30 | --- |
| 31 | libparted/arch/linux.c | 62 +++++++++++++++++++++++++++++++++++--------------- |
| 32 | 1 file changed, 44 insertions(+), 18 deletions(-) |
| 33 | |
| 34 | diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c |
| 35 | index f612617..0a06a54 100644 |
| 36 | --- a/libparted/arch/linux.c |
| 37 | +++ b/libparted/arch/linux.c |
| 38 | @@ -294,7 +294,9 @@ struct blkdev_ioctl_param { |
| 39 | static char* _device_get_part_path (PedDevice const *dev, int num); |
| 40 | static int _partition_is_mounted_by_path (const char* path); |
| 41 | static unsigned int _device_get_partition_range(PedDevice const* dev); |
| 42 | - |
| 43 | +static int _device_open (PedDevice* dev, int flags); |
| 44 | +static int _device_open_ro (PedDevice* dev); |
| 45 | +static int _device_close (PedDevice* dev); |
| 46 | |
| 47 | static int |
| 48 | _read_fd (int fd, char **buf) |
| 49 | @@ -913,7 +915,7 @@ init_ide (PedDevice* dev) |
| 50 | if (!_device_stat (dev, &dev_stat)) |
| 51 | goto error; |
| 52 | |
| 53 | - if (!ped_device_open (dev)) |
| 54 | + if (!_device_open_ro (dev)) |
| 55 | goto error; |
| 56 | |
| 57 | if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) { |
| 58 | @@ -982,11 +984,11 @@ init_ide (PedDevice* dev) |
| 59 | if (!_device_probe_geometry (dev)) |
| 60 | goto error_close_dev; |
| 61 | |
| 62 | - ped_device_close (dev); |
| 63 | + _device_close (dev); |
| 64 | return 1; |
| 65 | |
| 66 | error_close_dev: |
| 67 | - ped_device_close (dev); |
| 68 | + _device_close (dev); |
| 69 | error: |
| 70 | return 0; |
| 71 | } |
| 72 | @@ -1119,7 +1121,7 @@ init_scsi (PedDevice* dev) |
| 73 | char* vendor; |
| 74 | char* product; |
| 75 | |
| 76 | - if (!ped_device_open (dev)) |
| 77 | + if (!_device_open_ro (dev)) |
| 78 | goto error; |
| 79 | |
| 80 | if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) { |
| 81 | @@ -1133,7 +1135,7 @@ init_scsi (PedDevice* dev) |
| 82 | goto error_close_dev; |
| 83 | if (!_device_probe_geometry (dev)) |
| 84 | goto error_close_dev; |
| 85 | - ped_device_close (dev); |
| 86 | + _device_close (dev); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | @@ -1155,11 +1157,11 @@ init_scsi (PedDevice* dev) |
| 91 | if (!_device_probe_geometry (dev)) |
| 92 | goto error_close_dev; |
| 93 | |
| 94 | - ped_device_close (dev); |
| 95 | + _device_close (dev); |
| 96 | return 1; |
| 97 | |
| 98 | error_close_dev: |
| 99 | - ped_device_close (dev); |
| 100 | + _device_close (dev); |
| 101 | error: |
| 102 | return 0; |
| 103 | } |
| 104 | @@ -1171,7 +1173,7 @@ init_file (PedDevice* dev) |
| 105 | |
| 106 | if (!_device_stat (dev, &dev_stat)) |
| 107 | goto error; |
| 108 | - if (!ped_device_open (dev)) |
| 109 | + if (!_device_open_ro (dev)) |
| 110 | goto error; |
| 111 | |
| 112 | dev->sector_size = PED_SECTOR_SIZE_DEFAULT; |
| 113 | @@ -1198,7 +1200,7 @@ init_file (PedDevice* dev) |
| 114 | goto error_close_dev; |
| 115 | } |
| 116 | |
| 117 | - ped_device_close (dev); |
| 118 | + _device_close (dev); |
| 119 | |
| 120 | dev->bios_geom.cylinders = dev->length / 4 / 32; |
| 121 | dev->bios_geom.heads = 4; |
| 122 | @@ -1209,7 +1211,7 @@ init_file (PedDevice* dev) |
| 123 | return 1; |
| 124 | |
| 125 | error_close_dev: |
| 126 | - ped_device_close (dev); |
| 127 | + _device_close (dev); |
| 128 | error: |
| 129 | return 0; |
| 130 | } |
| 131 | @@ -1225,7 +1227,7 @@ init_dasd (PedDevice* dev, const char* model_name) |
| 132 | if (!_device_stat (dev, &dev_stat)) |
| 133 | goto error; |
| 134 | |
| 135 | - if (!ped_device_open (dev)) |
| 136 | + if (!_device_open_ro (dev)) |
| 137 | goto error; |
| 138 | |
| 139 | LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev); |
| 140 | @@ -1265,11 +1267,11 @@ init_dasd (PedDevice* dev, const char* model_name) |
| 141 | |
| 142 | dev->model = strdup (model_name); |
| 143 | |
| 144 | - ped_device_close (dev); |
| 145 | + _device_close (dev); |
| 146 | return 1; |
| 147 | |
| 148 | error_close_dev: |
| 149 | - ped_device_close (dev); |
| 150 | + _device_close (dev); |
| 151 | error: |
| 152 | return 0; |
| 153 | } |
| 154 | @@ -1284,7 +1286,7 @@ init_generic (PedDevice* dev, const char* model_name) |
| 155 | if (!_device_stat (dev, &dev_stat)) |
| 156 | goto error; |
| 157 | |
| 158 | - if (!ped_device_open (dev)) |
| 159 | + if (!_device_open_ro (dev)) |
| 160 | goto error; |
| 161 | |
| 162 | ped_exception_fetch_all (); |
| 163 | @@ -1332,11 +1334,11 @@ init_generic (PedDevice* dev, const char* model_name) |
| 164 | |
| 165 | dev->model = strdup (model_name); |
| 166 | |
| 167 | - ped_device_close (dev); |
| 168 | + _device_close (dev); |
| 169 | return 1; |
| 170 | |
| 171 | error_close_dev: |
| 172 | - ped_device_close (dev); |
| 173 | + _device_close (dev); |
| 174 | error: |
| 175 | return 0; |
| 176 | } |
| 177 | @@ -1623,12 +1625,27 @@ retry: |
| 178 | } |
| 179 | |
| 180 | static int |
| 181 | +_device_open_ro (PedDevice* dev) |
| 182 | +{ |
| 183 | + int rc = _device_open (dev, RD_MODE); |
| 184 | + if (rc) |
| 185 | + dev->open_count++; |
| 186 | + return rc; |
| 187 | +} |
| 188 | + |
| 189 | +static int |
| 190 | linux_open (PedDevice* dev) |
| 191 | { |
| 192 | + return _device_open (dev, RW_MODE); |
| 193 | +} |
| 194 | + |
| 195 | +static int |
| 196 | +_device_open (PedDevice* dev, int flags) |
| 197 | +{ |
| 198 | LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev); |
| 199 | |
| 200 | retry: |
| 201 | - arch_specific->fd = open (dev->path, RW_MODE); |
| 202 | + arch_specific->fd = open (dev->path, flags); |
| 203 | |
| 204 | if (arch_specific->fd == -1) { |
| 205 | char* rw_error_msg = strerror (errno); |
| 206 | @@ -1697,6 +1714,15 @@ linux_refresh_close (PedDevice* dev) |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | +static int |
| 211 | +_device_close (PedDevice* dev) |
| 212 | +{ |
| 213 | + int rc = linux_close (dev); |
| 214 | + if (dev->open_count > 0) |
| 215 | + dev->open_count--; |
| 216 | + return rc; |
| 217 | +} |
| 218 | + |
| 219 | #if SIZEOF_OFF_T < 8 |
| 220 | |
| 221 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) |
| 222 | -- |
| 223 | 2.11.0 |
| 224 | |