static void Hash_inc_nlm_client_ref(hash_buffer_t *buffval) { state_nlm_client_t *pclient = (state_nlm_client_t *)(buffval->pdata); P(pclient->slc_mutex); pclient->slc_refcount++; if(isFullDebug(COMPONENT_STATE)) { char str[HASHTABLE_DISPLAY_STRLEN]; display_nlm_client(pclient, str); LogFullDebug(COMPONENT_STATE, "Increment refcount NLM Client {%s}", str); } V(pclient->slc_mutex); }
int display_nlm_owner(state_owner_t *pkey, char *str) { char *strtmp = str; if(pkey == NULL) return sprintf(str, "STATE_LOCK_OWNER_NLM <NULL>"); strtmp += sprintf(strtmp, "STATE_LOCK_OWNER_NLM %p: {", pkey); strtmp += display_nlm_client(pkey->so_owner.so_nlm_owner.so_client, strtmp); strtmp += sprintf(strtmp, "} oh="); strtmp += DisplayOpaqueValue(pkey->so_owner_val, pkey->so_owner_len, strtmp); strtmp += sprintf(strtmp, " svid=%d", pkey->so_owner.so_nlm_owner.so_nlm_svid); strtmp += sprintf(strtmp, " refcount=%d", atomic_fetch_int32_t(&pkey->so_refcount)); return strtmp - str; }
state_nlm_client_t *get_nlm_client(care_t care, SVCXPRT * xprt, state_nsm_client_t * pnsm_client, char * caller_name) { state_nlm_client_t key; state_nlm_client_t * pclient; char str[HASHTABLE_DISPLAY_STRLEN]; struct hash_latch latch; hash_error_t rc; hash_buffer_t buffkey; hash_buffer_t buffval; if(caller_name == NULL) return NULL; memset(&key, 0, sizeof(key)); key.slc_nsm_client = pnsm_client; key.slc_nlm_caller_name_len = strlen(caller_name); key.slc_client_type = svc_get_xprt_type(xprt); if(key.slc_nlm_caller_name_len > LM_MAXSTRLEN) return NULL; key.slc_nlm_caller_name = caller_name; if(isFullDebug(COMPONENT_STATE)) { display_nlm_client(&key, str); LogFullDebug(COMPONENT_STATE, "Find {%s}", str); } buffkey.pdata = &key; buffkey.len = sizeof(key); rc = HashTable_GetLatch(ht_nlm_client, &buffkey, &buffval, TRUE, &latch); /* If we found it, return it */ if(rc == HASHTABLE_SUCCESS) { pclient = buffval.pdata; /* Return the found NLM Client */ if(isFullDebug(COMPONENT_STATE)) { display_nlm_client(pclient, str); LogFullDebug(COMPONENT_STATE, "Found {%s}", str); } /* Increment refcount under hash latch. * This prevents dec ref from removing this entry from hash if a race * occurs. */ inc_nlm_client_ref(pclient); HashTable_ReleaseLatched(ht_nlm_client, &latch); if(care == CARE_MONITOR && !nsm_monitor(pnsm_client)) { dec_nlm_client_ref(pclient); pclient = NULL; } return pclient; } /* An error occurred, return NULL */ if(rc != HASHTABLE_ERROR_NO_SUCH_KEY) { display_nlm_client(&key, str); LogCrit(COMPONENT_STATE, "Error %s, could not find {%s}", hash_table_err_to_str(rc), str); return NULL; } /* Not found, but we don't care, return NULL */ if(care == CARE_NOT) { /* Return the found NLM Client */ if(isFullDebug(COMPONENT_STATE)) { display_nlm_client(&key, str); LogFullDebug(COMPONENT_STATE, "Ignoring {%s}", str); } HashTable_ReleaseLatched(ht_nlm_client, &latch); return NULL; } pclient = gsh_malloc(sizeof(*pclient)); if(pclient == NULL) { display_nlm_client(&key, str); LogCrit(COMPONENT_STATE, "No memory for {%s}", str); return NULL; } /* Copy everything over */ memcpy(pclient, &key, sizeof(key)); pclient->slc_nlm_caller_name = gsh_strdup(key.slc_nlm_caller_name); /* Take a reference to the NSM Client */ inc_nsm_client_ref(pnsm_client); if(pclient->slc_nlm_caller_name == NULL) { /* Discard the created client */ free_nlm_client(pclient); return NULL; } pclient->slc_refcount = 1; if(isFullDebug(COMPONENT_STATE)) { display_nlm_client(pclient, str); LogFullDebug(COMPONENT_STATE, "New {%s}", str); } buffkey.pdata = pclient; buffkey.len = sizeof(*pclient); buffval.pdata = pclient; buffval.len = sizeof(*pclient); rc = HashTable_SetLatched(ht_nlm_client, &buffval, &buffval, &latch, FALSE, NULL, NULL); /* An error occurred, return NULL */ if(rc != HASHTABLE_SUCCESS) { display_nlm_client(pclient, str); LogCrit(COMPONENT_STATE, "Error %s, inserting {%s}", hash_table_err_to_str(rc), str); free_nlm_client(pclient); return NULL; } if(care != CARE_MONITOR || nsm_monitor(pnsm_client)) return pclient; /* Failed to monitor, release client reference * and almost certainly remove it from the hash table. */ dec_nlm_client_ref(pclient); return NULL; }
void dec_nlm_client_ref(state_nlm_client_t *pclient) { char str[HASHTABLE_DISPLAY_STRLEN]; struct hash_latch latch; hash_error_t rc; hash_buffer_t buffkey; hash_buffer_t old_value; hash_buffer_t old_key; int32_t refcount; if(isDebug(COMPONENT_STATE)) display_nlm_client(pclient, str); refcount = atomic_dec_int32_t(&pclient->slc_refcount); if(refcount > 0) { LogFullDebug(COMPONENT_STATE, "Decrement refcount now=%"PRId32" {%s}", refcount, str); return; } LogFullDebug(COMPONENT_STATE, "Try to remove {%s}", str); buffkey.pdata = pclient; buffkey.len = sizeof(*pclient); /* Get the hash table entry and hold latch */ rc = HashTable_GetLatch(ht_nlm_client, &buffkey, &old_value, TRUE, &latch); if(rc != HASHTABLE_SUCCESS) { if(rc == HASHTABLE_ERROR_NO_SUCH_KEY) HashTable_ReleaseLatched(ht_nlm_client, &latch); display_nlm_client(pclient, str); LogCrit(COMPONENT_STATE, "Error %s, could not find {%s}", hash_table_err_to_str(rc), str); return; } refcount = atomic_fetch_int32_t(&pclient->slc_refcount); if(refcount > 0) { LogDebug(COMPONENT_STATE, "Did not release refcount now=%"PRId32" {%s}", refcount, str); HashTable_ReleaseLatched(ht_nlm_client, &latch); return; } /* use the key to delete the entry */ rc = HashTable_DeleteLatched(ht_nlm_client, &buffkey, &latch, &old_key, &old_value); if(rc != HASHTABLE_SUCCESS) { if(rc == HASHTABLE_ERROR_NO_SUCH_KEY) HashTable_ReleaseLatched(ht_nlm_client, &latch); display_nlm_client(pclient, str); LogCrit(COMPONENT_STATE, "Error %s, could not remove {%s}", hash_table_err_to_str(rc), str); return; } LogFullDebug(COMPONENT_STATE, "Free {%s}", str); free_nlm_client(old_value.pdata); }
int display_nlm_client_val(hash_buffer_t * pbuff, char *str) { return display_nlm_client(pbuff->pdata, str); }
int display_nlm_client_key(hash_buffer_t * pbuff, char *str) { return display_nlm_client((state_nlm_client_t *)pbuff->pdata, str); }
state_nlm_client_t *get_nlm_client(care_t care, SVCXPRT * xprt, state_nsm_client_t * pnsm_client, const char * caller_name) { state_nlm_client_t *pkey, *pclient; if(caller_name == NULL) return NULL; pkey = (state_nlm_client_t *)Mem_Alloc(sizeof(*pkey)); if(pkey == NULL) return NULL; memset(pkey, 0, sizeof(*pkey)); pkey->slc_refcount = 1; pkey->slc_nsm_client = pnsm_client; pkey->slc_nlm_caller_name_len = strlen(caller_name); pkey->slc_client_type = get_xprt_type(xprt); if(pkey->slc_nlm_caller_name_len > LM_MAXSTRLEN) { /* Discard the key we created */ Mem_Free(pkey); return NULL; } memcpy(pkey->slc_nlm_caller_name, caller_name, pkey->slc_nlm_caller_name_len); pkey->slc_nlm_caller_name[pkey->slc_nlm_caller_name_len] = '\0'; if(isFullDebug(COMPONENT_STATE)) { char str[HASHTABLE_DISPLAY_STRLEN]; display_nlm_client(pkey, str); LogFullDebug(COMPONENT_STATE, "Find NLM Client pkey {%s}", str); } /* If we found it, return it, if we don't care, return NULL */ if(nlm_client_Get_Pointer(pkey, &pclient) == 1 || care == CARE_NOT) { /* Discard the key we created and return the found NLM Client */ Mem_Free(pkey); if(isFullDebug(COMPONENT_STATE)) { char str[HASHTABLE_DISPLAY_STRLEN]; display_nlm_client(pclient, str); LogFullDebug(COMPONENT_STATE, "Found NLM Client {%s}", str); } if(care == CARE_MONITOR) if(!nsm_monitor(pnsm_client)) { dec_nlm_client_ref(pclient); return NULL; } return pclient; } pclient = (state_nlm_client_t *)Mem_Alloc(sizeof(*pkey)); if(pclient == NULL) { Mem_Free(pkey); return NULL; } /* Copy everything over */ *pclient = *pkey; if(isFullDebug(COMPONENT_STATE)) { char str[HASHTABLE_DISPLAY_STRLEN]; display_nlm_client(pclient, str); LogFullDebug(COMPONENT_STATE, "New NLM Client {%s}", str); } if(pthread_mutex_init(&pclient->slc_mutex, NULL) == -1) { /* Mutex initialization failed, free the key and created owner */ Mem_Free(pkey); Mem_Free(pclient); return NULL; } /* Ref count the NSM Client as being used by this NLM Client */ inc_nsm_client_ref(pnsm_client); if(nlm_client_Set(pkey, pclient) == 1) { if(isFullDebug(COMPONENT_STATE)) { char str[HASHTABLE_DISPLAY_STRLEN]; display_nlm_client(pclient, str); LogFullDebug(COMPONENT_STATE, "Set NLM Client {%s}", str); } if(care != CARE_MONITOR || nsm_monitor(pnsm_client)) return pclient; dec_nlm_client_ref(pclient); return NULL; } dec_nsm_client_ref(pnsm_client); Mem_Free(pkey); Mem_Free(pclient); return NULL; }
void dec_nlm_client_ref_locked(state_nlm_client_t *pclient) { bool_t remove = FALSE; char str[HASHTABLE_DISPLAY_STRLEN]; if(isFullDebug(COMPONENT_STATE)) display_nlm_client(pclient, str); if(pclient->slc_refcount > 1) { pclient->slc_refcount--; LogFullDebug(COMPONENT_STATE, "Decrement refcount NLM Client {%s}", str); } else remove = TRUE; V(pclient->slc_mutex); if(remove) { hash_buffer_t buffkey, old_key, old_value; buffkey.pdata = (caddr_t) pclient; buffkey.len = sizeof(*pclient); switch(HashTable_DelRef(ht_nlm_client, &buffkey, &old_key, &old_value, Hash_dec_nlm_client_ref)) { case HASHTABLE_SUCCESS: LogFullDebug(COMPONENT_STATE, "Free NLM Client {%s} size %llx", str, (unsigned long long) old_value.len); if(pclient->slc_callback_clnt != NULL) Clnt_destroy(pclient->slc_callback_clnt); dec_nsm_client_ref(pclient->slc_nsm_client); if(isFullDebug(COMPONENT_MEMLEAKS)) { memset(old_key.pdata, 0, old_key.len); memset(old_value.pdata, 0, old_value.len); } Mem_Free(old_key.pdata); Mem_Free(old_value.pdata); break; case HASHTABLE_NOT_DELETED: /* ref count didn't end up at 0, don't free. */ LogDebug(COMPONENT_STATE, "HashTable_DelRef didn't reduce refcount to 0 for NLM Client {%s}", str); break; default: /* some problem occurred */ LogDebug(COMPONENT_STATE, "HashTable_DelRef failed for NLM Client {%s}", str); break; } } }
/** * @brief Display an NLM client in the hash table * * @param[in] buff The value * @param[out] str Output buffer * * @return Length of output string. */ int display_nlm_client_val(struct gsh_buffdesc *buff, char *str) { struct display_buffer dspbuf = {HASHTABLE_DISPLAY_STRLEN, str, str}; display_nlm_client(&dspbuf, buff->addr); return display_buffer_len(&dspbuf); }
/** * @brief Get an NLM client * * @param[in] care Care status * @param[in] xprt RPC transport * @param[in] nsm_client Related NSM client * @param[in] caller_name Caller name * * @return NLM client or NULL. */ state_nlm_client_t *get_nlm_client(care_t care, SVCXPRT *xprt, state_nsm_client_t *nsm_client, char *caller_name) { state_nlm_client_t key; state_nlm_client_t *pclient; char str[LOG_BUFF_LEN]; struct display_buffer dspbuf = {sizeof(str), str, str}; struct hash_latch latch; hash_error_t rc; struct gsh_buffdesc buffkey; struct gsh_buffdesc buffval; struct sockaddr_storage local_addr; socklen_t addr_len; if (caller_name == NULL) return NULL; memset(&key, 0, sizeof(key)); key.slc_nsm_client = nsm_client; key.slc_nlm_caller_name_len = strlen(caller_name); key.slc_client_type = svc_get_xprt_type(xprt); addr_len = sizeof(local_addr); if (getsockname(xprt->xp_fd, (struct sockaddr *)&local_addr, &addr_len) == -1) { LogEvent(COMPONENT_CLIENTID, "Failed to get local addr."); } else { memcpy(&(key.slc_server_addr), &local_addr, sizeof(struct sockaddr_storage)); } if (key.slc_nlm_caller_name_len > LM_MAXSTRLEN) return NULL; key.slc_nlm_caller_name = caller_name; if (isFullDebug(COMPONENT_STATE)) { display_nlm_client(&dspbuf, &key); LogFullDebug(COMPONENT_STATE, "Find {%s}", str); } buffkey.addr = &key; buffkey.len = sizeof(key); rc = hashtable_getlatch(ht_nlm_client, &buffkey, &buffval, true, &latch); /* If we found it, return it */ if (rc == HASHTABLE_SUCCESS) { pclient = buffval.addr; /* Return the found NLM Client */ if (isFullDebug(COMPONENT_STATE)) { display_nlm_client(&dspbuf, pclient); LogFullDebug(COMPONENT_STATE, "Found {%s}", str); } /* Increment refcount under hash latch. * This prevents dec ref from removing this entry from hash * if a race occurs. */ inc_nlm_client_ref(pclient); hashtable_releaselatched(ht_nlm_client, &latch); if (care == CARE_MONITOR && !nsm_monitor(nsm_client)) { dec_nlm_client_ref(pclient); pclient = NULL; } return pclient; } /* An error occurred, return NULL */ if (rc != HASHTABLE_ERROR_NO_SUCH_KEY) { display_nlm_client(&dspbuf, &key); LogCrit(COMPONENT_STATE, "Error %s, could not find {%s}", hash_table_err_to_str(rc), str); return NULL; } /* Not found, but we don't care, return NULL */ if (care == CARE_NOT) { /* Return the found NLM Client */ if (isFullDebug(COMPONENT_STATE)) { display_nlm_client(&dspbuf, &key); LogFullDebug(COMPONENT_STATE, "Ignoring {%s}", str); } hashtable_releaselatched(ht_nlm_client, &latch); return NULL; } pclient = gsh_malloc(sizeof(*pclient)); if (pclient == NULL) { display_nlm_client(&dspbuf, &key); LogCrit(COMPONENT_STATE, "No memory for {%s}", str); return NULL; } /* Copy everything over */ memcpy(pclient, &key, sizeof(key)); pclient->slc_nlm_caller_name = gsh_strdup(key.slc_nlm_caller_name); /* Take a reference to the NSM Client */ inc_nsm_client_ref(nsm_client); if (pclient->slc_nlm_caller_name == NULL) { /* Discard the created client */ free_nlm_client(pclient); return NULL; } pclient->slc_refcount = 1; if (isFullDebug(COMPONENT_STATE)) { display_nlm_client(&dspbuf, pclient); LogFullDebug(COMPONENT_STATE, "New {%s}", str); } buffkey.addr = pclient; buffkey.len = sizeof(*pclient); buffval.addr = pclient; buffval.len = sizeof(*pclient); rc = hashtable_setlatched(ht_nlm_client, &buffval, &buffval, &latch, false, NULL, NULL); /* An error occurred, return NULL */ if (rc != HASHTABLE_SUCCESS) { display_nlm_client(&dspbuf, pclient); LogCrit(COMPONENT_STATE, "Error %s, inserting {%s}", hash_table_err_to_str(rc), str); free_nlm_client(pclient); return NULL; } if (care != CARE_MONITOR || nsm_monitor(nsm_client)) return pclient; /* Failed to monitor, release client reference * and almost certainly remove it from the hash table. */ dec_nlm_client_ref(pclient); return NULL; }
/** * @brief Relinquish a reference on an NLM client * * @param[in] client Client to release */ void dec_nlm_client_ref(state_nlm_client_t *client) { char str[LOG_BUFF_LEN]; struct display_buffer dspbuf = {sizeof(str), str, str}; bool str_valid = false; struct hash_latch latch; hash_error_t rc; struct gsh_buffdesc buffkey; struct gsh_buffdesc old_value; struct gsh_buffdesc old_key; int32_t refcount; if (isDebug(COMPONENT_STATE)) { display_nlm_client(&dspbuf, client); str_valid = true; } refcount = atomic_dec_int32_t(&client->slc_refcount); if (refcount > 0) { if (str_valid) LogFullDebug(COMPONENT_STATE, "Decrement refcount now=%" PRId32 " {%s}", refcount, str); return; } if (str_valid) LogFullDebug(COMPONENT_STATE, "Try to remove {%s}", str); buffkey.addr = client; buffkey.len = sizeof(*client); /* Get the hash table entry and hold latch */ rc = hashtable_getlatch(ht_nlm_client, &buffkey, &old_value, true, &latch); if (rc != HASHTABLE_SUCCESS) { if (rc == HASHTABLE_ERROR_NO_SUCH_KEY) hashtable_releaselatched(ht_nlm_client, &latch); if (!str_valid) display_nlm_client(&dspbuf, client); LogCrit(COMPONENT_STATE, "Error %s, could not find {%s}", hash_table_err_to_str(rc), str); return; } refcount = atomic_fetch_int32_t(&client->slc_refcount); if (refcount > 0) { if (str_valid) LogDebug(COMPONENT_STATE, "Did not release refcount now=%"PRId32" {%s}", refcount, str); hashtable_releaselatched(ht_nlm_client, &latch); return; } /* use the key to delete the entry */ rc = hashtable_deletelatched(ht_nlm_client, &buffkey, &latch, &old_key, &old_value); if (rc != HASHTABLE_SUCCESS) { if (rc == HASHTABLE_ERROR_NO_SUCH_KEY) hashtable_releaselatched(ht_nlm_client, &latch); if (!str_valid) display_nlm_client(&dspbuf, client); LogCrit(COMPONENT_STATE, "Error %s, could not remove {%s}", hash_table_err_to_str(rc), str); return; } if (str_valid) LogFullDebug(COMPONENT_STATE, "Free {%s}", str); free_nlm_client(old_value.addr); }