Sage Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 1 | |
| 2 | #include "ceph_debug.h" |
| 3 | #include "buffer.h" |
| 4 | |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame^] | 5 | struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) |
Sage Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 6 | { |
| 7 | struct ceph_buffer *b; |
| 8 | |
| 9 | b = kmalloc(sizeof(*b), gfp); |
| 10 | if (!b) |
| 11 | return NULL; |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame^] | 12 | |
| 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 Weil | dd26d85 | 2009-12-05 10:13:33 -0800 | [diff] [blame] | 25 | kref_init(&b->kref); |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame^] | 26 | b->alloc_len = len; |
| 27 | b->vec.iov_len = len; |
| 28 | dout("buffer_new %p\n", b); |
Sage Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 29 | return b; |
| 30 | } |
| 31 | |
Sage Weil | dd26d85 | 2009-12-05 10:13:33 -0800 | [diff] [blame] | 32 | void ceph_buffer_release(struct kref *kref) |
| 33 | { |
| 34 | struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref); |
Sage Weil | b6c1d5b | 2009-12-07 12:17:17 -0800 | [diff] [blame^] | 35 | |
| 36 | dout("buffer_release %p\n", b); |
Sage Weil | dd26d85 | 2009-12-05 10:13:33 -0800 | [diff] [blame] | 37 | 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 Weil | c30dbb9 | 2009-10-06 11:31:07 -0700 | [diff] [blame] | 46 | int 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 | |