Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * osd_uld.c - OSD Upper Layer Driver |
| 3 | * |
| 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 |
| 8 | * in Kernel tests. |
| 9 | * |
| 10 | * Copyright (C) 2008 Panasas Inc. All rights reserved. |
| 11 | * |
| 12 | * Authors: |
| 13 | * Boaz Harrosh <bharrosh@panasas.com> |
| 14 | * Benny Halevy <bhalevy@panasas.com> |
| 15 | * |
| 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 |
| 18 | * |
| 19 | * Redistribution and use in source and binary forms, with or without |
| 20 | * modification, are permitted provided that the following conditions |
| 21 | * are met: |
| 22 | * |
| 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. |
| 31 | * |
| 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. |
| 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 | |
| 53 | #include <scsi/scsi.h> |
| 54 | #include <scsi/scsi_driver.h> |
| 55 | #include <scsi/scsi_device.h> |
| 56 | #include <scsi/scsi_ioctl.h> |
| 57 | |
| 58 | #include <scsi/osd_initiator.h> |
| 59 | #include <scsi/osd_sec.h> |
| 60 | |
| 61 | #include "osd_debug.h" |
| 62 | |
| 63 | #ifndef TYPE_OSD |
| 64 | # define TYPE_OSD 0x11 |
| 65 | #endif |
| 66 | |
| 67 | #ifndef SCSI_OSD_MAJOR |
| 68 | # define SCSI_OSD_MAJOR 260 |
| 69 | #endif |
| 70 | #define SCSI_OSD_MAX_MINOR 64 |
| 71 | |
| 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"; |
| 75 | |
| 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); |
| 81 | |
| 82 | struct osd_uld_device { |
| 83 | int minor; |
| 84 | struct kref kref; |
| 85 | struct cdev cdev; |
| 86 | struct osd_dev od; |
| 87 | struct gendisk *disk; |
| 88 | struct device *class_member; |
| 89 | }; |
| 90 | |
| 91 | static void __uld_get(struct osd_uld_device *oud); |
| 92 | static void __uld_put(struct osd_uld_device *oud); |
| 93 | |
| 94 | /* |
| 95 | * Char Device operations |
| 96 | */ |
| 97 | |
| 98 | static int osd_uld_open(struct inode *inode, struct file *file) |
| 99 | { |
| 100 | struct osd_uld_device *oud = container_of(inode->i_cdev, |
| 101 | struct osd_uld_device, cdev); |
| 102 | |
| 103 | __uld_get(oud); |
| 104 | /* cache osd_uld_device on file handle */ |
| 105 | file->private_data = oud; |
| 106 | OSD_DEBUG("osd_uld_open %p\n", oud); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int osd_uld_release(struct inode *inode, struct file *file) |
| 111 | { |
| 112 | struct osd_uld_device *oud = file->private_data; |
| 113 | |
| 114 | OSD_DEBUG("osd_uld_release %p\n", file->private_data); |
| 115 | file->private_data = NULL; |
| 116 | __uld_put(oud); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* FIXME: Only one vector for now */ |
| 121 | unsigned g_test_ioctl; |
| 122 | do_test_fn *g_do_test; |
| 123 | |
| 124 | int osduld_register_test(unsigned ioctl, do_test_fn *do_test) |
| 125 | { |
| 126 | if (g_test_ioctl) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | g_test_ioctl = ioctl; |
| 130 | g_do_test = do_test; |
| 131 | return 0; |
| 132 | } |
| 133 | EXPORT_SYMBOL(osduld_register_test); |
| 134 | |
| 135 | void osduld_unregister_test(unsigned ioctl) |
| 136 | { |
| 137 | if (ioctl == g_test_ioctl) { |
| 138 | g_test_ioctl = 0; |
| 139 | g_do_test = NULL; |
| 140 | } |
| 141 | } |
| 142 | EXPORT_SYMBOL(osduld_unregister_test); |
| 143 | |
| 144 | static do_test_fn *_find_ioctl(unsigned cmd) |
| 145 | { |
| 146 | if (g_test_ioctl == cmd) |
| 147 | return g_do_test; |
| 148 | else |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static long osd_uld_ioctl(struct file *file, unsigned int cmd, |
| 153 | unsigned long arg) |
| 154 | { |
| 155 | struct osd_uld_device *oud = file->private_data; |
| 156 | int ret; |
| 157 | do_test_fn *do_test; |
| 158 | |
| 159 | do_test = _find_ioctl(cmd); |
| 160 | if (do_test) |
| 161 | ret = do_test(&oud->od, cmd, arg); |
| 162 | else { |
| 163 | OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud); |
| 164 | ret = -ENOIOCTLCMD; |
| 165 | } |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 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, |
| 174 | }; |
| 175 | |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 176 | struct osd_dev *osduld_path_lookup(const char *path) |
| 177 | { |
| 178 | struct nameidata nd; |
| 179 | struct inode *inode; |
| 180 | struct cdev *cdev; |
| 181 | struct osd_uld_device *uninitialized_var(oud); |
| 182 | int error; |
| 183 | |
| 184 | if (!path || !*path) { |
| 185 | OSD_ERR("Mount with !path || !*path\n"); |
| 186 | return ERR_PTR(-EINVAL); |
| 187 | } |
| 188 | |
| 189 | error = path_lookup(path, LOOKUP_FOLLOW, &nd); |
| 190 | if (error) { |
| 191 | OSD_ERR("path_lookup of %s faild=>%d\n", path, error); |
| 192 | return ERR_PTR(error); |
| 193 | } |
| 194 | |
| 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"); |
| 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | cdev = inode->i_cdev; |
| 203 | if (!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); |
| 207 | goto out; |
| 208 | } |
| 209 | |
| 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); |
| 214 | goto out; |
| 215 | } |
| 216 | |
| 217 | oud = container_of(cdev, struct osd_uld_device, cdev); |
| 218 | |
| 219 | __uld_get(oud); |
| 220 | error = 0; |
| 221 | |
| 222 | out: |
| 223 | path_put(&nd.path); |
| 224 | return error ? ERR_PTR(error) : &oud->od; |
| 225 | } |
| 226 | EXPORT_SYMBOL(osduld_path_lookup); |
| 227 | |
| 228 | void osduld_put_device(struct osd_dev *od) |
| 229 | { |
| 230 | if (od) { |
| 231 | struct osd_uld_device *oud = container_of(od, |
| 232 | struct osd_uld_device, od); |
| 233 | |
| 234 | __uld_put(oud); |
| 235 | } |
| 236 | } |
| 237 | EXPORT_SYMBOL(osduld_put_device); |
| 238 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 239 | /* |
| 240 | * Scsi Device operations |
| 241 | */ |
| 242 | |
| 243 | static int __detect_osd(struct osd_uld_device *oud) |
| 244 | { |
| 245 | struct scsi_device *scsi_device = oud->od.scsi_device; |
| 246 | int error; |
| 247 | |
| 248 | /* sending a test_unit_ready as first command seems to be needed |
| 249 | * by some targets |
| 250 | */ |
| 251 | OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n", |
| 252 | oud, scsi_device, scsi_device->request_queue); |
| 253 | error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL); |
| 254 | if (error) |
| 255 | OSD_ERR("warning: scsi_test_unit_ready failed\n"); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static struct class *osd_sysfs_class; |
| 261 | static DEFINE_IDA(osd_minor_ida); |
| 262 | |
| 263 | static int osd_probe(struct device *dev) |
| 264 | { |
| 265 | struct scsi_device *scsi_device = to_scsi_device(dev); |
| 266 | struct gendisk *disk; |
| 267 | struct osd_uld_device *oud; |
| 268 | int minor; |
| 269 | int error; |
| 270 | |
| 271 | if (scsi_device->type != TYPE_OSD) |
| 272 | return -ENODEV; |
| 273 | |
| 274 | do { |
| 275 | if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL)) |
| 276 | return -ENODEV; |
| 277 | |
| 278 | error = ida_get_new(&osd_minor_ida, &minor); |
| 279 | } while (error == -EAGAIN); |
| 280 | |
| 281 | if (error) |
| 282 | return error; |
| 283 | if (minor >= SCSI_OSD_MAX_MINOR) { |
| 284 | error = -EBUSY; |
| 285 | goto err_retract_minor; |
| 286 | } |
| 287 | |
| 288 | error = -ENOMEM; |
| 289 | oud = kzalloc(sizeof(*oud), GFP_KERNEL); |
| 290 | if (NULL == oud) |
| 291 | goto err_retract_minor; |
| 292 | |
| 293 | kref_init(&oud->kref); |
| 294 | dev_set_drvdata(dev, oud); |
| 295 | oud->minor = minor; |
| 296 | |
| 297 | /* allocate a disk and set it up */ |
| 298 | /* FIXME: do we need this since sg has already done that */ |
| 299 | disk = alloc_disk(1); |
| 300 | if (!disk) { |
| 301 | OSD_ERR("alloc_disk failed\n"); |
| 302 | goto err_free_osd; |
| 303 | } |
| 304 | disk->major = SCSI_OSD_MAJOR; |
| 305 | disk->first_minor = oud->minor; |
| 306 | sprintf(disk->disk_name, "osd%d", oud->minor); |
| 307 | oud->disk = disk; |
| 308 | |
| 309 | /* hold one more reference to the scsi_device that will get released |
| 310 | * in __release, in case a logout is happening while fs is mounted |
| 311 | */ |
| 312 | scsi_device_get(scsi_device); |
| 313 | osd_dev_init(&oud->od, scsi_device); |
| 314 | |
| 315 | /* Detect the OSD Version */ |
| 316 | error = __detect_osd(oud); |
| 317 | if (error) { |
| 318 | OSD_ERR("osd detection failed, non-compatible OSD device\n"); |
| 319 | goto err_put_disk; |
| 320 | } |
| 321 | |
| 322 | /* init the char-device for communication with user-mode */ |
| 323 | cdev_init(&oud->cdev, &osd_fops); |
| 324 | oud->cdev.owner = THIS_MODULE; |
| 325 | error = cdev_add(&oud->cdev, |
| 326 | MKDEV(SCSI_OSD_MAJOR, oud->minor), 1); |
| 327 | if (error) { |
| 328 | OSD_ERR("cdev_add failed\n"); |
| 329 | goto err_put_disk; |
| 330 | } |
| 331 | kobject_get(&oud->cdev.kobj); /* 2nd ref see osd_remove() */ |
| 332 | |
| 333 | /* class_member */ |
| 334 | oud->class_member = device_create(osd_sysfs_class, dev, |
| 335 | MKDEV(SCSI_OSD_MAJOR, oud->minor), "%s", disk->disk_name); |
| 336 | if (IS_ERR(oud->class_member)) { |
| 337 | OSD_ERR("class_device_create failed\n"); |
| 338 | error = PTR_ERR(oud->class_member); |
| 339 | goto err_put_cdev; |
| 340 | } |
| 341 | |
| 342 | dev_set_drvdata(oud->class_member, oud); |
| 343 | error = sysfs_create_link(&scsi_device->sdev_gendev.kobj, |
| 344 | &oud->class_member->kobj, osd_symlink); |
| 345 | if (error) |
| 346 | OSD_ERR("warning: unable to make symlink\n"); |
| 347 | |
| 348 | OSD_INFO("osd_probe %s\n", disk->disk_name); |
| 349 | return 0; |
| 350 | |
| 351 | err_put_cdev: |
| 352 | cdev_del(&oud->cdev); |
| 353 | err_put_disk: |
| 354 | scsi_device_put(scsi_device); |
| 355 | put_disk(disk); |
| 356 | err_free_osd: |
| 357 | dev_set_drvdata(dev, NULL); |
| 358 | kfree(oud); |
| 359 | err_retract_minor: |
| 360 | ida_remove(&osd_minor_ida, minor); |
| 361 | return error; |
| 362 | } |
| 363 | |
| 364 | static int osd_remove(struct device *dev) |
| 365 | { |
| 366 | struct scsi_device *scsi_device = to_scsi_device(dev); |
| 367 | struct osd_uld_device *oud = dev_get_drvdata(dev); |
| 368 | |
| 369 | if (!oud || (oud->od.scsi_device != scsi_device)) { |
| 370 | OSD_ERR("Half cooked osd-device %p,%p || %p!=%p", |
| 371 | dev, oud, oud ? oud->od.scsi_device : NULL, |
| 372 | scsi_device); |
| 373 | } |
| 374 | |
| 375 | sysfs_remove_link(&oud->od.scsi_device->sdev_gendev.kobj, osd_symlink); |
| 376 | |
| 377 | if (oud->class_member) |
| 378 | device_destroy(osd_sysfs_class, |
| 379 | MKDEV(SCSI_OSD_MAJOR, oud->minor)); |
| 380 | |
| 381 | /* We have 2 references to the cdev. One is released here |
| 382 | * and also takes down the /dev/osdX mapping. The second |
| 383 | * Will be released in __remove() after all users have released |
| 384 | * the osd_uld_device. |
| 385 | */ |
| 386 | if (oud->cdev.owner) |
| 387 | cdev_del(&oud->cdev); |
| 388 | |
| 389 | __uld_put(oud); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static void __remove(struct kref *kref) |
| 394 | { |
| 395 | struct osd_uld_device *oud = container_of(kref, |
| 396 | struct osd_uld_device, kref); |
| 397 | struct scsi_device *scsi_device = oud->od.scsi_device; |
| 398 | |
| 399 | /* now let delete the char_dev */ |
| 400 | kobject_put(&oud->cdev.kobj); |
| 401 | |
| 402 | osd_dev_fini(&oud->od); |
| 403 | scsi_device_put(scsi_device); |
| 404 | |
| 405 | OSD_INFO("osd_remove %s\n", |
| 406 | oud->disk ? oud->disk->disk_name : NULL); |
| 407 | |
| 408 | if (oud->disk) |
| 409 | put_disk(oud->disk); |
| 410 | |
| 411 | ida_remove(&osd_minor_ida, oud->minor); |
| 412 | kfree(oud); |
| 413 | } |
| 414 | |
| 415 | static void __uld_get(struct osd_uld_device *oud) |
| 416 | { |
| 417 | kref_get(&oud->kref); |
| 418 | } |
| 419 | |
| 420 | static void __uld_put(struct osd_uld_device *oud) |
| 421 | { |
| 422 | kref_put(&oud->kref, __remove); |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Global driver and scsi registration |
| 427 | */ |
| 428 | |
| 429 | static struct scsi_driver osd_driver = { |
| 430 | .owner = THIS_MODULE, |
| 431 | .gendrv = { |
| 432 | .name = osd_name, |
| 433 | .probe = osd_probe, |
| 434 | .remove = osd_remove, |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | static int __init osd_uld_init(void) |
| 439 | { |
| 440 | int err; |
| 441 | |
| 442 | osd_sysfs_class = class_create(THIS_MODULE, osd_symlink); |
| 443 | if (IS_ERR(osd_sysfs_class)) { |
| 444 | OSD_ERR("Unable to register sysfs class => %ld\n", |
| 445 | PTR_ERR(osd_sysfs_class)); |
| 446 | return PTR_ERR(osd_sysfs_class); |
| 447 | } |
| 448 | |
| 449 | err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), |
| 450 | SCSI_OSD_MAX_MINOR, osd_name); |
| 451 | if (err) { |
| 452 | OSD_ERR("Unable to register major %d for osd ULD => %d\n", |
| 453 | SCSI_OSD_MAJOR, err); |
| 454 | goto err_out; |
| 455 | } |
| 456 | |
| 457 | err = scsi_register_driver(&osd_driver.gendrv); |
| 458 | if (err) { |
| 459 | OSD_ERR("scsi_register_driver failed => %d\n", err); |
| 460 | goto err_out_chrdev; |
| 461 | } |
| 462 | |
| 463 | OSD_INFO("LOADED %s\n", osd_version_string); |
| 464 | return 0; |
| 465 | |
| 466 | err_out_chrdev: |
| 467 | unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR); |
| 468 | err_out: |
| 469 | class_destroy(osd_sysfs_class); |
| 470 | return err; |
| 471 | } |
| 472 | |
| 473 | static void __exit osd_uld_exit(void) |
| 474 | { |
| 475 | scsi_unregister_driver(&osd_driver.gendrv); |
| 476 | unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR); |
| 477 | class_destroy(osd_sysfs_class); |
| 478 | OSD_INFO("UNLOADED %s\n", osd_version_string); |
| 479 | } |
| 480 | |
| 481 | module_init(osd_uld_init); |
| 482 | module_exit(osd_uld_exit); |