blob: 2576bd452cb8403eb201be578d4d7673b296a82d [file] [log] [blame]
Sage Weilc30dbb92009-10-06 11:31:07 -07001
2#include "ceph_debug.h"
3#include "buffer.h"
4
Sage Weilb6c1d5b2009-12-07 12:17:17 -08005struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
Sage Weilc30dbb92009-10-06 11:31:07 -07006{
7 struct ceph_buffer *b;
8
9 b = kmalloc(sizeof(*b), gfp);
10 if (!b)
11 return NULL;
Sage Weilb6c1d5b2009-12-07 12:17:17 -080012
13 b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN);
14 if (b->vec.iov_base) {
15 b->is_vmalloc = false;
16 } else {
17 b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL);
18 if (!b->vec.iov_base) {
19 kfree(b);
20 return NULL;
21 }
22 b->is_vmalloc = true;
23 }
24
Sage Weildd26d852009-12-05 10:13:33 -080025 kref_init(&b->kref);
Sage Weilb6c1d5b2009-12-07 12:17:17 -080026 b->alloc_len = len;
27 b->vec.iov_len = len;
28 dout("buffer_new %p\n", b);
Sage Weilc30dbb92009-10-06 11:31:07 -070029 return b;
30}
31
Sage Weildd26d852009-12-05 10:13:33 -080032void ceph_buffer_release(struct kref *kref)
33{
34 struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
Sage Weilb6c1d5b2009-12-07 12:17:17 -080035
36 dout("buffer_release %p\n", b);
Sage Weildd26d852009-12-05 10:13:33 -080037 if (b->vec.iov_base) {
38 if (b->is_vmalloc)
39 vfree(b->vec.iov_base);
40 else
41 kfree(b->vec.iov_base);
42 }
43 kfree(b);
44}
45
Sage Weilc30dbb92009-10-06 11:31:07 -070046int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp)
47{
48 b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN);
49 if (b->vec.iov_base) {
50 b->is_vmalloc = false;
51 } else {
52 b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL);
53 b->is_vmalloc = true;
54 }
55 if (!b->vec.iov_base)
56 return -ENOMEM;
57 b->alloc_len = len;
58 b->vec.iov_len = len;
59 return 0;
60}
61