^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/ceph/ceph_debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/ceph/buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ceph/decode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/ceph/libceph.h> /* for ceph_kvmalloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct ceph_buffer *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) b = kmalloc(sizeof(*b), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (!b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) b->vec.iov_base = ceph_kvmalloc(len, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (!b->vec.iov_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) kref_init(&b->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) b->alloc_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) b->vec.iov_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) dout("buffer_new %p\n", b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) EXPORT_SYMBOL(ceph_buffer_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void ceph_buffer_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) dout("buffer_release %p\n", b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) kvfree(b->vec.iov_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) EXPORT_SYMBOL(ceph_buffer_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ceph_decode_need(p, end, sizeof(u32), bad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) len = ceph_decode_32(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) dout("decode_buffer len %d\n", (int)len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ceph_decode_need(p, end, len, bad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *b = ceph_buffer_new(len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!*b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ceph_decode_copy(p, (*b)->vec.iov_base, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }