buffer.c 1.4 KB

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