e4725ec579f22c9e785465adcaba2b990f8ef453
[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 #include "lib/events/events.h"
26
27 static void pvfs_write_time_update_handler(struct tevent_context *ev,
28                                            struct tevent_timer *te,
29                                            struct timeval tv,
30                                            void *private_data)
31 {
32         struct pvfs_file_handle *h = talloc_get_type(private_data,
33                                      struct pvfs_file_handle);
34         struct odb_lock *lck;
35         NTSTATUS status;
36         NTTIME write_time;
37
38         lck = odb_lock(h, h->pvfs->odb_context, &h->odb_locking_key);
39         if (lck == NULL) {
40                 DEBUG(0,("Unable to lock opendb for write time update\n"));
41                 return;
42         }
43
44         write_time = timeval_to_nttime(&tv);
45
46         status = odb_set_write_time(lck, write_time, false);
47         if (!NT_STATUS_IS_OK(status)) {
48                 DEBUG(0,("Unable to update write time: %s\n",
49                         nt_errstr(status)));
50                 return;
51         }
52
53         talloc_free(lck);
54
55         h->write_time.update_event = NULL;
56 }
57
58 static void pvfs_trigger_write_time_update(struct pvfs_file_handle *h)
59 {
60         struct pvfs_state *pvfs = h->pvfs;
61         struct timeval tv;
62
63         if (h->write_time.update_triggered) {
64                 return;
65         }
66
67         tv = timeval_current_ofs_usec(pvfs->writetime_delay);
68
69         h->write_time.update_triggered = true;
70         h->write_time.update_on_close = true;
71         h->write_time.update_event = tevent_add_timer(pvfs->ntvfs->ctx->event_ctx,
72                                                      h, tv,
73                                                      pvfs_write_time_update_handler,
74                                                      h);
75         if (!h->write_time.update_event) {
76                 DEBUG(0,("Failed tevent_add_timer\n"));
77         }
78 }
79
80 /*
81   write to a file
82 */
83 NTSTATUS pvfs_write(struct ntvfs_module_context *ntvfs,
84                     struct ntvfs_request *req, union smb_write *wr)
85 {
86         struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
87                                   struct pvfs_state);
88         ssize_t ret;
89         struct pvfs_file *f;
90         NTSTATUS status;
91
92         if (wr->generic.level != RAW_WRITE_WRITEX) {
93                 return ntvfs_map_write(ntvfs, req, wr);
94         }
95
96         f = pvfs_find_fd(pvfs, req, wr->writex.in.file.ntvfs);
97         if (!f) {
98                 return NT_STATUS_INVALID_HANDLE;
99         }
100
101         if (f->handle->fd == -1) {
102                 return NT_STATUS_INVALID_DEVICE_REQUEST;
103         }
104
105         if (!(f->access_mask & (SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA))) {
106                 return NT_STATUS_ACCESS_DENIED;
107         }
108
109         status = pvfs_check_lock(pvfs, f, req->smbpid, 
110                                  wr->writex.in.offset,
111                                  wr->writex.in.count,
112                                  WRITE_LOCK);
113         NT_STATUS_NOT_OK_RETURN(status);
114
115         status = pvfs_break_level2_oplocks(f);
116         NT_STATUS_NOT_OK_RETURN(status);
117
118         pvfs_trigger_write_time_update(f->handle);
119
120         if (f->handle->name->stream_name) {
121                 ret = pvfs_stream_write(pvfs,
122                                         f->handle,
123                                         wr->writex.in.data, 
124                                         wr->writex.in.count,
125                                         wr->writex.in.offset);
126         } else {
127 #ifdef HAVE_LINUX_AIO
128                 /* possibly try an aio write */
129                 if ((req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC) &&
130                     (pvfs->flags & PVFS_FLAG_LINUX_AIO)) {
131                         status = pvfs_aio_pwrite(req, wr, f);
132                         if (NT_STATUS_IS_OK(status)) {
133                                 return NT_STATUS_OK;
134                         }
135                 }
136 #endif
137                 ret = pwrite(f->handle->fd, 
138                              wr->writex.in.data, 
139                              wr->writex.in.count,
140                              wr->writex.in.offset);
141         }
142         if (ret == -1) {
143                 if (errno == EFBIG) {
144                         return NT_STATUS_INVALID_PARAMETER;
145                 }
146                 return pvfs_map_errno(pvfs, errno);
147         }
148
149         f->handle->seek_offset = wr->writex.in.offset + ret;
150         
151         wr->writex.out.nwritten = ret;
152         wr->writex.out.remaining = 0; /* should fill this in? */
153
154         return NT_STATUS_OK;
155 }