diff options
| author | David S. Miller <davem@davemloft.net> | 2010-03-22 20:05:26 -0700 | 
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2010-03-22 20:05:26 -0700 | 
| commit | 91c030b4d6445ebe9fbddb86d584441f300df15c (patch) | |
| tree | e8a48e3ad9e0448c690268175e555fa651983546 /fs/ceph/buffer.c | |
| parent | 7c3456fdb503071787f7f972de1069b9cacd16f0 (diff) | |
| parent | ae6be51ed01d6c4aaf249a207b4434bc7785853b (diff) | |
Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
Diffstat (limited to 'fs/ceph/buffer.c')
| -rw-r--r-- | fs/ceph/buffer.c | 78 | 
1 files changed, 78 insertions, 0 deletions
| diff --git a/fs/ceph/buffer.c b/fs/ceph/buffer.c new file mode 100644 index 000000000000..b98086c7aeba --- /dev/null +++ b/fs/ceph/buffer.c @@ -0,0 +1,78 @@ + +#include "ceph_debug.h" +#include "buffer.h" +#include "decode.h" + +struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) +{ +	struct ceph_buffer *b; + +	b = kmalloc(sizeof(*b), gfp); +	if (!b) +		return NULL; + +	b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); +	if (b->vec.iov_base) { +		b->is_vmalloc = false; +	} else { +		b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); +		if (!b->vec.iov_base) { +			kfree(b); +			return NULL; +		} +		b->is_vmalloc = true; +	} + +	kref_init(&b->kref); +	b->alloc_len = len; +	b->vec.iov_len = len; +	dout("buffer_new %p\n", b); +	return b; +} + +void ceph_buffer_release(struct kref *kref) +{ +	struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref); + +	dout("buffer_release %p\n", b); +	if (b->vec.iov_base) { +		if (b->is_vmalloc) +			vfree(b->vec.iov_base); +		else +			kfree(b->vec.iov_base); +	} +	kfree(b); +} + +int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp) +{ +	b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); +	if (b->vec.iov_base) { +		b->is_vmalloc = false; +	} else { +		b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); +		b->is_vmalloc = true; +	} +	if (!b->vec.iov_base) +		return -ENOMEM; +	b->alloc_len = len; +	b->vec.iov_len = len; +	return 0; +} + +int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end) +{ +	size_t len; + +	ceph_decode_need(p, end, sizeof(u32), bad); +	len = ceph_decode_32(p); +	dout("decode_buffer len %d\n", (int)len); +	ceph_decode_need(p, end, len, bad); +	*b = ceph_buffer_new(len, GFP_NOFS); +	if (!*b) +		return -ENOMEM; +	ceph_decode_copy(p, (*b)->vec.iov_base, len); +	return 0; +bad: +	return -EINVAL; +} | 
