blob: add5f921a0ffbef5149852177412aa02bee95e7b [file] [log] [blame]
Sage Weilc30dbb92009-10-06 11:31:07 -07001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
6
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/ceph/buffer.h>
8#include <linux/ceph/decode.h>
Ilya Dryomov4965fc32014-10-23 16:32:57 +04009#include <linux/ceph/libceph.h> /* for ceph_kvmalloc */
Sage Weilc30dbb92009-10-06 11:31:07 -070010
Sage Weilb6c1d5b2009-12-07 12:17:17 -080011struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
Sage Weilc30dbb92009-10-06 11:31:07 -070012{
13 struct ceph_buffer *b;
14
15 b = kmalloc(sizeof(*b), gfp);
16 if (!b)
17 return NULL;
Sage Weilb6c1d5b2009-12-07 12:17:17 -080018
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +020019 b->vec.iov_base = ceph_kvmalloc(len, gfp);
20 if (!b->vec.iov_base) {
21 kfree(b);
22 return NULL;
Sage Weilb6c1d5b2009-12-07 12:17:17 -080023 }
24
Sage Weildd26d852009-12-05 10:13:33 -080025 kref_init(&b->kref);
Sage Weilb6c1d5b2009-12-07 12:17:17 -080026 b->alloc_len = len;
27 b->vec.iov_len = len;
28 dout("buffer_new %p\n", b);
Sage Weilc30dbb92009-10-06 11:31:07 -070029 return b;
30}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070031EXPORT_SYMBOL(ceph_buffer_new);
Sage Weilc30dbb92009-10-06 11:31:07 -070032
Sage Weildd26d852009-12-05 10:13:33 -080033void ceph_buffer_release(struct kref *kref)
34{
35 struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
Sage Weilb6c1d5b2009-12-07 12:17:17 -080036
37 dout("buffer_release %p\n", b);
Ilya Dryomov4965fc32014-10-23 16:32:57 +040038 kvfree(b->vec.iov_base);
Sage Weildd26d852009-12-05 10:13:33 -080039 kfree(b);
40}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070041EXPORT_SYMBOL(ceph_buffer_release);
Sage Weildd26d852009-12-05 10:13:33 -080042
Sage Weilc7e337d2010-02-02 16:11:19 -080043int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end)
44{
45 size_t len;
46
47 ceph_decode_need(p, end, sizeof(u32), bad);
48 len = ceph_decode_32(p);
49 dout("decode_buffer len %d\n", (int)len);
50 ceph_decode_need(p, end, len, bad);
51 *b = ceph_buffer_new(len, GFP_NOFS);
52 if (!*b)
53 return -ENOMEM;
54 ceph_decode_copy(p, (*b)->vec.iov_base, len);
55 return 0;
56bad:
57 return -EINVAL;
58}