msgpool.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/msgpool.h>
  7. static void *alloc_fn(gfp_t gfp_mask, void *arg)
  8. {
  9. struct ceph_msgpool *pool = arg;
  10. void *p;
  11. p = ceph_msg_new(0, pool->front_len, gfp_mask);
  12. if (!p)
  13. pr_err("msgpool %s alloc failed\n", pool->name);
  14. return p;
  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, const char *name)
  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. pool->name = name;
  28. return 0;
  29. }
  30. void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  31. {
  32. mempool_destroy(pool->pool);
  33. }
  34. struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
  35. int front_len)
  36. {
  37. if (front_len > pool->front_len) {
  38. pr_err("msgpool_get pool %s need front %d, pool size is %d\n",
  39. pool->name, front_len, pool->front_len);
  40. WARN_ON(1);
  41. /* try to alloc a fresh message */
  42. return ceph_msg_new(0, front_len, GFP_NOFS);
  43. }
  44. return mempool_alloc(pool->pool, GFP_NOFS);
  45. }
  46. void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  47. {
  48. /* reset msg front_len; user may have changed it */
  49. msg->front.iov_len = pool->front_len;
  50. msg->hdr.front_len = cpu_to_le32(pool->front_len);
  51. kref_init(&msg->kref); /* retake single ref */
  52. }