From db14ac2c81cc03084bfb63f393ac71a36e142266 Mon Sep 17 00:00:00 2001
From: Niels de Vos <ndevos@redhat.com>
Date: Tue, 16 May 2017 18:10:16 +0200
Subject: [PATCH 445/473] refcount: return pointer to the structure instead of
a counter
There are no real users of the counter. It was thought of a handy tool
to track and debug refcounting, but it is not used at all. Some parts of
the code would benefit from a pointer getting returned instead.
Cherry picked from commit 2f0e9ab1ef271132b8b7b1af25e23ac7bb0720c8:
> BUG: 1399780
> Change-Id: I97e52c48420fed61be942ea27ff4849b803eed12
> Signed-off-by: Niels de Vos <ndevos@redhat.com>
> Reviewed-on: http://review.gluster.org/15971
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: Xavier Hernandez <xhernandez@datalab.es>
> Reviewed-by: Kaleb KEITHLEY <kkeithle@redhat.com>
There has been a slight omission in the backport of change
I19d225b39aaa272d9005ba7adc3104c3764f1572. The return value of
GF_REF_GET() is used and causes compile warnings. The resulting build
with that backport may not function correctly without this change that
returns the pointer of the structure with GF_REF_GET().
Change-Id: I97e52c48420fed61be942ea27ff4849b803eed12
BUG: 1450336
Reported-by: Mohammed Rafi KC <rkavunga@redhat.com>
Signed-off-by: Niels de Vos <ndevos@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/106356
Reviewed-by: Poornima Gurusiddaiah <pgurusid@redhat.com>
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
---
libglusterfs/src/refcount.c | 21 +++++++--------------
libglusterfs/src/refcount.h | 10 +++++-----
xlators/nfs/server/src/auth-cache.c | 10 ++++++----
3 files changed, 18 insertions(+), 23 deletions(-)
diff --git a/libglusterfs/src/refcount.c b/libglusterfs/src/refcount.c
index 96edc10..9d33b73 100644
--- a/libglusterfs/src/refcount.c
+++ b/libglusterfs/src/refcount.c
@@ -13,7 +13,7 @@
#ifndef REFCOUNT_NEEDS_LOCK
-unsigned int
+void *
_gf_ref_get (gf_ref_t *ref)
{
unsigned int cnt = __sync_fetch_and_add (&ref->cnt, 1);
@@ -27,10 +27,10 @@ _gf_ref_get (gf_ref_t *ref)
*/
GF_ASSERT (cnt != 0);
- return cnt;
+ return cnt ? ref->data : NULL;
}
-unsigned int
+void
_gf_ref_put (gf_ref_t *ref)
{
unsigned int cnt = __sync_fetch_and_sub (&ref->cnt, 1);
@@ -43,18 +43,13 @@ _gf_ref_put (gf_ref_t *ref)
*/
GF_ASSERT (cnt != 0);
- if (cnt == 1 && ref->release) {
+ if (cnt == 1 && ref->release)
ref->release (ref->data);
- /* set return value to 0 to inform the caller correctly */
- cnt = 0;
- }
-
- return cnt;
}
#else
-unsigned int
+void *
_gf_ref_get (gf_ref_t *ref)
{
unsigned int cnt = 0;
@@ -69,10 +64,10 @@ _gf_ref_get (gf_ref_t *ref)
}
UNLOCK (&ref->lk);
- return cnt;
+ return cnt ? ref->data : NULL;
}
-unsigned int
+void
_gf_ref_put (gf_ref_t *ref)
{
unsigned int cnt = 0;
@@ -91,8 +86,6 @@ _gf_ref_put (gf_ref_t *ref)
if (release && ref->release)
ref->release (ref->data);
-
- return cnt;
}
#endif /* REFCOUNT_NEEDS_LOCK */
diff --git a/libglusterfs/src/refcount.h b/libglusterfs/src/refcount.h
index e850e63..db9432a 100644
--- a/libglusterfs/src/refcount.h
+++ b/libglusterfs/src/refcount.h
@@ -42,7 +42,7 @@ typedef struct _gf_ref_t gf_ref_t;
*
* @return: greater then 0 when a reference was taken, 0 when not
*/
-unsigned int
+void *
_gf_ref_get (gf_ref_t *ref);
/* _gf_ref_put -- decrease the refcount
@@ -50,7 +50,7 @@ _gf_ref_get (gf_ref_t *ref);
* @return: greater then 0 when there are still references, 0 when cleanup
* should be done, gf_ref_release_t is called on cleanup
*/
-unsigned int
+void
_gf_ref_put (gf_ref_t *ref);
/* _gf_ref_init -- initalize an embedded refcount object
@@ -84,20 +84,20 @@ _gf_ref_init (gf_ref_t *ref, gf_ref_release_t release, void *data);
*
* Sets the refcount to 1.
*/
-#define GF_REF_INIT(p, d) _gf_ref_init (&p->_ref, d, p)
+#define GF_REF_INIT(p, d) _gf_ref_init (&(p)->_ref, d, p)
/* GF_REF_GET -- increase the refcount of a GF_REF_DECL structure
*
* @return: greater then 0 when a reference was taken, 0 when not
*/
-#define GF_REF_GET(p) _gf_ref_get (&p->_ref)
+#define GF_REF_GET(p) _gf_ref_get (&(p)->_ref)
/* GF_REF_PUT -- decrease the refcount of a GF_REF_DECL structure
*
* @return: greater then 0 when there are still references, 0 when cleanup
* should be done, gf_ref_release_t is called on cleanup
*/
-#define GF_REF_PUT(p) _gf_ref_put (&p->_ref)
+#define GF_REF_PUT(p) _gf_ref_put (&(p)->_ref)
#endif /* _REFCOUNT_H */
diff --git a/xlators/nfs/server/src/auth-cache.c b/xlators/nfs/server/src/auth-cache.c
index 730e0a9..a901e80 100644
--- a/xlators/nfs/server/src/auth-cache.c
+++ b/xlators/nfs/server/src/auth-cache.c
@@ -145,8 +145,9 @@ auth_cache_add (struct auth_cache *cache, char *hashkey,
GF_VALIDATE_OR_GOTO (GF_NFS, cache, out);
GF_VALIDATE_OR_GOTO (GF_NFS, cache->cache_dict, out);
- ret = GF_REF_GET (entry);
- if (ret == 0) {
+ /* FIXME: entry is passed as parameter, this can never fail? */
+ entry = GF_REF_GET (entry);
+ if (!entry) {
/* entry does not have any references */
ret = -1;
goto out;
@@ -221,8 +222,9 @@ auth_cache_get (struct auth_cache *cache, char *hashkey,
if (!entry_data)
goto unlock;
- lookup_res = (struct auth_cache_entry *)(entry_data->data);
- if (GF_REF_GET (lookup_res) == 0) {
+ /* FIXME: this is dangerous use of entry_data */
+ lookup_res = GF_REF_GET ((struct auth_cache_entry *) entry_data->data);
+ if (lookup_res == NULL) {
/* entry has been free'd */
ret = ENTRY_EXPIRED;
goto unlock;
--
1.8.3.1