In aio_fork, we have to close all fd's, we might hold a gpfs share mode
[ira/wip.git] / source3 / modules / gpfs.c
index 90be778260ec5994c5bcd0e1eb944166786cf101..9fc4524654b3263c0605496e30a4ee1cbbc9ec62 100644 (file)
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
 #ifdef HAVE_GPFS
 
 #include "gpfs_gpl.h"
+#include "vfs_gpfs.h"
 
-static void *libgpfs_handle = NULL;
-static BOOL gpfs_share_modes;
+static bool gpfs_share_modes;
+static bool gpfs_leases;
+static bool gpfs_getrealfilename;
 
 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
+static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
+                                           int *buflen);
 
 
-BOOL set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
+bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
                        uint32 share_access)
 {
        unsigned int allow = GPFS_SHARE_NONE;
@@ -89,7 +92,7 @@ int set_gpfs_lease(int fd, int leasetype)
 {
        int gpfs_type = GPFS_LEASE_NONE;
 
-       if (!gpfs_share_modes) {
+       if (!gpfs_leases) {
                return True;
        }
 
@@ -104,6 +107,13 @@ int set_gpfs_lease(int fd, int leasetype)
        if (leasetype == F_WRLCK) {
                gpfs_type = GPFS_LEASE_WRITE;
        }
+
+       /* we unconditionally set CAP_LEASE, rather than looking for
+          -1/EACCES as there is a bug in some versions of
+          libgpfs_gpl.so which results in a leaked fd on /dev/ss0
+          each time we try this with the wrong capabilities set
+       */
+       linux_set_lease_capability();
        return gpfs_set_lease_fn(fd, gpfs_type);
 }
 
@@ -127,68 +137,83 @@ int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
        return gpfs_putacl_fn(pathname, flags, acl);
 }
 
-void init_gpfs(void)
+int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
+                                   int *buflen)
 {
-       if (libgpfs_handle != NULL) {
-               return;
+       if ((!gpfs_getrealfilename)
+           || (gpfs_get_realfilename_path_fn == NULL)) {
+               errno = ENOSYS;
+               return -1;
        }
 
-       libgpfs_handle = sys_dlopen("libgpfs_gpl.so", RTLD_LAZY);
+       return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
+}
 
-       if (libgpfs_handle == NULL) {
-               DEBUG(10, ("sys_dlopen for libgpfs_gpl failed: %s\n",
-                          strerror(errno)));
-               return;
-       }
+static bool init_gpfs_function_lib(void *plibhandle_pointer,
+                                  const char *libname,
+                                  void *pfn_pointer, const char *fn_name)
+{
+       bool did_open_here = false;
+       void **libhandle_pointer = (void **)plibhandle_pointer;
+       void **fn_pointer = (void **)pfn_pointer;
 
-       DEBUG(10, ("libgpfs_gpl.so loaded\n"));
+       DEBUG(10, ("trying to load name %s from %s\n",
+                  fn_name, libname));
 
-       gpfs_set_share_fn = sys_dlsym(libgpfs_handle, "gpfs_set_share");
-       if (gpfs_set_share_fn == NULL) {
-               DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
-                         "'gpfs_set_share'\n"));
-               goto failed;
+       if (*libhandle_pointer == NULL) {
+               *libhandle_pointer = dlopen(libname, RTLD_LAZY);
+               did_open_here = true;
        }
-
-       gpfs_set_lease_fn = sys_dlsym(libgpfs_handle, "gpfs_set_lease");
-       if (gpfs_set_lease_fn == NULL) {
-               DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
-                         "'gpfs_set_lease'\n"));
-               sys_dlclose(libgpfs_handle);
-
-               goto failed;
+       if (*libhandle_pointer == NULL) {
+               DEBUG(10, ("Could not open lib %s\n", libname));
+               return false;
        }
 
-       gpfs_getacl_fn = sys_dlsym(libgpfs_handle, "gpfs_getacl");
-       if (gpfs_getacl_fn == NULL) {
-               DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
-                         "'gpfs_getacl'\n"));
-               goto failed;
+       *fn_pointer = dlsym(*libhandle_pointer, fn_name);
+       if (*fn_pointer == NULL) {
+               DEBUG(10, ("Did not find symbol %s in lib %s\n",
+                          fn_name, libname));
+               if (did_open_here) {
+                       dlclose(*libhandle_pointer);
+                       *libhandle_pointer = NULL;
+               }
+               return false;
        }
 
-       gpfs_putacl_fn = sys_dlsym(libgpfs_handle, "gpfs_putacl");
-       if (gpfs_putacl_fn == NULL) {
-               DEBUG(3, ("libgpfs_gpl.so does not contain the symbol "
-                         "'gpfs_putacl'\n"));
-               goto failed;
-       }
+       return true;
+}
+
+static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
+{
+       static void *libgpfs_handle = NULL;
+       static void *libgpfs_gpl_handle = NULL;
 
-       if (lp_parm_bool(-1, "gpfs", "sharemodes", True)) {
-               gpfs_share_modes = True;
-       } else {
-               gpfs_share_modes = False;
+       if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
+                                  fn_pointer, fn_name)) {
+               return true;
        }
+       if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
+                                  fn_pointer, fn_name)) {
+               return true;
+       }
+       return false;
+}
 
-       return;
+void init_gpfs(void)
+{
+       init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
+       init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
+       init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
+       init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
+       init_gpfs_function(&gpfs_get_realfilename_path_fn,
+                          "gpfs_get_realfilename_path");
+
+       gpfs_share_modes = lp_parm_bool(-1, "gpfs", "sharemodes", True);
+       gpfs_leases      = lp_parm_bool(-1, "gpfs", "leases", True);
+       gpfs_getrealfilename = lp_parm_bool(-1, "gpfs", "getrealfilename",
+                                           True);
 
-failed:
-       sys_dlclose(libgpfs_handle);
-       /* leave libgpfs_handle != NULL around, no point
-          in trying twice */
-       gpfs_set_share_fn = NULL;
-       gpfs_set_lease_fn = NULL;
-       gpfs_getacl_fn = NULL;
-       gpfs_putacl_fn = NULL;
+       return;
 }
 
 #else
@@ -203,7 +228,7 @@ int set_gpfs_lease(int snum, int leasetype)
        return -1;
 }
 
-BOOL set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
+bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
                        uint32 share_access)
 {
        DEBUG(0, ("VFS module - smbgpfs.so loaded, without gpfs support compiled\n"));
@@ -223,6 +248,13 @@ int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
        return -1;
 }
 
+int smbd_gpfs_get_realfilename_path(char *pathname, char *fileamep,
+                                   int *buflen)
+{
+       errno = ENOSYS;
+       return -1;
+}
+
 void init_gpfs(void)
 {
        return;