blob: 847c5da9a0dbc4785ac1723b206ce76af7a6a14f [file] [log] [blame]
Sage Weilc30dbb92009-10-06 11:31:07 -07001
2#include "ceph_debug.h"
3#include "buffer.h"
4
5struct ceph_buffer *ceph_buffer_new(gfp_t gfp)
6{
7 struct ceph_buffer *b;
8
9 b = kmalloc(sizeof(*b), gfp);
10 if (!b)
11 return NULL;
Sage Weildd26d852009-12-05 10:13:33 -080012 kref_init(&b->kref);
Sage Weilc30dbb92009-10-06 11:31:07 -070013 b->vec.iov_base = NULL;
14 b->vec.iov_len = 0;
15 b->alloc_len = 0;
16 return b;
17}
18
Sage Weildd26d852009-12-05 10:13:33 -080019void ceph_buffer_release(struct kref *kref)
20{
21 struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
22 if (b->vec.iov_base) {
23 if (b->is_vmalloc)
24 vfree(b->vec.iov_base);
25 else
26 kfree(b->vec.iov_base);
27 }
28 kfree(b);
29}
30
Sage Weilc30dbb92009-10-06 11:31:07 -070031int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp)
32{
33 b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN);
34 if (b->vec.iov_base) {
35 b->is_vmalloc = false;
36 } else {
37 b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL);
38 b->is_vmalloc = true;
39 }
40 if (!b->vec.iov_base)
41 return -ENOMEM;
42 b->alloc_len = len;
43 b->vec.iov_len = len;
44 return 0;
45}
46