nfs-ganesha 1.4

fsal_convert.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  */
00004 
00015 #ifdef HAVE_CONFIG_H
00016 #include "config.h"
00017 #endif
00018 #include "fsal_convert.h"
00019 #include "fsal_internal.h"
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <errno.h>
00023 #include <string.h>
00024 
00025 #define MAX_2( x, y )    ( (x) > (y) ? (x) : (y) )
00026 #define MAX_3( x, y, z ) ( (x) > (y) ? MAX_2((x),(z)) : MAX_2((y),(z)) )
00027 
00039 int posix2fsal_error(int posix_errorcode)
00040 {
00041 
00042   switch (posix_errorcode)
00043     {
00044 
00045     case EPERM:
00046       return ERR_FSAL_PERM;
00047 
00048     case ENOENT:
00049       return ERR_FSAL_NOENT;
00050 
00051       /* connection error */
00052 #ifdef _AIX_5
00053     case ENOCONNECT:
00054 #elif defined _LINUX
00055     case ECONNREFUSED:
00056     case ECONNABORTED:
00057     case ECONNRESET:
00058 #endif
00059 
00060       /* IO error */
00061     case EIO:
00062 
00063       /* too many open files */
00064     case ENFILE:
00065     case EMFILE:
00066 
00067       /* broken pipe */
00068     case EPIPE:
00069 
00070       /* all shown as IO errors */
00071       return ERR_FSAL_IO;
00072 
00073       /* no such device */
00074     case ENODEV:
00075     case ENXIO:
00076       return ERR_FSAL_NXIO;
00077 
00078       /* invalid file descriptor : */
00079     case EBADF:
00080       /* we suppose it was not opened... */
00081 
00089       return ERR_FSAL_NOT_OPENED;
00090 
00091     case ENOMEM:
00092       return ERR_FSAL_NOMEM;
00093 
00094     case EACCES:
00095       return ERR_FSAL_ACCESS;
00096 
00097     case EFAULT:
00098       return ERR_FSAL_FAULT;
00099 
00100     case EEXIST:
00101       return ERR_FSAL_EXIST;
00102 
00103     case EXDEV:
00104       return ERR_FSAL_XDEV;
00105 
00106     case ENOTDIR:
00107       return ERR_FSAL_NOTDIR;
00108 
00109     case EISDIR:
00110       return ERR_FSAL_ISDIR;
00111 
00112     case EINVAL:
00113       return ERR_FSAL_INVAL;
00114 
00115     case EFBIG:
00116       return ERR_FSAL_FBIG;
00117 
00118     case ENOSPC:
00119       return ERR_FSAL_NOSPC;
00120 
00121     case EMLINK:
00122       return ERR_FSAL_MLINK;
00123 
00124     case EDQUOT:
00125       return ERR_FSAL_DQUOT;
00126 
00127     case ENAMETOOLONG:
00128       return ERR_FSAL_NAMETOOLONG;
00129 
00136 #ifdef _AIX
00137     case 87:
00138 #else
00139     case ENOTEMPTY:
00140     case -ENOTEMPTY:
00141 #endif
00142       return ERR_FSAL_NOTEMPTY;
00143 
00144     case ESTALE:
00145       return ERR_FSAL_STALE;
00146 
00147       /* Error code that needs a retry */
00148     case EAGAIN:
00149     case EBUSY:
00150 
00151       return ERR_FSAL_DELAY;
00152 
00153     default:
00154 
00155       /* other unexpected errors */
00156       return ERR_FSAL_SERVERFAULT;
00157 
00158     }
00159 
00160 }
00161 
00175 #ifdef _FSAL_POSIX_USE_STREAM
00176 int fsal2posix_openflags(fsal_openflags_t fsal_flags, char *p_posix_flags)
00177 {
00178   int cpt;
00179 
00180   if(!p_posix_flags)
00181     return ERR_FSAL_FAULT;
00182 
00183   /* check that all used flags exist */
00184 
00185   if(fsal_flags &
00186      ~(FSAL_O_RDONLY | FSAL_O_RDWR | FSAL_O_WRONLY | FSAL_O_APPEND | FSAL_O_TRUNC))
00187     return ERR_FSAL_INVAL;
00188 
00189   /* Check for flags compatibility */
00190 
00191   /* O_RDONLY O_WRONLY O_RDWR cannot be used together */
00192 
00193   cpt = 0;
00194   if(fsal_flags & FSAL_O_RDONLY)
00195     cpt++;
00196   if(fsal_flags & FSAL_O_RDWR)
00197     cpt++;
00198   if(fsal_flags & FSAL_O_WRONLY)
00199     cpt++;
00200 
00201   if(cpt > 1)
00202     return ERR_FSAL_INVAL;
00203 
00204   /* FSAL_O_APPEND et FSAL_O_TRUNC cannot be used together */
00205 
00206   if((fsal_flags & FSAL_O_APPEND) && (fsal_flags & FSAL_O_TRUNC))
00207     return ERR_FSAL_INVAL;
00208 
00209   /* FSAL_O_TRUNC without FSAL_O_WRONLY or FSAL_O_RDWR */
00210 
00211   if((fsal_flags & FSAL_O_TRUNC) && !(fsal_flags & (FSAL_O_WRONLY | FSAL_O_RDWR)))
00212     return ERR_FSAL_INVAL;
00213 
00214   /* conversion */
00215 
00216   if(fsal_flags & FSAL_O_RDONLY)
00217     strcpy(p_posix_flags, "r");
00218   else if(fsal_flags & FSAL_O_WRONLY & FSAL_O_APPEND)
00219     strcpy(p_posix_flags, "a");
00220   else if(fsal_flags & FSAL_O_WRONLY & FSAL_O_TRUNC)
00221     strcpy(p_posix_flags, "w");
00222   else if(fsal_flags & FSAL_O_APPEND)
00223     strcpy(p_posix_flags, "a+");
00224   else if(fsal_flags & FSAL_O_TRUNC)
00225     strcpy(p_posix_flags, "w+");
00226   else
00227     strcpy(p_posix_flags, "r+");
00228 
00229   return ERR_FSAL_NO_ERROR;
00230 
00231 }
00232 #else
00233 int fsal2posix_openflags(fsal_openflags_t fsal_flags, int *p_posix_flags)
00234 {
00235   int cpt;
00236 
00237   if(!p_posix_flags)
00238     return ERR_FSAL_FAULT;
00239 
00240   /* check that all used flags exist */
00241 
00242   if(fsal_flags &
00243      ~(FSAL_O_RDONLY | FSAL_O_RDWR | FSAL_O_WRONLY | FSAL_O_APPEND | FSAL_O_TRUNC))
00244     return ERR_FSAL_INVAL;
00245 
00246   /* Check for flags compatibility */
00247 
00248   /* O_RDONLY O_WRONLY O_RDWR cannot be used together */
00249 
00250   cpt = 0;
00251   if(fsal_flags & FSAL_O_RDONLY)
00252     cpt++;
00253   if(fsal_flags & FSAL_O_RDWR)
00254     cpt++;
00255   if(fsal_flags & FSAL_O_WRONLY)
00256     cpt++;
00257 
00258   if(cpt > 1)
00259     return ERR_FSAL_INVAL;
00260 
00261   /* FSAL_O_APPEND et FSAL_O_TRUNC cannot be used together */
00262 
00263   if((fsal_flags & FSAL_O_APPEND) && (fsal_flags & FSAL_O_TRUNC))
00264     return ERR_FSAL_INVAL;
00265 
00266   /* FSAL_O_TRUNC without FSAL_O_WRONLY or FSAL_O_RDWR */
00267 
00268   if((fsal_flags & FSAL_O_TRUNC) && !(fsal_flags & (FSAL_O_WRONLY | FSAL_O_RDWR)))
00269     return ERR_FSAL_INVAL;
00270 
00271   /* conversion */
00272   *p_posix_flags = 0;
00273 
00274   if(fsal_flags & FSAL_O_RDONLY)
00275     *p_posix_flags |= O_RDONLY;
00276 
00277   if(fsal_flags & FSAL_O_RDWR)
00278     *p_posix_flags |= O_RDWR;
00279 
00280   if(fsal_flags & FSAL_O_WRONLY)
00281     *p_posix_flags |= O_WRONLY;
00282 
00283   if(fsal_flags & FSAL_O_APPEND)
00284     *p_posix_flags |= O_APPEND;
00285 
00286   if(fsal_flags & FSAL_O_TRUNC)
00287     *p_posix_flags |= O_TRUNC;
00288 
00289   if(fsal_flags & FSAL_O_CREATE)
00290     *p_posix_flags |= O_CREAT;
00291 
00292   return ERR_FSAL_NO_ERROR;
00293 }
00294 #endif                          /* _FSAL_POSIX_USE_STREAM */
00295 
00296 fsal_status_t posix2fsal_attributes(struct stat * p_buffstat,
00297                                     fsal_attrib_list_t * p_fsalattr_out)
00298 {
00299 
00300   fsal_attrib_mask_t supp_attr, unsupp_attr;
00301 
00302   /* sanity checks */
00303   if(!p_buffstat || !p_fsalattr_out)
00304     ReturnCode(ERR_FSAL_FAULT, 0);
00305 
00306   /* check that asked attributes are supported */
00307   supp_attr = global_fs_info.supported_attrs;
00308 
00309   unsupp_attr = (p_fsalattr_out->asked_attributes) & (~supp_attr);
00310   if(unsupp_attr)
00311     {
00312       LogFullDebug(COMPONENT_FSAL, "Unsupported attributes: %#llX", unsupp_attr);
00313       ReturnCode(ERR_FSAL_ATTRNOTSUPP, 0);
00314     }
00315 
00316   /* Initialize ACL regardless of whether ACL was asked or not.
00317    * This is needed to make sure ACL attribute is initialized. */
00318   p_fsalattr_out->acl = NULL;
00319 
00320   /* Fills the output struct */
00321   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_SUPPATTR))
00322     {
00323       p_fsalattr_out->supported_attributes = supp_attr;
00324     }
00325   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_TYPE))
00326     {
00327       p_fsalattr_out->type = posix2fsal_type(p_buffstat->st_mode);
00328     }
00329   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_SIZE))
00330     {
00331       p_fsalattr_out->filesize = p_buffstat->st_size;
00332     }
00333   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_FSID))
00334     {
00335       p_fsalattr_out->fsid = posix2fsal_fsid(p_buffstat->st_dev);
00336     }
00337   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_ACL))
00338     {
00339       p_fsalattr_out->acl = NULL;
00340     }
00341   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_FILEID))
00342     {
00343       p_fsalattr_out->fileid = (fsal_u64_t) (p_buffstat->st_ino);
00344     }
00345 
00346   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_MODE))
00347     {
00348       p_fsalattr_out->mode = unix2fsal_mode(p_buffstat->st_mode);
00349     }
00350   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_NUMLINKS))
00351     {
00352       p_fsalattr_out->numlinks = p_buffstat->st_nlink;
00353     }
00354   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_OWNER))
00355     {
00356       p_fsalattr_out->owner = p_buffstat->st_uid;
00357     }
00358   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_GROUP))
00359     {
00360       p_fsalattr_out->group = p_buffstat->st_gid;
00361     }
00362   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_ATIME))
00363     {
00364       p_fsalattr_out->atime = posix2fsal_time(p_buffstat->st_atime, p_buffstat->st_atim.tv_nsec);
00365 
00366     }
00367 
00368   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_CTIME))
00369     {
00370       p_fsalattr_out->ctime = posix2fsal_time(p_buffstat->st_ctime, p_buffstat->st_atim.tv_nsec);
00371     }
00372   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_MTIME))
00373     {
00374       p_fsalattr_out->mtime = posix2fsal_time(p_buffstat->st_mtime, p_buffstat->st_atim.tv_nsec);
00375     }
00376   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_MTIME))
00377     {
00378       p_fsalattr_out->mtime = posix2fsal_time(p_buffstat->st_mtime, p_buffstat->st_atim.tv_nsec);
00379     }
00380 
00381   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_CHGTIME))
00382     {
00383       p_fsalattr_out->chgtime
00384               = posix2fsal_time(MAX_2(p_buffstat->st_mtime, p_buffstat->st_ctime), 0);
00385     }
00386 
00387   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_SPACEUSED))
00388     {
00389       p_fsalattr_out->spaceused = p_buffstat->st_blocks * S_BLKSIZE;
00390     }
00391 
00392   if(FSAL_TEST_MASK(p_fsalattr_out->asked_attributes, FSAL_ATTR_RAWDEV))
00393     {
00394       p_fsalattr_out->rawdev = posix2fsal_devt(p_buffstat->st_rdev);    /* XXX: convert ? */
00395     }
00396   /* mounted_on_fileid :
00397      if ( FSAL_TEST_MASK(p_fsalattr_out->asked_attributes,
00398      FSAL_ATTR_MOUNTFILEID )){
00399      p_fsalattr_out->mounted_on_fileid = 
00400      hpss2fsal_64( p_hpss_attr_in->FilesetRootId );
00401      }
00402    */
00403 
00404   /* everything has been copied ! */
00405 
00406   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00407 }
00408 
00412 fsal_status_t posixdb2fsal_error(fsal_posixdb_status_t statusdb)
00413 {
00414   switch (statusdb.major)
00415     {
00416     case ERR_FSAL_POSIXDB_TOOMANYPATHS:
00417       LogEvent(COMPONENT_FSAL, "Fsal posixdb : too many paths ! (%d)", statusdb.minor);
00418     case ERR_FSAL_POSIXDB_NOERR:
00419       ReturnCode(ERR_FSAL_NO_ERROR, statusdb.major);
00420 
00421     case ERR_FSAL_POSIXDB_BADCONN:
00422     case ERR_FSAL_POSIXDB_CMDFAILED:
00423       ReturnCode(ERR_FSAL_SERVERFAULT, statusdb.major);
00424 
00425     case ERR_FSAL_POSIXDB_NOENT:
00426     case ERR_FSAL_POSIXDB_NOPATH:
00427       ReturnCode(ERR_FSAL_STALE, statusdb.major);
00428 
00429     case ERR_FSAL_POSIXDB_FAULT:
00430       ReturnCode(ERR_FSAL_FAULT, statusdb.major);
00431 
00432     case ERR_FSAL_POSIXDB_NO_MEM:
00433       ReturnCode(ERR_FSAL_NOMEM, statusdb.major);
00434 
00435     case ERR_FSAL_POSIXDB_PATHTOOLONG:
00436       ReturnCode(ERR_FSAL_NAMETOOLONG, statusdb.major);
00437 
00438     default:
00439       LogEvent(COMPONENT_FSAL, "Unknown posixdb error type: %d", statusdb.major);
00440       ReturnCode(ERR_FSAL_FAULT, statusdb.major);
00441     }
00442 }