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