nfs-ganesha 1.4

fsal_tools.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  */
00004 
00013 #ifdef HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 #include "fsal.h"
00018 #include "fsal_internal.h"
00019 #include "fsal_common.h"
00020 #include "fsal_convert.h"
00021 #include "config_parsing.h"
00022 #include "fsal_common.h"
00023 #include <string.h>
00024 
00025 /* case unsensitivity */
00026 #define STRCMP   strcasecmp
00027 
00028 char *FSAL_GetFSName()
00029 {
00030   return "TEMPLATE";
00031 }
00032 
00049 int FSAL_handlecmp(fsal_handle_t * handle1, fsal_handle_t * handle2,
00050                    fsal_status_t * status)
00051 {
00052 
00053   fsal_u64_t fileid1, fileid2;
00054 
00055   *status = FSAL_STATUS_NO_ERROR;
00056 
00057   if(!handle1 || !handle2)
00058     {
00059       status->major = ERR_FSAL_FAULT;
00060       return -1;
00061     }
00062 
00063   /* >> compare your handles here << */
00064 
00065 }
00066 
00081 unsigned int FSAL_Handle_to_HashIndex(fsal_handle_t * p_handle,
00082                                       unsigned int cookie,
00083                                       unsigned int alphabet_len, unsigned int index_size)
00084 {
00085 
00086   /* >> here must be your implementation of your fsal_handle_t hashing */
00087   return (3 * (unsigned int)p_handle->object_identifier + 1999 + cookie) % index_size;
00088 
00089 }
00090 
00091 /*
00092  * FSAL_Handle_to_RBTIndex
00093  * This function is used for generating a RBT node ID
00094  * in order to identify entries into the RBT.
00095  *
00096  * \param p_handle      The handle to be hashed
00097  * \param cookie        Makes it possible to have different hash value for the
00098  *                      same handle, when cookie changes.
00099  *
00100  * \return The hash value
00101  */
00102 
00103 unsigned int FSAL_Handle_to_RBTIndex(fsal_handle_t * p_handle, unsigned int cookie)
00104 {
00105   /* >> here must be your implementation of your fsal_handle_t hashing << */
00106   return (unsigned int)(0xABCD1234 ^ p_handle->object_identifier ^ cookie);
00107 
00108 }
00109 
00127 fsal_status_t FSAL_DigestHandle(fsal_export_context_t * p_expcontext,   /* IN */
00128                                 fsal_digesttype_t output_type,  /* IN */
00129                                 const fsal_handle_t * in_fsal_handle, /* IN */
00130                                 caddr_t out_buff        /* OUT */
00131     )
00132 {
00133 
00134   /* sanity checks */
00135   if(!in_fsal_handle || !out_buff || !p_expcontext)
00136     ReturnCode(ERR_FSAL_FAULT, 0);
00137 
00138   switch (output_type)
00139     {
00140 
00141       /* NFSV2 handle digest */
00142     case FSAL_DIGEST_NFSV2:
00143 
00144       /* >> Your FSAL handle must be converted to a 25 bytes digest. << */
00145 
00146       break;
00147 
00148       /* NFSV3 handle digest */
00149     case FSAL_DIGEST_NFSV3:
00150 
00151       /* >> Your FSAL handle must be converted to a 25 bytes digest. << */
00152 
00153       break;
00154 
00155       /* NFSV4 handle digest */
00156     case FSAL_DIGEST_NFSV4:
00157 
00158       /* >> Your FSAL handle must be converted to a 25 bytes digest. << */
00159 
00160       break;
00161 
00162       /* FileId digest for NFSv2 */
00163     case FSAL_DIGEST_FILEID2:
00164 
00165       /* >> You must store the objectid into the output buff (of size FSAL_DIGEST_SIZE_FILEID2). << */
00166 
00167       break;
00168 
00169       /* FileId digest for NFSv3 */
00170     case FSAL_DIGEST_FILEID3:
00171 
00172       /* >> You must store the objectid into the output buff (of size FSAL_DIGEST_SIZE_FILEID3). << */
00173 
00174       break;
00175 
00176       /* FileId digest for NFSv4 */
00177 
00178     case FSAL_DIGEST_FILEID4:
00179 
00180       /* >> You must store the objectid into the output buff (of size FSAL_DIGEST_SIZE_FILEID4). << */
00181 
00182       break;
00183 
00184     default:
00185       ReturnCode(ERR_FSAL_SERVERFAULT, 0);
00186     }
00187 
00188   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00189 
00190 }                               /* FSAL_DigestHandle */
00191 
00207 fsal_status_t FSAL_ExpandHandle(fsal_export_context_t * p_expcontext,   /* IN */
00208                                 fsal_digesttype_t in_type,      /* IN */
00209                                 caddr_t in_buff,        /* IN */
00210                                 fsal_handle_t * out_fsal_handle /* OUT */
00211     )
00212 {
00213 
00214   /* sanity checks */
00215   if(!out_fsal_handle || !in_buff || !p_expcontext)
00216     ReturnCode(ERR_FSAL_FAULT, 0);
00217 
00218   switch (in_type)
00219     {
00220 
00221     case FSAL_DIGEST_NFSV2:
00222 
00223       /* >> do the inverse of FSAL_DigestHandle << */
00224 
00225       break;
00226 
00227     case FSAL_DIGEST_NFSV3:
00228 
00229       /* >> do the inverse of FSAL_DigestHandle << */
00230 
00231       break;
00232 
00233     case FSAL_DIGEST_NFSV4:
00234 
00235       /* >> do the inverse of FSAL_DigestHandle << */
00236 
00237       break;
00238 
00239     default:
00240       /* Invalid input digest type. */
00241       ReturnCode(ERR_FSAL_INVAL, 0);
00242     }
00243 
00244   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00245 
00246 }
00247 
00255 fsal_status_t FSAL_SetDefault_FSAL_parameter(fsal_parameter_t * out_parameter)
00256 {
00257   /* defensive programming... */
00258   if(out_parameter == NULL)
00259     ReturnCode(ERR_FSAL_FAULT, 0);
00260 
00261   /* init max FS calls = unlimited */
00262   out_parameter->fsal_info.max_fs_calls = 0;
00263 
00264   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00265 
00266 }
00267 
00268 fsal_status_t FSAL_SetDefault_FS_common_parameter(fsal_parameter_t * out_parameter)
00269 {
00270   /* defensive programming... */
00271   if(out_parameter == NULL)
00272     ReturnCode(ERR_FSAL_FAULT, 0);
00273 
00274   /* set default values for all parameters of fs_common_info */
00275 
00276   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, maxfilesize);
00277   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, maxlink);
00278   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, maxnamelen);
00279   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, maxpathlen);
00280   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, no_trunc);
00281   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, chown_restricted);
00282   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, case_insensitive);
00283   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, case_preserving);
00284   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, fh_expire_type);
00285   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, link_support);
00286   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, symlink_support);
00287   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, named_attr);
00288   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, unique_handles);
00289   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, lease_time);
00290   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, acl_support);
00291   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, cansettime);
00292   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, homogenous);
00293   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, supported_attrs);
00294   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, maxread);
00295   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, maxwrite);
00296   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, umask);
00297   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, auth_exportpath_xdev);
00298   FSAL_SET_INIT_DEFAULT(out_parameter->fs_common_info, xattr_access_rights);
00299 
00300   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00301 
00302 }
00303 
00304 fsal_status_t FSAL_SetDefault_FS_specific_parameter(fsal_parameter_t * out_parameter)
00305 {
00306   /* defensive programming... */
00307   if(out_parameter == NULL)
00308     ReturnCode(ERR_FSAL_FAULT, 0);
00309 
00310   /* >> set your default FS configuration into the
00311      out_parameter->fs_specific_info structure << */
00312 
00313   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00314 
00315 }
00316 
00338 /* load FSAL init info */
00339 
00340 fsal_status_t FSAL_load_FSAL_parameter_from_conf(config_file_t in_config,
00341                                                  fsal_parameter_t * out_parameter)
00342 {
00343   int err;
00344   int var_max, var_index;
00345   char *key_name;
00346   char *key_value;
00347   config_item_t block;
00348 
00349   int DebugLevel = -1;
00350   char *LogFile = NULL;
00351 
00352   block = config_FindItemByName(in_config, CONF_LABEL_FSAL);
00353 
00354   /* cannot read item */
00355 
00356   if(block == NULL)
00357     {
00358       LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: Cannot read item \"%s\" from configuration file",
00359               CONF_LABEL_FSAL);
00360       ReturnCode(ERR_FSAL_NOENT, 0);
00361     }
00362   else if(config_ItemType(block) != CONFIG_ITEM_BLOCK)
00363     {
00364       LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: Item \"%s\" is expected to be a block",
00365               CONF_LABEL_FSAL);
00366       ReturnCode(ERR_FSAL_INVAL, 0);
00367     }
00368 
00369   /* read variable for fsal init */
00370 
00371   var_max = config_GetNbItems(block);
00372 
00373   for(var_index = 0; var_index < var_max; var_index++)
00374     {
00375       config_item_t item;
00376 
00377       item = config_GetItemByIndex(block, var_index);
00378 
00379       err = config_GetKeyValue(item, &key_name, &key_value);
00380       if(err)
00381         {
00382           LogCrit(COMPONENT_CONFIG,
00383                   "FSAL LOAD PARAMETER: ERROR reading key[%d] from section \"%s\" of configuration file.",
00384                   var_index, CONF_LABEL_FSAL);
00385           ReturnCode(ERR_FSAL_SERVERFAULT, err);
00386         }
00387 
00388       if(!STRCMP(key_name, "DebugLevel"))
00389         {
00390           DebugLevel = ReturnLevelAscii(key_value);
00391 
00392           if(DebugLevel == -1)
00393             {
00394               LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: ERROR: Invalid debug level name: \"%s\".",
00395                       key_value);
00396               ReturnCode(ERR_FSAL_INVAL, -1);
00397             }
00398 
00399         }
00400       else if(!STRCMP(key_name, "LogFile"))
00401         {
00402 
00403           LogFile = key_value;
00404 
00405         }
00406       else if(!STRCMP(key_name, "Max_FS_calls"))
00407         {
00408 
00409           int maxcalls = s_read_int(key_value);
00410 
00411           if(maxcalls < 0)
00412             {
00413               LogCrit(COMPONENT_CONFIG,
00414                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: null or positive integer expected.",
00415                       key_name);
00416               ReturnCode(ERR_FSAL_INVAL, 0);
00417             }
00418 
00419           out_parameter->fsal_info.max_fs_calls = (unsigned int)maxcalls;
00420 
00421         }
00422       else
00423         {
00424           LogCrit(COMPONENT_CONFIG,
00425                   "FSAL LOAD PARAMETER: ERROR: Unknown or unsettable key: %s (item %s)",
00426                   key_name, CONF_LABEL_FSAL);
00427           ReturnCode(ERR_FSAL_INVAL, 0);
00428         }
00429 
00430     }
00431 
00432   /* init logging */
00433 
00434   if(LogFile)
00435     SetComponentLogFile(COMPONENT_FSAL, LogFile);
00436 
00437   if(DebugLevel != -1)
00438     SetComponentLogLevel(COMPONENT_FSAL, DebugLevel);
00439 
00440   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00441 
00442 }                               /* FSAL_load_FSAL_parameter_from_conf */
00443 
00444 /* load general filesystem configuration options */
00445 
00446 fsal_status_t FSAL_load_FS_common_parameter_from_conf(config_file_t in_config,
00447                                                       fsal_parameter_t * out_parameter)
00448 {
00449   int err;
00450   int var_max, var_index;
00451   char *key_name;
00452   char *key_value;
00453   config_item_t block;
00454 
00455   block = config_FindItemByName(in_config, CONF_LABEL_FS_COMMON);
00456 
00457   /* cannot read item */
00458   if(block == NULL)
00459     {
00460       LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: Cannot read item \"%s\" from configuration file",
00461               CONF_LABEL_FS_COMMON);
00462       ReturnCode(ERR_FSAL_NOENT, 0);
00463     }
00464   else if(config_ItemType(block) != CONFIG_ITEM_BLOCK)
00465     {
00466       LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: Item \"%s\" is expected to be a block",
00467               CONF_LABEL_FS_COMMON);
00468       ReturnCode(ERR_FSAL_INVAL, 0);
00469     }
00470 
00471   /*
00472      configurable common info for filesystem are:
00473      link_support      # hardlink support
00474      symlink_support   # symlinks support
00475      cansettime        # Is it possible to change file times
00476      maxread           # Max read size from FS
00477      maxwrite          # Max write size to FS
00478      umask
00479      auth_exportpath_xdev
00480      xattr_access_rights
00481    */
00482 
00483   var_max = config_GetNbItems(block);
00484 
00485   for(var_index = 0; var_index < var_max; var_index++)
00486     {
00487       config_item_t item;
00488 
00489       item = config_GetItemByIndex(block, var_index);
00490 
00491       err = config_GetKeyValue(item, &key_name, &key_value);
00492       if(err)
00493         {
00494           LogCrit(COMPONENT_CONFIG,
00495                   "FSAL LOAD PARAMETER: ERROR reading key[%d] from section \"%s\" of configuration file.",
00496                   var_index, CONF_LABEL_FS_COMMON);
00497           ReturnCode(ERR_FSAL_SERVERFAULT, err);
00498         }
00499 
00500       /* does the variable exists ? */
00501       if(!STRCMP(key_name, "link_support"))
00502         {
00503 
00504           int bool = StrToBoolean(key_value);
00505 
00506           if(bool == -1)
00507             {
00508               LogCrit(COMPONENT_CONFIG,
00509                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: boolean expected.",
00510                       key_name);
00511               ReturnCode(ERR_FSAL_INVAL, 0);
00512             }
00513 
00514           /* if set to false, force value to false.
00515            * else keep fs default.
00516            */
00517           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, link_support,
00518                              FSAL_INIT_MAX_LIMIT, bool);
00519 
00520         }
00521       else if(!STRCMP(key_name, "symlink_support"))
00522         {
00523           int bool = StrToBoolean(key_value);
00524 
00525           if(bool == -1)
00526             {
00527               LogCrit(COMPONENT_CONFIG,
00528                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: boolean expected.",
00529                       key_name);
00530               ReturnCode(ERR_FSAL_INVAL, 0);
00531             }
00532 
00533           /* if set to false, force value to false.
00534            * else keep fs default.
00535            */
00536           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, symlink_support,
00537                              FSAL_INIT_MAX_LIMIT, bool);
00538         }
00539       else if(!STRCMP(key_name, "cansettime"))
00540         {
00541           int bool = StrToBoolean(key_value);
00542 
00543           if(bool == -1)
00544             {
00545               LogCrit(COMPONENT_CONFIG,
00546                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: boolean expected.",
00547                       key_name);
00548               ReturnCode(ERR_FSAL_INVAL, 0);
00549             }
00550 
00551           /* if set to false, force value to false.
00552            * else keep fs default.
00553            */
00554           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, cansettime,
00555                              FSAL_INIT_MAX_LIMIT, bool);
00556 
00557         }
00558       else if(!STRCMP(key_name, "maxread"))
00559         {
00560           fsal_u64_t size;
00561 
00562           if(s_read_int64(key_value, &size))
00563             {
00564               LogCrit(COMPONENT_CONFIG,
00565                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: positive integer expected.",
00566                       key_name);
00567               ReturnCode(ERR_FSAL_INVAL, 0);
00568             }
00569 
00570           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, maxread,
00571                              FSAL_INIT_FORCE_VALUE, size);
00572 
00573         }
00574       else if(!STRCMP(key_name, "maxwrite"))
00575         {
00576           fsal_u64_t size;
00577 
00578           if(s_read_int64(key_value, &size))
00579             {
00580               LogCrit(COMPONENT_CONFIG,
00581                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: positive integer expected.",
00582                       key_name);
00583               ReturnCode(ERR_FSAL_INVAL, 0);
00584             }
00585 
00586           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, maxwrite,
00587                              FSAL_INIT_FORCE_VALUE, size);
00588 
00589         }
00590       else if(!STRCMP(key_name, "umask"))
00591         {
00592           int mode = s_read_octal(key_value);
00593 
00594           if(mode < 0)
00595             {
00596               LogCrit(COMPONENT_CONFIG,
00597                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: octal expected.",
00598                       key_name);
00599               ReturnCode(ERR_FSAL_INVAL, 0);
00600             }
00601 
00602           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, umask,
00603                              FSAL_INIT_FORCE_VALUE, unix2fsal_mode(mode));
00604 
00605         }
00606       else if(!STRCMP(key_name, "auth_xdev_export"))
00607         {
00608           int bool = StrToBoolean(key_value);
00609 
00610           if(bool == -1)
00611             {
00612               LogCrit(COMPONENT_CONFIG,
00613                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: boolean expected.",
00614                       key_name);
00615               ReturnCode(ERR_FSAL_INVAL, 0);
00616             }
00617 
00618           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, auth_exportpath_xdev,
00619                              FSAL_INIT_FORCE_VALUE, bool);
00620         }
00621       else if(!STRCMP(key_name, "xattr_access_rights"))
00622         {
00623           int mode = s_read_octal(key_value);
00624 
00625           if(mode < 0)
00626             {
00627               LogCrit(COMPONENT_CONFIG,
00628                       "FSAL LOAD PARAMETER: ERROR: Unexpected value for %s: octal expected.",
00629                       key_name);
00630               ReturnCode(ERR_FSAL_INVAL, 0);
00631             }
00632 
00633           FSAL_SET_INIT_INFO(out_parameter->fs_common_info, xattr_access_rights,
00634                              FSAL_INIT_FORCE_VALUE, unix2fsal_mode(mode));
00635 
00636         }
00637       else
00638         {
00639           LogCrit(COMPONENT_CONFIG,
00640                   "FSAL LOAD PARAMETER: ERROR: Unknown or unsettable key: %s (item %s)",
00641                   key_name, CONF_LABEL_FS_COMMON);
00642           ReturnCode(ERR_FSAL_INVAL, 0);
00643         }
00644 
00645     }
00646 
00647   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00648 
00649 }                               /* FSAL_load_FS_common_parameter_from_conf */
00650 
00651 /* load specific filesystem configuration options */
00652 
00653 fsal_status_t FSAL_load_FS_specific_parameter_from_conf(config_file_t in_config,
00654                                                         fsal_parameter_t * out_parameter)
00655 {
00656   int err;
00657   int blk_index;
00658   int var_max, var_index;
00659   char *key_name;
00660   char *key_value;
00661   config_item_t block;
00662 
00663   block = config_FindItemByName(in_config, CONF_LABEL_FS_SPECIFIC);
00664 
00665   /* cannot read item */
00666   if(block == NULL)
00667     {
00668       LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: Cannot read item \"%s\" from configuration file",
00669               CONF_LABEL_FS_SPECIFIC);
00670       ReturnCode(ERR_FSAL_NOENT, 0);
00671     }
00672   else if(config_ItemType(block) != CONFIG_ITEM_BLOCK)
00673     {
00674       LogCrit(COMPONENT_CONFIG, "FSAL LOAD PARAMETER: Item \"%s\" is expected to be a block",
00675               CONF_LABEL_FS_SPECIFIC);
00676       ReturnCode(ERR_FSAL_INVAL, 0);
00677     }
00678 
00679   /* makes an iteration on the (key, value) couplets */
00680 
00681   var_max = config_GetNbItems(block);
00682 
00683   for(var_index = 0; var_index < var_max; var_index++)
00684     {
00685       config_item_t item;
00686 
00687       item = config_GetItemByIndex(block, var_index);
00688 
00689       err = config_GetKeyValue(item, &key_name, &key_value);
00690       if(err)
00691         {
00692           LogCrit(COMPONENT_CONFIG,
00693                   "FSAL LOAD PARAMETER: ERROR reading key[%d] from section \"%s\" of configuration file.",
00694                   var_index, CONF_LABEL_FS_SPECIFIC);
00695           ReturnCode(ERR_FSAL_SERVERFAULT, err);
00696         }
00697 
00698       /* what parameter is it ? */
00699 
00700       if(!STRCMP(key_name, "my_parameter_name1"))
00701         {
00702           /* >> interpret the parameter string and fill the fs_specific_info structure << */
00703         }
00704       else if(!STRCMP(key_name, "my_parameter_name2"))
00705         {
00706           /* >> interpret the parameter string and fill the fs_specific_info structure << */
00707         }
00708       /* etc... */
00709       else
00710         {
00711           LogCrit(COMPONENT_CONFIG,
00712                   "FSAL LOAD PARAMETER: ERROR: Unknown or unsettable key: %s (item %s)",
00713                   key_name, CONF_LABEL_FS_SPECIFIC);
00714           ReturnCode(ERR_FSAL_INVAL, 0);
00715         }
00716 
00717     }
00718 
00719   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00720 
00721 }                               /* FSAL_load_FS_specific_parameter_from_conf */