dda8c8340738e4b8d16e1f12200ab83934d3a6a7
[kai/samba.git] / source4 / ntvfs / posix / pvfs_write.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - write
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 "librpc/gen_ndr/security.h"
25
26
27 /*
28   write to a file
29 */
30 NTSTATUS pvfs_write(struct ntvfs_module_context *ntvfs,
31                     struct ntvfs_request *req, union smb_write *wr)
32 {
33         struct pvfs_state *pvfs = ntvfs->private_data;
34         ssize_t ret;
35         struct pvfs_file *f;
36         NTSTATUS status;
37
38         if (wr->generic.level != RAW_WRITE_WRITEX) {
39                 return ntvfs_map_write(ntvfs, req, wr);
40         }
41
42         f = pvfs_find_fd(pvfs, req, wr->writex.in.file.ntvfs);
43         if (!f) {
44                 return NT_STATUS_INVALID_HANDLE;
45         }
46
47         if (f->handle->fd == -1) {
48                 return NT_STATUS_FILE_IS_A_DIRECTORY;
49         }
50
51         if (!(f->access_mask & (SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA))) {
52                 return NT_STATUS_ACCESS_DENIED;
53         }
54
55         status = pvfs_check_lock(pvfs, f, req->smbpid, 
56                                  wr->writex.in.offset,
57                                  wr->writex.in.count,
58                                  WRITE_LOCK);
59         NT_STATUS_NOT_OK_RETURN(status);
60
61         status = pvfs_break_level2_oplocks(f);
62         NT_STATUS_NOT_OK_RETURN(status);
63
64         if (f->handle->name->stream_name) {
65                 ret = pvfs_stream_write(pvfs,
66                                         f->handle,
67                                         wr->writex.in.data, 
68                                         wr->writex.in.count,
69                                         wr->writex.in.offset);
70         } else {
71 #if HAVE_LINUX_AIO
72                 /* possibly try an aio write */
73                 if ((req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC) &&
74                     (pvfs->flags & PVFS_FLAG_LINUX_AIO)) {
75                         status = pvfs_aio_pwrite(req, wr, f);
76                         if (NT_STATUS_IS_OK(status)) {
77                                 return NT_STATUS_OK;
78                         }
79                 }
80 #endif
81                 ret = pwrite(f->handle->fd, 
82                              wr->writex.in.data, 
83                              wr->writex.in.count,
84                              wr->writex.in.offset);
85         }
86         if (ret == -1) {
87                 if (errno == EFBIG) {
88                         return NT_STATUS_INVALID_PARAMETER;
89                 }
90                 return pvfs_map_errno(pvfs, errno);
91         }
92
93         f->handle->seek_offset = wr->writex.in.offset + ret;
94         
95         wr->writex.out.nwritten = ret;
96         wr->writex.out.remaining = 0; /* should fill this in? */
97
98         return NT_STATUS_OK;
99 }