r7963: Add aio support to 3.0.
[jra/samba/.git] / source3 / smbd / open.c
index 9d9d9d9208313062c095b80b0b01a15d2b0ef5a7..9da53a5057a96116e457157010bfbd7cb6ff5c4d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "includes.h"
 
+extern struct current_user current_user;
 extern userdom_struct current_user_info;
 extern uint16 global_oplock_port;
 extern uint16 global_smbpid;
@@ -76,9 +77,7 @@ static void check_for_pipe(const char *fname)
        strlower_m(s);
        if (strstr(s,"pipe/")) {
                DEBUG(3,("Rejecting named pipe open for %s\n",fname));
-               unix_ERR_class = ERRSRV;
-               unix_ERR_code = ERRaccess;
-               unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
+               set_saved_error_triple(ERRSRV, ERRaccess, NT_STATUS_ACCESS_DENIED);
        }
 }
 
@@ -174,7 +173,6 @@ Error was %s\n",
 static BOOL open_file(files_struct *fsp,connection_struct *conn,
                      const char *fname,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access)
 {
-       extern struct current_user current_user;
        int accmode = (flags & O_ACCMODE);
        int local_flags = flags;
 
@@ -250,9 +248,7 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
 
                /* Don't create files with Microsoft wildcard characters. */
                if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf) && ms_has_wild(fname))  {
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRinvalidname;
-                       unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID;
+                       set_saved_error_triple(ERRDOS, ERRinvalidname, NT_STATUS_OBJECT_NAME_INVALID);
                        return False;
                }
 
@@ -308,7 +304,6 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->dev = psbuf->st_dev;
        fsp->vuid = current_user.vuid;
        fsp->file_pid = global_smbpid;
-       fsp->size = psbuf->st_size;
        fsp->can_lock = True;
        fsp->can_read = ((flags & O_WRONLY)==0);
        fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
@@ -321,6 +316,10 @@ static BOOL open_file(files_struct *fsp,connection_struct *conn,
        fsp->is_directory = False;
        fsp->is_stat = False;
        fsp->directory_delete_on_close = False;
+       if (conn->aio_write_behind_list && is_in_path(fname, conn->aio_write_behind_list, conn->case_sensitive)) {
+               fsp->aio_write_behind = True;
+       }
+
        string_set(&fsp->fsp_name,fname);
        fsp->wcp = NULL; /* Write cache pointer. */
 
@@ -487,9 +486,7 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
                DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
                        fname ));
                /* Use errno to map to correct error. */
-               unix_ERR_class = SMB_SUCCESS;
-               unix_ERR_code = 0;
-               unix_ERR_ntstatus = NT_STATUS_OK;
+               set_saved_error_triple(SMB_SUCCESS, 0, NT_STATUS_OK);
                return False;
        }
 
@@ -529,10 +526,7 @@ static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, i
                                (!GET_ALLOW_SHARE_DELETE(share->share_mode) || !GET_ALLOW_SHARE_DELETE(share_mode))) {
                        DEBUG(5,("check_share_mode: Failing open on file %s as delete access requests conflict.\n",
                                fname ));
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRbadshare;
-                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
-
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return False;
                }
 
@@ -557,10 +551,7 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
        if ((desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share->share_mode)) {
                DEBUG(5,("check_share_mode: Failing open on file %s as delete access requested and allow share delete not set.\n",
                        fname ));
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRbadshare;
-               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
-
+               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                return False;
        }
 
@@ -573,18 +564,14 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
        if ((share->desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share_mode)) {
                DEBUG(5,("check_share_mode: Failing open on file %s as delete access granted and allow share delete not requested.\n",
                        fname ));
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRbadshare;
-               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                return False;
        }
 
 #if 0
        /* Bluarc test may need this ... needs further investigation. */
        if (deny_mode == DENY_ALL || old_deny_mode == DENY_ALL) {
-               unix_ERR_class = ERRDOS;
-               unix_ERR_code = ERRbadshare;
-               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                return False;
        }
 #endif
@@ -614,10 +601,7 @@ existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsign
                                deny_mode,old_deny_mode,old_open_mode,
                                (int)share->pid,fname, fcbopen, *flags, access_allowed));
 
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRbadshare;
-                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
-
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return False;
                }
 
@@ -768,10 +752,7 @@ dev = %x, inode = %.0f\n", *p_oplock_request, share_entry->op_type, fname, (unsi
                                        DEBUG(0,("open_mode_check: FAILED when breaking oplock (%x) on file %s, \
 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
                                        SAFE_FREE(old_shares);
-                                       errno = EACCES;
-                                       unix_ERR_class = ERRDOS;
-                                       unix_ERR_code = ERRbadshare;
-                                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                                        return -1;
                                }
                                
@@ -835,9 +816,7 @@ after break ! For file %s, dev = %x, inode = %.0f. Deleting it to continue...\n"
                                        if (del_share_entry(dev, inode, &broken_entry->entry, NULL) == -1) {
                                                free_broken_entry_list(broken_entry_list);
                                                errno = EACCES;
-                                               unix_ERR_class = ERRDOS;
-                                               unix_ERR_code = ERRbadshare;
-                                               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                                               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                                                return -1;
                                        }
                                        
@@ -1104,9 +1083,7 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                        delete_defered_open_entry_record(conn, dib.dev, dib.inode);
                        unlock_share_entry(conn, dib.dev, dib.inode);
 
-                       unix_ERR_class = ERRDOS;
-                       unix_ERR_code = ERRbadshare;
-                       unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return NULL;
                }
                /* Ensure we don't reprocess this message. */
@@ -1124,15 +1101,10 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                return print_fsp_open(conn, fname);
        }
 
-       fsp = file_new(conn);
-       if(!fsp)
-               return NULL;
-
        DEBUG(10,("open_file_shared: fname = %s, dos_attrs = %x, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
                fname, new_dos_mode, share_mode, ofun, (int)mode,  oplock_request ));
 
        if (!check_name(fname,conn)) {
-               file_free(fsp);
                return NULL;
        } 
 
@@ -1148,21 +1120,15 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
 
        /* this is for OS/2 long file names - say we don't support them */
        if (strstr(fname,".+,;=[].")) {
-               unix_ERR_class = ERRDOS;
                /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
-               unix_ERR_code = ERRcannotopen;
-               unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_NOT_FOUND;
+               set_saved_error_triple(ERRDOS, ERRcannotopen, NT_STATUS_OBJECT_NAME_NOT_FOUND);
                DEBUG(5,("open_file_shared: OS/2 long filenames are not supported.\n"));
-               /* need to reset errno or DEVELOPER will cause us to coredump */
-               errno = 0;
-               file_free(fsp);
                return NULL;
        }
 
        if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed)  {
                DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
                        fname ));
-               file_free(fsp);
                if (S_ISDIR(psbuf->st_mode)) {
                        errno = EISDIR;
                } else {
@@ -1184,7 +1150,6 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                        DEBUG(5,("open_file_shared: attributes missmatch for file %s (%x %x) (0%o, 0%o)\n",
                                                fname, existing_dos_mode, new_dos_mode,
                                                (int)psbuf->st_mode, (int)mode ));
-                       file_free(fsp);
                        errno = EACCES;
                        return NULL;
                }
@@ -1197,6 +1162,12 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                append does not mean the same thing under dos and unix */
 
        switch (GET_OPEN_MODE(share_mode)) {
+               case DOS_OPEN_EXEC:
+               case DOS_OPEN_RDONLY:
+                       flags = O_RDONLY;
+                       if (desired_access == 0)
+                               desired_access = FILE_READ_DATA;
+                       break;
                case DOS_OPEN_WRONLY: 
                        flags = O_WRONLY; 
                        if (desired_access == 0)
@@ -1214,10 +1185,9 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                                desired_access = FILE_READ_DATA|FILE_WRITE_DATA;
                        break;
                default:
-                       flags = O_RDONLY;
-                       if (desired_access == 0)
-                               desired_access = FILE_READ_DATA;
-                       break;
+                       /* Force DOS error. */
+                       set_saved_error_triple(ERRDOS, ERRinvalidparam, NT_STATUS_INVALID);
+                       return NULL;
        }
 
 #if defined(O_SYNC)
@@ -1231,7 +1201,6 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                if (!fcbopen) {
                        DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
                                fname, !CAN_WRITE(conn) ? "share" : "file" ));
-                       file_free(fsp);
                        errno = EACCES;
                        return NULL;
                }
@@ -1240,7 +1209,6 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
 
        if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) {
                DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
-               file_free(fsp);
                errno = EINVAL;
                return NULL;
        }
@@ -1256,6 +1224,10 @@ files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_
                }
        }
 
+       fsp = file_new(conn);
+       if(!fsp)
+               return NULL;
+
        if (file_existed) {
 
                dev = psbuf->st_dev;
@@ -1289,9 +1261,8 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                flags,(flags2&~(O_TRUNC|O_CREAT)),(int)mode,(int)fsp_open ));
 
                        if (!fsp_open && errno) {
-                               unix_ERR_class = ERRDOS;
-                               unix_ERR_code = ERRnoaccess;
-                               unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED;
+                               /* Default error. */
+                               set_saved_error_triple(ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED);
                        }
 
                        /* 
@@ -1299,9 +1270,13 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                         * the braindead 1 second delay.
                         */
 
-                       if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
-                               /* The fsp->open_time here represents the current time of day. */
-                               defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
+                       if (!internal_only_open) {
+                               NTSTATUS status;
+                               get_saved_error_triple(NULL, NULL, &status);
+                               if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION)) {
+                                       /* The fsp->open_time here represents the current time of day. */
+                                       defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
+                               }
                        }
 
                        unlock_share_entry(conn, dev, inode);
@@ -1311,9 +1286,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                                 * We have detected a sharing violation here
                                 * so return the correct error code
                                 */
-                               unix_ERR_class = ERRDOS;
-                               unix_ERR_code = ERRbadshare;
-                               unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                               set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        }
                        file_free(fsp);
                        return NULL;
@@ -1385,7 +1358,9 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                         * the braindead 1 second delay.
                         */
 
-                       if (!internal_only_open && NT_STATUS_EQUAL(unix_ERR_ntstatus,NT_STATUS_SHARING_VIOLATION)) {
+                       NTSTATUS status;
+                       get_saved_error_triple(NULL, NULL, &status);
+                       if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION)) {
                                /* The fsp->open_time here represents the current time of day. */
                                defer_open_sharing_error(conn, &fsp->open_time, fname, dev, inode);
                        }
@@ -1397,9 +1372,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                         * We have detected a sharing violation here, so
                         * return the correct code.
                         */
-                        unix_ERR_class = ERRDOS;
-                        unix_ERR_code = ERRbadshare;
-                        unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+                       set_saved_error_triple(ERRDOS, ERRbadshare, NT_STATUS_SHARING_VIOLATION);
                        return NULL;
                }
 
@@ -1465,7 +1438,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
        DEBUG(10,("open_file_shared : share_mode = %x\n", fsp->share_mode ));
 
        if (Access) {
-               (*Access) = open_mode;
+               (*Access) = (SET_DENY_MODE(deny_mode) | SET_OPEN_MODE(open_mode));
        }
 
        action = 0;
@@ -1527,9 +1500,7 @@ flags=0x%X flags2=0x%X mode=0%o returned %d\n",
                        fd_close(conn,fsp);
                        file_free(fsp);
                        ntstatus_to_dos(result, &u_e_c, &u_e_code);
-                        unix_ERR_ntstatus = result;
-                        unix_ERR_class = u_e_c;
-                        unix_ERR_code = u_e_code;
+                       set_saved_error_triple(u_e_c, u_e_code, result);
                        return NULL;
                }
        }
@@ -1638,7 +1609,6 @@ int close_file_fchmod(files_struct *fsp)
 files_struct *open_directory(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf,
                        uint32 desired_access, int share_mode, int smb_ofun, int *action)
 {
-       extern struct current_user current_user;
        BOOL got_stat = False;
        files_struct *fsp = file_new(conn);
        BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode);
@@ -1682,7 +1652,7 @@ files_struct *open_directory(connection_struct *conn, const char *fname, SMB_STR
                                         fname, strerror(errno) ));
                                file_free(fsp);
                                /* Ensure we return the correct NT status to the client. */
-                               unix_ERR_ntstatus = status;
+                               set_saved_error_triple(0, 0, status);
                                return NULL;
                        }
 
@@ -1734,7 +1704,6 @@ files_struct *open_directory(connection_struct *conn, const char *fname, SMB_STR
        fsp->mode = psbuf->st_mode;
        fsp->inode = psbuf->st_ino;
        fsp->dev = psbuf->st_dev;
-       fsp->size = psbuf->st_size;
        fsp->vuid = current_user.vuid;
        fsp->file_pid = global_smbpid;
        fsp->can_lock = True;
@@ -1776,7 +1745,6 @@ files_struct *open_directory(connection_struct *conn, const char *fname, SMB_STR
 
 files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf)
 {
-       extern struct current_user current_user;
        files_struct *fsp = NULL;
 
        if (!VALID_STAT(*psbuf))
@@ -1799,7 +1767,6 @@ files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_ST
        fsp->mode = psbuf->st_mode;
        fsp->inode = psbuf->st_ino;
        fsp->dev = psbuf->st_dev;
-       fsp->size = psbuf->st_size;
        fsp->vuid = current_user.vuid;
        fsp->file_pid = global_smbpid;
        fsp->can_lock = False;