r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
[samba.git] / source3 / smbd / open.c
index dea7edc528ee4612c4c7f843ed5f1d002141a2b7..bf3fbf7fecd556afbed62caaf134432421731a8b 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/CIFS implementation.
    file opening and share modes
    Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Jeremy Allison 2001
+   Copyright (C) Jeremy Allison 2001-2004
    
    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
 
 extern userdom_struct current_user_info;
 extern uint16 global_oplock_port;
+extern uint16 global_smbpid;
 extern BOOL global_client_failed_oplock_break;
 
+struct dev_inode_bundle {
+       SMB_DEV_T dev;
+       SMB_INO_T inode;
+};
+
 /****************************************************************************
  fd support routines - attempt to do a dos_open.
 ****************************************************************************/
 
-static int fd_open(struct connection_struct *conn, char *fname, 
+static int fd_open(struct connection_struct *conn, const char *fname, 
                   int flags, mode_t mode)
 {
        int fd;
@@ -38,14 +44,7 @@ static int fd_open(struct connection_struct *conn, char *fname,
                flags |= O_NOFOLLOW;
 #endif
 
-       fd = conn->vfs_ops.open(conn,fname,flags,mode);
-
-       /* Fix for files ending in '.' */
-       if((fd == -1) && (errno == ENOENT) &&
-          (strchr_m(fname,'.')==NULL)) {
-               pstrcat(fname,".");
-               fd = conn->vfs_ops.open(conn,fname,flags,mode);
-       }
+       fd = SMB_VFS_OPEN(conn,fname,flags,mode);
 
        DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n", fname,
                flags, (int)mode, fd, (fd == -1) ? strerror(errno) : "" ));
@@ -69,16 +68,17 @@ int fd_close(struct connection_struct *conn, files_struct *fsp)
  Check a filename for the pipe string.
 ****************************************************************************/
 
-static void check_for_pipe(char *fname)
+static void check_for_pipe(const char *fname)
 {
        /* special case of pipe opens */
        char s[10];
        StrnCpy(s,fname,sizeof(s)-1);
-       strlower(s);
+       strlower_m(s);
        if (strstr(s,"pipe/")) {
                DEBUG(3,("Rejecting named pipe open for %s\n",fname));
                unix_ERR_class = ERRSRV;
                unix_ERR_code = ERRaccess;
+               unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
        }
 }
 
@@ -87,10 +87,9 @@ static void check_for_pipe(char *fname)
 ****************************************************************************/
 
 static BOOL open_file(files_struct *fsp,connection_struct *conn,
-                     const char *fname1,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access)
+                     const char *fname,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access)
 {
        extern struct current_user current_user;
-       pstring fname;
        int accmode = (flags & O_ACCMODE);
        int local_flags = flags;
 
@@ -98,8 +97,6 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->oplock_type = NO_OPLOCK;
        errno = EPERM;
 
-       pstrcpy(fname,fname1);
-
        /* Check permissions */
 
        /*
@@ -124,6 +121,7 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
                           directory.
                        */
                        flags &= ~O_CREAT;
+                       local_flags &= ~O_CREAT;
                }
        }
 
@@ -165,19 +163,27 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
                        local_flags |= O_NONBLOCK;
 #endif
 
+               /* Don't create files with Microsoft wildcard characters. */
+               if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf) && ms_has_wild(fname))  {
+                       unix_ERR_class = ERRDOS;
+                       unix_ERR_code = ERRinvalidname;
+                       unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
+                       return False;
+               }
+
                /* Actually do the open */
                fsp->fd = fd_open(conn, fname, local_flags, mode);
-
-               /* Inherit the ACL if the file was created. */
-               if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf))
-                       inherit_access_acl(conn, fname, mode);
-
                if (fsp->fd == -1)  {
                        DEBUG(3,("Error opening file %s (%s) (local_flags=%d) (flags=%d)\n",
                                 fname,strerror(errno),local_flags,flags));
                        check_for_pipe(fname);
                        return False;
                }
+
+               /* Inherit the ACL if the file was created. */
+               if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf))
+                       inherit_access_acl(conn, fname, mode);
+
        } else
                fsp->fd = -1; /* What we used to call a stat open. */
 
@@ -185,9 +191,9 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
                int ret;
 
                if (fsp->fd == -1)
-                       ret = vfs_stat(conn, fname, psbuf);
+                       ret = SMB_VFS_STAT(conn, fname, psbuf);
                else {
-                       ret = vfs_fstat(fsp,fsp->fd,psbuf);
+                       ret = SMB_VFS_FSTAT(fsp,fsp->fd,psbuf);
                        /* If we have an fd, this stat should succeed. */
                        if (ret == -1)
                                DEBUG(0,("Error doing fstat on open file %s (%s)\n", fname,strerror(errno) ));
@@ -216,8 +222,8 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->inode = psbuf->st_ino;
        fsp->dev = psbuf->st_dev;
        fsp->vuid = current_user.vuid;
+       fsp->file_pid = global_smbpid;
        fsp->size = psbuf->st_size;
-       fsp->pos = -1;
        fsp->can_lock = True;
        fsp->can_read = ((flags & O_WRONLY)==0);
        fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
@@ -230,7 +236,6 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->is_directory = False;
        fsp->is_stat = False;
        fsp->directory_delete_on_close = False;
-       fsp->conn = conn;
        string_set(&fsp->fsp_name,fname);
        fsp->wcp = NULL; /* Write cache pointer. */
 
@@ -239,32 +244,14 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
                 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
                 conn->num_files_open + 1));
 
+       errno = 0;
        return True;
 }
 
-/****************************************************************************
-  C. Hoch 11/22/95
-  Helper for open_file_shared. 
-  Truncate a file after checking locking; close file if locked.
-  **************************************************************************/
-
-static int truncate_unless_locked(struct connection_struct *conn, files_struct *fsp)
-{
-       SMB_BIG_UINT mask = (SMB_BIG_UINT)-1;
-
-       if (is_locked(fsp,fsp->conn,mask,0,WRITE_LOCK,True)){
-               errno = EACCES;
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRlock;
-               return -1;
-       } else {
-               return conn->vfs_ops.ftruncate(fsp,fsp->fd,0); 
-       }
-}
-
 /*******************************************************************
-return True if the filename is one of the special executable types
+ Return True if the filename is one of the special executable types.
 ********************************************************************/
+
 static BOOL is_executable(const char *fname)
 {
        if ((fname = strrchr_m(fname,'.'))) {
@@ -281,12 +268,13 @@ static BOOL is_executable(const char *fname)
 enum {AFAIL,AREAD,AWRITE,AALL};
 
 /*******************************************************************
-reproduce the share mode access table
-this is horrendoously complex, and really can't be justified on any
-rational grounds except that this is _exactly_ what NT does. See
-the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
-test routines.
+ Reproduce the share mode access table.
+ This is horrendoously complex, and really can't be justified on any
+ rational grounds except that this is _exactly_ what NT does. See
+ the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
+ test routines.
 ********************************************************************/
+
 static int access_table(int new_deny,int old_deny,int old_mode,
                        BOOL same_pid, BOOL isexe)
 {
@@ -372,9 +360,8 @@ static int access_table(int new_deny,int old_deny,int old_mode,
          return(AFAIL);      
 }
 
-
 /****************************************************************************
-check if we can open a file with a share mode
+ Check if we can open a file with a share mode.
 ****************************************************************************/
 
 static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, int share_mode, uint32 desired_access,
@@ -383,6 +370,8 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
        int deny_mode = GET_DENY_MODE(share_mode);
        int old_open_mode = GET_OPEN_MODE(share->share_mode);
        int old_deny_mode = GET_DENY_MODE(share->share_mode);
+       BOOL non_io_open_request;
+       BOOL non_io_open_existing;
 
        /*
         * share modes = false means don't bother to check for
@@ -392,6 +381,18 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
        if(!lp_share_modes(SNUM(conn)))
                return True;
 
+       if (desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
+               non_io_open_request = False;
+       } else {
+               non_io_open_request = True;
+       }
+
+       if (share->desired_access & ~(SYNCHRONIZE_ACCESS|READ_CONTROL_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) {
+               non_io_open_existing = False;
+       } else {
+               non_io_open_existing = True;
+       }
+
        /*
         * Don't allow any opens once the delete on close flag has been
         * set.
@@ -400,9 +401,10 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
        if (GET_DELETE_ON_CLOSE_FLAG(share->share_mode)) {
                DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
                        fname ));
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRnoaccess;
-               unix_ERR_ntstatus = NT_STATUS_DELETE_PENDING;
+               /* Use errno to map to correct error. */
+               unix_ERR_class = SMB_SUCCESS;
+               unix_ERR_code = 0;
+               unix_ERR_ntstatus = NT_STATUS_OK;
                return False;
        }
 
@@ -429,8 +431,7 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
         * and the existing desired_acces then share modes don't conflict.
         */
 
-       if ( !(desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) &&
-               !(share->desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ) {
+       if (non_io_open_request && non_io_open_existing) {
 
                /*
                 * Wrinkle discovered by smbtorture....
@@ -445,6 +446,7 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
                                fname ));
                        unix_ERR_class = ERRDOS;
                        unix_ERR_code = ERRbadshare;
+                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
 
                        return False;
                }
@@ -453,6 +455,13 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
 and existing desired access (0x%x) are non-data opens\n", 
                        fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
                return True;
+       } else if (non_io_open_request || non_io_open_existing) {
+               /*
+                * If either are non-io opens then share modes don't conflict.
+                */
+               DEBUG(5,("check_share_mode: One non-io open. Allowing open on file %s as desired access (0x%x) doesn't conflict with\
+existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access ));
+               return True;
        }
 
        /*
@@ -465,6 +474,7 @@ and existing desired access (0x%x) are non-data opens\n",
                        fname ));
                unix_ERR_class = ERRDOS;
                unix_ERR_code = ERRbadshare;
+               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
 
                return False;
        }
@@ -480,6 +490,7 @@ and existing desired access (0x%x) are non-data opens\n",
                        fname ));
                unix_ERR_class = ERRDOS;
                unix_ERR_code = ERRbadshare;
+               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
 
                return False;
        }
@@ -511,6 +522,7 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
 
                        unix_ERR_class = ERRDOS;
                        unix_ERR_code = ERRbadshare;
+                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
 
                        return False;
                }
@@ -526,6 +538,45 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
        return True;
 }
 
+
+#if defined(DEVELOPER)
+static void validate_my_share_entries(int num, share_mode_entry *share_entry)
+{
+       files_struct *fsp;
+
+       if (share_entry->pid != sys_getpid())
+               return;
+
+       fsp = file_find_dif(share_entry->dev, share_entry->inode, share_entry->share_file_id);
+       if (!fsp) {
+               DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) ));
+               smb_panic("validate_my_share_entries: Cannot match a share entry with an open file\n");
+       }
+
+       if (((uint16)fsp->oplock_type) != share_entry->op_type) {
+               pstring str;
+               DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) ));
+               slprintf(str, sizeof(str)-1, "validate_my_share_entries: file %s, oplock_type = 0x%x, op_type = 0x%x\n",
+                               fsp->fsp_name, (unsigned int)fsp->oplock_type, (unsigned int)share_entry->op_type );
+               smb_panic(str);
+       }
+}
+#endif
+
+struct share_mode_entry_list {
+       struct share_mode_entry_list *next, *prev;
+       share_mode_entry entry;
+};
+
+static void free_broken_entry_list(struct share_mode_entry_list *broken_entry_list)
+{
+       while (broken_entry_list) {
+               struct share_mode_entry_list *broken_entry = broken_entry_list;
+               DLIST_REMOVE(broken_entry_list, broken_entry);
+               SAFE_FREE(broken_entry);
+       }
+}
+
 /****************************************************************************
  Deal with open deny mode and oplock break processing.
  Invarient: Share mode must be locked on entry and exit.
@@ -541,31 +592,46 @@ static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T
        int i;
        int num_share_modes;
        int oplock_contention_count = 0;
-       share_mode_entry *old_shares = 0;
+       share_mode_entry *old_shares = NULL;
        BOOL fcbopen = False;
-       BOOL broke_oplock;      
-       
+       BOOL broke_oplock;
+
        if(GET_OPEN_MODE(share_mode) == DOS_OPEN_FCB)
                fcbopen = True;
        
        num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
        
-       if(num_share_modes == 0)
+       if(num_share_modes == 0) {
+               SAFE_FREE(old_shares);
                return 0;
+       }
        
+       if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
+               ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
+               /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
+               SAFE_FREE(old_shares);
+               return num_share_modes;
+       }
+
        /*
         * Check if the share modes will give us access.
         */
        
        do {
-               share_mode_entry broken_entry;
-               
+               struct share_mode_entry_list *broken_entry_list = NULL;
+               struct share_mode_entry_list *broken_entry = NULL;
+
                broke_oplock = False;
                *p_all_current_opens_are_level_II = True;
                
                for(i = 0; i < num_share_modes; i++) {
+                       BOOL cause_oplock_break = False;
                        share_mode_entry *share_entry = &old_shares[i];
                        
+#if defined(DEVELOPER)
+                       validate_my_share_entries(i, share_entry);
+#endif
+
                        /* 
                         * By observation of NetBench, oplocks are broken *before* share
                         * modes are checked. This allows a file to be closed by the client
@@ -574,14 +640,28 @@ static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T
                         * it before continuing. 
                         */
                        
-                       if((*p_oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
+                       /* Was this a delete this file request ? */
+                       if (!*p_oplock_request && desired_access == DELETE_ACCESS &&
+                                       !BATCH_OPLOCK_TYPE(share_entry->op_type)) {
+                               /* Don't break the oplock in this case. */
+                               cause_oplock_break = False;
+                       } else if((*p_oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
                           (!*p_oplock_request && (share_entry->op_type != NO_OPLOCK))) {
-                               
+                               cause_oplock_break = True;
+                       }
+
+                       if(cause_oplock_break) {
                                BOOL opb_ret;
 
                                DEBUG(5,("open_mode_check: oplock_request = %d, breaking oplock (%x) on file %s, \
 dev = %x, inode = %.0f\n", *p_oplock_request, share_entry->op_type, fname, (unsigned int)dev, (double)inode));
                                
+                               /* Ensure the reply for the open uses the correct sequence number. */
+                               /* This isn't a real deferred packet as it's response will also increment
+                                * the sequence.
+                                */
+                               srv_defer_sign_response(get_current_mid());
+
                                /* Oplock break - unlock to request it. */
                                unlock_share_entry(conn, dev, inode);
                                
@@ -597,56 +677,73 @@ dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (dou
                                        errno = EACCES;
                                        unix_ERR_class = ERRDOS;
                                        unix_ERR_code = ERRbadshare;
+                                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
                                        return -1;
                                }
                                
+                               broken_entry = SMB_MALLOC_P(struct share_mode_entry_list);
+                               if (!broken_entry) {
+                                       smb_panic("open_mode_check: malloc fail.\n");
+                               }
+                               broken_entry->entry = *share_entry;
+                               DLIST_ADD(broken_entry_list, broken_entry);
                                broke_oplock = True;
-                               broken_entry = *share_entry;
-                               break;
                                
                        } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
                                *p_all_current_opens_are_level_II = False;
                        }
-                       
+               } /* end for */
+               
+               if (broke_oplock) {
+                       /* Update the current open table. */
+                       SAFE_FREE(old_shares);
+                       num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
+               }
+
+               /* Now we check the share modes, after any oplock breaks. */
+               for(i = 0; i < num_share_modes; i++) {
+                       share_mode_entry *share_entry = &old_shares[i];
+
                        /* someone else has a share lock on it, check to see if we can too */
                        if (!check_share_mode(conn, share_entry, share_mode, desired_access,
                                                fname, fcbopen, p_flags)) {
                                SAFE_FREE(old_shares);
+                               free_broken_entry_list(broken_entry_list);
                                errno = EACCES;
                                return -1;
                         }
-                       
-               } /* end for */
-               
-               if(broke_oplock) {
-                       SAFE_FREE(old_shares);
-                       num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
+               }
+
+               for(broken_entry = broken_entry_list; broken_entry; broken_entry = broken_entry->next) {
                        oplock_contention_count++;
                        
                        /* Paranoia check that this is no longer an exlusive entry. */
                        for(i = 0; i < num_share_modes; i++) {
                                share_mode_entry *share_entry = &old_shares[i];
                                
-                               if (share_modes_identical(&broken_entry, share_entry) && 
-                                   EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type) ) {
+                               if (share_modes_identical(&broken_entry->entry, share_entry) && 
+                                           EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type) ) {
                                        
                                        /*
                                         * This should not happen. The target left this oplock
                                         * as exlusive.... The process *must* be dead.... 
                                         */
                                        
-                                       DEBUG(0,("open_mode_check: exlusive oplock left by process %d after break ! For file %s, \
-dev = %x, inode = %.0f. Deleting it to continue...\n", (int)broken_entry.pid, fname, (unsigned int)dev, (double)inode));
+                                       DEBUG(0,("open_mode_check: exlusive oplock left by process %d \
+after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n",
+                                               (int)broken_entry->entry.pid, fname, (unsigned int)dev, (double)inode));
                                        
-                                       if (process_exists(broken_entry.pid)) {
-                                               DEBUG(0,("open_mode_check: Existent process %d left active oplock.\n",
-                                                        broken_entry.pid ));
+                                       if (process_exists(broken_entry->entry.pid)) {
+                                               DEBUG(0,("open_mode_check: Existent process %lu left active oplock.\n",
+                                                        (unsigned long)broken_entry->entry.pid ));
                                        }
                                        
-                                       if (del_share_entry(dev, inode, &broken_entry, NULL) == -1) {
+                                       if (del_share_entry(dev, inode, &broken_entry->entry, NULL) == -1) {
+                                               free_broken_entry_list(broken_entry_list);
                                                errno = EACCES;
                                                unix_ERR_class = ERRDOS;
                                                unix_ERR_code = ERRbadshare;
+                                               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
                                                return -1;
                                        }
                                        
@@ -660,13 +757,10 @@ dev = %x, inode = %.0f. Deleting it to continue...\n", (int)broken_entry.pid, fn
                                        break;
                                }
                        } /* end for paranoia... */
-               } /* end if broke_oplock */
-               
+               } /* end for broken_entry */
+               free_broken_entry_list(broken_entry_list);
        } while(broke_oplock);
        
-       if(old_shares != 0)
-               SAFE_FREE(old_shares);
-       
        /*
         * Refuse to grant an oplock in case the contention limit is
         * reached when going through the lock list multiple times.
@@ -678,13 +772,124 @@ dev = %x, inode = %.0f. Deleting it to continue...\n", (int)broken_entry.pid, fn
                         oplock_contention_count ));
        }
        
+       SAFE_FREE(old_shares);
        return num_share_modes;
 }
 
 /****************************************************************************
-set a kernel flock on a file for NFS interoperability
-this requires a patch to Linux
+ Delete the record for a handled deferred open entry.
 ****************************************************************************/
+
+static void delete_defered_open_entry_record(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T inode)
+{
+       uint16 mid = get_current_mid();
+       pid_t mypid = sys_getpid();
+       deferred_open_entry *de_array = NULL;
+       int num_de_entries, i;
+
+       if (!lp_defer_sharing_violations()) {
+               return;
+       }
+
+       num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
+       for (i = 0; i < num_de_entries; i++) {
+               deferred_open_entry *entry = &de_array[i];
+               if (entry->pid == mypid && entry->mid == mid && entry->dev == dev &&
+                       entry->inode == inode) {
+
+                       /* Remove the deferred open entry from the array. */
+                       delete_deferred_open_entry(entry);
+                       SAFE_FREE(de_array);
+                       return;
+               }
+       }
+       SAFE_FREE(de_array);
+}
+
+/****************************************************************************
+ Handle the 1 second delay in returning a SHARING_VIOLATION error.
+****************************************************************************/
+
+void defer_open_sharing_error(connection_struct *conn, struct timeval *ptv,
+               char *fname, SMB_DEV_T dev, SMB_INO_T inode)
+{
+       uint16 mid = get_current_mid();
+       pid_t mypid = sys_getpid();
+       deferred_open_entry *de_array = NULL;
+       int num_de_entries, i;
+       struct dev_inode_bundle dib;
+
+       if (!lp_defer_sharing_violations()) {
+               return;
+       }
+
+       dib.dev = dev;
+       dib.inode = inode;
+
+       num_de_entries = get_deferred_opens(conn, dev, inode, &de_array);
+       for (i = 0; i < num_de_entries; i++) {
+               deferred_open_entry *entry = &de_array[i];
+               if (entry->pid == mypid && entry->mid == mid) {
+                       /*
+                        * Check if a 1 second timeout has expired.
+                        */
+                       if (usec_time_diff(ptv, &entry->time) > SHARING_VIOLATION_USEC_WAIT) {
+                               DEBUG(10,("defer_open_sharing_error: Deleting deferred open entry for mid %u, \
+file %s\n",
+                                       (unsigned int)mid, fname ));
+
+                               /* Expired, return a real error. */
+                               /* Remove the deferred open entry from the array. */
+
+                               delete_deferred_open_entry(entry);
+                               SAFE_FREE(de_array);
+                               return;
+                       }
+                       /*
+                        * If the timeout hasn't expired yet and we still have a sharing violation,
+                        * just leave the entry in the deferred open array alone. We do need to
+                        * reschedule this open call though (with the original created time).
+                        */
+                       DEBUG(10,("defer_open_sharing_error: time [%u.%06u] updating \
+deferred open entry for mid %u, file %s\n",
+                               (unsigned int)entry->time.tv_sec,
+                               (unsigned int)entry->time.tv_usec,
+                               (unsigned int)mid, fname ));
+
+                       push_sharing_violation_open_smb_message(&entry->time, (char *)&dib, sizeof(dib));
+                       SAFE_FREE(de_array);
+                       return;
+               }
+       }
+
+       DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred open entry for mid %u, file %s\n",
+               (unsigned int)ptv->tv_sec, (unsigned int)ptv->tv_usec, (unsigned int)mid, fname ));
+
+       if (!push_sharing_violation_open_smb_message(ptv, (char *)&dib, sizeof(dib))) {
+               SAFE_FREE(de_array);
+               return;
+       }
+       if (!add_deferred_open(mid, ptv, dev, inode, global_oplock_port, fname)) {
+               remove_sharing_violation_open_smb_message(mid);
+       }
+
+       /*
+        * Push the MID of this packet on the signing queue.
+        * We only do this once, the first time we push the packet
+        * onto the deferred open queue, as this has a side effect
+        * of incrementing the response sequence number.
+        */
+
+       srv_defer_sign_response(mid);
+
+       SAFE_FREE(de_array);
+}
+
+/****************************************************************************
+ Set a kernel flock on a file for NFS interoperability.
+ This requires a patch to Linux.
+****************************************************************************/
+
 static void kernel_flock(files_struct *fsp, int deny_mode)
 {
 #if HAVE_KERNEL_SHARE_MODES
@@ -698,20 +903,10 @@ static void kernel_flock(files_struct *fsp, int deny_mode)
 }
 
 
-static BOOL open_match_attributes(connection_struct *conn, char *path, mode_t existing_mode,
-               mode_t new_mode, mode_t *returned_mode)
+static BOOL open_match_attributes(connection_struct *conn, const char *path, uint32 old_dos_mode, uint32 new_dos_mode,
+               mode_t existing_mode, mode_t new_mode, mode_t *returned_mode)
 {
-       uint32 old_dos_mode, new_dos_mode;
        uint32 noarch_old_dos_mode, noarch_new_dos_mode;
-       SMB_STRUCT_STAT sbuf;
-
-       ZERO_STRUCT(sbuf);
-
-       sbuf.st_mode = existing_mode;
-       old_dos_mode = dos_mode(conn, path, &sbuf);
-
-       sbuf.st_mode = new_mode;
-       new_dos_mode = dos_mode(conn, path, &sbuf);
 
        noarch_old_dos_mode = (old_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE);
        noarch_new_dos_mode = (new_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE);
@@ -727,11 +922,11 @@ static BOOL open_match_attributes(connection_struct *conn, char *path, mode_t ex
                old_dos_mode, (unsigned int)existing_mode, new_dos_mode, (unsigned int)*returned_mode ));
 
        /* If we're mapping SYSTEM and HIDDEN ensure they match. */
-       if (lp_map_system(SNUM(conn))) {
+       if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
                if ((old_dos_mode & FILE_ATTRIBUTE_SYSTEM) && !(new_dos_mode & FILE_ATTRIBUTE_SYSTEM))
                        return False;
        }
-       if (lp_map_hidden(SNUM(conn))) {
+       if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
                if ((old_dos_mode & FILE_ATTRIBUTE_HIDDEN) && !(new_dos_mode & FILE_ATTRIBUTE_HIDDEN))
                        return False;
        }
@@ -743,10 +938,10 @@ static BOOL open_match_attributes(connection_struct *conn, char *path, mode_t ex
 ****************************************************************************/
 
 files_struct *open_file_shared(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf, 
-                              int share_mode,int ofun, mode_t mode,int oplock_request, 
+                              int share_mode,int ofun, uint32 new_dos_mode, int oplock_request, 
                               int *Access,int *action)
 {
-       return open_file_shared1(conn, fname, psbuf, 0, share_mode, ofun, mode, 
+       return open_file_shared1(conn, fname, psbuf, 0, share_mode, ofun, new_dos_mode,
                                 oplock_request, Access, action);
 }
 
@@ -756,8 +951,9 @@ files_struct *open_file_shared(connection_struct *conn,char *fname, SMB_STRUCT_S
 
 files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf, 
                                uint32 desired_access, 
-                               int share_mode,int ofun, mode_t mode,int oplock_request, 
-                               int *Access,int *action)
+                               int share_mode,int ofun, uint32 new_dos_mode,
+                               int oplock_request, 
+                               int *Access,int *paction)
 {
        int flags=0;
        int flags2=0;
@@ -767,6 +963,8 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
        BOOL file_existed = VALID_STAT(*psbuf);
        BOOL fcbopen = False;
        BOOL def_acl = False;
+       BOOL add_share_mode = True;
+       BOOL internal_only_open = False;
        SMB_DEV_T dev = 0;
        SMB_INO_T inode = 0;
        int num_share_modes = 0;
@@ -776,14 +974,59 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
        int open_mode=0;
        uint16 port = 0;
        mode_t new_mode = (mode_t)0;
+       int action;
+       uint32 existing_dos_mode = 0;
+       struct pending_message_list *pml = NULL;
+       uint16 mid = get_current_mid();
+       /* We add aARCH to this as this mode is only used if the file is created new. */
+       mode_t mode = unix_mode(conn,new_dos_mode | aARCH,fname, True);
+
+       if (oplock_request == INTERNAL_OPEN_ONLY) {
+               internal_only_open = True;
+               oplock_request = 0;
+       }
+
+       if ((pml = get_open_deferred_message(mid)) != NULL) {
+               struct dev_inode_bundle dib;
+
+               memcpy(&dib, pml->private_data.data, sizeof(dib));
+
+               /* There could be a race condition where the dev/inode pair
+                       has changed since we deferred the message. If so, just
+                       remove the deferred open entry and return sharing violation. */
+
+               /* If the timeout value is non-zero, we need to just
+                       return sharing violation. Don't retry the open
+                       as we were not notified of a close and we don't want to
+                       trigger another spurious oplock break. */
+
+               if (!file_existed || dib.dev != psbuf->st_dev || dib.inode != psbuf->st_ino ||
+                               pml->msg_time.tv_sec || pml->msg_time.tv_usec) {
+                       /* Ensure we don't reprocess this message. */
+                       remove_sharing_violation_open_smb_message(mid);
+
+                       /* Now remove the deferred open entry under lock. */
+                       lock_share_entry(conn, dib.dev, dib.inode);
+                       delete_defered_open_entry_record(conn, dib.dev, dib.inode);
+                       unlock_share_entry(conn, dib.dev, dib.inode);
+
+                       unix_ERR_class = ERRDOS;
+                       unix_ERR_code = ERRbadshare;
+                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                       return NULL;
+               }
+               /* Ensure we don't reprocess this message. */
+               remove_sharing_violation_open_smb_message(mid);
+
+       }
 
        if (conn->printer) {
                /* printers are handled completely differently. Most of the passed parameters are
                        ignored */
                if (Access)
                        *Access = DOS_OPEN_WRONLY;
-               if (action)
-                       *action = FILE_WAS_CREATED;
+               if (paction)
+                       *paction = FILE_WAS_CREATED;
                return print_fsp_open(conn, fname);
        }
 
@@ -791,14 +1034,19 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
        if(!fsp)
                return NULL;
 
-       DEBUG(10,("open_file_shared: fname = %s, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
-               fname, share_mode, ofun, (int)mode,  oplock_request ));
+       DEBUG(10,("open_file_shared: fname = %s, dos_attrs = %x, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
+               fname, new_dos_mode, share_mode, ofun, (int)mode,  oplock_request ));
 
        if (!check_name(fname,conn)) {
                file_free(fsp);
                return NULL;
        } 
 
+       new_dos_mode &= SAMBA_ATTRIBUTES_MASK;
+       if (file_existed) {
+               existing_dos_mode = dos_mode(conn, fname, psbuf);
+       }
+
        /* ignore any oplock requests if oplocks are disabled */
        if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) {
                oplock_request = 0;
@@ -823,7 +1071,11 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
                        fname ));
                file_free(fsp);
-               errno = EEXIST;
+               if (S_ISDIR(psbuf->st_mode)) {
+                       errno = EISDIR;
+               } else {
+                       errno = EEXIST;
+               }
                return NULL;
        }
       
@@ -835,9 +1087,11 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
 
        /* We only care about matching attributes on file exists and truncate. */
        if (file_existed && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)) {
-               if (!open_match_attributes(conn, fname, psbuf->st_mode, mode, &new_mode)) {
-                       DEBUG(5,("open_file_shared: attributes missmatch for file %s (0%o, 0%o)\n",
-                                               fname, psbuf->st_mode, mode ));
+               if (!open_match_attributes(conn, fname, existing_dos_mode, new_dos_mode,
+                                       psbuf->st_mode, mode, &new_mode)) {
+                       DEBUG(5,("open_file_shared: attributes missmatch for file %s (%x %x) (0%o, 0%o)\n",
+                                               fname, existing_dos_mode, new_dos_mode,
+                                               (int)psbuf->st_mode, (int)mode ));
                        file_free(fsp);
                        errno = EACCES;
                        return NULL;
@@ -881,7 +1135,7 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
 #endif /* O_SYNC */
   
        if (flags != O_RDONLY && file_existed && 
-                       (!CAN_WRITE(conn) || IS_DOS_READONLY(dos_mode(conn,fname,psbuf)))) {
+                       (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_mode))) {
                if (!fcbopen) {
                        DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
                                fname, !CAN_WRITE(conn) ? "share" : "file" ));
@@ -899,6 +1153,17 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                return NULL;
        }
 
+       if (desired_access && ((desired_access & ~(SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES))==0) &&
+               ((desired_access & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES)) != 0)) {
+               /* Stat open that doesn't trigger oplock breaks or share mode checks... ! JRA. */
+               deny_mode = DENY_NONE;
+               if (file_existed) {
+                       oplock_request = 0;
+                       add_share_mode = False;
+                       flags2 &= ~O_CREAT;
+               }
+       }
+
        if (file_existed) {
 
                dev = psbuf->st_dev;
@@ -923,6 +1188,7 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                         * we can do. We also ensure we're not going to create or tuncate
                         * the file as we only want an access decision at this stage. JRA.
                         */
+                       errno = 0;
                        fsp_open = open_file(fsp,conn,fname,psbuf,
                                                flags|(flags2&~(O_TRUNC|O_CREAT)),mode,desired_access);
 
@@ -930,9 +1196,33 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
 flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                flags,(flags2&~(O_TRUNC|O_CREAT)),(int)mode,(int)fsp_open ));
 
+                       if (!fsp_open && errno) {
+                               unix_ERR_class = ERRDOS;
+                               unix_ERR_code = ERRnoaccess;
+                               unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
+                       }
+
+                       /* 
+                        * If we're returning a share violation, ensure we cope with
+                        * the braindead 1 second delay.
+                        */
+
+                       if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
+                               /* The fsp->open_time here represents the current time of day. */
+                               defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
+                       }
+
                        unlock_share_entry(conn, dev, inode);
-                       if (fsp_open)
+                       if (fsp_open) {
                                fd_close(conn, fsp);
+                               /*
+                                * We have detected a sharing violation here
+                                * so return the correct error code
+                                */
+                               unix_ERR_class = ERRDOS;
+                               unix_ERR_code = ERRbadshare;
+                               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                       }
                        file_free(fsp);
                        return NULL;
                }
@@ -980,6 +1270,16 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
 
        if (!file_existed) { 
 
+               /*
+                * Now the file exists and fsp is successfully opened,
+                * fsp->dev and fsp->inode are valid and should replace the
+                * dev=0,inode=0 from a non existent file. Spotted by
+                * Nadav Danieli <nadavd@exanet.com>. JRA.
+                */
+
+               dev = fsp->dev;
+               inode = fsp->inode;
+
                lock_share_entry_fsp(fsp);
 
                num_share_modes = open_mode_check(conn, fname, dev, inode, 
@@ -988,9 +1288,26 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                                  &flags, &oplock_request, &all_current_opens_are_level_II);
 
                if(num_share_modes == -1) {
+                       /* 
+                        * If we're returning a share violation, ensure we cope with
+                        * the braindead 1 second delay.
+                        */
+
+                       if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
+                               /* The fsp->open_time here represents the current time of day. */
+                               defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
+                       }
+
                        unlock_share_entry_fsp(fsp);
                        fd_close(conn,fsp);
                        file_free(fsp);
+                       /*
+                        * We have detected a sharing violation here, so
+                        * return the correct code.
+                        */
+                        unix_ERR_class = ERRDOS;
+                        unix_ERR_code = ERRbadshare;
+                        unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
                        return NULL;
                }
 
@@ -1029,7 +1346,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                /*
                 * We are modifing the file after open - update the stat struct..
                 */
-               if ((truncate_unless_locked(conn,fsp) == -1) || (vfs_fstat(fsp,fsp->fd,psbuf)==-1)) {
+               if ((SMB_VFS_FTRUNCATE(fsp,fsp->fd,0) == -1) || (SMB_VFS_FSTAT(fsp,fsp->fd,psbuf)==-1)) {
                        unlock_share_entry_fsp(fsp);
                        fd_close(conn,fsp);
                        file_free(fsp);
@@ -1055,16 +1372,21 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
 
        DEBUG(10,("open_file_shared : share_mode = %x\n", fsp->share_mode ));
 
-       if (Access)
+       if (Access) {
                (*Access) = open_mode;
+       }
 
-       if (action) {
-               if (file_existed && !(flags2 & O_TRUNC))
-                       *action = FILE_WAS_OPENED;
-               if (!file_existed)
-                       *action = FILE_WAS_CREATED;
-               if (file_existed && (flags2 & O_TRUNC))
-                       *action = FILE_WAS_OVERWRITTEN;
+       action = 0;
+
+       if (file_existed && !(flags2 & O_TRUNC))
+               action = FILE_WAS_OPENED;
+       if (file_existed && (flags2 & O_TRUNC))
+               action = FILE_WAS_OVERWRITTEN;
+       if (!file_existed) 
+               action = FILE_WAS_CREATED;
+
+       if (paction) {
+               *paction = action;
        }
 
        /* 
@@ -1084,31 +1406,54 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                oplock_request = 0;
        }
 
-       set_share_mode(fsp, port, oplock_request);
+       if (add_share_mode) {
+               set_share_mode(fsp, port, oplock_request);
+       }
 
        if (delete_on_close) {
-               NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close);
+               uint32 dosmode = existing_dos_mode;
+               NTSTATUS result;
+
+               if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
+                       dosmode = new_dos_mode;
+               }
+               result = set_delete_on_close_internal(fsp, delete_on_close, dosmode);
 
                if (NT_STATUS_V(result) !=  NT_STATUS_V(NT_STATUS_OK)) {
+                       uint8 u_e_c;
+                       uint32 u_e_code;
                        /* Remember to delete the mode we just added. */
-                       del_share_mode(fsp, NULL);
+                       if (add_share_mode) {
+                               del_share_mode(fsp, NULL);
+                       }
                        unlock_share_entry_fsp(fsp);
                        fd_close(conn,fsp);
                        file_free(fsp);
+                       ntstatus_to_dos(result, &u_e_c, &u_e_code);
+                        unix_ERR_ntstatus = result;
+                        unix_ERR_class = u_e_c;
+                        unix_ERR_code = u_e_code;
                        return NULL;
                }
        }
        
+       if (action == FILE_WAS_OVERWRITTEN || action == FILE_WAS_CREATED) {
+               /* Files should be initially set as archive */
+               if (lp_map_archive(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) {
+                       file_set_dosmode(conn, fname, new_dos_mode | aARCH, NULL, True);
+               }
+       }
+
        /*
         * Take care of inherited ACLs on created files - if default ACL not
         * selected.
         */
 
-       if (!file_existed && !def_acl && (conn->vfs_ops.fchmod_acl != NULL)) {
+       if (!file_existed && !def_acl) {
 
                int saved_errno = errno; /* We might get ENOSYS in the next call.. */
 
-               if (conn->vfs_ops.fchmod_acl(fsp, fsp->fd, mode) == -1 && errno == ENOSYS)
+               if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, mode) == -1 && errno == ENOSYS)
                        errno = saved_errno; /* Ignore ENOSYS */
 
        } else if (new_mode) {
@@ -1117,9 +1462,9 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
 
                /* Attributes need changing. File already existed. */
 
-               if (conn->vfs_ops.fchmod_acl != NULL) {
+               {
                        int saved_errno = errno; /* We might get ENOSYS in the next call.. */
-                       ret = conn->vfs_ops.fchmod_acl(fsp, fsp->fd, new_mode);
+                       ret = SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, new_mode);
 
                        if (ret == -1 && errno == ENOSYS) {
                                errno = saved_errno; /* Ignore ENOSYS */
@@ -1130,11 +1475,13 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                        }
                }
 
-               if ((ret == -1) && (conn->vfs_ops.fchmod(fsp, fsp->fd, new_mode) == -1))
+               if ((ret == -1) && (SMB_VFS_FCHMOD(fsp, fsp->fd, new_mode) == -1))
                        DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n",
                                fname, (int)new_mode));
        }
 
+       /* If this is a successful open, we must remove any deferred open records. */
+       delete_defered_open_entry_record(conn, fsp->dev, fsp->inode);
        unlock_share_entry_fsp(fsp);
 
        conn->num_files_open++;
@@ -1192,7 +1539,7 @@ int close_file_fchmod(files_struct *fsp)
 ****************************************************************************/
 
 files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf,
-                       uint32 desired_access, int share_mode, int smb_ofun, mode_t unixmode, int *action)
+                       uint32 desired_access, int share_mode, int smb_ofun, int *action)
 {
        extern struct current_user current_user;
        BOOL got_stat = False;
@@ -1202,8 +1549,6 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
        if(!fsp)
                return NULL;
 
-       fsp->conn = conn; /* The vfs_fXXX() macros need this. */
-
        if (VALID_STAT(*psbuf))
                got_stat = True;
 
@@ -1238,14 +1583,32 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
                                return NULL;
                        }
 
-                       if(vfs_mkdir(conn,fname, unix_mode(conn,aDIR, fname)) < 0) {
+                       if (ms_has_wild(fname))  {
+                               file_free(fsp);
+                               DEBUG(5,("open_directory: failing create on filename %s with wildcards\n", fname));
+                               unix_ERR_class = ERRDOS;
+                               unix_ERR_code = ERRinvalidname;
+                               unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
+                               return NULL;
+                       }
+
+                       if( strchr_m(fname, ':')) {
+                               file_free(fsp);
+                               DEBUG(5,("open_directory: failing create on filename %s with colon in name\n", fname));
+                               unix_ERR_class = ERRDOS;
+                               unix_ERR_code = ERRinvalidname;
+                               unix_ERR_ntstatus = NT_STATUS_NOT_A_DIRECTORY;
+                               return NULL;
+                       }
+
+                       if(vfs_MkDir(conn,fname, unix_mode(conn,aDIR, fname, True)) < 0) {
                                DEBUG(2,("open_directory: unable to create %s. Error was %s\n",
                                         fname, strerror(errno) ));
                                file_free(fsp);
                                return NULL;
                        }
 
-                       if(vfs_stat(conn,fname, psbuf) != 0) {
+                       if(SMB_VFS_STAT(conn,fname, psbuf) != 0) {
                                file_free(fsp);
                                return NULL;
                        }
@@ -1286,7 +1649,7 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
        fsp->dev = psbuf->st_dev;
        fsp->size = psbuf->st_size;
        fsp->vuid = current_user.vuid;
-       fsp->pos = -1;
+       fsp->file_pid = global_smbpid;
        fsp->can_lock = True;
        fsp->can_read = False;
        fsp->can_write = False;
@@ -1299,11 +1662,10 @@ files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_ST
        fsp->is_directory = True;
        fsp->is_stat = False;
        fsp->directory_delete_on_close = False;
-       fsp->conn = conn;
        string_set(&fsp->fsp_name,fname);
 
        if (delete_on_close) {
-               NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close);
+               NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close, 0);
 
                if (NT_STATUS_V(result) !=  NT_STATUS_V(NT_STATUS_OK)) {
                        file_free(fsp);
@@ -1335,8 +1697,6 @@ files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_ST
        if(!fsp)
                return NULL;
 
-       fsp->conn = conn; /* The vfs_fXXX() macros need this. */
-
        DEBUG(5,("open_file_stat: 'opening' file %s\n", fname));
 
        /*
@@ -1344,15 +1704,11 @@ files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_ST
         */
        
        fsp->mode = psbuf->st_mode;
-       /* 
-        * Don't store dev or inode, we don't want any iterator
-        * to see this.
-        */
-       fsp->inode = (SMB_INO_T)0;
-       fsp->dev = (SMB_DEV_T)0;
+       fsp->inode = psbuf->st_ino;
+       fsp->dev = psbuf->st_dev;
        fsp->size = psbuf->st_size;
        fsp->vuid = current_user.vuid;
-       fsp->pos = -1;
+       fsp->file_pid = global_smbpid;
        fsp->can_lock = False;
        fsp->can_read = False;
        fsp->can_write = False;
@@ -1365,7 +1721,6 @@ files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_ST
        fsp->is_directory = False;
        fsp->is_stat = True;
        fsp->directory_delete_on_close = False;
-       fsp->conn = conn;
        string_set(&fsp->fsp_name,fname);
 
        conn->num_files_open++;