snapshot.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * snapshot.c Ceph snapshot context utility routines (part of libceph)
  3. *
  4. * Copyright (C) 2013 Inktank Storage, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <stddef.h>
  21. #include <linux/types.h>
  22. #include <linux/export.h>
  23. #include <linux/ceph/libceph.h>
  24. /*
  25. * Ceph snapshot contexts are reference counted objects, and the
  26. * returned structure holds a single reference. Acquire additional
  27. * references with ceph_get_snap_context(), and release them with
  28. * ceph_put_snap_context(). When the reference count reaches zero
  29. * the entire structure is freed.
  30. */
  31. /*
  32. * Create a new ceph snapshot context large enough to hold the
  33. * indicated number of snapshot ids (which can be 0). Caller has
  34. * to fill in snapc->seq and snapc->snaps[0..snap_count-1].
  35. *
  36. * Returns a null pointer if an error occurs.
  37. */
  38. struct ceph_snap_context *ceph_create_snap_context(u32 snap_count,
  39. gfp_t gfp_flags)
  40. {
  41. struct ceph_snap_context *snapc;
  42. size_t size;
  43. size = sizeof (struct ceph_snap_context);
  44. size += snap_count * sizeof (snapc->snaps[0]);
  45. snapc = kzalloc(size, gfp_flags);
  46. if (!snapc)
  47. return NULL;
  48. atomic_set(&snapc->nref, 1);
  49. snapc->num_snaps = snap_count;
  50. return snapc;
  51. }
  52. EXPORT_SYMBOL(ceph_create_snap_context);
  53. struct ceph_snap_context *ceph_get_snap_context(struct ceph_snap_context *sc)
  54. {
  55. if (sc)
  56. atomic_inc(&sc->nref);
  57. return sc;
  58. }
  59. EXPORT_SYMBOL(ceph_get_snap_context);
  60. void ceph_put_snap_context(struct ceph_snap_context *sc)
  61. {
  62. if (!sc)
  63. return;
  64. if (atomic_dec_and_test(&sc->nref)) {
  65. /*printk(" deleting snap_context %p\n", sc);*/
  66. kfree(sc);
  67. }
  68. }
  69. EXPORT_SYMBOL(ceph_put_snap_context);