nfs-ganesha 1.4

fsal_context.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  */
00004 
00014 #ifdef HAVE_CONFIG_H
00015 #include "config.h"
00016 #endif
00017 
00018 #include "fsal.h"
00019 #include "fsal_internal.h"
00020 #include "fsal_common.h"
00021 #include <pwd.h>
00022 #include <errno.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00027 enum
00028 {
00029   FILESET_OPTION = 0,
00030   COS_OPTION = 1
00031 };
00032 
00033 const char *fs_specific_opts[] = {
00034   "fileset",
00035   "cos",
00036   NULL
00037 };
00038 
00043 static int Getsubopt(char **optionp, const char *const *tokens, char **valuep)
00044 {
00045   char *endp, *vstart;
00046   int cnt;
00047 
00048   if(**optionp == '\0')
00049     return -1;
00050 
00051   /* Find end of next token.  */
00052   endp = strchr(*optionp, ',');
00053   if(endp == NULL)
00054     endp = strchr(*optionp, '\0');
00055 
00056   /* Find start of value.  */
00057   vstart = memchr(*optionp, '=', endp - *optionp);
00058   if(vstart == NULL)
00059     vstart = endp;
00060 
00061   /* Try to match the characters between *OPTIONP and VSTART against
00062      one of the TOKENS.  */
00063   for(cnt = 0; tokens[cnt] != NULL; ++cnt)
00064     if(memcmp(*optionp, tokens[cnt], vstart - *optionp) == 0
00065        && tokens[cnt][vstart - *optionp] == '\0')
00066       {
00067         /* We found the current option in TOKENS.  */
00068         *valuep = vstart != endp ? vstart + 1 : NULL;
00069 
00070         if(*endp != '\0')
00071           *endp++ = '\0';
00072         *optionp = endp;
00073 
00074         return cnt;
00075       }
00076 
00077   /* The current suboption does not match any option.  */
00078   *valuep = *optionp;
00079 
00080   if(*endp != '\0')
00081     *endp++ = '\0';
00082   *optionp = endp;
00083 
00084   return -1;
00085 }
00086 
00099 fsal_status_t HPSSFSAL_BuildExportContext(hpssfsal_export_context_t * p_export_context, /* OUT */
00100                                           fsal_path_t * p_export_path,  /* IN */
00101                                           char *fs_specific_options     /* IN */
00102     )
00103 {
00104   char subopts[256];
00105   char *p_subop;
00106   char *value;
00107 
00108   char *filesetname = NULL;
00109   int read_cos = 0;             /* 0 => not set */
00110 
00111   int rc;
00112 
00113   /* sanity check */
00114   if(!p_export_context)
00115     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_BuildExportContext);
00116 
00117   if((fs_specific_options != NULL) && (fs_specific_options[0] != '\0'))
00118     {
00119 
00120       /* cleans the export context */
00121       memset(p_export_context, 0, sizeof(hpssfsal_export_context_t));
00122 
00123       /* copy the option string (because it is modified by getsubopt call) */
00124       strncpy(subopts, fs_specific_options, 256);
00125       p_subop = subopts;        /* set initial pointer */
00126 
00127       /* parse the FS specific option string */
00128 
00129       switch (Getsubopt(&p_subop, fs_specific_opts, &value))
00130         {
00131         case FILESET_OPTION:
00132           filesetname = value;
00133           break;
00134 
00135         case COS_OPTION:
00136           read_cos = atoi(value);
00137           if(read_cos <= 0)
00138             {
00139               LogCrit(COMPONENT_FSAL,
00140                   "FSAL LOAD PARAMETER: ERROR: Unexpected value for EXPORT::FS_Specific::%s : ( %s ) positive integer expected.",
00141                    fs_specific_opts[COS_OPTION], value);
00142               Return(ERR_FSAL_INVAL, 0, INDEX_FSAL_BuildExportContext);
00143             }
00144           break;
00145 
00146         default:
00147           {
00148             LogCrit(COMPONENT_FSAL,
00149                 "FSAL LOAD PARAMETER: ERROR: Invalid suboption found in EXPORT::FS_Specific : %s : %s or %s expected.",
00150                  value, fs_specific_opts[FILESET_OPTION], fs_specific_opts[COS_OPTION]);
00151             Return(ERR_FSAL_INVAL, 0, INDEX_FSAL_BuildExportContext);
00152           }
00153         }
00154 
00155     }
00156 
00157   /* fills the export context structure */
00158 
00159   /* Save pointer to fsal_staticfsinfo_t in export context */
00160   p_export_context->fe_static_fs_info = &global_fs_info;
00161 
00162   p_export_context->default_cos = read_cos;
00163 
00164   rc = HPSSFSAL_GetFilesetRoot(filesetname, &p_export_context->fileset_root_handle);
00165 
00166   if(rc != 0)
00167     {
00168       LogCrit(COMPONENT_FSAL,
00169           "FSAL LOAD PARAMETER: ERROR: Could not get root handle for fileset \"%s\"",
00170            (filesetname == NULL ? filesetname : "<root>"));
00171       Return(ERR_FSAL_INVAL, -rc, INDEX_FSAL_BuildExportContext);
00172     }
00173 
00174   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_BuildExportContext);
00175 
00176 }
00177 
00186 fsal_status_t HPSSFSAL_CleanUpExportContext(hpssfsal_export_context_t * p_export_context) 
00187 {
00188   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_CleanUpExportContext);
00189 }
00190 
00191 fsal_status_t HPSSFSAL_InitClientContext(hpssfsal_op_context_t * p_thr_context)
00192 {
00193 
00194   int rc, i;
00195 
00196   /* sanity check */
00197   if(!p_thr_context)
00198     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_InitClientContext);
00199 
00200   /* initialy set the export entry to none */
00201   p_thr_context->export_context = NULL;
00202 
00203   /* LoadThreadState */
00204 
00205   /* We set umask to 0.
00206    * The specified umask is applied later (before calling HPSS API functions)
00207    */
00208   if(rc = hpss_LoadThreadState(0, 0, NULL))
00209     {
00210       Return(ERR_FSAL_PERM, -rc, INDEX_FSAL_GetClientContext);
00211     }
00212 
00213   /* get associated user p_cred */
00214   if(rc = hpss_GetThreadUcred(&(p_thr_context->credential.hpss_usercred)))
00215     {
00216       Return(ERR_FSAL_PERM, -rc, INDEX_FSAL_InitClientContext);
00217     }
00218 
00219   /* sets the credential time */
00220   p_thr_context->credential.last_update = time(NULL);
00221 
00222 #if (HPSS_MAJOR_VERSION == 5)
00223 
00224   /* traces: prints p_credential structure */
00225   if (isFullDebug(COMPONENT_FSAL))
00226     {
00227       LogFullDebug(COMPONENT_FSAL, "credential created:");
00228       LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d",
00229                     p_thr_context->credential.hpss_usercred.SecPWent.Uid,
00230                     p_thr_context->credential.hpss_usercred.SecPWent.Gid);
00231       LogFullDebug(COMPONENT_FSAL, "\tName = %s",
00232                     p_thr_context->credential.hpss_usercred.SecPWent.Name);
00233 
00234       for(i = 0; i < p_thr_context->credential.hpss_usercred.NumGroups; i++)
00235         LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d",
00236                       p_thr_context->credential.hpss_usercred.AltGroups[i]);
00237     }
00238 #elif ( HPSS_MAJOR_VERSION == 6 )
00239 
00240   /* traces: prints p_credential structure */
00241   if (isFullDebug(COMPONENT_FSAL))
00242     {
00243       LogFullDebug(COMPONENT_FSAL, "credential created:");
00244       LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d",
00245                     p_thr_context->credential.hpss_usercred.Uid,
00246                     p_thr_context->credential.hpss_usercred.Gid);
00247       LogFullDebug(COMPONENT_FSAL, "\tName = %s",
00248                     p_thr_context->credential.hpss_usercred.Name);
00249 
00250       for(i = 0; i < p_thr_context->credential.hpss_usercred.NumGroups; i++)
00251         LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d",
00252                       p_thr_context->credential.hpss_usercred.AltGroups[i]);
00253     }
00254 #elif ( HPSS_MAJOR_VERSION == 7 )
00255 
00256   /* traces: prints p_credential structure */
00257   if (isFullDebug(COMPONENT_FSAL))
00258     {
00259       LogFullDebug(COMPONENT_FSAL, "credential created:");
00260       LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d",
00261                     p_thr_context->credential.hpss_usercred.Uid,
00262                     p_thr_context->credential.hpss_usercred.Gid);
00263       LogFullDebug(COMPONENT_FSAL, "\tName = %s",
00264                     p_thr_context->credential.hpss_usercred.Name);
00265 
00266       for(i = 0; i < p_thr_context->credential.hpss_usercred.NumGroups; i++)
00267         LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d",
00268                       p_thr_context->credential.hpss_usercred.AltGroups[i]);
00269     }
00270 #endif
00271 
00272   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_InitClientContext);
00273 
00274 }
00275 
00299 fsal_status_t HPSSFSAL_GetClientContext(hpssfsal_op_context_t * p_thr_context,  /* IN/OUT  */
00300                                         hpssfsal_export_context_t * p_export_context,   /* IN */
00301                                         fsal_uid_t uid, /* IN */
00302                                         fsal_gid_t gid, /* IN */
00303                                         fsal_gid_t * alt_groups,        /* IN */
00304                                         fsal_count_t nb_alt_groups      /* IN */
00305     )
00306 {
00307 
00308   fsal_count_t ng = nb_alt_groups;
00309   unsigned int i;
00310   fsal_status_t st;
00311 
00312   /* sanity check */
00313   if(!p_thr_context || !p_export_context)
00314     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetClientContext);
00315 
00316   /* if the credential is too old, renew it */
00317   if(time(NULL) - p_thr_context->credential.last_update > (int)CredentialLifetime)
00318     {
00319       st = HPSSFSAL_InitClientContext(p_thr_context);
00320       if(FSAL_IS_ERROR(st))
00321         return st;
00322     }
00323 
00324   /* set the export specific context */
00325   p_thr_context->export_context = p_export_context;
00326 
00327   /* Extracted from  /opt/hpss/src/nfs/nfsd/nfs_Dispatch.c */
00328 
00329 #if HPSS_MAJOR_VERSION == 5
00330   strcpy(p_thr_context->credential.hpss_usercred.SecPWent.Name, "NFS.User");
00331   p_thr_context->credential.hpss_usercred.SecLabel = 0; /* Symbol? */
00332   p_thr_context->credential.hpss_usercred.CurAccount = ACCT_REC_DEFAULT;
00333   p_thr_context->credential.hpss_usercred.DefAccount = ACCT_REC_DEFAULT;
00334   p_thr_context->credential.hpss_usercred.SecPWent.Uid = uid;
00335   p_thr_context->credential.hpss_usercred.SecPWent.Gid = gid;
00336 #elif  (HPSS_MAJOR_VERSION == 6) || (HPSS_MAJOR_VERSION == 7)
00337   strcpy(p_thr_context->credential.hpss_usercred.Name, "NFS.User");
00338   p_thr_context->credential.hpss_usercred.CurAccount = ACCT_REC_DEFAULT;
00339   p_thr_context->credential.hpss_usercred.DefAccount = ACCT_REC_DEFAULT;
00340   p_thr_context->credential.hpss_usercred.Uid = uid;
00341   p_thr_context->credential.hpss_usercred.Gid = gid;
00342 #endif
00343 
00344   if(ng > HPSS_NGROUPS_MAX)
00345     ng = HPSS_NGROUPS_MAX;
00346 
00347   if((ng > 0) && (alt_groups == NULL))
00348     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetClientContext);
00349 
00350   p_thr_context->credential.hpss_usercred.NumGroups = ng;
00351 
00352   for(i = 0; i < ng; i++)
00353     p_thr_context->credential.hpss_usercred.AltGroups[i] = alt_groups[i];
00354 
00355 #if (HPSS_MAJOR_VERSION == 5)
00356 
00357   /* traces: prints p_credential structure */
00358   if (isFullDebug(COMPONENT_FSAL))
00359     {
00360       LogFullDebug(COMPONENT_FSAL, "credential modified:");
00361       LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d",
00362                     p_thr_context->credential.hpss_usercred.SecPWent.Uid,
00363                     p_thr_context->credential.hpss_usercred.SecPWent.Gid);
00364       LogFullDebug(COMPONENT_FSAL, "\tName = %s",
00365                     p_thr_context->credential.hpss_usercred.SecPWent.Name);
00366 
00367       for(i = 0; i < p_thr_context->credential.hpss_usercred.NumGroups; i++)
00368         LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d",
00369                       p_thr_context->credential.hpss_usercred.AltGroups[i]);
00370     }
00371 #elif ( HPSS_MAJOR_VERSION == 6 )
00372 
00373   /* traces: prints p_credential structure */
00374   if (isFullDebug(COMPONENT_FSAL))
00375     {
00376       LogFullDebug(COMPONENT_FSAL, "credential modified:");
00377       LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d",
00378                     p_thr_context->credential.hpss_usercred.Uid,
00379                     p_thr_context->credential.hpss_usercred.Gid);
00380       LogFullDebug(COMPONENT_FSAL, "\tName = %s",
00381                     p_thr_context->credential.hpss_usercred.Name);
00382 
00383       for(i = 0; i < p_thr_context->credential.hpss_usercred.NumGroups; i++)
00384         LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d",
00385                       p_thr_context->credential.hpss_usercred.AltGroups[i]);
00386     }
00387 #elif ( HPSS_MAJOR_VERSION == 7 )
00388 
00389   /* traces: prints p_credential structure */
00390   if (isFullDebug(COMPONENT_FSAL))
00391     {
00392       LogFullDebug(COMPONENT_FSAL, "credential modified:");
00393       LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d",
00394                     p_thr_context->credential.hpss_usercred.Uid,
00395                     p_thr_context->credential.hpss_usercred.Gid);
00396       LogFullDebug(COMPONENT_FSAL, "\tName = %s",
00397                     p_thr_context->credential.hpss_usercred.Name);
00398 
00399       for(i = 0; i < p_thr_context->credential.hpss_usercred.NumGroups; i++)
00400         LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d",
00401                       p_thr_context->credential.hpss_usercred.AltGroups[i]);
00402     }
00403 #endif
00404 
00405   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_GetClientContext);
00406 
00407 }
00408 
00409 /* @} */