nfs-ganesha 1.4

mfsl_async_hash.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2005)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  * 
00023  * ---------------------------------------
00024  */
00025 
00036 #ifdef HAVE_CONFIG_H
00037 #include "config.h"
00038 #endif
00039 
00040 #ifdef _SOLARIS
00041 #include "solaris_port.h"
00042 #endif                          /* _SOLARIS */
00043 
00044 #include "log.h"
00045 #include "fsal_types.h"
00046 #include "fsal.h"
00047 #include "mfsl_types.h"
00048 #include "mfsl.h"
00049 #include "common_utils.h"
00050 
00051 #include <unistd.h>             /* for using gethostname */
00052 #include <stdlib.h>             /* for using exit */
00053 #include <string.h>
00054 #include <sys/types.h>
00055 
00056 hash_table_t *mfsl_ht = NULL;
00057 hash_parameter_t mfsl_hparam;
00058 
00071 unsigned long mfsl_async_hash_func(hash_parameter_t * p_hparam, hash_buffer_t * buffclef)
00072 {
00073   unsigned long h = 0;
00074 
00075   mfsl_object_t *mobject = (mfsl_object_t *) (buffclef->pdata);
00076 
00077   h = FSAL_Handle_to_HashIndex(&mobject->handle, 0, mfsl_hparam.alphabet_length,
00078                                mfsl_hparam.index_size);
00079 
00080   if (isFullDebug(COMPONENT_HASHTABLE))
00081     {
00082       char printbuf[128];
00083       snprintHandle(printbuf, 128, &mobject->handle);
00084       LogFullDebug(COMPONENT_HASHTABLE, "hash_func key: buff =(Handle=%s), hash value=%lu\n", printbuf, h);
00085     }
00086 
00087   return h;
00088 }                               /* mfsl_async_hash_func */
00089 
00102 unsigned long mfsl_async_rbt_func(hash_parameter_t * p_hparam, hash_buffer_t * buffclef)
00103 {
00104   /* A polynomial function too, but reversed, to avoid producing same value as decimal_simple_hash_func */
00105   unsigned long h = 0;
00106 
00107   mfsl_object_t *mobject = (mfsl_object_t *) (buffclef->pdata);
00108 
00109   h = FSAL_Handle_to_RBTIndex(&mobject->handle, 0);
00110 
00111 
00112   if (isFullDebug(COMPONENT_HASHTABLE))
00113     {
00114       char printbuf[128];
00115       snprintHandle(printbuf, 128, &mobject->handle);
00116       LogFullDebug(COMPONENT_HASHTABLE, "hash_func rbt: buff =(Handle=%s), value=%lu\n", printbuf, h);
00117     }
00118 
00119   return h;
00120 }                               /* mfsl_async_rbt_func */
00121 
00122 int mfsl_async_display_key(hash_buffer_t * pbuff, char *str)
00123 {
00124   mfsl_object_t *pfsdata;
00125   char buffer[128];
00126 
00127   pfsdata = (mfsl_object_t *) pbuff->pdata;
00128 
00129   snprintHandle(buffer, 128, &(pfsdata->handle));
00130 
00131   return snprintf(str, HASHTABLE_DISPLAY_STRLEN, "(Handle=%s)", buffer);
00132 }                               /* mfsl_async_display_key */
00133 
00134 int mfsl_async_display_not_implemented(hash_buffer_t * pbuff, char *str)
00135 {
00136   return snprintf(str, HASHTABLE_DISPLAY_STRLEN, "Print Not Implemented");
00137 }
00138 
00152 int mfsl_async_compare_key(hash_buffer_t * buff1, hash_buffer_t * buff2)
00153 {
00154   fsal_status_t status;
00155   mfsl_object_t *mobject1 = NULL;
00156   mfsl_object_t *mobject2 = NULL;
00157 
00158   /* Test if one of teh entries are NULL */
00159   if(buff1->pdata == NULL)
00160     return (buff2->pdata == NULL) ? 0 : 1;
00161   else
00162     {
00163       if(buff2->pdata == NULL)
00164         return -1;              /* left member is the greater one */
00165       else
00166         {
00167           int rc;
00168           mobject1 = (mfsl_object_t *) (buff1->pdata);
00169           mobject2 = (mfsl_object_t *) (buff2->pdata);
00170 
00171           rc = FSAL_handlecmp(&mobject1->handle, &mobject2->handle, &status);
00172 
00173           return rc;
00174         }
00175 
00176     }
00177   /* This line should never be reached */
00178 }                               /* mfsl_async_compare_key */
00179 
00180 int mfsl_async_hash_init(void)
00181 {
00182   mfsl_hparam.index_size = 31;
00183   mfsl_hparam.alphabet_length = 10;
00184   mfsl_hparam.nb_node_prealloc = 100;
00185   mfsl_hparam.hash_func_key = mfsl_async_hash_func;
00186   mfsl_hparam.hash_func_rbt = mfsl_async_rbt_func;
00187   mfsl_hparam.compare_key = mfsl_async_compare_key;
00188   mfsl_hparam.key_to_str = mfsl_async_display_key;
00189   mfsl_hparam.val_to_str = mfsl_async_display_not_implemented;
00190   mfsl_hparam.ht_name = "MFSL Async Cache";
00191   mfsl_hparam.flags = HT_FLAG_CACHE;
00192   mfsl_hparam.ht_log_component = COMPONENT_MFSL;
00193   
00194 
00195   /* Init de la table */
00196   if((mfsl_ht = HashTable_Init(&mfsl_hparam)) == NULL)
00197     return 0;
00198 
00199   return 1;
00200 }                               /* mfsl_async_hash_init */
00201 
00202 int mfsl_async_set_specdata(mfsl_object_t * key, mfsl_object_specific_data_t * value)
00203 {
00204   hash_buffer_t buffkey;
00205   hash_buffer_t buffdata;
00206   int rc;
00207 
00208   /* Build the key */
00209   buffkey.pdata = (caddr_t) key;
00210   buffkey.len = sizeof(mfsl_object_t);
00211 
00212   /* Build the value */
00213   buffdata.pdata = (caddr_t) value;
00214   buffdata.len = sizeof(mfsl_object_specific_data_t);
00215 
00216   rc = HashTable_Test_And_Set(mfsl_ht, &buffkey, &buffdata,
00217                               HASHTABLE_SET_HOW_SET_OVERWRITE);
00218 
00219   if(rc != HASHTABLE_SUCCESS && rc != HASHTABLE_ERROR_KEY_ALREADY_EXISTS)
00220     return 0;
00221 
00222   if (isFullDebug(COMPONENT_HASHTABLE))
00223       HashTable_Log(COMPONENT_MFSL, mfsl_ht);
00224 
00225   return 1;
00226 }                               /* mfsl_async_set_specdata */
00227 
00228 int mfsl_async_get_specdata(mfsl_object_t * key, mfsl_object_specific_data_t ** ppvalue)
00229 {
00230   hash_buffer_t buffkey;
00231   hash_buffer_t buffval;
00232   int status;
00233   int rc = 0;
00234 
00235   if(key == NULL || ppvalue == NULL)
00236     return 0;
00237 
00238   if (isFullDebug(COMPONENT_HASHTABLE))
00239       HashTable_Log(COMPONENT_MFSL, mfsl_ht);
00240 
00241   buffkey.pdata = (caddr_t) key;
00242   buffkey.len = sizeof(mfsl_object_t);
00243 
00244   rc = HashTable_Get(mfsl_ht, &buffkey, &buffval);
00245   if(rc == HASHTABLE_SUCCESS)
00246     {
00247       *ppvalue = (mfsl_object_specific_data_t *) (buffval.pdata);
00248       status = 1;
00249     }
00250   else
00251     {
00252       status = 0;
00253     }
00254 
00255   return status;
00256 }                               /* mfslasync_get_specdata */
00257 
00258 int mfsl_async_remove_specdata(mfsl_object_t * key)
00259 {
00260   hash_buffer_t buffkey, old_key;
00261   int status;
00262 
00263   if(key == NULL)
00264     return 0;
00265 
00266   buffkey.pdata = (caddr_t) key;
00267   buffkey.len = sizeof(mfsl_object_t);
00268 
00269   if(HashTable_Del(mfsl_ht, &buffkey, &old_key, NULL) == HASHTABLE_SUCCESS)
00270     {
00271       status = 1;
00272     }
00273   else
00274     {
00275       status = 0;
00276     }
00277 
00278   return status;
00279 }                               /* mfsl_async_remove_specdata */
00280 
00281 int mfsl_async_is_object_asynchronous(mfsl_object_t * object)
00282 {
00283   hash_buffer_t buffkey;
00284   hash_buffer_t buffval;
00285   int status;
00286   int rc = 0;
00287 
00288   if(object == NULL)
00289     return 0;
00290 
00291   if (isFullDebug(COMPONENT_HASHTABLE))
00292   HashTable_Log(COMPONENT_MFSL, mfsl_ht);
00293 
00294   buffkey.pdata = (caddr_t) object;
00295   buffkey.len = sizeof(mfsl_object_t);
00296 
00297   rc = HashTable_Get(mfsl_ht, &buffkey, &buffval);
00298   if(rc == HASHTABLE_SUCCESS)
00299     status = 1;
00300   else
00301     status = 0;
00302 
00303   return status;
00304 }                               /* mfsl_async_is_object_asynchronous */