]>
Commit | Line | Data |
---|---|---|
c30dbb9c SW |
1 | |
2 | #include "ceph_debug.h" | |
3 | #include "buffer.h" | |
c7e337d6 | 4 | #include "decode.h" |
c30dbb9c | 5 | |
b6c1d5b8 | 6 | struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) |
c30dbb9c SW |
7 | { |
8 | struct ceph_buffer *b; | |
9 | ||
10 | b = kmalloc(sizeof(*b), gfp); | |
11 | if (!b) | |
12 | return NULL; | |
b6c1d5b8 SW |
13 | |
14 | b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); | |
15 | if (b->vec.iov_base) { | |
16 | b->is_vmalloc = false; | |
17 | } else { | |
18 | b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); | |
19 | if (!b->vec.iov_base) { | |
20 | kfree(b); | |
21 | return NULL; | |
22 | } | |
23 | b->is_vmalloc = true; | |
24 | } | |
25 | ||
dd26d857 | 26 | kref_init(&b->kref); |
b6c1d5b8 SW |
27 | b->alloc_len = len; |
28 | b->vec.iov_len = len; | |
29 | dout("buffer_new %p\n", b); | |
c30dbb9c SW |
30 | return b; |
31 | } | |
32 | ||
dd26d857 SW |
33 | void ceph_buffer_release(struct kref *kref) |
34 | { | |
35 | struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref); | |
b6c1d5b8 SW |
36 | |
37 | dout("buffer_release %p\n", b); | |
dd26d857 SW |
38 | if (b->vec.iov_base) { |
39 | if (b->is_vmalloc) | |
40 | vfree(b->vec.iov_base); | |
41 | else | |
42 | kfree(b->vec.iov_base); | |
43 | } | |
44 | kfree(b); | |
45 | } | |
46 | ||
c30dbb9c SW |
47 | int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp) |
48 | { | |
49 | b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); | |
50 | if (b->vec.iov_base) { | |
51 | b->is_vmalloc = false; | |
52 | } else { | |
53 | b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); | |
54 | b->is_vmalloc = true; | |
55 | } | |
56 | if (!b->vec.iov_base) | |
57 | return -ENOMEM; | |
58 | b->alloc_len = len; | |
59 | b->vec.iov_len = len; | |
60 | return 0; | |
61 | } | |
62 | ||
c7e337d6 SW |
63 | int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end) |
64 | { | |
65 | size_t len; | |
66 | ||
67 | ceph_decode_need(p, end, sizeof(u32), bad); | |
68 | len = ceph_decode_32(p); | |
69 | dout("decode_buffer len %d\n", (int)len); | |
70 | ceph_decode_need(p, end, len, bad); | |
71 | *b = ceph_buffer_new(len, GFP_NOFS); | |
72 | if (!*b) | |
73 | return -ENOMEM; | |
74 | ceph_decode_copy(p, (*b)->vec.iov_base, len); | |
75 | return 0; | |
76 | bad: | |
77 | return -EINVAL; | |
78 | } |