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