]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/device_handler/scsi_dh.c
dm-mpath, scsi_dh: don't let dm detach device handlers
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / device_handler / scsi_dh.c
CommitLineData
a6a8d9f8
CS
1/*
2 * SCSI device handler infrastruture.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright IBM Corporation, 2007
19 * Authors:
20 * Chandra Seetharaman <sekharan@us.ibm.com>
21 * Mike Anderson <andmike@linux.vnet.ibm.com>
22 */
23
5a0e3ad6 24#include <linux/slab.h>
acf3368f 25#include <linux/module.h>
a6a8d9f8
CS
26#include <scsi/scsi_dh.h>
27#include "../scsi_priv.h"
28
29static DEFINE_SPINLOCK(list_lock);
30static LIST_HEAD(scsi_dh_list);
31
32static struct scsi_device_handler *get_device_handler(const char *name)
33{
34 struct scsi_device_handler *tmp, *found = NULL;
35
36 spin_lock(&list_lock);
37 list_for_each_entry(tmp, &scsi_dh_list, list) {
765cbc6d 38 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
a6a8d9f8
CS
39 found = tmp;
40 break;
41 }
42 }
43 spin_unlock(&list_lock);
44 return found;
45}
46
6c3633d0
HR
47/*
48 * device_handler_match_function - Match a device handler to a device
49 * @sdev - SCSI device to be tested
50 *
51 * Tests @sdev against the match function of all registered device_handler.
52 * Returns the found device handler or NULL if not found.
53 */
54static struct scsi_device_handler *
55device_handler_match_function(struct scsi_device *sdev)
56{
57 struct scsi_device_handler *tmp_dh, *found_dh = NULL;
58
59 spin_lock(&list_lock);
60 list_for_each_entry(tmp_dh, &scsi_dh_list, list) {
61 if (tmp_dh->match && tmp_dh->match(sdev)) {
62 found_dh = tmp_dh;
63 break;
64 }
65 }
66 spin_unlock(&list_lock);
67 return found_dh;
68}
69
7c32c7a2
HR
70/*
71 * device_handler_match - Attach a device handler to a device
72 * @scsi_dh - The device handler to match against or NULL
73 * @sdev - SCSI device to be tested against @scsi_dh
74 *
75 * Tests @sdev against the device handler @scsi_dh or against
76 * all registered device_handler if @scsi_dh == NULL.
77 * Returns the found device handler or NULL if not found.
78 */
79static struct scsi_device_handler *
80device_handler_match(struct scsi_device_handler *scsi_dh,
81 struct scsi_device *sdev)
82{
6c3633d0 83 struct scsi_device_handler *found_dh;
7c32c7a2 84
6c3633d0 85 found_dh = device_handler_match_function(sdev);
7c32c7a2 86
940d7faa
PJ
87 if (scsi_dh && found_dh != scsi_dh)
88 found_dh = NULL;
7c32c7a2
HR
89
90 return found_dh;
a6a8d9f8
CS
91}
92
93/*
765cbc6d
HR
94 * scsi_dh_handler_attach - Attach a device handler to a device
95 * @sdev - SCSI device the device handler should attach to
96 * @scsi_dh - The device handler to attach
97 */
98static int scsi_dh_handler_attach(struct scsi_device *sdev,
99 struct scsi_device_handler *scsi_dh)
100{
1d520328 101 struct scsi_dh_data *d;
765cbc6d 102
1f12ffa5
CH
103 if (!try_module_get(scsi_dh->module))
104 return -EINVAL;
27c888f0 105
1d520328
CH
106 d = scsi_dh->attach(sdev);
107 if (IS_ERR(d)) {
108 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%ld)\n",
109 scsi_dh->name, PTR_ERR(d));
1f12ffa5 110 module_put(scsi_dh->module);
1d520328 111 return PTR_ERR(d);
6c10db72 112 }
1f12ffa5 113
1d520328 114 d->scsi_dh = scsi_dh;
1d520328
CH
115 d->sdev = sdev;
116
117 spin_lock_irq(sdev->request_queue->queue_lock);
118 sdev->scsi_dh_data = d;
119 spin_unlock_irq(sdev->request_queue->queue_lock);
120 return 0;
765cbc6d
HR
121}
122
1bab0de0
CH
123/*
124 * scsi_dh_handler_detach - Detach a device handler from a device
125 * @sdev - SCSI device the device handler should be detached from
126 */
127static void scsi_dh_handler_detach(struct scsi_device *sdev)
6c10db72 128{
1bab0de0 129 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
27c888f0 130 struct scsi_device_handler *scsi_dh = scsi_dh_data->scsi_dh;
1d520328 131
28072ad5
MC
132 scsi_dh->detach(sdev);
133
1d520328
CH
134 spin_lock_irq(sdev->request_queue->queue_lock);
135 sdev->scsi_dh_data = NULL;
136 spin_unlock_irq(sdev->request_queue->queue_lock);
27c888f0 137
1d520328 138 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", scsi_dh->name);
27c888f0 139 module_put(scsi_dh->module);
6c10db72
CS
140}
141
4c05ae52
HR
142/*
143 * Functions for sysfs attribute 'dh_state'
144 */
145static ssize_t
146store_dh_state(struct device *dev, struct device_attribute *attr,
147 const char *buf, size_t count)
148{
149 struct scsi_device *sdev = to_scsi_device(dev);
150 struct scsi_device_handler *scsi_dh;
151 int err = -EINVAL;
152
6bc8d2a0
HR
153 if (sdev->sdev_state == SDEV_CANCEL ||
154 sdev->sdev_state == SDEV_DEL)
155 return -ENODEV;
156
4c05ae52
HR
157 if (!sdev->scsi_dh_data) {
158 /*
159 * Attach to a device handler
160 */
161 if (!(scsi_dh = get_device_handler(buf)))
162 return err;
163 err = scsi_dh_handler_attach(sdev, scsi_dh);
164 } else {
165 scsi_dh = sdev->scsi_dh_data->scsi_dh;
166 if (!strncmp(buf, "detach", 6)) {
167 /*
168 * Detach from a device handler
169 */
1bab0de0 170 scsi_dh_handler_detach(sdev);
4c05ae52
HR
171 err = 0;
172 } else if (!strncmp(buf, "activate", 8)) {
173 /*
174 * Activate a device handler
175 */
176 if (scsi_dh->activate)
3ae31f6a 177 err = scsi_dh->activate(sdev, NULL, NULL);
4c05ae52
HR
178 else
179 err = 0;
180 }
181 }
182
183 return err<0?err:count;
184}
185
186static ssize_t
187show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
188{
189 struct scsi_device *sdev = to_scsi_device(dev);
190
191 if (!sdev->scsi_dh_data)
192 return snprintf(buf, 20, "detached\n");
193
194 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
195}
196
197static struct device_attribute scsi_dh_state_attr =
198 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
199 store_dh_state);
200
201/*
202 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
203 */
204static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
205{
206 struct scsi_device *sdev;
207 int err;
208
209 if (!scsi_is_sdev_device(dev))
210 return 0;
211
212 sdev = to_scsi_device(dev);
213
214 err = device_create_file(&sdev->sdev_gendev,
215 &scsi_dh_state_attr);
216
217 return 0;
218}
219
220/*
221 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
222 */
223static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
224{
225 struct scsi_device *sdev;
226
227 if (!scsi_is_sdev_device(dev))
228 return 0;
229
230 sdev = to_scsi_device(dev);
231
232 device_remove_file(&sdev->sdev_gendev,
233 &scsi_dh_state_attr);
234
235 return 0;
236}
237
765cbc6d
HR
238/*
239 * scsi_dh_notifier - notifier chain callback
240 */
241static int scsi_dh_notifier(struct notifier_block *nb,
242 unsigned long action, void *data)
243{
244 struct device *dev = data;
245 struct scsi_device *sdev;
246 int err = 0;
7c32c7a2 247 struct scsi_device_handler *devinfo = NULL;
765cbc6d
HR
248
249 if (!scsi_is_sdev_device(dev))
250 return 0;
251
252 sdev = to_scsi_device(dev);
a6a8d9f8 253
765cbc6d 254 if (action == BUS_NOTIFY_ADD_DEVICE) {
5917290c
CS
255 err = device_create_file(dev, &scsi_dh_state_attr);
256 /* don't care about err */
7c32c7a2 257 devinfo = device_handler_match(NULL, sdev);
5917290c
CS
258 if (devinfo)
259 err = scsi_dh_handler_attach(sdev, devinfo);
765cbc6d 260 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
4c05ae52 261 device_remove_file(dev, &scsi_dh_state_attr);
1bab0de0
CH
262 if (sdev->scsi_dh_data)
263 scsi_dh_handler_detach(sdev);
765cbc6d 264 }
765cbc6d 265 return err;
a6a8d9f8 266}
a6a8d9f8 267
765cbc6d
HR
268/*
269 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
270 */
271static int scsi_dh_notifier_add(struct device *dev, void *data)
272{
273 struct scsi_device_handler *scsi_dh = data;
274 struct scsi_device *sdev;
275
276 if (!scsi_is_sdev_device(dev))
277 return 0;
278
279 if (!get_device(dev))
280 return 0;
281
282 sdev = to_scsi_device(dev);
283
284 if (device_handler_match(scsi_dh, sdev))
285 scsi_dh_handler_attach(sdev, scsi_dh);
286
287 put_device(dev);
288
289 return 0;
290}
291
292/*
293 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
294 */
a6a8d9f8
CS
295static int scsi_dh_notifier_remove(struct device *dev, void *data)
296{
297 struct scsi_device_handler *scsi_dh = data;
765cbc6d
HR
298 struct scsi_device *sdev;
299
300 if (!scsi_is_sdev_device(dev))
301 return 0;
302
303 if (!get_device(dev))
304 return 0;
305
306 sdev = to_scsi_device(dev);
307
1bab0de0
CH
308 if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh == scsi_dh)
309 scsi_dh_handler_detach(sdev);
765cbc6d
HR
310
311 put_device(dev);
a6a8d9f8 312
a6a8d9f8
CS
313 return 0;
314}
315
765cbc6d
HR
316/*
317 * scsi_register_device_handler - register a device handler personality
318 * module.
319 * @scsi_dh - device handler to be registered.
320 *
321 * Returns 0 on success, -EBUSY if handler already registered.
322 */
323int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
324{
940d7faa 325
765cbc6d
HR
326 if (get_device_handler(scsi_dh->name))
327 return -EBUSY;
328
1f12ffa5
CH
329 if (!scsi_dh->attach || !scsi_dh->detach)
330 return -EINVAL;
331
765cbc6d
HR
332 spin_lock(&list_lock);
333 list_add(&scsi_dh->list, &scsi_dh_list);
334 spin_unlock(&list_lock);
940d7faa 335
765cbc6d
HR
336 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
337 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
338
339 return SCSI_DH_OK;
340}
341EXPORT_SYMBOL_GPL(scsi_register_device_handler);
342
a6a8d9f8
CS
343/*
344 * scsi_unregister_device_handler - register a device handler personality
345 * module.
346 * @scsi_dh - device handler to be unregistered.
347 *
348 * Returns 0 on success, -ENODEV if handler not registered.
349 */
350int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
351{
7c32c7a2 352
765cbc6d
HR
353 if (!get_device_handler(scsi_dh->name))
354 return -ENODEV;
a6a8d9f8
CS
355
356 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
765cbc6d
HR
357 scsi_dh_notifier_remove);
358
a6a8d9f8
CS
359 spin_lock(&list_lock);
360 list_del(&scsi_dh->list);
361 spin_unlock(&list_lock);
765cbc6d 362 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
a6a8d9f8 363
765cbc6d 364 return SCSI_DH_OK;
a6a8d9f8
CS
365}
366EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
367
368/*
369 * scsi_dh_activate - activate the path associated with the scsi_device
370 * corresponding to the given request queue.
3ae31f6a
CS
371 * Returns immediately without waiting for activation to be completed.
372 * @q - Request queue that is associated with the scsi_device to be
373 * activated.
374 * @fn - Function to be called upon completion of the activation.
375 * Function fn is called with data (below) and the error code.
376 * Function fn may be called from the same calling context. So,
377 * do not hold the lock in the caller which may be needed in fn.
378 * @data - data passed to the function fn upon completion.
379 *
a6a8d9f8 380 */
3ae31f6a 381int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
a6a8d9f8
CS
382{
383 int err = 0;
384 unsigned long flags;
385 struct scsi_device *sdev;
386 struct scsi_device_handler *scsi_dh = NULL;
0b839357 387 struct device *dev = NULL;
a6a8d9f8
CS
388
389 spin_lock_irqsave(q->queue_lock, flags);
390 sdev = q->queuedata;
a18a920c
MB
391 if (!sdev) {
392 spin_unlock_irqrestore(q->queue_lock, flags);
393 err = SCSI_DH_NOSYS;
394 if (fn)
395 fn(data, err);
396 return err;
397 }
398
399 if (sdev->scsi_dh_data)
a6a8d9f8 400 scsi_dh = sdev->scsi_dh_data->scsi_dh;
0b839357
MS
401 dev = get_device(&sdev->sdev_gendev);
402 if (!scsi_dh || !dev ||
db422318
MH
403 sdev->sdev_state == SDEV_CANCEL ||
404 sdev->sdev_state == SDEV_DEL)
a6a8d9f8 405 err = SCSI_DH_NOSYS;
db422318
MH
406 if (sdev->sdev_state == SDEV_OFFLINE)
407 err = SCSI_DH_DEV_OFFLINED;
a6a8d9f8
CS
408 spin_unlock_irqrestore(q->queue_lock, flags);
409
db422318
MH
410 if (err) {
411 if (fn)
412 fn(data, err);
0b839357 413 goto out;
db422318 414 }
a6a8d9f8
CS
415
416 if (scsi_dh->activate)
3ae31f6a 417 err = scsi_dh->activate(sdev, fn, data);
0b839357
MS
418out:
419 put_device(dev);
a6a8d9f8
CS
420 return err;
421}
422EXPORT_SYMBOL_GPL(scsi_dh_activate);
423
18ee70c9
CS
424/*
425 * scsi_dh_set_params - set the parameters for the device as per the
426 * string specified in params.
427 * @q - Request queue that is associated with the scsi_device for
428 * which the parameters to be set.
429 * @params - parameters in the following format
430 * "no_of_params\0param1\0param2\0param3\0...\0"
431 * for example, string for 2 parameters with value 10 and 21
432 * is specified as "2\010\021\0".
433 */
434int scsi_dh_set_params(struct request_queue *q, const char *params)
435{
436 int err = -SCSI_DH_NOSYS;
437 unsigned long flags;
438 struct scsi_device *sdev;
439 struct scsi_device_handler *scsi_dh = NULL;
440
441 spin_lock_irqsave(q->queue_lock, flags);
442 sdev = q->queuedata;
443 if (sdev && sdev->scsi_dh_data)
444 scsi_dh = sdev->scsi_dh_data->scsi_dh;
445 if (scsi_dh && scsi_dh->set_params && get_device(&sdev->sdev_gendev))
446 err = 0;
447 spin_unlock_irqrestore(q->queue_lock, flags);
448
449 if (err)
450 return err;
451 err = scsi_dh->set_params(sdev, params);
452 put_device(&sdev->sdev_gendev);
453 return err;
454}
455EXPORT_SYMBOL_GPL(scsi_dh_set_params);
456
a6a8d9f8
CS
457/*
458 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
459 * the given name. FALSE(0) otherwise.
460 * @name - name of the device handler.
461 */
462int scsi_dh_handler_exist(const char *name)
463{
464 return (get_device_handler(name) != NULL);
465}
466EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
467
ae11b1b3 468/*
2a9ab40f 469 * scsi_dh_attach - Attach device handler
7e8a74b1
MS
470 * @q - Request queue that is associated with the scsi_device
471 * the handler should be attached to
ae11b1b3
HR
472 * @name - name of the handler to attach
473 */
474int scsi_dh_attach(struct request_queue *q, const char *name)
475{
476 unsigned long flags;
477 struct scsi_device *sdev;
478 struct scsi_device_handler *scsi_dh;
479 int err = 0;
480
481 scsi_dh = get_device_handler(name);
482 if (!scsi_dh)
483 return -EINVAL;
484
485 spin_lock_irqsave(q->queue_lock, flags);
486 sdev = q->queuedata;
487 if (!sdev || !get_device(&sdev->sdev_gendev))
488 err = -ENODEV;
489 spin_unlock_irqrestore(q->queue_lock, flags);
490
1bab0de0
CH
491 if (err)
492 return err;
ae11b1b3
HR
493
494 if (sdev->scsi_dh_data) {
1bab0de0
CH
495 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
496 err = -EBUSY;
497 goto out_put_device;
ae11b1b3 498 }
1bab0de0
CH
499
500 err = scsi_dh_handler_attach(sdev, scsi_dh);
501
502out_put_device:
ae11b1b3 503 put_device(&sdev->sdev_gendev);
1bab0de0 504 return err;
ae11b1b3 505}
1bab0de0 506EXPORT_SYMBOL_GPL(scsi_dh_attach);
ae11b1b3 507
7e8a74b1
MS
508/*
509 * scsi_dh_attached_handler_name - Get attached device handler's name
510 * @q - Request queue that is associated with the scsi_device
511 * that may have a device handler attached
512 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
513 *
514 * Returns name of attached handler, NULL if no handler is attached.
515 * Caller must take care to free the returned string.
516 */
517const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
518{
519 unsigned long flags;
520 struct scsi_device *sdev;
521 const char *handler_name = NULL;
522
523 spin_lock_irqsave(q->queue_lock, flags);
524 sdev = q->queuedata;
525 if (!sdev || !get_device(&sdev->sdev_gendev))
526 sdev = NULL;
527 spin_unlock_irqrestore(q->queue_lock, flags);
528
529 if (!sdev)
530 return NULL;
531
532 if (sdev->scsi_dh_data)
533 handler_name = kstrdup(sdev->scsi_dh_data->scsi_dh->name, gfp);
534
535 put_device(&sdev->sdev_gendev);
536 return handler_name;
537}
538EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
539
765cbc6d
HR
540static struct notifier_block scsi_dh_nb = {
541 .notifier_call = scsi_dh_notifier
542};
543
544static int __init scsi_dh_init(void)
545{
546 int r;
547
548 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
549
4c05ae52
HR
550 if (!r)
551 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
552 scsi_dh_sysfs_attr_add);
553
765cbc6d
HR
554 return r;
555}
556
557static void __exit scsi_dh_exit(void)
558{
4c05ae52
HR
559 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
560 scsi_dh_sysfs_attr_remove);
765cbc6d
HR
561 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
562}
563
564module_init(scsi_dh_init);
565module_exit(scsi_dh_exit);
566
a6a8d9f8
CS
567MODULE_DESCRIPTION("SCSI device handler");
568MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
569MODULE_LICENSE("GPL");