nfs-ganesha 1.4

nfs3_Access.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
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 
00037 #ifdef HAVE_CONFIG_H
00038 #include "config.h"
00039 #endif
00040 
00041 #ifdef _SOLARIS
00042 #include "solaris_port.h"
00043 #endif
00044 
00045 #include <stdio.h>
00046 #include <string.h>
00047 #include <pthread.h>
00048 #include <fcntl.h>
00049 #include <sys/file.h>           /* for having FNDELAY */
00050 #include "HashData.h"
00051 #include "HashTable.h"
00052 #include "log.h"
00053 #include "nfs23.h"
00054 #include "nfs4.h"
00055 #include "mount.h"
00056 #include "nfs_core.h"
00057 #include "cache_inode.h"
00058 #include "nfs_exports.h"
00059 #include "nfs_creds.h"
00060 #include "nfs_proto_functions.h"
00061 #include "nfs_proto_tools.h"
00062 #include "nfs_tools.h"
00063 
00082 int nfs3_Access(nfs_arg_t *parg,
00083                 exportlist_t *pexport,
00084                 fsal_op_context_t *pcontext,
00085                 nfs_worker_data_t *pworker,
00086                 struct svc_req *preq,
00087                 nfs_res_t *pres)
00088 {
00089   fsal_accessflags_t access_mode;
00090   cache_inode_status_t cache_status;
00091   cache_inode_file_type_t filetype;
00092   cache_entry_t *pentry = NULL;
00093   cache_inode_fsal_data_t fsal_data;
00094   fsal_attrib_list_t attr;
00095   int rc = NFS_REQ_OK;
00096 
00097   if(isDebug(COMPONENT_NFSPROTO))
00098     {
00099       char str[LEN_FH_STR];
00100       sprint_fhandle3(str, &(parg->arg_access3.object));
00101       LogDebug(COMPONENT_NFSPROTO,
00102                "REQUEST PROCESSING: Calling nfs3_Access handle: %s", str);
00103     }
00104 
00105   /* Is this a xattr FH ? */
00106   if(nfs3_Is_Fh_Xattr(&(parg->arg_access3.object)))
00107     {
00108       rc = nfs3_Access_Xattr(parg, pexport, pcontext, preq, pres);
00109       goto out;
00110     }
00111 
00112   /* to avoid setting it on each error case */
00113   pres->res_access3.ACCESS3res_u.resfail.obj_attributes.attributes_follow
00114     = FALSE;
00115 
00116   /* Convert file handle into a fsal_handle */
00117   if(nfs3_FhandleToFSAL(&(parg->arg_access3.object),
00118                         &fsal_data.fh_desc, pcontext) == 0)
00119     {
00120       rc = NFS_REQ_DROP;
00121       goto out;
00122     }
00123 
00124   /* Get the entry in the cache_inode */
00125   if((pentry = cache_inode_get(&fsal_data,
00126                                &attr,
00127                                pcontext,
00128                                NULL,
00129                                &cache_status)) == NULL)
00130     {
00131       if(nfs_RetryableError(cache_status))
00132         {
00133           rc = NFS_REQ_DROP;
00134           goto out;
00135         }
00136       else
00137         {
00138           pres->res_access3.status = nfs3_Errno(cache_status);
00139           rc = NFS_REQ_OK;
00140           goto out;
00141         }
00142     }
00143 
00144   /* Get file type */
00145   filetype = cache_inode_fsal_type_convert(attr.type);
00146 
00147   access_mode = 0;
00148 
00149   if(parg->arg_access3.access & ACCESS3_READ)
00150     access_mode |= nfs_get_access_mask(ACCESS3_READ, &attr);
00151 
00152   if(parg->arg_access3.access & ACCESS3_MODIFY)
00153     access_mode |= nfs_get_access_mask(ACCESS3_MODIFY, &attr);
00154 
00155   if(parg->arg_access3.access & ACCESS3_EXTEND)
00156     access_mode |= nfs_get_access_mask(ACCESS3_EXTEND, &attr);
00157 
00158   if(filetype == REGULAR_FILE)
00159     {
00160       if(parg->arg_access3.access & ACCESS3_EXECUTE)
00161         access_mode |= nfs_get_access_mask(ACCESS3_EXECUTE, &attr);
00162     }
00163   else if(parg->arg_access3.access & ACCESS3_LOOKUP)
00164     access_mode |= nfs_get_access_mask(ACCESS3_LOOKUP, &attr);
00165 
00166   if(filetype == DIRECTORY)
00167     {
00168       if(parg->arg_access3.access & ACCESS3_DELETE)
00169         access_mode |= nfs_get_access_mask(ACCESS3_DELETE, &attr);
00170     }
00171 
00172   nfs3_access_debug("requested access", parg->arg_access3.access);
00173 
00174   /* Perform the 'access' call */
00175   if(cache_inode_access(pentry,
00176                         access_mode,
00177                         pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00178     {
00179       nfs3_access_debug("granted access", parg->arg_access3.access);
00180 
00181       /* In Unix, delete permission only applies to directories */
00182 
00183       if(filetype == DIRECTORY)
00184         pres->res_access3.ACCESS3res_u.resok.access = parg->arg_access3.access;
00185       else
00186         pres->res_access3.ACCESS3res_u.resok.access =
00187             (parg->arg_access3.access & ~ACCESS3_DELETE);
00188 
00189       /* Build Post Op Attributes */
00190       nfs_SetPostOpAttr(pexport,
00191                         &attr,
00192                         &(pres->res_access3.ACCESS3res_u
00193                           .resok.obj_attributes));
00194 
00195       pres->res_access3.status = NFS3_OK;
00196       rc = NFS_REQ_OK;
00197       goto out;
00198     }
00199 
00200   if(cache_status == CACHE_INODE_FSAL_EACCESS)
00201     {
00202       /*
00203        * We have to determine which access bits are good one by one 
00204        */
00205       pres->res_access3.ACCESS3res_u.resok.access = 0;
00206 
00207       access_mode = nfs_get_access_mask(ACCESS3_READ, &attr);
00208       if(cache_inode_access(pentry,
00209                             access_mode,
00210                             pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00211         pres->res_access3.ACCESS3res_u.resok.access |= ACCESS3_READ;
00212 
00213       access_mode = nfs_get_access_mask(ACCESS3_MODIFY, &attr);
00214       if(cache_inode_access(pentry,
00215                             access_mode,
00216                             pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00217         pres->res_access3.ACCESS3res_u.resok.access |= ACCESS3_MODIFY;
00218 
00219       access_mode = nfs_get_access_mask(ACCESS3_EXTEND, &attr);
00220       if(cache_inode_access(pentry,
00221                             access_mode,
00222                             pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00223         pres->res_access3.ACCESS3res_u.resok.access |= ACCESS3_EXTEND;
00224 
00225       if(filetype == REGULAR_FILE)
00226         {
00227           access_mode = nfs_get_access_mask(ACCESS3_EXECUTE, &attr);
00228           if(cache_inode_access(pentry,
00229                                 access_mode,
00230                                 pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00231             pres->res_access3.ACCESS3res_u.resok.access |= ACCESS3_EXECUTE;
00232         }
00233       else
00234         {
00235           access_mode = nfs_get_access_mask(ACCESS3_LOOKUP, &attr);
00236           if(cache_inode_access(pentry,
00237                                 access_mode,
00238                                 pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00239             pres->res_access3.ACCESS3res_u.resok.access |= ACCESS3_LOOKUP;
00240         }
00241 
00242       if(filetype == DIRECTORY)
00243         {
00244           access_mode = nfs_get_access_mask(ACCESS3_DELETE, &attr);
00245           if(cache_inode_access(pentry,
00246                                 access_mode,
00247                                 pcontext, &cache_status) == CACHE_INODE_SUCCESS)
00248             pres->res_access3.ACCESS3res_u.resok.access |= ACCESS3_DELETE;
00249         }
00250 
00251       nfs3_access_debug("reduced access", pres->res_access3.ACCESS3res_u.resok.access);
00252 
00253       pres->res_access3.status = NFS3_OK;
00254       rc = NFS_REQ_OK;
00255       goto out;
00256     }
00257 
00258   /* If we are here, there was an error */
00259   if(nfs_RetryableError(cache_status))
00260     {
00261       rc = NFS_REQ_DROP;
00262       goto out;
00263     }
00264 
00265   nfs_SetFailedStatus(pcontext, pexport,
00266                       NFS_V3,
00267                       cache_status,
00268                       NULL,
00269                       &pres->res_access3.status,
00270                       pentry,
00271                       &(pres->res_access3.ACCESS3res_u.resfail.obj_attributes),
00272                       NULL, NULL, NULL, NULL, NULL, NULL);
00273 
00274 out:
00275 
00276   if (pentry)
00277     {
00278       cache_inode_put(pentry);
00279     }
00280 
00281   return rc;
00282 }                               /* nfs3_Access */
00283 
00292 void nfs3_Access_Free(nfs_res_t * pres)
00293 {
00294   /* Nothing to do */
00295   return;
00296 }                               /* nfs3_Access_Free */