nfs-ganesha 1.4

9p_proto_tools.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2011)
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 
00034 #ifdef HAVE_CONFIG_H
00035 #include "config.h"
00036 #endif
00037 
00038 #ifdef _SOLARIS
00039 #include "solaris_port.h"
00040 #endif
00041 
00042 #include <stdio.h>
00043 #include <string.h>
00044 #include <pthread.h>
00045 #include <sys/types.h>
00046 #include <pwd.h>
00047 #include <sys/stat.h>
00048 #include <fcntl.h>
00049 #include "nfs_core.h"
00050 #include "log.h"
00051 #include "9p.h"
00052 
00053 int _9p_init(  _9p_parameter_t * pparam ) 
00054 {
00055   return 0 ;
00056 } /* _9p_init */
00057 
00058 int _9p_tools_get_fsal_op_context_by_uid( u32 uid, _9p_fid_t * pfid ) 
00059 {
00060   char buff[1024];
00061   struct passwd p;
00062   struct passwd *pp;
00063   gid_t gid ;
00064   fsal_status_t fsal_status ;
00065 
00066   if((getpwuid_r( uid, &p, buff, MAXPATHLEN, &pp) != 0) || (pp == NULL))
00067     {
00068       LogFullDebug(COMPONENT_IDMAPPER, "getpwuid_r %d failed", uid ) ;
00069       return -ENOENT;
00070     }
00071   else
00072    gid = p.pw_gid ;
00073   
00074 
00075   fsal_status = FSAL_GetClientContext( &pfid->fsal_op_context,
00076                                        &pfid->pexport->FS_export_context,
00077                                        uid, gid, NULL, 0 ) ;
00078   if( FSAL_IS_ERROR( fsal_status ) )
00079    return -fsal_status.major ; 
00080 
00081   return 0 ;
00082 } /* _9p_tools_get_fsal_cred */
00083 
00084 int _9p_tools_get_fsal_op_context_by_name( int uname_len, char * uname_str, _9p_fid_t * pfid ) 
00085 {
00086   char name[1024] ;
00087   uid_t uid ;
00088 
00089   strncpy( name, uname_str, uname_len) ;
00090 
00091   if(name2uid(name, &uid) )
00092     {
00093       LogFullDebug(COMPONENT_IDMAPPER, "uidmap_get mapped %s to uid= %d",
00094                    name, uid);
00095     }
00096   else
00097     return -ENOENT ;
00098   
00099   return _9p_tools_get_fsal_op_context_by_uid( uid, pfid ) ; 
00100 } /* _9p_tools_get_fsal_cred */
00101 
00102 
00103 int _9p_tools_errno( cache_inode_status_t cache_status )
00104 {
00105   int rc = 0 ;
00106 
00107   switch( cache_status )
00108    {
00109      case CACHE_INODE_SUCCESS:
00110         rc = 0 ;
00111         break ;
00112 
00113      case CACHE_INODE_MALLOC_ERROR:
00114         rc = ENOMEM ;
00115         break ;
00116 
00117      case CACHE_INODE_NOT_A_DIRECTORY:
00118         rc = ENOTDIR ;
00119         break ;
00120 
00121      case CACHE_INODE_ENTRY_EXISTS:
00122         rc = EEXIST ;
00123         break ;
00124 
00125      case CACHE_INODE_DIR_NOT_EMPTY:
00126         rc = ENOTEMPTY ;
00127         break ;
00128 
00129      case CACHE_INODE_NOT_FOUND:
00130         rc = ENOENT ;
00131         break ;
00132 
00133      case CACHE_INODE_IS_A_DIRECTORY:
00134         rc = EISDIR ;
00135         break ;
00136 
00137      case CACHE_INODE_FSAL_EPERM:
00138      case CACHE_INODE_FSAL_ERR_SEC:
00139         rc = EPERM ;
00140         break ;
00141 
00142      case CACHE_INODE_INVALID_ARGUMENT:
00143      case CACHE_INODE_NAME_TOO_LONG:
00144      case CACHE_INODE_UNAPPROPRIATED_KEY:
00145      case CACHE_INODE_INCONSISTENT_ENTRY:
00146      case CACHE_INODE_FSAL_ERROR:
00147      case CACHE_INODE_BAD_TYPE: 
00148      case CACHE_INODE_STATE_CONFLICT:
00149      case CACHE_INODE_STATE_ERROR:
00150      case CACHE_INODE_POOL_MUTEX_INIT_ERROR:
00151      case CACHE_INODE_INIT_ENTRY_FAILED:
00152         rc = EINVAL ;
00153         break ;
00154 
00155      case CACHE_INODE_NO_SPACE_LEFT:
00156         rc = ENOSPC ;
00157         break ;
00158 
00159      case CACHE_INODE_READ_ONLY_FS: 
00160         rc = EROFS ;
00161         break ;
00162 
00163      case CACHE_INODE_FSAL_ESTALE:
00164      case CACHE_INODE_DEAD_ENTRY:
00165         rc = ESTALE ;
00166         break ;
00167 
00168      case CACHE_INODE_QUOTA_EXCEEDED:
00169         rc = EDQUOT ;
00170         break ; 
00171 
00172      case CACHE_INODE_CACHE_CONTENT_ERROR:
00173      case CACHE_INODE_CACHE_CONTENT_EXISTS:
00174      case CACHE_INODE_CACHE_CONTENT_EMPTY:
00175      case CACHE_INODE_IO_ERROR:
00176      case CACHE_INODE_ASYNC_POST_ERROR:
00177      case CACHE_INODE_GET_NEW_LRU_ENTRY:
00178      case CACHE_INODE_LRU_ERROR:
00179      case CACHE_INODE_HASH_SET_ERROR:
00180      case CACHE_INODE_INSERT_ERROR:
00181      case CACHE_INODE_HASH_TABLE_ERROR:
00182         rc = EIO ;
00183         break ;
00184 
00185      case CACHE_INODE_NOT_SUPPORTED:
00186         rc = ENOTSUP ;
00187         break ;
00188 
00189      case CACHE_INODE_FSAL_EACCESS:
00190         rc = EACCES ;
00191         break ;
00192 
00193      case CACHE_INODE_DELAY:
00194         rc = EAGAIN ;
00195         break ;
00196 
00197      default:
00198         rc = EIO ;
00199         break ; 
00200    }
00201 
00202   return rc ;
00203 } /* _9p_tools_errno */
00204 
00205 void _9p_tools_fsal_attr2stat( fsal_attrib_list_t * pfsalattr, struct stat * pstat )
00206 {
00207   /* zero output structure */
00208   memset( (char *)pstat, 0, sizeof( struct stat ) ) ;
00209 
00210   pstat->st_dev   = 0 ; /* Omitted in 9P */
00211   pstat->st_ino   = pfsalattr->fileid ;
00212 
00213   pstat->st_mode  = pfsalattr->mode ;
00214   if( pfsalattr->type == FSAL_TYPE_DIR ) pstat->st_mode  |= __S_IFDIR  ;
00215   if( pfsalattr->type == FSAL_TYPE_FILE ) pstat->st_mode |= __S_IFREG  ;
00216   if( pfsalattr->type == FSAL_TYPE_LNK ) pstat->st_mode  |= __S_IFLNK  ;
00217   if( pfsalattr->type == FSAL_TYPE_SOCK ) pstat->st_mode |= __S_IFSOCK ;
00218   if( pfsalattr->type == FSAL_TYPE_BLK ) pstat->st_mode  |= __S_IFBLK  ;
00219   if( pfsalattr->type == FSAL_TYPE_CHR ) pstat->st_mode  |= __S_IFCHR  ;
00220   if( pfsalattr->type == FSAL_TYPE_FIFO ) pstat->st_mode |= __S_IFIFO  ;
00221 
00222   pstat->st_nlink   = (u32)pfsalattr->numlinks ;
00223   pstat->st_uid     = pfsalattr->owner ;
00224   pstat->st_gid     = pfsalattr->group ;
00225   pstat->st_rdev    = pfsalattr->rawdev.major ;
00226   pstat->st_size    = pfsalattr->filesize ;
00227   pstat->st_blksize = 4096 ;
00228   pstat->st_blocks  = (pfsalattr->filesize/4096) + 1 ; 
00229   pstat->st_atime   = pfsalattr->atime.seconds ;
00230   pstat->st_mtime   = pfsalattr->mtime.seconds ;
00231   pstat->st_ctime   = pfsalattr->ctime.seconds ;
00232 
00233 } /* _9p_tools_fsal_attr2stat */
00234 
00235 void _9p_tools_acess2fsal( u32 * paccessin, fsal_accessflags_t * pfsalaccess )
00236 {
00237   memset( (char *)pfsalaccess, 0 , sizeof( fsal_accessflags_t ) ) ;
00238 
00239   if( *paccessin & O_WRONLY ) *pfsalaccess |= FSAL_W_OK ;
00240   if( *paccessin & O_RDONLY ) *pfsalaccess |= FSAL_R_OK ;
00241   if( *paccessin & O_RDWR )   *pfsalaccess |= FSAL_R_OK|FSAL_W_OK ; 
00242 } /* _9p_tools_acess2fsal */
00243 
00244 void _9p_chomp_attr_value(char *str, size_t size)
00245 {
00246   int len;
00247 
00248   if(str == NULL)
00249     return;
00250 
00251   /* security: set last char to '\0' */
00252   str[size - 1] = '\0';
00253 
00254   len = strnlen(str, size);
00255   if((len > 0) && (str[len - 1] == '\n'))
00256     str[len - 1] = '\0';
00257 }
00258 
00259 void _9p_openflags2FSAL( u32 * inflags, fsal_openflags_t * outflags )
00260 {
00261   if( inflags == NULL || outflags == NULL )
00262     return ; 
00263 
00264   if( *inflags & O_WRONLY ) *outflags |= FSAL_O_WRONLY  ;
00265   if( *inflags & O_RDWR ) *outflags |= FSAL_O_RDWR  ;
00266   /* Exception : O_RDONLY has value 0, it can't be tested with a logical and */
00267   /* We consider that a non( has O_WRONLY or has O_RDWR ) case is RD_ONLY */
00268   if( !(*inflags & (O_WRONLY|O_RDWR)) )
00269      *outflags = FSAL_O_RDONLY ;
00270 
00271   return ;
00272 } /* _9p_openflags2FSAL */
00273