2 Unix SMB/CIFS implementation.
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/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
28 #define DBGC_CLASS DBGC_SMB2
30 static struct tevent_req
*smbd_smb2_flush_send(TALLOC_CTX
*mem_ctx
,
31 struct tevent_context
*ev
,
32 struct smbd_smb2_request
*smb2req
,
33 struct files_struct
*fsp
);
34 static NTSTATUS
smbd_smb2_flush_recv(struct tevent_req
*req
);
36 static void smbd_smb2_request_flush_done(struct tevent_req
*subreq
);
37 NTSTATUS
smbd_smb2_request_process_flush(struct smbd_smb2_request
*req
)
40 const uint8_t *inbody
;
41 uint64_t in_file_id_persistent
;
42 uint64_t in_file_id_volatile
;
43 struct files_struct
*in_fsp
;
44 struct tevent_req
*subreq
;
46 status
= smbd_smb2_request_verify_sizes(req
, 0x18);
47 if (!NT_STATUS_IS_OK(status
)) {
48 return smbd_smb2_request_error(req
, status
);
50 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
52 in_file_id_persistent
= BVAL(inbody
, 0x08);
53 in_file_id_volatile
= BVAL(inbody
, 0x10);
55 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
57 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
60 subreq
= smbd_smb2_flush_send(req
, req
->sconn
->ev_ctx
,
63 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
65 tevent_req_set_callback(subreq
, smbd_smb2_request_flush_done
, req
);
67 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
70 static void smbd_smb2_request_flush_done(struct tevent_req
*subreq
)
72 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
73 struct smbd_smb2_request
);
76 NTSTATUS error
; /* transport error */
78 status
= smbd_smb2_flush_recv(subreq
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 error
= smbd_smb2_request_error(req
, status
);
82 if (!NT_STATUS_IS_OK(error
)) {
83 smbd_server_connection_terminate(req
->xconn
,
90 outbody
= smbd_smb2_generate_outbody(req
, 0x04);
91 if (outbody
.data
== NULL
) {
92 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
93 if (!NT_STATUS_IS_OK(error
)) {
94 smbd_server_connection_terminate(req
->xconn
,
101 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
102 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
104 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
105 if (!NT_STATUS_IS_OK(error
)) {
106 smbd_server_connection_terminate(req
->xconn
,
112 struct smbd_smb2_flush_state
{
113 struct smbd_smb2_request
*smb2req
;
116 static void smbd_smb2_flush_done(struct tevent_req
*subreq
);
118 static struct tevent_req
*smbd_smb2_flush_send(TALLOC_CTX
*mem_ctx
,
119 struct tevent_context
*ev
,
120 struct smbd_smb2_request
*smb2req
,
121 struct files_struct
*fsp
)
123 struct tevent_req
*req
;
124 struct tevent_req
*subreq
;
125 struct smbd_smb2_flush_state
*state
;
126 struct smb_request
*smbreq
;
129 req
= tevent_req_create(mem_ctx
, &state
,
130 struct smbd_smb2_flush_state
);
134 state
->smb2req
= smb2req
;
136 DEBUG(10,("smbd_smb2_flush: %s - %s\n",
137 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
139 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
140 if (tevent_req_nomem(smbreq
, req
)) {
141 return tevent_req_post(req
, ev
);
144 if (IS_IPC(smbreq
->conn
)) {
145 tevent_req_nterror(req
, NT_STATUS_NOT_IMPLEMENTED
);
146 return tevent_req_post(req
, ev
);
149 if (!CHECK_WRITE(fsp
)) {
150 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
151 return tevent_req_post(req
, ev
);
154 if (fsp
->fh
->fd
== -1) {
155 tevent_req_nterror(req
, NT_STATUS_INVALID_HANDLE
);
156 return tevent_req_post(req
, ev
);
159 if (!lp_strict_sync(SNUM(smbreq
->conn
))) {
161 * No strict sync. Don't really do
164 tevent_req_done(req
);
165 return tevent_req_post(req
, ev
);
168 ret
= flush_write_cache(fsp
, SAMBA_SYNC_FLUSH
);
170 tevent_req_nterror(req
, map_nt_error_from_unix(errno
));
171 return tevent_req_post(req
, ev
);
174 subreq
= SMB_VFS_FSYNC_SEND(state
, ev
, fsp
);
175 if (tevent_req_nomem(subreq
, req
)) {
176 return tevent_req_post(req
, ev
);
179 tevent_req_set_callback(subreq
, smbd_smb2_flush_done
, req
);
181 /* Ensure any close request knows about this outstanding IO. */
182 if (!aio_add_req_to_fsp(fsp
, req
)) {
183 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
184 return tevent_req_post(req
, ev
);
191 static void smbd_smb2_flush_done(struct tevent_req
*subreq
)
193 struct tevent_req
*req
= tevent_req_callback_data(
194 subreq
, struct tevent_req
);
196 struct vfs_aio_state vfs_aio_state
;
198 ret
= SMB_VFS_FSYNC_RECV(subreq
, &vfs_aio_state
);
201 tevent_req_nterror(req
, map_nt_error_from_unix(vfs_aio_state
.error
));
204 tevent_req_done(req
);
207 static NTSTATUS
smbd_smb2_flush_recv(struct tevent_req
*req
)
211 if (tevent_req_is_nterror(req
, &status
)) {
212 tevent_req_received(req
);
216 tevent_req_received(req
);