osd_uld.c 12 KB

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