2 Unix SMB/CIFS implementation.
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/>.
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
,
38 DATA_BLOB
*out_output
)
40 struct compression_state cmpr_state
;
41 enum ndr_err_code ndr_ret
;
46 return NT_STATUS_FILE_CLOSED
;
49 /* Windows doesn't check for SEC_FILE_READ_ATTRIBUTE permission here */
51 ZERO_STRUCT(cmpr_state
);
52 if (fsp
->conn
->fs_capabilities
& FILE_FILE_COMPRESSION
) {
53 status
= SMB_VFS_GET_COMPRESSION(fsp
->conn
,
58 if (!NT_STATUS_IS_OK(status
)) {
63 * bso#12144: The underlying filesystem doesn't support
64 * compression, so we should respond with "not-compressed"
65 * (like WS2016 ReFS) instead of STATUS_NOT_SUPPORTED or
66 * NT_STATUS_INVALID_DEVICE_REQUEST.
68 cmpr_state
.format
= COMPRESSION_FORMAT_NONE
;
71 ndr_ret
= ndr_push_struct_blob(&output
, mem_ctx
,
73 (ndr_push_flags_fn_t
)ndr_push_compression_state
);
74 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
75 return NT_STATUS_INTERNAL_ERROR
;
78 if (in_max_output
< output
.length
) {
79 DEBUG(1, ("max output %u too small for compression state %ld\n",
80 (unsigned int)in_max_output
, (long int)output
.length
));
81 return NT_STATUS_INVALID_USER_BUFFER
;
88 static NTSTATUS
fsctl_set_cmprn(TALLOC_CTX
*mem_ctx
,
89 struct tevent_context
*ev
,
90 struct files_struct
*fsp
,
93 struct compression_state cmpr_state
;
94 enum ndr_err_code ndr_ret
;
98 return NT_STATUS_FILE_CLOSED
;
101 /* WRITE_DATA permission is required, WRITE_ATTRIBUTES is not */
102 status
= check_access_fsp(fsp
, FILE_WRITE_DATA
);
103 if (!NT_STATUS_IS_OK(status
)) {
107 ndr_ret
= ndr_pull_struct_blob(in_input
, mem_ctx
, &cmpr_state
,
108 (ndr_pull_flags_fn_t
)ndr_pull_compression_state
);
109 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
110 DEBUG(0, ("failed to unmarshall set compression req\n"));
111 return NT_STATUS_INVALID_PARAMETER
;
114 status
= NT_STATUS_NOT_SUPPORTED
;
115 if (fsp
->conn
->fs_capabilities
& FILE_FILE_COMPRESSION
) {
116 status
= SMB_VFS_SET_COMPRESSION(fsp
->conn
,
120 } else if (cmpr_state
.format
== COMPRESSION_FORMAT_NONE
) {
122 * bso#12144: The underlying filesystem doesn't support
123 * compression. We should still accept set(FORMAT_NONE) requests
124 * (like WS2016 ReFS).
126 status
= NT_STATUS_OK
;
132 static NTSTATUS
fsctl_zero_data(TALLOC_CTX
*mem_ctx
,
133 struct tevent_context
*ev
,
134 struct files_struct
*fsp
,
137 struct file_zero_data_info zdata_info
;
138 enum ndr_err_code ndr_ret
;
139 struct lock_struct lck
;
146 return NT_STATUS_FILE_CLOSED
;
149 /* WRITE_DATA permission is required */
150 status
= check_access_fsp(fsp
, FILE_WRITE_DATA
);
151 if (!NT_STATUS_IS_OK(status
)) {
155 /* allow regardless of whether FS supports sparse or not */
157 ndr_ret
= ndr_pull_struct_blob(in_input
, mem_ctx
, &zdata_info
,
158 (ndr_pull_flags_fn_t
)ndr_pull_file_zero_data_info
);
159 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
160 DEBUG(0, ("failed to unmarshall zero data request\n"));
161 return NT_STATUS_INVALID_PARAMETER
;
164 if (zdata_info
.beyond_final_zero
< zdata_info
.file_off
) {
165 DEBUG(0, ("invalid zero data params: off %lu, bfz, %lu\n",
166 (unsigned long)zdata_info
.file_off
,
167 (unsigned long)zdata_info
.beyond_final_zero
));
168 return NT_STATUS_INVALID_PARAMETER
;
171 /* convert strange "beyond final zero" param into length */
172 len
= zdata_info
.beyond_final_zero
- zdata_info
.file_off
;
175 DEBUG(2, ("zero data called with zero length range\n"));
179 init_strict_lock_struct(fsp
,
180 fsp
->op
->global
->open_persistent_id
,
186 if (!SMB_VFS_STRICT_LOCK(fsp
->conn
, fsp
, &lck
)) {
187 DEBUG(2, ("failed to lock range for zero-data\n"));
188 return NT_STATUS_FILE_LOCK_CONFLICT
;
192 * MS-FSCC <58> Section 2.3.67
193 * This FSCTL sets the range of bytes to zero (0) without extending the
196 * The VFS_FALLOCATE_FL_KEEP_SIZE flag is used to satisfy this
200 mode
= VFS_FALLOCATE_FL_PUNCH_HOLE
| VFS_FALLOCATE_FL_KEEP_SIZE
;
201 ret
= SMB_VFS_FALLOCATE(fsp
, mode
, zdata_info
.file_off
, len
);
203 status
= map_nt_error_from_unix_common(errno
);
204 DEBUG(2, ("zero-data fallocate(0x%x) failed: %s\n", mode
,
206 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lck
);
210 if (!fsp
->is_sparse
&& lp_strict_allocate(SNUM(fsp
->conn
))) {
212 * File marked non-sparse and "strict allocate" is enabled -
213 * allocate the range that we just punched out.
214 * In future FALLOC_FL_ZERO_RANGE could be used exclusively for
215 * this, but it's currently only supported on XFS and ext4.
217 * The newly allocated range still won't be found by SEEK_DATA
218 * for QAR, but stat.st_blocks will reflect it.
220 ret
= SMB_VFS_FALLOCATE(fsp
, VFS_FALLOCATE_FL_KEEP_SIZE
,
221 zdata_info
.file_off
, len
);
223 status
= map_nt_error_from_unix_common(errno
);
224 DEBUG(0, ("fallocate failed: %s\n", strerror(errno
)));
225 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lck
);
230 SMB_VFS_STRICT_UNLOCK(fsp
->conn
, fsp
, &lck
);
234 static NTSTATUS
fsctl_qar_buf_push(TALLOC_CTX
*mem_ctx
,
235 struct file_alloced_range_buf
*qar_buf
,
236 DATA_BLOB
*qar_array_blob
)
239 enum ndr_err_code ndr_ret
;
242 ndr_ret
= ndr_push_struct_blob(&new_slot
, mem_ctx
, qar_buf
,
243 (ndr_push_flags_fn_t
)ndr_push_file_alloced_range_buf
);
244 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
245 DEBUG(0, ("failed to marshall QAR buf\n"));
246 return NT_STATUS_INVALID_PARAMETER
;
249 /* TODO should be able to avoid copy by pushing into prealloced buf */
250 ok
= data_blob_append(mem_ctx
, qar_array_blob
, new_slot
.data
,
252 data_blob_free(&new_slot
);
254 return NT_STATUS_NO_MEMORY
;
260 static NTSTATUS
fsctl_qar_seek_fill(TALLOC_CTX
*mem_ctx
,
261 struct files_struct
*fsp
,
264 DATA_BLOB
*qar_array_blob
)
266 NTSTATUS status
= NT_STATUS_NOT_SUPPORTED
;
268 #ifdef HAVE_LSEEK_HOLE_DATA
269 while (curr_off
<= max_off
) {
272 struct file_alloced_range_buf qar_buf
;
275 data_off
= SMB_VFS_LSEEK(fsp
, curr_off
, SEEK_DATA
);
276 if ((data_off
== -1) && (errno
== ENXIO
)) {
277 /* no data from curr_off to EOF */
279 } else if (data_off
== -1) {
280 status
= map_nt_error_from_unix_common(errno
);
281 DEBUG(1, ("lseek data failed: %s\n", strerror(errno
)));
285 if (data_off
> max_off
) {
286 /* found something, but passed range of interest */
290 hole_off
= SMB_VFS_LSEEK(fsp
, data_off
, SEEK_HOLE
);
291 if (hole_off
== -1) {
292 status
= map_nt_error_from_unix_common(errno
);
293 DEBUG(1, ("lseek hole failed: %s\n", strerror(errno
)));
297 if (hole_off
<= data_off
) {
298 DEBUG(1, ("lseek inconsistent: hole %lu at or before "
299 "data %lu\n", (unsigned long)hole_off
,
300 (unsigned long)data_off
));
301 return NT_STATUS_INTERNAL_ERROR
;
304 qar_buf
.file_off
= data_off
;
305 /* + 1 to convert maximum offset to length */
306 qar_buf
.len
= MIN(hole_off
, max_off
+ 1) - data_off
;
308 status
= fsctl_qar_buf_push(mem_ctx
, &qar_buf
, qar_array_blob
);
309 if (!NT_STATUS_IS_OK(status
)) {
310 return NT_STATUS_NO_MEMORY
;
315 status
= NT_STATUS_OK
;
321 static NTSTATUS
fsctl_qar(TALLOC_CTX
*mem_ctx
,
322 struct tevent_context
*ev
,
323 struct files_struct
*fsp
,
325 size_t in_max_output
,
326 DATA_BLOB
*out_output
)
328 struct fsctl_query_alloced_ranges_req qar_req
;
329 struct fsctl_query_alloced_ranges_rsp qar_rsp
;
330 DATA_BLOB qar_array_blob
= data_blob_null
;
332 enum ndr_err_code ndr_ret
;
335 SMB_STRUCT_STAT sbuf
;
338 return NT_STATUS_FILE_CLOSED
;
341 /* READ_DATA permission is required */
342 status
= check_access_fsp(fsp
, FILE_READ_DATA
);
343 if (!NT_STATUS_IS_OK(status
)) {
347 ndr_ret
= ndr_pull_struct_blob(in_input
, mem_ctx
, &qar_req
,
348 (ndr_pull_flags_fn_t
)ndr_pull_fsctl_query_alloced_ranges_req
);
349 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
350 DEBUG(0, ("failed to unmarshall QAR req\n"));
351 return NT_STATUS_INVALID_PARAMETER
;
355 * XXX Windows Server 2008 & 2012 servers don't return lock-conflict
356 * for QAR requests over an exclusively locked range!
359 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
361 status
= map_nt_error_from_unix_common(errno
);
362 DEBUG(2, ("fstat failed: %s\n", strerror(errno
)));
366 if ((qar_req
.buf
.len
== 0)
367 || (sbuf
.st_ex_size
== 0)
368 || (qar_req
.buf
.file_off
>= sbuf
.st_ex_size
)) {
369 /* zero length range or after EOF, no ranges to return */
373 /* check for integer overflow */
374 if (qar_req
.buf
.file_off
+ qar_req
.buf
.len
< qar_req
.buf
.file_off
) {
375 return NT_STATUS_INVALID_PARAMETER
;
379 * Maximum offset is either the last valid offset _before_ EOF, or the
380 * last byte offset within the requested range. -1 converts length to
381 * offset, which is easier to work with for SEEK_DATA/SEEK_HOLE, E.g.:
383 * /off=0 /off=512K /st_ex_size=1M
384 * |-------------------------------------|
386 * |-------------------------------------|
388 * |=====================================|
389 * | QAR off=512K, len=1M |
390 * |=================^===================|
393 * |==================|
394 * |QAR off=0 len=512K|
395 * |==================|
399 max_off
= MIN(sbuf
.st_ex_size
,
400 qar_req
.buf
.file_off
+ qar_req
.buf
.len
) - 1;
402 if (!fsp
->is_sparse
) {
403 struct file_alloced_range_buf qar_buf
;
405 /* file is non-sparse, claim file_off->max_off is allocated */
406 qar_buf
.file_off
= qar_req
.buf
.file_off
;
407 /* + 1 to convert maximum offset back to length */
408 qar_buf
.len
= max_off
- qar_req
.buf
.file_off
+ 1;
410 status
= fsctl_qar_buf_push(mem_ctx
, &qar_buf
, &qar_array_blob
);
412 status
= fsctl_qar_seek_fill(mem_ctx
, fsp
, qar_req
.buf
.file_off
,
413 max_off
, &qar_array_blob
);
415 if (!NT_STATUS_IS_OK(status
)) {
419 /* marshall response buffer. */
420 qar_rsp
.far_buf_array
= qar_array_blob
;
422 ndr_ret
= ndr_push_struct_blob(out_output
, mem_ctx
, &qar_rsp
,
423 (ndr_push_flags_fn_t
)ndr_push_fsctl_query_alloced_ranges_rsp
);
424 if (ndr_ret
!= NDR_ERR_SUCCESS
) {
425 DEBUG(0, ("failed to marshall QAR rsp\n"));
426 return NT_STATUS_INVALID_PARAMETER
;
429 if (out_output
->length
> in_max_output
) {
430 DEBUG(2, ("QAR output len %lu exceeds max %lu\n",
431 (unsigned long)out_output
->length
,
432 (unsigned long)in_max_output
));
433 data_blob_free(out_output
);
434 return NT_STATUS_BUFFER_TOO_SMALL
;
440 struct tevent_req
*smb2_ioctl_filesys(uint32_t ctl_code
,
441 struct tevent_context
*ev
,
442 struct tevent_req
*req
,
443 struct smbd_smb2_ioctl_state
*state
)
448 case FSCTL_GET_COMPRESSION
:
449 status
= fsctl_get_cmprn(state
, ev
, state
->fsp
,
450 state
->in_max_output
,
452 if (!tevent_req_nterror(req
, status
)) {
453 tevent_req_done(req
);
455 return tevent_req_post(req
, ev
);
457 case FSCTL_SET_COMPRESSION
:
458 status
= fsctl_set_cmprn(state
, ev
, state
->fsp
,
460 if (!tevent_req_nterror(req
, status
)) {
461 tevent_req_done(req
);
463 return tevent_req_post(req
, ev
);
465 case FSCTL_SET_ZERO_DATA
:
466 status
= fsctl_zero_data(state
, ev
, state
->fsp
,
468 if (!tevent_req_nterror(req
, status
)) {
469 tevent_req_done(req
);
471 return tevent_req_post(req
, ev
);
473 case FSCTL_QUERY_ALLOCATED_RANGES
:
474 status
= fsctl_qar(state
, ev
, state
->fsp
,
476 state
->in_max_output
,
478 if (!tevent_req_nterror(req
, status
)) {
479 tevent_req_done(req
);
481 return tevent_req_post(req
, ev
);
484 uint8_t *out_data
= NULL
;
485 uint32_t out_data_len
= 0;
487 if (state
->fsp
== NULL
) {
488 status
= NT_STATUS_NOT_SUPPORTED
;
490 status
= SMB_VFS_FSCTL(state
->fsp
,
493 state
->smbreq
->flags2
,
494 state
->in_input
.data
,
495 state
->in_input
.length
,
497 state
->in_max_output
,
499 state
->out_output
= data_blob_const(out_data
, out_data_len
);
500 if (NT_STATUS_IS_OK(status
)) {
501 tevent_req_done(req
);
502 return tevent_req_post(req
, ev
);
506 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
507 if (IS_IPC(state
->smbreq
->conn
)) {
508 status
= NT_STATUS_FS_DRIVER_REQUIRED
;
510 status
= NT_STATUS_INVALID_DEVICE_REQUEST
;
514 tevent_req_nterror(req
, status
);
515 return tevent_req_post(req
, ev
);
520 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
521 return tevent_req_post(req
, ev
);