1 From d66b197b227e1fbd4a72f002cb8b8a7ee9461062 Mon Sep 17 00:00:00 2001
2 From: "Brian C. Lane" <bcl@redhat.com>
3 Date: Thu, 6 Aug 2015 07:17:14 -0700
4 Subject: [PATCH 19/20] libparted: Use read only when probing devices on linux
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.
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.
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.
17 _device_close is a wrapper around linux_close that decrements the
18 open_counter and is used in the init_* functions.
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.
24 Resolves: rhbz#1245144
25 (cherry picked from commit 0e169215efcdb33d588ddc2267467593bbf717c9)
27 libparted/arch/linux.c | 62 +++++++++++++++++++++++++++++++++++---------------
28 1 file changed, 44 insertions(+), 18 deletions(-)
30 diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
31 index b68130b..3934a5b 100644
32 --- a/libparted/arch/linux.c
33 +++ b/libparted/arch/linux.c
34 @@ -291,7 +291,9 @@ struct blkdev_ioctl_param {
35 static char* _device_get_part_path (PedDevice const *dev, int num);
36 static int _partition_is_mounted_by_path (const char* path);
37 static unsigned int _device_get_partition_range(PedDevice const* dev);
39 +static int _device_open (PedDevice* dev, int flags);
40 +static int _device_open_ro (PedDevice* dev);
41 +static int _device_close (PedDevice* dev);
44 _read_fd (int fd, char **buf)
45 @@ -910,7 +912,7 @@ init_ide (PedDevice* dev)
46 if (!_device_stat (dev, &dev_stat))
49 - if (!ped_device_open (dev))
50 + if (!_device_open_ro (dev))
53 if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) {
54 @@ -979,11 +981,11 @@ init_ide (PedDevice* dev)
55 if (!_device_probe_geometry (dev))
58 - ped_device_close (dev);
59 + _device_close (dev);
63 - ped_device_close (dev);
64 + _device_close (dev);
68 @@ -1116,7 +1118,7 @@ init_scsi (PedDevice* dev)
72 - if (!ped_device_open (dev))
73 + if (!_device_open_ro (dev))
76 if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) {
77 @@ -1130,7 +1132,7 @@ init_scsi (PedDevice* dev)
79 if (!_device_probe_geometry (dev))
81 - ped_device_close (dev);
82 + _device_close (dev);
86 @@ -1152,11 +1154,11 @@ init_scsi (PedDevice* dev)
87 if (!_device_probe_geometry (dev))
90 - ped_device_close (dev);
91 + _device_close (dev);
95 - ped_device_close (dev);
96 + _device_close (dev);
100 @@ -1168,7 +1170,7 @@ init_file (PedDevice* dev)
102 if (!_device_stat (dev, &dev_stat))
104 - if (!ped_device_open (dev))
105 + if (!_device_open_ro (dev))
108 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
109 @@ -1195,7 +1197,7 @@ init_file (PedDevice* dev)
110 goto error_close_dev;
113 - ped_device_close (dev);
114 + _device_close (dev);
116 dev->bios_geom.cylinders = dev->length / 4 / 32;
117 dev->bios_geom.heads = 4;
118 @@ -1206,7 +1208,7 @@ init_file (PedDevice* dev)
122 - ped_device_close (dev);
123 + _device_close (dev);
127 @@ -1222,7 +1224,7 @@ init_dasd (PedDevice* dev, const char* model_name)
128 if (!_device_stat (dev, &dev_stat))
131 - if (!ped_device_open (dev))
132 + if (!_device_open_ro (dev))
135 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
136 @@ -1262,11 +1264,11 @@ init_dasd (PedDevice* dev, const char* model_name)
138 dev->model = strdup (model_name);
140 - ped_device_close (dev);
141 + _device_close (dev);
145 - ped_device_close (dev);
146 + _device_close (dev);
150 @@ -1281,7 +1283,7 @@ init_generic (PedDevice* dev, const char* model_name)
151 if (!_device_stat (dev, &dev_stat))
154 - if (!ped_device_open (dev))
155 + if (!_device_open_ro (dev))
158 ped_exception_fetch_all ();
159 @@ -1329,11 +1331,11 @@ init_generic (PedDevice* dev, const char* model_name)
161 dev->model = strdup (model_name);
163 - ped_device_close (dev);
164 + _device_close (dev);
168 - ped_device_close (dev);
169 + _device_close (dev);
173 @@ -1620,12 +1622,27 @@ retry:
177 +_device_open_ro (PedDevice* dev)
179 + int rc = _device_open (dev, RD_MODE);
186 linux_open (PedDevice* dev)
188 + return _device_open (dev, RW_MODE);
192 +_device_open (PedDevice* dev, int flags)
194 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
197 - arch_specific->fd = open (dev->path, RW_MODE);
198 + arch_specific->fd = open (dev->path, flags);
200 if (arch_specific->fd == -1) {
201 char* rw_error_msg = strerror (errno);
202 @@ -1694,6 +1711,15 @@ linux_refresh_close (PedDevice* dev)
207 +_device_close (PedDevice* dev)
209 + int rc = linux_close (dev);
210 + if (dev->open_count > 0)
217 static _syscall5(int,_llseek,