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