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>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_driver.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_ioctl.h>
58 #include <scsi/osd_initiator.h>
59 #include <scsi/osd_sec.h>
61 #include "osd_debug.h"
64 # define TYPE_OSD 0x11
67 #ifndef SCSI_OSD_MAJOR
68 # define SCSI_OSD_MAJOR 260
70 #define SCSI_OSD_MAX_MINOR 64
72 static const char osd_name[] = "osd";
73 static const char *osd_version_string = "open-osd 0.1.0";
74 const char osd_symlink[] = "scsi_osd";
76 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
77 MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
78 MODULE_LICENSE("GPL");
79 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
80 MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
82 struct osd_uld_device {
88 struct device *class_member;
91 static void __uld_get(struct osd_uld_device *oud);
92 static void __uld_put(struct osd_uld_device *oud);
95 * Char Device operations
98 static int osd_uld_open(struct inode *inode, struct file *file)
100 struct osd_uld_device *oud = container_of(inode->i_cdev,
101 struct osd_uld_device, cdev);
104 /* cache osd_uld_device on file handle */
105 file->private_data = oud;
106 OSD_DEBUG("osd_uld_open %p\n", oud);
110 static int osd_uld_release(struct inode *inode, struct file *file)
112 struct osd_uld_device *oud = file->private_data;
114 OSD_DEBUG("osd_uld_release %p\n", file->private_data);
115 file->private_data = NULL;
120 /* FIXME: Only one vector for now */
121 unsigned g_test_ioctl;
122 do_test_fn *g_do_test;
124 int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
129 g_test_ioctl = ioctl;
133 EXPORT_SYMBOL(osduld_register_test);
135 void osduld_unregister_test(unsigned ioctl)
137 if (ioctl == g_test_ioctl) {
142 EXPORT_SYMBOL(osduld_unregister_test);
144 static do_test_fn *_find_ioctl(unsigned cmd)
146 if (g_test_ioctl == cmd)
152 static long osd_uld_ioctl(struct file *file, unsigned int cmd,
155 struct osd_uld_device *oud = file->private_data;
159 do_test = _find_ioctl(cmd);
161 ret = do_test(&oud->od, cmd, arg);
163 OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
169 static const struct file_operations osd_fops = {
170 .owner = THIS_MODULE,
171 .open = osd_uld_open,
172 .release = osd_uld_release,
173 .unlocked_ioctl = osd_uld_ioctl,
176 struct osd_dev *osduld_path_lookup(const char *path)
181 struct osd_uld_device *uninitialized_var(oud);
184 if (!path || !*path) {
185 OSD_ERR("Mount with !path || !*path\n");
186 return ERR_PTR(-EINVAL);
189 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
191 OSD_ERR("path_lookup of %s faild=>%d\n", path, error);
192 return ERR_PTR(error);
195 inode = nd.path.dentry->d_inode;
196 error = -EINVAL; /* Not the right device e.g osd_uld_device */
197 if (!S_ISCHR(inode->i_mode)) {
198 OSD_DEBUG("!S_ISCHR()\n");
202 cdev = inode->i_cdev;
204 OSD_ERR("Before mounting an OSD Based filesystem\n");
205 OSD_ERR(" user-mode must open+close the %s device\n", path);
206 OSD_ERR(" Example: bash: echo < %s\n", path);
210 /* The Magic wand. Is it our char-dev */
211 /* TODO: Support sg devices */
212 if (cdev->owner != THIS_MODULE) {
213 OSD_ERR("Error mounting %s - is not an OSD device\n", path);
217 oud = container_of(cdev, struct osd_uld_device, cdev);
224 return error ? ERR_PTR(error) : &oud->od;
226 EXPORT_SYMBOL(osduld_path_lookup);
228 void osduld_put_device(struct osd_dev *od)
231 struct osd_uld_device *oud = container_of(od,
232 struct osd_uld_device, od);
237 EXPORT_SYMBOL(osduld_put_device);
240 * Scsi Device operations
243 static int __detect_osd(struct osd_uld_device *oud)
245 struct scsi_device *scsi_device = oud->od.scsi_device;
246 char caps[OSD_CAP_LEN];
249 /* sending a test_unit_ready as first command seems to be needed
252 OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
253 oud, scsi_device, scsi_device->request_queue);
254 error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
256 OSD_ERR("warning: scsi_test_unit_ready failed\n");
258 osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
259 if (osd_auto_detect_ver(&oud->od, caps))
265 static struct class *osd_sysfs_class;
266 static DEFINE_IDA(osd_minor_ida);
268 static int osd_probe(struct device *dev)
270 struct scsi_device *scsi_device = to_scsi_device(dev);
271 struct gendisk *disk;
272 struct osd_uld_device *oud;
276 if (scsi_device->type != TYPE_OSD)
280 if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
283 error = ida_get_new(&osd_minor_ida, &minor);
284 } while (error == -EAGAIN);
288 if (minor >= SCSI_OSD_MAX_MINOR) {
290 goto err_retract_minor;
294 oud = kzalloc(sizeof(*oud), GFP_KERNEL);
296 goto err_retract_minor;
298 kref_init(&oud->kref);
299 dev_set_drvdata(dev, oud);
302 /* allocate a disk and set it up */
303 /* FIXME: do we need this since sg has already done that */
304 disk = alloc_disk(1);
306 OSD_ERR("alloc_disk failed\n");
309 disk->major = SCSI_OSD_MAJOR;
310 disk->first_minor = oud->minor;
311 sprintf(disk->disk_name, "osd%d", oud->minor);
314 /* hold one more reference to the scsi_device that will get released
315 * in __release, in case a logout is happening while fs is mounted
317 scsi_device_get(scsi_device);
318 osd_dev_init(&oud->od, scsi_device);
320 /* Detect the OSD Version */
321 error = __detect_osd(oud);
323 OSD_ERR("osd detection failed, non-compatible OSD device\n");
327 /* init the char-device for communication with user-mode */
328 cdev_init(&oud->cdev, &osd_fops);
329 oud->cdev.owner = THIS_MODULE;
330 error = cdev_add(&oud->cdev,
331 MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
333 OSD_ERR("cdev_add failed\n");
336 kobject_get(&oud->cdev.kobj); /* 2nd ref see osd_remove() */
339 oud->class_member = device_create(osd_sysfs_class, dev,
340 MKDEV(SCSI_OSD_MAJOR, oud->minor), "%s", disk->disk_name);
341 if (IS_ERR(oud->class_member)) {
342 OSD_ERR("class_device_create failed\n");
343 error = PTR_ERR(oud->class_member);
347 dev_set_drvdata(oud->class_member, oud);
349 OSD_INFO("osd_probe %s\n", disk->disk_name);
353 cdev_del(&oud->cdev);
355 scsi_device_put(scsi_device);
358 dev_set_drvdata(dev, NULL);
361 ida_remove(&osd_minor_ida, minor);
365 static int osd_remove(struct device *dev)
367 struct scsi_device *scsi_device = to_scsi_device(dev);
368 struct osd_uld_device *oud = dev_get_drvdata(dev);
370 if (!oud || (oud->od.scsi_device != scsi_device)) {
371 OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
372 dev, oud, oud ? oud->od.scsi_device : NULL,
376 if (oud->class_member)
377 device_destroy(osd_sysfs_class,
378 MKDEV(SCSI_OSD_MAJOR, oud->minor));
380 /* We have 2 references to the cdev. One is released here
381 * and also takes down the /dev/osdX mapping. The second
382 * Will be released in __remove() after all users have released
383 * the osd_uld_device.
386 cdev_del(&oud->cdev);
392 static void __remove(struct kref *kref)
394 struct osd_uld_device *oud = container_of(kref,
395 struct osd_uld_device, kref);
396 struct scsi_device *scsi_device = oud->od.scsi_device;
398 /* now let delete the char_dev */
399 kobject_put(&oud->cdev.kobj);
401 osd_dev_fini(&oud->od);
402 scsi_device_put(scsi_device);
404 OSD_INFO("osd_remove %s\n",
405 oud->disk ? oud->disk->disk_name : NULL);
410 ida_remove(&osd_minor_ida, oud->minor);
414 static void __uld_get(struct osd_uld_device *oud)
416 kref_get(&oud->kref);
419 static void __uld_put(struct osd_uld_device *oud)
421 kref_put(&oud->kref, __remove);
425 * Global driver and scsi registration
428 static struct scsi_driver osd_driver = {
429 .owner = THIS_MODULE,
433 .remove = osd_remove,
437 static int __init osd_uld_init(void)
441 osd_sysfs_class = class_create(THIS_MODULE, osd_symlink);
442 if (IS_ERR(osd_sysfs_class)) {
443 OSD_ERR("Unable to register sysfs class => %ld\n",
444 PTR_ERR(osd_sysfs_class));
445 return PTR_ERR(osd_sysfs_class);
448 err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
449 SCSI_OSD_MAX_MINOR, osd_name);
451 OSD_ERR("Unable to register major %d for osd ULD => %d\n",
452 SCSI_OSD_MAJOR, err);
456 err = scsi_register_driver(&osd_driver.gendrv);
458 OSD_ERR("scsi_register_driver failed => %d\n", err);
462 OSD_INFO("LOADED %s\n", osd_version_string);
466 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
468 class_destroy(osd_sysfs_class);
472 static void __exit osd_uld_exit(void)
474 scsi_unregister_driver(&osd_driver.gendrv);
475 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
476 class_destroy(osd_sysfs_class);
477 OSD_INFO("UNLOADED %s\n", osd_version_string);
480 module_init(osd_uld_init);
481 module_exit(osd_uld_exit);