Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * snapshot.c    Ceph snapshot context utility routines (part of libceph)
0004  *
0005  * Copyright (C) 2013 Inktank Storage, Inc.
0006  */
0007 
0008 #include <linux/types.h>
0009 #include <linux/export.h>
0010 #include <linux/ceph/libceph.h>
0011 
0012 /*
0013  * Ceph snapshot contexts are reference counted objects, and the
0014  * returned structure holds a single reference.  Acquire additional
0015  * references with ceph_get_snap_context(), and release them with
0016  * ceph_put_snap_context().  When the reference count reaches zero
0017  * the entire structure is freed.
0018  */
0019 
0020 /*
0021  * Create a new ceph snapshot context large enough to hold the
0022  * indicated number of snapshot ids (which can be 0).  Caller has
0023  * to fill in snapc->seq and snapc->snaps[0..snap_count-1].
0024  *
0025  * Returns a null pointer if an error occurs.
0026  */
0027 struct ceph_snap_context *ceph_create_snap_context(u32 snap_count,
0028                         gfp_t gfp_flags)
0029 {
0030     struct ceph_snap_context *snapc;
0031     size_t size;
0032 
0033     size = sizeof (struct ceph_snap_context);
0034     size += snap_count * sizeof (snapc->snaps[0]);
0035     snapc = kzalloc(size, gfp_flags);
0036     if (!snapc)
0037         return NULL;
0038 
0039     refcount_set(&snapc->nref, 1);
0040     snapc->num_snaps = snap_count;
0041 
0042     return snapc;
0043 }
0044 EXPORT_SYMBOL(ceph_create_snap_context);
0045 
0046 struct ceph_snap_context *ceph_get_snap_context(struct ceph_snap_context *sc)
0047 {
0048     if (sc)
0049         refcount_inc(&sc->nref);
0050     return sc;
0051 }
0052 EXPORT_SYMBOL(ceph_get_snap_context);
0053 
0054 void ceph_put_snap_context(struct ceph_snap_context *sc)
0055 {
0056     if (!sc)
0057         return;
0058     if (refcount_dec_and_test(&sc->nref)) {
0059         /*printk(" deleting snap_context %p\n", sc);*/
0060         kfree(sc);
0061     }
0062 }
0063 EXPORT_SYMBOL(ceph_put_snap_context);