nfs-ganesha 1.4

9p_attach.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2011)
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 
00035 #ifdef HAVE_CONFIG_H
00036 #include "config.h"
00037 #endif
00038 
00039 #ifdef _SOLARIS
00040 #include "solaris_port.h"
00041 #endif
00042 
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <pthread.h>
00046 #include "nfs_core.h"
00047 #include "log.h"
00048 #include "cache_inode.h"
00049 #include "fsal.h"
00050 #include "9p.h"
00051 
00052 
00053 int _9p_attach( _9p_request_data_t * preq9p,
00054                 void  * pworker_data,
00055                 u32 * plenout,
00056                 char * preply)
00057 {
00058   char * cursor = preq9p->_9pmsg + _9P_HDR_SIZE + _9P_TYPE_SIZE ;
00059 
00060   u16 * msgtag = NULL ;
00061   u32 * fid = NULL ;
00062   u32 * afid = NULL ;
00063   u16 * uname_len = NULL ;
00064   char * uname_str = NULL ;
00065   u16 * aname_len = NULL ;
00066   char * aname_str = NULL ;
00067   u32 * n_aname = NULL ;
00068 
00069   fsal_attrib_list_t fsalattr ;
00070 
00071   u32 err = 0 ;
00072  
00073   _9p_fid_t * pfid = NULL ;
00074 
00075   exportlist_t * pexport = NULL;
00076   unsigned int found = FALSE;
00077   cache_inode_status_t cache_status ;
00078   cache_inode_fsal_data_t fsdata ;
00079 
00080   if ( !preq9p || !pworker_data || !plenout || !preply )
00081    return -1 ;
00082 
00083   /* Get data */
00084   _9p_getptr( cursor, msgtag, u16 ) ; 
00085   _9p_getptr( cursor, fid,    u32 ) ; 
00086   _9p_getptr( cursor, afid,   u32 ) ; 
00087   _9p_getstr( cursor, uname_len, uname_str ) ;
00088   _9p_getstr( cursor, aname_len, aname_str ) ;
00089   _9p_getptr( cursor, n_aname, u32 ) ; 
00090 
00091   LogDebug( COMPONENT_9P, "TATTACH: tag=%u fid=%u afid=%d uname='%.*s' aname='%.*s' n_uname=%d", 
00092             (u32)*msgtag, *fid, *afid, (int)*uname_len, uname_str, (int)*aname_len, aname_str, *n_aname ) ;
00093 
00094   /*
00095    * Find the export for the aname (using as well Path or Tag ) 
00096    */
00097   for( pexport = nfs_param.pexportlist; pexport != NULL;
00098        pexport = pexport->next)
00099     {
00100       if(aname_str[0] != '/')
00101         {
00102           /* The input value may be a "Tag" */
00103           if(!strncmp(aname_str, pexport->FS_tag, strlen( pexport->FS_tag ) ) )
00104             {
00105               found = TRUE ;
00106               break;
00107             }
00108         }
00109       else
00110         {
00111           if(!strncmp(aname_str, pexport->fullpath, strlen( pexport->fullpath ) ) )
00112            {
00113               found = TRUE ;
00114               break;
00115            }
00116         }
00117     } /* for */
00118 
00119   /* Did we find something ? */
00120   if( found == FALSE )
00121     return _9p_rerror( preq9p, msgtag, ENOENT, plenout, preply ) ;
00122 
00123   if( *fid >= _9P_FID_PER_CONN )
00124     return _9p_rerror( preq9p, msgtag, ERANGE, plenout, preply ) ;
00125  
00126   /* Set pexport and fid id in fid */
00127   pfid= &preq9p->pconn->fids[*fid] ;
00128   pfid->pexport = pexport ;
00129   pfid->fid = *fid ;
00130 
00131   /* Is user name provided as a string or as an uid ? */
00132   if( *uname_len != 0 )
00133    {
00134      /* Build the fid creds */
00135     if( ( err = _9p_tools_get_fsal_op_context_by_name( *uname_len, uname_str, pfid ) ) !=  0 )
00136       return _9p_rerror( preq9p, msgtag, -err, plenout, preply ) ;
00137    }
00138   else
00139    {
00140     /* Build the fid creds */
00141     if( ( err = _9p_tools_get_fsal_op_context_by_uid( *n_aname, pfid ) ) !=  0 )
00142       return _9p_rerror( preq9p, msgtag, -err, plenout, preply ) ;
00143    }
00144 
00145   /* Get the related pentry */
00146   fsdata.fh_desc.start = (char *)pexport->proot_handle ;
00147   FSAL_ExpandHandle(pfid->fsal_op_context.export_context, FSAL_DIGEST_SIZEOF, &fsdata.fh_desc);
00148 
00149   /* refcount */
00150   if (pfid->pentry) {
00151       cache_inode_put(pfid->pentry);
00152   }
00153 
00154   /* refcount +1 */
00155   pfid->pentry = cache_inode_get( &fsdata,
00156                                   &fsalattr,
00157                                   &pfid->fsal_op_context,
00158                                   NULL,
00159                                   &cache_status ) ;
00160 
00161   if( pfid->pentry == NULL )
00162      return _9p_rerror( preq9p, msgtag, err, plenout, preply ) ;
00163 
00164   /* Compute the qid */
00165   pfid->qid.type = _9P_QTDIR ;
00166   pfid->qid.version = 0 ; /* No cache, we want the client to stay synchronous with the server */
00167   pfid->qid.path = fsalattr.fileid ;
00168 
00169   /* Build the reply */
00170   _9p_setinitptr( cursor, preply, _9P_RATTACH ) ;
00171   _9p_setptr( cursor, msgtag, u16 ) ;
00172 
00173   _9p_setqid( cursor, pfid->qid ) ;
00174 
00175   _9p_setendptr( cursor, preply ) ;
00176   _9p_checkbound( cursor, preply, plenout ) ;
00177 
00178   LogDebug( COMPONENT_9P, "RATTACH: tag=%u fid=%u qid=(type=%u,version=%u,path=%llu)", 
00179             *msgtag, *fid, (u32)pfid->qid.type, pfid->qid.version, (unsigned long long)pfid->qid.path ) ;
00180 
00181   return 1 ;
00182 }
00183