@@ -86,6 +86,9 @@ struct iscsi_internal {
86
86
struct transport_container session_cont ;
87
87
};
88
88
89
+ static DEFINE_IDR (iscsi_ep_idr );
90
+ static DEFINE_MUTEX (iscsi_ep_idr_mutex );
91
+
89
92
static atomic_t iscsi_session_nr ; /* sysfs session id for next new session */
90
93
91
94
static struct workqueue_struct * iscsi_conn_cleanup_workq ;
@@ -168,6 +171,11 @@ struct device_attribute dev_attr_##_prefix##_##_name = \
168
171
static void iscsi_endpoint_release (struct device * dev )
169
172
{
170
173
struct iscsi_endpoint * ep = iscsi_dev_to_endpoint (dev );
174
+
175
+ mutex_lock (& iscsi_ep_idr_mutex );
176
+ idr_remove (& iscsi_ep_idr , ep -> id );
177
+ mutex_unlock (& iscsi_ep_idr_mutex );
178
+
171
179
kfree (ep );
172
180
}
173
181
@@ -180,7 +188,7 @@ static ssize_t
180
188
show_ep_handle (struct device * dev , struct device_attribute * attr , char * buf )
181
189
{
182
190
struct iscsi_endpoint * ep = iscsi_dev_to_endpoint (dev );
183
- return sysfs_emit (buf , "%llu \n" , ( unsigned long long ) ep -> id );
191
+ return sysfs_emit (buf , "%d \n" , ep -> id );
184
192
}
185
193
static ISCSI_ATTR (ep , handle , S_IRUGO , show_ep_handle , NULL) ;
186
194
@@ -193,48 +201,32 @@ static struct attribute_group iscsi_endpoint_group = {
193
201
.attrs = iscsi_endpoint_attrs ,
194
202
};
195
203
196
- #define ISCSI_MAX_EPID -1
197
-
198
- static int iscsi_match_epid (struct device * dev , const void * data )
199
- {
200
- struct iscsi_endpoint * ep = iscsi_dev_to_endpoint (dev );
201
- const uint64_t * epid = data ;
202
-
203
- return * epid == ep -> id ;
204
- }
205
-
206
204
struct iscsi_endpoint *
207
205
iscsi_create_endpoint (int dd_size )
208
206
{
209
- struct device * dev ;
210
207
struct iscsi_endpoint * ep ;
211
- uint64_t id ;
212
- int err ;
213
-
214
- for (id = 1 ; id < ISCSI_MAX_EPID ; id ++ ) {
215
- dev = class_find_device (& iscsi_endpoint_class , NULL , & id ,
216
- iscsi_match_epid );
217
- if (!dev )
218
- break ;
219
- else
220
- put_device (dev );
221
- }
222
- if (id == ISCSI_MAX_EPID ) {
223
- printk (KERN_ERR "Too many connections. Max supported %u\n" ,
224
- ISCSI_MAX_EPID - 1 );
225
- return NULL ;
226
- }
208
+ int err , id ;
227
209
228
210
ep = kzalloc (sizeof (* ep ) + dd_size , GFP_KERNEL );
229
211
if (!ep )
230
212
return NULL ;
231
213
214
+ mutex_lock (& iscsi_ep_idr_mutex );
215
+ id = idr_alloc (& iscsi_ep_idr , ep , 0 , -1 , GFP_NOIO );
216
+ if (id < 0 ) {
217
+ mutex_unlock (& iscsi_ep_idr_mutex );
218
+ printk (KERN_ERR "Could not allocate endpoint ID. Error %d.\n" ,
219
+ id );
220
+ goto free_ep ;
221
+ }
222
+ mutex_unlock (& iscsi_ep_idr_mutex );
223
+
232
224
ep -> id = id ;
233
225
ep -> dev .class = & iscsi_endpoint_class ;
234
- dev_set_name (& ep -> dev , "ep-%llu" , ( unsigned long long ) id );
226
+ dev_set_name (& ep -> dev , "ep-%d" , id );
235
227
err = device_register (& ep -> dev );
236
228
if (err )
237
- goto free_ep ;
229
+ goto free_id ;
238
230
239
231
err = sysfs_create_group (& ep -> dev .kobj , & iscsi_endpoint_group );
240
232
if (err )
@@ -248,6 +240,10 @@ iscsi_create_endpoint(int dd_size)
248
240
device_unregister (& ep -> dev );
249
241
return NULL ;
250
242
243
+ free_id :
244
+ mutex_lock (& iscsi_ep_idr_mutex );
245
+ idr_remove (& iscsi_ep_idr , id );
246
+ mutex_unlock (& iscsi_ep_idr_mutex );
251
247
free_ep :
252
248
kfree (ep );
253
249
return NULL ;
@@ -275,14 +271,17 @@ EXPORT_SYMBOL_GPL(iscsi_put_endpoint);
275
271
*/
276
272
struct iscsi_endpoint * iscsi_lookup_endpoint (u64 handle )
277
273
{
278
- struct device * dev ;
274
+ struct iscsi_endpoint * ep ;
279
275
280
- dev = class_find_device ( & iscsi_endpoint_class , NULL , & handle ,
281
- iscsi_match_epid );
282
- if (!dev )
283
- return NULL ;
276
+ mutex_lock ( & iscsi_ep_idr_mutex );
277
+ ep = idr_find ( & iscsi_ep_idr , handle );
278
+ if (!ep )
279
+ goto unlock ;
284
280
285
- return iscsi_dev_to_endpoint (dev );
281
+ get_device (& ep -> dev );
282
+ unlock :
283
+ mutex_unlock (& iscsi_ep_idr_mutex );
284
+ return ep ;
286
285
}
287
286
EXPORT_SYMBOL_GPL (iscsi_lookup_endpoint );
288
287
0 commit comments