nfs-ganesha 1.4
|
00001 00010 #ifdef HAVE_CONFIG_H 00011 #include "config.h" 00012 #endif 00013 00014 #include "fsal.h" 00015 #include "fsal_internal.h" 00016 #include "fsal_convert.h" 00017 #include <pwd.h> 00018 #include <errno.h> 00019 #include <stdlib.h> 00020 #include <string.h> 00021 #include <unistd.h> 00022 #include <time.h> 00023 #include <stdio.h> 00024 #include <pthread.h> 00025 #include <mntent.h> /* for handling mntent */ 00026 #include <libgen.h> /* for dirname */ 00027 00039 fsal_status_t VFSFSAL_BuildExportContext(fsal_export_context_t * context, /* OUT */ 00040 fsal_path_t * p_export_path, /* IN */ 00041 char *fs_specific_options /* IN */ 00042 ) 00043 { 00044 /* Get the mount point for this lustre FS, 00045 * so it can be used for building .lustre/fid paths. 00046 */ 00047 00048 vfsfsal_export_context_t * p_export_context = (vfsfsal_export_context_t *) context; 00049 FILE *fp; 00050 struct mntent *p_mnt; 00051 00052 char rpath[MAXPATHLEN]; 00053 char mntdir[MAXPATHLEN]; 00054 char fs_spec[MAXPATHLEN]; 00055 00056 char *first_vfs_dir = NULL; 00057 char type[MAXNAMLEN]; 00058 00059 size_t pathlen, outlen; 00060 int rc; 00061 int mnt_id = 0 ; 00062 00063 /* sanity check */ 00064 if(p_export_context == NULL) 00065 { 00066 LogCrit(COMPONENT_FSAL, "NULL mandatory argument passed to %s()", __FUNCTION__); 00067 Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_BuildExportContext); 00068 } 00069 00070 outlen = 0; 00071 00072 if(p_export_path != NULL) 00073 strncpy(rpath, p_export_path->path, MAXPATHLEN); 00074 00075 /* open mnt file */ 00076 fp = setmntent(MOUNTED, "r"); 00077 00078 if(fp == NULL) 00079 { 00080 rc = errno; 00081 LogCrit(COMPONENT_FSAL, "Error %d in setmntent(%s): %s", rc, MOUNTED, 00082 strerror(rc)); 00083 Return(posix2fsal_error(rc), rc, INDEX_FSAL_BuildExportContext); 00084 } 00085 00086 while((p_mnt = getmntent(fp)) != NULL) 00087 { 00088 /* get the longer path vfs related export that matches export path */ 00089 00090 if(p_mnt->mnt_dir != NULL) 00091 { 00092 00093 pathlen = strlen(p_mnt->mnt_dir); 00094 00095 if(first_vfs_dir == NULL) 00096 first_vfs_dir = p_mnt->mnt_dir; 00097 00098 if((pathlen > outlen) && !strcmp(p_mnt->mnt_dir, "/")) 00099 { 00100 LogDebug(COMPONENT_FSAL, 00101 "Root mountpoint is allowed for matching %s, type=%s, fs=%s", 00102 rpath, p_mnt->mnt_type, p_mnt->mnt_fsname); 00103 outlen = pathlen; 00104 strncpy(mntdir, p_mnt->mnt_dir, MAXPATHLEN); 00105 strncpy(type, p_mnt->mnt_type, MAXNAMLEN); 00106 strncpy(fs_spec, p_mnt->mnt_fsname, MAXPATHLEN); 00107 } 00108 /* in other cases, the filesystem must be <mountpoint>/<smthg> or <mountpoint>\0 */ 00109 else if((pathlen > outlen) && 00110 !strncmp(rpath, p_mnt->mnt_dir, pathlen) && 00111 ((rpath[pathlen] == '/') || (rpath[pathlen] == '\0'))) 00112 { 00113 LogFullDebug(COMPONENT_FSAL, "%s is under mountpoint %s, type=%s, fs=%s", 00114 rpath, p_mnt->mnt_dir, p_mnt->mnt_type, p_mnt->mnt_fsname); 00115 00116 outlen = pathlen; 00117 strncpy(mntdir, p_mnt->mnt_dir, MAXPATHLEN); 00118 strncpy(type, p_mnt->mnt_type, MAXNAMLEN); 00119 strncpy(fs_spec, p_mnt->mnt_fsname, MAXPATHLEN); 00120 } 00121 } 00122 } 00123 00124 if(outlen <= 0) 00125 { 00126 if(p_export_path == NULL) 00127 strncpy(mntdir, first_vfs_dir, MAXPATHLEN); 00128 else 00129 { 00130 LogCrit(COMPONENT_FSAL, "No mount entry matches '%s' in %s", rpath, MOUNTED); 00131 endmntent(fp); 00132 Return(ERR_FSAL_NOENT, 0, INDEX_FSAL_BuildExportContext); 00133 } 00134 } 00135 00136 /* Save pointer to fsal_staticfsinfo_t in export context */ 00137 p_export_context->fe_static_fs_info = &global_fs_info; 00138 00139 /* save file descriptor to root of VFS export */ 00140 if( ( p_export_context->mount_root_fd = open(mntdir, O_RDONLY | O_DIRECTORY) ) < 0 ) 00141 { 00142 LogMajor(COMPONENT_FSAL, 00143 "FSAL BUILD EXPORT CONTEXT: ERROR: Could not open VFS mount point %s: rc = %d", 00144 mntdir, errno); 00145 ReturnCode(ERR_FSAL_INVAL, 0); 00146 } 00147 00148 /* Keep fstype in export_context */ 00149 strncpy( p_export_context->fstype, type, MAXNAMLEN ) ; 00150 00151 if( !strncmp( type, "xfs", MAXNAMLEN ) ) 00152 { 00153 LogMajor( COMPONENT_FSAL, 00154 "Trying to export XFS filesystem via FSAL_VFS for mount point %s. Use FSAL_XFS instead", mntdir ) ; 00155 ReturnCode(ERR_FSAL_INVAL, 0); 00156 } 00157 00158 p_export_context->root_handle.handle_bytes = VFS_HANDLE_LEN ; 00159 if( vfs_fd_to_handle( p_export_context->mount_root_fd, 00160 &p_export_context->root_handle, 00161 &mnt_id ) ) { 00162 LogMajor(COMPONENT_FSAL, 00163 "vfs_fd_to_handle: root_path: %s, root_fd=%d, errno=(%d) %s", 00164 mntdir, p_export_context->mount_root_fd, 00165 errno, strerror(errno)); 00166 Return(posix2fsal_error(errno), errno, INDEX_FSAL_BuildExportContext) ; 00167 } 00168 00169 #ifdef TODO 00170 if(isFullDebug(COMPONENT_FSAL)) 00171 { 00172 char str[1024] ; 00173 00174 sprint_mem( str, p_export_context->root_handle.handle ,p_export_context->root_handle.handle_bytes ) ; 00175 LogFullDebug(COMPONENT_FSAL, 00176 "=====> root Handle: type=%u bytes=%u|%s\n", 00177 p_export_context->root_handle.handle_type, p_export_context->root_handle.handle_bytes, str ) ; 00178 00179 } 00180 #endif 00181 00182 Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_BuildExportContext); 00183 } 00184 00185 fsal_status_t VFSFSAL_InitClientContext(fsal_op_context_t * p_context) 00186 { 00187 vfsfsal_op_context_t * p_thr_context = (vfsfsal_op_context_t *)p_context; 00188 /* sanity check */ 00189 if(!p_thr_context) 00190 Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_InitClientContext); 00191 00192 /* initialy set the export entry to none */ 00193 p_thr_context->export_context = NULL; 00194 00195 Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_InitClientContext); 00196 00197 } 00198 00222 fsal_status_t VFSFSAL_GetClientContext(fsal_op_context_t * thr_context, /* IN/OUT */ 00223 fsal_export_context_t * p_export_context, /* IN */ 00224 fsal_uid_t uid, /* IN */ 00225 fsal_gid_t gid, /* IN */ 00226 fsal_gid_t * alt_groups, /* IN */ 00227 fsal_count_t nb_alt_groups /* IN */ 00228 ) 00229 { 00230 vfsfsal_op_context_t * p_thr_context = (vfsfsal_op_context_t *) thr_context; 00231 fsal_count_t ng = nb_alt_groups; 00232 unsigned int i; 00233 00234 /* sanity check */ 00235 if(!p_thr_context || !p_export_context) 00236 Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetClientContext); 00237 00238 /* set the export specific context */ 00239 p_thr_context->export_context = (vfsfsal_export_context_t *) p_export_context; 00240 00241 p_thr_context->credential.user = uid; 00242 p_thr_context->credential.group = gid; 00243 00244 if(ng > FSAL_NGROUPS_MAX) 00245 ng = FSAL_NGROUPS_MAX; 00246 if((ng > 0) && (alt_groups == NULL)) 00247 Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetClientContext); 00248 00249 p_thr_context->credential.nbgroups = ng; 00250 00251 for(i = 0; i < ng; i++) 00252 p_thr_context->credential.alt_groups[i] = alt_groups[i]; 00253 00254 if(isFullDebug(COMPONENT_FSAL)) 00255 { 00256 /* traces: prints p_credential structure */ 00257 00258 LogFullDebug(COMPONENT_FSAL, "credential modified:"); 00259 LogFullDebug(COMPONENT_FSAL, "\tuid = %d, gid = %d", 00260 p_thr_context->credential.user, p_thr_context->credential.group); 00261 00262 if (isFullDebug(COMPONENT_FSAL)) 00263 { 00264 for(i = 0; i < p_thr_context->credential.nbgroups; i++) 00265 LogFullDebug(COMPONENT_FSAL, "\tAlt grp: %d", 00266 p_thr_context->credential.alt_groups[i]); 00267 } 00268 } 00269 00270 Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_GetClientContext); 00271 } 00272 00273 /* @} */