Ensure we return NT_STATUS_FILE_IS_A_DIRECTORY on a posix open on a
[ira/wip.git] / source3 / smbd / trans2.c
index 4d60eecda9ec3f24445401845e9ccccf9884c013..e907902c65afcbe8de593f535602b193bad4a988 100644 (file)
 */
 
 #include "includes.h"
+#include "version.h"
+#include "smbd/globals.h"
+#include "../libcli/auth/libcli_auth.h"
 
-extern int max_send;
 extern enum protocol_types Protocol;
-extern uint32 global_client_caps;
-extern struct current_user current_user;
 
-#define get_file_size(sbuf) ((sbuf).st_size)
 #define DIR_ENTRY_SAFETY_MARGIN 4096
 
 static char *store_file_unix_basic(connection_struct *conn,
@@ -48,9 +47,9 @@ static char *store_file_unix_basic_info2(connection_struct *conn,
  Only do this for Windows clients.
 ********************************************************************/
 
-SMB_BIG_UINT smb_roundup(connection_struct *conn, SMB_BIG_UINT val)
+uint64_t smb_roundup(connection_struct *conn, uint64_t val)
 {
-       SMB_BIG_UINT rval = lp_allocation_roundup_size(SNUM(conn));
+       uint64_t rval = lp_allocation_roundup_size(SNUM(conn));
 
        /* Only roundup for Windows clients. */
        enum remote_arch_types ra_type = get_remote_arch();
@@ -60,31 +59,6 @@ SMB_BIG_UINT smb_roundup(connection_struct *conn, SMB_BIG_UINT val)
        return val;
 }
 
-/********************************************************************
- Given a stat buffer return the allocated size on disk, taking into
- account sparse files.
-********************************************************************/
-
-SMB_BIG_UINT get_allocation_size(connection_struct *conn, files_struct *fsp, const SMB_STRUCT_STAT *sbuf)
-{
-       SMB_BIG_UINT ret;
-
-       if(S_ISDIR(sbuf->st_mode)) {
-               return 0;
-       }
-
-#if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
-       ret = (SMB_BIG_UINT)STAT_ST_BLOCKSIZE * (SMB_BIG_UINT)sbuf->st_blocks;
-#else
-       ret = (SMB_BIG_UINT)get_file_size(*sbuf);
-#endif
-
-       if (fsp && fsp->initial_allocation_size)
-               ret = MAX(ret,fsp->initial_allocation_size);
-
-       return smb_roundup(conn, ret);
-}
-
 /****************************************************************************
  Utility functions for dealing with extended attributes.
 ****************************************************************************/
@@ -95,7 +69,7 @@ SMB_BIG_UINT get_allocation_size(connection_struct *conn, files_struct *fsp, con
 
 static bool samba_private_attr_name(const char *unix_ea_name)
 {
-       static const char *prohibited_ea_names[] = {
+       static const char * const prohibited_ea_names[] = {
                SAMBA_POSIX_INHERITANCE_EA_NAME,
                SAMBA_XATTR_DOS_ATTRIB,
                NULL
@@ -178,7 +152,7 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx, connection_struct *conn,
        char *p;
        char **names, **tmp;
        size_t num_names;
-       ssize_t sizeret;
+       ssize_t sizeret = -1;
 
        if (!lp_ea_support(SNUM(conn))) {
                *pnames = NULL;
@@ -504,7 +478,7 @@ NTSTATUS set_ea(connection_struct *conn, files_struct *fsp, const char *fname, s
 static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
 {
        struct ea_list *ea_list_head = NULL;
-       size_t offset = 0;
+       size_t converted_size, offset = 0;
 
        while (offset + 2 < data_size) {
                struct ea_list *eal = TALLOC_ZERO_P(ctx, struct ea_list);
@@ -522,7 +496,11 @@ static struct ea_list *read_ea_name_list(TALLOC_CTX *ctx, const char *pdata, siz
                if (pdata[offset + namelen] != '\0') {
                        return NULL;
                }
-               pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset]);
+               if (!pull_ascii_talloc(ctx, &eal->ea.name, &pdata[offset],
+                                      &converted_size)) {
+                       DEBUG(0,("read_ea_name_list: pull_ascii_talloc "
+                                "failed: %s", strerror(errno)));
+               }
                if (!eal->ea.name) {
                        return NULL;
                }
@@ -544,6 +522,7 @@ struct ea_list *read_ea_list_entry(TALLOC_CTX *ctx, const char *pdata, size_t da
        struct ea_list *eal = TALLOC_ZERO_P(ctx, struct ea_list);
        uint16 val_len;
        unsigned int namelen;
+       size_t converted_size;
 
        if (!eal) {
                return NULL;
@@ -565,7 +544,10 @@ struct ea_list *read_ea_list_entry(TALLOC_CTX *ctx, const char *pdata, size_t da
        if (pdata[namelen + 4] != '\0') {
                return NULL;
        }
-       pull_ascii_talloc(ctx, &eal->ea.name, pdata + 4);
+       if (!pull_ascii_talloc(ctx, &eal->ea.name, pdata + 4, &converted_size)) {
+               DEBUG(0,("read_ea_list_entry: pull_ascii_talloc failed: %s",
+                        strerror(errno)));
+       }
        if (!eal->ea.name) {
                return NULL;
        }
@@ -737,14 +719,16 @@ void send_trans2_replies(connection_struct *conn,
                                    + alignment_offset
                                    + data_alignment_offset);
 
-       /* useable_space can never be more than max_send minus the alignment offset. */
-
-       useable_space = MIN(useable_space, max_send - (alignment_offset+data_alignment_offset));
+       if (useable_space < 0) {
+               DEBUG(0, ("send_trans2_replies failed sanity useable_space "
+                         "= %d!!!", useable_space));
+               exit_server_cleanly("send_trans2_replies: Not enough space");
+       }
 
        while (params_to_send || data_to_send) {
                /* Calculate whether we will totally or partially fill this packet */
 
-               total_sent_thistime = params_to_send + data_to_send + alignment_offset + data_alignment_offset;
+               total_sent_thistime = params_to_send + data_to_send;
 
                /* We can never send more than useable_space */
                /*
@@ -754,9 +738,16 @@ void send_trans2_replies(connection_struct *conn,
                 * are sent here. Fix from Marc_Jacobsen@hp.com.
                 */
 
-               total_sent_thistime = MIN(total_sent_thistime, useable_space+ alignment_offset + data_alignment_offset);
+               total_sent_thistime = MIN(total_sent_thistime, useable_space);
+
+               reply_outbuf(req, 10, total_sent_thistime + alignment_offset
+                            + data_alignment_offset);
 
-               reply_outbuf(req, 10, total_sent_thistime);
+               /*
+                * We might have SMBtrans2s in req which was transferred to
+                * the outbuf, fix that.
+                */
+               SCVAL(req->outbuf, smb_com, SMBtrans2);
 
                /* Set total params and data to be sent */
                SSVAL(req->outbuf,smb_tprcnt,paramsize);
@@ -842,7 +833,9 @@ void send_trans2_replies(connection_struct *conn,
                show_msg((char *)req->outbuf);
                if (!srv_send_smb(smbd_server_fd(),
                                (char *)req->outbuf,
-                               IS_CONN_ENCRYPTED(conn)))
+                               true, req->seqnum+1,
+                               IS_CONN_ENCRYPTED(conn),
+                               &req->pcd))
                        exit_server_cleanly("send_trans2_replies: srv_send_smb failed.");
 
                TALLOC_FREE(req->outbuf);
@@ -903,6 +896,8 @@ static void call_trans2open(connection_struct *conn,
        uint32 create_options = 0;
        TALLOC_CTX *ctx = talloc_tos();
 
+       SET_STAT_INVALID(sbuf);
+
        /*
         * Ensure we have enough parameters to perform the operation.
         */
@@ -990,22 +985,24 @@ static void call_trans2open(connection_struct *conn,
                return;
        }
 
-       status = create_file(conn,                      /* conn */
-                            req,                       /* req */
-                            0,                         /* root_dir_fid */
-                            fname,                     /* fname */
-                            access_mask,               /* access_mask */
-                            share_mode,                /* share_access */
-                            create_disposition,        /* create_disposition*/
-                            create_options,            /* create_options */
-                            open_attr,                 /* file_attributes */
-                            oplock_request,            /* oplock_request */
-                            open_size,                 /* allocation_size */
-                            NULL,                      /* sd */
-                            ea_list,                   /* ea_list */
-                            &fsp,                      /* result */
-                            &smb_action,               /* pinfo */
-                            &sbuf);                    /* psbuf */
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               fname,                                  /* fname */
+               CFF_DOS_PATH,                           /* create_file_flags */
+               access_mask,                            /* access_mask */
+               share_mode,                             /* share_access */
+               create_disposition,                     /* create_disposition*/
+               create_options,                         /* create_options */
+               open_attr,                              /* file_attributes */
+               oplock_request,                         /* oplock_request */
+               open_size,                              /* allocation_size */
+               NULL,                                   /* sd */
+               ea_list,                                /* ea_list */
+               &fsp,                                   /* result */
+               &smb_action,                            /* pinfo */
+               &sbuf);                                 /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -1016,12 +1013,12 @@ static void call_trans2open(connection_struct *conn,
                return;
        }
 
-       size = get_file_size(sbuf);
+       size = get_file_size_stat(&sbuf);
        fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
        mtime = sbuf.st_mtime;
        inode = sbuf.st_ino;
        if (fattr & aDIR) {
-               close_file(fsp,ERROR_CLOSE);
+               close_file(req, fsp, ERROR_CLOSE);
                reply_doserror(req, ERRDOS,ERRnoaccess);
                return;
        }
@@ -1077,15 +1074,13 @@ static bool exact_match(connection_struct *conn,
 {
        if (mask[0] == '.' && mask[1] == 0)
                return False;
-       if (conn->case_sensitive)
-               return strcmp(str,mask)==0;
-       if (StrCaseCmp(str,mask) != 0) {
-               return False;
-       }
        if (dptr_has_wild(conn->dirptr)) {
                return False;
        }
-       return True;
+       if (conn->case_sensitive)
+               return strcmp(str,mask)==0;
+       else
+               return StrCaseCmp(str,mask) == 0;
 }
 
 /****************************************************************************
@@ -1119,7 +1114,7 @@ static uint32 unix_filetype(mode_t mode)
                return UNIX_TYPE_SOCKET;
 #endif
 
-       DEBUG(0,("unix_filetype: unknown filetype %u", (unsigned)mode));
+       DEBUG(0,("unix_filetype: unknown filetype %u\n", (unsigned)mode));
        return UNIX_TYPE_UNKNOWN;
 }
 
@@ -1243,18 +1238,18 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                                int *last_entry_off,
                                struct ea_list *name_list)
 {
-       const char *dname;
+       char *dname;
        bool found = False;
        SMB_STRUCT_STAT sbuf;
        const char *mask = NULL;
        char *pathreal = NULL;
-       const char *fname = NULL;
+       char *fname = NULL;
        char *p, *q, *pdata = *ppdata;
        uint32 reskey=0;
        long prev_dirpos=0;
        uint32 mode=0;
        SMB_OFF_T file_size = 0;
-       SMB_BIG_UINT allocation_size = 0;
+       uint64_t allocation_size = 0;
        uint32 len;
        struct timespec mdate_ts, adate_ts, create_date_ts;
        time_t mdate = (time_t)0, adate = (time_t)0, create_date = (time_t)0;
@@ -1324,9 +1319,13 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                /* Mangle fname if it's an illegal name. */
                if (mangle_must_mangle(dname,conn->params)) {
                        if (!name_to_8_3(dname,mangled_name,True,conn->params)) {
+                               TALLOC_FREE(fname);
                                continue; /* Error - couldn't mangle. */
                        }
-                       fname = mangled_name;
+                       fname = talloc_strdup(ctx, mangled_name);
+                       if (!fname) {
+                               return False;
+                       }
                }
 
                if(!(got_match = *got_exact_match = exact_match(conn, fname, mask))) {
@@ -1343,6 +1342,7 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                         */
                        /* Force the mangling into 8.3. */
                        if (!name_to_8_3( fname, mangled_name, False, conn->params)) {
+                               TALLOC_FREE(fname);
                                continue; /* Error - couldn't mangle. */
                        }
 
@@ -1355,6 +1355,7 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        bool isdots = (ISDOT(dname) || ISDOTDOT(dname));
 
                        if (dont_descend && !isdots) {
+                               TALLOC_FREE(fname);
                                continue;
                        }
 
@@ -1372,6 +1373,7 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        }
 
                        if (!pathreal) {
+                               TALLOC_FREE(fname);
                                return False;
                        }
 
@@ -1380,6 +1382,7 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                                        DEBUG(5,("get_lanman2_dir_entry:Couldn't lstat [%s] (%s)\n",
                                                pathreal,strerror(errno)));
                                        TALLOC_FREE(pathreal);
+                                       TALLOC_FREE(fname);
                                        continue;
                                }
                        } else if (!VALID_STAT(sbuf) && SMB_VFS_STAT(conn,pathreal,&sbuf) != 0) {
@@ -1391,6 +1394,7 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                                        DEBUG(5,("get_lanman2_dir_entry:Couldn't stat [%s] (%s)\n",
                                                pathreal,strerror(errno)));
                                        TALLOC_FREE(pathreal);
+                                       TALLOC_FREE(fname);
                                        continue;
                                }
                        }
@@ -1404,17 +1408,19 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        if (!dir_check_ftype(conn,mode,dirtype)) {
                                DEBUG(5,("get_lanman2_dir_entry: [%s] attribs didn't match %x\n",fname,dirtype));
                                TALLOC_FREE(pathreal);
+                               TALLOC_FREE(fname);
                                continue;
                        }
 
                        if (!(mode & aDIR)) {
-                               file_size = get_file_size(sbuf);
+                               file_size = get_file_size_stat(&sbuf);
                        }
-                       allocation_size = get_allocation_size(conn,NULL,&sbuf);
+                       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,NULL,&sbuf);
 
                        mdate_ts = get_mtimespec(&sbuf);
                        adate_ts = get_atimespec(&sbuf);
-                       create_date_ts = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
+                       create_date_ts = get_create_timespec(&sbuf,
+                           lp_fake_dir_create_times(SNUM(conn)));
 
                        if (ask_sharemode) {
                                struct timespec write_time_ts;
@@ -1437,12 +1443,16 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        mdate = convert_timespec_to_time_t(mdate_ts);
                        adate = convert_timespec_to_time_t(adate_ts);
 
-                       DEBUG(5,("get_lanman2_dir_entry: found %s fname=%s\n",pathreal,fname));
+                       DEBUG(5,("get_lanman2_dir_entry: found %s fname=%s\n",
+                               pathreal,fname));
 
                        found = True;
 
                        dptr_DirCacheAdd(conn->dirptr, dname, curr_dirpos);
                }
+
+               if (!found)
+                       TALLOC_FREE(fname);
        }
 
        p = pdata;
@@ -1836,10 +1846,11 @@ static bool get_lanman2_dir_entry(TALLOC_CTX *ctx,
                        break;
 
                default:
+                       TALLOC_FREE(fname);
                        return(False);
        }
 
-
+       TALLOC_FREE(fname);
        if (PTR_DIFF(p,pdata) > space_remaining) {
                /* Move the dirptr back to prev_dirpos */
                dptr_SeekDir(conn->dirptr, prev_dirpos);
@@ -1871,6 +1882,7 @@ static void call_trans2findfirst(connection_struct *conn,
                maxentries then so be it. We assume that the redirector has
                enough room for the fixed number of parameter bytes it has
                requested. */
+       struct smb_filename *smb_dname = NULL;
        char *params = *pparams;
        char *pdata = *ppdata;
        char *data_end;
@@ -1882,7 +1894,7 @@ static void call_trans2findfirst(connection_struct *conn,
        bool requires_resume_key;
        int info_level;
        char *directory = NULL;
-       const char *mask = NULL;
+       char *mask = NULL;
        char *p;
        int last_entry_off=0;
        int dptr_num = -1;
@@ -1893,7 +1905,6 @@ static void call_trans2findfirst(connection_struct *conn,
        bool out_of_space = False;
        int space_remaining;
        bool mask_contains_wcard = False;
-       SMB_STRUCT_STAT sbuf;
        struct ea_list *ea_list = NULL;
        NTSTATUS ntstatus = NT_STATUS_OK;
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
@@ -1921,7 +1932,7 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                /* W2K3 seems to treat zero as 1. */
                maxentries = 1;
        }
+
        switch (info_level) {
                case SMB_FIND_INFO_STANDARD:
                case SMB_FIND_EA_SIZE:
@@ -1935,6 +1946,8 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                        break;
                case SMB_FIND_FILE_UNIX:
                case SMB_FIND_FILE_UNIX_INFO2:
+                       /* Always use filesystem for UNIX mtime query. */
+                       ask_sharemode = false;
                        if (!lp_unix_extensions()) {
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
@@ -1968,7 +1981,17 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                return;
        }
 
-       ntstatus = unix_convert(ctx, conn, directory, True, &directory, NULL, &sbuf);
+       ntstatus = unix_convert(ctx, conn, directory, &smb_dname,
+                               (UCF_SAVE_LCOMP | UCF_ALLOW_WCARD_LCOMP));
+       if (!NT_STATUS_IS_OK(ntstatus)) {
+               reply_nterror(req, ntstatus);
+               return;
+       }
+
+       mask = smb_dname->original_lcomp;
+
+       ntstatus = get_full_smb_filename(ctx, smb_dname, &directory);
+       TALLOC_FREE(smb_dname);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                reply_nterror(req, ntstatus);
                return;
@@ -1984,10 +2007,12 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
        if(p == NULL) {
                /* Windows and OS/2 systems treat search on the root '\' as if it were '\*' */
                if((directory[0] == '.') && (directory[1] == '\0')) {
-                       mask = "*";
+                       mask = talloc_strdup(ctx,"*");
+                       if (!mask) {
+                               reply_nterror(req, NT_STATUS_NO_MEMORY);
+                               return;
+                       }
                        mask_contains_wcard = True;
-               } else {
-                       mask = directory;
                }
                directory = talloc_strdup(talloc_tos(), "./");
                if (!directory) {
@@ -1995,7 +2020,6 @@ close_if_end = %d requires_resume_key = %d level = 0x%x, max_data_bytes = %d\n",
                        return;
                }
        } else {
-               mask = p+1;
                *p = 0;
        }
 
@@ -2047,7 +2071,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        }
        params = *pparams;
 
-       /* Save the wildcard match and attribs we are using on this directory - 
+       /* Save the wildcard match and attribs we are using on this directory -
                needed as lanman2 assumes these are being saved between calls */
 
        ntstatus = dptr_create(conn,
@@ -2068,6 +2092,9 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        dptr_num = dptr_dnum(conn->dirptr);
        DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
 
+       /* Initialize per TRANS2_FIND_FIRST operation data */
+       dptr_init_search_op(conn->dirptr);
+
        /* We don't need to check for VOL here as this is returned by
                a different TRANS2 call. */
 
@@ -2082,7 +2109,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        for (i=0;(i<maxentries) && !finished && !out_of_space;i++) {
                bool got_exact_match = False;
 
-               /* this is a heuristic to avoid seeking the dirptr except when 
+               /* this is a heuristic to avoid seeking the dirptr except when
                        absolutely necessary. It allows for a filename of about 40 chars */
                if (space_remaining < DIRLEN_GUESS && numentries > 0) {
                        out_of_space = True;
@@ -2170,7 +2197,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        }
 
        DEBUG( 4, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
-               smb_fn_name(CVAL(req->inbuf,smb_com)),
+               smb_fn_name(req->cmd),
                mask, directory, dirtype, numentries ) );
 
        /*
@@ -2292,6 +2319,8 @@ resume_key = %d resume name = %s continue=%d level = %d\n",
                        break;
                case SMB_FIND_FILE_UNIX:
                case SMB_FIND_FILE_UNIX_INFO2:
+                       /* Always use filesystem for UNIX mtime query. */
+                       ask_sharemode = false;
                        if (!lp_unix_extensions()) {
                                reply_nterror(req, NT_STATUS_INVALID_LEVEL);
                                return;
@@ -2376,6 +2405,9 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                (long)conn->dirptr,
                dptr_TellDir(conn->dirptr)));
 
+       /* Initialize per TRANS2_FIND_NEXT operation data */
+       dptr_init_search_op(conn->dirptr);
+
        /* We don't need to check for VOL here as this is returned by
                a different TRANS2 call. */
 
@@ -2466,7 +2498,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        }
 
        DEBUG( 3, ( "%s mask=%s directory=%s dirtype=%d numentries=%d\n",
-               smb_fn_name(CVAL(req->inbuf,smb_com)),
+               smb_fn_name(req->cmd),
                mask, directory, dirtype, numentries ) );
 
        /* Check if we can close the dirptr */
@@ -2598,22 +2630,22 @@ static void call_trans2qfsinfo(connection_struct *conn,
        switch (info_level) {
                case SMB_INFO_ALLOCATION:
                {
-                       SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
+                       uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
                        data_len = 18;
-                       if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
+                       if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
                                reply_unixerror(req, ERRHRD, ERRgeneral);
                                return;
                        }
 
                        block_size = lp_block_size(snum);
                        if (bsize < block_size) {
-                               SMB_BIG_UINT factor = block_size/bsize;
+                               uint64_t factor = block_size/bsize;
                                bsize = block_size;
                                dsize /= factor;
                                dfree /= factor;
                        }
                        if (bsize > block_size) {
-                               SMB_BIG_UINT factor = bsize/block_size;
+                               uint64_t factor = bsize/block_size;
                                bsize = block_size;
                                dsize *= factor;
                                dfree *= factor;
@@ -2718,21 +2750,21 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)st.st_dev, (unsi
                case SMB_QUERY_FS_SIZE_INFO:
                case SMB_FS_SIZE_INFORMATION:
                {
-                       SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
+                       uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
                        data_len = 24;
-                       if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
+                       if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
                                reply_unixerror(req, ERRHRD, ERRgeneral);
                                return;
                        }
                        block_size = lp_block_size(snum);
                        if (bsize < block_size) {
-                               SMB_BIG_UINT factor = block_size/bsize;
+                               uint64_t factor = block_size/bsize;
                                bsize = block_size;
                                dsize /= factor;
                                dfree /= factor;
                        }
                        if (bsize > block_size) {
-                               SMB_BIG_UINT factor = bsize/block_size;
+                               uint64_t factor = bsize/block_size;
                                bsize = block_size;
                                dsize *= factor;
                                dfree *= factor;
@@ -2751,21 +2783,21 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
 
                case SMB_FS_FULL_SIZE_INFORMATION:
                {
-                       SMB_BIG_UINT dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
+                       uint64_t dfree,dsize,bsize,block_size,sectors_per_unit,bytes_per_sector;
                        data_len = 32;
-                       if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
+                       if (get_dfree_info(conn,".",False,&bsize,&dfree,&dsize) == (uint64_t)-1) {
                                reply_unixerror(req, ERRHRD, ERRgeneral);
                                return;
                        }
                        block_size = lp_block_size(snum);
                        if (bsize < block_size) {
-                               SMB_BIG_UINT factor = block_size/bsize;
+                               uint64_t factor = block_size/bsize;
                                bsize = block_size;
                                dsize /= factor;
                                dfree /= factor;
                        }
                        if (bsize > block_size) {
-                               SMB_BIG_UINT factor = bsize/block_size;
+                               uint64_t factor = bsize/block_size;
                                bsize = block_size;
                                dsize *= factor;
                                dfree *= factor;
@@ -2796,8 +2828,8 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                 * what we have to send --metze:
                 *
                 * Unknown1:            24 NULL bytes
-                * Soft Quota Treshold: 8 bytes seems like SMB_BIG_UINT or so
-                * Hard Quota Limit:    8 bytes seems like SMB_BIG_UINT or so
+                * Soft Quota Treshold: 8 bytes seems like uint64_t or so
+                * Hard Quota Limit:    8 bytes seems like uint64_t or so
                 * Quota Flags:         2 byte :
                 * Unknown3:            6 NULL bytes
                 *
@@ -2825,9 +2857,11 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        fsp.fnum = -1;
                        
                        /* access check */
-                       if (current_user.ut.uid != 0) {
-                               DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
-                                       lp_servicename(SNUM(conn)),conn->user));
+                       if (conn->server_info->utok.uid != 0) {
+                               DEBUG(0,("set_user_quota: access_denied "
+                                        "service [%s] user [%s]\n",
+                                        lp_servicename(SNUM(conn)),
+                                        conn->server_info->unix_name));
                                reply_doserror(req, ERRDOS, ERRnoaccess);
                                return;
                        }
@@ -2843,9 +2877,9 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        DEBUG(10,("SMB_FS_QUOTA_INFORMATION: for service [%s]\n",lp_servicename(SNUM(conn))));          
                
                        /* Unknown1 24 NULL bytes*/
-                       SBIG_UINT(pdata,0,(SMB_BIG_UINT)0);
-                       SBIG_UINT(pdata,8,(SMB_BIG_UINT)0);
-                       SBIG_UINT(pdata,16,(SMB_BIG_UINT)0);
+                       SBIG_UINT(pdata,0,(uint64_t)0);
+                       SBIG_UINT(pdata,8,(uint64_t)0);
+                       SBIG_UINT(pdata,16,(uint64_t)0);
                
                        /* Default Soft Quota 8 bytes */
                        SBIG_UINT(pdata,24,quotas.softlim);
@@ -2886,8 +2920,8 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                case SMB_QUERY_CIFS_UNIX_INFO:
                {
                        bool large_write = lp_min_receive_file_size() &&
-                                               !srv_is_signing_active();
-                       bool large_read = !srv_is_signing_active();
+                                       !srv_is_signing_active(smbd_server_conn);
+                       bool large_read = !srv_is_signing_active(smbd_server_conn);
                        int encrypt_caps = 0;
 
                        if (!lp_unix_extensions()) {
@@ -2918,7 +2952,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        /* We have POSIX ACLs, pathname, encryption, 
                         * large read/write, and locking capability. */
 
-                       SBIG_UINT(pdata,4,((SMB_BIG_UINT)(
+                       SBIG_UINT(pdata,4,((uint64_t)(
                                        CIFS_UNIX_POSIX_ACLS_CAP|
                                        CIFS_UNIX_POSIX_PATHNAMES_CAP|
                                        CIFS_UNIX_FCNTL_LOCKS_CAP|
@@ -2987,7 +3021,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                         * in our list of SIDs.
                         */
                        if (nt_token_check_sid(&global_sid_Builtin_Guests,
-                                   current_user.nt_user_token)) {
+                                              conn->server_info->ptok)) {
                                flags |= SMB_WHOAMI_GUEST;
                        }
 
@@ -2995,7 +3029,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                         * is in our list of SIDs.
                         */
                        if (nt_token_check_sid(&global_sid_Authenticated_Users,
-                                   current_user.nt_user_token)) {
+                                              conn->server_info->ptok)) {
                                flags &= ~SMB_WHOAMI_GUEST;
                        }
 
@@ -3011,16 +3045,18 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                            + 4 /* num_sids */
                            + 4 /* SID bytes */
                            + 4 /* pad/reserved */
-                           + (current_user.ut.ngroups * 8)
+                           + (conn->server_info->utok.ngroups * 8)
                                /* groups list */
-                           + (current_user.nt_user_token->num_sids *
+                           + (conn->server_info->ptok->num_sids *
                                    SID_MAX_SIZE)
                                /* SID list */;
 
                        SIVAL(pdata, 0, flags);
                        SIVAL(pdata, 4, SMB_WHOAMI_MASK);
-                       SBIG_UINT(pdata, 8, (SMB_BIG_UINT)current_user.ut.uid);
-                       SBIG_UINT(pdata, 16, (SMB_BIG_UINT)current_user.ut.gid);
+                       SBIG_UINT(pdata, 8,
+                                 (uint64_t)conn->server_info->utok.uid);
+                       SBIG_UINT(pdata, 16,
+                                 (uint64_t)conn->server_info->utok.gid);
 
 
                        if (data_len >= max_data_bytes) {
@@ -3035,18 +3071,19 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                                break;
                        }
 
-                       SIVAL(pdata, 24, current_user.ut.ngroups);
-                       SIVAL(pdata, 28,
-                               current_user.nt_user_token->num_sids);
+                       SIVAL(pdata, 24, conn->server_info->utok.ngroups);
+                       SIVAL(pdata, 28, conn->server_info->num_sids);
 
                        /* We walk the SID list twice, but this call is fairly
                         * infrequent, and I don't expect that it's performance
                         * sensitive -- jpeach
                         */
                        for (i = 0, sid_bytes = 0;
-                           i < current_user.nt_user_token->num_sids; ++i) {
+                            i < conn->server_info->ptok->num_sids; ++i) {
                                sid_bytes += ndr_size_dom_sid(
-                                       &current_user.nt_user_token->user_sids[i], 0);
+                                       &conn->server_info->ptok->user_sids[i],
+                                       NULL, 
+                                       0);
                        }
 
                        /* SID list byte count */
@@ -3057,20 +3094,22 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                        data_len = 40;
 
                        /* GID list */
-                       for (i = 0; i < current_user.ut.ngroups; ++i) {
+                       for (i = 0; i < conn->server_info->utok.ngroups; ++i) {
                                SBIG_UINT(pdata, data_len,
-                                       (SMB_BIG_UINT)current_user.ut.groups[i]);
+                                         (uint64_t)conn->server_info->utok.groups[i]);
                                data_len += 8;
                        }
 
                        /* SID list */
                        for (i = 0;
-                           i < current_user.nt_user_token->num_sids; ++i) {
+                           i < conn->server_info->ptok->num_sids; ++i) {
                                int sid_len = ndr_size_dom_sid(
-                                       &current_user.nt_user_token->user_sids[i], 0);
+                                       &conn->server_info->ptok->user_sids[i],
+                                       NULL,
+                                       0);
 
                                sid_linearize(pdata + data_len, sid_len,
-                                   &current_user.nt_user_token->user_sids[i]);
+                                   &conn->server_info->ptok->user_sids[i]);
                                data_len += sid_len;
                        }
 
@@ -3098,7 +3137,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                            max_data_bytes);
 
        DEBUG( 4, ( "%s info_level = %d\n",
-                   smb_fn_name(CVAL(req->inbuf,smb_com)), info_level) );
+                   smb_fn_name(req->cmd), info_level) );
 
        return;
 }
@@ -3265,9 +3304,11 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                ZERO_STRUCT(quotas);
 
                                /* access check */
-                               if ((current_user.ut.uid != 0)||!CAN_WRITE(conn)) {
+                               if ((conn->server_info->utok.uid != 0)
+                                   ||!CAN_WRITE(conn)) {
                                        DEBUG(0,("set_user_quota: access_denied service [%s] user [%s]\n",
-                                               lp_servicename(SNUM(conn)),conn->user));
+                                                lp_servicename(SNUM(conn)),
+                                                conn->server_info->unix_name));
                                        reply_doserror(req, ERRSRV, ERRaccess);
                                        return;
                                }
@@ -3276,8 +3317,10 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                 * but we didn't use the last 6 bytes for now 
                                 * --metze 
                                 */
-                               fsp = file_fsp(SVAL(params,0));
-                               if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
+                               fsp = file_fsp(req, SVAL(params,0));
+
+                               if (!check_fsp_ntquota_handle(conn, req,
+                                                             fsp)) {
                                        DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
                                        reply_nterror(
                                                req, NT_STATUS_INVALID_HANDLE);
@@ -3295,10 +3338,10 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                        
                                /* unknown_1 24 NULL bytes in pdata*/
                
-                               /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
-                               quotas.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
+                               /* the soft quotas 8 bytes (uint64_t)*/
+                               quotas.softlim = (uint64_t)IVAL(pdata,24);
 #ifdef LARGE_SMB_OFF_T
-                               quotas.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
+                               quotas.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
 #else /* LARGE_SMB_OFF_T */
                                if ((IVAL(pdata,28) != 0)&&
                                        ((quotas.softlim != 0xFFFFFFFF)||
@@ -3311,10 +3354,10 @@ cap_low = 0x%x, cap_high = 0x%x\n",
                                }
 #endif /* LARGE_SMB_OFF_T */
                
-                               /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
-                               quotas.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
+                               /* the hard quotas 8 bytes (uint64_t)*/
+                               quotas.hardlim = (uint64_t)IVAL(pdata,32);
 #ifdef LARGE_SMB_OFF_T
-                               quotas.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
+                               quotas.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
 #else /* LARGE_SMB_OFF_T */
                                if ((IVAL(pdata,36) != 0)&&
                                        ((quotas.hardlim != 0xFFFFFFFF)||
@@ -3489,10 +3532,10 @@ static char *store_file_unix_basic(connection_struct *conn,
        DEBUG(10,("store_file_unix_basic: SMB_QUERY_FILE_UNIX_BASIC\n"));
        DEBUG(4,("store_file_unix_basic: st_mode=%o\n",(int)psbuf->st_mode));
 
-       SOFF_T(pdata,0,get_file_size(*psbuf));             /* File size 64 Bit */
+       SOFF_T(pdata,0,get_file_size_stat(psbuf));             /* File size 64 Bit */
        pdata += 8;
 
-       SOFF_T(pdata,0,get_allocation_size(conn,fsp,psbuf)); /* Number of bytes used on disk - 64 Bit */
+       SOFF_T(pdata,0,SMB_VFS_GET_ALLOC_SIZE(conn,fsp,psbuf)); /* Number of bytes used on disk - 64 Bit */
        pdata += 8;
 
        put_long_date_timespec(pdata,get_ctimespec(psbuf));       /* Change Time 64 Bit */
@@ -3657,15 +3700,15 @@ static NTSTATUS marshall_stream_info(unsigned int num_streams,
        unsigned int i;
        unsigned int ofs = 0;
 
-       for (i=0; i<num_streams; i++) {
+       for (i = 0; i < num_streams && ofs <= max_data_bytes; i++) {
                unsigned int next_offset;
                size_t namelen;
                smb_ucs2_t *namebuf;
 
-               namelen = push_ucs2_talloc(talloc_tos(), &namebuf,
-                                           streams[i].name);
-
-               if ((namelen == (size_t)-1) || (namelen <= 2)) {
+               if (!push_ucs2_talloc(talloc_tos(), &namebuf,
+                                     streams[i].name, &namelen) ||
+                   namelen <= 2)
+               {
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
@@ -3676,11 +3719,6 @@ static NTSTATUS marshall_stream_info(unsigned int num_streams,
 
                namelen -= 2;
 
-               if (ofs + 24 + namelen > max_data_bytes) {
-                       TALLOC_FREE(namebuf);
-                       return NT_STATUS_BUFFER_TOO_SMALL;
-               }
-
                SIVAL(data, ofs+4, namelen);
                SOFF_T(data, ofs+8, streams[i].size);
                SOFF_T(data, ofs+16, streams[i].alloc_size);
@@ -3695,10 +3733,6 @@ static NTSTATUS marshall_stream_info(unsigned int num_streams,
                else {
                        unsigned int align = ndr_align_size(next_offset, 8);
 
-                       if (next_offset + align > max_data_bytes) {
-                               return NT_STATUS_BUFFER_TOO_SMALL;
-                       }
-
                        memset(data+next_offset, 0, align);
                        next_offset += align;
 
@@ -3730,7 +3764,7 @@ static void call_trans2qpipeinfo(connection_struct *conn,
        unsigned int data_size = 0;
        unsigned int param_size = 2;
        uint16 info_level;
-       smb_np_struct *p_pipe = NULL;
+       files_struct *fsp;
 
        if (!params) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -3742,8 +3776,8 @@ static void call_trans2qpipeinfo(connection_struct *conn,
                return;
        }
 
-       p_pipe = get_rpc_pipe_p(SVAL(params,0));
-       if (p_pipe == NULL) {
+       fsp = file_fsp(req, SVAL(params,0));
+       if (!fsp_is_np(fsp)) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return;
        }
@@ -3804,12 +3838,13 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        int mode=0;
        int nlink;
        SMB_OFF_T file_size=0;
-       SMB_BIG_UINT allocation_size=0;
+       uint64_t allocation_size=0;
        unsigned int data_size = 0;
        unsigned int param_size = 2;
        SMB_STRUCT_STAT sbuf;
        char *dos_fname = NULL;
        char *fname = NULL;
+       struct smb_filename *smb_fname = NULL;
        char *fullpathname;
        char *base_name;
        char *p;
@@ -3822,7 +3857,6 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
        files_struct *fsp = NULL;
        struct file_id fileid;
        struct ea_list *ea_list = NULL;
-       uint32 access_mask = 0x12019F; /* Default - GENERIC_EXECUTE mapping from Windows */
        char *lock_data = NULL;
        bool ms_dfs_link = false;
        TALLOC_CTX *ctx = talloc_tos();
@@ -3849,7 +3883,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               fsp = file_fsp(SVAL(params,0));
+               fsp = file_fsp(req, SVAL(params,0));
                info_level = SVAL(params,2);
 
                DEBUG(3,("call_trans2qfilepathinfo: TRANSACT2_QFILEINFO: level = %d\n", info_level));
@@ -3860,7 +3894,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                }
 
                /* Initial check for valid fsp ptr. */
-               if (!check_fsp_open(conn, req, fsp, &current_user)) {
+               if (!check_fsp_open(conn, req, fsp)) {
                        return;
                }
 
@@ -3903,7 +3937,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        /*
                         * Original code - this is an open file.
                         */
-                       if (!check_fsp(conn, req, fsp, &current_user)) {
+                       if (!check_fsp(conn, req, fsp)) {
                                return;
                        }
 
@@ -3915,7 +3949,6 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        pos = fsp->fh->position_information;
                        fileid = vfs_file_id_from_sbuf(conn, &sbuf);
                        get_file_infos(fileid, &delete_pending, &write_time_ts);
-                       access_mask = fsp->access_mask;
                }
 
        } else {
@@ -3959,11 +3992,20 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
+               status = unix_convert(ctx, conn, fname, &smb_fname, 0);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        return;
                }
+               sbuf = smb_fname->st;
+
+               status = get_full_smb_filename(ctx, smb_fname, &fname);
+               TALLOC_FREE(smb_fname);
+               if (!NT_STATUS_IS_OK(status)) {
+                       reply_nterror(req, status);
+                       return;
+               }
+
                status = check_name(conn, fname);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(3,("call_trans2qfilepathinfo: fileinfo of %s failed (%s)\n",fname,nt_errstr(status)));
@@ -3971,6 +4013,46 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
                        return;
                }
 
+               if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
+                   && is_ntfs_stream_name(fname)) {
+                       char *base;
+                       SMB_STRUCT_STAT bsbuf;
+
+                       status = split_ntfs_stream_name(talloc_tos(), fname,
+                                                       &base, NULL);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               DEBUG(10, ("create_file_unixpath: "
+                                       "split_ntfs_stream_name failed: %s\n",
+                                       nt_errstr(status)));
+                               reply_nterror(req, status);
+                               return;
+                       }
+
+                       SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
+
+                       if (INFO_LEVEL_IS_UNIX(info_level)) {
+                               /* Always do lstat for UNIX calls. */
+                               if (SMB_VFS_LSTAT(conn,base,&bsbuf)) {
+                                       DEBUG(3,("call_trans2qfilepathinfo: SMB_VFS_LSTAT of %s failed (%s)\n",base,strerror(errno)));
+                                       reply_unixerror(req,ERRDOS,ERRbadpath);
+                                       return;
+                               }
+                       } else {
+                               if (SMB_VFS_STAT(conn,base,&bsbuf) != 0) {
+                                       DEBUG(3,("call_trans2qfilepathinfo: fileinfo of %s failed (%s)\n",base,strerror(errno)));
+                                       reply_unixerror(req,ERRDOS,ERRbadpath);
+                                       return;
+                               }
+                       }
+
+                       fileid = vfs_file_id_from_sbuf(conn, &bsbuf);
+                       get_file_infos(fileid, &delete_pending, NULL);
+                       if (delete_pending) {
+                               reply_nterror(req, NT_STATUS_DELETE_PENDING);
+                               return;
+                       }
+               }
+
                if (INFO_LEVEL_IS_UNIX(info_level)) {
                        /* Always do lstat for UNIX calls. */
                        if (SMB_VFS_LSTAT(conn,fname,&sbuf)) {
@@ -4031,7 +4113,7 @@ static void call_trans2qfilepathinfo(connection_struct *conn,
 
        fullpathname = fname;
        if (!(mode & aDIR))
-               file_size = get_file_size(sbuf);
+               file_size = get_file_size_stat(&sbuf);
 
        /* Pull out any data sent here before we realloc. */
        switch (info_level) {
@@ -4117,7 +4199,7 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        mtime_ts = get_mtimespec(&sbuf);
        atime_ts = get_atimespec(&sbuf);
 
-       allocation_size = get_allocation_size(conn,fsp,&sbuf);
+       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf);
 
        if (!fsp) {
                /* Do we have this path open ? */
@@ -4125,11 +4207,11 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                fileid = vfs_file_id_from_sbuf(conn, &sbuf);
                fsp1 = file_find_di_first(fileid);
                if (fsp1 && fsp1->initial_allocation_size) {
-                       allocation_size = get_allocation_size(conn, fsp1, &sbuf);
+                       allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, fsp1, &sbuf);
                }
        }
 
-       if (!null_timespec(write_time_ts)) {
+       if (!null_timespec(write_time_ts) && !INFO_LEVEL_IS_UNIX(info_level)) {
                mtime_ts = write_time_ts;
        }
 
@@ -4379,7 +4461,12 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                case SMB_FILE_ACCESS_INFORMATION:
                        DEBUG(10,("call_trans2qfilepathinfo: SMB_FILE_ACCESS_INFORMATION\n"));
-                       SIVAL(pdata,0,access_mask);
+                       if (fsp) {
+                               SIVAL(pdata,0,fsp->access_mask);
+                       } else {
+                               /* GENERIC_EXECUTE mapping from Windows */
+                               SIVAL(pdata,0,0x12019F);
+                       }
                        data_size = 4;
                        break;
 
@@ -4658,8 +4745,8 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                case SMB_QUERY_POSIX_LOCK:
                {
                        NTSTATUS status = NT_STATUS_INVALID_LEVEL;
-                       SMB_BIG_UINT count;
-                       SMB_BIG_UINT offset;
+                       uint64_t count;
+                       uint64_t offset;
                        uint32 lock_pid;
                        enum brl_type lock_type;
 
@@ -4687,13 +4774,13 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
 
                        lock_pid = IVAL(pdata, POSIX_LOCK_PID_OFFSET);
 #if defined(HAVE_LONGLONG)
-                       offset = (((SMB_BIG_UINT) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
-                                       ((SMB_BIG_UINT) IVAL(pdata,POSIX_LOCK_START_OFFSET));
-                       count = (((SMB_BIG_UINT) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
-                                       ((SMB_BIG_UINT) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
+                       offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
+                                       ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
+                       count = (((uint64_t) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
+                                       ((uint64_t) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
 #else /* HAVE_LONGLONG */
-                       offset = (SMB_BIG_UINT)IVAL(pdata,POSIX_LOCK_START_OFFSET);
-                       count = (SMB_BIG_UINT)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
+                       offset = (uint64_t)IVAL(pdata,POSIX_LOCK_START_OFFSET);
+                       count = (uint64_t)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
 #endif /* HAVE_LONGLONG */
 
                        status = query_lock(fsp,
@@ -4754,57 +4841,64 @@ NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
                const char *oldname_in,
                const char *newname_in)
 {
-       SMB_STRUCT_STAT sbuf1, sbuf2;
-       char *last_component_oldname = NULL;
-       char *last_component_newname = NULL;
+       struct smb_filename *smb_fname = NULL;
+       struct smb_filename *smb_fname_new = NULL;
        char *oldname = NULL;
        char *newname = NULL;
        NTSTATUS status = NT_STATUS_OK;
 
-       ZERO_STRUCT(sbuf1);
-       ZERO_STRUCT(sbuf2);
+       status = unix_convert(ctx, conn, oldname_in, &smb_fname, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
 
-       status = unix_convert(ctx, conn, oldname_in, False, &oldname,
-                       &last_component_oldname, &sbuf1);
+       status = get_full_smb_filename(ctx, smb_fname, &oldname);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
        status = check_name(conn, oldname);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
        /* source must already exist. */
-       if (!VALID_STAT(sbuf1)) {
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       if (!VALID_STAT(smb_fname->st)) {
+               status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               goto out;
        }
 
-       status = unix_convert(ctx, conn, newname_in, False, &newname,
-                       &last_component_newname, &sbuf2);
+       status = unix_convert(ctx, conn, newname_in, &smb_fname_new, 0);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
+       }
+
+       status = get_full_smb_filename(ctx, smb_fname_new, &newname);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
        status = check_name(conn, newname);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
        /* Disallow if newname already exists. */
-       if (VALID_STAT(sbuf2)) {
-               return NT_STATUS_OBJECT_NAME_COLLISION;
+       if (VALID_STAT(smb_fname_new->st)) {
+               status = NT_STATUS_OBJECT_NAME_COLLISION;
+               goto out;
        }
 
        /* No links from a directory. */
-       if (S_ISDIR(sbuf1.st_mode)) {
-               return NT_STATUS_FILE_IS_A_DIRECTORY;
+       if (S_ISDIR(smb_fname->st.st_mode)) {
+               status = NT_STATUS_FILE_IS_A_DIRECTORY;
+               goto out;
        }
 
        /* Ensure this is within the share. */
        status = check_reduced_name(conn, oldname);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto out;
        }
 
        DEBUG(10,("hardlink_internals: doing hard link %s -> %s\n", newname, oldname ));
@@ -4812,9 +4906,15 @@ NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
        if (SMB_VFS_LINK(conn,oldname,newname) != 0) {
                status = map_nt_error_from_unix(errno);
                DEBUG(3,("hardlink_internals: Error %s hard link %s -> %s\n",
-                                nt_errstr(status), newname, oldname));
+                                nt_errstr(status), newname, oldname));
+       }
+ out:
+       if (smb_fname) {
+               TALLOC_FREE(smb_fname);
+       }
+       if (smb_fname_new) {
+               TALLOC_FREE(smb_fname_new);
        }
-
        return status;
 }
 
@@ -4826,7 +4926,7 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
                           files_struct *fsp,
                           const char *fname,
                           const SMB_STRUCT_STAT *psbuf,
-                          struct timespec ts[2],
+                          struct smb_file_time *ft,
                           bool setting_write_time)
 {
        uint32 action =
@@ -4838,23 +4938,29 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
        }
 
        /* get some defaults (no modifications) if any info is zero or -1. */
-       if (null_timespec(ts[0])) {
-               ts[0] = get_atimespec(psbuf);
+       if (null_timespec(ft->atime)) {
+               ft->atime= get_atimespec(psbuf);
                action &= ~FILE_NOTIFY_CHANGE_LAST_ACCESS;
        }
 
-       if (null_timespec(ts[1])) {
-               ts[1] = get_mtimespec(psbuf);
+       if (null_timespec(ft->mtime)) {
+               ft->mtime = get_mtimespec(psbuf);
                action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
 
        if (!setting_write_time) {
-               /* ts[1] comes from change time, not write time. */
+               /* ft->mtime comes from change time, not write time. */
                action &= ~FILE_NOTIFY_CHANGE_LAST_WRITE;
        }
 
-       DEBUG(6,("smb_set_file_time: actime: %s " , time_to_asc(convert_timespec_to_time_t(ts[0])) ));
-       DEBUG(6,("smb_set_file_time: modtime: %s ", time_to_asc(convert_timespec_to_time_t(ts[1])) ));
+       DEBUG(5,("smb_set_filetime: actime: %s\n ",
+               time_to_asc(convert_timespec_to_time_t(ft->atime))));
+       DEBUG(5,("smb_set_filetime: modtime: %s\n ",
+               time_to_asc(convert_timespec_to_time_t(ft->mtime))));
+       if (!null_timespec(ft->create_time)) {
+               DEBUG(5,("smb_set_file_time: createtime: %s\n ",
+                  time_to_asc(convert_timespec_to_time_t(ft->create_time))));
+       }
 
        /*
         * Try and set the times of this file if
@@ -4864,7 +4970,8 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
        {
                struct timespec mts = get_mtimespec(psbuf);
                struct timespec ats = get_atimespec(psbuf);
-               if ((timespec_compare(&ts[0], &ats) == 0) && (timespec_compare(&ts[1], &mts) == 0)) {
+               if ((timespec_compare(&ft->atime, &ats) == 0) &&
+                   (timespec_compare(&ft->mtime, &mts) == 0)) {
                        return NT_STATUS_OK;
                }
        }
@@ -4881,20 +4988,29 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
                 */
 
                DEBUG(10,("smb_set_file_time: setting pending modtime to %s\n",
-                         time_to_asc(convert_timespec_to_time_t(ts[1])) ));
+                         time_to_asc(convert_timespec_to_time_t(ft->mtime))));
 
                if (fsp != NULL) {
-                       set_write_time_fsp(fsp, ts[1], true);
+                       if (fsp->base_fsp) {
+                               set_sticky_write_time_fsp(fsp->base_fsp,
+                                                         ft->mtime);
+                       } else {
+                               set_sticky_write_time_fsp(fsp, ft->mtime);
+                       }
                } else {
-                       set_write_time_path(conn, fname,
+                       set_sticky_write_time_path(conn, fname,
                                            vfs_file_id_from_sbuf(conn, psbuf),
-                                           ts[1], true);
+                                           ft->mtime);
                }
        }
 
        DEBUG(10,("smb_set_file_time: setting utimes to modified values.\n"));
 
-       if(file_ntimes(conn, fname, ts)!=0) {
+       if (fsp && fsp->base_fsp) {
+               fname = fsp->base_fsp->fsp_name;
+       }
+
+       if(file_ntimes(conn, fname, ft)!=0) {
                return map_nt_error_from_unix(errno);
        }
        notify_fname(conn, NOTIFY_ACTION_MODIFIED, action, fname);
@@ -4907,6 +5023,7 @@ NTSTATUS smb_set_file_time(connection_struct *conn,
 ****************************************************************************/
 
 static NTSTATUS smb_set_file_dosmode(connection_struct *conn,
+                               files_struct *fsp,
                                const char *fname,
                                SMB_STRUCT_STAT *psbuf,
                                uint32 dosmode)
@@ -4915,6 +5032,14 @@ static NTSTATUS smb_set_file_dosmode(connection_struct *conn,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
+       if (fsp) {
+               if (fsp->base_fsp) {
+                       fname = fsp->base_fsp->fsp_name;
+               } else {
+                       fname = fsp->fsp_name;
+               }
+       }
+               
        if (dosmode) {
                if (S_ISDIR(psbuf->st_mode)) {
                        dosmode |= aDIR;
@@ -4960,7 +5085,7 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
 
        DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));
 
-       if (size == get_file_size(*psbuf)) {
+       if (size == get_file_size_stat(psbuf)) {
                return NT_STATUS_OK;
        }
 
@@ -4972,18 +5097,30 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                if (vfs_set_filelen(fsp, size) == -1) {
                        return map_nt_error_from_unix(errno);
                }
+               trigger_write_time_update_immediate(fsp);
                return NT_STATUS_OK;
        }
 
-       status = open_file_ntcreate(conn, req, fname, psbuf,
-                               FILE_WRITE_ATTRIBUTES,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                               FILE_OPEN,
-                               0,
-                               FILE_ATTRIBUTE_NORMAL,
-                               FORCE_OPLOCK_BREAK_TO_NONE,
-                               NULL, &new_fsp);
-       
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               fname,                                  /* fname */
+               0,                                      /* create_file_flags */
+               FILE_WRITE_ATTRIBUTES,                  /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               FORCE_OPLOCK_BREAK_TO_NONE,             /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &new_fsp,                               /* result */
+               NULL,                                   /* pinfo */
+               psbuf);                                 /* psbuf */
+
        if (!NT_STATUS_IS_OK(status)) {
                /* NB. We check for open_was_deferred in the caller. */
                return status;
@@ -4991,11 +5128,12 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
 
        if (vfs_set_filelen(new_fsp, size) == -1) {
                status = map_nt_error_from_unix(errno);
-               close_file(new_fsp,NORMAL_CLOSE);
+               close_file(req, new_fsp,NORMAL_CLOSE);
                return status;
        }
 
-       close_file(new_fsp,NORMAL_CLOSE);
+       trigger_write_time_update_immediate(new_fsp);
+       close_file(req, new_fsp,NORMAL_CLOSE);
        return NT_STATUS_OK;
 }
 
@@ -5081,7 +5219,8 @@ static NTSTATUS smb_set_file_disposition_info(connection_struct *conn,
        }
 
        /* The set is across all open files on this dev/inode pair. */
-       if (!set_delete_on_close(fsp, delete_on_close, &current_user.ut)) {
+       if (!set_delete_on_close(fsp, delete_on_close,
+                                &conn->server_info->utok)) {
                return NT_STATUS_ACCESS_DENIED;
        }
        return NT_STATUS_OK;
@@ -5096,7 +5235,7 @@ static NTSTATUS smb_file_position_information(connection_struct *conn,
                                int total_data,
                                files_struct *fsp)
 {
-       SMB_BIG_UINT position_information;
+       uint64_t position_information;
 
        if (total_data < 8) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -5107,9 +5246,9 @@ static NTSTATUS smb_file_position_information(connection_struct *conn,
                return NT_STATUS_OK;
        }
 
-       position_information = (SMB_BIG_UINT)IVAL(pdata,0);
+       position_information = (uint64_t)IVAL(pdata,0);
 #ifdef LARGE_SMB_OFF_T
-       position_information |= (((SMB_BIG_UINT)IVAL(pdata,4)) << 32);
+       position_information |= (((uint64_t)IVAL(pdata,4)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if (IVAL(pdata,4) != 0) {
                /* more than 32 bits? */
@@ -5276,6 +5415,7 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
        uint32 len;
        char *newname = NULL;
        char *base_name = NULL;
+       struct smb_filename *smb_fname = NULL;
        bool dest_has_wcard = False;
        NTSTATUS status = NT_STATUS_OK;
        char *p;
@@ -5317,38 +5457,47 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                return NT_STATUS_NOT_SUPPORTED;
        }
 
-       /* Create the base directory. */
-       base_name = talloc_strdup(ctx, fname);
-       if (!base_name) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       p = strrchr_m(base_name, '/');
-       if (p) {
-               p[1] = '\0';
-       } else {
-               base_name = talloc_strdup(ctx, "./");
+       if (fsp && fsp->base_fsp) {
+               /* newname must be a stream name. */
+               if (newname[0] != ':') {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
+               base_name = talloc_asprintf(ctx, "%s%s",
+                                          fsp->base_fsp->fsp_name,
+                                          newname);
                if (!base_name) {
                        return NT_STATUS_NO_MEMORY;
                }
-       }
-       /* Append the new name. */
-       base_name = talloc_asprintf_append(base_name,
-                       "%s",
-                       newname);
-       if (!base_name) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (fsp) {
-               SMB_STRUCT_STAT sbuf;
-               char *newname_last_component = NULL;
+       } else {
+               /* newname must *not* be a stream name. */
+               if (is_ntfs_stream_name(newname)) {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
 
-               ZERO_STRUCT(sbuf);
+               /* Create the base directory. */
+               base_name = talloc_strdup(ctx, fname);
+               if (!base_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               p = strrchr_m(base_name, '/');
+               if (p) {
+                       p[1] = '\0';
+               } else {
+                       base_name = talloc_strdup(ctx, "./");
+                       if (!base_name) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               }
+               /* Append the new name. */
+               base_name = talloc_asprintf_append(base_name,
+                               "%s",
+                               newname);
+               if (!base_name) {
+                       return NT_STATUS_NO_MEMORY;
+               }
 
-               status = unix_convert(ctx, conn, newname, False,
-                                       &newname,
-                                       &newname_last_component,
-                                       &sbuf);
+               status = unix_convert(ctx, conn, newname, &smb_fname,
+                                     UCF_SAVE_LCOMP);
 
                /* If an error we expect this to be
                 * NT_STATUS_OBJECT_PATH_NOT_FOUND */
@@ -5356,14 +5505,17 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                if (!NT_STATUS_IS_OK(status)
                    && !NT_STATUS_EQUAL(NT_STATUS_OBJECT_PATH_NOT_FOUND,
                                        status)) {
-                       return status;
+                       goto out;
                }
+       }
 
+       if (fsp) {
                DEBUG(10,("smb_file_rename_information: SMB_FILE_RENAME_INFORMATION (fnum %d) %s -> %s\n",
                        fsp->fnum, fsp->fsp_name, base_name ));
                status = rename_internals_fsp(conn, fsp, base_name,
-                                             newname_last_component, 0,
-                                             overwrite);
+                                             smb_fname ?
+                                             smb_fname->original_lcomp : NULL,
+                                             0, overwrite);
        } else {
                DEBUG(10,("smb_file_rename_information: SMB_FILE_RENAME_INFORMATION %s -> %s\n",
                        fname, base_name ));
@@ -5371,7 +5523,10 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                                        overwrite, False, dest_has_wcard,
                                        FILE_WRITE_ATTRIBUTES);
        }
-
+ out:
+       if (smb_fname) {
+               TALLOC_FREE(smb_fname);
+       }
        return status;
 }
 
@@ -5443,13 +5598,13 @@ static NTSTATUS smb_set_posix_acl(connection_struct *conn,
 ****************************************************************************/
 
 static NTSTATUS smb_set_posix_lock(connection_struct *conn,
-                               const struct smb_request *req,
+                               struct smb_request *req,
                                const char *pdata,
                                int total_data,
                                files_struct *fsp)
 {
-       SMB_BIG_UINT count;
-       SMB_BIG_UINT offset;
+       uint64_t count;
+       uint64_t offset;
        uint32 lock_pid;
        bool blocking_lock = False;
        enum brl_type lock_type;
@@ -5496,13 +5651,13 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
 
        lock_pid = IVAL(pdata, POSIX_LOCK_PID_OFFSET);
 #if defined(HAVE_LONGLONG)
-       offset = (((SMB_BIG_UINT) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
-                       ((SMB_BIG_UINT) IVAL(pdata,POSIX_LOCK_START_OFFSET));
-       count = (((SMB_BIG_UINT) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
-                       ((SMB_BIG_UINT) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
+       offset = (((uint64_t) IVAL(pdata,(POSIX_LOCK_START_OFFSET+4))) << 32) |
+                       ((uint64_t) IVAL(pdata,POSIX_LOCK_START_OFFSET));
+       count = (((uint64_t) IVAL(pdata,(POSIX_LOCK_LEN_OFFSET+4))) << 32) |
+                       ((uint64_t) IVAL(pdata,POSIX_LOCK_LEN_OFFSET));
 #else /* HAVE_LONGLONG */
-       offset = (SMB_BIG_UINT)IVAL(pdata,POSIX_LOCK_START_OFFSET);
-       count = (SMB_BIG_UINT)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
+       offset = (uint64_t)IVAL(pdata,POSIX_LOCK_START_OFFSET);
+       count = (uint64_t)IVAL(pdata,POSIX_LOCK_LEN_OFFSET);
 #endif /* HAVE_LONGLONG */
 
        DEBUG(10,("smb_set_posix_lock: file %s, lock_type = %u,"
@@ -5532,7 +5687,8 @@ static NTSTATUS smb_set_posix_lock(connection_struct *conn,
                                                        POSIX_LOCK,
                                                        blocking_lock,
                                                        &status,
-                                                       &block_smbpid);
+                                                       &block_smbpid,
+                                                       NULL);
 
                if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
                        /*
@@ -5572,16 +5728,21 @@ static NTSTATUS smb_set_info_standard(connection_struct *conn,
                                        const char *fname,
                                        const SMB_STRUCT_STAT *psbuf)
 {
-       struct timespec ts[2];
+       struct smb_file_time ft;
+       ZERO_STRUCT(ft);
 
        if (total_data < 12) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       /* create time */
+       ft.create_time = interpret_long_date(pdata);
+
        /* access time */
-       ts[0] = convert_time_t_to_timespec(srv_make_unix_date2(pdata+l1_fdateLastAccess));
+       ft.atime = interpret_long_date(pdata + 8);
+
        /* write time */
-       ts[1] = convert_time_t_to_timespec(srv_make_unix_date2(pdata+l1_fdateLastWrite));
+       ft.mtime = interpret_long_date(pdata + 16);
 
        DEBUG(10,("smb_set_info_standard: file %s\n",
                fname ? fname : fsp->fsp_name ));
@@ -5590,7 +5751,7 @@ static NTSTATUS smb_set_info_standard(connection_struct *conn,
                                fsp,
                                fname,
                                psbuf,
-                               ts,
+                               &ft,
                                true);
 }
 
@@ -5608,47 +5769,48 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
        /* Patch to do this correctly from Paul Eggert <eggert@twinsun.com>. */
        struct timespec write_time;
        struct timespec changed_time;
+       struct smb_file_time ft;
        uint32 dosmode = 0;
-       struct timespec ts[2];
        NTSTATUS status = NT_STATUS_OK;
        bool setting_write_time = true;
 
+       ZERO_STRUCT(ft);
+
        if (total_data < 36) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        /* Set the attributes */
        dosmode = IVAL(pdata,32);
-       status = smb_set_file_dosmode(conn,
-                                       fname,
-                                       psbuf,
-                                       dosmode);
+       status = smb_set_file_dosmode(conn, fsp, fname, psbuf, dosmode);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       /* Ignore create time at offset pdata. */
-
        /* access time */
-       ts[0] = interpret_long_date(pdata+8);
+       ft.atime = interpret_long_date(pdata+8);
 
        write_time = interpret_long_date(pdata+16);
        changed_time = interpret_long_date(pdata+24);
 
        /* mtime */
-       ts[1] = timespec_min(&write_time, &changed_time);
+       ft.mtime = timespec_min(&write_time, &changed_time);
+
+       /* create time */
+       ft.create_time = interpret_long_date(pdata);
 
-       if ((timespec_compare(&write_time, &ts[1]) == 1) && !null_timespec(write_time)) {
-               ts[1] = write_time;
+       if ((timespec_compare(&write_time, &ft.mtime) == 1) &&
+           !null_timespec(write_time)) {
+               ft.mtime = write_time;
        }
 
        /* Prefer a defined time to an undefined one. */
-       if (null_timespec(ts[1])) {
+       if (null_timespec(ft.mtime)) {
                if (null_timespec(write_time)) {
-                       ts[1] = changed_time;
+                       ft.mtime = changed_time;
                        setting_write_time = false;
                } else {
-                       ts[1] = write_time;
+                       ft.mtime = write_time;
                }
        }
 
@@ -5659,7 +5821,7 @@ static NTSTATUS smb_set_file_basic_info(connection_struct *conn,
                                fsp,
                                fname,
                                psbuf,
-                               ts,
+                               &ft,
                                setting_write_time);
 }
 
@@ -5675,7 +5837,7 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
                                        const char *fname,
                                        SMB_STRUCT_STAT *psbuf)
 {
-       SMB_BIG_UINT allocation_size = 0;
+       uint64_t allocation_size = 0;
        NTSTATUS status = NT_STATUS_OK;
        files_struct *new_fsp = NULL;
 
@@ -5687,9 +5849,9 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       allocation_size = (SMB_BIG_UINT)IVAL(pdata,0);
+       allocation_size = (uint64_t)IVAL(pdata,0);
 #ifdef LARGE_SMB_OFF_T
-       allocation_size |= (((SMB_BIG_UINT)IVAL(pdata,4)) << 32);
+       allocation_size |= (((uint64_t)IVAL(pdata,4)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if (IVAL(pdata,4) != 0) {
                /* more than 32 bits? */
@@ -5710,7 +5872,7 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
        if (fsp && fsp->fh->fd != -1) {
                /* Open file handle. */
                /* Only change if needed. */
-               if (allocation_size != get_file_size(*psbuf)) {
+               if (allocation_size != get_file_size_stat(psbuf)) {
                        if (vfs_allocate_file_space(fsp, allocation_size) == -1) {
                                return map_nt_error_from_unix(errno);
                        }
@@ -5720,20 +5882,31 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
                 * This is equivalent to a write. Ensure it's seen immediately
                 * if there are no pending writes.
                 */
-               trigger_write_time_update(fsp);
+               trigger_write_time_update_immediate(fsp);
                return NT_STATUS_OK;
        }
 
        /* Pathname or stat or directory file. */
 
-       status = open_file_ntcreate(conn, req, fname, psbuf,
-                               FILE_WRITE_DATA,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                               FILE_OPEN,
-                               0,
-                               FILE_ATTRIBUTE_NORMAL,
-                               FORCE_OPLOCK_BREAK_TO_NONE,
-                               NULL, &new_fsp);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               fname,                                  /* fname */
+               0,                                      /* create_file_flags */
+               FILE_WRITE_DATA,                        /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
+               FORCE_OPLOCK_BREAK_TO_NONE,             /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &new_fsp,                               /* result */
+               NULL,                                   /* pinfo */
+               psbuf);                                 /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                /* NB. We check for open_was_deferred in the caller. */
@@ -5741,10 +5914,10 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
        }
 
        /* Only change if needed. */
-       if (allocation_size != get_file_size(*psbuf)) {
+       if (allocation_size != get_file_size_stat(psbuf)) {
                if (vfs_allocate_file_space(new_fsp, allocation_size) == -1) {
                        status = map_nt_error_from_unix(errno);
-                       close_file(new_fsp,NORMAL_CLOSE);
+                       close_file(req, new_fsp, NORMAL_CLOSE);
                        return status;
                }
        }
@@ -5754,9 +5927,9 @@ static NTSTATUS smb_set_file_allocation_info(connection_struct *conn,
         * This is equivalent to a write. Ensure it's seen immediately
         * if there are no pending writes.
         */
-       trigger_write_time_update(new_fsp);
+       trigger_write_time_update_immediate(new_fsp);
 
-       close_file(new_fsp,NORMAL_CLOSE);
+       close_file(req, new_fsp, NORMAL_CLOSE);
        return NT_STATUS_OK;
 }
 
@@ -5868,9 +6041,12 @@ static NTSTATUS smb_unix_mknod(connection_struct *conn,
         */
 
        if (lp_inherit_perms(SNUM(conn))) {
-               inherit_access_posix_acl(
-                       conn, parent_dirname(fname),
-                       fname, unixmode);
+               char *parent;
+               if (!parent_dirname(talloc_tos(), fname, &parent, NULL)) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               inherit_access_posix_acl(conn, parent, fname, unixmode);
+               TALLOC_FREE(parent);
        }
 
        if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
@@ -5893,7 +6069,7 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
                                        const char *fname,
                                        SMB_STRUCT_STAT *psbuf)
 {
-       struct timespec ts[2];
+       struct smb_file_time ft;
        uint32 raw_unixmode;
        mode_t unixmode;
        SMB_OFF_T size = 0;
@@ -5903,6 +6079,8 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
        bool delete_on_fail = False;
        enum perm_type ptype;
 
+       ZERO_STRUCT(ft);
+
        if (total_data < 100) {
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -5920,8 +6098,8 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
 #endif /* LARGE_SMB_OFF_T */
        }
 
-       ts[0] = interpret_long_date(pdata+24); /* access_time */
-       ts[1] = interpret_long_date(pdata+32); /* modification_time */
+       ft.atime = interpret_long_date(pdata+24); /* access_time */
+       ft.mtime = interpret_long_date(pdata+32); /* modification_time */
        set_owner = (uid_t)IVAL(pdata,40);
        set_grp = (gid_t)IVAL(pdata,48);
        raw_unixmode = IVAL(pdata,84);
@@ -5963,9 +6141,9 @@ size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
 
                /* Ensure we don't try and change anything else. */
                raw_unixmode = SMB_MODE_NO_CHANGE;
-               size = get_file_size(*psbuf);
-               ts[0] = get_atimespec(psbuf);
-               ts[1] = get_mtimespec(psbuf);
+               size = get_file_size_stat(psbuf);
+               ft.atime = get_atimespec(psbuf);
+               ft.mtime = get_mtimespec(psbuf);
                /* 
                 * We continue here as we might want to change the 
                 * owner uid/gid.
@@ -5979,7 +6157,7 @@ size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
         * */
 
        if (!size) {
-               size = get_file_size(*psbuf);
+               size = get_file_size_stat(psbuf);
        }
 #endif
 
@@ -6053,7 +6231,7 @@ size = %.0f, uid = %u, gid = %u, raw perms = 0%o\n",
                                fsp,
                                fname,
                                psbuf,
-                               ts,
+                               &ft,
                                true);
 }
 
@@ -6157,19 +6335,27 @@ static NTSTATUS smb_posix_mkdir(connection_struct *conn,
        DEBUG(10,("smb_posix_mkdir: file %s, mode 0%o\n",
                fname, (unsigned int)unixmode ));
 
-       status = open_directory(conn, req,
-                               fname,
-                               psbuf,
-                               FILE_READ_ATTRIBUTES, /* Just a stat open */
-                               FILE_SHARE_NONE, /* Ignored for stat opens */
-                               FILE_CREATE,
-                               0,
-                               mod_unixmode,
-                               &info,
-                               &fsp);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               fname,                                  /* fname */
+               0,                                      /* create_file_flags */
+               FILE_READ_ATTRIBUTES,                   /* access_mask */
+               FILE_SHARE_NONE,                        /* share_access */
+               FILE_CREATE,                            /* create_disposition*/
+               FILE_DIRECTORY_FILE,                    /* create_options */
+               mod_unixmode,                           /* file_attributes */
+               0,                                      /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info,                                  /* pinfo */
+               psbuf);                                 /* psbuf */
 
         if (NT_STATUS_IS_OK(status)) {
-                close_file(fsp, NORMAL_CLOSE);
+                close_file(req, fsp, NORMAL_CLOSE);
         }
 
        info_level_return = SVAL(pdata,16);
@@ -6287,6 +6473,8 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
                create_disp = FILE_OVERWRITE_IF;
        } else if((wire_open_mode & SMB_O_CREAT) == SMB_O_CREAT) {
                create_disp = FILE_OPEN_IF;
+       } else if ((wire_open_mode & (SMB_O_CREAT | SMB_O_EXCL | SMB_O_TRUNC)) == 0) {
+               create_disp = FILE_OPEN;
        } else {
                DEBUG(5,("smb_posix_open: invalid create mode 0x%x\n",
                        (unsigned int)wire_open_mode ));
@@ -6323,17 +6511,25 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
                (unsigned int)wire_open_mode,
                (unsigned int)unixmode ));
 
-       status = open_file_ntcreate(conn, req,
-                               fname,
-                               psbuf,
-                               access_mask,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                               create_disp,
-                               0,              /* no create options yet. */
-                               mod_unixmode,
-                               oplock_request,
-                               &info,
-                               &fsp);
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               fname,                                  /* fname */
+               0,                                      /* create_file_flags */
+               access_mask,                            /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               create_disp,                            /* create_disposition*/
+               FILE_NON_DIRECTORY_FILE,                /* create_options */
+               mod_unixmode,                           /* file_attributes */
+               oplock_request,                         /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info,                                  /* pinfo */
+               psbuf);                                 /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -6362,7 +6558,7 @@ static NTSTATUS smb_posix_open(connection_struct *conn,
        /* Realloc the data size */
        *ppdata = (char *)SMB_REALLOC(*ppdata,*pdata_return_size);
        if (*ppdata == NULL) {
-               close_file(fsp,ERROR_CLOSE);
+               close_file(req, fsp, ERROR_CLOSE);
                *pdata_return_size = 0;
                return NT_STATUS_NO_MEMORY;
        }
@@ -6418,6 +6614,7 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
        uint16 flags = 0;
        char del = 1;
        int info = 0;
+       int create_options = 0;
        int i;
        struct share_mode_lock *lck = NULL;
 
@@ -6441,30 +6638,28 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
                fname));
 
        if (VALID_STAT_OF_DIR(*psbuf)) {
-               status = open_directory(conn, req,
-                                       fname,
-                                       psbuf,
-                                       DELETE_ACCESS,
-                                       FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                                       FILE_OPEN,
-                                       0,
-                                       FILE_FLAG_POSIX_SEMANTICS|0777,
-                                       &info,
-                                       &fsp);
-       } else {
-
-               status = open_file_ntcreate(conn, req,
-                               fname,
-                               psbuf,
-                               DELETE_ACCESS,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-                               FILE_OPEN,
-                               0,
-                               FILE_FLAG_POSIX_SEMANTICS|0777,
-                               0, /* No oplock, but break existing ones. */
-                               &info,
-                               &fsp);
-       }
+               create_options |= FILE_DIRECTORY_FILE;
+       }
+
+        status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               fname,                                  /* fname */
+               0,                                      /* create_file_flags */
+               DELETE_ACCESS,                          /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                   FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               create_options,                         /* create_options */
+               FILE_FLAG_POSIX_SEMANTICS|0777,         /* file_attributes */
+               0,                                      /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info,                                  /* pinfo */
+               psbuf);                                 /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -6480,7 +6675,7 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
        if (lck == NULL) {
                DEBUG(0, ("smb_posix_unlink: Could not get share mode "
                        "lock for file %s\n", fsp->fsp_name));
-               close_file(fsp, NORMAL_CLOSE);
+               close_file(req, fsp, NORMAL_CLOSE);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -6496,7 +6691,7 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
                                continue;
                        }
                        /* Fail with sharing violation. */
-                       close_file(fsp, NORMAL_CLOSE);
+                       close_file(req, fsp, NORMAL_CLOSE);
                        TALLOC_FREE(lck);
                        return NT_STATUS_SHARING_VIOLATION;
                }
@@ -6513,12 +6708,12 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
                                                psbuf);
 
        if (!NT_STATUS_IS_OK(status)) {
-               close_file(fsp, NORMAL_CLOSE);
+               close_file(req, fsp, NORMAL_CLOSE);
                TALLOC_FREE(lck);
                return status;
        }
        TALLOC_FREE(lck);
-       return close_file(fsp, NORMAL_CLOSE);
+       return close_file(req, fsp, NORMAL_CLOSE);
 }
 
 /****************************************************************************
@@ -6537,6 +6732,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
        uint16 info_level;
        SMB_STRUCT_STAT sbuf;
        char *fname = NULL;
+       struct smb_filename *smb_fname = NULL;
        files_struct *fsp = NULL;
        NTSTATUS status = NT_STATUS_OK;
        int data_return_size = 0;
@@ -6555,9 +6751,9 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               fsp = file_fsp(SVAL(params,0));
+               fsp = file_fsp(req, SVAL(params,0));
                /* Basic check for non-null fsp. */
-               if (!check_fsp_open(conn, req, fsp, &current_user)) {
+               if (!check_fsp_open(conn, req, fsp)) {
                        return;
                }
                info_level = SVAL(params,2);
@@ -6610,7 +6806,7 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        /*
                         * Original code - this is an open file.
                         */
-                       if (!check_fsp(conn, req, fsp, &current_user)) {
+                       if (!check_fsp(conn, req, fsp)) {
                                return;
                        }
 
@@ -6651,8 +6847,15 @@ static void call_trans2setfilepathinfo(connection_struct *conn,
                        return;
                }
 
-               status = unix_convert(ctx, conn, fname, False,
-                               &fname, NULL, &sbuf);
+               status = unix_convert(ctx, conn, fname, &smb_fname, 0);
+               if (!NT_STATUS_IS_OK(status)) {
+                       reply_nterror(req, status);
+                       return;
+               }
+               sbuf = smb_fname->st;
+
+               status = get_full_smb_filename(ctx, smb_fname, &fname);
+               TALLOC_FREE(smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
                        reply_nterror(req, status);
                        return;
@@ -6966,10 +7169,10 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                             char **ppdata, int total_data,
                             unsigned int max_data_bytes)
 {
+       struct smb_filename *smb_dname = NULL;
        char *params = *pparams;
        char *pdata = *ppdata;
        char *directory = NULL;
-       SMB_STRUCT_STAT sbuf;
        NTSTATUS status = NT_STATUS_OK;
        struct ea_list *ea_list = NULL;
        TALLOC_CTX *ctx = talloc_tos();
@@ -6994,7 +7197,13 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
 
        DEBUG(3,("call_trans2mkdir : name = %s\n", directory));
 
-       status = unix_convert(ctx, conn, directory, False, &directory, NULL, &sbuf);
+       status = unix_convert(ctx, conn, directory, &smb_dname, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+               return;
+       }
+
+       status = get_full_smb_filename(ctx, smb_dname, &directory);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                return;
@@ -7038,10 +7247,11 @@ static void call_trans2mkdir(connection_struct *conn, struct smb_request *req,
                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        return;
                }
-       } else if (IVAL(pdata,0) != 4) {
-               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               return;
        }
+       /* If total_data == 4 Windows doesn't care what values
+        * are placed in that field, it just ignores them.
+        * The System i QNTC IBM SMB client puts bad values here,
+        * so ignore them. */
 
        status = create_directory(conn, req, directory);
 
@@ -7085,7 +7295,6 @@ static void call_trans2findnotifyfirst(connection_struct *conn,
                                       char **ppdata, int total_data,
                                       unsigned int max_data_bytes)
 {
-       static uint16 fnf_handle = 257;
        char *params = *pparams;
        uint16 info_level;
 
@@ -7223,7 +7432,7 @@ static void call_trans2ioctl(connection_struct *conn,
                             unsigned int max_data_bytes)
 {
        char *pdata = *ppdata;
-       files_struct *fsp = file_fsp(SVAL(req->inbuf,smb_vwv15));
+       files_struct *fsp = file_fsp(req, SVAL(req->vwv+15, 0));
 
        /* check for an invalid fid before proceeding */
 
@@ -7232,8 +7441,8 @@ static void call_trans2ioctl(connection_struct *conn,
                return;
        }
 
-       if ((SVAL(req->inbuf,(smb_setup+4)) == LMCAT_SPL)
-           && (SVAL(req->inbuf,(smb_setup+6)) == LMFUNC_GETJOBID)) {
+       if ((SVAL(req->vwv+16, 0) == LMCAT_SPL)
+           && (SVAL(req->vwv+17, 0) == LMFUNC_GETJOBID)) {
                *ppdata = (char *)SMB_REALLOC(*ppdata, 32);
                if (*ppdata == NULL) {
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
@@ -7276,7 +7485,7 @@ void reply_findclose(struct smb_request *req)
                return;
        }
 
-       dptr_num = SVALS(req->inbuf,smb_vwv0);
+       dptr_num = SVALS(req->vwv+0, 0);
 
        DEBUG(3,("reply_findclose, dptr_num = %d\n", dptr_num));
 
@@ -7306,7 +7515,7 @@ void reply_findnclose(struct smb_request *req)
                return;
        }
        
-       dptr_num = SVAL(req->inbuf,smb_vwv0);
+       dptr_num = SVAL(req->vwv+0, 0);
 
        DEBUG(3,("reply_findnclose, dptr_num = %d\n", dptr_num));
 
@@ -7341,6 +7550,8 @@ static void handle_trans2(connection_struct *conn, struct smb_request *req,
                }
        }
 
+       SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
+
        /* Now we must call the relevant TRANS2 function */
        switch(state->call)  {
        case TRANSACT2_OPEN:
@@ -7496,8 +7707,6 @@ void reply_trans2(struct smb_request *req)
        unsigned int psoff;
        unsigned int pscnt;
        unsigned int tran_call;
-       unsigned int size;
-       unsigned int av_size;
        struct trans_state *state;
        NTSTATUS result;
 
@@ -7509,13 +7718,11 @@ void reply_trans2(struct smb_request *req)
                return;
        }
 
-       dsoff = SVAL(req->inbuf, smb_dsoff);
-       dscnt = SVAL(req->inbuf, smb_dscnt);
-       psoff = SVAL(req->inbuf, smb_psoff);
-       pscnt = SVAL(req->inbuf, smb_pscnt);
-       tran_call = SVAL(req->inbuf, smb_setup0);
-       size = smb_len(req->inbuf) + 4;
-       av_size = smb_len(req->inbuf);
+       dsoff = SVAL(req->vwv+12, 0);
+       dscnt = SVAL(req->vwv+11, 0);
+       psoff = SVAL(req->vwv+10, 0);
+       pscnt = SVAL(req->vwv+9, 0);
+       tran_call = SVAL(req->vwv+14, 0);
 
        result = allow_new_trans(conn->pending_trans, req->mid);
        if (!NT_STATUS_IS_OK(result)) {
@@ -7553,17 +7760,17 @@ void reply_trans2(struct smb_request *req)
 
        state->mid = req->mid;
        state->vuid = req->vuid;
-       state->setup_count = SVAL(req->inbuf, smb_suwcnt);
+       state->setup_count = SVAL(req->vwv+13, 0);
        state->setup = NULL;
-       state->total_param = SVAL(req->inbuf, smb_tpscnt);
+       state->total_param = SVAL(req->vwv+0, 0);
        state->param = NULL;
-       state->total_data =  SVAL(req->inbuf, smb_tdscnt);
+       state->total_data =  SVAL(req->vwv+1, 0);
        state->data = NULL;
-       state->max_param_return = SVAL(req->inbuf, smb_mprcnt);
-       state->max_data_return  = SVAL(req->inbuf, smb_mdrcnt);
-       state->max_setup_return = SVAL(req->inbuf, smb_msrcnt);
-       state->close_on_completion = BITSETW(req->inbuf+smb_vwv5,0);
-       state->one_way = BITSETW(req->inbuf+smb_vwv5,1);
+       state->max_param_return = SVAL(req->vwv+2, 0);
+       state->max_data_return  = SVAL(req->vwv+3, 0);
+       state->max_setup_return = SVAL(req->vwv+4, 0);
+       state->close_on_completion = BITSETW(req->vwv+5, 0);
+       state->one_way = BITSETW(req->vwv+5, 1);
 
        state->call = tran_call;
 
@@ -7580,8 +7787,8 @@ void reply_trans2(struct smb_request *req)
                 */
                if ( (state->setup_count == 4)
                     && (tran_call == TRANSACT2_IOCTL)
-                    && (SVAL(req->inbuf,(smb_setup+4)) == LMCAT_SPL)
-                    && (SVAL(req->inbuf,(smb_setup+6)) == LMFUNC_GETJOBID)) {
+                    && (SVAL(req->vwv+16, 0) == LMCAT_SPL)
+                    && (SVAL(req->vwv+17, 0) == LMFUNC_GETJOBID)) {
                        DEBUG(2,("Got Trans2 DevIOctl jobid\n"));
                } else {
                        DEBUG(2,("Invalid smb_sucnt in trans2 call(%u)\n",state->setup_count));
@@ -7597,6 +7804,12 @@ void reply_trans2(struct smb_request *req)
                goto bad_param;
 
        if (state->total_data) {
+
+               if (trans_oob(state->total_data, 0, dscnt)
+                   || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
+                       goto bad_param;
+               }
+
                /* Can't use talloc here, the core routines do realloc on the
                 * params and data. */
                state->data = (char *)SMB_MALLOC(state->total_data);
@@ -7609,21 +7822,16 @@ void reply_trans2(struct smb_request *req)
                        return;
                }
 
-               if (dscnt > state->total_data ||
-                               dsoff+dscnt < dsoff) {
-                       goto bad_param;
-               }
-
-               if (dsoff > av_size ||
-                               dscnt > av_size ||
-                               dsoff+dscnt > av_size) {
-                       goto bad_param;
-               }
-
                memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
        }
 
        if (state->total_param) {
+
+               if (trans_oob(state->total_param, 0, pscnt)
+                   || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
+                       goto bad_param;
+               }
+
                /* Can't use talloc here, the core routines do realloc on the
                 * params and data. */
                state->param = (char *)SMB_MALLOC(state->total_param);
@@ -7637,17 +7845,6 @@ void reply_trans2(struct smb_request *req)
                        return;
                } 
 
-               if (pscnt > state->total_param ||
-                               psoff+pscnt < psoff) {
-                       goto bad_param;
-               }
-
-               if (psoff > av_size ||
-                               pscnt > av_size ||
-                               psoff+pscnt > av_size) {
-                       goto bad_param;
-               }
-
                memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
        }
 
@@ -7695,8 +7892,6 @@ void reply_transs2(struct smb_request *req)
        connection_struct *conn = req->conn;
        unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
        struct trans_state *state;
-       unsigned int size;
-       unsigned int av_size;
 
        START_PROFILE(SMBtranss2);
 
@@ -7708,9 +7903,6 @@ void reply_transs2(struct smb_request *req)
                return;
        }
 
-       size = smb_len(req->inbuf)+4;
-       av_size = smb_len(req->inbuf);
-
        for (state = conn->pending_trans; state != NULL;
             state = state->next) {
                if (state->mid == req->mid) {
@@ -7727,18 +7919,18 @@ void reply_transs2(struct smb_request *req)
        /* Revise state->total_param and state->total_data in case they have
           changed downwards */
 
-       if (SVAL(req->inbuf, smb_tpscnt) < state->total_param)
-               state->total_param = SVAL(req->inbuf, smb_tpscnt);
-       if (SVAL(req->inbuf, smb_tdscnt) < state->total_data)
-               state->total_data = SVAL(req->inbuf, smb_tdscnt);
+       if (SVAL(req->vwv+0, 0) < state->total_param)
+               state->total_param = SVAL(req->vwv+0, 0);
+       if (SVAL(req->vwv+1, 0) < state->total_data)
+               state->total_data = SVAL(req->vwv+1, 0);
 
-       pcnt = SVAL(req->inbuf, smb_spscnt);
-       poff = SVAL(req->inbuf, smb_spsoff);
-       pdisp = SVAL(req->inbuf, smb_spsdisp);
+       pcnt = SVAL(req->vwv+2, 0);
+       poff = SVAL(req->vwv+3, 0);
+       pdisp = SVAL(req->vwv+4, 0);
 
-       dcnt = SVAL(req->inbuf, smb_sdscnt);
-       doff = SVAL(req->inbuf, smb_sdsoff);
-       ddisp = SVAL(req->inbuf, smb_sdsdisp);
+       dcnt = SVAL(req->vwv+5, 0);
+       doff = SVAL(req->vwv+6, 0);
+       ddisp = SVAL(req->vwv+7, 0);
 
        state->received_param += pcnt;
        state->received_data += dcnt;
@@ -7748,41 +7940,19 @@ void reply_transs2(struct smb_request *req)
                goto bad_param;
 
        if (pcnt) {
-               if (pdisp > state->total_param ||
-                               pcnt > state->total_param ||
-                               pdisp+pcnt > state->total_param ||
-                               pdisp+pcnt < pdisp) {
+               if (trans_oob(state->total_param, pdisp, pcnt)
+                   || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
                        goto bad_param;
                }
-
-               if (poff > av_size ||
-                               pcnt > av_size ||
-                               poff+pcnt > av_size ||
-                               poff+pcnt < poff) {
-                       goto bad_param;
-               }
-
-               memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,
-                      pcnt);
+               memcpy(state->param+pdisp,smb_base(req->inbuf)+poff,pcnt);
        }
 
        if (dcnt) {
-               if (ddisp > state->total_data ||
-                               dcnt > state->total_data ||
-                               ddisp+dcnt > state->total_data ||
-                               ddisp+dcnt < ddisp) {
+               if (trans_oob(state->total_data, ddisp, dcnt)
+                   || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
                        goto bad_param;
                }
-
-               if (ddisp > av_size ||
-                               dcnt > av_size ||
-                               ddisp+dcnt > av_size ||
-                               ddisp+dcnt < ddisp) {
-                       goto bad_param;
-               }
-
-               memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
-                      dcnt);      
+               memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
        }
 
        if ((state->received_param < state->total_param) ||
@@ -7791,12 +7961,6 @@ void reply_transs2(struct smb_request *req)
                return;
        }
 
-       /*
-        * construct_reply_common will copy smb_com from inbuf to
-        * outbuf. SMBtranss2 is wrong here.
-        */
-       SCVAL(req->inbuf,smb_com,SMBtrans2);
-
        handle_trans2(conn, req, state);
 
        DLIST_REMOVE(conn->pending_trans, state);