buffer.c 1.2 KB

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