r10656: BIG merge from trunk. Features not copied over
[vlendec/samba-autobuild/.git] / source3 / smbd / reply.c
index 1c2e950836769a94064e070f8a3b992a613ae70d..ba22a56cfb4b9a2cdeded05518bec76ffc1a9f60 100644 (file)
@@ -30,7 +30,6 @@
 extern enum protocol_types Protocol;
 extern int max_send;
 extern int max_recv;
-extern int global_oplock_break;
 unsigned int smb_echo_count = 0;
 extern uint32 global_client_caps;
 
@@ -398,7 +397,9 @@ size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len
        } else {
                ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
        }
-       if (allow_wcard_names) {
+       if (lp_posix_pathnames()) {
+               *err = check_path_syntax_posix(dest, tmppath);
+       } else if (allow_wcard_names) {
                *err = check_path_syntax_wcard(dest, tmppath);
        } else {
                *err = check_path_syntax(dest, tmppath);
@@ -1012,9 +1013,9 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        pstring directory;
        pstring fname;
        SMB_OFF_T size;
-       int mode;
+       uint32 mode;
        time_t date;
-       int dirtype;
+       uint32 dirtype;
        int outsize = 0;
        unsigned int numentries = 0;
        unsigned int maxentries = 0;
@@ -1032,6 +1033,10 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        NTSTATUS nt_status;
        BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
 
+       if (lp_posix_pathnames()) {
+               return reply_unknown(inbuf, outbuf);
+       }
+
        START_PROFILE(SMBsearch);
 
        *mask = *directory = *fname = 0;
@@ -1109,7 +1114,7 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                ok = True;
      
                if (status_len == 0) {
-                       dptr_num = dptr_create(conn,directory,True,expect_close,SVAL(inbuf,smb_pid));
+                       dptr_num = dptr_create(conn,directory,True,expect_close,SVAL(inbuf,smb_pid), mask, dirtype);
                        if (dptr_num < 0) {
                                if(dptr_num == -2) {
                                        END_PROFILE(SMBsearch);
@@ -1118,10 +1123,6 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                                END_PROFILE(SMBsearch);
                                return ERROR_DOS(ERRDOS,ERRnofids);
                        }
-                       if (!dptr_set_wcard_and_attributes(dptr_num, mask, dirtype)) {
-                               END_PROFILE(SMBsearch);
-                               return ERROR_DOS(ERRDOS,ERRnomem);
-                       }
                } else {
                        dirtype = dptr_attr(dptr_num);
                }
@@ -1154,7 +1155,9 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                                                memcpy(p,status,21);
                                                make_dir_struct(p,mask,fname,size, mode,date,
                                                                !allow_long_path_components);
-                                               dptr_fill(p+12,dptr_num);
+                                               if (!dptr_fill(p+12,dptr_num)) {
+                                                       break;
+                                               }
                                                numentries++;
                                                p += DIR_STRUCT_SIZE;
                                        }
@@ -1228,6 +1231,10 @@ int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        char *p;
        NTSTATUS err;
 
+       if (lp_posix_pathnames()) {
+               return reply_unknown(inbuf, outbuf);
+       }
+
        START_PROFILE(SMBfclose);
 
        outsize = set_message(outbuf,1,0,True);
@@ -1269,20 +1276,24 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 {
        pstring fname;
        int outsize = 0;
-       int fmode=0;
-       int share_mode;
+       uint32 fattr=0;
        SMB_OFF_T size = 0;
        time_t mtime=0;
-       int rmode=0;
+       int info;
        SMB_STRUCT_STAT sbuf;
        BOOL bad_path = False;
        files_struct *fsp;
        int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
-       uint16 dos_attr = SVAL(inbuf,smb_vwv1);
+       int deny_mode;
+       uint32 dos_attr = SVAL(inbuf,smb_vwv1);
+       uint32 access_mask;
+       uint32 share_mode;
+       uint32 create_disposition;
+       uint32 create_options = 0;
        NTSTATUS status;
        START_PROFILE(SMBopen);
  
-       share_mode = SVAL(inbuf,smb_vwv0);
+       deny_mode = SVAL(inbuf,smb_vwv0);
 
        srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status, False);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1298,8 +1309,20 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
        }
     
-       fsp = open_file_shared(conn,fname,&sbuf,share_mode,(FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN),
-                       (uint32)dos_attr, oplock_request,&rmode,NULL);
+       if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
+                       &access_mask, &share_mode, &create_disposition, &create_options)) {
+               END_PROFILE(SMBopen);
+               return ERROR_DOS(ERRDOS, ERRbadaccess);
+       }
+
+       fsp = open_file_ntcreate(conn,fname,&sbuf,
+                       access_mask,
+                       share_mode,
+                       create_disposition,
+                       create_options,
+                       dos_attr,
+                       oplock_request,
+                       &info);
 
        if (!fsp) {
                END_PROFILE(SMBopen);
@@ -1311,10 +1334,10 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        }
 
        size = sbuf.st_size;
-       fmode = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fname,&sbuf);
        mtime = sbuf.st_mtime;
 
-       if (fmode & aDIR) {
+       if (fattr & aDIR) {
                DEBUG(3,("attempt to open a directory %s\n",fname));
                close_file(fsp,False);
                END_PROFILE(SMBopen);
@@ -1323,19 +1346,22 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
   
        outsize = set_message(outbuf,7,0,True);
        SSVAL(outbuf,smb_vwv0,fsp->fnum);
-       SSVAL(outbuf,smb_vwv1,fmode);
-       if(lp_dos_filetime_resolution(SNUM(conn)) )
+       SSVAL(outbuf,smb_vwv1,fattr);
+       if(lp_dos_filetime_resolution(SNUM(conn)) ) {
                put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
-       else
+       } else {
                put_dos_date3(outbuf,smb_vwv2,mtime);
+       }
        SIVAL(outbuf,smb_vwv4,(uint32)size);
-       SSVAL(outbuf,smb_vwv6,rmode);
+       SSVAL(outbuf,smb_vwv6,GET_OPENX_MODE(deny_mode));
 
-       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
+       if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
     
-       if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
+       if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
        END_PROFILE(SMBopen);
        return(outsize);
 }
@@ -1347,21 +1373,22 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
 {
        pstring fname;
-       int smb_mode = SVAL(inbuf,smb_vwv3);
-       int smb_attr = SVAL(inbuf,smb_vwv5);
+       uint16 open_flags = SVAL(inbuf,smb_vwv2);
+       int deny_mode = SVAL(inbuf,smb_vwv3);
+       uint32 smb_attr = SVAL(inbuf,smb_vwv5);
        /* Breakout the oplock request bits so we can set the
                reply bits separately. */
        BOOL ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
        BOOL core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
        BOOL oplock_request = ex_oplock_request | core_oplock_request;
 #if 0
-       int open_flags = SVAL(inbuf,smb_vwv2);
        int smb_sattr = SVAL(inbuf,smb_vwv4); 
        uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
 #endif
        int smb_ofun = SVAL(inbuf,smb_vwv8);
        SMB_OFF_T size=0;
-       int fmode=0,mtime=0,rmode=0;
+       uint32 fattr=0;
+       int mtime=0;
        SMB_STRUCT_STAT sbuf;
        int smb_action = 0;
        BOOL bad_path = False;
@@ -1369,6 +1396,10 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        NTSTATUS status;
        SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv9);
        ssize_t retval = -1;
+       uint32 access_mask;
+       uint32 share_mode;
+       uint32 create_disposition;
+       uint32 create_options = 0;
 
        START_PROFILE(SMBopenX);
 
@@ -1398,18 +1429,23 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
        }
 
-       /* Strange open mode mapping. */
-       if (smb_ofun == 0) {
-               if (GET_OPEN_MODE(smb_mode) == DOS_OPEN_EXEC) {
-                       smb_ofun = FILE_EXISTS_FAIL | FILE_CREATE_IF_NOT_EXIST;
-               } else {
-                       END_PROFILE(SMBopenX);
-                       return ERROR_FORCE_DOS(ERRDOS, ERRbadaccess);
-               }
+       if (!map_open_params_to_ntcreate(fname, deny_mode, smb_ofun,
+                               &access_mask,
+                               &share_mode,
+                               &create_disposition,
+                               &create_options)) {
+               END_PROFILE(SMBopenX);
+               return ERROR_DOS(ERRDOS, ERRbadaccess);
        }
 
-       fsp = open_file_shared(conn,fname,&sbuf,smb_mode,smb_ofun,(uint32)smb_attr,
-                       oplock_request, &rmode,&smb_action);
+       fsp = open_file_ntcreate(conn,fname,&sbuf,
+                       access_mask,
+                       share_mode,
+                       create_disposition,
+                       create_options,
+                       smb_attr,
+                       oplock_request,
+                       &smb_action);
       
        if (!fsp) {
                END_PROFILE(SMBopenX);
@@ -1440,9 +1476,9 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                size = get_allocation_size(conn,fsp,&sbuf);
        }
 
-       fmode = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fname,&sbuf);
        mtime = sbuf.st_mtime;
-       if (fmode & aDIR) {
+       if (fattr & aDIR) {
                close_file(fsp,False);
                END_PROFILE(SMBopenX);
                return ERROR_DOS(ERRDOS,ERRnoaccess);
@@ -1453,34 +1489,47 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                correct bit for extended oplock reply.
        */
 
-       if (ex_oplock_request && lp_fake_oplocks(SNUM(conn)))
+       if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
                smb_action |= EXTENDED_OPLOCK_GRANTED;
+       }
 
-       if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
+       if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
                smb_action |= EXTENDED_OPLOCK_GRANTED;
+       }
 
        /* If the caller set the core oplock request bit
                and we granted one (by whatever means) - set the
                correct bit for core oplock reply.
        */
 
-       if (core_oplock_request && lp_fake_oplocks(SNUM(conn)))
+       if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
 
-       if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
+       if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
 
-       set_message(outbuf,15,0,True);
+       if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
+               set_message(outbuf,19,0,True);
+       } else {
+               set_message(outbuf,15,0,True);
+       }
        SSVAL(outbuf,smb_vwv2,fsp->fnum);
-       SSVAL(outbuf,smb_vwv3,fmode);
-       if(lp_dos_filetime_resolution(SNUM(conn)) )
+       SSVAL(outbuf,smb_vwv3,fattr);
+       if(lp_dos_filetime_resolution(SNUM(conn)) ) {
                put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
-       else
+       } else {
                put_dos_date3(outbuf,smb_vwv4,mtime);
+       }
        SIVAL(outbuf,smb_vwv6,(uint32)size);
-       SSVAL(outbuf,smb_vwv8,rmode);
+       SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
        SSVAL(outbuf,smb_vwv11,smb_action);
 
+       if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
+               SIVAL(outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
+       }
+
        END_PROFILE(SMBopenX);
        return chain_reply(inbuf,outbuf,length,bufsize);
 }
@@ -1522,18 +1571,21 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        pstring fname;
        int com;
        int outsize = 0;
-       int createmode;
-       int ofun = 0;
+       uint32 fattr = SVAL(inbuf,smb_vwv0);
        BOOL bad_path = False;
        files_struct *fsp;
        int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
        SMB_STRUCT_STAT sbuf;
        NTSTATUS status;
+       uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
+       uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
+       uint32 create_disposition;
+       uint32 create_options = 0;
+
        START_PROFILE(SMBcreate);
  
        com = SVAL(inbuf,smb_com);
 
-       createmode = SVAL(inbuf,smb_vwv0);
        srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status, False);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBcreate);
@@ -1548,20 +1600,27 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
        }
 
-       if (createmode & aVOLID)
+       if (fattr & aVOLID) {
                DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
-  
+       }
+
        if(com == SMBmknew) {
                /* We should fail if file exists. */
-               ofun = FILE_CREATE_IF_NOT_EXIST;
+               create_disposition = FILE_CREATE;
        } else {
-               /* SMBcreate - Create if file doesn't exist, truncate if it does. */
-               ofun = FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE;
-       }
-
-       /* Open file in dos compatibility share mode. */
-       fsp = open_file_shared(conn,fname,&sbuf,SET_DENY_MODE(DENY_FCB)|SET_OPEN_MODE(DOS_OPEN_FCB), 
-                       ofun, (uint32)createmode, oplock_request, NULL, NULL);
+               /* Create if file doesn't exist, truncate if it does. */
+               create_disposition = FILE_OVERWRITE_IF;
+       }
+
+       /* Open file using ntcreate. */
+       fsp = open_file_ntcreate(conn,fname,&sbuf,
+                               access_mask,
+                               share_mode,
+                               create_disposition,
+                               create_options,
+                               fattr,
+                               oplock_request,
+                               NULL);
   
        if (!fsp) {
                END_PROFILE(SMBcreate);
@@ -1575,14 +1634,16 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        outsize = set_message(outbuf,1,0,True);
        SSVAL(outbuf,smb_vwv0,fsp->fnum);
 
-       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
+       if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
  
-       if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
+       if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
  
-       DEBUG( 2, ( "new file %s\n", fname ) );
-       DEBUG( 3, ( "mknew %s fd=%d dmode=%d\n", fname, fsp->fd, createmode ) );
+       DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
+       DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n", fname, fsp->fh->fd, (unsigned int)fattr ) );
 
        END_PROFILE(SMBcreate);
        return(outsize);
@@ -1596,7 +1657,7 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 {
        pstring fname;
        int outsize = 0;
-       int createattr;
+       uint32 fattr = SVAL(inbuf,smb_vwv0);
        BOOL bad_path = False;
        files_struct *fsp;
        int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
@@ -1608,7 +1669,6 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        START_PROFILE(SMBctemp);
 
-       createattr = SVAL(inbuf,smb_vwv0);
        srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status, False);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBctemp);
@@ -1636,12 +1696,15 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        SMB_VFS_STAT(conn,fname,&sbuf);
 
-       /* Open file in dos compatibility share mode. */
        /* We should fail if file does not exist. */
-       fsp = open_file_shared(conn,fname,&sbuf,
-               SET_DENY_MODE(DENY_FCB)|SET_OPEN_MODE(DOS_OPEN_FCB),
-               FILE_EXISTS_OPEN|FILE_FAIL_IF_NOT_EXIST,
-               (uint32)createattr, oplock_request, NULL, NULL);
+       fsp = open_file_ntcreate(conn,fname,&sbuf,
+                               FILE_GENERIC_READ | FILE_GENERIC_WRITE,
+                               FILE_SHARE_READ|FILE_SHARE_WRITE,
+                               FILE_OPEN,
+                               0,
+                               fattr,
+                               oplock_request,
+                               NULL);
 
        /* close fd from smb_mkstemp() */
        close(tmpfd);
@@ -1660,10 +1723,11 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        /* the returned filename is relative to the directory */
        s = strrchr_m(fname, '/');
-       if (!s)
+       if (!s) {
                s = fname;
-       else
+       } else {
                s++;
+       }
 
        p = smb_buf(outbuf);
 #if 0
@@ -1675,15 +1739,17 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        p += namelen;
        outsize = set_message_end(outbuf, p);
 
-       if (oplock_request && lp_fake_oplocks(SNUM(conn)))
+       if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
   
-       if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
+       if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
                SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
+       }
 
-       DEBUG( 2, ( "created temp file %s\n", fname ) );
-       DEBUG( 3, ( "ctemp %s fd=%d umode=%o\n",
-                       fname, fsp->fd, sbuf.st_mode ) );
+       DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
+       DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
+                       (unsigned int)sbuf.st_mode ) );
 
        END_PROFILE(SMBctemp);
        return(outsize);
@@ -1695,34 +1761,41 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
 static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype, SMB_STRUCT_STAT *pst)
 {
-       int smb_action;
-       int access_mode;
        files_struct *fsp;
-       uint16 fmode;
+       uint32 fmode;
 
-       if (!CAN_WRITE(conn))
+       if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
+       }
 
        fmode = dos_mode(conn,fname,pst);
-       if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM))
+       if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
                return NT_STATUS_NO_SUCH_FILE;
+       }
 
-       if (S_ISDIR(pst->st_mode))
+       if (S_ISDIR(pst->st_mode)) {
                return NT_STATUS_OK;
+       }
 
        /* We need a better way to return NT status codes from open... */
-       set_saved_error_triple(0, 0, NT_STATUS_OK);
+       set_saved_ntstatus(NT_STATUS_OK);
 
-       fsp = open_file_shared1(conn, fname, pst, DELETE_ACCESS, SET_DENY_MODE(DENY_ALL),
-               (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), FILE_ATTRIBUTE_NORMAL, 0, &access_mode, &smb_action);
+       fsp = open_file_ntcreate(conn, fname, pst,
+                               DELETE_ACCESS,
+                               FILE_SHARE_READ|FILE_SHARE_WRITE,
+                               FILE_OPEN,
+                               0,
+                               FILE_ATTRIBUTE_NORMAL,
+                               0,
+                               NULL);
 
        if (!fsp) {
-               NTSTATUS ret;
-               if (get_saved_error_triple(NULL, NULL, &ret)) {
-                       set_saved_error_triple(0, 0, NT_STATUS_OK);
+               NTSTATUS ret = get_saved_ntstatus();
+               if (!NT_STATUS_IS_OK(ret)) {
+                       set_saved_ntstatus(NT_STATUS_OK);
                        return ret;
                }
-               set_saved_error_triple(0, 0, NT_STATUS_OK);
+               set_saved_ntstatus(NT_STATUS_OK);
                return NT_STATUS_ACCESS_DENIED;
        }
        close_file(fsp,False);
@@ -1733,46 +1806,49 @@ static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype,
  Check if a user is allowed to delete a file.
 ********************************************************************/
 
-NTSTATUS can_delete(connection_struct *conn, char *fname, int dirtype, BOOL bad_path, BOOL check_is_at_open)
+NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL bad_path, BOOL check_is_at_open)
 {
        SMB_STRUCT_STAT sbuf;
-       int fmode;
-       int smb_action;
-       int access_mode;
+       uint32 fattr;
        files_struct *fsp;
 
-       DEBUG(10,("can_delete: %s, dirtype = %d\n",
-               fname, dirtype ));
+       DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
 
-       if (!CAN_WRITE(conn))
+       if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
+       }
 
        if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
                if(errno == ENOENT) {
-                       if (bad_path)
+                       if (bad_path) {
                                return NT_STATUS_OBJECT_PATH_NOT_FOUND;
-                       else
+                       } else {
                                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+                       }
                }
                return map_nt_error_from_unix(errno);
        }
 
-       fmode = dos_mode(conn,fname,&sbuf);
+       fattr = dos_mode(conn,fname,&sbuf);
 
        /* Can't delete a directory. */
-       if (fmode & aDIR)
+       if (fattr & aDIR) {
                return NT_STATUS_FILE_IS_A_DIRECTORY;
+       }
+
 #if 0 /* JRATEST */
        else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
                return NT_STATUS_OBJECT_NAME_INVALID;
 #endif /* JRATEST */
 
        if (!lp_delete_readonly(SNUM(conn))) {
-               if (fmode & aRONLY)
+               if (fattr & aRONLY) {
                        return NT_STATUS_CANNOT_DELETE;
+               }
        }
-       if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM))
+       if ((fattr & ~dirtype) & (aHIDDEN | aSYSTEM)) {
                return NT_STATUS_NO_SUCH_FILE;
+       }
 
        if (check_is_at_open) {
                if (!can_delete_file_in_directory(conn, fname)) {
@@ -1783,18 +1859,24 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, int dirtype, BOOL bad_
                   don't do it here as we'll get it wrong. */
 
                /* We need a better way to return NT status codes from open... */
-               set_saved_error_triple(0, 0, NT_STATUS_OK);
+               set_saved_ntstatus(NT_STATUS_OK);
 
-               fsp = open_file_shared1(conn, fname, &sbuf, DELETE_ACCESS, SET_DENY_MODE(DENY_ALL),
-                       (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN), FILE_ATTRIBUTE_NORMAL, 0, &access_mode, &smb_action);
+               fsp = open_file_ntcreate(conn, fname, &sbuf,
+                                       DELETE_ACCESS,
+                                       FILE_SHARE_NONE,
+                                       FILE_OPEN,
+                                       0,
+                                       FILE_ATTRIBUTE_NORMAL,
+                                       0,
+                                       NULL);
 
                if (!fsp) {
-                       NTSTATUS ret;
-                       if (get_saved_error_triple(NULL, NULL, &ret)) {
-                               set_saved_error_triple(0, 0, NT_STATUS_OK);
+                       NTSTATUS ret = get_saved_ntstatus();
+                       if (!NT_STATUS_IS_OK(ret)) {
+                               set_saved_ntstatus(NT_STATUS_OK);
                                return ret;
                        }
-                       set_saved_error_triple(0, 0, NT_STATUS_OK);
+                       set_saved_ntstatus(NT_STATUS_OK);
                        return NT_STATUS_ACCESS_DENIED;
                }
                close_file(fsp,False);
@@ -1807,7 +1889,7 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, int dirtype, BOOL bad_
  code.
 ****************************************************************************/
 
-NTSTATUS unlink_internals(connection_struct *conn, int dirtype, char *name)
+NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name)
 {
        pstring directory;
        pstring mask;
@@ -1869,8 +1951,11 @@ NTSTATUS unlink_internals(connection_struct *conn, int dirtype, char *name)
                struct smb_Dir *dir_hnd = NULL;
                const char *dname;
                
+               if (strequal(mask,"????????.???"))
+                       pstrcpy(mask,"*");
+
                if (check_name(directory,conn))
-                       dir_hnd = OpenDir(conn, directory);
+                       dir_hnd = OpenDir(conn, directory, mask, dirtype);
                
                /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
                   the pattern matches against the long name, otherwise the short name 
@@ -1881,9 +1966,6 @@ NTSTATUS unlink_internals(connection_struct *conn, int dirtype, char *name)
                        long offset = 0;
                        error = NT_STATUS_NO_SUCH_FILE;
 
-                       if (strequal(mask,"????????.???"))
-                               pstrcpy(mask,"*");
-
                        while ((dname = ReadDirName(dir_hnd, &offset))) {
                                SMB_STRUCT_STAT st;
                                pstring fname;
@@ -1944,7 +2026,7 @@ int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
 {
        int outsize = 0;
        pstring name;
-       int dirtype;
+       uint32 dirtype;
        NTSTATUS status;
        START_PROFILE(SMBunlink);
        
@@ -2052,7 +2134,7 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
                header.length = 4;
                header.free = NULL;
 
-               if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fd, &header, startpos, nread) == -1) {
+               if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, nread) == -1) {
                        /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
                        if (errno == ENOSYS) {
                                goto normal_readbraw;
@@ -2126,15 +2208,6 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
         * return a zero length response here.
         */
 
-       if(global_oplock_break) {
-               _smb_setlen(header,0);
-               if (write_data(smbd_server_fd(),header,4) != 4)
-                       fail_readraw();
-               DEBUG(5,("readbraw - oplock break finished\n"));
-               END_PROFILE(SMBreadbraw);
-               return -1;
-       }
-
        fsp = file_fsp(inbuf,smb_vwv0);
 
        if (!FNUM_OK(fsp,conn) || !fsp->can_read) {
@@ -2199,7 +2272,7 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
                SMB_STRUCT_STAT st;
                SMB_OFF_T size = 0;
   
-               if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0) {
+               if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
                        size = st.st_size;
                }
 
@@ -2215,8 +2288,8 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
                nread = 0;
 #endif
   
-       DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%d min=%d nread=%d\n", fsp->fnum, (double)startpos,
-                               (int)maxcount, (int)mincount, (int)nread ) );
+       DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp->fnum, (double)startpos,
+                               (unsigned long)maxcount, (unsigned long)mincount, (unsigned long)nread ) );
   
        send_file_readbraw(conn, fsp, startpos, nread, mincount, outbuf, out_buffsize);
 
@@ -2245,7 +2318,9 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
        START_PROFILE(SMBlockread);
 
        CHECK_FSP(fsp,conn);
-       CHECK_READ(fsp);
+       if (!CHECK_READ(fsp,inbuf)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        release_level_2_oplocks_on_change(fsp);
 
@@ -2339,7 +2414,9 @@ int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int
        START_PROFILE(SMBread);
 
        CHECK_FSP(fsp,conn);
-       CHECK_READ(fsp);
+       if (!CHECK_READ(fsp,inbuf)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        numtoread = SVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
@@ -2407,7 +2484,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                SMB_STRUCT_STAT sbuf;
                DATA_BLOB header;
 
-               if(SMB_VFS_FSTAT(fsp,fsp->fd, &sbuf) == -1)
+               if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1)
                        return(UNIXERROR(ERRDOS,ERRnoaccess));
 
                if (startpos > sbuf.st_size)
@@ -2436,7 +2513,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                header.length = data - outbuf;
                header.free = NULL;
 
-               if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fd, &header, startpos, smb_maxcnt)) == -1) {
+               if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, smb_maxcnt)) == -1) {
                        /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
                        if (errno == ENOSYS) {
                                goto normal_read;
@@ -2524,7 +2601,9 @@ int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        }
 
        CHECK_FSP(fsp,conn);
-       CHECK_READ(fsp);
+       if (!CHECK_READ(fsp,inbuf)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        set_message(outbuf,12,0,True);
 
@@ -2569,6 +2648,11 @@ int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                return ERROR_DOS(ERRDOS,ERRlock);
        }
 
+       if (schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) {
+               END_PROFILE(SMBreadX);
+               return -1;
+       }
+
        nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt);
        if (nread != -1)
                nread = chain_reply(inbuf,outbuf,length,bufsize);
@@ -2599,7 +2683,9 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        }
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
   
        tcount = IVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
@@ -2642,6 +2728,7 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        SCVAL(outbuf,smb_com,SMBwritebraw);
        SSVALS(outbuf,smb_vwv0,-1);
        outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
+       show_msg(outbuf);
        if (!send_smb(smbd_server_fd(),outbuf))
                exit_server("reply_writebraw: send_smb failed.");
   
@@ -2656,7 +2743,6 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        /* Set up outbuf to return the correct return */
        outsize = set_message(outbuf,1,0,True);
        SCVAL(outbuf,smb_com,SMBwritec);
-       SSVAL(outbuf,smb_vwv0,total_written);
 
        if (numtowrite != 0) {
 
@@ -2688,8 +2774,9 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
                        total_written += nwritten;
        }
  
-       if ((lp_syncalways(SNUM(conn)) || write_through) && lp_strict_sync(SNUM(conn)))
-               sync_file(conn,fsp);
+       SSVAL(outbuf,smb_vwv0,total_written);
+
+       sync_file(conn, fsp, write_through);
 
        DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
                fsp->fnum, (double)startpos, (int)numtowrite,(int)total_written));
@@ -2732,7 +2819,9 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        START_PROFILE(SMBwriteunlock);
        
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        numtowrite = SVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
@@ -2746,13 +2835,13 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        /* The special X/Open SMB protocol handling of
           zero length writes is *NOT* done for
           this call */
-       if(numtowrite == 0)
+       if(numtowrite == 0) {
                nwritten = 0;
-       else
+       } else {
                nwritten = write_file(fsp,data,startpos,numtowrite);
+       }
   
-       if (lp_syncalways(SNUM(conn)))
-               sync_file(conn,fsp);
+       sync_file(conn, fsp, False /* write through */);
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
                END_PROFILE(SMBwriteunlock);
@@ -2803,7 +2892,9 @@ int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int d
        }
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        numtowrite = SVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
@@ -2837,8 +2928,7 @@ int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int d
        } else
                nwritten = write_file(fsp,data,startpos,numtowrite);
   
-       if (lp_syncalways(SNUM(conn)))
-               sync_file(conn,fsp);
+       sync_file(conn, fsp, False);
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
                END_PROFILE(SMBwrite);
@@ -2884,11 +2974,16 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng
        }
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
+       set_message(outbuf,6,0,True);
+  
        /* Deal with possible LARGE_WRITEX */
-       if (large_writeX)
+       if (large_writeX) {
                numtowrite |= ((((size_t)SVAL(inbuf,smb_vwv9)) & 1 )<<16);
+       }
 
        if(smb_doff > smblen || (smb_doff + numtowrite > smblen)) {
                END_PROFILE(SMBwriteX);
@@ -2930,18 +3025,24 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng
        done, just a write of zero. To truncate a file,
        use SMBwrite. */
 
-       if(numtowrite == 0)
+       if(numtowrite == 0) {
                nwritten = 0;
-       else
+       } else {
+
+               if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize,
+                                       fsp,data,startpos,numtowrite)) {
+                       END_PROFILE(SMBwriteX);
+                       return -1;
+               }
+
                nwritten = write_file(fsp,data,startpos,numtowrite);
+       }
   
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
                END_PROFILE(SMBwriteX);
                return(UNIXERROR(ERRHRD,ERRdiskfull));
        }
 
-       set_message(outbuf,6,0,True);
-  
        SSVAL(outbuf,smb_vwv2,nwritten);
        if (large_writeX)
                SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
@@ -2954,8 +3055,7 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng
        DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
                fsp->fnum, (int)numtowrite, (int)nwritten));
 
-       if (lp_syncalways(SNUM(conn)) || write_through)
-               sync_file(conn,fsp);
+       sync_file(conn, fsp, write_through);
 
        END_PROFILE(SMBwriteX);
        return chain_reply(inbuf,outbuf,length,bufsize);
@@ -2989,7 +3089,7 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
                        break;
                case 1:
                        umode = SEEK_CUR;
-                       res = fsp->pos + startpos;
+                       res = fsp->fh->pos + startpos;
                        break;
                case 2:
                        umode = SEEK_END;
@@ -3001,19 +3101,19 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
        }
 
        if (umode == SEEK_END) {
-               if((res = SMB_VFS_LSEEK(fsp,fsp->fd,startpos,umode)) == -1) {
+               if((res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,startpos,umode)) == -1) {
                        if(errno == EINVAL) {
                                SMB_OFF_T current_pos = startpos;
                                SMB_STRUCT_STAT sbuf;
 
-                               if(SMB_VFS_FSTAT(fsp,fsp->fd, &sbuf) == -1) {
+                               if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
                                        END_PROFILE(SMBlseek);
                                        return(UNIXERROR(ERRDOS,ERRnoaccess));
                                }
 
                                current_pos += sbuf.st_size;
                                if(current_pos < 0)
-                                       res = SMB_VFS_LSEEK(fsp,fsp->fd,0,SEEK_SET);
+                                       res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,0,SEEK_SET);
                        }
                }
 
@@ -3023,7 +3123,7 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
                }
        }
 
-       fsp->pos = res;
+       fsp->fh->pos = res;
   
        outsize = set_message(outbuf,2,0,True);
        SIVAL(outbuf,smb_vwv0,res);
@@ -3052,7 +3152,7 @@ int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int
        if (!fsp) {
                file_sync_all(conn);
        } else {
-               sync_file(conn,fsp);
+               sync_file(conn,fsp, True);
        }
        
        DEBUG(3,("flush\n"));
@@ -3129,7 +3229,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
                pstrcpy( file_name, fsp->fsp_name);
 
                DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
-                        fsp->fd, fsp->fnum,
+                        fsp->fh->fd, fsp->fnum,
                         conn->num_files_open));
  
                /*
@@ -3180,7 +3280,9 @@ int reply_writeclose(connection_struct *conn,
        START_PROFILE(SMBwriteclose);
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        numtowrite = SVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
@@ -3255,7 +3357,7 @@ int reply_lock(connection_struct *conn,
        offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
 
        DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
-                fsp->fd, fsp->fnum, (double)offset, (double)count));
+                fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
 
        status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
        if (NT_STATUS_V(status)) {
@@ -3306,7 +3408,7 @@ int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
        }
 
        DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
-                   fsp->fd, fsp->fnum, (double)offset, (double)count ) );
+                   fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
        
        END_PROFILE(SMBunlock);
        return(outsize);
@@ -3375,6 +3477,7 @@ int reply_echo(connection_struct *conn,
 
                smb_setlen(outbuf,outsize - 4);
 
+               show_msg(outbuf);
                if (!send_smb(smbd_server_fd(),outbuf))
                        exit_server("reply_echo: send_smb failed.");
        }
@@ -3415,7 +3518,7 @@ int reply_printopen(connection_struct *conn,
        SSVAL(outbuf,smb_vwv0,fsp->fnum);
   
        DEBUG(3,("openprint fd=%d fnum=%d\n",
-                fsp->fd, fsp->fnum));
+                fsp->fh->fd, fsp->fnum));
 
        END_PROFILE(SMBsplopen);
        return(outsize);
@@ -3441,7 +3544,7 @@ int reply_printclose(connection_struct *conn,
        }
   
        DEBUG(3,("printclose fd=%d fnum=%d\n",
-                fsp->fd,fsp->fnum));
+                fsp->fh->fd,fsp->fnum));
   
        close_err = close_file(fsp,True);
 
@@ -3545,7 +3648,9 @@ int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_
        }
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        numtowrite = SVAL(smb_buf(inbuf),1);
        data = smb_buf(inbuf) + 3;
@@ -3629,7 +3734,7 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        if( is_ntfs_stream_name(directory)) {
                DEBUG(5,("reply_mkdir: failing create on filename %s with colon in name\n", directory));
                END_PROFILE(SMBmkdir);
-               return ERROR_FORCE_DOS(ERRDOS, ERRinvalidname);
+               return ERROR_NT(NT_STATUS_NOT_A_DIRECTORY);
        }
 
        status = mkdir_internal(conn, directory,bad_path);
@@ -3674,7 +3779,7 @@ static BOOL recursive_rmdir(connection_struct *conn, char *directory)
        const char *dname = NULL;
        BOOL ret = False;
        long offset = 0;
-       struct smb_Dir *dir_hnd = OpenDir(conn, directory);
+       struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
 
        if(dir_hnd == NULL)
                return True;
@@ -3742,10 +3847,10 @@ BOOL rmdir_internals(connection_struct *conn, char *directory)
                 */
                BOOL all_veto_files = True;
                const char *dname;
-               struct smb_Dir *dir_hnd = OpenDir(conn, directory);
+               struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
 
                if(dir_hnd != NULL) {
-                       long dirpos = TellDir(dir_hnd);
+                       long dirpos = 0;
                        while ((dname = ReadDirName(dir_hnd,&dirpos))) {
                                if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
                                        continue;
@@ -3758,7 +3863,7 @@ BOOL rmdir_internals(connection_struct *conn, char *directory)
                        }
 
                        if(all_veto_files) {
-                               SeekDir(dir_hnd,dirpos);
+                               RewindDir(dir_hnd,&dirpos);
                                while ((dname = ReadDirName(dir_hnd,&dirpos))) {
                                        pstring fullname;
 
@@ -3960,11 +4065,40 @@ static void rename_open_files(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T
                        (unsigned int)dev, (double)inode, newname ));
 }
 
+/****************************************************************************
+ We need to check if the source path is a parent directory of the destination
+ (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
+ refuse the rename with a sharing violation. Under UNIX the above call can
+ *succeed* if /foo/bar/baz is a symlink to another area in the share. We
+ probably need to check that the client is a Windows one before disallowing
+ this as a UNIX client (one with UNIX extensions) can know the source is a
+ symlink and make this decision intelligently. Found by an excellent bug
+ report from <AndyLiebman@aol.com>.
+****************************************************************************/
+
+static BOOL rename_path_prefix_equal(const char *src, const char *dest)
+{
+       const char *psrc = src;
+       const char *pdst = dest;
+       size_t slen;
+
+       if (psrc[0] == '.' && psrc[1] == '/') {
+               psrc += 2;
+       }
+       if (pdst[0] == '.' && pdst[1] == '/') {
+               pdst += 2;
+       }
+       if ((slen = strlen(psrc)) > strlen(pdst)) {
+               return False;
+       }
+       return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
+}
+
 /****************************************************************************
  Rename an open file - given an fsp.
 ****************************************************************************/
 
-NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *newname, uint16 attrs, BOOL replace_if_exists)
+NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *newname, uint32 attrs, BOOL replace_if_exists)
 {
        SMB_STRUCT_STAT sbuf;
        BOOL bad_path = False;
@@ -4055,6 +4189,10 @@ NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *
                return error;
        }
 
+       if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
                DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
                        fsp->fsp_name,newname));
@@ -4078,7 +4216,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *
  code. 
 ****************************************************************************/
 
-NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, uint16 attrs, BOOL replace_if_exists)
+NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, uint32 attrs, BOOL replace_if_exists)
 {
        pstring directory;
        pstring mask;
@@ -4276,6 +4414,10 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                        return NT_STATUS_OBJECT_NAME_COLLISION;
                }
 
+               if (rename_path_prefix_equal(directory, newname)) {
+                       return NT_STATUS_SHARING_VIOLATION;
+               }
+
                if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
                        DEBUG(3,("rename_internals: succeeded doing rename on %s -> %s\n",
                                directory,newname));
@@ -4300,17 +4442,17 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                const char *dname;
                pstring destname;
                
+               if (strequal(mask,"????????.???"))
+                       pstrcpy(mask,"*");
+                       
                if (check_name(directory,conn))
-                       dir_hnd = OpenDir(conn, directory);
+                       dir_hnd = OpenDir(conn, directory, mask, attrs);
                
                if (dir_hnd) {
                        long offset = 0;
                        error = NT_STATUS_NO_SUCH_FILE;
 /*                     Was error = NT_STATUS_OBJECT_NAME_NOT_FOUND; - gentest fix. JRA */
                        
-                       if (strequal(mask,"????????.???"))
-                               pstrcpy(mask,"*");
-                       
                        while ((dname = ReadDirName(dir_hnd, &offset))) {
                                pstring fname;
                                BOOL sysdir_entry = False;
@@ -4374,6 +4516,10 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                                        continue;
                                }
                                
+                               if (rename_path_prefix_equal(fname, destname)) {
+                                       return NT_STATUS_SHARING_VIOLATION;
+                               }
+
                                if (!SMB_VFS_RENAME(conn,fname,destname)) {
                                        rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
                                        count++;
@@ -4411,7 +4557,7 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        pstring name;
        pstring newname;
        char *p;
-       uint16 attrs = SVAL(inbuf,smb_vwv0);
+       uint32 attrs = SVAL(inbuf,smb_vwv0);
        NTSTATUS status;
 
        START_PROFILE(SMBmv);
@@ -4459,48 +4605,69 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
  Copy a file as part of a reply_copy.
 ******************************************************************/
 
-static BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
+BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                      int count,BOOL target_is_directory, int *err_ret)
 {
-       int Access,action;
        SMB_STRUCT_STAT src_sbuf, sbuf2;
        SMB_OFF_T ret=-1;
        files_struct *fsp1,*fsp2;
        pstring dest;
        uint32 dosattrs;
+       uint32 new_create_disposition;
  
        *err_ret = 0;
 
        pstrcpy(dest,dest1);
        if (target_is_directory) {
                char *p = strrchr_m(src,'/');
-               if (p) 
+               if (p) {
                        p++;
-               else
+               } else {
                        p = src;
+               }
                pstrcat(dest,"/");
                pstrcat(dest,p);
        }
 
-       if (!vfs_file_exist(conn,src,&src_sbuf))
+       if (!vfs_file_exist(conn,src,&src_sbuf)) {
                return(False);
+       }
+
+       if (!target_is_directory && count) {
+               new_create_disposition = FILE_OPEN;
+       } else {
+               if (!map_open_params_to_ntcreate(dest1,0,ofun,
+                               NULL, NULL, &new_create_disposition, NULL)) {
+                       return(False);
+               }
+       }
 
-       fsp1 = open_file_shared(conn,src,&src_sbuf,SET_DENY_MODE(DENY_NONE)|SET_OPEN_MODE(DOS_OPEN_RDONLY),
-                                       (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN),FILE_ATTRIBUTE_NORMAL,INTERNAL_OPEN_ONLY,
-                                       &Access,&action);
+       fsp1 = open_file_ntcreate(conn,src,&src_sbuf,
+                       FILE_GENERIC_READ,
+                       FILE_SHARE_READ|FILE_SHARE_WRITE,
+                       FILE_OPEN,
+                       0,
+                       FILE_ATTRIBUTE_NORMAL,
+                       INTERNAL_OPEN_ONLY,
+                       NULL);
 
-       if (!fsp1)
+       if (!fsp1) {
                return(False);
-
-       if (!target_is_directory && count)
-               ofun = FILE_EXISTS_OPEN;
+       }
 
        dosattrs = dos_mode(conn, src, &src_sbuf);
-       if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1)
+       if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
                ZERO_STRUCTP(&sbuf2);
+       }
 
-       fsp2 = open_file_shared(conn,dest,&sbuf2,SET_DENY_MODE(DENY_NONE)|SET_OPEN_MODE(DOS_OPEN_WRONLY),
-                       ofun,dosattrs,INTERNAL_OPEN_ONLY,&Access,&action);
+       fsp2 = open_file_ntcreate(conn,dest,&sbuf2,
+                       FILE_GENERIC_WRITE,
+                       FILE_SHARE_READ|FILE_SHARE_WRITE,
+                       new_create_disposition,
+                       0,
+                       dosattrs,
+                       INTERNAL_OPEN_ONLY,
+                       NULL);
 
        if (!fsp2) {
                close_file(fsp1,False);
@@ -4508,7 +4675,7 @@ static BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
        }
 
        if ((ofun&3) == 1) {
-               if(SMB_VFS_LSEEK(fsp2,fsp2->fd,0,SEEK_END) == -1) {
+               if(SMB_VFS_LSEEK(fsp2,fsp2->fh->fd,0,SEEK_END) == -1) {
                        DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
                        /*
                         * Stop the copy from occurring.
@@ -4518,8 +4685,9 @@ static BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                }
        }
   
-       if (src_sbuf.st_size)
+       if (src_sbuf.st_size) {
                ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
+       }
 
        close_file(fsp1,False);
 
@@ -4656,16 +4824,16 @@ int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                const char *dname;
                pstring destname;
 
+               if (strequal(mask,"????????.???"))
+                       pstrcpy(mask,"*");
+
                if (check_name(directory,conn))
-                       dir_hnd = OpenDir(conn, directory);
+                       dir_hnd = OpenDir(conn, directory, mask, 0);
 
                if (dir_hnd) {
                        long offset = 0;
                        error = ERRbadfile;
 
-                       if (strequal(mask,"????????.???"))
-                               pstrcpy(mask,"*");
-
                        while ((dname = ReadDirName(dir_hnd, &offset))) {
                                pstring fname;
                                pstrcpy(fname,dname);
@@ -4906,7 +5074,8 @@ SMB_BIG_UINT get_lock_offset( char *data, int data_offset, BOOL large_file_forma
  Reply to a lockingX request.
 ****************************************************************************/
 
-int reply_lockingX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
+int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
+                  int length, int bufsize)
 {
        files_struct *fsp = file_fsp(inbuf,smb_vwv2);
        unsigned char locktype = CVAL(inbuf,smb_vwv3);
@@ -4918,7 +5087,8 @@ int reply_lockingX(connection_struct *conn, char *inbuf,char *outbuf,int length,
        int32 lock_timeout = IVAL(inbuf,smb_vwv4);
        int i;
        char *data;
-       BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
+       BOOL large_file_format =
+               (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
        BOOL err;
        BOOL my_lock_ctx = False;
        NTSTATUS status;
@@ -4933,7 +5103,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf,char *outbuf,int length,
                /* we don't support these - and CANCEL_LOCK makes w2k
                   and XP reboot so I don't really want to be
                   compatible! (tridge) */
-               return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
+               return ERROR_DOS(ERRDOS, ERRnoatomiclocks);
        }
        
        if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
@@ -4947,19 +5117,25 @@ int reply_lockingX(connection_struct *conn, char *inbuf,char *outbuf,int length,
        if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
                /* Client can insist on breaking to none. */
                BOOL break_to_none = (oplocklevel == 0);
-               
-               DEBUG(5,("reply_lockingX: oplock break reply (%u) from client for fnum = %d\n",
-                        (unsigned int)oplocklevel, fsp->fnum ));
+               BOOL result;
+
+               DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
+                        "for fnum = %d\n", (unsigned int)oplocklevel,
+                        fsp->fnum ));
 
                /*
-                * Make sure we have granted an exclusive or batch oplock on this file.
+                * Make sure we have granted an exclusive or batch oplock on
+                * this file.
                 */
                
-               if(!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
-                       DEBUG(0,("reply_lockingX: Error : oplock break from client for fnum = %d and \
-no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
-
-                       /* if this is a pure oplock break request then don't send a reply */
+               if (fsp->oplock_type == 0) {
+                       DEBUG(0,("reply_lockingX: Error : oplock break from "
+                                "client for fnum = %d (oplock=%d) and no "
+                                "oplock granted on this file (%s).\n",
+                                fsp->fnum, fsp->oplock_type, fsp->fsp_name));
+
+                       /* if this is a pure oplock break request then don't
+                        * send a reply */
                        if (num_locks == 0 && num_ulocks == 0) {
                                END_PROFILE(SMBlockingX);
                                return -1;
@@ -4969,17 +5145,30 @@ no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
                        }
                }
 
-               if (remove_oplock(fsp, break_to_none) == False) {
-                       DEBUG(0,("reply_lockingX: error in removing oplock on file %s\n",
-                                fsp->fsp_name ));
+               if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
+                   (break_to_none)) {
+                       result = remove_oplock(fsp);
+               } else {
+                       result = downgrade_oplock(fsp);
                }
                
-               /* if this is a pure oplock break request then don't send a reply */
+               if (!result) {
+                       DEBUG(0, ("reply_lockingX: error in removing "
+                                 "oplock on file %s\n", fsp->fsp_name));
+                       /* Hmmm. Is this panic justified? */
+                       smb_panic("internal tdb error");
+               }
+
+               reply_to_oplock_break_requests(fsp);
+
+               /* if this is a pure oplock break request then don't send a
+                * reply */
                if (num_locks == 0 && num_ulocks == 0) {
                        /* Sanity check - ensure a pure oplock break is not a
                           chained request. */
                        if(CVAL(inbuf,smb_vwv0) != 0xff)
-                               DEBUG(0,("reply_lockingX: Error : pure oplock break is a chained %d request !\n",
+                               DEBUG(0,("reply_lockingX: Error : pure oplock "
+                                        "break is a chained %d request !\n",
                                         (unsigned int)CVAL(inbuf,smb_vwv0) ));
                        END_PROFILE(SMBlockingX);
                        return -1;
@@ -5008,8 +5197,9 @@ no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
                        return ERROR_DOS(ERRDOS,ERRnoaccess);
                }
 
-               DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for pid %u, file %s\n",
-                         (double)offset, (double)count, (unsigned int)lock_pid, fsp->fsp_name ));
+               DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
+                         "pid %u, file %s\n", (double)offset, (double)count,
+                         (unsigned int)lock_pid, fsp->fsp_name ));
                
                status = do_unlock(fsp,conn,lock_pid,count,offset);
                if (NT_STATUS_V(status)) {
@@ -5041,27 +5231,34 @@ no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
                        return ERROR_DOS(ERRDOS,ERRnoaccess);
                }
                
-               DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid %u, file %s timeout = %d\n",
-                       (double)offset, (double)count, (unsigned int)lock_pid,
-                       fsp->fsp_name, (int)lock_timeout ));
+               DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
+                         "%u, file %s timeout = %d\n", (double)offset,
+                         (double)count, (unsigned int)lock_pid,
+                         fsp->fsp_name, (int)lock_timeout ));
                
                status = do_lock_spin(fsp,conn,lock_pid, count,offset, 
-                                ((locktype & 1) ? READ_LOCK : WRITE_LOCK), &my_lock_ctx);
+                                     ((locktype & 1) ? READ_LOCK:WRITE_LOCK),
+                                     &my_lock_ctx);
                if (NT_STATUS_V(status)) {
                        /*
-                        * Interesting fact found by IFSTEST /t LockOverlappedTest...
-                        * Even if it's our own lock context, we need to wait here as
-                        * there may be an unlock on the way.
-                        * So I removed a "&& !my_lock_ctx" from the following
-                        * if statement. JRA.
+                        * Interesting fact found by IFSTEST /t
+                        * LockOverlappedTest...  Even if it's our own lock
+                        * context, we need to wait here as there may be an
+                        * unlock on the way.  So I removed a "&&
+                        * !my_lock_ctx" from the following if statement. JRA.
                         */
-                       if ((lock_timeout != 0) && lp_blocking_locks(SNUM(conn)) && ERROR_WAS_LOCK_DENIED(status)) {
+                       if ((lock_timeout != 0) &&
+                           lp_blocking_locks(SNUM(conn)) &&
+                           ERROR_WAS_LOCK_DENIED(status)) {
                                /*
                                 * A blocking lock was requested. Package up
                                 * this smb into a queued request and push it
                                 * onto the blocking lock queue.
                                 */
-                               if(push_blocking_lock_request(inbuf, length, lock_timeout, i, lock_pid, offset, count)) {
+                               if(push_blocking_lock_request(inbuf, length,
+                                                             lock_timeout, i,
+                                                             lock_pid, offset,
+                                                             count)) {
                                        END_PROFILE(SMBlockingX);
                                        return -1;
                                }
@@ -5081,10 +5278,12 @@ no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
                for(i--; i >= 0; i--) {
                        lock_pid = get_lock_pid( data, i, large_file_format);
                        count = get_lock_count( data, i, large_file_format);
-                       offset = get_lock_offset( data, i, large_file_format, &err);
+                       offset = get_lock_offset( data, i, large_file_format,
+                                                 &err);
                        
                        /*
-                        * There is no error code marked "stupid client bug".... :-).
+                        * There is no error code marked "stupid client
+                        * bug".... :-).
                         */
                        if(err) {
                                END_PROFILE(SMBlockingX);
@@ -5099,8 +5298,8 @@ no oplock granted on this file (%s).\n", fsp->fnum, fsp->fsp_name));
 
        set_message(outbuf,2,0,True);
        
-       DEBUG( 3, ( "lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
-                   fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks ) );
+       DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
+                 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
        
        END_PROFILE(SMBlockingX);
        return chain_reply(inbuf,outbuf,length,bufsize);
@@ -5136,7 +5335,9 @@ int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,
        outsize = set_message(outbuf,8,0,True);
 
        CHECK_FSP(fsp,conn);
-       CHECK_READ(fsp);
+       if (!CHECK_READ(fsp,inbuf)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
        maxcount = SVAL(inbuf,smb_vwv3);
@@ -5167,12 +5368,13 @@ int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,
                if (nread < (ssize_t)N)
                        tcount = total_read + nread;
 
-               set_message(outbuf,8,nread,False);
+               set_message(outbuf,8,nread+pad,False);
                SIVAL(outbuf,smb_vwv0,startpos);
                SSVAL(outbuf,smb_vwv2,tcount);
                SSVAL(outbuf,smb_vwv6,nread);
                SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
 
+               show_msg(outbuf);
                if (!send_smb(smbd_server_fd(),outbuf))
                        exit_server("reply_readbmpx: send_smb failed.");
 
@@ -5263,7 +5465,9 @@ int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size,
        START_PROFILE(SMBwriteBmpx);
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
        if (HAS_CACHED_ERROR(fsp)) {
                return(CACHED_ERROR(fsp));
        }
@@ -5287,8 +5491,7 @@ int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size,
 
        nwritten = write_file(fsp,data,startpos,numtowrite);
 
-       if(lp_syncalways(SNUM(conn)) || write_through)
-               sync_file(conn,fsp);
+       sync_file(conn, fsp, write_through);
   
        if(nwritten < (ssize_t)numtowrite) {
                END_PROFILE(SMBwriteBmpx);
@@ -5333,6 +5536,7 @@ int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size,
        if (write_through && tcount==nwritten) {
                /* We need to send both a primary and a secondary response */
                smb_setlen(outbuf,outsize - 4);
+               show_msg(outbuf);
                if (!send_smb(smbd_server_fd(),outbuf))
                        exit_server("reply_writebmpx: send_smb failed.");
 
@@ -5366,7 +5570,9 @@ int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_siz
        START_PROFILE(SMBwriteBs);
 
        CHECK_FSP(fsp,conn);
-       CHECK_WRITE(fsp);
+       if (!CHECK_WRITE(fsp)) {
+               return(ERROR_DOS(ERRDOS,ERRbadaccess));
+       }
 
        tcount = SVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
@@ -5397,8 +5603,7 @@ int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_siz
 
        nwritten = write_file(fsp,data,startpos,numtowrite);
 
-       if(lp_syncalways(SNUM(conn)) || write_through)
-               sync_file(conn,fsp);
+       sync_file(conn, fsp, write_through);
   
        if (nwritten < (ssize_t)numtowrite) {
                if(write_through) {