s3:printing: Fix size type in printing_db
[nivanova/samba-autobuild/.git] / source3 / smbd / smb2_flush.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
26
27 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
28                                                struct tevent_context *ev,
29                                                struct smbd_smb2_request *smb2req,
30                                                struct files_struct *fsp);
31 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req);
32
33 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
34 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
35 {
36         NTSTATUS status;
37         const uint8_t *inbody;
38         uint64_t in_file_id_persistent;
39         uint64_t in_file_id_volatile;
40         struct files_struct *in_fsp;
41         struct tevent_req *subreq;
42
43         status = smbd_smb2_request_verify_sizes(req, 0x18);
44         if (!NT_STATUS_IS_OK(status)) {
45                 return smbd_smb2_request_error(req, status);
46         }
47         inbody = SMBD_SMB2_IN_BODY_PTR(req);
48
49         in_file_id_persistent   = BVAL(inbody, 0x08);
50         in_file_id_volatile     = BVAL(inbody, 0x10);
51
52         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
53         if (in_fsp == NULL) {
54                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
55         }
56
57         subreq = smbd_smb2_flush_send(req, req->sconn->ev_ctx,
58                                       req, in_fsp);
59         if (subreq == NULL) {
60                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
61         }
62         tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
63
64         return smbd_smb2_request_pending_queue(req, subreq, 500);
65 }
66
67 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
68 {
69         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
70                                         struct smbd_smb2_request);
71         DATA_BLOB outbody;
72         NTSTATUS status;
73         NTSTATUS error; /* transport error */
74
75         status = smbd_smb2_flush_recv(subreq);
76         TALLOC_FREE(subreq);
77         if (!NT_STATUS_IS_OK(status)) {
78                 error = smbd_smb2_request_error(req, status);
79                 if (!NT_STATUS_IS_OK(error)) {
80                         smbd_server_connection_terminate(req->xconn,
81                                                          nt_errstr(error));
82                         return;
83                 }
84                 return;
85         }
86
87         outbody = smbd_smb2_generate_outbody(req, 0x04);
88         if (outbody.data == NULL) {
89                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
90                 if (!NT_STATUS_IS_OK(error)) {
91                         smbd_server_connection_terminate(req->xconn,
92                                                          nt_errstr(error));
93                         return;
94                 }
95                 return;
96         }
97
98         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
99         SSVAL(outbody.data, 0x02, 0);           /* reserved */
100
101         error = smbd_smb2_request_done(req, outbody, NULL);
102         if (!NT_STATUS_IS_OK(error)) {
103                 smbd_server_connection_terminate(req->xconn,
104                                                  nt_errstr(error));
105                 return;
106         }
107 }
108
109 struct smbd_smb2_flush_state {
110         struct smbd_smb2_request *smb2req;
111 };
112
113 static void smbd_smb2_flush_done(struct tevent_req *subreq);
114
115 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
116                                                struct tevent_context *ev,
117                                                struct smbd_smb2_request *smb2req,
118                                                struct files_struct *fsp)
119 {
120         struct tevent_req *req;
121         struct tevent_req *subreq;
122         struct smbd_smb2_flush_state *state;
123         struct smb_request *smbreq;
124         int ret;
125
126         req = tevent_req_create(mem_ctx, &state,
127                                 struct smbd_smb2_flush_state);
128         if (req == NULL) {
129                 return NULL;
130         }
131         state->smb2req = smb2req;
132
133         DEBUG(10,("smbd_smb2_flush: %s - %s\n",
134                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
135
136         smbreq = smbd_smb2_fake_smb_request(smb2req);
137         if (tevent_req_nomem(smbreq, req)) {
138                 return tevent_req_post(req, ev);
139         }
140
141         if (IS_IPC(smbreq->conn)) {
142                 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
143                 return tevent_req_post(req, ev);
144         }
145
146         if (!CHECK_WRITE(fsp)) {
147                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
148                 return tevent_req_post(req, ev);
149         }
150
151         if (fsp->fh->fd == -1) {
152                 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
153                 return tevent_req_post(req, ev);
154         }
155
156         if (!lp_strict_sync(SNUM(smbreq->conn))) {
157                 /*
158                  * No strict sync. Don't really do
159                  * anything here.
160                  */
161                 tevent_req_done(req);
162                 return tevent_req_post(req, ev);
163         }
164
165         ret = flush_write_cache(fsp, SAMBA_SYNC_FLUSH);
166         if (ret == -1) {
167                 tevent_req_nterror(req,  map_nt_error_from_unix(errno));
168                 return tevent_req_post(req, ev);
169         }
170
171         subreq = SMB_VFS_FSYNC_SEND(state, ev, fsp);
172         if (tevent_req_nomem(subreq, req)) {
173                 return tevent_req_post(req, ev);
174         }
175
176         tevent_req_set_callback(subreq, smbd_smb2_flush_done, req);
177
178         /* Ensure any close request knows about this outstanding IO. */
179         if (!aio_add_req_to_fsp(fsp, req)) {
180                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
181                 return tevent_req_post(req, ev);
182         }
183
184         increment_outstanding_aio_calls();
185         return req;
186
187 }
188
189 static void smbd_smb2_flush_done(struct tevent_req *subreq)
190 {
191         struct tevent_req *req = tevent_req_callback_data(
192                 subreq, struct tevent_req);
193         int ret;
194         struct vfs_aio_state vfs_aio_state;
195
196         decrement_outstanding_aio_calls();
197
198         ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
199         TALLOC_FREE(subreq);
200         if (ret == -1) {
201                 tevent_req_nterror(req, map_nt_error_from_unix(vfs_aio_state.error));
202                 return;
203         }
204         tevent_req_done(req);
205 }
206
207 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
208 {
209         NTSTATUS status;
210
211         if (tevent_req_is_nterror(req, &status)) {
212                 tevent_req_received(req);
213                 return status;
214         }
215
216         tevent_req_received(req);
217         return NT_STATUS_OK;
218 }