Add test for 'samba-tool user edit'
[Samba.git] / source3 / smbd / smb2_flush.c
blobd077c62c40345da38cee41dfedda24aa43709f96
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
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"
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);
33 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
34 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
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;
43 status = smbd_smb2_request_verify_sizes(req, 0x18);
44 if (!NT_STATUS_IS_OK(status)) {
45 return smbd_smb2_request_error(req, status);
47 inbody = SMBD_SMB2_IN_BODY_PTR(req);
49 in_file_id_persistent = BVAL(inbody, 0x08);
50 in_file_id_volatile = BVAL(inbody, 0x10);
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);
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);
62 tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
64 return smbd_smb2_request_pending_queue(req, subreq, 500);
67 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
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 */
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;
84 return;
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;
95 return;
98 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
99 SSVAL(outbody.data, 0x02, 0); /* reserved */
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;
109 struct smbd_smb2_flush_state {
110 struct smbd_smb2_request *smb2req;
113 static void smbd_smb2_flush_done(struct tevent_req *subreq);
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)
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;
126 req = tevent_req_create(mem_ctx, &state,
127 struct smbd_smb2_flush_state);
128 if (req == NULL) {
129 return NULL;
131 state->smb2req = smb2req;
133 DEBUG(10,("smbd_smb2_flush: %s - %s\n",
134 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
136 smbreq = smbd_smb2_fake_smb_request(smb2req);
137 if (tevent_req_nomem(smbreq, req)) {
138 return tevent_req_post(req, ev);
141 if (IS_IPC(smbreq->conn)) {
142 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
143 return tevent_req_post(req, ev);
146 if (!CHECK_WRITE(fsp)) {
147 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
148 return tevent_req_post(req, ev);
151 if (fsp->fh->fd == -1) {
152 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
153 return tevent_req_post(req, ev);
156 if (!lp_strict_sync(SNUM(smbreq->conn))) {
158 * No strict sync. Don't really do
159 * anything here.
161 tevent_req_done(req);
162 return tevent_req_post(req, ev);
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);
171 subreq = SMB_VFS_FSYNC_SEND(state, ev, fsp);
172 if (tevent_req_nomem(subreq, req)) {
173 return tevent_req_post(req, ev);
176 tevent_req_set_callback(subreq, smbd_smb2_flush_done, req);
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);
184 increment_outstanding_aio_calls();
185 return req;
189 static void smbd_smb2_flush_done(struct tevent_req *subreq)
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;
196 decrement_outstanding_aio_calls();
198 ret = SMB_VFS_FSYNC_RECV(subreq, &vfs_aio_state);
199 TALLOC_FREE(subreq);
200 if (ret == -1) {
201 tevent_req_error(req, vfs_aio_state.error);
202 return;
204 tevent_req_done(req);
207 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
209 NTSTATUS status;
211 if (tevent_req_is_nterror(req, &status)) {
212 tevent_req_received(req);
213 return status;
216 tevent_req_received(req);
217 return NT_STATUS_OK;