blob: ddec1c10ac80fc3b993cdc7ba5524e317382bd04 [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 Weild50b4092012-07-09 14:22:34 -070015 msg = ceph_msg_new(pool->type, 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
Sage Weild50b4092012-07-09 14:22:34 -070035int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
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 Weild50b4092012-07-09 14:22:34 -070039 pool->type = type;
Sage Weil8fc91fd2009-10-06 11:31:13 -070040 pool->front_len = front_len;
Sage Weil51853522011-08-09 14:48:11 -070041 pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
Sage Weild52f8472010-04-01 15:23:14 -070042 if (!pool->pool)
43 return -ENOMEM;
Sage Weil4f482802010-04-24 09:56:35 -070044 pool->name = name;
Sage Weild52f8472010-04-01 15:23:14 -070045 return 0;
Sage Weil8fc91fd2009-10-06 11:31:13 -070046}
47
48void ceph_msgpool_destroy(struct ceph_msgpool *pool)
49{
Sage Weil51853522011-08-09 14:48:11 -070050 dout("msgpool %s destroy\n", pool->name);
Sage Weild52f8472010-04-01 15:23:14 -070051 mempool_destroy(pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070052}
53
Sage Weild52f8472010-04-01 15:23:14 -070054struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
55 int front_len)
Sage Weil8fc91fd2009-10-06 11:31:13 -070056{
Sage Weil51853522011-08-09 14:48:11 -070057 struct ceph_msg *msg;
58
Sage Weild52f8472010-04-01 15:23:14 -070059 if (front_len > pool->front_len) {
Sage Weil51853522011-08-09 14:48:11 -070060 dout("msgpool_get %s need front %d, pool size is %d\n",
Sage Weil4f482802010-04-24 09:56:35 -070061 pool->name, front_len, pool->front_len);
Sage Weil8f3bc052009-10-14 17:36:07 -070062 WARN_ON(1);
63
64 /* try to alloc a fresh message */
Sage Weild50b4092012-07-09 14:22:34 -070065 return ceph_msg_new(pool->type, front_len, GFP_NOFS, false);
Sage Weil8f3bc052009-10-14 17:36:07 -070066 }
67
Sage Weil51853522011-08-09 14:48:11 -070068 msg = mempool_alloc(pool->pool, GFP_NOFS);
69 dout("msgpool_get %s %p\n", pool->name, msg);
70 return msg;
Sage Weil8fc91fd2009-10-06 11:31:13 -070071}
72
73void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
74{
Sage Weil51853522011-08-09 14:48:11 -070075 dout("msgpool_put %s %p\n", pool->name, msg);
76
Sage Weild52f8472010-04-01 15:23:14 -070077 /* reset msg front_len; user may have changed it */
78 msg->front.iov_len = pool->front_len;
79 msg->hdr.front_len = cpu_to_le32(pool->front_len);
Sage Weil3ca02ef2010-03-01 15:25:00 -080080
Sage Weild52f8472010-04-01 15:23:14 -070081 kref_init(&msg->kref); /* retake single ref */
Sage Weil51853522011-08-09 14:48:11 -070082 mempool_free(msg, pool->pool);
Sage Weil8fc91fd2009-10-06 11:31:13 -070083}