summaryrefslogtreecommitdiffstats
path: root/fs/ceph/buffer.h
blob: 16b1930acc45fbde34c3db7fe326f29c269b908b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef __FS_CEPH_BUFFER_H
#define __FS_CEPH_BUFFER_H

#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/types.h>
#include <linux/uio.h>

/*
 * a simple reference counted buffer.
 *
 * use kmalloc for small sizes (<= one page), vmalloc for larger
 * sizes.
 */
struct ceph_buffer {
	atomic_t nref;
	struct kvec vec;
	size_t alloc_len;
	bool is_vmalloc;
};

struct ceph_buffer *ceph_buffer_new(gfp_t gfp);
int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp);

static inline struct ceph_buffer *ceph_buffer_get(struct ceph_buffer *b)
{
	atomic_inc(&b->nref);
	return b;
}

static inline void ceph_buffer_put(struct ceph_buffer *b)
{
	if (b && atomic_dec_and_test(&b->nref)) {
		if (b->vec.iov_base) {
			if (b->is_vmalloc)
				vfree(b->vec.iov_base);
			else
				kfree(b->vec.iov_base);
		}
		kfree(b);
	}
}

static inline struct ceph_buffer *ceph_buffer_new_alloc(int len, gfp_t gfp)
{
	struct ceph_buffer *b = ceph_buffer_new(gfp);

	if (b && ceph_buffer_alloc(b, len, gfp) < 0) {
		ceph_buffer_put(b);
		b = NULL;
	}
	return b;
}

#endif