ensure that we honor SMB2 read min_count properly
[kai/samba.git] / source4 / ntvfs / posix / pvfs_read.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - read
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "vfs_posix.h"
24 #include "lib/events/events.h"
25
26 /*
27   read from a file
28 */
29 NTSTATUS pvfs_read(struct ntvfs_module_context *ntvfs,
30                    struct ntvfs_request *req, union smb_read *rd)
31 {
32         struct pvfs_state *pvfs = ntvfs->private_data;
33         ssize_t ret;
34         struct pvfs_file *f;
35         NTSTATUS status;
36         uint32_t maxcnt;
37         uint32_t mask;
38
39         if (rd->generic.level != RAW_READ_READX) {
40                 return ntvfs_map_read(ntvfs, req, rd);
41         }
42
43         f = pvfs_find_fd(pvfs, req, rd->readx.in.file.ntvfs);
44         if (!f) {
45                 return NT_STATUS_INVALID_HANDLE;
46         }
47
48         if (f->handle->fd == -1) {
49                 return NT_STATUS_FILE_IS_A_DIRECTORY;
50         }
51
52         mask = SEC_FILE_READ_DATA;
53         if (rd->readx.in.read_for_execute) {
54                 mask |= SEC_FILE_EXECUTE;
55         }
56         if (!(f->access_mask & mask)) {
57                 return NT_STATUS_ACCESS_DENIED;
58         }
59
60         maxcnt = rd->readx.in.maxcnt;
61         if (maxcnt > UINT16_MAX && req->ctx->protocol < PROTOCOL_SMB2) {
62                 maxcnt = 0;
63         }
64
65         status = pvfs_check_lock(pvfs, f, req->smbpid, 
66                                  rd->readx.in.offset,
67                                  maxcnt,
68                                  READ_LOCK);
69         if (!NT_STATUS_IS_OK(status)) {
70                 return status;
71         }
72
73         if (f->handle->name->stream_name) {
74                 ret = pvfs_stream_read(pvfs, f->handle, 
75                                        rd->readx.out.data, maxcnt, rd->readx.in.offset);
76         } else {
77 #if HAVE_LINUX_AIO
78                 /* possibly try an aio read */
79                 if ((req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC) &&
80                     (pvfs->flags & PVFS_FLAG_LINUX_AIO)) {
81                         status = pvfs_aio_pread(req, rd, f, maxcnt);
82                         if (NT_STATUS_IS_OK(status)) {
83                                 return NT_STATUS_OK;
84                         }
85                 }
86 #endif
87                 ret = pread(f->handle->fd, 
88                             rd->readx.out.data, 
89                             maxcnt,
90                             rd->readx.in.offset);
91         }
92         if (ret == -1) {
93                 return pvfs_map_errno(pvfs, errno);
94         }
95
96         /* only SMB2 honors mincnt */
97         if (req->ctx->protocol == PROTOCOL_SMB2) {
98                 if (rd->readx.in.mincnt > ret ||
99                     (ret == 0 && maxcnt > 0)) {
100                         return NT_STATUS_END_OF_FILE;
101                 }
102         }
103
104         f->handle->position = f->handle->seek_offset = rd->readx.in.offset + ret;
105
106         rd->readx.out.nread = ret;
107         rd->readx.out.remaining = 0xFFFF;
108         rd->readx.out.compaction_mode = 0; 
109
110         return NT_STATUS_OK;
111 }