Blame view

net/ceph/msgpool.c 1.94 KB
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
1
  #include <linux/ceph/ceph_debug.h>
8fc91fd85   Sage Weil   ceph: message pools
2
3
4
5
6
  
  #include <linux/err.h>
  #include <linux/sched.h>
  #include <linux/types.h>
  #include <linux/vmalloc.h>
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
7
  #include <linux/ceph/msgpool.h>
8fc91fd85   Sage Weil   ceph: message pools
8

5185352c1   Sage Weil   libceph: fix msgpool
9
  static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
d52f847a8   Sage Weil   ceph: rewrite msg...
10
11
  {
  	struct ceph_msgpool *pool = arg;
5185352c1   Sage Weil   libceph: fix msgpool
12
  	struct ceph_msg *msg;
8fc91fd85   Sage Weil   ceph: message pools
13

b61c27636   Sage Weil   libceph: don't co...
14
  	msg = ceph_msg_new(0, pool->front_len, gfp_mask, true);
5185352c1   Sage Weil   libceph: fix msgpool
15
16
17
18
19
20
21
22
23
  	if (!msg) {
  		dout("msgpool_alloc %s failed
  ", pool->name);
  	} else {
  		dout("msgpool_alloc %s %p
  ", pool->name, msg);
  		msg->pool = pool;
  	}
  	return msg;
d52f847a8   Sage Weil   ceph: rewrite msg...
24
  }
8fc91fd85   Sage Weil   ceph: message pools
25

5185352c1   Sage Weil   libceph: fix msgpool
26
  static void msgpool_free(void *element, void *arg)
8fc91fd85   Sage Weil   ceph: message pools
27
  {
5185352c1   Sage Weil   libceph: fix msgpool
28
29
30
31
32
33
34
  	struct ceph_msgpool *pool = arg;
  	struct ceph_msg *msg = element;
  
  	dout("msgpool_release %s %p
  ", pool->name, msg);
  	msg->pool = NULL;
  	ceph_msg_put(msg);
8fc91fd85   Sage Weil   ceph: message pools
35
36
37
  }
  
  int ceph_msgpool_init(struct ceph_msgpool *pool,
4f48280ee   Sage Weil   ceph: name msgpoo...
38
  		      int front_len, int size, bool blocking, const char *name)
8fc91fd85   Sage Weil   ceph: message pools
39
  {
5185352c1   Sage Weil   libceph: fix msgpool
40
41
  	dout("msgpool %s init
  ", name);
8fc91fd85   Sage Weil   ceph: message pools
42
  	pool->front_len = front_len;
5185352c1   Sage Weil   libceph: fix msgpool
43
  	pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
d52f847a8   Sage Weil   ceph: rewrite msg...
44
45
  	if (!pool->pool)
  		return -ENOMEM;
4f48280ee   Sage Weil   ceph: name msgpoo...
46
  	pool->name = name;
d52f847a8   Sage Weil   ceph: rewrite msg...
47
  	return 0;
8fc91fd85   Sage Weil   ceph: message pools
48
49
50
51
  }
  
  void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  {
5185352c1   Sage Weil   libceph: fix msgpool
52
53
  	dout("msgpool %s destroy
  ", pool->name);
d52f847a8   Sage Weil   ceph: rewrite msg...
54
  	mempool_destroy(pool->pool);
8fc91fd85   Sage Weil   ceph: message pools
55
  }
d52f847a8   Sage Weil   ceph: rewrite msg...
56
57
  struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
  				  int front_len)
8fc91fd85   Sage Weil   ceph: message pools
58
  {
5185352c1   Sage Weil   libceph: fix msgpool
59
  	struct ceph_msg *msg;
d52f847a8   Sage Weil   ceph: rewrite msg...
60
  	if (front_len > pool->front_len) {
5185352c1   Sage Weil   libceph: fix msgpool
61
62
  		dout("msgpool_get %s need front %d, pool size is %d
  ",
4f48280ee   Sage Weil   ceph: name msgpoo...
63
  		       pool->name, front_len, pool->front_len);
8f3bc053c   Sage Weil   ceph: warn on all...
64
65
66
  		WARN_ON(1);
  
  		/* try to alloc a fresh message */
b61c27636   Sage Weil   libceph: don't co...
67
  		return ceph_msg_new(0, front_len, GFP_NOFS, false);
8f3bc053c   Sage Weil   ceph: warn on all...
68
  	}
5185352c1   Sage Weil   libceph: fix msgpool
69
70
71
72
  	msg = mempool_alloc(pool->pool, GFP_NOFS);
  	dout("msgpool_get %s %p
  ", pool->name, msg);
  	return msg;
8fc91fd85   Sage Weil   ceph: message pools
73
74
75
76
  }
  
  void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  {
5185352c1   Sage Weil   libceph: fix msgpool
77
78
  	dout("msgpool_put %s %p
  ", pool->name, msg);
d52f847a8   Sage Weil   ceph: rewrite msg...
79
80
81
  	/* reset msg front_len; user may have changed it */
  	msg->front.iov_len = pool->front_len;
  	msg->hdr.front_len = cpu_to_le32(pool->front_len);
3ca02ef96   Sage Weil   ceph: reset front...
82

d52f847a8   Sage Weil   ceph: rewrite msg...
83
  	kref_init(&msg->kref);  /* retake single ref */
5185352c1   Sage Weil   libceph: fix msgpool
84
  	mempool_free(msg, pool->pool);
8fc91fd85   Sage Weil   ceph: message pools
85
  }