commit 68a6d3ab246d25891bf3b9958622ebf5d92e8729
Author:     Daniel Gryniewicz <dang@redhat.com>
AuthorDate: Tue Sep 24 10:55:51 2019 -0400
Commit:     Daniel Gryniewicz <dang@redhat.com>
CommitDate: Tue Sep 24 10:55:51 2019 -0400

    MDCACHE - Debug code for Entry LRU Refcounts
    
    This is debug code that helps tracking down refcount errors for MDCACHE
    entries.  What it does is to add new version of the get/put FSAL APIs
    that takes a function name and line number.  This is then plumbed all
    the way down to mdc_lru_get/put, so that the original caller of get/put
    is traced in LTTng.
    
    Change-Id: I693a3433853c2cbb2f1b5990a738bf6f93e49a2d
    Signed-off-by: Daniel Gryniewicz <dang@redhat.com>

diff --git a/src/FSAL/FSAL_MEM/mem_handle.c b/src/FSAL/FSAL_MEM/mem_handle.c
index f3f438100..3914ccccd 100644
--- a/src/FSAL/FSAL_MEM/mem_handle.c
+++ b/src/FSAL/FSAL_MEM/mem_handle.c
@@ -2317,7 +2317,8 @@ static void mem_handle_to_key(struct fsal_obj_handle *obj_hdl,
  *
  * @param[in] obj_hdl	Handle to ref
  */
-static void mem_get_ref(struct fsal_obj_handle *obj_hdl)
+static void mem_get_ref(struct fsal_obj_handle *obj_hdl, const char *func,
+			int line)
 {
 	struct mem_fsal_obj_handle *myself;
 
@@ -2334,7 +2335,8 @@ static void mem_get_ref(struct fsal_obj_handle *obj_hdl)
  *
  * @param[in] obj_hdl	Handle to unref
  */
-static void mem_put_ref(struct fsal_obj_handle *obj_hdl)
+static void mem_put_ref(struct fsal_obj_handle *obj_hdl, const char *func,
+			int line)
 {
 	struct mem_fsal_obj_handle *myself;
 
@@ -2409,8 +2411,8 @@ void mem_handle_ops_init(struct fsal_obj_ops *ops)
 {
 	fsal_default_obj_ops_init(ops);
 
-	ops->get_ref = mem_get_ref,
-	ops->put_ref = mem_put_ref,
+	ops->_get_ref = mem_get_ref,
+	ops->_put_ref = mem_put_ref,
 	ops->merge = mem_merge,
 	ops->release = mem_release;
 	ops->lookup = mem_lookup;
diff --git a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_handle.c b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_handle.c
index cbb490f7a..09a5e4f62 100644
--- a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_handle.c
+++ b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_handle.c
@@ -1300,12 +1300,13 @@ static nfsstat4 mdcache_layoutcommit(struct fsal_obj_handle *obj_hdl,
  * @param[in] obj_hdl	Handle to ref
  * @return FSAL status
  */
-static void mdcache_get_ref(struct fsal_obj_handle *obj_hdl)
+static void mdcache_get_ref(struct fsal_obj_handle *obj_hdl, const char *func,
+			    int line)
 {
 	mdcache_entry_t *entry =
 		container_of(obj_hdl, mdcache_entry_t, obj_handle);
 
-	mdcache_get(entry);
+	_mdcache_get(entry, func, line);
 }
 
 /**
@@ -1314,12 +1315,13 @@ static void mdcache_get_ref(struct fsal_obj_handle *obj_hdl)
  * @param[in] obj_hdl	Handle to unref
  * @return FSAL status
  */
-static void mdcache_put_ref(struct fsal_obj_handle *obj_hdl)
+static void mdcache_put_ref(struct fsal_obj_handle *obj_hdl, const char *func,
+			    int line)
 {
 	mdcache_entry_t *entry =
 		container_of(obj_hdl, mdcache_entry_t, obj_handle);
 
-	mdcache_put(entry);
+	_mdcache_put(entry, func, line);
 }
 
 /**
@@ -1452,8 +1454,8 @@ void mdcache_handle_ops_init(struct fsal_obj_ops *ops)
 {
 	fsal_default_obj_ops_init(ops);
 
-	ops->get_ref = mdcache_get_ref;
-	ops->put_ref = mdcache_put_ref;
+	ops->_get_ref = mdcache_get_ref;
+	ops->_put_ref = mdcache_put_ref;
 	ops->release = mdcache_hdl_release;
 	ops->merge = mdcache_merge;
 	ops->lookup = mdcache_lookup;
diff --git a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_helpers.c b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_helpers.c
index 94c9359a0..07886ff27 100644
--- a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_helpers.c
+++ b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_helpers.c
@@ -647,14 +647,15 @@ void mdcache_dirent_invalidate_all(mdcache_entry_t *entry)
  * @return FSAL status
  */
 fsal_status_t
-mdcache_new_entry(struct mdcache_fsal_export *export,
+_mdcache_new_entry(struct mdcache_fsal_export *export,
 		  struct fsal_obj_handle *sub_handle,
 		  struct attrlist *attrs_in,
 		  struct attrlist *attrs_out,
 		  bool new_directory,
 		  mdcache_entry_t **entry,
 		  struct state_t *state,
-		  mdc_reason_t reason)
+		  mdc_reason_t reason,
+		  const char *func, int line)
 {
 	fsal_status_t status;
 	mdcache_entry_t *oentry, *nentry = NULL;
@@ -675,7 +676,8 @@ mdcache_new_entry(struct mdcache_fsal_export *export,
 	/* Check if the entry already exists.  We allow the following race
 	 * because mdcache_lru_get has a slow path, and the latch is a
 	 * shared lock. */
-	status = mdcache_find_keyed(&key, entry);
+	status = _mdcache_find_keyed_reason(&key, entry, MDC_REASON_DEFAULT,
+					    func, line);
 	if (!FSAL_IS_ERROR(status)) {
 		LogDebug(COMPONENT_CACHE_INODE,
 			 "Trying to add an already existing entry. Found entry %p type: %d, New type: %d",
@@ -697,8 +699,8 @@ mdcache_new_entry(struct mdcache_fsal_export *export,
 	/* We did not find the object.  Pull an entry off the LRU. The entry
 	 * will already be mapped.
 	 */
-	nentry = mdcache_alloc_handle(export, sub_handle, sub_handle->fs,
-				      reason);
+	nentry = _mdcache_alloc_handle(export, sub_handle, sub_handle->fs,
+				      reason, func, line);
 
 	if (nentry == NULL) {
 		/* We didn't get an entry because of unexport in progress,
@@ -949,8 +951,8 @@ int display_mdcache_key(struct display_buffer *dspbuf, mdcache_key_t *key)
  * @return Status
  */
 fsal_status_t
-mdcache_find_keyed_reason(mdcache_key_t *key, mdcache_entry_t **entry,
-			  mdc_reason_t reason)
+_mdcache_find_keyed_reason(mdcache_key_t *key, mdcache_entry_t **entry,
+			  mdc_reason_t reason, const char *func, int line)
 {
 	cih_latch_t latch;
 
@@ -977,8 +979,9 @@ mdcache_find_keyed_reason(mdcache_key_t *key, mdcache_entry_t **entry,
 		fsal_status_t status;
 
 		/* Initial Ref on entry */
-		status = mdcache_lru_ref(*entry, (reason != MDC_REASON_SCAN) ?
-					 LRU_REQ_INITIAL : LRU_FLAG_NONE);
+		status = _mdcache_lru_ref(*entry, (reason != MDC_REASON_SCAN) ?
+					 LRU_REQ_INITIAL : LRU_FLAG_NONE, func,
+					 line);
 		/* Release the subtree hash table lock */
 		cih_hash_release(&latch);
 		if (FSAL_IS_ERROR(status)) {
diff --git a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_int.h b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_int.h
index 53e0cb38d..2eb7d6b65 100644
--- a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_int.h
+++ b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_int.h
@@ -482,17 +482,27 @@ fsal_status_t mdcache_alloc_and_check_handle(
 fsal_status_t mdcache_refresh_attrs(mdcache_entry_t *entry, bool need_acl,
 				    bool need_fslocations, bool invalidate);
 
-fsal_status_t mdcache_new_entry(struct mdcache_fsal_export *exp,
+#define mdcache_new_entry(exp, sub_handle, attrs_in, attrs_out, new_directory, \
+			  entry, state, reason) \
+_mdcache_new_entry(exp, sub_handle, attrs_in, attrs_out, new_directory, entry, \
+		   state, reason, __func__, __LINE__)
+fsal_status_t _mdcache_new_entry(struct mdcache_fsal_export *exp,
 				struct fsal_obj_handle *sub_handle,
 				struct attrlist *attrs_in,
 				struct attrlist *attrs_out,
 				bool new_directory,
 				mdcache_entry_t **entry,
 				struct state_t *state,
-				mdc_reason_t reason);
-fsal_status_t mdcache_find_keyed_reason(mdcache_key_t *key,
+				mdc_reason_t reason,
+				const char *func, int line);
+fsal_status_t _mdcache_find_keyed_reason(mdcache_key_t *key,
 					mdcache_entry_t **entry,
-					mdc_reason_t reason);
+					mdc_reason_t reason,
+					const char *func,
+					int line);
+#define mdcache_find_keyed_reason(key, entry, reason) \
+	_mdcache_find_keyed_reason(key, entry, reason, __func__, __LINE__)
+
 #define mdcache_find_keyed(key, entry) mdcache_find_keyed_reason((key), \
 					(entry), MDC_REASON_DEFAULT)
 fsal_status_t mdcache_locate_host(struct gsh_buffdesc *fh_desc,
diff --git a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_lru.h b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_lru.h
index 5959bc149..c674d689c 100644
--- a/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_lru.h
+++ b/src/FSAL/Stackable_FSALs/FSAL_MDCACHE/mdcache_lru.h
@@ -170,10 +170,12 @@ void mdcache_lru_kill_for_shutdown(mdcache_entry_t *entry);
  *
  * @param[in] entry Cache entry being returned
  */
-static inline fsal_status_t mdcache_get(mdcache_entry_t *entry)
+static inline fsal_status_t _mdcache_get(mdcache_entry_t *entry,
+					 const char *func, int line)
 {
-	return mdcache_lru_ref(entry, LRU_FLAG_NONE);
+	return _mdcache_lru_ref(entry, LRU_FLAG_NONE, func, line);
 }
+#define mdcache_get(e) _mdcache_get(e, __func__, __LINE__)
 
 /**
  *
@@ -191,10 +193,12 @@ static inline fsal_status_t mdcache_get(mdcache_entry_t *entry)
  *
  * @param[in] entry Cache entry being returned
  */
-static inline void mdcache_put(mdcache_entry_t *entry)
+static inline void _mdcache_put(mdcache_entry_t *entry, const char *func,
+				int line)
 {
-	mdcache_lru_unref(entry);
+	_mdcache_lru_unref(entry, LRU_FLAG_NONE, func, line);
 }
+#define mdcache_put(e) _mdcache_put(e, __func__, __LINE__)
 
 #define mdcache_lru_ref_chunk(chunk) \
 	_mdcache_lru_ref_chunk(chunk, __func__, __LINE__)
diff --git a/src/FSAL/default_methods.c b/src/FSAL/default_methods.c
index 4edb2a8d3..044342643 100644
--- a/src/FSAL/default_methods.c
+++ b/src/FSAL/default_methods.c
@@ -752,7 +752,8 @@ struct export_ops def_export_ops = {
  * FSAL_MDCACHE.
  */
 
-static void handle_get_ref(struct fsal_obj_handle *obj_hdl)
+static void handle_get_ref(struct fsal_obj_handle *obj_hdl, const char *func,
+			   int line)
 {
 	/* return */
 }
@@ -762,7 +763,8 @@ static void handle_get_ref(struct fsal_obj_handle *obj_hdl)
  * FSAL_MDCACHE.
  */
 
-static void handle_put_ref(struct fsal_obj_handle *obj_hdl)
+static void handle_put_ref(struct fsal_obj_handle *obj_hdl, const char *func,
+			   int line)
 {
 	/* return */
 }
@@ -1501,8 +1503,8 @@ static bool is_referral(struct fsal_obj_handle *obj_hdl,
  */
 
 struct fsal_obj_ops def_handle_ops = {
-	.get_ref = handle_get_ref,
-	.put_ref = handle_put_ref,
+	._get_ref = handle_get_ref,
+	._put_ref = handle_put_ref,
 	.release = handle_release,
 	.merge = handle_merge,
 	.lookup = lookup,
diff --git a/src/include/fsal_api.h b/src/include/fsal_api.h
index 9cfce4422..58ab0d59e 100644
--- a/src/include/fsal_api.h
+++ b/src/include/fsal_api.h
@@ -1351,7 +1351,9 @@ struct fsal_obj_ops {
  *
  * @param[in] obj_hdl Handle to release
  */
-	 void (*get_ref)(struct fsal_obj_handle *obj_hdl);
+	 void (*_get_ref)(struct fsal_obj_handle *obj_hdl, const char *func,
+			  int line);
+#define get_ref(o) _get_ref(o, __func__, __LINE__)
 
 /**
  * @brief Put a reference to a handle
@@ -1361,7 +1363,9 @@ struct fsal_obj_ops {
  *
  * @param[in] obj_hdl Handle to release
  */
-	 void (*put_ref)(struct fsal_obj_handle *obj_hdl);
+	 void (*_put_ref)(struct fsal_obj_handle *obj_hdl, const char *func,
+			  int line);
+#define put_ref(o) _put_ref(o, __func__, __LINE__)
 
 /**
  * @brief Clean up a filehandle
