osd_uld.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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.2.0";
  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 device class_dev;
  76. struct cdev cdev;
  77. struct osd_dev od;
  78. struct osd_dev_info odi;
  79. struct gendisk *disk;
  80. };
  81. struct osd_dev_handle {
  82. struct osd_dev od;
  83. struct file *file;
  84. struct osd_uld_device *oud;
  85. } ;
  86. static DEFINE_IDA(osd_minor_ida);
  87. static struct class osd_uld_class = {
  88. .owner = THIS_MODULE,
  89. .name = "scsi_osd",
  90. };
  91. /*
  92. * Char Device operations
  93. */
  94. static int osd_uld_open(struct inode *inode, struct file *file)
  95. {
  96. struct osd_uld_device *oud = container_of(inode->i_cdev,
  97. struct osd_uld_device, cdev);
  98. get_device(&oud->class_dev);
  99. /* cache osd_uld_device on file handle */
  100. file->private_data = oud;
  101. OSD_DEBUG("osd_uld_open %p\n", oud);
  102. return 0;
  103. }
  104. static int osd_uld_release(struct inode *inode, struct file *file)
  105. {
  106. struct osd_uld_device *oud = file->private_data;
  107. OSD_DEBUG("osd_uld_release %p\n", file->private_data);
  108. file->private_data = NULL;
  109. put_device(&oud->class_dev);
  110. return 0;
  111. }
  112. /* FIXME: Only one vector for now */
  113. unsigned g_test_ioctl;
  114. do_test_fn *g_do_test;
  115. int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
  116. {
  117. if (g_test_ioctl)
  118. return -EINVAL;
  119. g_test_ioctl = ioctl;
  120. g_do_test = do_test;
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(osduld_register_test);
  124. void osduld_unregister_test(unsigned ioctl)
  125. {
  126. if (ioctl == g_test_ioctl) {
  127. g_test_ioctl = 0;
  128. g_do_test = NULL;
  129. }
  130. }
  131. EXPORT_SYMBOL(osduld_unregister_test);
  132. static do_test_fn *_find_ioctl(unsigned cmd)
  133. {
  134. if (g_test_ioctl == cmd)
  135. return g_do_test;
  136. else
  137. return NULL;
  138. }
  139. static long osd_uld_ioctl(struct file *file, unsigned int cmd,
  140. unsigned long arg)
  141. {
  142. struct osd_uld_device *oud = file->private_data;
  143. int ret;
  144. do_test_fn *do_test;
  145. do_test = _find_ioctl(cmd);
  146. if (do_test)
  147. ret = do_test(&oud->od, cmd, arg);
  148. else {
  149. OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
  150. ret = -ENOIOCTLCMD;
  151. }
  152. return ret;
  153. }
  154. static const struct file_operations osd_fops = {
  155. .owner = THIS_MODULE,
  156. .open = osd_uld_open,
  157. .release = osd_uld_release,
  158. .unlocked_ioctl = osd_uld_ioctl,
  159. };
  160. struct osd_dev *osduld_path_lookup(const char *name)
  161. {
  162. struct osd_uld_device *oud;
  163. struct osd_dev_handle *odh;
  164. struct file *file;
  165. int error;
  166. if (!name || !*name) {
  167. OSD_ERR("Mount with !path || !*path\n");
  168. return ERR_PTR(-EINVAL);
  169. }
  170. odh = kzalloc(sizeof(*odh), GFP_KERNEL);
  171. if (unlikely(!odh))
  172. return ERR_PTR(-ENOMEM);
  173. file = filp_open(name, O_RDWR, 0);
  174. if (IS_ERR(file)) {
  175. error = PTR_ERR(file);
  176. goto free_od;
  177. }
  178. if (file->f_op != &osd_fops){
  179. error = -EINVAL;
  180. goto close_file;
  181. }
  182. oud = file->private_data;
  183. odh->od = oud->od;
  184. odh->file = file;
  185. odh->oud = oud;
  186. return &odh->od;
  187. close_file:
  188. fput(file);
  189. free_od:
  190. kfree(odh);
  191. return ERR_PTR(error);
  192. }
  193. EXPORT_SYMBOL(osduld_path_lookup);
  194. static inline bool _the_same_or_null(const u8 *a1, unsigned a1_len,
  195. const u8 *a2, unsigned a2_len)
  196. {
  197. if (!a2_len) /* User string is Empty means don't care */
  198. return true;
  199. if (a1_len != a2_len)
  200. return false;
  201. return 0 == memcmp(a1, a2, a1_len);
  202. }
  203. struct find_oud_t {
  204. const struct osd_dev_info *odi;
  205. struct device *dev;
  206. struct osd_uld_device *oud;
  207. } ;
  208. int _mach_odi(struct device *dev, void *find_data)
  209. {
  210. struct osd_uld_device *oud = container_of(dev, struct osd_uld_device,
  211. class_dev);
  212. struct find_oud_t *fot = find_data;
  213. const struct osd_dev_info *odi = fot->odi;
  214. if (_the_same_or_null(oud->odi.systemid, oud->odi.systemid_len,
  215. odi->systemid, odi->systemid_len) &&
  216. _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len,
  217. odi->osdname, odi->osdname_len)) {
  218. OSD_DEBUG("found device sysid_len=%d osdname=%d\n",
  219. odi->systemid_len, odi->osdname_len);
  220. fot->oud = oud;
  221. return 1;
  222. } else {
  223. return 0;
  224. }
  225. }
  226. /* osduld_info_lookup - Loop through all devices, return the requested osd_dev.
  227. *
  228. * if @odi->systemid_len and/or @odi->osdname_len are zero, they act as a don't
  229. * care. .e.g if they're both zero /dev/osd0 is returned.
  230. */
  231. struct osd_dev *osduld_info_lookup(const struct osd_dev_info *odi)
  232. {
  233. struct find_oud_t find = {.odi = odi};
  234. find.dev = class_find_device(&osd_uld_class, NULL, &find, _mach_odi);
  235. if (likely(find.dev)) {
  236. struct osd_dev_handle *odh = kzalloc(sizeof(*odh), GFP_KERNEL);
  237. if (unlikely(!odh)) {
  238. put_device(find.dev);
  239. return ERR_PTR(-ENOMEM);
  240. }
  241. odh->od = find.oud->od;
  242. odh->oud = find.oud;
  243. return &odh->od;
  244. }
  245. return ERR_PTR(-ENODEV);
  246. }
  247. EXPORT_SYMBOL(osduld_info_lookup);
  248. void osduld_put_device(struct osd_dev *od)
  249. {
  250. if (od && !IS_ERR(od)) {
  251. struct osd_dev_handle *odh =
  252. container_of(od, struct osd_dev_handle, od);
  253. struct osd_uld_device *oud = odh->oud;
  254. BUG_ON(od->scsi_device != oud->od.scsi_device);
  255. /* If scsi has released the device (logout), and exofs has last
  256. * reference on oud it will be freed by above osd_uld_release
  257. * within fput below. But this will oops in cdev_release which
  258. * is called after the fops->release. A get_/put_ pair makes
  259. * sure we have a cdev for the duration of fput
  260. */
  261. if (odh->file) {
  262. get_device(&oud->class_dev);
  263. fput(odh->file);
  264. }
  265. put_device(&oud->class_dev);
  266. kfree(odh);
  267. }
  268. }
  269. EXPORT_SYMBOL(osduld_put_device);
  270. const struct osd_dev_info *osduld_device_info(struct osd_dev *od)
  271. {
  272. struct osd_dev_handle *odh =
  273. container_of(od, struct osd_dev_handle, od);
  274. return &odh->oud->odi;
  275. }
  276. EXPORT_SYMBOL(osduld_device_info);
  277. bool osduld_device_same(struct osd_dev *od, const struct osd_dev_info *odi)
  278. {
  279. struct osd_dev_handle *odh =
  280. container_of(od, struct osd_dev_handle, od);
  281. struct osd_uld_device *oud = odh->oud;
  282. return (oud->odi.systemid_len == odi->systemid_len) &&
  283. _the_same_or_null(oud->odi.systemid, oud->odi.systemid_len,
  284. odi->systemid, odi->systemid_len) &&
  285. (oud->odi.osdname_len == odi->osdname_len) &&
  286. _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len,
  287. odi->osdname, odi->osdname_len);
  288. }
  289. EXPORT_SYMBOL(osduld_device_same);
  290. /*
  291. * Scsi Device operations
  292. */
  293. static int __detect_osd(struct osd_uld_device *oud)
  294. {
  295. struct scsi_device *scsi_device = oud->od.scsi_device;
  296. char caps[OSD_CAP_LEN];
  297. int error;
  298. /* sending a test_unit_ready as first command seems to be needed
  299. * by some targets
  300. */
  301. OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
  302. oud, scsi_device, scsi_device->request_queue);
  303. error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
  304. if (error)
  305. OSD_ERR("warning: scsi_test_unit_ready failed\n");
  306. osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
  307. if (osd_auto_detect_ver(&oud->od, caps, &oud->odi))
  308. return -ENODEV;
  309. return 0;
  310. }
  311. static void __remove(struct device *dev)
  312. {
  313. struct osd_uld_device *oud = container_of(dev, struct osd_uld_device,
  314. class_dev);
  315. struct scsi_device *scsi_device = oud->od.scsi_device;
  316. kfree(oud->odi.osdname);
  317. if (oud->cdev.owner)
  318. cdev_del(&oud->cdev);
  319. osd_dev_fini(&oud->od);
  320. scsi_device_put(scsi_device);
  321. OSD_INFO("osd_remove %s\n",
  322. oud->disk ? oud->disk->disk_name : NULL);
  323. if (oud->disk)
  324. put_disk(oud->disk);
  325. ida_remove(&osd_minor_ida, oud->minor);
  326. kfree(oud);
  327. }
  328. static int osd_probe(struct device *dev)
  329. {
  330. struct scsi_device *scsi_device = to_scsi_device(dev);
  331. struct gendisk *disk;
  332. struct osd_uld_device *oud;
  333. int minor;
  334. int error;
  335. if (scsi_device->type != TYPE_OSD)
  336. return -ENODEV;
  337. do {
  338. if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
  339. return -ENODEV;
  340. error = ida_get_new(&osd_minor_ida, &minor);
  341. } while (error == -EAGAIN);
  342. if (error)
  343. return error;
  344. if (minor >= SCSI_OSD_MAX_MINOR) {
  345. error = -EBUSY;
  346. goto err_retract_minor;
  347. }
  348. error = -ENOMEM;
  349. oud = kzalloc(sizeof(*oud), GFP_KERNEL);
  350. if (NULL == oud)
  351. goto err_retract_minor;
  352. dev_set_drvdata(dev, oud);
  353. oud->minor = minor;
  354. /* allocate a disk and set it up */
  355. /* FIXME: do we need this since sg has already done that */
  356. disk = alloc_disk(1);
  357. if (!disk) {
  358. OSD_ERR("alloc_disk failed\n");
  359. goto err_free_osd;
  360. }
  361. disk->major = SCSI_OSD_MAJOR;
  362. disk->first_minor = oud->minor;
  363. sprintf(disk->disk_name, "osd%d", oud->minor);
  364. oud->disk = disk;
  365. /* hold one more reference to the scsi_device that will get released
  366. * in __release, in case a logout is happening while fs is mounted
  367. */
  368. scsi_device_get(scsi_device);
  369. osd_dev_init(&oud->od, scsi_device);
  370. /* Detect the OSD Version */
  371. error = __detect_osd(oud);
  372. if (error) {
  373. OSD_ERR("osd detection failed, non-compatible OSD device\n");
  374. goto err_put_disk;
  375. }
  376. /* init the char-device for communication with user-mode */
  377. cdev_init(&oud->cdev, &osd_fops);
  378. oud->cdev.owner = THIS_MODULE;
  379. error = cdev_add(&oud->cdev,
  380. MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
  381. if (error) {
  382. OSD_ERR("cdev_add failed\n");
  383. goto err_put_disk;
  384. }
  385. /* class device member */
  386. oud->class_dev.devt = oud->cdev.dev;
  387. oud->class_dev.class = &osd_uld_class;
  388. oud->class_dev.parent = dev;
  389. oud->class_dev.release = __remove;
  390. error = dev_set_name(&oud->class_dev, disk->disk_name);
  391. if (error) {
  392. OSD_ERR("dev_set_name failed => %d\n", error);
  393. goto err_put_cdev;
  394. }
  395. error = device_register(&oud->class_dev);
  396. if (error) {
  397. OSD_ERR("device_register failed => %d\n", error);
  398. goto err_put_cdev;
  399. }
  400. get_device(&oud->class_dev);
  401. OSD_INFO("osd_probe %s\n", disk->disk_name);
  402. return 0;
  403. err_put_cdev:
  404. cdev_del(&oud->cdev);
  405. err_put_disk:
  406. scsi_device_put(scsi_device);
  407. put_disk(disk);
  408. err_free_osd:
  409. dev_set_drvdata(dev, NULL);
  410. kfree(oud);
  411. err_retract_minor:
  412. ida_remove(&osd_minor_ida, minor);
  413. return error;
  414. }
  415. static int osd_remove(struct device *dev)
  416. {
  417. struct scsi_device *scsi_device = to_scsi_device(dev);
  418. struct osd_uld_device *oud = dev_get_drvdata(dev);
  419. if (!oud || (oud->od.scsi_device != scsi_device)) {
  420. OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
  421. dev, oud, oud ? oud->od.scsi_device : NULL,
  422. scsi_device);
  423. }
  424. device_unregister(&oud->class_dev);
  425. put_device(&oud->class_dev);
  426. return 0;
  427. }
  428. /*
  429. * Global driver and scsi registration
  430. */
  431. static struct scsi_driver osd_driver = {
  432. .owner = THIS_MODULE,
  433. .gendrv = {
  434. .name = osd_name,
  435. .probe = osd_probe,
  436. .remove = osd_remove,
  437. }
  438. };
  439. static int __init osd_uld_init(void)
  440. {
  441. int err;
  442. err = class_register(&osd_uld_class);
  443. if (err) {
  444. OSD_ERR("Unable to register sysfs class => %d\n", err);
  445. return err;
  446. }
  447. err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
  448. SCSI_OSD_MAX_MINOR, osd_name);
  449. if (err) {
  450. OSD_ERR("Unable to register major %d for osd ULD => %d\n",
  451. SCSI_OSD_MAJOR, err);
  452. goto err_out;
  453. }
  454. err = scsi_register_driver(&osd_driver.gendrv);
  455. if (err) {
  456. OSD_ERR("scsi_register_driver failed => %d\n", err);
  457. goto err_out_chrdev;
  458. }
  459. OSD_INFO("LOADED %s\n", osd_version_string);
  460. return 0;
  461. err_out_chrdev:
  462. unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
  463. err_out:
  464. class_unregister(&osd_uld_class);
  465. return err;
  466. }
  467. static void __exit osd_uld_exit(void)
  468. {
  469. scsi_unregister_driver(&osd_driver.gendrv);
  470. unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
  471. class_unregister(&osd_uld_class);
  472. OSD_INFO("UNLOADED %s\n", osd_version_string);
  473. }
  474. module_init(osd_uld_init);
  475. module_exit(osd_uld_exit);