nfs-ganesha 1.4

fsal_xattrs.c

Go to the documentation of this file.
00001 
00009 #ifdef HAVE_CONFIG_H
00010 #include "config.h"
00011 #endif
00012 
00013 #include "fsal.h"
00014 #include "fsal_internal.h"
00015 #include "fsal_convert.h"
00016 
00017 #include <unistd.h>
00018 #include <sys/types.h>
00019 
00020 /* Those definitions are only used for attributes emulated by FSAL.
00021  * For FUSE filesystems, we call FS primitives directly.
00022  */
00023 #if 0
00024 
00025 /* generic definitions for extended attributes */
00026 
00027 #define XATTR_FOR_FILE     0x00000001
00028 #define XATTR_FOR_DIR      0x00000002
00029 #define XATTR_FOR_SYMLINK  0x00000004
00030 #define XATTR_FOR_ALL      0x0000000F
00031 #define XATTR_RO           0x00000100
00032 #define XATTR_RW           0x00000200
00033 
00034 /* function for getting an attribute value */
00035 
00036 typedef int (*xattr_getfunc_t) (fsal_handle_t *,        /* object handle */
00037                                 fsal_op_context_t *,    /* context */
00038                                 caddr_t,        /* output buff */
00039                                 size_t, /* output buff size */
00040                                 size_t *);      /* output size */
00041 
00042 typedef int (*xattr_setfunc_t) (fsal_handle_t *,        /* object handle */
00043                                 fsal_op_context_t *,    /* context */
00044                                 caddr_t,        /* input buff */
00045                                 size_t, /* input size */
00046                                 int);   /* creation flag */
00047 
00048 typedef int (*xattr_printfunc_t) (caddr_t,      /* Input buffer */
00049                                   size_t,       /* Input size   */
00050                                   caddr_t,      /* Output (ASCII) buffer */
00051                                   size_t *);    /* Output size */
00052 
00053 typedef struct fsal_xattr_def__
00054 {
00055   char xattr_name[FSAL_MAX_NAME_LEN];
00056   xattr_getfunc_t get_func;
00057   xattr_setfunc_t set_func;
00058   xattr_printfunc_t print_func;
00059   int flags;
00060 } fsal_xattr_def_t;
00061 
00062 /*
00063  * DEFINE HERE YOUR GET/SET FUNCTIONS
00064  */
00065 
00066 int get_void_attr(fsal_handle_t * p_objecthandle,       /* IN */
00067                   fsal_op_context_t * p_context,        /* IN */
00068                   caddr_t buffer_addr,  /* IN/OUT */
00069                   size_t buffer_size,   /* IN */
00070                   size_t * p_output_size)       /* OUT */
00071 {
00072   if(!p_objecthandle || !p_context || !p_output_size)
00073     return ERR_FSAL_FAULT;
00074 
00075   snprintf((char *)buffer_addr, buffer_size, "Hello World !");
00076 
00077   *p_output_size = strlen((char *)buffer_addr) + 1;
00078 
00079   return 0;
00080 
00081 }
00082 
00083 /* DEFINE HERE YOUR ATTRIBUTES LIST */
00084 
00085 static fsal_xattr_def_t xattr_list[] = {
00086   {"hello_world", get_void_attr, NULL, NULL, XATTR_FOR_ALL | XATTR_RO}
00087 };
00088 
00089 #define XATTR_COUNT 1
00090 
00091 /* YOUR SHOULD NOT HAVE TO MODIFY THE FOLLOWING FUNCTIONS */
00092 
00093 /* test if an object has a given attribute */
00094 int do_match_type(int xattr_flag, int obj_type)
00095 {
00096   switch (obj_type)
00097     {
00098     case FSAL_TYPE_FILE:
00099       return ((xattr_flag & XATTR_FOR_FILE) == XATTR_FOR_FILE);
00100 
00101     case FSAL_TYPE_DIR:
00102       return ((xattr_flag & XATTR_FOR_DIR) == XATTR_FOR_DIR);
00103 
00104     case FSAL_TYPE_LNK:
00105       return ((xattr_flag & XATTR_FOR_SYMLINK) == XATTR_FOR_SYMLINK);
00106 
00107     default:
00108       return ((xattr_flag & XATTR_FOR_ALL) == XATTR_FOR_ALL);
00109     }
00110 }
00111 #else
00112 
00113 #define XATTR_COUNT 0
00114 #endif
00115 
00116 #if 0
00117 static int file_attributes_to_xattr_attrs(fsal_attrib_list_t * file_attrs,
00118                                           fsal_attrib_list_t * p_xattr_attrs,
00119                                           unsigned int attr_index)
00120 {
00121 
00122   /* supported attributes are:
00123    * - owner (same as the objet)
00124    * - group (same as the objet)
00125    * - type FSAL_TYPE_XATTR
00126    * - fileid (attr index ? or (fileid^((index+1)<<24)) )
00127    * - mode (config & file)
00128    * - atime, mtime, ctime = these of the object ?
00129    * - size=1block, used=1block
00130    * - rdev=0
00131    * - nlink=1
00132    */
00133   fsal_attrib_mask_t supported = FSAL_ATTR_SUPPATTR | FSAL_ATTR_MODE | FSAL_ATTR_FILEID
00134       | FSAL_ATTR_TYPE | FSAL_ATTR_OWNER | FSAL_ATTR_GROUP
00135       | FSAL_ATTR_ATIME | FSAL_ATTR_MTIME | FSAL_ATTR_CTIME
00136       | FSAL_ATTR_CREATION | FSAL_ATTR_CHGTIME | FSAL_ATTR_SIZE
00137       | FSAL_ATTR_SPACEUSED | FSAL_ATTR_NUMLINKS | FSAL_ATTR_RAWDEV | FSAL_ATTR_FSID;
00138   fsal_attrib_mask_t unsupp;
00139 
00140   /* only those supported by filesystem */
00141   supported &= global_fs_info.supported_attrs;
00142 
00143   if(p_xattr_attrs->asked_attributes == 0)
00144     {
00145       p_xattr_attrs->asked_attributes = supported;
00146 
00147       LogCrit(COMPONENT_FSAL,
00148               "Error: p_xattr_attrs->asked_attributes was 0 in %s() line %d, file %s",
00149               __FUNCTION__, __LINE__, __FILE__);
00150     }
00151 
00152   unsupp = p_xattr_attrs->asked_attributes & (~supported);
00153 
00154   if(unsupp)
00155     {
00156       LogDebug(COMPONENT_FSAL,
00157                "Asking for unsupported attributes in %s(): %#llX removing it from asked attributes",
00158                __FUNCTION__, unsupp);
00159 
00160       p_xattr_attrs->asked_attributes &= (~unsupp);
00161     }
00162 
00163   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_SUPPATTR)
00164     p_xattr_attrs->supported_attributes = supported;
00165 
00166   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_MODE)
00167     {
00168       p_xattr_attrs->mode = file_attrs->mode & global_fs_info.xattr_access_rights;
00169       if(xattr_list[attr_index].flags & XATTR_RO)
00170         p_xattr_attrs->mode &= ~(0222);
00171     }
00172 
00173   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_FILEID)
00174     {
00175       unsigned int i;
00176       unsigned long hash = attr_index + 1;
00177       char *str = (char *)&file_attrs->fileid;
00178 
00179       for(i = 0; i < sizeof(p_xattr_attrs->fileid); i++, str++)
00180         {
00181           hash = (hash << 5) - hash + (unsigned long)(*str);
00182         }
00183       p_xattr_attrs->fileid = hash;
00184     }
00185 
00186   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_TYPE)
00187     p_xattr_attrs->type = FSAL_TYPE_XATTR;
00188 
00189   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_OWNER)
00190     p_xattr_attrs->owner = file_attrs->owner;
00191 
00192   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_GROUP)
00193     p_xattr_attrs->group = file_attrs->group;
00194 
00195   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_ATIME)
00196     p_xattr_attrs->atime = file_attrs->atime;
00197 
00198   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_MTIME)
00199     p_xattr_attrs->mtime = file_attrs->mtime;
00200 
00201   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_CTIME)
00202     p_xattr_attrs->ctime = file_attrs->ctime;
00203 
00204   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_CREATION)
00205     p_xattr_attrs->creation = file_attrs->creation;
00206 
00207   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_CHGTIME)
00208     {
00209       p_xattr_attrs->chgtime = file_attrs->chgtime;
00210       p_xattr_attrs->change = (uint64_t) p_xattr_attrs->chgtime.seconds +
00211                               (uint64_t) p_xattr_attrs->chgtime.nseconds;
00212     }
00213 
00214   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_SIZE)
00215     p_xattr_attrs->filesize = DEV_BSIZE;
00216 
00217   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_SPACEUSED)
00218     p_xattr_attrs->spaceused = DEV_BSIZE;
00219 
00220   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_NUMLINKS)
00221     p_xattr_attrs->numlinks = 1;
00222 
00223   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_RAWDEV)
00224     {
00225       p_xattr_attrs->rawdev.major = 0;
00226       p_xattr_attrs->rawdev.minor = 0;
00227     }
00228 
00229   if(p_xattr_attrs->asked_attributes & FSAL_ATTR_FSID)
00230     {
00231       p_xattr_attrs->fsid = file_attrs->fsid;
00232     }
00233 
00234   /* if mode==0, then owner is set to root and mode is set to 0600 */
00235   if((p_xattr_attrs->asked_attributes & FSAL_ATTR_OWNER)
00236      && (p_xattr_attrs->asked_attributes & FSAL_ATTR_MODE) && (p_xattr_attrs->mode == 0))
00237     {
00238       p_xattr_attrs->owner = 0;
00239       p_xattr_attrs->mode = 0600;
00240       if(xattr_list[attr_index].flags & XATTR_RO)
00241         p_xattr_attrs->mode &= ~(0200);
00242     }
00243 
00244   return 0;
00245 
00246 }
00247 #endif
00248 
00257 fsal_status_t GPFSFSAL_GetXAttrAttrs(fsal_handle_t * p_objecthandle,        /* IN */
00258                                  fsal_op_context_t * p_context, /* IN */
00259                                  unsigned int xattr_id, /* IN */
00260                                  fsal_attrib_list_t * p_attrs
00262     )
00263 {
00264 #if 0
00265   int rc;
00266   char buff[MAXNAMLEN];
00267   fsal_status_t st;
00268   fsal_attrib_list_t file_attrs;
00269 #endif
00270 
00271   /* sanity checks */
00272   if(!p_objecthandle || !p_context || !p_attrs)
00273     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetXAttrAttrs);
00274 
00275   /* @todo: to be implemented */
00276 
00277   Return(ERR_FSAL_NOTSUPP, 0, INDEX_FSAL_GetXAttrAttrs);
00278 #if 0
00279   /* check that this index match the type of entry */
00280   if(xattr_id >= XATTR_COUNT
00281      || !do_match_type(xattr_list[xattr_id].flags, p_objecthandle->handle.handle_type))
00282     {
00283       Return(ERR_FSAL_INVAL, 0, INDEX_FSAL_GetXAttrAttrs);
00284     }
00285 
00286   /* object attributes we want to retrieve from parent */
00287   file_attrs.asked_attributes = FSAL_ATTR_MODE | FSAL_ATTR_FILEID | FSAL_ATTR_OWNER
00288       | FSAL_ATTR_GROUP | FSAL_ATTR_ATIME | FSAL_ATTR_MTIME
00289       | FSAL_ATTR_CTIME | FSAL_ATTR_CREATION | FSAL_ATTR_CHGTIME | FSAL_ATTR_FSID;
00290 
00291   /* don't retrieve attributes not asked */
00292 
00293   file_attrs.asked_attributes &= p_attrs->asked_attributes;
00294 
00295   st = GPFSFSAL_getattrs(p_objecthandle, p_context, &file_attrs);
00296 
00297   if(FSAL_IS_ERROR(st))
00298     Return(st.major, st.minor, INDEX_FSAL_GetXAttrAttrs);
00299 
00300   if((rc = file_attributes_to_xattr_attrs(&file_attrs, p_attrs, xattr_id)))
00301     {
00302       Return(ERR_FSAL_INVAL, rc, INDEX_FSAL_GetXAttrAttrs);
00303     }
00304 
00305   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_GetXAttrAttrs);
00306 #endif
00307 
00308 }                               /* FSAL_GetXAttrAttrs */
00309 
00322 fsal_status_t GPFSFSAL_ListXAttrs(fsal_handle_t * p_objecthandle,   /* IN */
00323                               unsigned int cookie,      /* IN */
00324                               fsal_op_context_t * p_context,    /* IN */
00325                               fsal_xattrent_t * xattrs_tab,     /* IN/OUT */
00326                               unsigned int xattrs_tabsize,      /* IN */
00327                               unsigned int *p_nb_returned,      /* OUT */
00328                               int *end_of_list  /* OUT */
00329     )
00330 {
00331 #if 0
00332   int rc;
00333   unsigned int index;
00334   unsigned int out_index;
00335   char object_path[FSAL_MAX_PATH_LEN];
00336   fsal_status_t st;
00337   fsal_attrib_list_t file_attrs;
00338 #endif
00339 
00340   /* sanity checks */
00341   if(!p_objecthandle || !p_context || !xattrs_tab || !p_nb_returned || !end_of_list)
00342     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_ListXAttrs);
00343 
00344   /* @todo: to be implemented */
00345 
00346   Return(ERR_FSAL_NOTSUPP, 0, INDEX_FSAL_ListXAttrs);
00347 
00348 #if 0
00349 
00350   /* object attributes we want to retrieve from parent */
00351   file_attrs.asked_attributes = FSAL_ATTR_MODE | FSAL_ATTR_FILEID | FSAL_ATTR_OWNER
00352       | FSAL_ATTR_GROUP | FSAL_ATTR_ATIME | FSAL_ATTR_MTIME
00353       | FSAL_ATTR_CTIME | FSAL_ATTR_CREATION | FSAL_ATTR_CHGTIME | FSAL_ATTR_FSID;
00354 
00355   /* don't retrieve unsuipported attributes */
00356   file_attrs.asked_attributes &= global_fs_info.supported_attrs;
00357 
00358   st = GPFSFSAL_getattrs(p_objecthandle, p_context, &file_attrs);
00359 
00360   if(FSAL_IS_ERROR(st))
00361     Return(st.major, st.minor, INDEX_FSAL_ListXAttrs);
00362 
00363   for(index = cookie, out_index = 0;
00364       index < XATTR_COUNT && out_index < xattrs_tabsize; index++)
00365     {
00366       if(do_match_type(xattr_list[index].flags, p_objecthandle->handle.handle_type))
00367         {
00368           /* fills an xattr entry */
00369           xattrs_tab[out_index].xattr_id = index;
00370           FSAL_str2name(xattr_list[index].xattr_name, FSAL_MAX_NAME_LEN,
00371                         &xattrs_tab[out_index].xattr_name);
00372           xattrs_tab[out_index].xattr_cookie = index + 1;
00373 
00374           /* set asked attributes (all supported) */
00375           xattrs_tab[out_index].attributes.asked_attributes =
00376               global_fs_info.supported_attrs;
00377 
00378           if(file_attributes_to_xattr_attrs
00379              (&file_attrs, &xattrs_tab[out_index].attributes, index))
00380             {
00381               /* set error flag */
00382               xattrs_tab[out_index].attributes.asked_attributes = FSAL_ATTR_RDATTR_ERR;
00383             }
00384 
00385           /* next output slot */
00386           out_index++;
00387         }
00388     }
00389 
00390   *p_nb_returned = out_index;
00391   *end_of_list = (index == XATTR_COUNT);
00392 
00393   Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_ListXAttrs);
00394 #endif
00395 
00396 }
00397 
00408 fsal_status_t GPFSFSAL_GetXAttrValueById(fsal_handle_t * p_objecthandle,    /* IN */
00409                                      unsigned int xattr_id,     /* IN */
00410                                      fsal_op_context_t * p_context,     /* IN */
00411                                      caddr_t buffer_addr,       /* IN/OUT */
00412                                      size_t buffer_size,        /* IN */
00413                                      size_t * p_output_size     /* OUT */
00414     )
00415 {
00416 #if 0
00417   int rc;
00418   /* sanity checks */
00419   if(!p_objecthandle || !p_context || !p_output_size || !buffer_addr)
00420     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetXAttrValue);
00421 
00422   /* check that this index match the type of entry */
00423   if(xattr_id >= XATTR_COUNT
00424      || !do_match_type(xattr_list[xattr_id].flags, p_objecthandle->handle.handle_type))
00425     {
00426       Return(ERR_FSAL_INVAL, 0, INDEX_FSAL_GetXAttrValue);
00427     }
00428 
00429   /* get the value */
00430   rc = xattr_list[xattr_id].get_func(p_objecthandle,
00431                                      p_context, buffer_addr, buffer_size, p_output_size);
00432   Return(rc, 0, INDEX_FSAL_GetXAttrValue);
00433 #endif
00434   Return(0, 0, INDEX_FSAL_GetXAttrValue);
00435 }
00436 
00446 fsal_status_t GPFSFSAL_GetXAttrIdByName(fsal_handle_t * p_objecthandle,     /* IN */
00447                                     const fsal_name_t * xattr_name,     /* IN */
00448                                     fsal_op_context_t * p_context,      /* IN */
00449                                     unsigned int *pxattr_id     /* OUT */
00450     )
00451 {
00452 #if 0
00453   unsigned int index;
00454   int found = FALSE;
00455 
00456   /* sanity checks */
00457   if(!p_objecthandle || !xattr_name)
00458     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetXAttrValue);
00459 
00460   for(index = 0; index < XATTR_COUNT; index++)
00461     {
00462       if(do_match_type(xattr_list[index].flags, p_objecthandle->handle.handle_type)
00463          && !strcmp(xattr_list[index].xattr_name, xattr_name->name))
00464         {
00465           found = TRUE;
00466           break;
00467         }
00468     }
00469 
00470   if(found)
00471     {
00472       *pxattr_id = index;
00473       Return(ERR_FSAL_NO_ERROR, 0, INDEX_FSAL_GetXAttrValue);
00474     }
00475   else
00476     Return(ERR_FSAL_NOENT, ENOENT, INDEX_FSAL_GetXAttrValue);
00477 #endif
00478 
00479   Return(ERR_FSAL_NOENT, 0, INDEX_FSAL_GetXAttrValue);
00480 }                               /* FSAL_GetXAttrIdByName */
00481 
00492 fsal_status_t GPFSFSAL_GetXAttrValueByName(fsal_handle_t * p_objecthandle,  /* IN */
00493                                        const fsal_name_t * xattr_name,  /* IN */
00494                                        fsal_op_context_t * p_context,   /* IN */
00495                                        caddr_t buffer_addr,     /* IN/OUT */
00496                                        size_t buffer_size,      /* IN */
00497                                        size_t * p_output_size   /* OUT */
00498     )
00499 {
00500 #if 0
00501   unsigned int index;
00502 #endif
00503 
00504   /* sanity checks */
00505   if(!p_objecthandle || !p_context || !p_output_size || !buffer_addr || !xattr_name)
00506     Return(ERR_FSAL_FAULT, 0, INDEX_FSAL_GetXAttrValue);
00507 
00508   /* look for this name */
00509 #if 0
00510   for(index = 0; index < XATTR_COUNT; index++)
00511     {
00512       if(do_match_type(xattr_list[index].flags, p_objecthandle->handle.handle_type)
00513          && !strcmp(xattr_list[index].xattr_name, xattr_name->name))
00514         {
00515 
00516           return FSAL_GetXAttrValueById(p_objecthandle, index, p_context, buffer_addr,
00517                                         buffer_size, p_output_size);
00518 
00519         }
00520     }
00521 #endif
00522   /* not found */
00523   Return(ERR_FSAL_NOENT, 0, INDEX_FSAL_GetXAttrValue);
00524 
00525 }
00526 
00527 fsal_status_t GPFSFSAL_SetXAttrValue(fsal_handle_t * p_objecthandle,        /* IN */
00528                                  const fsal_name_t * xattr_name,        /* IN */
00529                                  fsal_op_context_t * p_context, /* IN */
00530                                  caddr_t buffer_addr,   /* IN */
00531                                  size_t buffer_size,    /* IN */
00532                                  int create     /* IN */
00533     )
00534 {
00535   Return(ERR_FSAL_PERM, 0, INDEX_FSAL_SetXAttrValue);
00536 }
00537 
00538 fsal_status_t GPFSFSAL_SetXAttrValueById(fsal_handle_t * p_objecthandle,    /* IN */
00539                                      unsigned int xattr_id,     /* IN */
00540                                      fsal_op_context_t * p_context,     /* IN */
00541                                      caddr_t buffer_addr,       /* IN */
00542                                      size_t buffer_size /* IN */
00543     )
00544 {
00545   Return(ERR_FSAL_PERM, 0, INDEX_FSAL_SetXAttrValue);
00546 }
00547 
00555 fsal_status_t GPFSFSAL_RemoveXAttrById(fsal_handle_t * p_objecthandle,    /* IN */
00556                                        fsal_op_context_t * p_context,     /* IN */
00557                                        unsigned int xattr_id)
00558 {
00559   ReturnCode(ERR_FSAL_NOTSUPP, 0);
00560 }                               /* FSAL_RemoveXAttrById */
00561 
00562 
00570 fsal_status_t GPFSFSAL_RemoveXAttrByName(fsal_handle_t * p_objecthandle,    /* IN */
00571                                      fsal_op_context_t * p_context,     /* IN */
00572                                      const fsal_name_t * xattr_name)    /* IN */
00573 {
00574   ReturnCode(ERR_FSAL_NOTSUPP, 0);
00575 }                               /* FSAL_RemoveXAttrById */
00576