smbd/smb2_ioctl: instruct VFS to ignore locks for dup extents
[kamenim/samba-autobuild/.git] / source3 / smbd / smb2_ioctl_filesys.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) David Disseldorp 2013-2015
7
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.
12
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.
17
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/>.
20 */
21
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"
33
34 /*
35  * XXX this may reduce dup_extents->byte_count so that it's less than the
36  * target file size.
37  */
38 static NTSTATUS fsctl_dup_extents_check_lengths(struct files_struct *src_fsp,
39                                                 struct files_struct *dst_fsp,
40                                 struct fsctl_dup_extents_to_file *dup_extents)
41 {
42         NTSTATUS status;
43
44         if ((dup_extents->source_off + dup_extents->byte_count
45                                                 < dup_extents->source_off)
46          || (dup_extents->target_off + dup_extents->byte_count
47                                                 < dup_extents->target_off)) {
48                 return NT_STATUS_INVALID_PARAMETER;     /* wrap */
49         }
50
51         status = vfs_stat_fsp(src_fsp);
52         if (!NT_STATUS_IS_OK(status)) {
53                 return status;
54         }
55
56         /*
57          * XXX vfs_btrfs and vfs_default have size checks in the copychunk
58          * handler, as this needs to be rechecked after the src has potentially
59          * been extended by a previous chunk in the compound copychunk req.
60          */
61         if (src_fsp->fsp_name->st.st_ex_size
62                         < dup_extents->source_off + dup_extents->byte_count) {
63                 DEBUG(2, ("dup_extents req exceeds src size\n"));
64                 return NT_STATUS_NOT_SUPPORTED;
65         }
66
67         status = vfs_stat_fsp(dst_fsp);
68         if (!NT_STATUS_IS_OK(status)) {
69                 return status;
70         }
71
72         if (dst_fsp->fsp_name->st.st_ex_size
73                         < dup_extents->target_off + dup_extents->byte_count) {
74
75                 if (dst_fsp->fsp_name->st.st_ex_size - dup_extents->target_off
76                                         > dst_fsp->fsp_name->st.st_ex_size) {
77                         return NT_STATUS_INVALID_PARAMETER;     /* wrap */
78                 }
79
80                 /*
81                  * this server behaviour is pretty hairy, but we need to match
82                  * Windows, so...
83                  */
84                 DEBUG(2, ("dup_extents req exceeds target size, capping\n"));
85                 dup_extents->byte_count = dst_fsp->fsp_name->st.st_ex_size
86                                                 - dup_extents->target_off;
87         }
88
89         return NT_STATUS_OK;
90 }
91
92 static NTSTATUS fsctl_dup_extents_check_overlap(struct files_struct *src_fsp,
93                                                 struct files_struct *dst_fsp,
94                                 struct fsctl_dup_extents_to_file *dup_extents)
95 {
96         uint64_t src_off_last;
97         uint64_t tgt_off_last;
98
99         if (!file_id_equal(&src_fsp->file_id, &dst_fsp->file_id)) {
100                 /* src and dest refer to different files */
101                 return NT_STATUS_OK;
102         }
103
104         if (dup_extents->byte_count == 0) {
105                 /* no range to overlap */
106                 return NT_STATUS_OK;
107         }
108
109         /*
110          * [MS-FSCC] 2.3.8 FSCTL_DUPLICATE_EXTENTS_TO_FILE Reply
111          * STATUS_NOT_SUPPORTED:
112          * The source and target destination ranges overlap on the same file.
113          */
114
115         src_off_last = dup_extents->source_off + dup_extents->byte_count - 1;
116         if ((dup_extents->target_off >= dup_extents->source_off)
117                                 && (dup_extents->target_off <= src_off_last)) {
118                 /*
119                  * src: |-----------|
120                  * tgt:       |-----------|
121                  */
122                 return NT_STATUS_NOT_SUPPORTED;
123         }
124
125
126         tgt_off_last = dup_extents->target_off + dup_extents->byte_count - 1;
127         if ((tgt_off_last >= dup_extents->source_off)
128                                         && (tgt_off_last <= src_off_last)) {
129                 /*
130                  * src:       |-----------|
131                  * tgt: |-----------|
132                  */
133                 return NT_STATUS_NOT_SUPPORTED;
134         }
135
136         return NT_STATUS_OK;
137 }
138
139 static NTSTATUS fsctl_dup_extents_check_sparse(struct files_struct *src_fsp,
140                                                struct files_struct *dst_fsp)
141 {
142         /*
143          * 2.3.8 FSCTL_DUPLICATE_EXTENTS_TO_FILE Reply...
144          * STATUS_NOT_SUPPORTED: Target file is sparse, while source
145          *                       is a non-sparse file.
146          *
147          * WS2016 has the following behaviour (MS are in the process of fixing
148          * the spec):
149          * STATUS_NOT_SUPPORTED is returned if the source is sparse, while the
150          * target is non-sparse. However, if target is sparse while the source
151          * is non-sparse, then FSCTL_DUPLICATE_EXTENTS_TO_FILE completes
152          * successfully.
153          */
154         if ((src_fsp->is_sparse) && (!dst_fsp->is_sparse)) {
155                 return NT_STATUS_NOT_SUPPORTED;
156         }
157
158         return NT_STATUS_OK;
159 }
160
161 struct fsctl_dup_extents_state {
162         struct tevent_context *ev;
163         struct connection_struct *conn;
164         struct fsctl_dup_extents_to_file dup_extents;
165 };
166
167 static void fsctl_dup_extents_vfs_done(struct tevent_req *subreq);
168
169 static struct tevent_req *fsctl_dup_extents_send(TALLOC_CTX *mem_ctx,
170                                                  struct tevent_context *ev,
171                                                  struct files_struct *dst_fsp,
172                                                  DATA_BLOB *in_input,
173                                                  struct smbd_smb2_request *smb2req)
174 {
175         struct tevent_req *req = NULL;
176         struct tevent_req *subreq = NULL;
177         struct fsctl_dup_extents_state *state = NULL;
178         uint64_t src_fid_persistent = 0;
179         uint64_t src_fid_volatile = 0;
180         struct files_struct *src_fsp = NULL;
181         int ndr_ret;
182         NTSTATUS status;
183
184         req = tevent_req_create(mem_ctx, &state,
185                                 struct fsctl_dup_extents_state);
186         if (req == NULL) {
187                 return NULL;
188         }
189         *state = (struct fsctl_dup_extents_state) {
190                 .conn = dst_fsp->conn,
191                 .ev = ev,
192         };
193
194         if (dst_fsp == NULL) {
195                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
196                 return tevent_req_post(req, ev);
197         }
198
199         if ((dst_fsp->conn->fs_capabilities
200                                 & FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0) {
201                 DBG_INFO("FS does not advertise block refcounting support\n");
202                 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
203                 return tevent_req_post(req, ev);
204         }
205
206         ndr_ret = ndr_pull_struct_blob(in_input, state, &state->dup_extents,
207                        (ndr_pull_flags_fn_t)ndr_pull_fsctl_dup_extents_to_file);
208         if (ndr_ret != NDR_ERR_SUCCESS) {
209                 DBG_ERR("failed to unmarshall dup extents to file req\n");
210                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
211                 return tevent_req_post(req, ev);
212         }
213
214         src_fid_persistent = BVAL(state->dup_extents.source_fid, 0);
215         src_fid_volatile = BVAL(state->dup_extents.source_fid, 8);
216         src_fsp = file_fsp_get(smb2req, src_fid_persistent, src_fid_volatile);
217         if ((src_fsp == NULL)
218                       || (src_fsp->file_id.devid != dst_fsp->file_id.devid)) {
219                 /*
220                  * [MS-FSCC] 2.3.8 FSCTL_DUPLICATE_EXTENTS_TO_FILE Reply
221                  * STATUS_INVALID_PARAMETER:
222                  * The FileHandle parameter is either invalid or does not
223                  * represent a handle to an opened file on the same volume.
224                  *
225                  * Windows Server responds with NT_STATUS_INVALID_HANDLE instead
226                  * of STATUS_INVALID_PARAMETER here, despite the above spec.
227                  */
228                 DBG_ERR("invalid src_fsp for dup_extents\n");
229                 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
230                 return tevent_req_post(req, ev);
231         }
232
233         status = fsctl_dup_extents_check_lengths(src_fsp, dst_fsp,
234                                                  &state->dup_extents);
235         if (!NT_STATUS_IS_OK(status)) {
236                 tevent_req_nterror(req, status);
237                 return tevent_req_post(req, ev);
238         }
239
240         if (state->dup_extents.byte_count == 0) {
241                 DBG_ERR("skipping zero length dup extents\n");
242                 tevent_req_done(req);
243                 return tevent_req_post(req, ev);
244         }
245
246         status = fsctl_dup_extents_check_overlap(src_fsp, dst_fsp,
247                                                  &state->dup_extents);
248         if (!NT_STATUS_IS_OK(status)) {
249                 tevent_req_nterror(req, status);
250                 return tevent_req_post(req, ev);
251         }
252
253         status = fsctl_dup_extents_check_sparse(src_fsp, dst_fsp);
254         if (!NT_STATUS_IS_OK(status)) {
255                 tevent_req_nterror(req, status);
256                 return tevent_req_post(req, ev);
257         }
258
259         /* tell the VFS to ignore locks across the clone, matching ReFS */
260         subreq = SMB_VFS_COPY_CHUNK_SEND(dst_fsp->conn, state, ev,
261                                          src_fsp, state->dup_extents.source_off,
262                                          dst_fsp, state->dup_extents.target_off,
263                                          state->dup_extents.byte_count,
264                                          VFS_COPY_CHUNK_FL_MUST_CLONE
265                                          | VFS_COPY_CHUNK_FL_IGNORE_LOCKS);
266         if (tevent_req_nomem(subreq, req)) {
267                 return tevent_req_post(req, ev);
268         }
269
270         tevent_req_set_callback(subreq, fsctl_dup_extents_vfs_done, req);
271
272         return subreq;
273 }
274
275 static void fsctl_dup_extents_vfs_done(struct tevent_req *subreq)
276 {
277         struct tevent_req *req = tevent_req_callback_data(
278                 subreq, struct tevent_req);
279         struct fsctl_dup_extents_state *state = tevent_req_data(
280                 req, struct fsctl_dup_extents_state);
281         off_t nb_chunk;
282         NTSTATUS status;
283
284         status = SMB_VFS_COPY_CHUNK_RECV(state->conn, subreq, &nb_chunk);
285         TALLOC_FREE(subreq);
286         if (tevent_req_nterror(req, status)) {
287                 return;
288         }
289
290         if (nb_chunk != state->dup_extents.byte_count) {
291                 tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
292                 return;
293         }
294
295         tevent_req_done(req);
296 }
297
298 static NTSTATUS fsctl_dup_extents_recv(struct tevent_req *req)
299 {
300         return tevent_req_simple_recv_ntstatus(req);
301 }
302
303 static NTSTATUS fsctl_get_cmprn(TALLOC_CTX *mem_ctx,
304                                 struct tevent_context *ev,
305                                 struct files_struct *fsp,
306                                 size_t in_max_output,
307                                 DATA_BLOB *out_output)
308 {
309         struct compression_state cmpr_state;
310         enum ndr_err_code ndr_ret;
311         DATA_BLOB output;
312         NTSTATUS status;
313
314         if (fsp == NULL) {
315                 return NT_STATUS_FILE_CLOSED;
316         }
317
318         /* Windows doesn't check for SEC_FILE_READ_ATTRIBUTE permission here */
319
320         ZERO_STRUCT(cmpr_state);
321         if (fsp->conn->fs_capabilities & FILE_FILE_COMPRESSION) {
322                 status = SMB_VFS_GET_COMPRESSION(fsp->conn,
323                                                  mem_ctx,
324                                                  fsp,
325                                                  NULL,
326                                                  &cmpr_state.format);
327                 if (!NT_STATUS_IS_OK(status)) {
328                         return status;
329                 }
330         } else {
331                 /*
332                  * bso#12144: The underlying filesystem doesn't support
333                  * compression, so we should respond with "not-compressed"
334                  * (like WS2016 ReFS) instead of STATUS_NOT_SUPPORTED or
335                  * NT_STATUS_INVALID_DEVICE_REQUEST.
336                  */
337                 cmpr_state.format = COMPRESSION_FORMAT_NONE;
338         }
339
340         ndr_ret = ndr_push_struct_blob(&output, mem_ctx,
341                                        &cmpr_state,
342                         (ndr_push_flags_fn_t)ndr_push_compression_state);
343         if (ndr_ret != NDR_ERR_SUCCESS) {
344                 return NT_STATUS_INTERNAL_ERROR;
345         }
346
347         if (in_max_output < output.length) {
348                 DEBUG(1, ("max output %u too small for compression state %ld\n",
349                       (unsigned int)in_max_output, (long int)output.length));
350                 return NT_STATUS_INVALID_USER_BUFFER;
351         }
352         *out_output = output;
353
354         return NT_STATUS_OK;
355 }
356
357 static NTSTATUS fsctl_set_cmprn(TALLOC_CTX *mem_ctx,
358                                 struct tevent_context *ev,
359                                 struct files_struct *fsp,
360                                 DATA_BLOB *in_input)
361 {
362         struct compression_state cmpr_state;
363         enum ndr_err_code ndr_ret;
364         NTSTATUS status;
365
366         if (fsp == NULL) {
367                 return NT_STATUS_FILE_CLOSED;
368         }
369
370         /* WRITE_DATA permission is required, WRITE_ATTRIBUTES is not */
371         status = check_access_fsp(fsp, FILE_WRITE_DATA);
372         if (!NT_STATUS_IS_OK(status)) {
373                 return status;
374         }
375
376         ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &cmpr_state,
377                         (ndr_pull_flags_fn_t)ndr_pull_compression_state);
378         if (ndr_ret != NDR_ERR_SUCCESS) {
379                 DEBUG(0, ("failed to unmarshall set compression req\n"));
380                 return NT_STATUS_INVALID_PARAMETER;
381         }
382
383         status = NT_STATUS_NOT_SUPPORTED;
384         if (fsp->conn->fs_capabilities & FILE_FILE_COMPRESSION) {
385                 status = SMB_VFS_SET_COMPRESSION(fsp->conn,
386                                                  mem_ctx,
387                                                  fsp,
388                                                  cmpr_state.format);
389         } else if (cmpr_state.format == COMPRESSION_FORMAT_NONE) {
390                 /*
391                  * bso#12144: The underlying filesystem doesn't support
392                  * compression. We should still accept set(FORMAT_NONE) requests
393                  * (like WS2016 ReFS).
394                  */
395                 status = NT_STATUS_OK;
396         }
397
398         return status;
399 }
400
401 static NTSTATUS fsctl_zero_data(TALLOC_CTX *mem_ctx,
402                                 struct tevent_context *ev,
403                                 struct files_struct *fsp,
404                                 DATA_BLOB *in_input)
405 {
406         struct file_zero_data_info zdata_info;
407         enum ndr_err_code ndr_ret;
408         struct lock_struct lck;
409         int mode;
410         uint64_t len;
411         int ret;
412         NTSTATUS status;
413
414         if (fsp == NULL) {
415                 return NT_STATUS_FILE_CLOSED;
416         }
417
418         /* WRITE_DATA permission is required */
419         status = check_access_fsp(fsp, FILE_WRITE_DATA);
420         if (!NT_STATUS_IS_OK(status)) {
421                 return status;
422         }
423
424         /* allow regardless of whether FS supports sparse or not */
425
426         ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &zdata_info,
427                         (ndr_pull_flags_fn_t)ndr_pull_file_zero_data_info);
428         if (ndr_ret != NDR_ERR_SUCCESS) {
429                 DEBUG(0, ("failed to unmarshall zero data request\n"));
430                 return NT_STATUS_INVALID_PARAMETER;
431         }
432
433         if (zdata_info.beyond_final_zero < zdata_info.file_off) {
434                 DEBUG(0, ("invalid zero data params: off %lu, bfz, %lu\n",
435                           (unsigned long)zdata_info.file_off,
436                           (unsigned long)zdata_info.beyond_final_zero));
437                 return NT_STATUS_INVALID_PARAMETER;
438         }
439
440         /* convert strange "beyond final zero" param into length */
441         len = zdata_info.beyond_final_zero - zdata_info.file_off;
442
443         if (len == 0) {
444                 DEBUG(2, ("zero data called with zero length range\n"));
445                 return NT_STATUS_OK;
446         }
447
448         init_strict_lock_struct(fsp,
449                                 fsp->op->global->open_persistent_id,
450                                 zdata_info.file_off,
451                                 len,
452                                 WRITE_LOCK,
453                                 &lck);
454
455         if (!SMB_VFS_STRICT_LOCK(fsp->conn, fsp, &lck)) {
456                 DEBUG(2, ("failed to lock range for zero-data\n"));
457                 return NT_STATUS_FILE_LOCK_CONFLICT;
458         }
459
460         /*
461          * MS-FSCC <58> Section 2.3.67
462          * This FSCTL sets the range of bytes to zero (0) without extending the
463          * file size.
464          *
465          * The VFS_FALLOCATE_FL_KEEP_SIZE flag is used to satisfy this
466          * constraint.
467          */
468
469         mode = VFS_FALLOCATE_FL_PUNCH_HOLE | VFS_FALLOCATE_FL_KEEP_SIZE;
470         ret = SMB_VFS_FALLOCATE(fsp, mode, zdata_info.file_off, len);
471         if (ret == -1)  {
472                 status = map_nt_error_from_unix_common(errno);
473                 DEBUG(2, ("zero-data fallocate(0x%x) failed: %s\n", mode,
474                       strerror(errno)));
475                 SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lck);
476                 return status;
477         }
478
479         if (!fsp->is_sparse && lp_strict_allocate(SNUM(fsp->conn))) {
480                 /*
481                  * File marked non-sparse and "strict allocate" is enabled -
482                  * allocate the range that we just punched out.
483                  * In future FALLOC_FL_ZERO_RANGE could be used exclusively for
484                  * this, but it's currently only supported on XFS and ext4.
485                  *
486                  * The newly allocated range still won't be found by SEEK_DATA
487                  * for QAR, but stat.st_blocks will reflect it.
488                  */
489                 ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_FL_KEEP_SIZE,
490                                         zdata_info.file_off, len);
491                 if (ret == -1)  {
492                         status = map_nt_error_from_unix_common(errno);
493                         DEBUG(0, ("fallocate failed: %s\n", strerror(errno)));
494                         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lck);
495                         return status;
496                 }
497         }
498
499         SMB_VFS_STRICT_UNLOCK(fsp->conn, fsp, &lck);
500         return NT_STATUS_OK;
501 }
502
503 static NTSTATUS fsctl_qar_buf_push(TALLOC_CTX *mem_ctx,
504                                    struct file_alloced_range_buf *qar_buf,
505                                    DATA_BLOB *qar_array_blob)
506 {
507         DATA_BLOB new_slot;
508         enum ndr_err_code ndr_ret;
509         bool ok;
510
511         ndr_ret = ndr_push_struct_blob(&new_slot, mem_ctx, qar_buf,
512                         (ndr_push_flags_fn_t)ndr_push_file_alloced_range_buf);
513         if (ndr_ret != NDR_ERR_SUCCESS) {
514                 DEBUG(0, ("failed to marshall QAR buf\n"));
515                 return NT_STATUS_INVALID_PARAMETER;
516         }
517
518         /* TODO should be able to avoid copy by pushing into prealloced buf */
519         ok = data_blob_append(mem_ctx, qar_array_blob, new_slot.data,
520                               new_slot.length);
521         data_blob_free(&new_slot);
522         if (!ok) {
523                 return NT_STATUS_NO_MEMORY;
524         }
525
526         return NT_STATUS_OK;
527 }
528
529 static NTSTATUS fsctl_qar_seek_fill(TALLOC_CTX *mem_ctx,
530                                     struct files_struct *fsp,
531                                     off_t curr_off,
532                                     off_t max_off,
533                                     DATA_BLOB *qar_array_blob)
534 {
535         NTSTATUS status = NT_STATUS_NOT_SUPPORTED;
536
537 #ifdef HAVE_LSEEK_HOLE_DATA
538         while (curr_off <= max_off) {
539                 off_t data_off;
540                 off_t hole_off;
541                 struct file_alloced_range_buf qar_buf;
542
543                 /* seek next data */
544                 data_off = SMB_VFS_LSEEK(fsp, curr_off, SEEK_DATA);
545                 if ((data_off == -1) && (errno == ENXIO)) {
546                         /* no data from curr_off to EOF */
547                         break;
548                 } else if (data_off == -1) {
549                         status = map_nt_error_from_unix_common(errno);
550                         DEBUG(1, ("lseek data failed: %s\n", strerror(errno)));
551                         return status;
552                 }
553
554                 if (data_off > max_off) {
555                         /* found something, but passed range of interest */
556                         break;
557                 }
558
559                 hole_off = SMB_VFS_LSEEK(fsp, data_off, SEEK_HOLE);
560                 if (hole_off == -1) {
561                         status = map_nt_error_from_unix_common(errno);
562                         DEBUG(1, ("lseek hole failed: %s\n", strerror(errno)));
563                         return status;
564                 }
565
566                 if (hole_off <= data_off) {
567                         DEBUG(1, ("lseek inconsistent: hole %lu at or before "
568                                   "data %lu\n", (unsigned long)hole_off,
569                                   (unsigned long)data_off));
570                         return NT_STATUS_INTERNAL_ERROR;
571                 }
572
573                 qar_buf.file_off = data_off;
574                 /* + 1 to convert maximum offset to length */
575                 qar_buf.len = MIN(hole_off, max_off + 1) - data_off;
576
577                 status = fsctl_qar_buf_push(mem_ctx, &qar_buf, qar_array_blob);
578                 if (!NT_STATUS_IS_OK(status)) {
579                         return NT_STATUS_NO_MEMORY;
580                 }
581
582                 curr_off = hole_off;
583         }
584         status = NT_STATUS_OK;
585 #endif
586
587         return status;
588 }
589
590 static NTSTATUS fsctl_qar(TALLOC_CTX *mem_ctx,
591                           struct tevent_context *ev,
592                           struct files_struct *fsp,
593                           DATA_BLOB *in_input,
594                           size_t in_max_output,
595                           DATA_BLOB *out_output)
596 {
597         struct fsctl_query_alloced_ranges_req qar_req;
598         struct fsctl_query_alloced_ranges_rsp qar_rsp;
599         DATA_BLOB qar_array_blob = data_blob_null;
600         uint64_t max_off;
601         enum ndr_err_code ndr_ret;
602         int ret;
603         NTSTATUS status;
604         SMB_STRUCT_STAT sbuf;
605
606         if (fsp == NULL) {
607                 return NT_STATUS_FILE_CLOSED;
608         }
609
610         /* READ_DATA permission is required */
611         status = check_access_fsp(fsp, FILE_READ_DATA);
612         if (!NT_STATUS_IS_OK(status)) {
613                 return status;
614         }
615
616         ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &qar_req,
617                 (ndr_pull_flags_fn_t)ndr_pull_fsctl_query_alloced_ranges_req);
618         if (ndr_ret != NDR_ERR_SUCCESS) {
619                 DEBUG(0, ("failed to unmarshall QAR req\n"));
620                 return NT_STATUS_INVALID_PARAMETER;
621         }
622
623         /*
624          * XXX Windows Server 2008 & 2012 servers don't return lock-conflict
625          * for QAR requests over an exclusively locked range!
626          */
627
628         ret = SMB_VFS_FSTAT(fsp, &sbuf);
629         if (ret == -1) {
630                 status = map_nt_error_from_unix_common(errno);
631                 DEBUG(2, ("fstat failed: %s\n", strerror(errno)));
632                 return status;
633         }
634
635         if ((qar_req.buf.len == 0)
636          || (sbuf.st_ex_size == 0)
637          || (qar_req.buf.file_off >= sbuf.st_ex_size)) {
638                 /* zero length range or after EOF, no ranges to return */
639                 return NT_STATUS_OK;
640         }
641
642         /* check for integer overflow */
643         if (qar_req.buf.file_off + qar_req.buf.len < qar_req.buf.file_off) {
644                 return NT_STATUS_INVALID_PARAMETER;
645         }
646
647         /*
648          * Maximum offset is either the last valid offset _before_ EOF, or the
649          * last byte offset within the requested range. -1 converts length to
650          * offset, which is easier to work with for SEEK_DATA/SEEK_HOLE, E.g.:
651          *
652          * /off=0             /off=512K          /st_ex_size=1M
653          * |-------------------------------------|
654          * | File data                           |
655          * |-------------------------------------|
656          *                                                   QAR end\
657          *                    |=====================================|
658          *                    |    QAR off=512K, len=1M             |
659          *                    |=================^===================|
660          *                                   max_off=1M - 1
661          *             QAR end\
662          * |==================|
663          * |QAR off=0 len=512K|
664          * |==================|
665          *                   ^
666          *                max_off=512K - 1
667          */
668         max_off = MIN(sbuf.st_ex_size,
669                       qar_req.buf.file_off + qar_req.buf.len) - 1;
670
671         if (!fsp->is_sparse) {
672                 struct file_alloced_range_buf qar_buf;
673
674                 /* file is non-sparse, claim file_off->max_off is allocated */
675                 qar_buf.file_off = qar_req.buf.file_off;
676                 /* + 1 to convert maximum offset back to length */
677                 qar_buf.len = max_off - qar_req.buf.file_off + 1;
678
679                 status = fsctl_qar_buf_push(mem_ctx, &qar_buf, &qar_array_blob);
680         } else {
681                 status = fsctl_qar_seek_fill(mem_ctx, fsp, qar_req.buf.file_off,
682                                              max_off, &qar_array_blob);
683         }
684         if (!NT_STATUS_IS_OK(status)) {
685                 return status;
686         }
687
688         /* marshall response buffer. */
689         qar_rsp.far_buf_array = qar_array_blob;
690
691         ndr_ret = ndr_push_struct_blob(out_output, mem_ctx, &qar_rsp,
692                 (ndr_push_flags_fn_t)ndr_push_fsctl_query_alloced_ranges_rsp);
693         if (ndr_ret != NDR_ERR_SUCCESS) {
694                 DEBUG(0, ("failed to marshall QAR rsp\n"));
695                 return NT_STATUS_INVALID_PARAMETER;
696         }
697
698         if (out_output->length > in_max_output) {
699                 DEBUG(2, ("QAR output len %lu exceeds max %lu\n",
700                           (unsigned long)out_output->length,
701                           (unsigned long)in_max_output));
702                 data_blob_free(out_output);
703                 return NT_STATUS_BUFFER_TOO_SMALL;
704         }
705
706         return NT_STATUS_OK;
707 }
708
709 static void smb2_ioctl_filesys_dup_extents_done(struct tevent_req *subreq);
710
711 struct tevent_req *smb2_ioctl_filesys(uint32_t ctl_code,
712                                       struct tevent_context *ev,
713                                       struct tevent_req *req,
714                                       struct smbd_smb2_ioctl_state *state)
715 {
716         NTSTATUS status;
717
718         switch (ctl_code) {
719         case FSCTL_GET_COMPRESSION:
720                 status = fsctl_get_cmprn(state, ev, state->fsp,
721                                          state->in_max_output,
722                                          &state->out_output);
723                 if (!tevent_req_nterror(req, status)) {
724                         tevent_req_done(req);
725                 }
726                 return tevent_req_post(req, ev);
727                 break;
728         case FSCTL_SET_COMPRESSION:
729                 status = fsctl_set_cmprn(state, ev, state->fsp,
730                                          &state->in_input);
731                 if (!tevent_req_nterror(req, status)) {
732                         tevent_req_done(req);
733                 }
734                 return tevent_req_post(req, ev);
735                 break;
736         case FSCTL_SET_ZERO_DATA:
737                 status = fsctl_zero_data(state, ev, state->fsp,
738                                          &state->in_input);
739                 if (!tevent_req_nterror(req, status)) {
740                         tevent_req_done(req);
741                 }
742                 return tevent_req_post(req, ev);
743                 break;
744         case FSCTL_QUERY_ALLOCATED_RANGES:
745                 status = fsctl_qar(state, ev, state->fsp,
746                                    &state->in_input,
747                                    state->in_max_output,
748                                    &state->out_output);
749                 if (!tevent_req_nterror(req, status)) {
750                         tevent_req_done(req);
751                 }
752                 return tevent_req_post(req, ev);
753                 break;
754         case FSCTL_DUP_EXTENTS_TO_FILE: {
755                 struct tevent_req *subreq = NULL;
756
757                 subreq = fsctl_dup_extents_send(state, ev,
758                                                 state->fsp,
759                                                 &state->in_input,
760                                                 state->smb2req);
761                 if (tevent_req_nomem(subreq, req)) {
762                         return tevent_req_post(req, ev);
763                 }
764                 tevent_req_set_callback(subreq,
765                                         smb2_ioctl_filesys_dup_extents_done,
766                                         req);
767                 return req;
768                 break;
769         }
770         default: {
771                 uint8_t *out_data = NULL;
772                 uint32_t out_data_len = 0;
773
774                 if (state->fsp == NULL) {
775                         status = NT_STATUS_NOT_SUPPORTED;
776                 } else {
777                         status = SMB_VFS_FSCTL(state->fsp,
778                                                state,
779                                                ctl_code,
780                                                state->smbreq->flags2,
781                                                state->in_input.data,
782                                                state->in_input.length,
783                                                &out_data,
784                                                state->in_max_output,
785                                                &out_data_len);
786                         state->out_output = data_blob_const(out_data, out_data_len);
787                         if (NT_STATUS_IS_OK(status)) {
788                                 tevent_req_done(req);
789                                 return tevent_req_post(req, ev);
790                         }
791                 }
792
793                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
794                         if (IS_IPC(state->smbreq->conn)) {
795                                 status = NT_STATUS_FS_DRIVER_REQUIRED;
796                         } else {
797                                 status = NT_STATUS_INVALID_DEVICE_REQUEST;
798                         }
799                 }
800
801                 tevent_req_nterror(req, status);
802                 return tevent_req_post(req, ev);
803                 break;
804         }
805         }
806
807         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
808         return tevent_req_post(req, ev);
809 }
810
811 static void smb2_ioctl_filesys_dup_extents_done(struct tevent_req *subreq)
812 {
813         struct tevent_req *req = tevent_req_callback_data(subreq,
814                                                           struct tevent_req);
815         NTSTATUS status;
816
817         status = fsctl_dup_extents_recv(subreq);
818         TALLOC_FREE(subreq);
819         if (!tevent_req_nterror(req, status)) {
820                 tevent_req_done(req);
821         }
822 }