blob: 39107620d5466309e87fbceafaacb7bb0c3617ef [file] [log] [blame]
Brad Bishop15ae2502019-06-18 21:44:24 -04001From 843225aa9d5077bebdb08bbf9699c02aec0b83eb Mon Sep 17 00:00:00 2001
2From: "Brian C. Lane" <bcl@redhat.com>
Brad Bishopd7bf8c12018-02-25 22:55:05 -05003Date: Tue, 26 Sep 2017 08:04:58 +0000
4Subject: [PATCH] libparted: Use read only when probing devices on linux
Brad Bishop15ae2502019-06-18 21:44:24 -04005
Brad Bishopd7bf8c12018-02-25 22:55:05 -05006 (#1245144)
7
8When a device is opened for RW closing it can trigger other actions,
9like udev scanning it for partition changes. Use read only for the
10init_* methods and RW for actual changes to the device.
11
12This adds _device_open which takes mode flags as an argument and turns
13linux_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
16the open_counter. This is used in the init_* functions.
17
18_device_close is a wrapper around linux_close that decrements the
19open_counter and is used in the init_* functions.
20
21All of these changes are self-contained with no external API changes.
22The only visible change in behavior is that when a new PedDevice is
23created the device is opened in RO_MODE instead of RW_MODE.
24
25Resolves: rhbz#1245144
26
27Upstream-Status: Backport
28
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
Brad Bishop15ae2502019-06-18 21:44:24 -040030
Brad Bishopd7bf8c12018-02-25 22:55:05 -050031---
32 libparted/arch/linux.c | 62 +++++++++++++++++++++++++++++++++++---------------
33 1 file changed, 44 insertions(+), 18 deletions(-)
34
35diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
Brad Bishop15ae2502019-06-18 21:44:24 -040036index 7f12f58..4ddea58 100644
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037--- a/libparted/arch/linux.c
38+++ b/libparted/arch/linux.c
Brad Bishop15ae2502019-06-18 21:44:24 -040039@@ -292,7 +292,9 @@ struct blkdev_ioctl_param {
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 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 Bishop15ae2502019-06-18 21:44:24 -040050@@ -911,7 +913,7 @@ init_ide (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051 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 Bishop15ae2502019-06-18 21:44:24 -040059@@ -980,11 +982,11 @@ init_ide (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050060 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 Bishop15ae2502019-06-18 21:44:24 -040073@@ -1117,7 +1119,7 @@ init_scsi (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 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 Bishop15ae2502019-06-18 21:44:24 -040082@@ -1131,7 +1133,7 @@ init_scsi (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083 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 Bishop15ae2502019-06-18 21:44:24 -040091@@ -1153,11 +1155,11 @@ init_scsi (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050092 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 Bishop15ae2502019-06-18 21:44:24 -0400105@@ -1169,7 +1171,7 @@ init_file (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106
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 Bishop15ae2502019-06-18 21:44:24 -0400114@@ -1196,7 +1198,7 @@ init_file (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 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 Bishop15ae2502019-06-18 21:44:24 -0400123@@ -1207,7 +1209,7 @@ init_file (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124 return 1;
125
126 error_close_dev:
127- ped_device_close (dev);
128+ _device_close (dev);
129 error:
130 return 0;
131 }
Brad Bishop15ae2502019-06-18 21:44:24 -0400132@@ -1223,7 +1225,7 @@ init_dasd (PedDevice* dev, const char* model_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133 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 Bishop15ae2502019-06-18 21:44:24 -0400141@@ -1263,11 +1265,11 @@ init_dasd (PedDevice* dev, const char* model_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500142
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 Bishop15ae2502019-06-18 21:44:24 -0400155@@ -1282,7 +1284,7 @@ init_generic (PedDevice* dev, const char* model_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156 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 Bishop15ae2502019-06-18 21:44:24 -0400164@@ -1330,11 +1332,11 @@ init_generic (PedDevice* dev, const char* model_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500165
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 Bishop15ae2502019-06-18 21:44:24 -0400178@@ -1621,12 +1623,27 @@ retry:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 }
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 Bishop15ae2502019-06-18 21:44:24 -0400207@@ -1695,6 +1712,15 @@ linux_refresh_close (PedDevice* dev)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500208 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 Bishop15ae2502019-06-18 21:44:24 -0400222 static _syscall5(int,_llseek,