msgpool.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/err.h>
  3. #include <linux/sched.h>
  4. #include <linux/types.h>
  5. #include <linux/vmalloc.h>
  6. #include <linux/ceph/messenger.h>
  7. #include <linux/ceph/msgpool.h>
  8. static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
  9. {
  10. struct ceph_msgpool *pool = arg;
  11. struct ceph_msg *msg;
  12. msg = ceph_msg_new(pool->type, pool->front_len, gfp_mask, true);
  13. if (!msg) {
  14. dout("msgpool_alloc %s failed\n", pool->name);
  15. } else {
  16. dout("msgpool_alloc %s %p\n", pool->name, msg);
  17. msg->pool = pool;
  18. }
  19. return msg;
  20. }
  21. static void msgpool_free(void *element, void *arg)
  22. {
  23. struct ceph_msgpool *pool = arg;
  24. struct ceph_msg *msg = element;
  25. dout("msgpool_release %s %p\n", pool->name, msg);
  26. msg->pool = NULL;
  27. ceph_msg_put(msg);
  28. }
  29. int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
  30. int front_len, int size, bool blocking, const char *name)
  31. {
  32. dout("msgpool %s init\n", name);
  33. pool->type = type;
  34. pool->front_len = front_len;
  35. pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
  36. if (!pool->pool)
  37. return -ENOMEM;
  38. pool->name = name;
  39. return 0;
  40. }
  41. void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  42. {
  43. dout("msgpool %s destroy\n", pool->name);
  44. mempool_destroy(pool->pool);
  45. }
  46. struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
  47. int front_len)
  48. {
  49. struct ceph_msg *msg;
  50. if (front_len > pool->front_len) {
  51. dout("msgpool_get %s need front %d, pool size is %d\n",
  52. pool->name, front_len, pool->front_len);
  53. WARN_ON(1);
  54. /* try to alloc a fresh message */
  55. return ceph_msg_new(pool->type, front_len, GFP_NOFS, false);
  56. }
  57. msg = mempool_alloc(pool->pool, GFP_NOFS);
  58. dout("msgpool_get %s %p\n", pool->name, msg);
  59. return msg;
  60. }
  61. void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  62. {
  63. dout("msgpool_put %s %p\n", pool->name, msg);
  64. /* reset msg front_len; user may have changed it */
  65. msg->front.iov_len = pool->front_len;
  66. msg->hdr.front_len = cpu_to_le32(pool->front_len);
  67. kref_init(&msg->kref); /* retake single ref */
  68. mempool_free(msg, pool->pool);
  69. }