osd_uld.c 15 KB

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