gpfs: Rename wrapper for gpfs_ftruncate
[samba.git] / source3 / modules / gpfs.c
index a9f267d3d9ed0ef9d121b5fabcd0e2098db2bab6..f67b38c5f0ffaf808dde67d5c7b90d81459e925f 100644 (file)
 #include "vfs_gpfs.h"
 
 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_set_lease_fn)(int fd, unsigned int type);
 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);
-static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
-static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
+                                           int *len);
+static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags,
+                                       struct gpfs_winattr *attrs);
+static int (*gpfs_get_winattrs_path_fn)(char *pathname,
+                                       struct gpfs_winattr *attrs);
 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
-static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t startOffset, gpfs_off64_t bytesToPrealloc);
+static int (*gpfs_prealloc_fn)(int fd, gpfs_off64_t start, gpfs_off64_t bytes);
 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
 static int (*gpfs_lib_init_fn)(int flags);
 static int (*gpfs_set_times_path_fn)(char *pathname, int flags,
@@ -44,80 +46,59 @@ static int (*gpfs_quotactl_fn)(char *pathname, int cmd, int id, void *bufferP);
 static int (*gpfs_fcntl_fn)(gpfs_file_t fileDesc, void *fcntlArgP);
 static int (*gpfs_getfilesetid_fn)(char *pathname, char *name, int *idP);
 
-bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
-                       uint32 share_access)
+int gpfswrap_init(void)
 {
-       unsigned int allow = GPFS_SHARE_NONE;
-       unsigned int deny = GPFS_DENY_NONE;
-       int result;
+       static void *l;
 
-       if (gpfs_set_share_fn == NULL) {
-               return False;
+       if (l != NULL) {
+               return 0;
        }
 
-       if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
-               /* No real file, don't disturb */
-               return True;
+       l = dlopen("libgpfs.so", RTLD_LAZY);
+       if (l == NULL) {
+               return -1;
        }
 
-       allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
-                                DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
-       allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
-               GPFS_SHARE_READ : 0;
-
-       if (allow == GPFS_SHARE_NONE) {
-               DEBUG(10, ("special case am=no_access:%x\n",access_mask));
-       }
-       else {  
-               deny |= (share_access & FILE_SHARE_WRITE) ?
-                       0 : GPFS_DENY_WRITE;
-               deny |= (share_access & (FILE_SHARE_READ)) ?
-                       0 : GPFS_DENY_READ;
-       }
-       DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
-                  access_mask, allow, share_access, deny));
+       gpfs_set_share_fn             = dlsym(l, "gpfs_set_share");
+       gpfs_set_lease_fn             = dlsym(l, "gpfs_set_lease");
+       gpfs_getacl_fn                = dlsym(l, "gpfs_getacl");
+       gpfs_putacl_fn                = dlsym(l, "gpfs_putacl");
+       gpfs_get_realfilename_path_fn = dlsym(l, "gpfs_get_realfilename_path");
+       gpfs_set_winattrs_path_fn     = dlsym(l, "gpfs_set_winattrs_path");
+       gpfs_get_winattrs_path_fn     = dlsym(l, "gpfs_get_winattrs_path");
+       gpfs_get_winattrs_fn          = dlsym(l, "gpfs_get_winattrs");
+       gpfs_prealloc_fn              = dlsym(l, "gpfs_prealloc");
+       gpfs_ftruncate_fn             = dlsym(l, "gpfs_ftruncate");
+       gpfs_lib_init_fn              = dlsym(l, "gpfs_lib_init");
+       gpfs_set_times_path_fn        = dlsym(l, "gpfs_set_times_path");
+       gpfs_quotactl_fn              = dlsym(l, "gpfs_quotactl");
+       gpfs_fcntl_fn                 = dlsym(l, "gpfs_fcntl");
+       gpfs_getfilesetid_fn          = dlsym(l, "gpfs_getfilesetid");
+
+       return 0;
+}
 
-       result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
-       if (result != 0) {
-               if (errno == ENOSYS) {
-                       DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
-                                 "support has been compiled into Samba. Allowing access\n"));
-                       return True;
-               } else {
-                       DEBUG(10, ("gpfs_set_share failed: %s\n",
-                                  strerror(errno)));
-               }
+int gpfswrap_set_share(int fd, unsigned int allow, unsigned int deny)
+{
+       if (gpfs_set_share_fn == NULL) {
+               errno = ENOSYS;
+               return -1;
        }
 
-       return (result == 0);
+       return gpfs_set_share_fn(fd, allow, deny);
 }
 
-int set_gpfs_lease(int fd, int leasetype)
+int gpfswrap_set_lease(int fd, unsigned int type)
 {
-       int gpfs_type = GPFS_LEASE_NONE;
-
        if (gpfs_set_lease_fn == NULL) {
-               errno = EINVAL;
+               errno = ENOSYS;
                return -1;
        }
 
-       if (leasetype == F_RDLCK) {
-               gpfs_type = GPFS_LEASE_READ;
-       }
-       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);
+       return gpfs_set_lease_fn(fd, type);
 }
 
-int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
+int gpfswrap_getacl(char *pathname, int flags, void *acl)
 {
        if (gpfs_getacl_fn == NULL) {
                errno = ENOSYS;
@@ -127,7 +108,7 @@ int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
        return gpfs_getacl_fn(pathname, flags, acl);
 }
 
-int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
+int gpfswrap_putacl(char *pathname, int flags, void *acl)
 {
        if (gpfs_putacl_fn == NULL) {
                errno = ENOSYS;
@@ -137,48 +118,48 @@ int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
        return gpfs_putacl_fn(pathname, flags, acl);
 }
 
-int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
+int gpfswrap_get_realfilename_path(char *pathname, char *filenamep, int *len)
 {
-       if (gpfs_ftruncate_fn == NULL) {
+       if (gpfs_get_realfilename_path_fn == NULL) {
                errno = ENOSYS;
                return -1;
        }
 
-       return gpfs_ftruncate_fn(fd, length);
+       return gpfs_get_realfilename_path_fn(pathname, filenamep, len);
 }
 
-int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
-                                   int *buflen)
+int gpfswrap_set_winattrs_path(char *pathname, int flags,
+                              struct gpfs_winattr *attrs)
 {
-       if (gpfs_get_realfilename_path_fn == NULL) {
+       if (gpfs_set_winattrs_path_fn == NULL) {
                errno = ENOSYS;
                return -1;
        }
 
-       return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
+       return gpfs_set_winattrs_path_fn(pathname, flags, attrs);
 }
 
-int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
+int gpfswrap_get_winattrs_path(char *pathname, struct gpfs_winattr *attrs)
 {
        if (gpfs_get_winattrs_path_fn == NULL) {
-                errno = ENOSYS;
-                return -1;
-        }
-        DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
-        return gpfs_get_winattrs_path_fn(pathname, attrs);
+               errno = ENOSYS;
+               return -1;
+       }
+
+       return gpfs_get_winattrs_path_fn(pathname, attrs);
 }
 
-int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
+int gpfswrap_get_winattrs(int fd, struct gpfs_winattr *attrs)
 {
        if (gpfs_get_winattrs_fn == NULL) {
-                errno = ENOSYS;
-                return -1;
-        }
-        DEBUG(10, ("gpfs_get_winattrs:open call %d\n", fd));
-        return gpfs_get_winattrs_fn(fd, attrs);
+               errno = ENOSYS;
+               return -1;
+       }
+
+       return gpfs_get_winattrs_fn(fd, attrs);
 }
 
-int smbd_gpfs_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
+int gpfswrap_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
 {
        if (gpfs_prealloc_fn == NULL) {
                errno = ENOSYS;
@@ -188,15 +169,79 @@ int smbd_gpfs_prealloc(int fd, gpfs_off64_t start, gpfs_off64_t bytes)
        return gpfs_prealloc_fn(fd, start, bytes);
 }
 
-int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
+int gpfswrap_ftruncate(int fd, gpfs_off64_t length)
 {
-       if (gpfs_set_winattrs_path_fn == NULL) {
-                errno = ENOSYS;
-                return -1;
-        }
+       if (gpfs_ftruncate_fn == NULL) {
+               errno = ENOSYS;
+               return -1;
+       }
+
+       return gpfs_ftruncate_fn(fd, length);
+}
 
-        DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
-        return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
+bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
+                       uint32 share_access)
+{
+       unsigned int allow = GPFS_SHARE_NONE;
+       unsigned int deny = GPFS_DENY_NONE;
+       int result;
+
+       if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
+               /* No real file, don't disturb */
+               return True;
+       }
+
+       allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
+                                DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
+       allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
+               GPFS_SHARE_READ : 0;
+
+       if (allow == GPFS_SHARE_NONE) {
+               DEBUG(10, ("special case am=no_access:%x\n",access_mask));
+       }
+       else {  
+               deny |= (share_access & FILE_SHARE_WRITE) ?
+                       0 : GPFS_DENY_WRITE;
+               deny |= (share_access & (FILE_SHARE_READ)) ?
+                       0 : GPFS_DENY_READ;
+       }
+       DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
+                  access_mask, allow, share_access, deny));
+
+       result = gpfswrap_set_share(fsp->fh->fd, allow, deny);
+       if (result != 0) {
+               if (errno == ENOSYS) {
+                       DEBUG(5, ("VFS module vfs_gpfs loaded, but gpfs "
+                                 "set_share function support not available. "
+                                 "Allowing access\n"));
+                       return True;
+               } else {
+                       DEBUG(10, ("gpfs_set_share failed: %s\n",
+                                  strerror(errno)));
+               }
+       }
+
+       return (result == 0);
+}
+
+int set_gpfs_lease(int fd, int leasetype)
+{
+       int gpfs_type = GPFS_LEASE_NONE;
+
+       if (leasetype == F_RDLCK) {
+               gpfs_type = GPFS_LEASE_READ;
+       }
+       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 gpfswrap_set_lease(fd, gpfs_type);
 }
 
 int get_gpfs_quota(const char *pathname, int type, int id,
@@ -331,75 +376,3 @@ int smbd_gpfs_set_times_path(char *path, struct smb_file_time *ft)
 
        return rc;
 }
-
-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, ("trying to load name %s from %s\n",
-                  fn_name, libname));
-
-       if (*libhandle_pointer == NULL) {
-               *libhandle_pointer = dlopen(libname, RTLD_LAZY);
-               did_open_here = true;
-       }
-       if (*libhandle_pointer == NULL) {
-               DEBUG(10, ("Could not open lib %s\n", libname));
-               return false;
-       }
-
-       *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;
-       }
-
-       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 (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;
-}
-
-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");
-       init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
-        init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
-        init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
-       init_gpfs_function(&gpfs_prealloc_fn, "gpfs_prealloc");
-       init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
-        init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
-       init_gpfs_function(&gpfs_set_times_path_fn, "gpfs_set_times_path");
-       init_gpfs_function(&gpfs_quotactl_fn, "gpfs_quotactl");
-       init_gpfs_function(&gpfs_fcntl_fn, "gpfs_fcntl");
-       init_gpfs_function(&gpfs_getfilesetid_fn, "gpfs_getfilesetid");
-
-       return;
-}