diff options
author | Sage Weil <sage@newdream.net> | 2009-10-06 14:31:07 -0400 |
---|---|---|
committer | Sage Weil <sage@newdream.net> | 2009-10-06 14:31:07 -0400 |
commit | c30dbb9cc7fc75ab1d0ee6fb084ba4684f7a665d (patch) | |
tree | 7e702615046c0c866bb0229c731f86916c526115 /fs | |
parent | de57606c23afded22202825b3db8a5d61859f198 (diff) |
ceph: ref counted buffer
struct ceph_buffer is a simple ref-counted buffer. We transparently
choose between kmalloc for small buffers and vmalloc for large ones.
This is currently used only for allocating memory for xattr data.
Signed-off-by: Sage Weil <sage@newdream.net>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/ceph/buffer.c | 34 | ||||
-rw-r--r-- | fs/ceph/buffer.h | 55 |
2 files changed, 89 insertions, 0 deletions
diff --git a/fs/ceph/buffer.c b/fs/ceph/buffer.c new file mode 100644 index 000000000000..cf9aaccef22b --- /dev/null +++ b/fs/ceph/buffer.c | |||
@@ -0,0 +1,34 @@ | |||
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 | atomic_set(&b->nref, 1); | ||
13 | b->vec.iov_base = NULL; | ||
14 | b->vec.iov_len = 0; | ||
15 | b->alloc_len = 0; | ||
16 | return b; | ||
17 | } | ||
18 | |||
19 | int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp) | ||
20 | { | ||
21 | b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); | ||
22 | if (b->vec.iov_base) { | ||
23 | b->is_vmalloc = false; | ||
24 | } else { | ||
25 | b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); | ||
26 | b->is_vmalloc = true; | ||
27 | } | ||
28 | if (!b->vec.iov_base) | ||
29 | return -ENOMEM; | ||
30 | b->alloc_len = len; | ||
31 | b->vec.iov_len = len; | ||
32 | return 0; | ||
33 | } | ||
34 | |||
diff --git a/fs/ceph/buffer.h b/fs/ceph/buffer.h new file mode 100644 index 000000000000..16b1930acc45 --- /dev/null +++ b/fs/ceph/buffer.h | |||
@@ -0,0 +1,55 @@ | |||
1 | #ifndef __FS_CEPH_BUFFER_H | ||
2 | #define __FS_CEPH_BUFFER_H | ||
3 | |||
4 | #include <linux/mm.h> | ||
5 | #include <linux/vmalloc.h> | ||
6 | #include <linux/types.h> | ||
7 | #include <linux/uio.h> | ||
8 | |||
9 | /* | ||
10 | * a simple reference counted buffer. | ||
11 | * | ||
12 | * use kmalloc for small sizes (<= one page), vmalloc for larger | ||
13 | * sizes. | ||
14 | */ | ||
15 | struct ceph_buffer { | ||
16 | atomic_t nref; | ||
17 | struct kvec vec; | ||
18 | size_t alloc_len; | ||
19 | bool is_vmalloc; | ||
20 | }; | ||
21 | |||
22 | struct ceph_buffer *ceph_buffer_new(gfp_t gfp); | ||
23 | int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp); | ||
24 | |||
25 | static inline struct ceph_buffer *ceph_buffer_get(struct ceph_buffer *b) | ||
26 | { | ||
27 | atomic_inc(&b->nref); | ||
28 | return b; | ||
29 | } | ||
30 | |||
31 | static inline void ceph_buffer_put(struct ceph_buffer *b) | ||
32 | { | ||
33 | if (b && atomic_dec_and_test(&b->nref)) { | ||
34 | if (b->vec.iov_base) { | ||
35 | if (b->is_vmalloc) | ||
36 | vfree(b->vec.iov_base); | ||
37 | else | ||
38 | kfree(b->vec.iov_base); | ||
39 | } | ||
40 | kfree(b); | ||
41 | } | ||
42 | } | ||
43 | |||
44 | static inline struct ceph_buffer *ceph_buffer_new_alloc(int len, gfp_t gfp) | ||
45 | { | ||
46 | struct ceph_buffer *b = ceph_buffer_new(gfp); | ||
47 | |||
48 | if (b && ceph_buffer_alloc(b, len, gfp) < 0) { | ||
49 | ceph_buffer_put(b); | ||
50 | b = NULL; | ||
51 | } | ||
52 | return b; | ||
53 | } | ||
54 | |||
55 | #endif | ||