blob: 11d5f4196a73cfeef3492389c76f9f285c50188d [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil8fc91fd2009-10-06 11:31:13 -07002
3#include <linux/err.h>
4#include <linux/sched.h>
5#include <linux/types.h>
6#include <linux/vmalloc.h>
7
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include <linux/ceph/msgpool.h>
Sage Weil8fc91fd2009-10-06 11:31:13 -07009
Sage Weil51853522011-08-09 14:48:11 -070010static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
Sage Weil8fc91fd2009-10-06 11:31:13 -070011{
Sage Weild52f8472010-04-01 15:23:14 -070012 struct ceph_msgpool *pool = arg;
Sage Weil51853522011-08-09 14:48:11 -070013 struct ceph_msg *msg;
Sage Weil8fc91fd2009-10-06 11:31:13 -070014
Sage Weilb61c2762011-08-09 15:03:46 -070015 msg = ceph_msg_new(0, pool->front_len, gfp_mask, true);
Sage Weil51853522011-08-09 14:48:11 -070016 if (!msg) {
17 dout("msgpool_alloc %s failed\n", pool->name);
18 } else {
19 dout("msgpool_alloc %s %p\n", pool->name, msg);
20 msg->pool = pool;
21 }
22 return msg;
Sage Weild52f8472010-04-01 15:23:14 -070023}
24
Sage Weil51853522011-08-09 14:48:11 -070025static void msgpool_free(void *element, void *arg)
Sage Weild52f8472010-04-01 15:23:14 -070026{
Sage Weil51853522011-08-09 14:48:11 -070027 struct ceph_msgpool *pool = arg;
28 struct ceph_msg *msg = element;
29
30 dout("msgpool_release %s %p\n", pool->name, msg);
31 msg->pool = NULL;
32 ceph_msg_put(msg);
Sage Weil8fc91fd2009-10-06 11:31:13 -070033}
34
35int ceph_msgpool_init(struct ceph_msgpool *pool,
Sage Weil4f482802010-04-24 09:56:35 -070036 int front_len, int size, bool blocking, const char *name)
Sage Weil8fc91fd2009-10-06 11:31:13 -070037{
Sage Weil51853522011-08-09 14:48:11 -070038 dout("msgpool %s init\n", name);
Sage Weil8fc91fd2009-10-06 11:31:13 -070039 pool->front_len = front_len;
Sage Weil51853522011-08-09 14:48:11 -070040 pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
Sage Weild52f8472010-04-01 15:23:14 -070041 if (!pool->pool)
42 return -ENOMEM;
Sage Weil4f482802010-04-24 09:56:35 -070043 pool->name = name;
Sage Weild52f8472010-04-01 15:23:14 -070044 return 0;
Sage Weil8fc91fd2009-10-06 11:31:13 -070045}
46
47void ceph_msgpool_destroy(struct ceph_msgpool *pool)
48{
Sage Weil51853522011-08-09 14:48:11 -070049 dout("msgpool %s destroy\n", pool->name);
Sage Weild52f8472010-04-01 15:23:14 -070050 mempool_destroy(pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070051}
52
Sage Weild52f8472010-04-01 15:23:14 -070053struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
54 int front_len)
Sage Weil8fc91fd2009-10-06 11:31:13 -070055{
Sage Weil51853522011-08-09 14:48:11 -070056 struct ceph_msg *msg;
57
Sage Weild52f8472010-04-01 15:23:14 -070058 if (front_len > pool->front_len) {
Sage Weil51853522011-08-09 14:48:11 -070059 dout("msgpool_get %s need front %d, pool size is %d\n",
Sage Weil4f482802010-04-24 09:56:35 -070060 pool->name, front_len, pool->front_len);
Sage Weil8f3bc052009-10-14 17:36:07 -070061 WARN_ON(1);
62
63 /* try to alloc a fresh message */
Sage Weilb61c2762011-08-09 15:03:46 -070064 return ceph_msg_new(0, front_len, GFP_NOFS, false);
Sage Weil8f3bc052009-10-14 17:36:07 -070065 }
66
Sage Weil51853522011-08-09 14:48:11 -070067 msg = mempool_alloc(pool->pool, GFP_NOFS);
68 dout("msgpool_get %s %p\n", pool->name, msg);
69 return msg;
Sage Weil8fc91fd2009-10-06 11:31:13 -070070}
71
72void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
73{
Sage Weil51853522011-08-09 14:48:11 -070074 dout("msgpool_put %s %p\n", pool->name, msg);
75
Sage Weild52f8472010-04-01 15:23:14 -070076 /* reset msg front_len; user may have changed it */
77 msg->front.iov_len = pool->front_len;
78 msg->hdr.front_len = cpu_to_le32(pool->front_len);
Sage Weil3ca02ef2010-03-01 15:25:00 -080079
Sage Weild52f8472010-04-01 15:23:14 -070080 kref_init(&msg->kref); /* retake single ref */
Sage Weil51853522011-08-09 14:48:11 -070081 mempool_free(msg, pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070082}