]>
Commit | Line | Data |
---|---|---|
c886eb9d AM |
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 | |
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 | (cherry picked from commit 0e169215efcdb33d588ddc2267467593bbf717c9) | |
26 | --- | |
27 | libparted/arch/linux.c | 62 +++++++++++++++++++++++++++++++++++--------------- | |
28 | 1 file changed, 44 insertions(+), 18 deletions(-) | |
29 | ||
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); | |
38 | - | |
39 | +static int _device_open (PedDevice* dev, int flags); | |
40 | +static int _device_open_ro (PedDevice* dev); | |
41 | +static int _device_close (PedDevice* dev); | |
42 | ||
43 | static int | |
44 | _read_fd (int fd, char **buf) | |
45 | @@ -910,7 +912,7 @@ init_ide (PedDevice* dev) | |
46 | if (!_device_stat (dev, &dev_stat)) | |
47 | goto error; | |
48 | ||
49 | - if (!ped_device_open (dev)) | |
50 | + if (!_device_open_ro (dev)) | |
51 | goto error; | |
52 | ||
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)) | |
56 | goto error_close_dev; | |
57 | ||
58 | - ped_device_close (dev); | |
59 | + _device_close (dev); | |
60 | return 1; | |
61 | ||
62 | error_close_dev: | |
63 | - ped_device_close (dev); | |
64 | + _device_close (dev); | |
65 | error: | |
66 | return 0; | |
67 | } | |
68 | @@ -1116,7 +1118,7 @@ init_scsi (PedDevice* dev) | |
69 | char* vendor; | |
70 | char* product; | |
71 | ||
72 | - if (!ped_device_open (dev)) | |
73 | + if (!_device_open_ro (dev)) | |
74 | goto error; | |
75 | ||
76 | if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) { | |
77 | @@ -1130,7 +1132,7 @@ init_scsi (PedDevice* dev) | |
78 | goto error_close_dev; | |
79 | if (!_device_probe_geometry (dev)) | |
80 | goto error_close_dev; | |
81 | - ped_device_close (dev); | |
82 | + _device_close (dev); | |
83 | return 1; | |
84 | } | |
85 | ||
86 | @@ -1152,11 +1154,11 @@ init_scsi (PedDevice* dev) | |
87 | if (!_device_probe_geometry (dev)) | |
88 | goto error_close_dev; | |
89 | ||
90 | - ped_device_close (dev); | |
91 | + _device_close (dev); | |
92 | return 1; | |
93 | ||
94 | error_close_dev: | |
95 | - ped_device_close (dev); | |
96 | + _device_close (dev); | |
97 | error: | |
98 | return 0; | |
99 | } | |
100 | @@ -1168,7 +1170,7 @@ init_file (PedDevice* dev) | |
101 | ||
102 | if (!_device_stat (dev, &dev_stat)) | |
103 | goto error; | |
104 | - if (!ped_device_open (dev)) | |
105 | + if (!_device_open_ro (dev)) | |
106 | goto error; | |
107 | ||
108 | dev->sector_size = PED_SECTOR_SIZE_DEFAULT; | |
109 | @@ -1195,7 +1197,7 @@ init_file (PedDevice* dev) | |
110 | goto error_close_dev; | |
111 | } | |
112 | ||
113 | - ped_device_close (dev); | |
114 | + _device_close (dev); | |
115 | ||
116 | dev->bios_geom.cylinders = dev->length / 4 / 32; | |
117 | dev->bios_geom.heads = 4; | |
118 | @@ -1206,7 +1208,7 @@ init_file (PedDevice* dev) | |
119 | return 1; | |
120 | ||
121 | error_close_dev: | |
122 | - ped_device_close (dev); | |
123 | + _device_close (dev); | |
124 | error: | |
125 | return 0; | |
126 | } | |
127 | @@ -1222,7 +1224,7 @@ init_dasd (PedDevice* dev, const char* model_name) | |
128 | if (!_device_stat (dev, &dev_stat)) | |
129 | goto error; | |
130 | ||
131 | - if (!ped_device_open (dev)) | |
132 | + if (!_device_open_ro (dev)) | |
133 | goto error; | |
134 | ||
135 | LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev); | |
136 | @@ -1262,11 +1264,11 @@ init_dasd (PedDevice* dev, const char* model_name) | |
137 | ||
138 | dev->model = strdup (model_name); | |
139 | ||
140 | - ped_device_close (dev); | |
141 | + _device_close (dev); | |
142 | return 1; | |
143 | ||
144 | error_close_dev: | |
145 | - ped_device_close (dev); | |
146 | + _device_close (dev); | |
147 | error: | |
148 | return 0; | |
149 | } | |
150 | @@ -1281,7 +1283,7 @@ init_generic (PedDevice* dev, const char* model_name) | |
151 | if (!_device_stat (dev, &dev_stat)) | |
152 | goto error; | |
153 | ||
154 | - if (!ped_device_open (dev)) | |
155 | + if (!_device_open_ro (dev)) | |
156 | goto error; | |
157 | ||
158 | ped_exception_fetch_all (); | |
159 | @@ -1329,11 +1331,11 @@ init_generic (PedDevice* dev, const char* model_name) | |
160 | ||
161 | dev->model = strdup (model_name); | |
162 | ||
163 | - ped_device_close (dev); | |
164 | + _device_close (dev); | |
165 | return 1; | |
166 | ||
167 | error_close_dev: | |
168 | - ped_device_close (dev); | |
169 | + _device_close (dev); | |
170 | error: | |
171 | return 0; | |
172 | } | |
173 | @@ -1620,12 +1622,27 @@ retry: | |
174 | } | |
175 | ||
176 | static int | |
177 | +_device_open_ro (PedDevice* dev) | |
178 | +{ | |
179 | + int rc = _device_open (dev, RD_MODE); | |
180 | + if (rc) | |
181 | + dev->open_count++; | |
182 | + return rc; | |
183 | +} | |
184 | + | |
185 | +static int | |
186 | linux_open (PedDevice* dev) | |
187 | { | |
188 | + return _device_open (dev, RW_MODE); | |
189 | +} | |
190 | + | |
191 | +static int | |
192 | +_device_open (PedDevice* dev, int flags) | |
193 | +{ | |
194 | LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev); | |
195 | ||
196 | retry: | |
197 | - arch_specific->fd = open (dev->path, RW_MODE); | |
198 | + arch_specific->fd = open (dev->path, flags); | |
199 | ||
200 | if (arch_specific->fd == -1) { | |
201 | char* rw_error_msg = strerror (errno); | |
202 | @@ -1694,6 +1711,15 @@ linux_refresh_close (PedDevice* dev) | |
203 | return 1; | |
204 | } | |
205 | ||
206 | +static int | |
207 | +_device_close (PedDevice* dev) | |
208 | +{ | |
209 | + int rc = linux_close (dev); | |
210 | + if (dev->open_count > 0) | |
211 | + dev->open_count--; | |
212 | + return rc; | |
213 | +} | |
214 | + | |
215 | #if SIZEOF_OFF_T < 8 | |
216 | ||
217 | static _syscall5(int,_llseek, | |
218 | -- | |
219 | 2.4.3 | |
220 |