Remove redundant parameter fd from SMB_VFS_PWRITE().
[jra/samba/.git] / source3 / smbd / vfs.c
index adbde4994db4331d1e20e49bc82b046b7c438838..aa914797d1d3432e08e37142ca9cd5359267f0b4 100644 (file)
@@ -4,10 +4,11 @@
    VFS initialisation and support functions
    Copyright (C) Tim Potter 1999
    Copyright (C) Alexander Bokovoy 2002
+   Copyright (C) James Peach 2006
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -16,8 +17,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/>.
 
    This work was sponsored by Optifacio Software Services, Inc.
 */
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
 
-struct vfs_init_function_entry {
-   char *name;
-   vfs_op_tuple *ops, *(*init)(const struct vfs_ops *, struct smb_vfs_handle_struct *);
-   struct vfs_init_function_entry *prev, *next;
-};
-
-static struct vfs_init_function_entry *backends = NULL;
-
-/* Some structures to help us initialise the vfs operations table */
+static_decl_vfs;
 
-struct vfs_syminfo {
+struct vfs_init_function_entry {
        char *name;
-       void *fptr;
+       const vfs_op_tuple *vfs_op_tuples;
+       struct vfs_init_function_entry *prev, *next;
 };
 
-/*
-  Opaque (final) vfs operations. This is a combination of first-met opaque vfs operations
-  across all currently processed modules.  */
-
-static vfs_op_tuple vfs_opaque_ops[SMB_VFS_OP_LAST];
-
-/* Default vfs hooks.  WARNING: The order of these initialisers is
-   very important.  They must be in the same order as defined in
-   vfs.h.  Change at your own peril. */
-
-static struct vfs_ops default_vfs_ops = {
-
-       /* Disk operations */
-
-       vfswrap_dummy_connect,
-       vfswrap_dummy_disconnect,
-       vfswrap_disk_free,
-
-       /* Directory operations */
-
-       vfswrap_opendir,
-       vfswrap_readdir,
-       vfswrap_mkdir,
-       vfswrap_rmdir,
-       vfswrap_closedir,
-
-       /* File operations */
-
-       vfswrap_open,
-       vfswrap_close,
-       vfswrap_read,
-       vfswrap_write,
-       vfswrap_lseek,
-       vfswrap_sendfile,
-       vfswrap_rename,
-       vfswrap_fsync,
-       vfswrap_stat,
-       vfswrap_fstat,
-       vfswrap_lstat,
-       vfswrap_unlink,
-       vfswrap_chmod,
-       vfswrap_fchmod,
-       vfswrap_chown,
-       vfswrap_fchown,
-       vfswrap_chdir,
-       vfswrap_getwd,
-       vfswrap_utime,
-       vfswrap_ftruncate,
-       vfswrap_lock,
-       vfswrap_symlink,
-       vfswrap_readlink,
-       vfswrap_link,
-       vfswrap_mknod,
-       vfswrap_realpath,
-
-       vfswrap_fget_nt_acl,
-       vfswrap_get_nt_acl,
-       vfswrap_fset_nt_acl,
-       vfswrap_set_nt_acl,
-
-       /* POSIX ACL operations. */
-#if defined(HAVE_NO_ACLS)
-       NULL,
-       NULL,
-#else
-       vfswrap_chmod_acl,
-       vfswrap_fchmod_acl,
-#endif
-       vfswrap_sys_acl_get_entry,
-       vfswrap_sys_acl_get_tag_type,
-       vfswrap_sys_acl_get_permset,
-       vfswrap_sys_acl_get_qualifier,
-       vfswrap_sys_acl_get_file,
-       vfswrap_sys_acl_get_fd,
-       vfswrap_sys_acl_clear_perms,
-       vfswrap_sys_acl_add_perm,
-       vfswrap_sys_acl_to_text,
-       vfswrap_sys_acl_init,
-       vfswrap_sys_acl_create_entry,
-       vfswrap_sys_acl_set_tag_type,
-       vfswrap_sys_acl_set_qualifier,
-       vfswrap_sys_acl_set_permset,
-       vfswrap_sys_acl_valid,
-       vfswrap_sys_acl_set_file,
-       vfswrap_sys_acl_set_fd,
-       vfswrap_sys_acl_delete_def_file,
-       vfswrap_sys_acl_get_perm,
-       vfswrap_sys_acl_free_text,
-       vfswrap_sys_acl_free_acl,
-       vfswrap_sys_acl_free_qualifier
-};
+static struct vfs_init_function_entry *backends = NULL;
 
 /****************************************************************************
     maintain the list of available backends
 ****************************************************************************/
 
-struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
+static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
 {
-   struct vfs_init_function_entry *entry = backends;
-   pstring stripped;
+       struct vfs_init_function_entry *entry = backends;
 
-   module_path_get_name(name, stripped);
-
-   while(entry) {
-       if (strequal(entry->name, stripped)) return entry;
-       entry = entry->next;
-   }
+       DEBUG(10, ("vfs_find_backend_entry called for %s\n", name));
+       while(entry) {
+               if (strcmp(entry->name, name)==0) return entry;
+               entry = entry->next;
+       }
 
-   return NULL;
+       return NULL;
 }
 
-BOOL smb_register_vfs(const char *name, vfs_op_tuple *(*init)(const struct vfs_ops *, struct smb_vfs_handle_struct *), int version)
+NTSTATUS smb_register_vfs(int version, const char *name, const vfs_op_tuple *vfs_op_tuples)
 {
-   struct vfs_init_function_entry *entry = backends;
-   
-   if ((version < SMB_VFS_INTERFACE_CASCADED)) {
-       DEBUG(0, ("vfs_init() returned wrong interface version info (was %d, should be no less than %d)\n",
-           version, SMB_VFS_INTERFACE_VERSION ));
-       return False;
-   }
-  
-   if ((version < SMB_VFS_INTERFACE_VERSION)) {
-       DEBUG(0, ("Warning: vfs_init() states that module confirms interface version #%d, current interface version is #%d.\n\
-               Proceeding in compatibility mode, new operations (since version #%d) will fallback to default ones.\n",
-               version, SMB_VFS_INTERFACE_VERSION, version ));
-          return False;
-   }
-   
-   while(entry) {
-       if (strequal(entry->name, name)) {
-           DEBUG(0,("VFS module %s already loaded!\n", name));
-           return False;
-       }
-       entry = entry->next;
-   }
-
-   entry = smb_xmalloc(sizeof(struct vfs_init_function_entry));
-   entry->name = smb_xstrdup(name);
-   entry->init = init;
-
-   DLIST_ADD(backends, entry);
-   DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
-   return True;
+       struct vfs_init_function_entry *entry = backends;
+
+       if ((version != SMB_VFS_INTERFACE_VERSION)) {
+               DEBUG(0, ("Failed to register vfs module.\n"
+                         "The module was compiled against SMB_VFS_INTERFACE_VERSION %d,\n"
+                         "current SMB_VFS_INTERFACE_VERSION is %d.\n"
+                         "Please recompile against the current Samba Version!\n",  
+                         version, SMB_VFS_INTERFACE_VERSION));
+               return NT_STATUS_OBJECT_TYPE_MISMATCH;
+       }
+
+       if (!name || !name[0] || !vfs_op_tuples) {
+               DEBUG(0,("smb_register_vfs() called with NULL pointer or empty name!\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       if (vfs_find_backend_entry(name)) {
+               DEBUG(0,("VFS module %s already loaded!\n", name));
+               return NT_STATUS_OBJECT_NAME_COLLISION;
+       }
+
+       entry = SMB_XMALLOC_P(struct vfs_init_function_entry);
+       entry->name = smb_xstrdup(name);
+       entry->vfs_op_tuples = vfs_op_tuples;
+
+       DLIST_ADD(backends, entry);
+       DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
+       return NT_STATUS_OK;
 }
 
 /****************************************************************************
@@ -194,202 +94,246 @@ BOOL smb_register_vfs(const char *name, vfs_op_tuple *(*init)(const struct vfs_o
 static void vfs_init_default(connection_struct *conn)
 {
        DEBUG(3, ("Initialising default vfs hooks\n"));
-
-       memcpy(&conn->vfs_ops, &default_vfs_ops, sizeof(struct vfs_ops));
-       conn->vfs_private = NULL;
+       vfs_init_custom(conn, DEFAULT_VFS_MODULE_NAME);
 }
 
-/***************************************************************************
- Function to load old VFS modules. Should go away after a while.
- **************************************************************************/
+/****************************************************************************
+  initialise custom vfs hooks
+ ****************************************************************************/
 
-static vfs_op_tuple *vfs_load_old_plugin(connection_struct *conn, const char *vfs_object)
+static inline void vfs_set_operation(struct vfs_ops * vfs, vfs_op_type which,
+                               struct vfs_handle_struct * handle, void * op)
 {
-       int vfs_version = -1;
-       vfs_op_tuple *ops, *(*init_fptr)(int *, const struct vfs_ops *, struct smb_vfs_handle_struct *);
-       /* Open object file */
+       ((struct vfs_handle_struct **)&vfs->handles)[which] = handle;
+       ((void **)(void *)&vfs->ops)[which] = op;
+}
 
-       if ((conn->vfs_private->handle = sys_dlopen(vfs_object, RTLD_NOW)) == NULL) {
-               DEBUG(0, ("Error opening %s: %s\n", vfs_object, sys_dlerror()));
-               return NULL;
+bool vfs_init_custom(connection_struct *conn, const char *vfs_object)
+{
+       const vfs_op_tuple *ops;
+       char *module_path = NULL;
+       char *module_name = NULL;
+       char *module_param = NULL, *p;
+       int i;
+       vfs_handle_struct *handle;
+       const struct vfs_init_function_entry *entry;
+       
+       if (!conn||!vfs_object||!vfs_object[0]) {
+               DEBUG(0,("vfs_init_custon() called with NULL pointer or emtpy vfs_object!\n"));
+               return False;
+       }
+
+       if(!backends) {
+               static_init_vfs;
        }
 
-       /* Get handle on vfs_init() symbol */
+       DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
 
-       init_fptr = (vfs_op_tuple *(*)(int *, const struct vfs_ops *, struct smb_vfs_handle_struct *))sys_dlsym(conn->vfs_private->handle, "vfs_init");
+       module_path = smb_xstrdup(vfs_object);
 
-       if (init_fptr == NULL) {
-               DEBUG(0, ("No vfs_init() symbol found in %s\n", vfs_object));
-               sys_dlclose(conn->vfs_private->handle);
-               return NULL;
+       p = strchr_m(module_path, ':');
+
+       if (p) {
+               *p = 0;
+               module_param = p+1;
+               trim_char(module_param, ' ', ' ');
        }
 
-       /* Initialise vfs_ops structure */
-       if ((ops = init_fptr(&vfs_version, &conn->vfs_ops, conn->vfs_private)) == NULL) {
-               DEBUG(0, ("vfs_init() function from %s failed\n", vfs_object));
-               sys_dlclose(conn->vfs_private->handle);
-               return NULL;
+       trim_char(module_path, ' ', ' ');
+
+       module_name = smb_xstrdup(module_path);
+
+       if ((module_name[0] == '/') &&
+           (strcmp(module_path, DEFAULT_VFS_MODULE_NAME) != 0)) {
+
+               /*
+                * Extract the module name from the path. Just use the base
+                * name of the last path component.
+                */
+
+               SAFE_FREE(module_name);
+               module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
+
+               p = strchr_m(module_name, '.');
+
+               if (p != NULL) {
+                       *p = '\0';
+               }
        }
 
-       if ((vfs_version < SMB_VFS_INTERFACE_CASCADED)) {
-               DEBUG(0, ("vfs_init() returned wrong interface version info (was %d, should be no less than %d)\n",
-                                 vfs_version, SMB_VFS_INTERFACE_VERSION ));
-               sys_dlclose(conn->vfs_private->handle);
-               return NULL;
+       /* First, try to load the module with the new module system */
+       if((entry = vfs_find_backend_entry(module_name)) || 
+          (NT_STATUS_IS_OK(smb_probe_module("vfs", module_path)) &&
+               (entry = vfs_find_backend_entry(module_name)))) {
+
+               DEBUGADD(5,("Successfully loaded vfs module [%s] with the new modules system\n", vfs_object));
+               
+               if ((ops = entry->vfs_op_tuples) == NULL) {
+                       DEBUG(0, ("entry->vfs_op_tuples==NULL for [%s] failed\n", vfs_object));
+                       goto fail;
+               }
+       } else {
+               DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
+               goto fail;
        }
 
-       if ((vfs_version < SMB_VFS_INTERFACE_VERSION)) {
-               DEBUG(0, ("Warning: vfs_init() states that module confirms interface version #%d, current interface version is #%d.\n\
-                                 Proceeding in compatibility mode, new operations (since version #%d) will fallback to default ones.\n",
-                                 vfs_version, SMB_VFS_INTERFACE_VERSION, vfs_version ));
-               sys_dlclose(conn->vfs_private->handle);
-               return NULL;
+       handle = TALLOC_ZERO_P(conn->mem_ctx,vfs_handle_struct);
+       if (!handle) {
+               DEBUG(0,("TALLOC_ZERO() failed!\n"));
+               goto fail;
        }
-       
-       return ops;
-}
+       memcpy(&handle->vfs_next, &conn->vfs, sizeof(struct vfs_ops));
+       handle->conn = conn;
+       if (module_param) {
+               handle->param = talloc_strdup(conn->mem_ctx, module_param);
+       }
+       DLIST_ADD(conn->vfs_handles, handle);
 
+       for(i=0; ops[i].op != NULL; i++) {
+               DEBUG(5, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer));
+               if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) {
+                       /* If this operation was already made opaque by different module, it
+                        * will be overridden here.
+                        */
+                       DEBUGADD(5, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object));
+                       vfs_set_operation(&conn->vfs_opaque, ops[i].type, handle, ops[i].op);
+               }
+               /* Change current VFS disposition*/
+               DEBUGADD(5, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object));
+               vfs_set_operation(&conn->vfs, ops[i].type, handle, ops[i].op);
+       }
 
+       SAFE_FREE(module_path);
+       SAFE_FREE(module_name);
+       return True;
 
-/****************************************************************************
-  initialise custom vfs hooks
- ****************************************************************************/
+ fail:
+       SAFE_FREE(module_path);
+       SAFE_FREE(module_name);
+       return False;
+}
+
+/*****************************************************************
+ Allow VFS modules to extend files_struct with VFS-specific state.
+ This will be ok for small numbers of extensions, but might need to
+ be refactored if it becomes more widely used.
+******************************************************************/
 
-BOOL vfs_init_custom(connection_struct *conn, const char *vfs_object)
+#define EXT_DATA_AREA(e) ((uint8 *)(e) + sizeof(struct vfs_fsp_data))
+
+void *vfs_add_fsp_extension_notype(vfs_handle_struct *handle, files_struct *fsp, size_t ext_size)
 {
-       vfs_op_tuple *ops;
-       int i;
-       struct vfs_init_function_entry *entry;
+       struct vfs_fsp_data *ext;
+       void * ext_data;
 
-       DEBUG(3, ("Initialising custom vfs hooks from %s\n", vfs_object));
+       /* Prevent VFS modules adding multiple extensions. */
+       if ((ext_data = vfs_fetch_fsp_extension(handle, fsp))) {
+               return ext_data;
+       }
 
-       if(!backends) static_init_vfs;
+       ext = (struct vfs_fsp_data *)TALLOC_ZERO(
+               handle->conn->mem_ctx, sizeof(struct vfs_fsp_data) + ext_size);
+       if (ext == NULL) {
+               return NULL;
+       }
 
-       /* First, try to load the module with the new module system */
-       if((entry = vfs_find_backend_entry(vfs_object)) || 
-          (smb_probe_module("vfs", vfs_object) && 
-               (entry = vfs_find_backend_entry(vfs_object)))) {
+       ext->owner = handle;
+       ext->next = fsp->vfs_extension;
+       fsp->vfs_extension = ext;
+       return EXT_DATA_AREA(ext);
+}
 
-               DEBUG(3,("Successfully loaded %s with the new modules system\n", vfs_object));
-               
-               if ((ops = entry->init(&conn->vfs_ops, conn->vfs_private)) == NULL) {
-                       DEBUG(0, ("vfs init function from %s failed\n", vfs_object));
-                       return False;
+void vfs_remove_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
+{
+       struct vfs_fsp_data *curr;
+       struct vfs_fsp_data *prev;
+
+       for (curr = fsp->vfs_extension, prev = NULL;
+            curr;
+            prev = curr, curr = curr->next) {
+               if (curr->owner == handle) {
+                   if (prev) {
+                           prev->next = curr->next;
+                   } else {
+                           fsp->vfs_extension = curr->next;
+                   }
+                   TALLOC_FREE(curr);
+                   return;
                }
-       } else {
-               /* If that doesn't work, fall back to the old system 
-                * (This part should go away after a while, it's only here 
-                * for backwards compatibility) */
-               DEBUG(2, ("Can't load module %s with new modules system, falling back to compatibility\n",
-                       vfs_object));
-               if ((ops = vfs_load_old_plugin(conn, vfs_object)) == NULL) {
-                       DEBUG(0, ("vfs init function from %s failed\n", vfs_object));
-                       return False;
+       }
+}
+
+void *vfs_memctx_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
+{
+       struct vfs_fsp_data *head;
+
+       for (head = fsp->vfs_extension; head; head = head->next) {
+               if (head->owner == handle) {
+                       return head;
                }
        }
 
-       for(i=0; ops[i].op != NULL; i++) {
-         DEBUG(3, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer));
-         if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) {
-           /* Check whether this operation was already made opaque by different module */
-           if(vfs_opaque_ops[ops[i].type].op == ((void**)&default_vfs_ops)[ops[i].type]) {
-             /* No, it isn't overloaded yet. Overload. */
-             DEBUG(3, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object));
-             vfs_opaque_ops[ops[i].type] = ops[i];
-           }
-         }
-         /* Change current VFS disposition*/
-         DEBUG(3, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object));
-         ((void**)&conn->vfs_ops)[ops[i].type] = ops[i].op;
+       return NULL;
+}
+
+void *vfs_fetch_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
+{
+       struct vfs_fsp_data *head;
+
+       head = (struct vfs_fsp_data *)vfs_memctx_fsp_extension(handle, fsp);
+       if (head != NULL) {
+               return EXT_DATA_AREA(head);
        }
 
-       return True;
+       return NULL;
 }
 
+#undef EXT_DATA_AREA
+
 /*****************************************************************
  Generic VFS init.
 ******************************************************************/
 
-BOOL smbd_vfs_init(connection_struct *conn)
+bool smbd_vfs_init(connection_struct *conn)
 {
        const char **vfs_objects;
-       char *vfs_module, *vfs_path;
        unsigned int i = 0;
        int j = 0;
-       struct smb_vfs_handle_struct *handle;
        
        /* Normal share - initialise with disk access functions */
        vfs_init_default(conn);
-       vfs_objects = lp_vfsobj(SNUM(conn));
+       vfs_objects = lp_vfs_objects(SNUM(conn));
 
-       /* Override VFS functions if 'vfs object' was specified*/
-       if (!vfs_objects)
+       /* Override VFS functions if 'vfs object' was not specified*/
+       if (!vfs_objects || !vfs_objects[0])
                return True;
-
-       for(i=0; i<SMB_VFS_OP_LAST; i++) {
-               vfs_opaque_ops[i].op = ((void**)&default_vfs_ops)[i];
-               vfs_opaque_ops[i].type = i;
-               vfs_opaque_ops[i].layer = SMB_VFS_LAYER_OPAQUE;
+       
+       for (i=0; vfs_objects[i] ;) {
+               i++;
        }
 
-       vfs_path = lp_vfs_path(SNUM(conn));
-       
-       for (i=0; vfs_objects[i]; i++); /* count passed modules */
-        
        for (j=i-1; j >= 0; j--) {
-               conn->vfs_private = NULL;
-               handle = (struct smb_vfs_handle_struct *) smb_xmalloc(sizeof(smb_vfs_handle_struct));
-               /* Loadable object file */
-               handle->handle = NULL;
-               DLIST_ADD(conn->vfs_private, handle);
-               vfs_module = NULL;
-               if (vfs_path && *vfs_path) {
-                       asprintf(&vfs_module, "%s/%s", vfs_path, vfs_objects[j]);
-               } else {
-                       asprintf(&vfs_module, "%s", vfs_objects[j]);
-               }
-               if (!vfs_init_custom(conn, vfs_module)) {
-                       DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_module));
-                       SAFE_FREE(vfs_module);
-                       DLIST_REMOVE(conn->vfs_private, handle);
-                       SAFE_FREE(handle);
+               if (!vfs_init_custom(conn, vfs_objects[j])) {
+                       DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_objects[j]));
                        return False;
                }
-               SAFE_FREE(vfs_module);
        }
        return True;
 }
 
-/*******************************************************************
- Create vfs_ops reflecting current vfs_opaque_ops
-*******************************************************************/
-
-struct vfs_ops *smb_vfs_get_opaque_ops(void)
-{
-  int i;
-  struct vfs_ops *ops;
-
-  ops = smb_xmalloc(sizeof(struct vfs_ops));
-
-  for(i=0; i<SMB_VFS_OP_LAST; i++) {
-    ((void**)ops)[i] = vfs_opaque_ops[i].op;
-  }
-  return ops;
-}
-
 /*******************************************************************
  Check if directory exists.
 ********************************************************************/
 
-BOOL vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_STAT *st)
+bool vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_STAT *st)
 {
        SMB_STRUCT_STAT st2;
-       BOOL ret;
+       bool ret;
 
        if (!st)
                st = &st2;
 
-       if (vfs_stat(conn,dname,st) != 0)
+       if (SMB_VFS_STAT(conn,dname,st) != 0)
                return(False);
 
        ret = S_ISDIR(st->st_mode);
@@ -399,45 +343,11 @@ BOOL vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_
        return ret;
 }
 
-/*******************************************************************
- vfs getwd wrapper 
-********************************************************************/
-
-static char *vfs_getwd(connection_struct *conn, char *path)
-{
-       return conn->vfs_ops.getwd(conn,path);
-}
-
-/*******************************************************************
- vfs mkdir wrapper 
-********************************************************************/
-
-int vfs_mkdir(connection_struct *conn, const char *name, mode_t mode)
-{
-       int ret;
-       SMB_STRUCT_STAT sbuf;
-
-       if(!(ret=conn->vfs_ops.mkdir(conn,name,mode))) {
-
-               inherit_access_acl(conn, name, mode);
-
-               /*
-                * Check if high bits should have been set,
-                * then (if bits are missing): add them.
-                * Consider bits automagically set by UNIX, i.e. SGID bit from parent dir.
-                */
-               if(mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) &&
-                               !vfs_stat(conn,name,&sbuf) && (mode & ~sbuf.st_mode))
-                       vfs_chmod(conn,name,sbuf.st_mode | (mode & ~sbuf.st_mode));
-       }
-       return ret;
-}
-
 /*******************************************************************
  Check if an object exists in the vfs.
 ********************************************************************/
 
-BOOL vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT *sbuf)
+bool vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT *sbuf)
 {
        SMB_STRUCT_STAT st;
 
@@ -446,7 +356,7 @@ BOOL vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT
 
        ZERO_STRUCTP(sbuf);
 
-       if (vfs_stat(conn,fname,sbuf) == -1)
+       if (SMB_VFS_STAT(conn,fname,sbuf) == -1)
                return(False);
        return True;
 }
@@ -455,7 +365,7 @@ BOOL vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT
  Check if a file exists in the vfs.
 ********************************************************************/
 
-BOOL vfs_file_exist(connection_struct *conn, const char *fname,SMB_STRUCT_STAT *sbuf)
+bool vfs_file_exist(connection_struct *conn, const char *fname,SMB_STRUCT_STAT *sbuf)
 {
        SMB_STRUCT_STAT st;
 
@@ -464,7 +374,7 @@ BOOL vfs_file_exist(connection_struct *conn, const char *fname,SMB_STRUCT_STAT *
 
        ZERO_STRUCTP(sbuf);
 
-       if (vfs_stat(conn,fname,sbuf) == -1)
+       if (SMB_VFS_STAT(conn,fname,sbuf) == -1)
                return False;
        return(S_ISREG(sbuf->st_mode));
 }
@@ -479,7 +389,7 @@ ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
 
        while (total < byte_count)
        {
-               ssize_t ret = fsp->conn->vfs_ops.read(fsp, fsp->fd, buf + total,
+               ssize_t ret = SMB_VFS_READ(fsp, fsp->fh->fd, buf + total,
                                        byte_count - total);
 
                if (ret == 0) return total;
@@ -494,17 +404,54 @@ ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
        return (ssize_t)total;
 }
 
+ssize_t vfs_pread_data(files_struct *fsp, char *buf,
+                size_t byte_count, SMB_OFF_T offset)
+{
+       size_t total=0;
+
+       while (total < byte_count)
+       {
+               ssize_t ret = SMB_VFS_PREAD(fsp, buf + total,
+                                       byte_count - total, offset + total);
+
+               if (ret == 0) return total;
+               if (ret == -1) {
+                       if (errno == EINTR)
+                               continue;
+                       else
+                               return -1;
+               }
+               total += ret;
+       }
+       return (ssize_t)total;
+}
+
 /****************************************************************************
  Write data to a fd on the vfs.
 ****************************************************************************/
 
-ssize_t vfs_write_data(files_struct *fsp,const char *buffer,size_t N)
+ssize_t vfs_write_data(struct smb_request *req,
+                       files_struct *fsp,
+                       const char *buffer,
+                       size_t N)
 {
        size_t total=0;
        ssize_t ret;
 
+       if (req && req->unread_bytes) {
+               SMB_ASSERT(req->unread_bytes == N);
+               /* VFS_RECVFILE must drain the socket
+                * before returning. */
+               req->unread_bytes = 0;
+               return SMB_VFS_RECVFILE(smbd_server_fd(),
+                                       fsp,
+                                       fsp->fh->fd,
+                                       (SMB_OFF_T)-1,
+                                       N);
+       }
+
        while (total < N) {
-               ret = fsp->conn->vfs_ops.write(fsp,fsp->fd,buffer + total,N - total);
+               ret = SMB_VFS_WRITE(fsp,fsp->fh->fd,buffer + total,N - total);
 
                if (ret == -1)
                        return -1;
@@ -516,6 +463,40 @@ ssize_t vfs_write_data(files_struct *fsp,const char *buffer,size_t N)
        return (ssize_t)total;
 }
 
+ssize_t vfs_pwrite_data(struct smb_request *req,
+                       files_struct *fsp,
+                       const char *buffer,
+                       size_t N,
+                       SMB_OFF_T offset)
+{
+       size_t total=0;
+       ssize_t ret;
+
+       if (req && req->unread_bytes) {
+               SMB_ASSERT(req->unread_bytes == N);
+               /* VFS_RECVFILE must drain the socket
+                * before returning. */
+               req->unread_bytes = 0;
+               return SMB_VFS_RECVFILE(smbd_server_fd(),
+                                       fsp,
+                                       fsp->fh->fd,
+                                       offset,
+                                       N);
+       }
+
+       while (total < N) {
+               ret = SMB_VFS_PWRITE(fsp, buffer + total, N - total,
+                                    offset + total);
+
+               if (ret == -1)
+                       return -1;
+               if (ret == 0)
+                       return total;
+
+               total += ret;
+       }
+       return (ssize_t)total;
+}
 /****************************************************************************
  An allocate file space call using the vfs interface.
  Allocates space for a file from a filedescriptor.
@@ -527,7 +508,6 @@ int vfs_allocate_file_space(files_struct *fsp, SMB_BIG_UINT len)
        int ret;
        SMB_STRUCT_STAT st;
        connection_struct *conn = fsp->conn;
-       struct vfs_ops *vfs_ops = &conn->vfs_ops;
        SMB_BIG_UINT space_avail;
        SMB_BIG_UINT bsize,dfree,dsize;
 
@@ -541,10 +521,11 @@ int vfs_allocate_file_space(files_struct *fsp, SMB_BIG_UINT len)
 
        if (((SMB_OFF_T)len) < 0) {
                DEBUG(0,("vfs_allocate_file_space: %s negative len requested.\n", fsp->fsp_name ));
+               errno = EINVAL;
                return -1;
        }
 
-       ret = vfs_fstat(fsp,fsp->fd,&st);
+       ret = SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st);
        if (ret == -1)
                return ret;
 
@@ -558,7 +539,7 @@ int vfs_allocate_file_space(files_struct *fsp, SMB_BIG_UINT len)
                                fsp->fsp_name, (double)st.st_size ));
 
                flush_write_cache(fsp, SIZECHANGE_FLUSH);
-               if ((ret = vfs_ops->ftruncate(fsp, fsp->fd, (SMB_OFF_T)len)) != -1) {
+               if ((ret = SMB_VFS_FTRUNCATE(fsp, fsp->fh->fd, (SMB_OFF_T)len)) != -1) {
                        set_filelen_write_cache(fsp, len);
                }
                return ret;
@@ -571,7 +552,10 @@ int vfs_allocate_file_space(files_struct *fsp, SMB_BIG_UINT len)
 
        len -= st.st_size;
        len /= 1024; /* Len is now number of 1k blocks needed. */
-       space_avail = conn->vfs_ops.disk_free(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
+       space_avail = get_dfree_info(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
+       if (space_avail == (SMB_BIG_UINT)-1) {
+               return -1;
+       }
 
        DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, needed blocks = %.0f, space avail = %.0f\n",
                        fsp->fsp_name, (double)st.st_size, (double)len, (double)space_avail ));
@@ -597,12 +581,83 @@ int vfs_set_filelen(files_struct *fsp, SMB_OFF_T len)
        release_level_2_oplocks_on_change(fsp);
        DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n", fsp->fsp_name, (double)len));
        flush_write_cache(fsp, SIZECHANGE_FLUSH);
-       if ((ret = fsp->conn->vfs_ops.ftruncate(fsp, fsp->fd, len)) != -1)
+       if ((ret = SMB_VFS_FTRUNCATE(fsp, fsp->fh->fd, len)) != -1) {
                set_filelen_write_cache(fsp, len);
+               notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
+                            FILE_NOTIFY_CHANGE_SIZE
+                            | FILE_NOTIFY_CHANGE_ATTRIBUTES,
+                            fsp->fsp_name);
+       }
 
        return ret;
 }
 
+/****************************************************************************
+ A vfs fill sparse call.
+ Writes zeros from the end of file to len, if len is greater than EOF.
+ Used only by strict_sync.
+ Returns 0 on success, -1 on failure.
+****************************************************************************/
+
+static char *sparse_buf;
+#define SPARSE_BUF_WRITE_SIZE (32*1024)
+
+int vfs_fill_sparse(files_struct *fsp, SMB_OFF_T len)
+{
+       int ret;
+       SMB_STRUCT_STAT st;
+       SMB_OFF_T offset;
+       size_t total;
+       size_t num_to_write;
+       ssize_t pwrite_ret;
+
+       release_level_2_oplocks_on_change(fsp);
+       ret = SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st);
+       if (ret == -1) {
+               return ret;
+       }
+
+       if (len <= st.st_size) {
+               return 0;
+       }
+
+       DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to len %.0f (%.0f bytes)\n",
+               fsp->fsp_name, (double)st.st_size, (double)len, (double)(len - st.st_size)));
+
+       flush_write_cache(fsp, SIZECHANGE_FLUSH);
+
+       if (!sparse_buf) {
+               sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
+               if (!sparse_buf) {
+                       errno = ENOMEM;
+                       return -1;
+               }
+       }
+
+       offset = st.st_size;
+       num_to_write = len - st.st_size;
+       total = 0;
+
+       while (total < num_to_write) {
+               size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (num_to_write - total));
+
+               pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
+               if (pwrite_ret == -1) {
+                       DEBUG(10,("vfs_fill_sparse: SMB_VFS_PWRITE for file %s failed with error %s\n",
+                               fsp->fsp_name, strerror(errno) ));
+                       return -1;
+               }
+               if (pwrite_ret == 0) {
+                       return 0;
+               }
+
+               total += pwrite_ret;
+       }
+
+       set_filelen_write_cache(fsp, len);
+       return 0;
+}
+
 /****************************************************************************
  Transfer some data (n bytes) between two file_struct's.
 ****************************************************************************/
@@ -612,12 +667,12 @@ static files_struct *out_fsp;
 
 static ssize_t read_fn(int fd, void *buf, size_t len)
 {
-       return in_fsp->conn->vfs_ops.read(in_fsp, fd, buf, len);
+       return SMB_VFS_READ(in_fsp, fd, buf, len);
 }
 
 static ssize_t write_fn(int fd, const void *buf, size_t len)
 {
-       return out_fsp->conn->vfs_ops.write(out_fsp, fd, buf, len);
+       return SMB_VFS_WRITE(out_fsp, fd, buf, len);
 }
 
 SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
@@ -625,7 +680,7 @@ SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
        in_fsp = in;
        out_fsp = out;
 
-       return transfer_file_internal(in_fsp->fd, out_fsp->fd, n, read_fn, write_fn);
+       return transfer_file_internal(in_fsp->fh->fd, out_fsp->fh->fd, n, read_fn, write_fn);
 }
 
 /*******************************************************************
@@ -634,13 +689,13 @@ SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
 
 char *vfs_readdirname(connection_struct *conn, void *p)
 {
-       struct dirent *ptr;
+       SMB_STRUCT_DIRENT *ptr= NULL;
        char *dname;
 
        if (!p)
                return(NULL);
 
-       ptr = (struct dirent *)conn->vfs_ops.readdir(conn,p);
+       ptr = SMB_VFS_READDIR(conn, (DIR *)p);
        if (!ptr)
                return(NULL);
 
@@ -651,7 +706,7 @@ char *vfs_readdirname(connection_struct *conn, void *p)
                return(NULL);
 #endif
 
-#ifdef HAVE_BROKEN_READDIR
+#ifdef HAVE_BROKEN_READDIR_NAME
        /* using /usr/ucb/cc is BAD */
        dname = dname - 2;
 #endif
@@ -659,72 +714,6 @@ char *vfs_readdirname(connection_struct *conn, void *p)
        return(dname);
 }
 
-/* VFS options not quite working yet */
-
-#if 0
-
-/***************************************************************************
-  handle the interpretation of the vfs option parameter
- *************************************************************************/
-static BOOL handle_vfs_option(char *pszParmValue, char **ptr)
-{
-    struct vfs_options *new_option, **options = (struct vfs_options **)ptr;
-    int i;
-
-    /* Create new vfs option */
-
-    new_option = (struct vfs_options *)malloc(sizeof(*new_option));
-    if (new_option == NULL) {
-       return False;
-    }
-
-    ZERO_STRUCTP(new_option);
-
-    /* Get name and value */
-
-    new_option->name = strtok(pszParmValue, "=");
-
-    if (new_option->name == NULL) {
-       return False;
-    }
-
-    while(isspace(*new_option->name)) {
-       new_option->name++;
-    }
-
-    for (i = strlen(new_option->name); i > 0; i--) {
-       if (!isspace(new_option->name[i - 1])) break;
-    }
-
-    new_option->name[i] = '\0';
-    new_option->name = strdup(new_option->name);
-
-    new_option->value = strtok(NULL, "=");
-
-    if (new_option->value != NULL) {
-
-       while(isspace(*new_option->value)) {
-           new_option->value++;
-       }
-       
-       for (i = strlen(new_option->value); i > 0; i--) {
-           if (!isspace(new_option->value[i - 1])) break;
-       }
-       
-       new_option->value[i] = '\0';
-       new_option->value = strdup(new_option->value);
-    }
-
-    /* Add to list */
-
-    DLIST_ADD(*options, new_option);
-
-    return True;
-}
-
-#endif
-
-
 /*******************************************************************
  A wrapper for vfs_chdir().
 ********************************************************************/
@@ -732,7 +721,11 @@ static BOOL handle_vfs_option(char *pszParmValue, char **ptr)
 int vfs_ChDir(connection_struct *conn, const char *path)
 {
        int res;
-       static pstring LastDir="";
+       static char *LastDir = NULL;
+
+       if (!LastDir) {
+               LastDir = SMB_STRDUP("");
+       }
 
        if (strcsequal(path,"."))
                return(0);
@@ -740,47 +733,14 @@ int vfs_ChDir(connection_struct *conn, const char *path)
        if (*path == '/' && strcsequal(LastDir,path))
                return(0);
 
-       DEBUG(3,("vfs_ChDir to %s\n",path));
-
-       res = vfs_chdir(conn,path);
-       if (!res)
-               pstrcpy(LastDir,path);
-       return(res);
-}
-
-/* number of list structures for a caching GetWd function. */
-#define MAX_GETWDCACHE (50)
-
-static struct {
-       SMB_DEV_T dev; /* These *must* be compatible with the types returned in a stat() call. */
-       SMB_INO_T inode; /* These *must* be compatible with the types returned in a stat() call. */
-       char *dos_path; /* The pathname in DOS format. */
-       BOOL valid;
-} ino_list[MAX_GETWDCACHE];
-
-extern BOOL use_getwd_cache;
-
-/****************************************************************************
- Prompte a ptr (to make it recently used)
-****************************************************************************/
-
-static void array_promote(char *array,int elsize,int element)
-{
-       char *p;
-       if (element == 0)
-               return;
-
-       p = (char *)malloc(elsize);
+       DEBUG(4,("vfs_ChDir to %s\n",path));
 
-       if (!p) {
-               DEBUG(5,("array_promote: malloc fail\n"));
-               return;
+       res = SMB_VFS_CHDIR(conn,path);
+       if (!res) {
+               SAFE_FREE(LastDir);
+               LastDir = SMB_STRDUP(path);
        }
-
-       memcpy(p,array + element * elsize, elsize);
-       memmove(array + elsize,array,elsize*element);
-       memcpy(array,p,elsize);
-       SAFE_FREE(p);
+       return(res);
 }
 
 /*******************************************************************
@@ -789,266 +749,228 @@ static void array_promote(char *array,int elsize,int element)
  format. Note this can be called with conn == NULL.
 ********************************************************************/
 
-char *vfs_GetWd(connection_struct *conn, char *path)
+struct getwd_cache_key {
+       SMB_DEV_T dev;
+       SMB_INO_T ino;
+};
+
+char *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
 {
-       pstring s;
-       static BOOL getwd_cache_init = False;
+        char s[PATH_MAX+1];
        SMB_STRUCT_STAT st, st2;
-       int i;
+       char *result;
+       DATA_BLOB cache_value;
+       struct getwd_cache_key key;
 
        *s = 0;
 
-       if (!use_getwd_cache)
-               return(vfs_getwd(conn,path));
-
-       /* init the cache */
-       if (!getwd_cache_init) {
-               getwd_cache_init = True;
-               for (i=0;i<MAX_GETWDCACHE;i++) {
-                       string_set(&ino_list[i].dos_path,"");
-                       ino_list[i].valid = False;
-               }
+       if (!lp_getwd_cache()) {
+               goto nocache;
        }
 
-       /*  Get the inode of the current directory, if this doesn't work we're
-               in trouble :-) */
+       SET_STAT_INVALID(st);
 
-       if (vfs_stat(conn, ".",&st) == -1) {
-               DEBUG(0,("Very strange, couldn't stat \".\" path=%s\n", path));
-               return(vfs_getwd(conn,path));
+       if (SMB_VFS_STAT(conn, ".",&st) == -1) {
+               /*
+                * Known to fail for root: the directory may be NFS-mounted
+                * and exported with root_squash (so has no root access).
+                */
+               DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
+                        "(NFS problem ?)\n", strerror(errno) ));
+               goto nocache;
        }
 
+       ZERO_STRUCT(key); /* unlikely, but possible padding */
+       key.dev = st.st_dev;
+       key.ino = st.st_ino;
 
-       for (i=0; i<MAX_GETWDCACHE; i++) {
-               if (ino_list[i].valid) {
-
-                       /*  If we have found an entry with a matching inode and dev number
-                               then find the inode number for the directory in the cached string.
-                               If this agrees with that returned by the stat for the current
-                               directory then all is o.k. (but make sure it is a directory all
-                               the same...) */
-
-                       if (st.st_ino == ino_list[i].inode && st.st_dev == ino_list[i].dev) {
-                               if (vfs_stat(conn,ino_list[i].dos_path,&st2) == 0) {
-                                       if (st.st_ino == st2.st_ino && st.st_dev == st2.st_dev &&
-                                                       (st2.st_mode & S_IFMT) == S_IFDIR) {
-                                               pstrcpy (path, ino_list[i].dos_path);
-
-                                               /* promote it for future use */
-                                               array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
-                                               return (path);
-                                       } else {
-                                               /*  If the inode is different then something's changed,
-                                                       scrub the entry and start from scratch. */
-                                               ino_list[i].valid = False;
-                                       }
-                               }
-                       }
-               }
+       if (!memcache_lookup(smbd_memcache(), GETWD_CACHE,
+                            data_blob_const(&key, sizeof(key)),
+                            &cache_value)) {
+               goto nocache;
        }
 
-       /*  We don't have the information to hand so rely on traditional methods.
-               The very slow getcwd, which spawns a process on some systems, or the
-               not quite so bad getwd. */
+       SMB_ASSERT((cache_value.length > 0)
+                  && (cache_value.data[cache_value.length-1] == '\0'));
 
-       if (!vfs_getwd(conn,s)) {
-               DEBUG(0,("vfs_GetWd: vfs_getwd call failed, errno %s\n",strerror(errno)));
-               return (NULL);
+       if ((SMB_VFS_STAT(conn, (char *)cache_value.data, &st2) == 0)
+           && (st.st_dev == st2.st_dev) && (st.st_ino == st2.st_ino)
+           && (S_ISDIR(st.st_mode))) {
+               /*
+                * Ok, we're done
+                */
+               result = talloc_strdup(ctx, (char *)cache_value.data);
+               if (result == NULL) {
+                       errno = ENOMEM;
+               }
+               return result;
        }
 
-       pstrcpy(path,s);
+ nocache:
 
-       DEBUG(5,("vfs_GetWd %s, inode %.0f, dev %.0f\n",s,(double)st.st_ino,(double)st.st_dev));
-
-       /* add it to the cache */
-       i = MAX_GETWDCACHE - 1;
-       string_set(&ino_list[i].dos_path,s);
-       ino_list[i].dev = st.st_dev;
-       ino_list[i].inode = st.st_ino;
-       ino_list[i].valid = True;
-
-       /* put it at the top of the list */
-       array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
-
-       return (path);
-}
-
-
-/* check if the file 'nmae' is a symlink, in that case check that it point to
-   a file that reside under the 'dir' tree */
+       /*
+        * We don't have the information to hand so rely on traditional
+        * methods. The very slow getcwd, which spawns a process on some
+        * systems, or the not quite so bad getwd.
+        */
 
-static BOOL readlink_check(connection_struct *conn, const char *dir, char *name)
-{
-       BOOL ret = True;
-       pstring flink;
-       pstring cleanlink;
-       pstring savedir;
-       pstring realdir;
-       size_t reallen;
-
-       if (!vfs_GetWd(conn, savedir)) {
-               DEBUG(0,("couldn't vfs_GetWd for %s %s\n", name, dir));
-               return False;
+       if (!SMB_VFS_GETWD(conn,s)) {
+               DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
+                         strerror(errno)));
+               return NULL;
        }
 
-       if (vfs_ChDir(conn, dir) != 0) {
-               DEBUG(0,("couldn't vfs_ChDir to %s\n", dir));
-               return False;
-       }
+       if (lp_getwd_cache() && VALID_STAT(st)) {
+               ZERO_STRUCT(key); /* unlikely, but possible padding */
+               key.dev = st.st_dev;
+               key.ino = st.st_ino;
 
-       if (!vfs_GetWd(conn, realdir)) {
-               DEBUG(0,("couldn't vfs_GetWd for %s\n", dir));
-               vfs_ChDir(conn, savedir);
-               return(False);
-       }
-       
-       reallen = strlen(realdir);
-       if (realdir[reallen -1] == '/') {
-               reallen--;
-               realdir[reallen] = 0;
+               memcache_add(smbd_memcache(), GETWD_CACHE,
+                            data_blob_const(&key, sizeof(key)),
+                            data_blob_const(s, strlen(s)+1));
        }
 
-       if (conn->vfs_ops.readlink(conn, name, flink, sizeof(pstring) -1) != -1) {
-               DEBUG(3,("reduce_name: file path name %s is a symlink\nChecking it's path\n", name));
-               if (*flink == '/') {
-                       pstrcpy(cleanlink, flink);
-               } else {
-                       pstrcpy(cleanlink, realdir);
-                       pstrcat(cleanlink, "/");
-                       pstrcat(cleanlink, flink);
-               }
-               unix_clean_name(cleanlink);
-
-               if (strncmp(cleanlink, realdir, reallen) != 0) {
-                       DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n", name, realdir, cleanlink, (int)reallen));
-                       ret = False;
-               }
+       result = talloc_strdup(ctx, s);
+       if (result == NULL) {
+               errno = ENOMEM;
        }
-
-       vfs_ChDir(conn, savedir);
-       
-       return ret;
+       return result;
 }
 
 /*******************************************************************
  Reduce a file name, removing .. elements and checking that
- it is below dir in the heirachy. This uses vfs_GetWd() and so must be run
- on the system that has the referenced file system.
- Widelinks are allowed if widelinks is true.
+ it is below dir in the heirachy. This uses realpath.
 ********************************************************************/
 
-BOOL reduce_name(connection_struct *conn, pstring s, const char *dir,BOOL widelinks)
+NTSTATUS check_reduced_name(connection_struct *conn, const char *fname)
 {
-#ifndef REDUCE_PATHS
-       return True;
+#ifdef REALPATH_TAKES_NULL
+       bool free_resolved_name = True;
 #else
-       pstring dir2;
-       pstring wd;
-       pstring base_name;
-       pstring newname;
-       char *p=NULL;
-       BOOL relative = (*s != '/');
-
-       *dir2 = *wd = *base_name = *newname = 0;
-
-       if (widelinks) {
-               unix_clean_name(s);
-               /* can't have a leading .. */
-               if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/')) {
-                       DEBUG(3,("Illegal file name? (%s)\n",s));
-                       return(False);
-               }
-
-               if (strlen(s) == 0)
-                       pstrcpy(s,"./");
-
-               return(True);
-       }
-
-       DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
-
-       /* remove any double slashes */
-       all_string_sub(s,"//","/",0);
-
-       pstrcpy(base_name,s);
-       p = strrchr_m(base_name,'/');
-
-       if (!p)
-               return readlink_check(conn, dir, s);
-
-       if (!vfs_GetWd(conn,wd)) {
-               DEBUG(0,("couldn't vfs_GetWd for %s %s\n",s,dir));
-               return(False);
-       }
-
-       if (vfs_ChDir(conn,dir) != 0) {
-               DEBUG(0,("couldn't vfs_ChDir to %s\n",dir));
-               return(False);
-       }
+        char resolved_name_buf[PATH_MAX+1];
+       bool free_resolved_name = False;
+#endif
+       char *resolved_name = NULL;
+       size_t con_path_len = strlen(conn->connectpath);
+       char *p = NULL;
 
-       if (!vfs_GetWd(conn,dir2)) {
-               DEBUG(0,("couldn't vfs_GetWd for %s\n",dir));
-               vfs_ChDir(conn,wd);
-               return(False);
-       }
+       DEBUG(3,("reduce_name [%s] [%s]\n", fname, conn->connectpath));
 
-       if (p && (p != base_name)) {
-               *p = 0;
-               if (strcmp(p+1,".")==0)
-                       p[1]=0;
-               if (strcmp(p+1,"..")==0)
-                       *p = '/';
-       }
+#ifdef REALPATH_TAKES_NULL
+       resolved_name = SMB_VFS_REALPATH(conn,fname,NULL);
+#else
+       resolved_name = SMB_VFS_REALPATH(conn,fname,resolved_name_buf);
+#endif
 
-       if (vfs_ChDir(conn,base_name) != 0) {
-               vfs_ChDir(conn,wd);
-               DEBUG(3,("couldn't vfs_ChDir for %s %s basename=%s\n",s,dir,base_name));
-               return(False);
-       }
+       if (!resolved_name) {
+               switch (errno) {
+                       case ENOTDIR:
+                               DEBUG(3,("reduce_name: Component not a directory in getting realpath for %s\n", fname));
+                               return map_nt_error_from_unix(errno);
+                       case ENOENT:
+                       {
+                               TALLOC_CTX *tmp_ctx = talloc_stackframe();
+                               char *tmp_fname = NULL;
+                               char *last_component = NULL;
+                               /* Last component didn't exist. Remove it and try and canonicalise the directory. */
+
+                               tmp_fname = talloc_strdup(tmp_ctx, fname);
+                               if (!tmp_fname) {
+                                       TALLOC_FREE(tmp_ctx);
+                                       return NT_STATUS_NO_MEMORY;
+                               }
+                               p = strrchr_m(tmp_fname, '/');
+                               if (p) {
+                                       *p++ = '\0';
+                                       last_component = p;
+                               } else {
+                                       last_component = tmp_fname;
+                                       tmp_fname = talloc_strdup(tmp_ctx,
+                                                       ".");
+                                       if (!tmp_fname) {
+                                               TALLOC_FREE(tmp_ctx);
+                                               return NT_STATUS_NO_MEMORY;
+                                       }
+                               }
 
-       if (!vfs_GetWd(conn,newname)) {
-               vfs_ChDir(conn,wd);
-               DEBUG(2,("couldn't get vfs_GetWd for %s %s\n",s,base_name));
-               return(False);
+#ifdef REALPATH_TAKES_NULL
+                               resolved_name = SMB_VFS_REALPATH(conn,tmp_fname,NULL);
+#else
+                               resolved_name = SMB_VFS_REALPATH(conn,tmp_fname,resolved_name_buf);
+#endif
+                               if (!resolved_name) {
+                                       DEBUG(3,("reduce_name: couldn't get realpath for %s\n", fname));
+                                       TALLOC_FREE(tmp_ctx);
+                                       return map_nt_error_from_unix(errno);
+                               }
+                               tmp_fname = talloc_asprintf(tmp_ctx,
+                                               "%s/%s",
+                                               resolved_name,
+                                               last_component);
+                               if (!tmp_fname) {
+                                       TALLOC_FREE(tmp_ctx);
+                                       return NT_STATUS_NO_MEMORY;
+                               }
+#ifdef REALPATH_TAKES_NULL
+                               SAFE_FREE(resolved_name);
+                               resolved_name = SMB_STRDUP(tmp_fname);
+                               if (!resolved_name) {
+                                       DEBUG(0,("reduce_name: malloc fail for %s\n", tmp_fname));
+                                       return NT_STATUS_NO_MEMORY;
+                               }
+#else
+                               safe_strcpy(resolved_name_buf, tmp_fname, PATH_MAX);
+                               resolved_name = resolved_name_buf;
+#endif
+                               TALLOC_FREE(tmp_ctx);
+                               break;
+                       }
+                       default:
+                               DEBUG(1,("reduce_name: couldn't get realpath for %s\n", fname));
+                               return map_nt_error_from_unix(errno);
+               }
        }
 
-       if (p && (p != base_name)) {
-               pstrcat(newname,"/");
-               pstrcat(newname,p+1);
-       }
+       DEBUG(10,("reduce_name realpath [%s] -> [%s]\n", fname, resolved_name));
 
-       {
-               size_t l = strlen(dir2);
-               if (dir2[l-1] == '/')
-                       l--;
-
-               if (strncmp(newname,dir2,l) != 0) {
-                       vfs_ChDir(conn,wd);
-                       DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,(int)l));
-                       return(False);
+       if (*resolved_name != '/') {
+               DEBUG(0,("reduce_name: realpath doesn't return absolute paths !\n"));
+               if (free_resolved_name) {
+                       SAFE_FREE(resolved_name);
                }
+               return NT_STATUS_OBJECT_NAME_INVALID;
+       }
 
-               if (!readlink_check(conn, dir, newname)) {
-                       DEBUG(2, ("Bad access attemt? %s is a symlink outside the share path", s));
-                       return(False);
+       /* Check for widelinks allowed. */
+       if (!lp_widelinks(SNUM(conn)) && (strncmp(conn->connectpath, resolved_name, con_path_len) != 0)) {
+               DEBUG(2, ("reduce_name: Bad access attempt: %s is a symlink outside the share path", fname));
+               if (free_resolved_name) {
+                       SAFE_FREE(resolved_name);
                }
-
-               if (relative) {
-                       if (newname[l] == '/')
-                               pstrcpy(s,newname + l + 1);
-                       else
-                               pstrcpy(s,newname+l);
-               } else
-                       pstrcpy(s,newname);
+               return NT_STATUS_ACCESS_DENIED;
        }
 
-       vfs_ChDir(conn,wd);
+        /* Check if we are allowing users to follow symlinks */
+        /* Patch from David Clerc <David.Clerc@cui.unige.ch>
+                University of Geneva */
 
-       if (strlen(s) == 0)
-               pstrcpy(s,"./");
-
-       DEBUG(3,("reduced to %s\n",s));
-       return(True);
+#ifdef S_ISLNK
+        if (!lp_symlinks(SNUM(conn))) {
+                SMB_STRUCT_STAT statbuf;
+                if ( (SMB_VFS_LSTAT(conn,fname,&statbuf) != -1) &&
+                                (S_ISLNK(statbuf.st_mode)) ) {
+                       if (free_resolved_name) {
+                               SAFE_FREE(resolved_name);
+                       }
+                        DEBUG(3,("reduce_name: denied: file path name %s is a symlink\n",resolved_name));
+                       return NT_STATUS_ACCESS_DENIED;
+                }
+        }
 #endif
-}
 
+       DEBUG(3,("reduce_name: %s reduced to %s\n", fname, resolved_name));
+       if (free_resolved_name) {
+               SAFE_FREE(resolved_name);
+       }
+       return NT_STATUS_OK;
+}