buffer.c 1.2 KB

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