blob: 5622763ad4027b84aaed90be96664d2d1550e008 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sage Weilc30dbb92009-10-06 11:31:07 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/ceph/ceph_debug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
7
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07008#include <linux/ceph/buffer.h>
9#include <linux/ceph/decode.h>
Ilya Dryomov4965fc32014-10-23 16:32:57 +040010#include <linux/ceph/libceph.h> /* for ceph_kvmalloc */
Sage Weilc30dbb92009-10-06 11:31:07 -070011
Sage Weilb6c1d5b2009-12-07 12:17:17 -080012struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
Sage Weilc30dbb92009-10-06 11:31:07 -070013{
14 struct ceph_buffer *b;
15
16 b = kmalloc(sizeof(*b), gfp);
17 if (!b)
18 return NULL;
Sage Weilb6c1d5b2009-12-07 12:17:17 -080019
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +020020 b->vec.iov_base = ceph_kvmalloc(len, gfp);
21 if (!b->vec.iov_base) {
22 kfree(b);
23 return NULL;
Sage Weilb6c1d5b2009-12-07 12:17:17 -080024 }
25
Sage Weildd26d852009-12-05 10:13:33 -080026 kref_init(&b->kref);
Sage Weilb6c1d5b2009-12-07 12:17:17 -080027 b->alloc_len = len;
28 b->vec.iov_len = len;
29 dout("buffer_new %p\n", b);
Sage Weilc30dbb92009-10-06 11:31:07 -070030 return b;
31}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070032EXPORT_SYMBOL(ceph_buffer_new);
Sage Weilc30dbb92009-10-06 11:31:07 -070033
Sage Weildd26d852009-12-05 10:13:33 -080034void ceph_buffer_release(struct kref *kref)
35{
36 struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
Sage Weilb6c1d5b2009-12-07 12:17:17 -080037
38 dout("buffer_release %p\n", b);
Ilya Dryomov4965fc32014-10-23 16:32:57 +040039 kvfree(b->vec.iov_base);
Sage Weildd26d852009-12-05 10:13:33 -080040 kfree(b);
41}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070042EXPORT_SYMBOL(ceph_buffer_release);
Sage Weildd26d852009-12-05 10:13:33 -080043
Sage Weilc7e337d2010-02-02 16:11:19 -080044int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end)
45{
46 size_t len;
47
48 ceph_decode_need(p, end, sizeof(u32), bad);
49 len = ceph_decode_32(p);
50 dout("decode_buffer len %d\n", (int)len);
51 ceph_decode_need(p, end, len, bad);
52 *b = ceph_buffer_new(len, GFP_NOFS);
53 if (!*b)
54 return -ENOMEM;
55 ceph_decode_copy(p, (*b)->vec.iov_base, len);
56 return 0;
57bad:
58 return -EINVAL;
59}