snapshot.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * snapshot.c Ceph snapshot context utility routines (part of libceph)
  4. *
  5. * Copyright (C) 2013 Inktank Storage, Inc.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/export.h>
  9. #include <linux/ceph/libceph.h>
  10. /*
  11. * Ceph snapshot contexts are reference counted objects, and the
  12. * returned structure holds a single reference. Acquire additional
  13. * references with ceph_get_snap_context(), and release them with
  14. * ceph_put_snap_context(). When the reference count reaches zero
  15. * the entire structure is freed.
  16. */
  17. /*
  18. * Create a new ceph snapshot context large enough to hold the
  19. * indicated number of snapshot ids (which can be 0). Caller has
  20. * to fill in snapc->seq and snapc->snaps[0..snap_count-1].
  21. *
  22. * Returns a null pointer if an error occurs.
  23. */
  24. struct ceph_snap_context *ceph_create_snap_context(u32 snap_count,
  25. gfp_t gfp_flags)
  26. {
  27. struct ceph_snap_context *snapc;
  28. size_t size;
  29. size = sizeof (struct ceph_snap_context);
  30. size += snap_count * sizeof (snapc->snaps[0]);
  31. snapc = kzalloc(size, gfp_flags);
  32. if (!snapc)
  33. return NULL;
  34. refcount_set(&snapc->nref, 1);
  35. snapc->num_snaps = snap_count;
  36. return snapc;
  37. }
  38. EXPORT_SYMBOL(ceph_create_snap_context);
  39. struct ceph_snap_context *ceph_get_snap_context(struct ceph_snap_context *sc)
  40. {
  41. if (sc)
  42. refcount_inc(&sc->nref);
  43. return sc;
  44. }
  45. EXPORT_SYMBOL(ceph_get_snap_context);
  46. void ceph_put_snap_context(struct ceph_snap_context *sc)
  47. {
  48. if (!sc)
  49. return;
  50. if (refcount_dec_and_test(&sc->nref)) {
  51. /*printk(" deleting snap_context %p\n", sc);*/
  52. kfree(sc);
  53. }
  54. }
  55. EXPORT_SYMBOL(ceph_put_snap_context);