1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#include <linux/namei.h>
46#include <linux/cdev.h>
47#include <linux/fs.h>
48#include <linux/module.h>
49#include <linux/device.h>
50#include <linux/idr.h>
51#include <linux/major.h>
52#include <linux/file.h>
53#include <linux/slab.h>
54
55#include <scsi/scsi.h>
56#include <scsi/scsi_driver.h>
57#include <scsi/scsi_device.h>
58#include <scsi/scsi_ioctl.h>
59
60#include <scsi/osd_initiator.h>
61#include <scsi/osd_sec.h>
62
63#include "osd_debug.h"
64
65#ifndef TYPE_OSD
66# define TYPE_OSD 0x11
67#endif
68
69#ifndef SCSI_OSD_MAJOR
70# define SCSI_OSD_MAJOR 260
71#endif
72#define SCSI_OSD_MAX_MINOR 64
73
74static const char osd_name[] = "osd";
75static const char *osd_version_string = "open-osd 0.2.0";
76
77MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
78MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
79MODULE_LICENSE("GPL");
80MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
81MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
82
83struct osd_uld_device {
84 int minor;
85 struct device class_dev;
86 struct cdev cdev;
87 struct osd_dev od;
88 struct osd_dev_info odi;
89 struct gendisk *disk;
90};
91
92struct osd_dev_handle {
93 struct osd_dev od;
94 struct file *file;
95 struct osd_uld_device *oud;
96} ;
97
98static DEFINE_IDA(osd_minor_ida);
99
100static struct class osd_uld_class = {
101 .owner = THIS_MODULE,
102 .name = "scsi_osd",
103};
104
105
106
107
108
109static int osd_uld_open(struct inode *inode, struct file *file)
110{
111 struct osd_uld_device *oud = container_of(inode->i_cdev,
112 struct osd_uld_device, cdev);
113
114 get_device(&oud->class_dev);
115
116 file->private_data = oud;
117 OSD_DEBUG("osd_uld_open %p\n", oud);
118 return 0;
119}
120
121static int osd_uld_release(struct inode *inode, struct file *file)
122{
123 struct osd_uld_device *oud = file->private_data;
124
125 OSD_DEBUG("osd_uld_release %p\n", file->private_data);
126 file->private_data = NULL;
127 put_device(&oud->class_dev);
128 return 0;
129}
130
131
132unsigned g_test_ioctl;
133do_test_fn *g_do_test;
134
135int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
136{
137 if (g_test_ioctl)
138 return -EINVAL;
139
140 g_test_ioctl = ioctl;
141 g_do_test = do_test;
142 return 0;
143}
144EXPORT_SYMBOL(osduld_register_test);
145
146void osduld_unregister_test(unsigned ioctl)
147{
148 if (ioctl == g_test_ioctl) {
149 g_test_ioctl = 0;
150 g_do_test = NULL;
151 }
152}
153EXPORT_SYMBOL(osduld_unregister_test);
154
155static do_test_fn *_find_ioctl(unsigned cmd)
156{
157 if (g_test_ioctl == cmd)
158 return g_do_test;
159 else
160 return NULL;
161}
162
163static long osd_uld_ioctl(struct file *file, unsigned int cmd,
164 unsigned long arg)
165{
166 struct osd_uld_device *oud = file->private_data;
167 int ret;
168 do_test_fn *do_test;
169
170 do_test = _find_ioctl(cmd);
171 if (do_test)
172 ret = do_test(&oud->od, cmd, arg);
173 else {
174 OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
175 ret = -ENOIOCTLCMD;
176 }
177 return ret;
178}
179
180static const struct file_operations osd_fops = {
181 .owner = THIS_MODULE,
182 .open = osd_uld_open,
183 .release = osd_uld_release,
184 .unlocked_ioctl = osd_uld_ioctl,
185 .llseek = noop_llseek,
186};
187
188struct osd_dev *osduld_path_lookup(const char *name)
189{
190 struct osd_uld_device *oud;
191 struct osd_dev_handle *odh;
192 struct file *file;
193 int error;
194
195 if (!name || !*name) {
196 OSD_ERR("Mount with !path || !*path\n");
197 return ERR_PTR(-EINVAL);
198 }
199
200 odh = kzalloc(sizeof(*odh), GFP_KERNEL);
201 if (unlikely(!odh))
202 return ERR_PTR(-ENOMEM);
203
204 file = filp_open(name, O_RDWR, 0);
205 if (IS_ERR(file)) {
206 error = PTR_ERR(file);
207 goto free_od;
208 }
209
210 if (file->f_op != &osd_fops){
211 error = -EINVAL;
212 goto close_file;
213 }
214
215 oud = file->private_data;
216
217 odh->od = oud->od;
218 odh->file = file;
219 odh->oud = oud;
220
221 return &odh->od;
222
223close_file:
224 fput(file);
225free_od:
226 kfree(odh);
227 return ERR_PTR(error);
228}
229EXPORT_SYMBOL(osduld_path_lookup);
230
231static inline bool _the_same_or_null(const u8 *a1, unsigned a1_len,
232 const u8 *a2, unsigned a2_len)
233{
234 if (!a2_len)
235 return true;
236
237 if (a1_len != a2_len)
238 return false;
239
240 return 0 == memcmp(a1, a2, a1_len);
241}
242
243struct find_oud_t {
244 const struct osd_dev_info *odi;
245 struct device *dev;
246 struct osd_uld_device *oud;
247} ;
248
249int _mach_odi(struct device *dev, void *find_data)
250{
251 struct osd_uld_device *oud = container_of(dev, struct osd_uld_device,
252 class_dev);
253 struct find_oud_t *fot = find_data;
254 const struct osd_dev_info *odi = fot->odi;
255
256 if (_the_same_or_null(oud->odi.systemid, oud->odi.systemid_len,
257 odi->systemid, odi->systemid_len) &&
258 _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len,
259 odi->osdname, odi->osdname_len)) {
260 OSD_DEBUG("found device sysid_len=%d osdname=%d\n",
261 odi->systemid_len, odi->osdname_len);
262 fot->oud = oud;
263 return 1;
264 } else {
265 return 0;
266 }
267}
268
269
270
271
272
273
274struct osd_dev *osduld_info_lookup(const struct osd_dev_info *odi)
275{
276 struct find_oud_t find = {.odi = odi};
277
278 find.dev = class_find_device(&osd_uld_class, NULL, &find, _mach_odi);
279 if (likely(find.dev)) {
280 struct osd_dev_handle *odh = kzalloc(sizeof(*odh), GFP_KERNEL);
281
282 if (unlikely(!odh)) {
283 put_device(find.dev);
284 return ERR_PTR(-ENOMEM);
285 }
286
287 odh->od = find.oud->od;
288 odh->oud = find.oud;
289
290 return &odh->od;
291 }
292
293 return ERR_PTR(-ENODEV);
294}
295EXPORT_SYMBOL(osduld_info_lookup);
296
297void osduld_put_device(struct osd_dev *od)
298{
299 if (od && !IS_ERR(od)) {
300 struct osd_dev_handle *odh =
301 container_of(od, struct osd_dev_handle, od);
302 struct osd_uld_device *oud = odh->oud;
303
304 BUG_ON(od->scsi_device != oud->od.scsi_device);
305
306
307
308
309
310
311
312 if (odh->file) {
313 get_device(&oud->class_dev);
314 fput(odh->file);
315 }
316 put_device(&oud->class_dev);
317 kfree(odh);
318 }
319}
320EXPORT_SYMBOL(osduld_put_device);
321
322const struct osd_dev_info *osduld_device_info(struct osd_dev *od)
323{
324 struct osd_dev_handle *odh =
325 container_of(od, struct osd_dev_handle, od);
326 return &odh->oud->odi;
327}
328EXPORT_SYMBOL(osduld_device_info);
329
330bool osduld_device_same(struct osd_dev *od, const struct osd_dev_info *odi)
331{
332 struct osd_dev_handle *odh =
333 container_of(od, struct osd_dev_handle, od);
334 struct osd_uld_device *oud = odh->oud;
335
336 return (oud->odi.systemid_len == odi->systemid_len) &&
337 _the_same_or_null(oud->odi.systemid, oud->odi.systemid_len,
338 odi->systemid, odi->systemid_len) &&
339 (oud->odi.osdname_len == odi->osdname_len) &&
340 _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len,
341 odi->osdname, odi->osdname_len);
342}
343EXPORT_SYMBOL(osduld_device_same);
344
345
346
347
348
349static int __detect_osd(struct osd_uld_device *oud)
350{
351 struct scsi_device *scsi_device = oud->od.scsi_device;
352 char caps[OSD_CAP_LEN];
353 int error;
354
355
356
357
358 OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
359 oud, scsi_device, scsi_device->request_queue);
360 error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
361 if (error)
362 OSD_ERR("warning: scsi_test_unit_ready failed\n");
363
364 osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
365 if (osd_auto_detect_ver(&oud->od, caps, &oud->odi))
366 return -ENODEV;
367
368 return 0;
369}
370
371static void __remove(struct device *dev)
372{
373 struct osd_uld_device *oud = container_of(dev, struct osd_uld_device,
374 class_dev);
375 struct scsi_device *scsi_device = oud->od.scsi_device;
376
377 kfree(oud->odi.osdname);
378
379 if (oud->cdev.owner)
380 cdev_del(&oud->cdev);
381
382 osd_dev_fini(&oud->od);
383 scsi_device_put(scsi_device);
384
385 OSD_INFO("osd_remove %s\n",
386 oud->disk ? oud->disk->disk_name : NULL);
387
388 if (oud->disk)
389 put_disk(oud->disk);
390 ida_remove(&osd_minor_ida, oud->minor);
391
392 kfree(oud);
393}
394
395static int osd_probe(struct device *dev)
396{
397 struct scsi_device *scsi_device = to_scsi_device(dev);
398 struct gendisk *disk;
399 struct osd_uld_device *oud;
400 int minor;
401 int error;
402
403 if (scsi_device->type != TYPE_OSD)
404 return -ENODEV;
405
406 do {
407 if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
408 return -ENODEV;
409
410 error = ida_get_new(&osd_minor_ida, &minor);
411 } while (error == -EAGAIN);
412
413 if (error)
414 return error;
415 if (minor >= SCSI_OSD_MAX_MINOR) {
416 error = -EBUSY;
417 goto err_retract_minor;
418 }
419
420 error = -ENOMEM;
421 oud = kzalloc(sizeof(*oud), GFP_KERNEL);
422 if (NULL == oud)
423 goto err_retract_minor;
424
425 dev_set_drvdata(dev, oud);
426 oud->minor = minor;
427
428
429
430 disk = alloc_disk(1);
431 if (!disk) {
432 OSD_ERR("alloc_disk failed\n");
433 goto err_free_osd;
434 }
435 disk->major = SCSI_OSD_MAJOR;
436 disk->first_minor = oud->minor;
437 sprintf(disk->disk_name, "osd%d", oud->minor);
438 oud->disk = disk;
439
440
441
442
443 scsi_device_get(scsi_device);
444 osd_dev_init(&oud->od, scsi_device);
445
446
447 error = __detect_osd(oud);
448 if (error) {
449 OSD_ERR("osd detection failed, non-compatible OSD device\n");
450 goto err_put_disk;
451 }
452
453
454 cdev_init(&oud->cdev, &osd_fops);
455 oud->cdev.owner = THIS_MODULE;
456 error = cdev_add(&oud->cdev,
457 MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
458 if (error) {
459 OSD_ERR("cdev_add failed\n");
460 goto err_put_disk;
461 }
462
463
464 oud->class_dev.devt = oud->cdev.dev;
465 oud->class_dev.class = &osd_uld_class;
466 oud->class_dev.parent = dev;
467 oud->class_dev.release = __remove;
468 error = dev_set_name(&oud->class_dev, disk->disk_name);
469 if (error) {
470 OSD_ERR("dev_set_name failed => %d\n", error);
471 goto err_put_cdev;
472 }
473
474 error = device_register(&oud->class_dev);
475 if (error) {
476 OSD_ERR("device_register failed => %d\n", error);
477 goto err_put_cdev;
478 }
479
480 get_device(&oud->class_dev);
481
482 OSD_INFO("osd_probe %s\n", disk->disk_name);
483 return 0;
484
485err_put_cdev:
486 cdev_del(&oud->cdev);
487err_put_disk:
488 scsi_device_put(scsi_device);
489 put_disk(disk);
490err_free_osd:
491 dev_set_drvdata(dev, NULL);
492 kfree(oud);
493err_retract_minor:
494 ida_remove(&osd_minor_ida, minor);
495 return error;
496}
497
498static int osd_remove(struct device *dev)
499{
500 struct scsi_device *scsi_device = to_scsi_device(dev);
501 struct osd_uld_device *oud = dev_get_drvdata(dev);
502
503 if (!oud || (oud->od.scsi_device != scsi_device)) {
504 OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
505 dev, oud, oud ? oud->od.scsi_device : NULL,
506 scsi_device);
507 }
508
509 device_unregister(&oud->class_dev);
510
511 put_device(&oud->class_dev);
512 return 0;
513}
514
515
516
517
518
519static struct scsi_driver osd_driver = {
520 .owner = THIS_MODULE,
521 .gendrv = {
522 .name = osd_name,
523 .probe = osd_probe,
524 .remove = osd_remove,
525 }
526};
527
528static int __init osd_uld_init(void)
529{
530 int err;
531
532 err = class_register(&osd_uld_class);
533 if (err) {
534 OSD_ERR("Unable to register sysfs class => %d\n", err);
535 return err;
536 }
537
538 err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
539 SCSI_OSD_MAX_MINOR, osd_name);
540 if (err) {
541 OSD_ERR("Unable to register major %d for osd ULD => %d\n",
542 SCSI_OSD_MAJOR, err);
543 goto err_out;
544 }
545
546 err = scsi_register_driver(&osd_driver.gendrv);
547 if (err) {
548 OSD_ERR("scsi_register_driver failed => %d\n", err);
549 goto err_out_chrdev;
550 }
551
552 OSD_INFO("LOADED %s\n", osd_version_string);
553 return 0;
554
555err_out_chrdev:
556 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
557err_out:
558 class_unregister(&osd_uld_class);
559 return err;
560}
561
562static void __exit osd_uld_exit(void)
563{
564 scsi_unregister_driver(&osd_driver.gendrv);
565 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
566 class_unregister(&osd_uld_class);
567 OSD_INFO("UNLOADED %s\n", osd_version_string);
568}
569
570module_init(osd_uld_init);
571module_exit(osd_uld_exit);
572