nfs-ganesha 1.4

nfs_mnt_list.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 
00064 #ifdef HAVE_CONFIG_H
00065 #include "config.h"
00066 #endif
00067 
00068 #ifdef _SOLARIS
00069 #include "solaris_port.h"
00070 #endif
00071 
00072 #include <stdio.h>
00073 #include <sys/types.h>
00074 #include <ctype.h>              /* for having isalnum */
00075 #include <stdlib.h>             /* for having atoi */
00076 #include <dirent.h>             /* for having MAXNAMLEN */
00077 #include <netdb.h>
00078 #include <netinet/in.h>
00079 #include <arpa/inet.h>
00080 #include <string.h>
00081 #include <pthread.h>
00082 #include <fcntl.h>
00083 #include <sys/file.h>           /* for having FNDELAY */
00084 #include <pwd.h>
00085 #include <grp.h>
00086 #include "log.h"
00087 #include "ganesha_rpc.h"
00088 #include "nfs_core.h"
00089 #include "nfs23.h"
00090 #include "nfs4.h"
00091 #include "fsal.h"
00092 #include "nfs_tools.h"
00093 #include "nfs_exports.h"
00094 #include "nfs_file_handle.h"
00095 
00096 /* The server's mount list (we use the native mount v3 structure) */
00097 mountlist MNT_List_head = NULL;
00098 mountlist MNT_List_tail = NULL;
00099 
00112 int nfs_Add_MountList_Entry(char *hostname, char *dirpath)
00113 {
00114 #ifndef _NO_MOUNT_LIST
00115   mountlist pnew_mnt_list_entry;
00116 #endif
00117 
00118   /* Sanity check */
00119   if(hostname == NULL || dirpath == NULL)
00120     return 0;
00121 
00122 #ifndef _NO_MOUNT_LIST
00123 
00124   /* Allocate the new entry */
00125   if((pnew_mnt_list_entry = gsh_calloc(1, sizeof(struct mountbody))) == NULL)
00126     return 0;
00127 
00128   if((pnew_mnt_list_entry->ml_hostname
00129       = gsh_calloc(1, MAXHOSTNAMELEN)) == NULL)
00130     {
00131       gsh_free(pnew_mnt_list_entry);
00132       return 0;
00133     }
00134 
00135   if((pnew_mnt_list_entry->ml_directory
00136       = gsh_calloc(1, MAXPATHLEN)) == NULL)
00137     {
00138       gsh_free(pnew_mnt_list_entry->ml_hostname);
00139       gsh_free(pnew_mnt_list_entry);
00140       return 0;
00141     }
00142   /* Copy the data */
00143   strncpy(pnew_mnt_list_entry->ml_hostname, hostname, MAXHOSTNAMELEN);
00144   strncpy(pnew_mnt_list_entry->ml_directory, dirpath, MAXPATHLEN);
00145 
00146   /* initialize next pointer */
00147   pnew_mnt_list_entry->ml_next = NULL;
00148 
00149   /* This should occur only for the first mount */
00150   if(MNT_List_head == NULL)
00151     {
00152       MNT_List_head = pnew_mnt_list_entry;
00153     }
00154 
00155   /* Append to the tail of the list */
00156   if(MNT_List_tail == NULL)
00157     MNT_List_tail = pnew_mnt_list_entry;
00158   else
00159     {
00160       MNT_List_tail->ml_next = pnew_mnt_list_entry;
00161       MNT_List_tail = pnew_mnt_list_entry;
00162     }
00163 
00164   if(isFullDebug(COMPONENT_NFSPROTO))
00165     nfs_Print_MountList();
00166 
00167 #endif
00168 
00169   return 1;
00170 }                               /* nfs_Add_MountList_Entry */
00171 
00184 int nfs_Remove_MountList_Entry(char *hostname, char *dirpath)
00185 {
00186 #ifndef _NO_MOUNT_LIST
00187   mountlist piter_mnt_list_entry;
00188   mountlist piter_mnt_list_entry_prev;
00189   int found = 0;
00190 #endif
00191 
00192   if(hostname == NULL)
00193     return 0;
00194 
00195 #ifndef _NO_MOUNT_LIST
00196 
00197   piter_mnt_list_entry_prev = NULL;
00198 
00199   for(piter_mnt_list_entry = MNT_List_head;
00200       piter_mnt_list_entry != NULL; piter_mnt_list_entry = piter_mnt_list_entry->ml_next)
00201     {
00202       /* BUGAZOMEU: pas de verif sur le path */
00203       if(!strncmp(piter_mnt_list_entry->ml_hostname, hostname, MAXHOSTNAMELEN)
00204          /*  && !strncmp( piter_mnt_list_entry->ml_directory, dirpath, MAXPATHLEN ) */ )
00205         {
00206           found = 1;
00207           break;
00208         }
00209 
00210       piter_mnt_list_entry_prev = piter_mnt_list_entry;
00211 
00212     }
00213 
00214   if(found)
00215     {
00216       /* remove head item ? */
00217       if(piter_mnt_list_entry_prev == NULL)
00218         MNT_List_head = MNT_List_head->ml_next;
00219       else
00220         piter_mnt_list_entry_prev->ml_next = piter_mnt_list_entry->ml_next;
00221 
00222       /* remove tail item ? */
00223       if(MNT_List_tail == piter_mnt_list_entry)
00224         MNT_List_tail = piter_mnt_list_entry_prev;
00225 
00226       gsh_free(piter_mnt_list_entry->ml_hostname);
00227       gsh_free(piter_mnt_list_entry->ml_directory);
00228       gsh_free(piter_mnt_list_entry);
00229 
00230     }
00231 
00232   if(isFullDebug(COMPONENT_NFSPROTO))
00233     nfs_Print_MountList();
00234 
00235 #endif
00236 
00237   return 1;
00238 }                               /* nfs_Remove_MountList_Entry */
00239 
00251 int nfs_Purge_MountList(void)
00252 {
00253   mountlist piter_mnt_list_entry __attribute__((unused)),
00254     piter_mnt_list_entry_next __attribute__((unused));
00255 
00256   piter_mnt_list_entry = MNT_List_head;
00257   piter_mnt_list_entry_next = MNT_List_head;
00258 
00259 #ifndef _NO_MOUNT_LIST
00260 
00261   while(piter_mnt_list_entry_next != NULL)
00262     {
00263       piter_mnt_list_entry_next = piter_mnt_list_entry->ml_next;
00264       gsh_free(piter_mnt_list_entry->ml_hostname);
00265       gsh_free(piter_mnt_list_entry->ml_directory);
00266       gsh_free(piter_mnt_list_entry);
00267       piter_mnt_list_entry = piter_mnt_list_entry_next;
00268     }
00269 
00270   MNT_List_head = NULL;
00271   MNT_List_tail = NULL;
00272 
00273   if(isFullDebug(COMPONENT_NFSPROTO))
00274     nfs_Print_MountList();
00275 
00276 #endif
00277 
00278   return 1;
00279 }                               /* nfs_Purge_MountList */
00280 
00292 int nfs_Init_MountList(void)
00293 {
00294   MNT_List_head = NULL;
00295   MNT_List_tail = NULL;
00296 
00297   if(isFullDebug(COMPONENT_NFSPROTO))
00298     nfs_Print_MountList();
00299 
00300   return 1;
00301 }                               /* nfs_Init_MountList */
00302 
00314 mountlist nfs_Get_MountList(void)
00315 {
00316   if(isFullDebug(COMPONENT_NFSPROTO))
00317     nfs_Print_MountList();
00318 
00319   return MNT_List_head;
00320 }                               /* nfs_Get_MountList */
00321 
00333 void nfs_Print_MountList(void)
00334 {
00335   mountlist piter_mnt_list_entry = NULL;
00336 
00337   if(MNT_List_head == NULL)
00338     LogFullDebug(COMPONENT_NFSPROTO, "Mount List Entry is empty");
00339 
00340   for(piter_mnt_list_entry = MNT_List_head;
00341       piter_mnt_list_entry != NULL; piter_mnt_list_entry = piter_mnt_list_entry->ml_next)
00342     LogFullDebug(COMPONENT_NFSPROTO,
00343                  "Mount List Entry : ml_hostname=%s   ml_directory=%s",
00344                  piter_mnt_list_entry->ml_hostname,
00345                  piter_mnt_list_entry->ml_directory);
00346 
00347   return;
00348 }                               /* nfs_Print_MountList */