r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
[abartlet/samba.git/.git] / source3 / smbd / vfs.c
index 1d1e1e45fc043fb534b11029635ebd8ec55ff5aa..0102739fe3992ce587e3c1896c5e2f4463cabb43 100644 (file)
@@ -3,6 +3,7 @@
    Version 1.9.
    VFS initialisation and support functions
    Copyright (C) Tim Potter 1999
+   Copyright (C) Alexander Bokovoy 2002
 
    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
    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.
+
+   This work was sponsored by Optifacio Software Services, Inc.
 */
 
 #include "includes.h"
 
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_VFS
+
+struct vfs_init_function_entry {
+       char *name;
+       vfs_op_tuple *vfs_op_tuples;
+       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 */
 
 struct vfs_syminfo {
@@ -32,145 +46,252 @@ struct vfs_syminfo {
    very important.  They must be in the same order as defined in
    vfs.h.  Change at your own peril. */
 
-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_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_ops default_vfs = {
+
+       {
+               /* Disk operations */
+       
+               vfswrap_dummy_connect,
+               vfswrap_dummy_disconnect,
+               vfswrap_disk_free,
+               vfswrap_get_quota,
+               vfswrap_set_quota,
+               vfswrap_get_shadow_copy_data,
+       
+               /* Directory operations */
+       
+               vfswrap_opendir,
+               vfswrap_readdir,
+               vfswrap_seekdir,
+               vfswrap_telldir,
+               vfswrap_rewinddir,
+               vfswrap_mkdir,
+               vfswrap_rmdir,
+               vfswrap_closedir,
+       
+               /* File operations */
+       
+               vfswrap_open,
+               vfswrap_close,
+               vfswrap_read,
+               vfswrap_pread,
+               vfswrap_write,
+               vfswrap_pwrite,
+               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,
+       
+               /* Windows ACL operations. */
+               vfswrap_fget_nt_acl,
+               vfswrap_get_nt_acl,
+               vfswrap_fset_nt_acl,
+               vfswrap_set_nt_acl,
+       
+               /* POSIX ACL operations. */
+               vfswrap_chmod_acl,
+               vfswrap_fchmod_acl,
+
+               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,
+
+               /* EA operations. */
+               vfswrap_getxattr,
+               vfswrap_lgetxattr,
+               vfswrap_fgetxattr,
+               vfswrap_listxattr,
+               vfswrap_llistxattr,
+               vfswrap_flistxattr,
+               vfswrap_removexattr,
+               vfswrap_lremovexattr,
+               vfswrap_fremovexattr,
+               vfswrap_setxattr,
+               vfswrap_lsetxattr,
+               vfswrap_fsetxattr
+       }
 };
 
 /****************************************************************************
-  initialise default vfs hooks
+    maintain the list of available backends
 ****************************************************************************/
 
-static BOOL vfs_init_default(connection_struct *conn)
+static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
 {
-       DEBUG(3, ("Initialising default vfs hooks\n"));
+       struct vfs_init_function_entry *entry = backends;
+       while(entry) {
+               if (strcmp(entry->name, name)==0) return entry;
+               entry = entry->next;
+       }
 
-       memcpy(&conn->vfs_ops, &default_vfs_ops, sizeof(struct vfs_ops));
-       return True;
+       return NULL;
+}
+
+NTSTATUS smb_register_vfs(int version, const char *name, vfs_op_tuple *vfs_op_tuples)
+{
+       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;
 }
 
 /****************************************************************************
-  initialise custom vfs hooks
+  initialise default vfs hooks
 ****************************************************************************/
 
-static BOOL vfs_init_custom(connection_struct *conn)
+static void vfs_init_default(connection_struct *conn)
 {
-       int vfs_version = -1;
-       struct vfs_ops *ops, *(*init_fptr)(int *, struct vfs_ops *);
+       DEBUG(3, ("Initialising default vfs hooks\n"));
 
-       DEBUG(3, ("Initialising custom vfs hooks from %s\n", lp_vfsobj(SNUM(conn))));
+       memcpy(&conn->vfs.ops, &default_vfs.ops, sizeof(default_vfs.ops));
+       memcpy(&conn->vfs_opaque.ops, &default_vfs.ops, sizeof(default_vfs.ops));
+}
 
-       /* Open object file */
+/****************************************************************************
+  initialise custom vfs hooks
+ ****************************************************************************/
 
-       if ((conn->dl_handle = sys_dlopen(lp_vfsobj(SNUM(conn)), RTLD_NOW | RTLD_GLOBAL)) == NULL) {
-               DEBUG(0, ("Error opening %s: %s\n", lp_vfsobj(SNUM(conn)), sys_dlerror()));
+BOOL vfs_init_custom(connection_struct *conn, const char *vfs_object)
+{
+       vfs_op_tuple *ops;
+       char *module_name = NULL;
+       char *module_param = NULL, *p;
+       int i;
+       vfs_handle_struct *handle;
+       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;
        }
 
-       /* Get handle on vfs_init() symbol */
+       if(!backends) static_init_vfs;
 
-       init_fptr = (struct vfs_ops *(*)(int *, struct vfs_ops *))sys_dlsym(conn->dl_handle, "vfs_init");
+       DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
 
-       if (init_fptr == NULL) {
-               DEBUG(0, ("No vfs_init() symbol found in %s\n", lp_vfsobj(SNUM(conn))));
-               return False;
-       }
+       module_name = smb_xstrdup(vfs_object);
 
-       /* Initialise vfs_ops structure */
+       p = strchr(module_name, ':');
 
-       conn->vfs_ops = default_vfs_ops;
+       if (p) {
+               *p = 0;
+               module_param = p+1;
+               trim_char(module_param, ' ', ' ');
+       }
 
-       if ((ops = init_fptr(&vfs_version, &default_vfs_ops)) == NULL) {
-               DEBUG(0, ("vfs_init function from %s failed\n", lp_vfsobj(SNUM(conn))));
+       trim_char(module_name, ' ', ' ');
+
+       /* 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_name)) && 
+               (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));
+                       SAFE_FREE(module_name);
+                       return False;
+               }
+       } else {
+               DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
+               SAFE_FREE(module_name);
                return False;
        }
 
-       if (vfs_version != SMB_VFS_INTERFACE_VERSION) {
-               DEBUG(0, ("vfs_init returned wrong interface version info (was %d, should be %d)\n",
-                       vfs_version, SMB_VFS_INTERFACE_VERSION ));
+       handle = TALLOC_ZERO_P(conn->mem_ctx,vfs_handle_struct);
+       if (!handle) {
+               DEBUG(0,("talloc_zero() failed!\n"));
+               SAFE_FREE(module_name);
                return False;
        }
-
-       if (ops != &conn->vfs_ops) {
-               memcpy(&conn->vfs_ops, ops, sizeof(struct vfs_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) {
+           /* Check whether this operation was already made opaque by different module */
+           if(((void**)&conn->vfs_opaque.ops)[ops[i].type] == ((void**)&default_vfs.ops)[ops[i].type]) {
+             /* No, it isn't overloaded yet. Overload. */
+             DEBUGADD(5, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object));
+             ((void**)&conn->vfs_opaque.ops)[ops[i].type] = ops[i].op;
+             ((vfs_handle_struct **)&conn->vfs_opaque.handles)[ops[i].type] = handle;
+           }
+         }
+         /* Change current VFS disposition*/
+         DEBUGADD(5, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object));
+         ((void**)&conn->vfs.ops)[ops[i].type] = ops[i].op;
+         ((vfs_handle_struct **)&conn->vfs.handles)[ops[i].type] = handle;
        }
 
+       SAFE_FREE(module_name);
        return True;
 }
 
@@ -180,21 +301,29 @@ static BOOL vfs_init_custom(connection_struct *conn)
 
 BOOL smbd_vfs_init(connection_struct *conn)
 {
-       if (*lp_vfsobj(SNUM(conn))) {
-               /* Loadable object file */
-               if (!vfs_init_custom(conn)) {
-                       DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed\n"));
-                       return False;
-               }
+       const char **vfs_objects;
+       unsigned int i = 0;
+       int j = 0;
+       
+       /* Normal share - initialise with disk access functions */
+       vfs_init_default(conn);
+       vfs_objects = lp_vfs_objects(SNUM(conn));
 
+       /* Override VFS functions if 'vfs object' was not specified*/
+       if (!vfs_objects || !vfs_objects[0])
                return True;
+       
+       for (i=0; vfs_objects[i] ;) {
+               i++;
        }
-       /* Normal share - initialise with disk access functions */
-       return vfs_init_default(conn);
+
+       for (j=i-1; j >= 0; j--) {
+               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;
+               }
+       }
+       return True;
 }
 
 /*******************************************************************
@@ -209,7 +338,7 @@ BOOL vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_
        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);
@@ -219,32 +348,27 @@ BOOL vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_
        return ret;
 }
 
-/*******************************************************************
- vfs getwd wrapper 
-********************************************************************/
-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 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))) {
+       if(!(ret=SMB_VFS_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));
+                               !SMB_VFS_STAT(conn,name,&sbuf) && (mode & ~sbuf.st_mode))
+                       SMB_VFS_CHMOD(conn,name,sbuf.st_mode | (mode & ~sbuf.st_mode));
        }
        return ret;
 }
@@ -262,7 +386,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;
 }
@@ -280,7 +404,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));
 }
@@ -295,7 +419,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->fd, buf + total,
                                        byte_count - total);
 
                if (ret == 0) return total;
@@ -310,6 +434,28 @@ 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, fsp->fd, 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.
 ****************************************************************************/
@@ -320,7 +466,7 @@ ssize_t vfs_write_data(files_struct *fsp,const char *buffer,size_t N)
        ssize_t ret;
 
        while (total < N) {
-               ret = fsp->conn->vfs_ops.write(fsp,fsp->fd,buffer + total,N - total);
+               ret = SMB_VFS_WRITE(fsp,fsp->fd,buffer + total,N - total);
 
                if (ret == -1)
                        return -1;
@@ -332,19 +478,37 @@ ssize_t vfs_write_data(files_struct *fsp,const char *buffer,size_t N)
        return (ssize_t)total;
 }
 
+ssize_t vfs_pwrite_data(files_struct *fsp,const char *buffer,
+                size_t N, SMB_OFF_T offset)
+{
+       size_t total=0;
+       ssize_t ret;
+
+       while (total < N) {
+               ret = SMB_VFS_PWRITE(fsp, fsp->fd, 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.
  Returns 0 on success, -1 on failure.
 ****************************************************************************/
 
-int vfs_allocate_file_space(files_struct *fsp, SMB_OFF_T len)
+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_OFF_T space_avail;
+       SMB_BIG_UINT space_avail;
        SMB_BIG_UINT bsize,dfree,dsize;
 
        release_level_2_oplocks_on_change(fsp);
@@ -355,21 +519,26 @@ int vfs_allocate_file_space(files_struct *fsp, SMB_OFF_T len)
 
        DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n", fsp->fsp_name, (double)len ));
 
-       ret = vfs_fstat(fsp,fsp->fd,&st);
+       if (((SMB_OFF_T)len) < 0) {
+               DEBUG(0,("vfs_allocate_file_space: %s negative len requested.\n", fsp->fsp_name ));
+               return -1;
+       }
+
+       ret = SMB_VFS_FSTAT(fsp,fsp->fd,&st);
        if (ret == -1)
                return ret;
 
-       if (len == st.st_size)
+       if (len == (SMB_BIG_UINT)st.st_size)
                return 0;
 
-       if (len < st.st_size) {
+       if (len < (SMB_BIG_UINT)st.st_size) {
                /* Shrink - use ftruncate. */
 
                DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current size %.0f\n",
                                fsp->fsp_name, (double)st.st_size ));
 
                flush_write_cache(fsp, SIZECHANGE_FLUSH);
-               if ((ret = vfs_ops->ftruncate(fsp, fsp->fd, len)) != -1) {
+               if ((ret = SMB_VFS_FTRUNCATE(fsp, fsp->fd, (SMB_OFF_T)len)) != -1) {
                        set_filelen_write_cache(fsp, len);
                }
                return ret;
@@ -382,10 +551,10 @@ int vfs_allocate_file_space(files_struct *fsp, SMB_OFF_T len)
 
        len -= st.st_size;
        len /= 1024; /* Len is now number of 1k blocks needed. */
-       space_avail = (SMB_OFF_T)conn->vfs_ops.disk_free(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
+       space_avail = SMB_VFS_DISK_FREE(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
 
-       DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, needed blocks = %lu, space avail = %lu\n",
-                       fsp->fsp_name, (double)st.st_size, (unsigned long)len, (unsigned long)space_avail ));
+       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 ));
 
        if (len > space_avail) {
                errno = ENOSPC;
@@ -408,7 +577,7 @@ 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->fd, len)) != -1)
                set_filelen_write_cache(fsp, len);
 
        return ret;
@@ -423,12 +592,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)
@@ -445,13 +614,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,p);
        if (!ptr)
                return(NULL);
 
@@ -470,77 +639,11 @@ 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().
 ********************************************************************/
 
-int vfs_ChDir(connection_struct *conn, char *path)
+int vfs_ChDir(connection_struct *conn, const char *path)
 {
        int res;
        static pstring LastDir="";
@@ -551,9 +654,9 @@ int vfs_ChDir(connection_struct *conn, char *path)
        if (*path == '/' && strcsequal(LastDir,path))
                return(0);
 
-       DEBUG(3,("vfs_ChDir to %s\n",path));
+       DEBUG(4,("vfs_ChDir to %s\n",path));
 
-       res = vfs_chdir(conn,path);
+       res = SMB_VFS_CHDIR(conn,path);
        if (!res)
                pstrcpy(LastDir,path);
        return(res);
@@ -562,7 +665,7 @@ int vfs_ChDir(connection_struct *conn, char *path)
 /* number of list structures for a caching GetWd function. */
 #define MAX_GETWDCACHE (50)
 
-struct {
+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. */
@@ -581,7 +684,7 @@ static void array_promote(char *array,int elsize,int element)
        if (element == 0)
                return;
 
-       p = (char *)malloc(elsize);
+       p = (char *)SMB_MALLOC(elsize);
 
        if (!p) {
                DEBUG(5,("array_promote: malloc fail\n"));
@@ -610,7 +713,7 @@ char *vfs_GetWd(connection_struct *conn, char *path)
        *s = 0;
 
        if (!use_getwd_cache)
-               return(vfs_getwd(conn,path));
+               return(SMB_VFS_GETWD(conn,path));
 
        /* init the cache */
        if (!getwd_cache_init) {
@@ -624,9 +727,9 @@ char *vfs_GetWd(connection_struct *conn, char *path)
        /*  Get the inode of the current directory, if this doesn't work we're
                in trouble :-) */
 
-       if (vfs_stat(conn, ".",&st) == -1) {
+       if (SMB_VFS_STAT(conn, ".",&st) == -1) {
                DEBUG(0,("Very strange, couldn't stat \".\" path=%s\n", path));
-               return(vfs_getwd(conn,path));
+               return(SMB_VFS_GETWD(conn,path));
        }
 
 
@@ -640,7 +743,7 @@ char *vfs_GetWd(connection_struct *conn, char *path)
                                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 (SMB_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);
@@ -662,8 +765,8 @@ char *vfs_GetWd(connection_struct *conn, char *path)
                The very slow getcwd, which spawns a process on some systems, or the
                not quite so bad getwd. */
 
-       if (!vfs_getwd(conn,s)) {
-               DEBUG(0,("vfs_GetWd: vfs_getwd call failed, errno %s\n",strerror(errno)));
+       if (!SMB_VFS_GETWD(conn,s)) {
+               DEBUG(0,("vfs_GetWd: SMB_VFS_GETWD call failed, errno %s\n",strerror(errno)));
                return (NULL);
        }
 
@@ -684,119 +787,162 @@ char *vfs_GetWd(connection_struct *conn, char *path)
        return (path);
 }
 
-/*******************************************************************
- 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.
-********************************************************************/
-
-BOOL reduce_name(connection_struct *conn, char *s,char *dir,BOOL widelinks)
+BOOL canonicalize_path(connection_struct *conn, pstring path)
 {
-#ifndef REDUCE_PATHS
+#ifdef REALPATH_TAKES_NULL
+       char *resolved_name = SMB_VFS_REALPATH(conn,path,NULL);
+       if (!resolved_name) {
+               return False;
+       }
+       pstrcpy(path, resolved_name);
+       SAFE_FREE(resolved_name);
        return 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);
+#ifdef PATH_MAX
+        char resolved_name_buf[PATH_MAX+1];
+#else
+        pstring resolved_name_buf;
+#endif
+       char *resolved_name = SMB_VFS_REALPATH(conn,path,resolved_name_buf);
+       if (!resolved_name) {
+               return False;
        }
+       pstrcpy(path, resolved_name);
+       return True;
+#endif /* REALPATH_TAKES_NULL */
+}
 
-       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(True);
+/*******************************************************************
+ Reduce a file name, removing .. elements and checking that
+ it is below dir in the heirachy. This uses realpath.
+********************************************************************/
 
-       if (!vfs_GetWd(conn,wd)) {
-               DEBUG(0,("couldn't vfs_GetWd for %s %s\n",s,dir));
-               return(False);
-       }
+BOOL reduce_name(connection_struct *conn, const pstring fname)
+{
+#ifdef REALPATH_TAKES_NULL
+       BOOL free_resolved_name = True;
+#else
+#ifdef PATH_MAX
+        char resolved_name_buf[PATH_MAX+1];
+#else
+        pstring resolved_name_buf;
+#endif
+       BOOL free_resolved_name = False;
+#endif
+       char *resolved_name = NULL;
+       size_t con_path_len = strlen(conn->connectpath);
+       char *p = NULL;
+       int saved_errno = errno;
 
-       if (vfs_ChDir(conn,dir) != 0) {
-               DEBUG(0,("couldn't vfs_ChDir to %s\n",dir));
-               return(False);
-       }
+       DEBUG(3,("reduce_name [%s] [%s]\n", fname, conn->connectpath));
 
-       if (!vfs_GetWd(conn,dir2)) {
-               DEBUG(0,("couldn't vfs_GetWd for %s\n",dir));
-               vfs_ChDir(conn,wd);
-               return(False);
-       }
+#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 (p && (p != base_name)) {
-               *p = 0;
-               if (strcmp(p+1,".")==0)
-                       p[1]=0;
-               if (strcmp(p+1,"..")==0)
-                       *p = '/';
-       }
+       if (!resolved_name) {
+               switch (errno) {
+                       case ENOTDIR:
+                               DEBUG(3,("reduce_name: Component not a directory in getting realpath for %s\n", fname));
+                               errno = saved_errno;
+                               return False;
+                       case ENOENT:
+                       {
+                               pstring tmp_fname;
+                               fstring last_component;
+                               /* Last component didn't exist. Remove it and try and canonicalise the directory. */
+
+                               pstrcpy(tmp_fname, fname);
+                               p = strrchr_m(tmp_fname, '/');
+                               if (p) {
+                                       *p++ = '\0';
+                                       fstrcpy(last_component, p);
+                               } else {
+                                       fstrcpy(last_component, tmp_fname);
+                                       pstrcpy(tmp_fname, ".");
+                               }
 
-       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);
+#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));
+                                       errno = saved_errno;
+                                       return False;
+                               }
+                               pstrcpy(tmp_fname, resolved_name);
+                               pstrcat(tmp_fname, "/");
+                               pstrcat(tmp_fname, last_component);
+#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));
+                                       errno = saved_errno;
+                                       return False;
+                               }
+#else
+#ifdef PATH_MAX
+                               safe_strcpy(resolved_name_buf, tmp_fname, PATH_MAX);
+#else
+                               pstrcpy(pstring resolved_name_buf, tmp_fname);
+#endif
+                               resolved_name = resolved_name_buf;
+#endif
+                               break;
+                       }
+                       default:
+                               DEBUG(1,("reduce_name: couldn't get realpath for %s\n", fname));
+                               /* Don't restore the saved errno. We need to return the error that
+                                  realpath caused here as it was not one of the cases we handle. JRA. */
+                               return False;
+               }
        }
 
-       if (!vfs_GetWd(conn,newname)) {
-               vfs_ChDir(conn,wd);
-               DEBUG(2,("couldn't get vfs_GetWd for %s %s\n",s,dir2));
-               return(False);
-       }
+       DEBUG(10,("reduce_name realpath [%s] -> [%s]\n", fname, resolved_name));
 
-       if (p && (p != base_name)) {
-               pstrcat(newname,"/");
-               pstrcat(newname,p+1);
+       if (*resolved_name != '/') {
+               DEBUG(0,("reduce_name: realpath doesn't return absolute paths !\n"));
+               if (free_resolved_name)
+                       SAFE_FREE(resolved_name);
+               errno = saved_errno;
+               return False;
        }
 
-       {
-               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 (relative) {
-                       if (newname[l] == '/')
-                               pstrcpy(s,newname + l + 1);
-                       else
-                               pstrcpy(s,newname+l);
-               } else
-                       pstrcpy(s,newname);
+       /* 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);
+               errno = EACCES;
+               return False;
        }
 
-       vfs_ChDir(conn,wd);
-
-       if (strlen(s) == 0)
-               pstrcpy(s,"./");
+        /* Check if we are allowing users to follow symlinks */
+        /* Patch from David Clerc <David.Clerc@cui.unige.ch>
+                University of Geneva */
+                                                                                                                                                    
+#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));
+                        errno = EACCES;
+                       return False;
+                }
+        }
+#endif
 
-       DEBUG(3,("reduced to %s\n",s));
+       DEBUG(3,("reduce_name: %s reduced to %s\n", fname, p));
+       if (free_resolved_name)
+               SAFE_FREE(resolved_name);
+       errno = saved_errno;
        return(True);
-#endif
 }