osd_uld.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. #include <linux/namei.h>
  45. #include <linux/cdev.h>
  46. #include <linux/fs.h>
  47. #include <linux/module.h>
  48. #include <linux/device.h>
  49. #include <linux/idr.h>
  50. #include <linux/major.h>
  51. #include <linux/file.h>
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_driver.h>
  54. #include <scsi/scsi_device.h>
  55. #include <scsi/scsi_ioctl.h>
  56. #include <scsi/osd_initiator.h>
  57. #include <scsi/osd_sec.h>
  58. #include "osd_debug.h"
  59. #ifndef TYPE_OSD
  60. # define TYPE_OSD 0x11
  61. #endif
  62. #ifndef SCSI_OSD_MAJOR
  63. # define SCSI_OSD_MAJOR 260
  64. #endif
  65. #define SCSI_OSD_MAX_MINOR 64
  66. static const char osd_name[] = "osd";
  67. static const char *osd_version_string = "open-osd 0.1.0";
  68. const char osd_symlink[] = "scsi_osd";
  69. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  70. MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
  71. MODULE_LICENSE("GPL");
  72. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
  73. MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
  74. struct osd_uld_device {
  75. int minor;
  76. struct kref kref;
  77. struct cdev cdev;
  78. struct osd_dev od;
  79. struct gendisk *disk;
  80. struct device *class_member;
  81. };
  82. static void __uld_get(struct osd_uld_device *oud);
  83. static void __uld_put(struct osd_uld_device *oud);
  84. /*
  85. * Char Device operations
  86. */
  87. static int osd_uld_open(struct inode *inode, struct file *file)
  88. {
  89. struct osd_uld_device *oud = container_of(inode->i_cdev,
  90. struct osd_uld_device, cdev);
  91. __uld_get(oud);
  92. /* cache osd_uld_device on file handle */
  93. file->private_data = oud;
  94. OSD_DEBUG("osd_uld_open %p\n", oud);
  95. return 0;
  96. }
  97. static int osd_uld_release(struct inode *inode, struct file *file)
  98. {
  99. struct osd_uld_device *oud = file->private_data;
  100. OSD_DEBUG("osd_uld_release %p\n", file->private_data);
  101. file->private_data = NULL;
  102. __uld_put(oud);
  103. return 0;
  104. }
  105. /* FIXME: Only one vector for now */
  106. unsigned g_test_ioctl;
  107. do_test_fn *g_do_test;
  108. int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
  109. {
  110. if (g_test_ioctl)
  111. return -EINVAL;
  112. g_test_ioctl = ioctl;
  113. g_do_test = do_test;
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(osduld_register_test);
  117. void osduld_unregister_test(unsigned ioctl)
  118. {
  119. if (ioctl == g_test_ioctl) {
  120. g_test_ioctl = 0;
  121. g_do_test = NULL;
  122. }
  123. }
  124. EXPORT_SYMBOL(osduld_unregister_test);
  125. static do_test_fn *_find_ioctl(unsigned cmd)
  126. {
  127. if (g_test_ioctl == cmd)
  128. return g_do_test;
  129. else
  130. return NULL;
  131. }
  132. static long osd_uld_ioctl(struct file *file, unsigned int cmd,
  133. unsigned long arg)
  134. {
  135. struct osd_uld_device *oud = file->private_data;
  136. int ret;
  137. do_test_fn *do_test;
  138. do_test = _find_ioctl(cmd);
  139. if (do_test)
  140. ret = do_test(&oud->od, cmd, arg);
  141. else {
  142. OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
  143. ret = -ENOIOCTLCMD;
  144. }
  145. return ret;
  146. }
  147. static const struct file_operations osd_fops = {
  148. .owner = THIS_MODULE,
  149. .open = osd_uld_open,
  150. .release = osd_uld_release,
  151. .unlocked_ioctl = osd_uld_ioctl,
  152. };
  153. struct osd_dev *osduld_path_lookup(const char *name)
  154. {
  155. struct osd_uld_device *oud;
  156. struct osd_dev *od;
  157. struct file *file;
  158. int error;
  159. if (!name || !*name) {
  160. OSD_ERR("Mount with !path || !*path\n");
  161. return ERR_PTR(-EINVAL);
  162. }
  163. od = kzalloc(sizeof(*od), GFP_KERNEL);
  164. if (!od)
  165. return ERR_PTR(-ENOMEM);
  166. file = filp_open(name, O_RDWR, 0);
  167. if (IS_ERR(file)) {
  168. error = PTR_ERR(file);
  169. goto free_od;
  170. }
  171. if (file->f_op != &osd_fops){
  172. error = -EINVAL;
  173. goto close_file;
  174. }
  175. oud = file->private_data;
  176. *od = oud->od;
  177. od->file = file;
  178. return od;
  179. close_file:
  180. fput(file);
  181. free_od:
  182. kfree(od);
  183. return ERR_PTR(error);
  184. }
  185. EXPORT_SYMBOL(osduld_path_lookup);
  186. void osduld_put_device(struct osd_dev *od)
  187. {
  188. if (od && !IS_ERR(od)) {
  189. struct osd_uld_device *oud = od->file->private_data;
  190. BUG_ON(od->scsi_device != oud->od.scsi_device);
  191. fput(od->file);
  192. kfree(od);
  193. }
  194. }
  195. EXPORT_SYMBOL(osduld_put_device);
  196. /*
  197. * Scsi Device operations
  198. */
  199. static int __detect_osd(struct osd_uld_device *oud)
  200. {
  201. struct scsi_device *scsi_device = oud->od.scsi_device;
  202. char caps[OSD_CAP_LEN];
  203. int error;
  204. /* sending a test_unit_ready as first command seems to be needed
  205. * by some targets
  206. */
  207. OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
  208. oud, scsi_device, scsi_device->request_queue);
  209. error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
  210. if (error)
  211. OSD_ERR("warning: scsi_test_unit_ready failed\n");
  212. osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
  213. if (osd_auto_detect_ver(&oud->od, caps))
  214. return -ENODEV;
  215. return 0;
  216. }
  217. static struct class *osd_sysfs_class;
  218. static DEFINE_IDA(osd_minor_ida);
  219. static int osd_probe(struct device *dev)
  220. {
  221. struct scsi_device *scsi_device = to_scsi_device(dev);
  222. struct gendisk *disk;
  223. struct osd_uld_device *oud;
  224. int minor;
  225. int error;
  226. if (scsi_device->type != TYPE_OSD)
  227. return -ENODEV;
  228. do {
  229. if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
  230. return -ENODEV;
  231. error = ida_get_new(&osd_minor_ida, &minor);
  232. } while (error == -EAGAIN);
  233. if (error)
  234. return error;
  235. if (minor >= SCSI_OSD_MAX_MINOR) {
  236. error = -EBUSY;
  237. goto err_retract_minor;
  238. }
  239. error = -ENOMEM;
  240. oud = kzalloc(sizeof(*oud), GFP_KERNEL);
  241. if (NULL == oud)
  242. goto err_retract_minor;
  243. kref_init(&oud->kref);
  244. dev_set_drvdata(dev, oud);
  245. oud->minor = minor;
  246. /* allocate a disk and set it up */
  247. /* FIXME: do we need this since sg has already done that */
  248. disk = alloc_disk(1);
  249. if (!disk) {
  250. OSD_ERR("alloc_disk failed\n");
  251. goto err_free_osd;
  252. }
  253. disk->major = SCSI_OSD_MAJOR;
  254. disk->first_minor = oud->minor;
  255. sprintf(disk->disk_name, "osd%d", oud->minor);
  256. oud->disk = disk;
  257. /* hold one more reference to the scsi_device that will get released
  258. * in __release, in case a logout is happening while fs is mounted
  259. */
  260. scsi_device_get(scsi_device);
  261. osd_dev_init(&oud->od, scsi_device);
  262. /* Detect the OSD Version */
  263. error = __detect_osd(oud);
  264. if (error) {
  265. OSD_ERR("osd detection failed, non-compatible OSD device\n");
  266. goto err_put_disk;
  267. }
  268. /* init the char-device for communication with user-mode */
  269. cdev_init(&oud->cdev, &osd_fops);
  270. oud->cdev.owner = THIS_MODULE;
  271. error = cdev_add(&oud->cdev,
  272. MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
  273. if (error) {
  274. OSD_ERR("cdev_add failed\n");
  275. goto err_put_disk;
  276. }
  277. kobject_get(&oud->cdev.kobj); /* 2nd ref see osd_remove() */
  278. /* class_member */
  279. oud->class_member = device_create(osd_sysfs_class, dev,
  280. MKDEV(SCSI_OSD_MAJOR, oud->minor), "%s", disk->disk_name);
  281. if (IS_ERR(oud->class_member)) {
  282. OSD_ERR("class_device_create failed\n");
  283. error = PTR_ERR(oud->class_member);
  284. goto err_put_cdev;
  285. }
  286. dev_set_drvdata(oud->class_member, oud);
  287. OSD_INFO("osd_probe %s\n", disk->disk_name);
  288. return 0;
  289. err_put_cdev:
  290. cdev_del(&oud->cdev);
  291. err_put_disk:
  292. scsi_device_put(scsi_device);
  293. put_disk(disk);
  294. err_free_osd:
  295. dev_set_drvdata(dev, NULL);
  296. kfree(oud);
  297. err_retract_minor:
  298. ida_remove(&osd_minor_ida, minor);
  299. return error;
  300. }
  301. static int osd_remove(struct device *dev)
  302. {
  303. struct scsi_device *scsi_device = to_scsi_device(dev);
  304. struct osd_uld_device *oud = dev_get_drvdata(dev);
  305. if (!oud || (oud->od.scsi_device != scsi_device)) {
  306. OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
  307. dev, oud, oud ? oud->od.scsi_device : NULL,
  308. scsi_device);
  309. }
  310. if (oud->class_member)
  311. device_destroy(osd_sysfs_class,
  312. MKDEV(SCSI_OSD_MAJOR, oud->minor));
  313. /* We have 2 references to the cdev. One is released here
  314. * and also takes down the /dev/osdX mapping. The second
  315. * Will be released in __remove() after all users have released
  316. * the osd_uld_device.
  317. */
  318. if (oud->cdev.owner)
  319. cdev_del(&oud->cdev);
  320. __uld_put(oud);
  321. return 0;
  322. }
  323. static void __remove(struct kref *kref)
  324. {
  325. struct osd_uld_device *oud = container_of(kref,
  326. struct osd_uld_device, kref);
  327. struct scsi_device *scsi_device = oud->od.scsi_device;
  328. /* now let delete the char_dev */
  329. kobject_put(&oud->cdev.kobj);
  330. osd_dev_fini(&oud->od);
  331. scsi_device_put(scsi_device);
  332. OSD_INFO("osd_remove %s\n",
  333. oud->disk ? oud->disk->disk_name : NULL);
  334. if (oud->disk)
  335. put_disk(oud->disk);
  336. ida_remove(&osd_minor_ida, oud->minor);
  337. kfree(oud);
  338. }
  339. static void __uld_get(struct osd_uld_device *oud)
  340. {
  341. kref_get(&oud->kref);
  342. }
  343. static void __uld_put(struct osd_uld_device *oud)
  344. {
  345. kref_put(&oud->kref, __remove);
  346. }
  347. /*
  348. * Global driver and scsi registration
  349. */
  350. static struct scsi_driver osd_driver = {
  351. .owner = THIS_MODULE,
  352. .gendrv = {
  353. .name = osd_name,
  354. .probe = osd_probe,
  355. .remove = osd_remove,
  356. }
  357. };
  358. static int __init osd_uld_init(void)
  359. {
  360. int err;
  361. osd_sysfs_class = class_create(THIS_MODULE, osd_symlink);
  362. if (IS_ERR(osd_sysfs_class)) {
  363. OSD_ERR("Unable to register sysfs class => %ld\n",
  364. PTR_ERR(osd_sysfs_class));
  365. return PTR_ERR(osd_sysfs_class);
  366. }
  367. err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
  368. SCSI_OSD_MAX_MINOR, osd_name);
  369. if (err) {
  370. OSD_ERR("Unable to register major %d for osd ULD => %d\n",
  371. SCSI_OSD_MAJOR, err);
  372. goto err_out;
  373. }
  374. err = scsi_register_driver(&osd_driver.gendrv);
  375. if (err) {
  376. OSD_ERR("scsi_register_driver failed => %d\n", err);
  377. goto err_out_chrdev;
  378. }
  379. OSD_INFO("LOADED %s\n", osd_version_string);
  380. return 0;
  381. err_out_chrdev:
  382. unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
  383. err_out:
  384. class_destroy(osd_sysfs_class);
  385. return err;
  386. }
  387. static void __exit osd_uld_exit(void)
  388. {
  389. scsi_unregister_driver(&osd_driver.gendrv);
  390. unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
  391. class_destroy(osd_sysfs_class);
  392. OSD_INFO("UNLOADED %s\n", osd_version_string);
  393. }
  394. module_init(osd_uld_init);
  395. module_exit(osd_uld_exit);