msgpool.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "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 "msgpool.h"
  7. static void *alloc_fn(gfp_t gfp_mask, void *arg)
  8. {
  9. struct ceph_msgpool *pool = arg;
  10. struct ceph_msg *m;
  11. m = ceph_msg_new(0, pool->front_len, 0, 0, NULL);
  12. if (IS_ERR(m))
  13. return NULL;
  14. return m;
  15. }
  16. static void free_fn(void *element, void *arg)
  17. {
  18. ceph_msg_put(element);
  19. }
  20. int ceph_msgpool_init(struct ceph_msgpool *pool,
  21. int front_len, int size, bool blocking)
  22. {
  23. pool->front_len = front_len;
  24. pool->pool = mempool_create(size, alloc_fn, free_fn, pool);
  25. if (!pool->pool)
  26. return -ENOMEM;
  27. return 0;
  28. }
  29. void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  30. {
  31. mempool_destroy(pool->pool);
  32. }
  33. struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
  34. int front_len)
  35. {
  36. if (front_len > pool->front_len) {
  37. struct ceph_msg *msg;
  38. pr_err("msgpool_get pool %p need front %d, pool size is %d\n",
  39. pool, front_len, pool->front_len);
  40. WARN_ON(1);
  41. /* try to alloc a fresh message */
  42. msg = ceph_msg_new(0, front_len, 0, 0, NULL);
  43. if (!IS_ERR(msg))
  44. return msg;
  45. return NULL;
  46. }
  47. return mempool_alloc(pool->pool, GFP_NOFS);
  48. }
  49. void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  50. {
  51. /* reset msg front_len; user may have changed it */
  52. msg->front.iov_len = pool->front_len;
  53. msg->hdr.front_len = cpu_to_le32(pool->front_len);
  54. kref_init(&msg->kref); /* retake single ref */
  55. }