smbd/ioctl: add FSCTL_SET_ZERO_DATA support
[Samba.git] / source3 / smbd / smb2_ioctl_filesys.c
blob63ec19661efdddf958db195268e8c8a651dc2c88
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) David Disseldorp 2013-2015
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.
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.
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/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../libcli/security/security.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "rpc_server/srv_pipe_hnd.h"
29 #include "include/ntioctl.h"
30 #include "../librpc/ndr/libndr.h"
31 #include "librpc/gen_ndr/ndr_ioctl.h"
32 #include "smb2_ioctl_private.h"
34 static NTSTATUS fsctl_get_cmprn(TALLOC_CTX *mem_ctx,
35 struct tevent_context *ev,
36 struct files_struct *fsp,
37 size_t in_max_output,
38 DATA_BLOB *out_output)
40 struct compression_state cmpr_state;
41 enum ndr_err_code ndr_ret;
42 DATA_BLOB output;
43 NTSTATUS status;
45 if (fsp == NULL) {
46 return NT_STATUS_FILE_CLOSED;
49 /* Windows doesn't check for SEC_FILE_READ_ATTRIBUTE permission here */
51 if ((fsp->conn->fs_capabilities & FILE_FILE_COMPRESSION) == 0) {
52 DEBUG(4, ("FS does not advertise compression support\n"));
53 return NT_STATUS_NOT_SUPPORTED;
56 ZERO_STRUCT(cmpr_state);
57 status = SMB_VFS_GET_COMPRESSION(fsp->conn,
58 mem_ctx,
59 fsp,
60 NULL,
61 &cmpr_state.format);
62 if (!NT_STATUS_IS_OK(status)) {
63 return status;
66 ndr_ret = ndr_push_struct_blob(&output, mem_ctx,
67 &cmpr_state,
68 (ndr_push_flags_fn_t)ndr_push_compression_state);
69 if (ndr_ret != NDR_ERR_SUCCESS) {
70 return NT_STATUS_INTERNAL_ERROR;
73 if (in_max_output < output.length) {
74 DEBUG(1, ("max output %u too small for compression state %ld\n",
75 (unsigned int)in_max_output, (long int)output.length));
76 return NT_STATUS_INVALID_USER_BUFFER;
78 *out_output = output;
80 return NT_STATUS_OK;
83 static NTSTATUS fsctl_set_cmprn(TALLOC_CTX *mem_ctx,
84 struct tevent_context *ev,
85 struct files_struct *fsp,
86 DATA_BLOB *in_input)
88 struct compression_state cmpr_state;
89 enum ndr_err_code ndr_ret;
90 NTSTATUS status;
92 if (fsp == NULL) {
93 return NT_STATUS_FILE_CLOSED;
96 /* WRITE_DATA permission is required, WRITE_ATTRIBUTES is not */
97 status = check_access(fsp->conn, fsp, NULL,
98 FILE_WRITE_DATA);
99 if (!NT_STATUS_IS_OK(status)) {
100 return status;
103 if ((fsp->conn->fs_capabilities & FILE_FILE_COMPRESSION) == 0) {
104 DEBUG(4, ("FS does not advertise compression support\n"));
105 return NT_STATUS_NOT_SUPPORTED;
108 ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &cmpr_state,
109 (ndr_pull_flags_fn_t)ndr_pull_compression_state);
110 if (ndr_ret != NDR_ERR_SUCCESS) {
111 DEBUG(0, ("failed to unmarshall set compression req\n"));
112 return NT_STATUS_INVALID_PARAMETER;
115 status = SMB_VFS_SET_COMPRESSION(fsp->conn,
116 mem_ctx,
117 fsp,
118 cmpr_state.format);
119 if (!NT_STATUS_IS_OK(status)) {
120 return status;
123 return NT_STATUS_OK;
126 static NTSTATUS fsctl_zero_data(TALLOC_CTX *mem_ctx,
127 struct tevent_context *ev,
128 struct files_struct *fsp,
129 DATA_BLOB *in_input)
131 struct file_zero_data_info zdata_info;
132 enum ndr_err_code ndr_ret;
133 struct lock_struct lck;
134 int mode;
135 uint64_t len;
136 int ret;
137 NTSTATUS status;
139 if (fsp == NULL) {
140 return NT_STATUS_FILE_CLOSED;
143 /* WRITE_DATA permission is required */
144 status = check_access(fsp->conn, fsp, NULL, FILE_WRITE_DATA);
145 if (!NT_STATUS_IS_OK(status)) {
146 return status;
149 /* allow regardless of whether FS supports sparse or not */
151 ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &zdata_info,
152 (ndr_pull_flags_fn_t)ndr_pull_file_zero_data_info);
153 if (ndr_ret != NDR_ERR_SUCCESS) {
154 DEBUG(0, ("failed to unmarshall zero data request\n"));
155 return NT_STATUS_INVALID_PARAMETER;
158 if (zdata_info.beyond_final_zero < zdata_info.file_off) {
159 DEBUG(0, ("invalid zero data params: off %lu, bfz, %lu\n",
160 (unsigned long)zdata_info.file_off,
161 (unsigned long)zdata_info.beyond_final_zero));
162 return NT_STATUS_INVALID_PARAMETER;
165 /* convert strange "beyond final zero" param into length */
166 len = zdata_info.beyond_final_zero - zdata_info.file_off;
168 if (len == 0) {
169 DEBUG(2, ("zero data called with zero length range\n"));
170 return NT_STATUS_OK;
173 init_strict_lock_struct(fsp,
174 fsp->op->global->open_persistent_id,
175 zdata_info.file_off,
176 len,
177 WRITE_LOCK,
178 &lck);
180 if (!SMB_VFS_STRICT_LOCK(fsp->conn, fsp, &lck)) {
181 DEBUG(2, ("failed to lock range for zero-data\n"));
182 return NT_STATUS_FILE_LOCK_CONFLICT;
186 * MS-FSCC <58> Section 2.3.65
187 * This FSCTL sets the range of bytes to zero (0) without extending the
188 * file size.
190 * The VFS_FALLOCATE_FL_KEEP_SIZE flag is used to satisfy this
191 * constraint.
194 mode = VFS_FALLOCATE_FL_PUNCH_HOLE | VFS_FALLOCATE_FL_KEEP_SIZE;
195 ret = SMB_VFS_FALLOCATE(fsp, mode, zdata_info.file_off, len);
196 if (ret == -1) {
197 status = map_nt_error_from_unix_common(errno);
198 DEBUG(2, ("zero-data fallocate(0x%x) failed: %s\n", mode,
199 strerror(errno)));
200 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lck);
201 return status;
204 if (!fsp->is_sparse && lp_strict_allocate(SNUM(fsp->conn))) {
206 * File marked non-sparse and "strict allocate" is enabled -
207 * allocate the range that we just punched out.
208 * In future FALLOC_FL_ZERO_RANGE could be used exclusively for
209 * this, but it's currently only supported on XFS and ext4.
211 * The newly allocated range still won't be found by SEEK_DATA
212 * for QAR, but stat.st_blocks will reflect it.
214 ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_FL_KEEP_SIZE,
215 zdata_info.file_off, len);
216 if (ret == -1) {
217 status = map_nt_error_from_unix_common(errno);
218 DEBUG(0, ("fallocate failed: %s\n", strerror(errno)));
219 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lck);
220 return status;
224 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lck);
225 return NT_STATUS_OK;
228 struct tevent_req *smb2_ioctl_filesys(uint32_t ctl_code,
229 struct tevent_context *ev,
230 struct tevent_req *req,
231 struct smbd_smb2_ioctl_state *state)
233 NTSTATUS status;
235 switch (ctl_code) {
236 case FSCTL_GET_COMPRESSION:
237 status = fsctl_get_cmprn(state, ev, state->fsp,
238 state->in_max_output,
239 &state->out_output);
240 if (!tevent_req_nterror(req, status)) {
241 tevent_req_done(req);
243 return tevent_req_post(req, ev);
244 break;
245 case FSCTL_SET_COMPRESSION:
246 status = fsctl_set_cmprn(state, ev, state->fsp,
247 &state->in_input);
248 if (!tevent_req_nterror(req, status)) {
249 tevent_req_done(req);
251 return tevent_req_post(req, ev);
252 break;
253 case FSCTL_SET_ZERO_DATA:
254 status = fsctl_zero_data(state, ev, state->fsp,
255 &state->in_input);
256 if (!tevent_req_nterror(req, status)) {
257 tevent_req_done(req);
259 return tevent_req_post(req, ev);
260 break;
261 default: {
262 uint8_t *out_data = NULL;
263 uint32_t out_data_len = 0;
265 if (state->fsp == NULL) {
266 status = NT_STATUS_NOT_SUPPORTED;
267 } else {
268 status = SMB_VFS_FSCTL(state->fsp,
269 state,
270 ctl_code,
271 state->smbreq->flags2,
272 state->in_input.data,
273 state->in_input.length,
274 &out_data,
275 state->in_max_output,
276 &out_data_len);
277 state->out_output = data_blob_const(out_data, out_data_len);
278 if (NT_STATUS_IS_OK(status)) {
279 tevent_req_done(req);
280 return tevent_req_post(req, ev);
284 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
285 if (IS_IPC(state->smbreq->conn)) {
286 status = NT_STATUS_FS_DRIVER_REQUIRED;
287 } else {
288 status = NT_STATUS_INVALID_DEVICE_REQUEST;
292 tevent_req_nterror(req, status);
293 return tevent_req_post(req, ev);
294 break;
298 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
299 return tevent_req_post(req, ev);