nfs-ganesha 1.4

nfs_Readlink.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 "ganesha_rpc.h"
00054 #include "nfs23.h"
00055 #include "nfs4.h"
00056 #include "mount.h"
00057 #include "nfs_core.h"
00058 #include "cache_inode.h"
00059 #include "nfs_exports.h"
00060 #include "nfs_creds.h"
00061 #include "nfs_proto_functions.h"
00062 #include "nfs_tools.h"
00063 #include "nfs_proto_tools.h"
00064 
00085 int nfs_Readlink(nfs_arg_t *parg,
00086                  exportlist_t *pexport,
00087                  fsal_op_context_t *pcontext,
00088                  nfs_worker_data_t *pworker,
00089                  struct svc_req *preq,
00090                  nfs_res_t * pres)
00091 {
00092   cache_entry_t *pentry = NULL;
00093   fsal_attrib_list_t attr;
00094   cache_inode_file_type_t filetype;
00095   cache_inode_status_t cache_status;
00096   fsal_path_t symlink_data;
00097   char *ptr = NULL;
00098   int rc = NFS_REQ_OK;
00099 
00100   if(isDebug(COMPONENT_NFSPROTO))
00101     {
00102       char str[LEN_FH_STR];
00103       nfs_FhandleToStr(preq->rq_vers,
00104                        &(parg->arg_readlink2),
00105                        &(parg->arg_readlink3.symlink),
00106                        NULL,
00107                        str);
00108       LogDebug(COMPONENT_NFSPROTO,
00109                "REQUEST PROCESSING: Calling nfs_Readlink handle: %s", str);
00110     }
00111 
00112   if(preq->rq_vers == NFS_V3)
00113     {
00114       /* to avoid setting it on each error case */
00115       pres->res_readlink3.READLINK3res_u.resfail.symlink_attributes.attributes_follow =
00116           FALSE;
00117     }
00118   /* Convert file handle into a vnode */
00119   if((pentry = nfs_FhandleToCache(preq->rq_vers,
00120                                   &(parg->arg_readlink2),
00121                                   &(parg->arg_readlink3.symlink),
00122                                   NULL,
00123                                   &(pres->res_readlink2.status),
00124                                   &(pres->res_readlink3.status),
00125                                   NULL, &attr, pcontext, &rc)) == NULL)
00126     {
00127       /* Stale NFS FH ? */
00128       goto out;
00129     }
00130 
00131   /* Extract the filetype */
00132   filetype = cache_inode_fsal_type_convert(attr.type);
00133 
00134   /* Sanity Check: the pentry must be a link */
00135   if(filetype != SYMBOLIC_LINK)
00136     {
00137       switch (preq->rq_vers)
00138         {
00139         case NFS_V2:
00140           pres->res_readlink2.status = NFSERR_IO;
00141           break;
00142 
00143         case NFS_V3:
00144           pres->res_readlink3.status = NFS3ERR_INVAL;
00145         }                       /* switch */
00146       rc = NFS_REQ_OK;
00147       goto out;
00148     }
00149 
00150   /* if */
00151   /* Perform readlink on the pentry */
00152   if(cache_inode_readlink(pentry,
00153                           &symlink_data,
00154                           pcontext, &cache_status)
00155      == CACHE_INODE_SUCCESS)
00156     {
00157       if((ptr = gsh_malloc(symlink_data.len+1)) == NULL)
00158         {
00159           switch (preq->rq_vers)
00160             {
00161             case NFS_V2:
00162               pres->res_readlink2.status = NFSERR_NXIO;
00163               break;
00164 
00165             case NFS_V3:
00166               pres->res_readlink3.status = NFS3ERR_IO;
00167             }                   /* switch */
00168           rc = NFS_REQ_OK;
00169           goto out;
00170         }
00171 
00172       strcpy(ptr, symlink_data.path);
00173 
00174       /* Reply to the client (think about free data after use ) */
00175       switch (preq->rq_vers)
00176         {
00177         case NFS_V2:
00178           pres->res_readlink2.READLINK2res_u.data = ptr;
00179           pres->res_readlink2.status = NFS_OK;
00180           break;
00181 
00182         case NFS_V3:
00183           pres->res_readlink3.READLINK3res_u.resok.data = ptr;
00184           nfs_SetPostOpAttr(pexport,
00185                             &attr,
00186                             &(pres->res_readlink3.READLINK3res_u.resok.
00187                               symlink_attributes));
00188           pres->res_readlink3.status = NFS3_OK;
00189           break;
00190         }
00191       rc = NFS_REQ_OK;
00192       goto out;
00193     }
00194 
00195   /* If we are here, there was an error */
00196   if(nfs_RetryableError(cache_status))
00197     {
00198       rc = NFS_REQ_DROP;
00199       goto out;
00200     }
00201 
00202   nfs_SetFailedStatus(pcontext, pexport,
00203                       preq->rq_vers,
00204                       cache_status,
00205                       &pres->res_readlink2.status,
00206                       &pres->res_readlink3.status,
00207                       pentry,
00208                       &(pres->res_readlink3.READLINK3res_u.resfail.symlink_attributes),
00209                       NULL, NULL, NULL, NULL, NULL, NULL);
00210 
00211   rc = NFS_REQ_OK;
00212 
00213 out:
00214   /* return references */
00215   if (pentry)
00216       cache_inode_put(pentry);
00217 
00218   return (rc);
00219 }                               /* nfs_Readlink */
00220 
00229 void nfs2_Readlink_Free(nfs_res_t * resp)
00230 {
00231   if(resp->res_readlink2.status == NFS_OK)
00232     gsh_free(resp->res_readlink2.READLINK2res_u.data);
00233 }                               /* nfs2_Readlink_Free */
00234 
00243 void nfs3_Readlink_Free(nfs_res_t * resp)
00244 {
00245   if(resp->res_readlink3.status == NFS3_OK)
00246     gsh_free(resp->res_readlink3.READLINK3res_u.resok.data);
00247 }                               /* nfs3_Readlink_Free */