s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_flush.c
bloba0c3f71a8b57e0f629da701ba94b87cb5b258e26
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 int i = req->current_idx;
39 uint64_t in_file_id_persistent;
40 uint64_t in_file_id_volatile;
41 struct files_struct *in_fsp;
42 struct tevent_req *subreq;
44 status = smbd_smb2_request_verify_sizes(req, 0x18);
45 if (!NT_STATUS_IS_OK(status)) {
46 return smbd_smb2_request_error(req, status);
48 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
50 in_file_id_persistent = BVAL(inbody, 0x08);
51 in_file_id_volatile = BVAL(inbody, 0x10);
53 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
54 if (in_fsp == NULL) {
55 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
58 subreq = smbd_smb2_flush_send(req, req->sconn->ev_ctx,
59 req, in_fsp);
60 if (subreq == NULL) {
61 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
63 tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
65 return smbd_smb2_request_pending_queue(req, subreq, 500);
68 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
70 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
71 struct smbd_smb2_request);
72 DATA_BLOB outbody;
73 NTSTATUS status;
74 NTSTATUS error; /* transport error */
76 status = smbd_smb2_flush_recv(subreq);
77 TALLOC_FREE(subreq);
78 if (!NT_STATUS_IS_OK(status)) {
79 error = smbd_smb2_request_error(req, status);
80 if (!NT_STATUS_IS_OK(error)) {
81 smbd_server_connection_terminate(req->sconn,
82 nt_errstr(error));
83 return;
85 return;
88 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
89 if (outbody.data == NULL) {
90 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
91 if (!NT_STATUS_IS_OK(error)) {
92 smbd_server_connection_terminate(req->sconn,
93 nt_errstr(error));
94 return;
96 return;
99 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
100 SSVAL(outbody.data, 0x02, 0); /* reserved */
102 error = smbd_smb2_request_done(req, outbody, NULL);
103 if (!NT_STATUS_IS_OK(error)) {
104 smbd_server_connection_terminate(req->sconn,
105 nt_errstr(error));
106 return;
110 struct smbd_smb2_flush_state {
111 struct smbd_smb2_request *smb2req;
114 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
115 struct tevent_context *ev,
116 struct smbd_smb2_request *smb2req,
117 struct files_struct *fsp)
119 struct tevent_req *req;
120 struct smbd_smb2_flush_state *state;
121 NTSTATUS status;
122 struct smb_request *smbreq;
124 req = tevent_req_create(mem_ctx, &state,
125 struct smbd_smb2_flush_state);
126 if (req == NULL) {
127 return NULL;
129 state->smb2req = smb2req;
131 DEBUG(10,("smbd_smb2_flush: %s - fnum[%d]\n",
132 fsp_str_dbg(fsp), fsp->fnum));
134 smbreq = smbd_smb2_fake_smb_request(smb2req);
135 if (tevent_req_nomem(smbreq, req)) {
136 return tevent_req_post(req, ev);
139 if (IS_IPC(smbreq->conn)) {
140 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
141 return tevent_req_post(req, ev);
144 if (!CHECK_WRITE(fsp)) {
145 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
146 return tevent_req_post(req, ev);
149 status = sync_file(smbreq->conn, fsp, true);
150 if (!NT_STATUS_IS_OK(status)) {
151 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
152 fsp_str_dbg(fsp), nt_errstr(status)));
153 tevent_req_nterror(req, status);
154 return tevent_req_post(req, ev);
157 tevent_req_done(req);
158 return tevent_req_post(req, ev);
161 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
163 NTSTATUS status;
165 if (tevent_req_is_nterror(req, &status)) {
166 tevent_req_received(req);
167 return status;
170 tevent_req_received(req);
171 return NT_STATUS_OK;