194a312a1SFrançois Tigeot /*-
294a312a1SFrançois Tigeot * Copyright (c) 2010 Isilon Systems, Inc.
394a312a1SFrançois Tigeot * Copyright (c) 2010 iX Systems, Inc.
494a312a1SFrançois Tigeot * Copyright (c) 2010 Panasas, Inc.
5a85cb24fSFrançois Tigeot * Copyright (c) 2013-2020 François Tigeot <ftigeot@wolfpond.org>
694a312a1SFrançois Tigeot * All rights reserved.
794a312a1SFrançois Tigeot *
894a312a1SFrançois Tigeot * Redistribution and use in source and binary forms, with or without
994a312a1SFrançois Tigeot * modification, are permitted provided that the following conditions
1094a312a1SFrançois Tigeot * are met:
1194a312a1SFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
1294a312a1SFrançois Tigeot * notice unmodified, this list of conditions, and the following
1394a312a1SFrançois Tigeot * disclaimer.
1494a312a1SFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
1594a312a1SFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
1694a312a1SFrançois Tigeot * documentation and/or other materials provided with the distribution.
1794a312a1SFrançois Tigeot *
1894a312a1SFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1994a312a1SFrançois Tigeot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2094a312a1SFrançois Tigeot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2194a312a1SFrançois Tigeot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2294a312a1SFrançois Tigeot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2394a312a1SFrançois Tigeot * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2494a312a1SFrançois Tigeot * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2594a312a1SFrançois Tigeot * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2694a312a1SFrançois Tigeot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2794a312a1SFrançois Tigeot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2894a312a1SFrançois Tigeot */
29*3f2dd94aSFrançois Tigeot
3094a312a1SFrançois Tigeot #ifndef _LINUX_KREF_H_
3194a312a1SFrançois Tigeot #define _LINUX_KREF_H_
3294a312a1SFrançois Tigeot
33a85cb24fSFrançois Tigeot #include <linux/spinlock.h>
34*3f2dd94aSFrançois Tigeot #include <linux/refcount.h>
35a85cb24fSFrançois Tigeot
3694a312a1SFrançois Tigeot struct kref {
37*3f2dd94aSFrançois Tigeot refcount_t refcount;
3894a312a1SFrançois Tigeot };
3994a312a1SFrançois Tigeot
4094a312a1SFrançois Tigeot static inline void
kref_init(struct kref * kref)4194a312a1SFrançois Tigeot kref_init(struct kref *kref)
4294a312a1SFrançois Tigeot {
43*3f2dd94aSFrançois Tigeot atomic_set(&kref->refcount.refs, 1);
4494a312a1SFrançois Tigeot }
4594a312a1SFrançois Tigeot
46a85cb24fSFrançois Tigeot static inline unsigned int
kref_read(const struct kref * kref)47a85cb24fSFrançois Tigeot kref_read(const struct kref *kref)
48a85cb24fSFrançois Tigeot {
49*3f2dd94aSFrançois Tigeot return atomic_read(&kref->refcount.refs);
50a85cb24fSFrançois Tigeot }
51a85cb24fSFrançois Tigeot
5294a312a1SFrançois Tigeot static inline void
kref_get(struct kref * kref)5394a312a1SFrançois Tigeot kref_get(struct kref *kref)
5494a312a1SFrançois Tigeot {
55*3f2dd94aSFrançois Tigeot refcount_inc(&kref->refcount);
5694a312a1SFrançois Tigeot }
5794a312a1SFrançois Tigeot
5894a312a1SFrançois Tigeot static inline int
kref_put(struct kref * kref,void (* release)(struct kref * kref))59*3f2dd94aSFrançois Tigeot kref_put(struct kref *kref, void (*release)(struct kref *kref))
6094a312a1SFrançois Tigeot {
61*3f2dd94aSFrançois Tigeot if (atomic_dec_and_test(&kref->refcount.refs)) {
62*3f2dd94aSFrançois Tigeot release(kref);
6332dbd5b5SFrançois Tigeot return 1;
6432dbd5b5SFrançois Tigeot }
65*3f2dd94aSFrançois Tigeot
6632dbd5b5SFrançois Tigeot return 0;
6732dbd5b5SFrançois Tigeot }
6832dbd5b5SFrançois Tigeot
69*3f2dd94aSFrançois Tigeot #if 0
7032dbd5b5SFrançois Tigeot static inline int
7132dbd5b5SFrançois Tigeot kref_sub(struct kref *kref, unsigned int count,
7232dbd5b5SFrançois Tigeot void (*rel)(struct kref *kref))
7332dbd5b5SFrançois Tigeot {
7432dbd5b5SFrançois Tigeot if (refcount_release_n(&kref->refcount.counter, count)) {
7594a312a1SFrançois Tigeot rel(kref);
7694a312a1SFrançois Tigeot return 1;
7794a312a1SFrançois Tigeot }
7894a312a1SFrançois Tigeot return 0;
7994a312a1SFrançois Tigeot }
80*3f2dd94aSFrançois Tigeot #endif
8194a312a1SFrançois Tigeot
821dca5014SFrançois Tigeot /*
831dca5014SFrançois Tigeot * kref_get_unless_zero: Increment refcount for object unless it is zero.
841dca5014SFrançois Tigeot */
kref_get_unless_zero(struct kref * kref)851dca5014SFrançois Tigeot static inline int __must_check kref_get_unless_zero(struct kref *kref)
861dca5014SFrançois Tigeot {
87*3f2dd94aSFrançois Tigeot return atomic_add_unless(&kref->refcount.refs, 1, 0);
881dca5014SFrançois Tigeot }
891dca5014SFrançois Tigeot
kref_put_mutex(struct kref * kref,void (* release)(struct kref * kref),struct lock * lock)90b4b38be0SFrançois Tigeot static inline int kref_put_mutex(struct kref *kref,
91b4b38be0SFrançois Tigeot void (*release)(struct kref *kref),
92b4b38be0SFrançois Tigeot struct lock *lock)
93b4b38be0SFrançois Tigeot {
94*3f2dd94aSFrançois Tigeot if (!atomic_add_unless(&kref->refcount.refs, -1, 1)) {
95b4b38be0SFrançois Tigeot mutex_lock(lock);
96*3f2dd94aSFrançois Tigeot if (likely(atomic_dec_and_test(&kref->refcount.refs))) {
97b4b38be0SFrançois Tigeot release(kref);
98b4b38be0SFrançois Tigeot return 1;
99b4b38be0SFrançois Tigeot }
100b4b38be0SFrançois Tigeot mutex_unlock(lock);
101b4b38be0SFrançois Tigeot return 0;
102b4b38be0SFrançois Tigeot }
103b4b38be0SFrançois Tigeot
104b4b38be0SFrançois Tigeot return 0;
105b4b38be0SFrançois Tigeot }
106b4b38be0SFrançois Tigeot
1071dca5014SFrançois Tigeot #endif /* _LINUX_KREF_H_ */
108