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