buffer.c 1.2 KB

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