nfs-ganesha 1.4

fsal_convert.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 #include "fsal_convert.h"
00018 #include "fsal_internal.h"
00019 #include <sys/types.h>
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 
00023 #define MAX_2( x, y )    ( (x) > (y) ? (x) : (y) )
00024 
00025 /* some ideas of conversion functions...
00026 
00027 int fs2fsal_error(int fs_errorcode);
00028 
00029 int fsal2fs_openflags( fsal_openflags_t fsal_flags, int * p_fs_flags );
00030 
00031 int fsal2fs_testperm(fsal_accessflags_t testperm);
00032 
00033 fsal_status_t fs2fsal_attributes(  <your fs attribute structure (input)>,
00034                                    fsal_attrib_list_t * p_fsalattr_out );
00035 
00036 fsal_accessmode_t fs2fsal_mode( <your fs object permission (input)> );
00037 
00038 void fsal2fs_mode( fsal_accessmode_t fsal_mode, <your fs mode type (output)> );
00039 
00040 fsal_nodetype_t  hpss2fsal_type( <your fs object type (input)> );
00041 
00042 fsal_u64_t fs2fsal_64( <your fs 64bits type> );
00043 
00044 <your fs 64bits type> fsal2hpss_64( fsal_u64_t fsal_size_in );
00045 
00046 fsal_fsid_t fs2fsal_fsid( <you fs fsid type> );
00047 
00048 fsal_status_t fsal2fs_attribset( zfsfsal_handle_t  * p_fsal_handle,
00049                                  fsal_attrib_list_t  * p_attrib_set ,
00050                                 <depends on your fs way of setting attributes> );
00051 
00052 fsal_time_t fs2fsal_time( <your fs time structure> );
00053 
00054 <your fs time structure> fsal2fs_time(fsal_time_t in_time);
00055      
00056 */
00057 
00058 /* THOSE FUNCTIONS CAN BE USED FROM OUTSIDE THE MODULE : */
00059 
00073 int fsal2posix_openflags(fsal_openflags_t fsal_flags, int *p_posix_flags)
00074 {
00075   int cpt;
00076 
00077   if(!p_posix_flags)
00078     return ERR_FSAL_FAULT;
00079 
00080   /* check that all used flags exist */
00081 
00082   if(fsal_flags &
00083      ~(FSAL_O_RDONLY | FSAL_O_RDWR | FSAL_O_WRONLY | FSAL_O_APPEND | FSAL_O_TRUNC))
00084     return ERR_FSAL_INVAL;
00085 
00086   /* Check for flags compatibility */
00087 
00088   /* O_RDONLY O_WRONLY O_RDWR cannot be used together */
00089 
00090   cpt = 0;
00091   if(fsal_flags & FSAL_O_RDONLY)
00092     cpt++;
00093   if(fsal_flags & FSAL_O_RDWR)
00094     cpt++;
00095   if(fsal_flags & FSAL_O_WRONLY)
00096     cpt++;
00097 
00098   if(cpt > 1)
00099     return ERR_FSAL_INVAL;
00100 
00101   /* FSAL_O_APPEND et FSAL_O_TRUNC cannot be used together */
00102 
00103   if((fsal_flags & FSAL_O_APPEND) && (fsal_flags & FSAL_O_TRUNC))
00104     return ERR_FSAL_INVAL;
00105 
00106   /* FSAL_O_TRUNC without FSAL_O_WRONLY or FSAL_O_RDWR */
00107 
00108   if((fsal_flags & FSAL_O_TRUNC) && !(fsal_flags & (FSAL_O_WRONLY | FSAL_O_RDWR)))
00109     return ERR_FSAL_INVAL;
00110 
00111   /* conversion */
00112   *p_posix_flags = 0;
00113 
00114   if(fsal_flags & FSAL_O_RDONLY)
00115     *p_posix_flags |= O_RDONLY;
00116   if(fsal_flags & FSAL_O_WRONLY)
00117     *p_posix_flags |= O_WRONLY;
00118   if(fsal_flags & FSAL_O_RDWR)
00119     *p_posix_flags |= O_RDWR;
00120 
00121   return ERR_FSAL_NO_ERROR;
00122 
00123 }
00124 
00125 int posix2fsal_error(int posix_errorcode)
00126 {
00127 
00128   switch (posix_errorcode)
00129     {
00130 
00131     case EPERM:
00132       return ERR_FSAL_PERM;
00133 
00134     case ENOENT:
00135       return ERR_FSAL_NOENT;
00136 
00137       /* connection error */
00138 #ifdef _AIX_5
00139     case ENOCONNECT:
00140 #elif defined _LINUX
00141     case ECONNREFUSED:
00142     case ECONNABORTED:
00143     case ECONNRESET:
00144 #endif
00145 
00146       /* IO error */
00147     case EIO:
00148 
00149       /* too many open files */
00150     case ENFILE:
00151     case EMFILE:
00152 
00153       /* broken pipe */
00154     case EPIPE:
00155 
00156       /* all shown as IO errors */
00157       return ERR_FSAL_IO;
00158 
00159       /* no such device */
00160     case ENODEV:
00161     case ENXIO:
00162       return ERR_FSAL_NXIO;
00163 
00164       /* invalid file descriptor : */
00165     case EBADF:
00166       /* we suppose it was not opened... */
00167 
00175       return ERR_FSAL_NOT_OPENED;
00176 
00177     case ENOMEM:
00178       return ERR_FSAL_NOMEM;
00179 
00180     case EACCES:
00181       return ERR_FSAL_ACCESS;
00182 
00183     case EFAULT:
00184       return ERR_FSAL_FAULT;
00185 
00186     case EEXIST:
00187       return ERR_FSAL_EXIST;
00188 
00189     case EXDEV:
00190       return ERR_FSAL_XDEV;
00191 
00192     case ENOTDIR:
00193       return ERR_FSAL_NOTDIR;
00194 
00195     case EISDIR:
00196       return ERR_FSAL_ISDIR;
00197 
00198     case EINVAL:
00199       return ERR_FSAL_INVAL;
00200 
00201     case EFBIG:
00202       return ERR_FSAL_FBIG;
00203 
00204     case ENOSPC:
00205       return ERR_FSAL_NOSPC;
00206 
00207     case EMLINK:
00208       return ERR_FSAL_MLINK;
00209 
00210     case EDQUOT:
00211       return ERR_FSAL_DQUOT;
00212 
00213     case ESRCH:                /* Returned by quotaclt */
00214       return ERR_FSAL_NO_QUOTA;
00215 
00216     case ENAMETOOLONG:
00217       return ERR_FSAL_NAMETOOLONG;
00218 
00225 #ifdef _AIX
00226     case 87:
00227 #else
00228     case ENOTEMPTY:
00229     case -ENOTEMPTY:
00230 #endif
00231       return ERR_FSAL_NOTEMPTY;
00232 
00233     case ESTALE:
00234       return ERR_FSAL_STALE;
00235 
00236       /* Error code that needs a retry */
00237     case EAGAIN:
00238     case EBUSY:
00239 
00240       return ERR_FSAL_DELAY;
00241 
00242     case ENOTSUP:
00243       return ERR_FSAL_NOTSUPP;
00244 
00245     default:
00246 
00247       /* other unexpected errors */
00248       return ERR_FSAL_SERVERFAULT;
00249 
00250     }
00251 
00252 }
00253 
00254 fsal_status_t posix2fsal_attributes(struct stat *p_buffstat,
00255                                     fsal_attrib_list_t * p_fsalattr_out)
00256 {
00257 
00258   fsal_attrib_mask_t supp_attr, unsupp_attr;
00259 
00260   /* sanity checks */
00261   if(!p_buffstat || !p_fsalattr_out)
00262     ReturnCode(ERR_FSAL_FAULT, 0);
00263 
00264   /* check that asked attributes are supported */
00265   supp_attr = global_fs_info.supported_attrs;
00266 
00267   unsupp_attr = (p_fsalattr_out->asked_attributes) & (~supp_attr);
00268   if(unsupp_attr)
00269     {
00270       LogFullDebug(COMPONENT_FSAL, "Unsupported attributes: %#llX",
00271                         unsupp_attr);
00272       ReturnCode(ERR_FSAL_ATTRNOTSUPP, 0);
00273     }
00274 
00275   /* Initialize ACL regardless of whether ACL was asked or not.
00276    * This is needed to make sure ACL attribute is initialized. */
00277   p_fsalattr_out->acl = NULL;
00278 
00279   /* Fills the output struct */
00280   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_SUPPATTR))
00281     {
00282       p_fsalattr_out->supported_attributes = supp_attr;
00283     }
00284   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_TYPE))
00285     {
00286       p_fsalattr_out->type = posix2fsal_type(p_buffstat->st_mode);
00287     }
00288   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_SIZE))
00289     {
00290       p_fsalattr_out->filesize = p_buffstat->st_size;
00291     }
00292   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_FSID))
00293     {
00294       p_fsalattr_out->fsid = posix2fsal_fsid(p_buffstat->st_dev);
00295     }
00296   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_ACL))
00297     {
00298       p_fsalattr_out->acl = NULL;
00299     }
00300   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_FILEID))
00301     {
00302       p_fsalattr_out->fileid = (fsal_u64_t) (p_buffstat->st_ino);
00303     }
00304 
00305   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_MODE))
00306     {
00307       p_fsalattr_out->mode = unix2fsal_mode(p_buffstat->st_mode);
00308     }
00309   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_NUMLINKS))
00310     {
00311       p_fsalattr_out->numlinks = p_buffstat->st_nlink;
00312     }
00313   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_OWNER))
00314     {
00315       p_fsalattr_out->owner = p_buffstat->st_uid;
00316     }
00317   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_GROUP))
00318     {
00319       p_fsalattr_out->group = p_buffstat->st_gid;
00320     }
00321   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_ATIME))
00322     {
00323       p_fsalattr_out->atime = posix2fsal_time(p_buffstat->st_atime, 0);
00324 
00325     }
00326 
00327   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_CTIME))
00328     {
00329       p_fsalattr_out->ctime = posix2fsal_time(p_buffstat->st_ctime, 0);
00330     }
00331   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_MTIME))
00332     {
00333       p_fsalattr_out->mtime = posix2fsal_time(p_buffstat->st_mtime, 0);
00334     }
00335 
00336   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_CHGTIME))
00337     {
00338       p_fsalattr_out->chgtime
00339           = posix2fsal_time(MAX_2(p_buffstat->st_mtime, p_buffstat->st_ctime), 0);
00340       p_fsalattr_out->change = (uint64_t) p_fsalattr_out->chgtime.seconds ;
00341     }
00342 
00343   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_SPACEUSED))
00344     {
00345       p_fsalattr_out->spaceused = p_buffstat->st_blocks * S_BLKSIZE;
00346     }
00347 
00348   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_RAWDEV))
00349     {
00350       p_fsalattr_out->rawdev = posix2fsal_devt(p_buffstat->st_rdev);    /* XXX: convert ? */
00351     }
00352   /* mounted_on_fileid :
00353      if ( FSAL_TEST_MASK(p_fsalattr_out->asked_attributes,
00354      FSAL_ATTR_MOUNTFILEID )){
00355      p_fsalattr_out->mounted_on_fileid = 
00356      hpss2fsal_64( p_hpss_attr_in->FilesetRootId );
00357      }
00358    */
00359 
00360   /* everything has been copied ! */
00361 
00362   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00363 }