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