]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/ceph/buffer.c
ceph: use kref for ceph_buffer
[mv-sheeva.git] / fs / ceph / buffer.c
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;
12         kref_init(&b->kref);
13         b->vec.iov_base = NULL;
14         b->vec.iov_len = 0;
15         b->alloc_len = 0;
16         return b;
17 }
18
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
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