// SPDX-License-Identifier: GPL-2.0-only1/*2* snapshot.c Ceph snapshot context utility routines (part of libceph)3*4* Copyright (C) 2013 Inktank Storage, Inc.5*/67#include <linux/types.h>8#include <linux/export.h>9#include <linux/ceph/libceph.h>1011/*12* Ceph snapshot contexts are reference counted objects, and the13* returned structure holds a single reference. Acquire additional14* references with ceph_get_snap_context(), and release them with15* ceph_put_snap_context(). When the reference count reaches zero16* the entire structure is freed.17*/1819/*20* Create a new ceph snapshot context large enough to hold the21* indicated number of snapshot ids (which can be 0). Caller has22* to fill in snapc->seq and snapc->snaps[0..snap_count-1].23*24* Returns a null pointer if an error occurs.25*/26struct ceph_snap_context *ceph_create_snap_context(u32 snap_count,27gfp_t gfp_flags)28{29struct ceph_snap_context *snapc;30size_t size;3132size = sizeof (struct ceph_snap_context);33size += snap_count * sizeof (snapc->snaps[0]);34snapc = kzalloc(size, gfp_flags);35if (!snapc)36return NULL;3738refcount_set(&snapc->nref, 1);39snapc->num_snaps = snap_count;4041return snapc;42}43EXPORT_SYMBOL(ceph_create_snap_context);4445struct ceph_snap_context *ceph_get_snap_context(struct ceph_snap_context *sc)46{47if (sc)48refcount_inc(&sc->nref);49return sc;50}51EXPORT_SYMBOL(ceph_get_snap_context);5253void ceph_put_snap_context(struct ceph_snap_context *sc)54{55if (!sc)56return;57if (refcount_dec_and_test(&sc->nref)) {58/*printk(" deleting snap_context %p\n", sc);*/59kfree(sc);60}61}62EXPORT_SYMBOL(ceph_put_snap_context);636465