Sage Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 1 | |
| 2 | #include "ceph_debug.h" |
| 3 | #include "buffer.h" |
| 4 | |
| 5 | struct 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 Weil | dd26d85 | 2009-12-05 10:13:33 -0800 | [diff] [blame^] | 12 | kref_init(&b->kref); |
Sage Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 13 | b->vec.iov_base = NULL; |
| 14 | b->vec.iov_len = 0; |
| 15 | b->alloc_len = 0; |
| 16 | return b; |
| 17 | } |
| 18 | |
Sage Weil | dd26d85 | 2009-12-05 10:13:33 -0800 | [diff] [blame^] | 19 | void 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 Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 31 | int 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 | |