2 * osd_uld.c - OSD Upper Layer Driver
4 * A Linux driver module that registers as a SCSI ULD and probes
5 * for OSD type SCSI devices.
6 * It's main function is to export osd devices to in-kernel users like
7 * osdfs and pNFS-objects-LD. It also provides one ioctl for running
10 * Copyright (C) 2008 Panasas Inc. All rights reserved.
13 * Boaz Harrosh <bharrosh@panasas.com>
14 * Benny Halevy <bhalevy@panasas.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the Panasas company nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <linux/namei.h>
46 #include <linux/cdev.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>
55 #include <scsi/scsi.h>
56 #include <scsi/scsi_driver.h>
57 #include <scsi/scsi_device.h>
58 #include <scsi/scsi_ioctl.h>
60 #include <scsi/osd_initiator.h>
61 #include <scsi/osd_sec.h>
63 #include "osd_debug.h"
66 # define TYPE_OSD 0x11
69 #ifndef SCSI_OSD_MAJOR
70 # define SCSI_OSD_MAJOR 260
72 #define SCSI_OSD_MAX_MINOR MINORMASK
74 static const char osd_name[] = "osd";
75 static const char *osd_version_string = "open-osd 0.2.1";
77 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
78 MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
79 MODULE_LICENSE("GPL");
80 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
81 MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
83 struct osd_uld_device {
85 struct device class_dev;
88 struct osd_dev_info odi;
92 struct osd_dev_handle {
95 struct osd_uld_device *oud;
98 static DEFINE_IDA(osd_minor_ida);
101 * scsi sysfs attribute operations
103 static ssize_t osdname_show(struct device *dev, struct device_attribute *attr,
106 struct osd_uld_device *ould = container_of(dev, struct osd_uld_device,
108 return sprintf(buf, "%s\n", ould->odi.osdname);
111 static ssize_t systemid_show(struct device *dev, struct device_attribute *attr,
114 struct osd_uld_device *ould = container_of(dev, struct osd_uld_device,
117 memcpy(buf, ould->odi.systemid, ould->odi.systemid_len);
118 return ould->odi.systemid_len;
121 static struct device_attribute osd_uld_attrs[] = {
122 __ATTR(osdname, S_IRUGO, osdname_show, NULL),
123 __ATTR(systemid, S_IRUGO, systemid_show, NULL),
127 static struct class osd_uld_class = {
128 .owner = THIS_MODULE,
130 .dev_attrs = osd_uld_attrs,
134 * Char Device operations
137 static int osd_uld_open(struct inode *inode, struct file *file)
139 struct osd_uld_device *oud = container_of(inode->i_cdev,
140 struct osd_uld_device, cdev);
142 get_device(&oud->class_dev);
143 /* cache osd_uld_device on file handle */
144 file->private_data = oud;
145 OSD_DEBUG("osd_uld_open %p\n", oud);
149 static int osd_uld_release(struct inode *inode, struct file *file)
151 struct osd_uld_device *oud = file->private_data;
153 OSD_DEBUG("osd_uld_release %p\n", file->private_data);
154 file->private_data = NULL;
155 put_device(&oud->class_dev);
159 /* FIXME: Only one vector for now */
160 unsigned g_test_ioctl;
161 do_test_fn *g_do_test;
163 int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
168 g_test_ioctl = ioctl;
172 EXPORT_SYMBOL(osduld_register_test);
174 void osduld_unregister_test(unsigned ioctl)
176 if (ioctl == g_test_ioctl) {
181 EXPORT_SYMBOL(osduld_unregister_test);
183 static do_test_fn *_find_ioctl(unsigned cmd)
185 if (g_test_ioctl == cmd)
191 static long osd_uld_ioctl(struct file *file, unsigned int cmd,
194 struct osd_uld_device *oud = file->private_data;
198 do_test = _find_ioctl(cmd);
200 ret = do_test(&oud->od, cmd, arg);
202 OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
208 static const struct file_operations osd_fops = {
209 .owner = THIS_MODULE,
210 .open = osd_uld_open,
211 .release = osd_uld_release,
212 .unlocked_ioctl = osd_uld_ioctl,
213 .llseek = noop_llseek,
216 struct osd_dev *osduld_path_lookup(const char *name)
218 struct osd_uld_device *oud;
219 struct osd_dev_handle *odh;
223 if (!name || !*name) {
224 OSD_ERR("Mount with !path || !*path\n");
225 return ERR_PTR(-EINVAL);
228 odh = kzalloc(sizeof(*odh), GFP_KERNEL);
230 return ERR_PTR(-ENOMEM);
232 file = filp_open(name, O_RDWR, 0);
234 error = PTR_ERR(file);
238 if (file->f_op != &osd_fops){
243 oud = file->private_data;
255 return ERR_PTR(error);
257 EXPORT_SYMBOL(osduld_path_lookup);
259 static inline bool _the_same_or_null(const u8 *a1, unsigned a1_len,
260 const u8 *a2, unsigned a2_len)
262 if (!a2_len) /* User string is Empty means don't care */
265 if (a1_len != a2_len)
268 return 0 == memcmp(a1, a2, a1_len);
271 static int _match_odi(struct device *dev, const void *find_data)
273 struct osd_uld_device *oud = container_of(dev, struct osd_uld_device,
275 const struct osd_dev_info *odi = find_data;
277 if (_the_same_or_null(oud->odi.systemid, oud->odi.systemid_len,
278 odi->systemid, odi->systemid_len) &&
279 _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len,
280 odi->osdname, odi->osdname_len)) {
281 OSD_DEBUG("found device sysid_len=%d osdname=%d\n",
282 odi->systemid_len, odi->osdname_len);
289 /* osduld_info_lookup - Loop through all devices, return the requested osd_dev.
291 * if @odi->systemid_len and/or @odi->osdname_len are zero, they act as a don't
292 * care. .e.g if they're both zero /dev/osd0 is returned.
294 struct osd_dev *osduld_info_lookup(const struct osd_dev_info *odi)
296 struct device *dev = class_find_device(&osd_uld_class, NULL, odi, _match_odi);
298 struct osd_dev_handle *odh = kzalloc(sizeof(*odh), GFP_KERNEL);
299 struct osd_uld_device *oud = container_of(dev,
300 struct osd_uld_device, class_dev);
302 if (unlikely(!odh)) {
304 return ERR_PTR(-ENOMEM);
313 return ERR_PTR(-ENODEV);
315 EXPORT_SYMBOL(osduld_info_lookup);
317 void osduld_put_device(struct osd_dev *od)
319 if (od && !IS_ERR(od)) {
320 struct osd_dev_handle *odh =
321 container_of(od, struct osd_dev_handle, od);
322 struct osd_uld_device *oud = odh->oud;
324 BUG_ON(od->scsi_device != oud->od.scsi_device);
326 /* If scsi has released the device (logout), and exofs has last
327 * reference on oud it will be freed by above osd_uld_release
328 * within fput below. But this will oops in cdev_release which
329 * is called after the fops->release. A get_/put_ pair makes
330 * sure we have a cdev for the duration of fput
333 get_device(&oud->class_dev);
336 put_device(&oud->class_dev);
340 EXPORT_SYMBOL(osduld_put_device);
342 const struct osd_dev_info *osduld_device_info(struct osd_dev *od)
344 struct osd_dev_handle *odh =
345 container_of(od, struct osd_dev_handle, od);
346 return &odh->oud->odi;
348 EXPORT_SYMBOL(osduld_device_info);
350 bool osduld_device_same(struct osd_dev *od, const struct osd_dev_info *odi)
352 struct osd_dev_handle *odh =
353 container_of(od, struct osd_dev_handle, od);
354 struct osd_uld_device *oud = odh->oud;
356 return (oud->odi.systemid_len == odi->systemid_len) &&
357 _the_same_or_null(oud->odi.systemid, oud->odi.systemid_len,
358 odi->systemid, odi->systemid_len) &&
359 (oud->odi.osdname_len == odi->osdname_len) &&
360 _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len,
361 odi->osdname, odi->osdname_len);
363 EXPORT_SYMBOL(osduld_device_same);
366 * Scsi Device operations
369 static int __detect_osd(struct osd_uld_device *oud)
371 struct scsi_device *scsi_device = oud->od.scsi_device;
372 char caps[OSD_CAP_LEN];
375 /* sending a test_unit_ready as first command seems to be needed
378 OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
379 oud, scsi_device, scsi_device->request_queue);
380 error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
382 OSD_ERR("warning: scsi_test_unit_ready failed\n");
384 osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
385 if (osd_auto_detect_ver(&oud->od, caps, &oud->odi))
391 static void __remove(struct device *dev)
393 struct osd_uld_device *oud = container_of(dev, struct osd_uld_device,
395 struct scsi_device *scsi_device = oud->od.scsi_device;
397 kfree(oud->odi.osdname);
400 cdev_del(&oud->cdev);
402 osd_dev_fini(&oud->od);
403 scsi_device_put(scsi_device);
405 OSD_INFO("osd_remove %s\n",
406 oud->disk ? oud->disk->disk_name : NULL);
410 ida_remove(&osd_minor_ida, oud->minor);
415 static int osd_probe(struct device *dev)
417 struct scsi_device *scsi_device = to_scsi_device(dev);
418 struct gendisk *disk;
419 struct osd_uld_device *oud;
423 if (scsi_device->type != TYPE_OSD)
427 if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
430 error = ida_get_new(&osd_minor_ida, &minor);
431 } while (error == -EAGAIN);
435 if (minor >= SCSI_OSD_MAX_MINOR) {
437 goto err_retract_minor;
441 oud = kzalloc(sizeof(*oud), GFP_KERNEL);
443 goto err_retract_minor;
445 dev_set_drvdata(dev, oud);
448 /* allocate a disk and set it up */
449 /* FIXME: do we need this since sg has already done that */
450 disk = alloc_disk(1);
452 OSD_ERR("alloc_disk failed\n");
455 disk->major = SCSI_OSD_MAJOR;
456 disk->first_minor = oud->minor;
457 sprintf(disk->disk_name, "osd%d", oud->minor);
460 /* hold one more reference to the scsi_device that will get released
461 * in __release, in case a logout is happening while fs is mounted
463 scsi_device_get(scsi_device);
464 osd_dev_init(&oud->od, scsi_device);
466 /* Detect the OSD Version */
467 error = __detect_osd(oud);
469 OSD_ERR("osd detection failed, non-compatible OSD device\n");
473 /* init the char-device for communication with user-mode */
474 cdev_init(&oud->cdev, &osd_fops);
475 oud->cdev.owner = THIS_MODULE;
476 error = cdev_add(&oud->cdev,
477 MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
479 OSD_ERR("cdev_add failed\n");
483 /* class device member */
484 oud->class_dev.devt = oud->cdev.dev;
485 oud->class_dev.class = &osd_uld_class;
486 oud->class_dev.parent = dev;
487 oud->class_dev.release = __remove;
488 error = dev_set_name(&oud->class_dev, disk->disk_name);
490 OSD_ERR("dev_set_name failed => %d\n", error);
494 error = device_register(&oud->class_dev);
496 OSD_ERR("device_register failed => %d\n", error);
500 get_device(&oud->class_dev);
502 OSD_INFO("osd_probe %s\n", disk->disk_name);
506 cdev_del(&oud->cdev);
508 scsi_device_put(scsi_device);
511 dev_set_drvdata(dev, NULL);
514 ida_remove(&osd_minor_ida, minor);
518 static int osd_remove(struct device *dev)
520 struct scsi_device *scsi_device = to_scsi_device(dev);
521 struct osd_uld_device *oud = dev_get_drvdata(dev);
523 if (!oud || (oud->od.scsi_device != scsi_device)) {
524 OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
525 dev, oud, oud ? oud->od.scsi_device : NULL,
529 device_unregister(&oud->class_dev);
531 put_device(&oud->class_dev);
536 * Global driver and scsi registration
539 static struct scsi_driver osd_driver = {
540 .owner = THIS_MODULE,
544 .remove = osd_remove,
548 static int __init osd_uld_init(void)
552 err = class_register(&osd_uld_class);
554 OSD_ERR("Unable to register sysfs class => %d\n", err);
558 err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
559 SCSI_OSD_MAX_MINOR, osd_name);
561 OSD_ERR("Unable to register major %d for osd ULD => %d\n",
562 SCSI_OSD_MAJOR, err);
566 err = scsi_register_driver(&osd_driver.gendrv);
568 OSD_ERR("scsi_register_driver failed => %d\n", err);
572 OSD_INFO("LOADED %s\n", osd_version_string);
576 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
578 class_unregister(&osd_uld_class);
582 static void __exit osd_uld_exit(void)
584 scsi_unregister_driver(&osd_driver.gendrv);
585 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
586 class_unregister(&osd_uld_class);
587 OSD_INFO("UNLOADED %s\n", osd_version_string);
590 module_init(osd_uld_init);
591 module_exit(osd_uld_exit);