osdblk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. osdblk.c -- Export a single SCSI OSD object as a Linux block device
  3. Copyright 2009 Red Hat, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING. If not, write to
  13. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14. Instructions for use
  15. --------------------
  16. 1) Map a Linux block device to an existing OSD object.
  17. In this example, we will use partition id 1234, object id 5678,
  18. OSD device /dev/osd1.
  19. $ echo "1234 5678 /dev/osd1" > /sys/class/osdblk/add
  20. 2) List all active blkdev<->object mappings.
  21. In this example, we have performed step #1 twice, creating two blkdevs,
  22. mapped to two separate OSD objects.
  23. $ cat /sys/class/osdblk/list
  24. 0 174 1234 5678 /dev/osd1
  25. 1 179 1994 897123 /dev/osd0
  26. The columns, in order, are:
  27. - blkdev unique id
  28. - blkdev assigned major
  29. - OSD object partition id
  30. - OSD object id
  31. - OSD device
  32. 3) Remove an active blkdev<->object mapping.
  33. In this example, we remove the mapping with blkdev unique id 1.
  34. $ echo 1 > /sys/class/osdblk/remove
  35. NOTE: The actual creation and deletion of OSD objects is outside the scope
  36. of this driver.
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/device.h>
  40. #include <linux/module.h>
  41. #include <linux/fs.h>
  42. #include <linux/slab.h>
  43. #include <scsi/osd_initiator.h>
  44. #include <scsi/osd_attributes.h>
  45. #include <scsi/osd_sec.h>
  46. #include <scsi/scsi_device.h>
  47. #define DRV_NAME "osdblk"
  48. #define PFX DRV_NAME ": "
  49. /* #define _OSDBLK_DEBUG */
  50. #ifdef _OSDBLK_DEBUG
  51. #define OSDBLK_DEBUG(fmt, a...) \
  52. printk(KERN_NOTICE "osdblk @%s:%d: " fmt, __func__, __LINE__, ##a)
  53. #else
  54. #define OSDBLK_DEBUG(fmt, a...) \
  55. do { if (0) printk(fmt, ##a); } while (0)
  56. #endif
  57. MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
  58. MODULE_DESCRIPTION("block device inside an OSD object osdblk.ko");
  59. MODULE_LICENSE("GPL");
  60. struct osdblk_device;
  61. enum {
  62. OSDBLK_MINORS_PER_MAJOR = 256, /* max minors per blkdev */
  63. OSDBLK_MAX_REQ = 32, /* max parallel requests */
  64. OSDBLK_OP_TIMEOUT = 4 * 60, /* sync OSD req timeout */
  65. };
  66. struct osdblk_request {
  67. struct request *rq; /* blk layer request */
  68. struct bio *bio; /* cloned bio */
  69. struct osdblk_device *osdev; /* associated blkdev */
  70. };
  71. struct osdblk_device {
  72. int id; /* blkdev unique id */
  73. int major; /* blkdev assigned major */
  74. struct gendisk *disk; /* blkdev's gendisk and rq */
  75. struct request_queue *q;
  76. struct osd_dev *osd; /* associated OSD */
  77. char name[32]; /* blkdev name, e.g. osdblk34 */
  78. spinlock_t lock; /* queue lock */
  79. struct osd_obj_id obj; /* OSD partition, obj id */
  80. uint8_t obj_cred[OSD_CAP_LEN]; /* OSD cred */
  81. struct osdblk_request req[OSDBLK_MAX_REQ]; /* request table */
  82. struct list_head node;
  83. char osd_path[0]; /* OSD device path */
  84. };
  85. static struct class *class_osdblk; /* /sys/class/osdblk */
  86. static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
  87. static LIST_HEAD(osdblkdev_list);
  88. static const struct block_device_operations osdblk_bd_ops = {
  89. .owner = THIS_MODULE,
  90. };
  91. static const struct osd_attr g_attr_logical_length = ATTR_DEF(
  92. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  93. static void osdblk_make_credential(u8 cred_a[OSD_CAP_LEN],
  94. const struct osd_obj_id *obj)
  95. {
  96. osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
  97. }
  98. /* copied from exofs; move to libosd? */
  99. /*
  100. * Perform a synchronous OSD operation. copied from exofs; move to libosd?
  101. */
  102. static int osd_sync_op(struct osd_request *or, int timeout, uint8_t *credential)
  103. {
  104. int ret;
  105. or->timeout = timeout;
  106. ret = osd_finalize_request(or, 0, credential, NULL);
  107. if (ret)
  108. return ret;
  109. ret = osd_execute_request(or);
  110. /* osd_req_decode_sense(or, ret); */
  111. return ret;
  112. }
  113. /*
  114. * Perform an asynchronous OSD operation. copied from exofs; move to libosd?
  115. */
  116. static int osd_async_op(struct osd_request *or, osd_req_done_fn *async_done,
  117. void *caller_context, u8 *cred)
  118. {
  119. int ret;
  120. ret = osd_finalize_request(or, 0, cred, NULL);
  121. if (ret)
  122. return ret;
  123. ret = osd_execute_request_async(or, async_done, caller_context);
  124. return ret;
  125. }
  126. /* copied from exofs; move to libosd? */
  127. static int extract_attr_from_req(struct osd_request *or, struct osd_attr *attr)
  128. {
  129. struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
  130. void *iter = NULL;
  131. int nelem;
  132. do {
  133. nelem = 1;
  134. osd_req_decode_get_attr_list(or, &cur_attr, &nelem, &iter);
  135. if ((cur_attr.attr_page == attr->attr_page) &&
  136. (cur_attr.attr_id == attr->attr_id)) {
  137. attr->len = cur_attr.len;
  138. attr->val_ptr = cur_attr.val_ptr;
  139. return 0;
  140. }
  141. } while (iter);
  142. return -EIO;
  143. }
  144. static int osdblk_get_obj_size(struct osdblk_device *osdev, u64 *size_out)
  145. {
  146. struct osd_request *or;
  147. struct osd_attr attr;
  148. int ret;
  149. /* start request */
  150. or = osd_start_request(osdev->osd, GFP_KERNEL);
  151. if (!or)
  152. return -ENOMEM;
  153. /* create a get-attributes(length) request */
  154. osd_req_get_attributes(or, &osdev->obj);
  155. osd_req_add_get_attr_list(or, &g_attr_logical_length, 1);
  156. /* execute op synchronously */
  157. ret = osd_sync_op(or, OSDBLK_OP_TIMEOUT, osdev->obj_cred);
  158. if (ret)
  159. goto out;
  160. /* extract length from returned attribute info */
  161. attr = g_attr_logical_length;
  162. ret = extract_attr_from_req(or, &attr);
  163. if (ret)
  164. goto out;
  165. *size_out = get_unaligned_be64(attr.val_ptr);
  166. out:
  167. osd_end_request(or);
  168. return ret;
  169. }
  170. static void osdblk_osd_complete(struct osd_request *or, void *private)
  171. {
  172. struct osdblk_request *orq = private;
  173. struct osd_sense_info osi;
  174. int ret = osd_req_decode_sense(or, &osi);
  175. if (ret) {
  176. ret = -EIO;
  177. OSDBLK_DEBUG("osdblk_osd_complete with err=%d\n", ret);
  178. }
  179. /* complete OSD request */
  180. osd_end_request(or);
  181. /* complete request passed to osdblk by block layer */
  182. __blk_end_request_all(orq->rq, ret);
  183. }
  184. static void bio_chain_put(struct bio *chain)
  185. {
  186. struct bio *tmp;
  187. while (chain) {
  188. tmp = chain;
  189. chain = chain->bi_next;
  190. bio_put(tmp);
  191. }
  192. }
  193. static struct bio *bio_chain_clone(struct bio *old_chain, gfp_t gfpmask)
  194. {
  195. struct bio *tmp, *new_chain = NULL, *tail = NULL;
  196. while (old_chain) {
  197. tmp = bio_clone_kmalloc(old_chain, gfpmask);
  198. if (!tmp)
  199. goto err_out;
  200. tmp->bi_bdev = NULL;
  201. gfpmask &= ~__GFP_DIRECT_RECLAIM;
  202. tmp->bi_next = NULL;
  203. if (!new_chain)
  204. new_chain = tail = tmp;
  205. else {
  206. tail->bi_next = tmp;
  207. tail = tmp;
  208. }
  209. old_chain = old_chain->bi_next;
  210. }
  211. return new_chain;
  212. err_out:
  213. OSDBLK_DEBUG("bio_chain_clone with err\n");
  214. bio_chain_put(new_chain);
  215. return NULL;
  216. }
  217. static void osdblk_rq_fn(struct request_queue *q)
  218. {
  219. struct osdblk_device *osdev = q->queuedata;
  220. while (1) {
  221. struct request *rq;
  222. struct osdblk_request *orq;
  223. struct osd_request *or;
  224. struct bio *bio;
  225. bool do_write, do_flush;
  226. /* peek at request from block layer */
  227. rq = blk_fetch_request(q);
  228. if (!rq)
  229. break;
  230. /* filter out block requests we don't understand */
  231. if (rq->cmd_type != REQ_TYPE_FS) {
  232. blk_end_request_all(rq, 0);
  233. continue;
  234. }
  235. /* deduce our operation (read, write, flush) */
  236. /* I wish the block layer simplified cmd_type/cmd_flags/cmd[]
  237. * into a clearly defined set of RPC commands:
  238. * read, write, flush, scsi command, power mgmt req,
  239. * driver-specific, etc.
  240. */
  241. do_flush = (req_op(rq) == REQ_OP_FLUSH);
  242. do_write = (rq_data_dir(rq) == WRITE);
  243. if (!do_flush) { /* osd_flush does not use a bio */
  244. /* a bio clone to be passed down to OSD request */
  245. bio = bio_chain_clone(rq->bio, GFP_ATOMIC);
  246. if (!bio)
  247. break;
  248. } else
  249. bio = NULL;
  250. /* alloc internal OSD request, for OSD command execution */
  251. or = osd_start_request(osdev->osd, GFP_ATOMIC);
  252. if (!or) {
  253. bio_chain_put(bio);
  254. OSDBLK_DEBUG("osd_start_request with err\n");
  255. break;
  256. }
  257. orq = &osdev->req[rq->tag];
  258. orq->rq = rq;
  259. orq->bio = bio;
  260. orq->osdev = osdev;
  261. /* init OSD command: flush, write or read */
  262. if (do_flush)
  263. osd_req_flush_object(or, &osdev->obj,
  264. OSD_CDB_FLUSH_ALL, 0, 0);
  265. else if (do_write)
  266. osd_req_write(or, &osdev->obj, blk_rq_pos(rq) * 512ULL,
  267. bio, blk_rq_bytes(rq));
  268. else
  269. osd_req_read(or, &osdev->obj, blk_rq_pos(rq) * 512ULL,
  270. bio, blk_rq_bytes(rq));
  271. OSDBLK_DEBUG("%s 0x%x bytes at 0x%llx\n",
  272. do_flush ? "flush" : do_write ?
  273. "write" : "read", blk_rq_bytes(rq),
  274. blk_rq_pos(rq) * 512ULL);
  275. /* begin OSD command execution */
  276. if (osd_async_op(or, osdblk_osd_complete, orq,
  277. osdev->obj_cred)) {
  278. osd_end_request(or);
  279. blk_requeue_request(q, rq);
  280. bio_chain_put(bio);
  281. OSDBLK_DEBUG("osd_execute_request_async with err\n");
  282. break;
  283. }
  284. /* remove the special 'flush' marker, now that the command
  285. * is executing
  286. */
  287. rq->special = NULL;
  288. }
  289. }
  290. static void osdblk_free_disk(struct osdblk_device *osdev)
  291. {
  292. struct gendisk *disk = osdev->disk;
  293. if (!disk)
  294. return;
  295. if (disk->flags & GENHD_FL_UP)
  296. del_gendisk(disk);
  297. if (disk->queue)
  298. blk_cleanup_queue(disk->queue);
  299. put_disk(disk);
  300. }
  301. static int osdblk_init_disk(struct osdblk_device *osdev)
  302. {
  303. struct gendisk *disk;
  304. struct request_queue *q;
  305. int rc;
  306. u64 obj_size = 0;
  307. /* contact OSD, request size info about the object being mapped */
  308. rc = osdblk_get_obj_size(osdev, &obj_size);
  309. if (rc)
  310. return rc;
  311. /* create gendisk info */
  312. disk = alloc_disk(OSDBLK_MINORS_PER_MAJOR);
  313. if (!disk)
  314. return -ENOMEM;
  315. sprintf(disk->disk_name, DRV_NAME "%d", osdev->id);
  316. disk->major = osdev->major;
  317. disk->first_minor = 0;
  318. disk->fops = &osdblk_bd_ops;
  319. disk->private_data = osdev;
  320. /* init rq */
  321. q = blk_init_queue(osdblk_rq_fn, &osdev->lock);
  322. if (!q) {
  323. put_disk(disk);
  324. return -ENOMEM;
  325. }
  326. /* switch queue to TCQ mode; allocate tag map */
  327. rc = blk_queue_init_tags(q, OSDBLK_MAX_REQ, NULL, BLK_TAG_ALLOC_FIFO);
  328. if (rc) {
  329. blk_cleanup_queue(q);
  330. put_disk(disk);
  331. return rc;
  332. }
  333. /* Set our limits to the lower device limits, because osdblk cannot
  334. * sleep when allocating a lower-request and therefore cannot be
  335. * bouncing.
  336. */
  337. blk_queue_stack_limits(q, osd_request_queue(osdev->osd));
  338. blk_queue_prep_rq(q, blk_queue_start_tag);
  339. blk_queue_write_cache(q, true, false);
  340. disk->queue = q;
  341. q->queuedata = osdev;
  342. osdev->disk = disk;
  343. osdev->q = q;
  344. /* finally, announce the disk to the world */
  345. set_capacity(disk, obj_size / 512ULL);
  346. add_disk(disk);
  347. printk(KERN_INFO "%s: Added of size 0x%llx\n",
  348. disk->disk_name, (unsigned long long)obj_size);
  349. return 0;
  350. }
  351. /********************************************************************
  352. * /sys/class/osdblk/
  353. * add map OSD object to blkdev
  354. * remove unmap OSD object
  355. * list show mappings
  356. *******************************************************************/
  357. static void class_osdblk_release(struct class *cls)
  358. {
  359. kfree(cls);
  360. }
  361. static ssize_t class_osdblk_list(struct class *c,
  362. struct class_attribute *attr,
  363. char *data)
  364. {
  365. int n = 0;
  366. struct list_head *tmp;
  367. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  368. list_for_each(tmp, &osdblkdev_list) {
  369. struct osdblk_device *osdev;
  370. osdev = list_entry(tmp, struct osdblk_device, node);
  371. n += sprintf(data+n, "%d %d %llu %llu %s\n",
  372. osdev->id,
  373. osdev->major,
  374. osdev->obj.partition,
  375. osdev->obj.id,
  376. osdev->osd_path);
  377. }
  378. mutex_unlock(&ctl_mutex);
  379. return n;
  380. }
  381. static ssize_t class_osdblk_add(struct class *c,
  382. struct class_attribute *attr,
  383. const char *buf, size_t count)
  384. {
  385. struct osdblk_device *osdev;
  386. ssize_t rc;
  387. int irc, new_id = 0;
  388. struct list_head *tmp;
  389. if (!try_module_get(THIS_MODULE))
  390. return -ENODEV;
  391. /* new osdblk_device object */
  392. osdev = kzalloc(sizeof(*osdev) + strlen(buf) + 1, GFP_KERNEL);
  393. if (!osdev) {
  394. rc = -ENOMEM;
  395. goto err_out_mod;
  396. }
  397. /* static osdblk_device initialization */
  398. spin_lock_init(&osdev->lock);
  399. INIT_LIST_HEAD(&osdev->node);
  400. /* generate unique id: find highest unique id, add one */
  401. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  402. list_for_each(tmp, &osdblkdev_list) {
  403. struct osdblk_device *osdev;
  404. osdev = list_entry(tmp, struct osdblk_device, node);
  405. if (osdev->id > new_id)
  406. new_id = osdev->id + 1;
  407. }
  408. osdev->id = new_id;
  409. /* add to global list */
  410. list_add_tail(&osdev->node, &osdblkdev_list);
  411. mutex_unlock(&ctl_mutex);
  412. /* parse add command */
  413. if (sscanf(buf, "%llu %llu %s", &osdev->obj.partition, &osdev->obj.id,
  414. osdev->osd_path) != 3) {
  415. rc = -EINVAL;
  416. goto err_out_slot;
  417. }
  418. /* initialize rest of new object */
  419. sprintf(osdev->name, DRV_NAME "%d", osdev->id);
  420. /* contact requested OSD */
  421. osdev->osd = osduld_path_lookup(osdev->osd_path);
  422. if (IS_ERR(osdev->osd)) {
  423. rc = PTR_ERR(osdev->osd);
  424. goto err_out_slot;
  425. }
  426. /* build OSD credential */
  427. osdblk_make_credential(osdev->obj_cred, &osdev->obj);
  428. /* register our block device */
  429. irc = register_blkdev(0, osdev->name);
  430. if (irc < 0) {
  431. rc = irc;
  432. goto err_out_osd;
  433. }
  434. osdev->major = irc;
  435. /* set up and announce blkdev mapping */
  436. rc = osdblk_init_disk(osdev);
  437. if (rc)
  438. goto err_out_blkdev;
  439. return count;
  440. err_out_blkdev:
  441. unregister_blkdev(osdev->major, osdev->name);
  442. err_out_osd:
  443. osduld_put_device(osdev->osd);
  444. err_out_slot:
  445. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  446. list_del_init(&osdev->node);
  447. mutex_unlock(&ctl_mutex);
  448. kfree(osdev);
  449. err_out_mod:
  450. OSDBLK_DEBUG("Error adding device %s\n", buf);
  451. module_put(THIS_MODULE);
  452. return rc;
  453. }
  454. static ssize_t class_osdblk_remove(struct class *c,
  455. struct class_attribute *attr,
  456. const char *buf,
  457. size_t count)
  458. {
  459. struct osdblk_device *osdev = NULL;
  460. int target_id, rc;
  461. unsigned long ul;
  462. struct list_head *tmp;
  463. rc = kstrtoul(buf, 10, &ul);
  464. if (rc)
  465. return rc;
  466. /* convert to int; abort if we lost anything in the conversion */
  467. target_id = (int) ul;
  468. if (target_id != ul)
  469. return -EINVAL;
  470. /* remove object from list immediately */
  471. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  472. list_for_each(tmp, &osdblkdev_list) {
  473. osdev = list_entry(tmp, struct osdblk_device, node);
  474. if (osdev->id == target_id) {
  475. list_del_init(&osdev->node);
  476. break;
  477. }
  478. osdev = NULL;
  479. }
  480. mutex_unlock(&ctl_mutex);
  481. if (!osdev)
  482. return -ENOENT;
  483. /* clean up and free blkdev and associated OSD connection */
  484. osdblk_free_disk(osdev);
  485. unregister_blkdev(osdev->major, osdev->name);
  486. osduld_put_device(osdev->osd);
  487. kfree(osdev);
  488. /* release module ref */
  489. module_put(THIS_MODULE);
  490. return count;
  491. }
  492. static struct class_attribute class_osdblk_attrs[] = {
  493. __ATTR(add, 0200, NULL, class_osdblk_add),
  494. __ATTR(remove, 0200, NULL, class_osdblk_remove),
  495. __ATTR(list, 0444, class_osdblk_list, NULL),
  496. __ATTR_NULL
  497. };
  498. static int osdblk_sysfs_init(void)
  499. {
  500. int ret = 0;
  501. /*
  502. * create control files in sysfs
  503. * /sys/class/osdblk/...
  504. */
  505. class_osdblk = kzalloc(sizeof(*class_osdblk), GFP_KERNEL);
  506. if (!class_osdblk)
  507. return -ENOMEM;
  508. class_osdblk->name = DRV_NAME;
  509. class_osdblk->owner = THIS_MODULE;
  510. class_osdblk->class_release = class_osdblk_release;
  511. class_osdblk->class_attrs = class_osdblk_attrs;
  512. ret = class_register(class_osdblk);
  513. if (ret) {
  514. kfree(class_osdblk);
  515. class_osdblk = NULL;
  516. printk(PFX "failed to create class osdblk\n");
  517. return ret;
  518. }
  519. return 0;
  520. }
  521. static void osdblk_sysfs_cleanup(void)
  522. {
  523. if (class_osdblk)
  524. class_destroy(class_osdblk);
  525. class_osdblk = NULL;
  526. }
  527. static int __init osdblk_init(void)
  528. {
  529. int rc;
  530. rc = osdblk_sysfs_init();
  531. if (rc)
  532. return rc;
  533. return 0;
  534. }
  535. static void __exit osdblk_exit(void)
  536. {
  537. osdblk_sysfs_cleanup();
  538. }
  539. module_init(osdblk_init);
  540. module_exit(osdblk_exit);