1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2020 Tintri by DDN, Inc. All rights reserved.
*/
/*
* Support functions for smb2_ioctl/fsctl codes:
* FSCTL_SET_SPARSE
* FSCTL_SET_ZERO_DATA
* FSCTL_QUERY_ALLOCATED_RANGES
*/
#include <smbsrv/smb2_kproto.h>
#include <smbsrv/smb_fsops.h>
#include <smb/winioctl.h>
/*
* FSCTL_SET_SPARSE
*
* In args: one byte flag (optional: default TRUE)
*/
uint32_t
smb2_fsctl_set_sparse(smb_request_t *sr, smb_fsctl_t *fsctl)
{
smb_attr_t attr;
smb_ofile_t *ofile = sr->fid_ofile;
cred_t *kcr;
uint32_t amask;
uint32_t status;
uint8_t flag;
int rc;
rc = smb_mbc_decodef(fsctl->in_mbc, "b", &flag);
if (rc != 0)
flag = 0xff;
if (!smb_node_is_file(ofile->f_node))
return (NT_STATUS_INVALID_PARAMETER);
/*
* Allow if we have any of FILE_WRITE_ATTRIBUTES,
* FILE_WRITE_DATA, FILE_APPEND_DATA
*/
amask = FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | FILE_APPEND_DATA;
if ((ofile->f_granted_access & amask) == 0)
return (NT_STATUS_ACCESS_DENIED);
/*
* Need the current DOS attributes
*/
bzero(&attr, sizeof (attr));
attr.sa_mask = SMB_AT_DOSATTR;
kcr = zone_kcred();
status = smb_node_getattr(sr, ofile->f_node, kcr, ofile, &attr);
if (status != NT_STATUS_SUCCESS)
return (status);
if (flag != 0) {
/* Set "sparse" */
if (attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE)
return (0);
attr.sa_dosattr |= FILE_ATTRIBUTE_SPARSE_FILE;
} else {
/* Clear "sparse" */
if ((attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
return (0);
attr.sa_dosattr &= ~FILE_ATTRIBUTE_SPARSE_FILE;
}
attr.sa_mask = SMB_AT_DOSATTR;
status = smb_node_setattr(sr, ofile->f_node, kcr, ofile, &attr);
return (status);
}
/*
* FSCTL_SET_ZERO_DATA
*
* In args: uint64_t start_off, end_off
*/
uint32_t
smb2_fsctl_set_zero_data(smb_request_t *sr, smb_fsctl_t *fsctl)
{
smb_attr_t attr;
smb_ofile_t *ofile = sr->fid_ofile;
uint64_t start_off, end_off, zero_len;
uint32_t status;
int rc;
rc = smb_mbc_decodef(fsctl->in_mbc, "qq",
&start_off, &end_off);
if (rc != 0)
return (NT_STATUS_BUFFER_TOO_SMALL);
/*
* The given offsets are actually int64_t (signed).
*/
if (start_off > INT64_MAX ||
end_off > INT64_MAX ||
start_off > end_off)
return (NT_STATUS_INVALID_PARAMETER);
if (!smb_node_is_file(ofile->f_node))
return (NT_STATUS_INVALID_PARAMETER);
/*
* This operation is effectively a write (of zeros)
*/
status = smb_ofile_access(ofile, ofile->f_cr, FILE_WRITE_DATA);
if (status != NT_STATUS_SUCCESS)
return (status);
/*
* Need the file size
*/
bzero(&attr, sizeof (attr));
attr.sa_mask = SMB_AT_SIZE;
status = smb_node_getattr(sr, ofile->f_node, ofile->f_cr,
ofile, &attr);
if (status != NT_STATUS_SUCCESS)
return (status);
/*
* Ignore any zero-ing beyond EOF
*/
if (end_off > attr.sa_vattr.va_size)
end_off = attr.sa_vattr.va_size;
if (start_off >= end_off)
return (0);
zero_len = end_off - start_off;
/*
* Check for lock conflicting with the write.
*/
status = smb_lock_range_access(sr, ofile->f_node,
start_off, zero_len, B_TRUE);
if (status != 0)
return (status); /* == FILE_LOCK_CONFLICT */
rc = smb_fsop_freesp(sr, ofile->f_cr, ofile,
start_off, zero_len);
if (rc != 0)
status = smb_errno2status(rc);
return (status);
}
/*
* FSCTL_QUERY_ALLOCATED_RANGES
*
* Incoming args: uint64_t start_off, end_off
*/
struct alloc_range {
off64_t off;
off64_t len;
};
uint32_t
smb2_fsctl_query_alloc_ranges(smb_request_t *sr, smb_fsctl_t *fsctl)
{
smb_attr_t attr;
cred_t *kcr;
smb_ofile_t *ofile = sr->fid_ofile;
struct alloc_range arg, res;
off64_t cur_off, end_off;
uint32_t status;
int err, rc;
/*
* Most ioctls return NT_STATUS_BUFFER_TOO_SMALL for
* short in/out buffers, but for this one, MS-FSA
* says short input returns invalid parameter.
*/
rc = smb_mbc_decodef(fsctl->in_mbc, "qq", &arg.off, &arg.len);
if (rc != 0)
return (NT_STATUS_INVALID_PARAMETER);
/*
* The given offsets are actually int64_t (signed).
*/
end_off = arg.off + arg.len;
if (arg.off > INT64_MAX || arg.len < 0 ||
end_off > INT64_MAX || end_off < arg.off)
return (NT_STATUS_INVALID_PARAMETER);
if (!smb_node_is_file(ofile->f_node))
return (NT_STATUS_INVALID_PARAMETER);
/*
* This operation is effectively a read
*/
status = smb_ofile_access(ofile, ofile->f_cr, FILE_READ_DATA);
if (status != NT_STATUS_SUCCESS)
return (status);
if (arg.len == 0) {
/* MS-FSA says empty result for this. */
return (0);
}
/*
* Need the file size and dosattr
*/
bzero(&attr, sizeof (attr));
attr.sa_mask = SMB_AT_SIZE | SMB_AT_DOSATTR;
kcr = zone_kcred();
status = smb_node_getattr(sr, ofile->f_node, kcr, ofile, &attr);
if (status != NT_STATUS_SUCCESS)
return (status);
if (end_off > attr.sa_vattr.va_size)
end_off = attr.sa_vattr.va_size;
/*
* Only sparse files should present un-allocated ranges.
* If non-sparse, MS-FSA says (just return one range).
*/
if ((attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
if (arg.off < end_off) {
res.off = arg.off;
res.len = end_off - arg.off;
rc = smb_mbc_encodef(fsctl->out_mbc, "qq",
res.off, res.len);
if (rc != 0)
return (NT_STATUS_BUFFER_TOO_SMALL);
}
return (0);
}
cur_off = arg.off;
while (cur_off < end_off) {
off64_t data, hole;
data = cur_off;
err = smb_fsop_next_alloc_range(kcr, ofile->f_node,
&data, &hole);
if (err != 0)
break;
/* sanity check data (ensure progress) */
if (data < cur_off) {
ASSERT(0);
data = cur_off;
}
/* Normal termination */
if (data >= end_off)
break;
/* sanity check hole (ensure progress) */
if (hole <= data)
hole = end_off;
/* Trim this range as needed. */
if (hole > end_off)
hole = end_off;
res.off = data;
res.len = hole - data;
if (res.len > 0) {
rc = smb_mbc_encodef(fsctl->out_mbc, "qq",
res.off, res.len);
if (rc != 0)
return (NT_STATUS_BUFFER_TOO_SMALL);
}
cur_off = hole;
}
return (0);
}
/*
* Copy a segment of a file, preserving sparseness.
* Uses a caller-provided buffer for read/write.
* Caller should already have checked for locks.
*
* On entry, *residp is the length to copy.
* On return, it's the "resid" (amount not copied)
*
* If this gets an error from any I/O, return it, even if some data
* have already been copied. The caller should normally ignore an
* error when some data have been copied.
*/
uint32_t
smb2_sparse_copy(
smb_request_t *sr,
smb_ofile_t *src_ofile, smb_ofile_t *dst_ofile,
off64_t src_off, off64_t dst_off, uint32_t *residp,
void *buffer, size_t bufsize)
{
iovec_t iov;
uio_t uio;
off64_t data, hole;
uint32_t xfer;
uint32_t status = 0;
int rc;
while (*residp > 0) {
if (sr->sr_state != SMB_REQ_STATE_ACTIVE)
break;
data = src_off;
rc = smb_fsop_next_alloc_range(src_ofile->f_cr,
src_ofile->f_node, &data, &hole);
switch (rc) {
case 0:
/* Found data, hole */
break;
case ENXIO:
/* No data after here (will skip below). */
data = hole = (src_off + *residp);
break;
default:
cmn_err(CE_NOTE,
"smb_fsop_next_alloc_range: rc=%d", rc);
/* FALLTHROUGH */
case ENOSYS: /* FS does not support VOP_IOCTL... */
case ENOTTY: /* ... or _FIO_SEEK_DATA, _HOLE */
data = src_off;
hole = src_off + *residp;
break;
}
/*
* Don't try to go past (src_off + *residp)
*/
if (hole > (src_off + *residp))
hole = src_off + *residp;
if (data > hole)
data = hole;
/*
* If there's a gap (src_off .. data)
* skip in src_ofile, zero in dst_ofile
*/
if (src_off < data) {
off64_t skip = data - src_off;
rc = smb_fsop_freesp(sr, dst_ofile->f_cr,
dst_ofile, dst_off, skip);
if (rc == 0) {
src_off += skip;
dst_off += skip;
*residp -= (uint32_t)skip;
} else {
/* Fall back to regular copy */
data = src_off;
}
}
ASSERT(src_off == data);
/*
* Copy this segment: src_off .. hole
*/
while (src_off < hole) {
ssize_t tsize = hole - src_off;
if (tsize > bufsize)
tsize = bufsize;
/*
* Read src_ofile into buffer
*/
iov.iov_base = buffer;
iov.iov_len = tsize;
bzero(&uio, sizeof (uio));
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_resid = tsize;
uio.uio_loffset = src_off;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_extflg = UIO_COPY_DEFAULT;
rc = smb_fsop_read(sr, src_ofile->f_cr,
src_ofile->f_node, src_ofile, &uio, 0);
if (rc != 0) {
status = smb_errno2status(rc);
return (status);
}
/* Note: Could be partial read. */
tsize -= uio.uio_resid;
ASSERT(tsize > 0);
/*
* Write buffer to dst_ofile
*/
iov.iov_base = buffer;
iov.iov_len = tsize;
bzero(&uio, sizeof (uio));
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_resid = tsize;
uio.uio_loffset = dst_off;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_extflg = UIO_COPY_DEFAULT;
rc = smb_fsop_write(sr, dst_ofile->f_cr,
dst_ofile->f_node, dst_ofile, &uio, &xfer, 0);
if (rc != 0) {
status = smb_errno2status(rc);
return (status);
}
ASSERT(xfer <= tsize);
src_off += xfer;
dst_off += xfer;
*residp -= xfer;
}
ASSERT(src_off == hole);
}
return (status);
}
/*
* Not sure what header this might go in.
*/
#define FILE_REGION_USAGE_VALID_CACHED_DATA 1
#define FILE_REGION_USAGE_VALID_NONCACHED_DATA 2
#define FILE_REGION_USAGE_VALID__MASK 3
typedef struct _FILE_REGION_INFO {
uint64_t off;
uint64_t len;
uint32_t usage;
uint32_t reserved;
} FILE_REGION_INFO;
/*
* FSCTL_QUERY_FILE_REGIONS
*
* [MS-FSCC] 2.3.39 FSCTL_QUERY_FILE_REGIONS Request
* [MS-FSCC] 2.3.40.1 FILE_REGION_INFO
*
* Looks like Hyper-V uses this to query the "valid data length",
* which to us is the beginning offset of the last "hole".
* Similar logic as smb2_sparse_copy()
*/
uint32_t
smb2_fsctl_query_file_regions(smb_request_t *sr, smb_fsctl_t *fsctl)
{
smb_attr_t attr;
cred_t *kcr;
smb_ofile_t *ofile = sr->fid_ofile;
FILE_REGION_INFO arg;
off64_t cur_off, end_off, eof;
off64_t data, hole;
uint32_t tot_regions, put_regions;
uint32_t status;
int rc;
if (fsctl->InputCount == 0) {
arg.off = 0;
arg.len = INT64_MAX;
arg.usage = FILE_REGION_USAGE_VALID_CACHED_DATA;
arg.reserved = 0;
} else {
/* min size check: reserved is optional */
rc = smb_mbc_decodef(fsctl->in_mbc, "qql",
&arg.off, &arg.len, &arg.usage);
if (rc != 0)
return (NT_STATUS_BUFFER_TOO_SMALL);
/*
* The given offset and length are int64_t (signed).
*/
if (arg.off > INT64_MAX || arg.len > INT64_MAX)
return (NT_STATUS_INVALID_PARAMETER);
if ((arg.off + arg.len) > INT64_MAX)
return (NT_STATUS_INVALID_PARAMETER);
if ((arg.usage & FILE_REGION_USAGE_VALID__MASK) == 0)
return (NT_STATUS_INVALID_PARAMETER);
arg.reserved = 0;
}
if (fsctl->MaxOutputResp < (16 + sizeof (FILE_REGION_INFO)))
return (NT_STATUS_BUFFER_TOO_SMALL);
if (!smb_node_is_file(ofile->f_node))
return (NT_STATUS_INVALID_PARAMETER);
/*
* This operation is effectively a read
*/
status = smb_ofile_access(ofile, ofile->f_cr, FILE_READ_DATA);
if (status != NT_STATUS_SUCCESS)
return (status);
/*
* Need the file size and dosattr
*/
bzero(&attr, sizeof (attr));
attr.sa_mask = SMB_AT_SIZE | SMB_AT_DOSATTR;
kcr = zone_kcred();
status = smb_node_getattr(sr, ofile->f_node, kcr, ofile, &attr);
if (status != NT_STATUS_SUCCESS)
return (status);
cur_off = arg.off;
end_off = arg.off + arg.len;
eof = attr.sa_vattr.va_size;
/*
* If (InputRegion.FileOffset > Eof) OR
* ((InputRegion.FileOffset == Eof) AND (Eof > 0)),
* the operation MUST return STATUS_SUCCESS, with
* BytesReturned set to 0 (empty data response)
*/
if ((arg.off > eof) || (arg.off == eof && eof > 0))
return (NT_STATUS_SUCCESS);
if (end_off > eof)
end_off = eof;
/*
* We're going to return at least one region. Put place-holder
* data for the fixed part of the response. Will overwrite this
* later, when we know how many regions there are and how many
* of those fit in the allowed response buffer space. These are:
* Flags, TotalRegionEntryCount, RegionEntryCount, Reserved
*/
rc = smb_mbc_encodef(fsctl->out_mbc, "llll", 0, 0, 0, 0);
if (rc != 0)
return (NT_STATUS_BUFFER_TOO_SMALL);
tot_regions = put_regions = 0;
/*
* Get the first pair of (data, hole) offsets at or after
* the current offset (cur_off).
*/
data = hole = cur_off;
rc = smb_fsop_next_alloc_range(ofile->f_cr,
ofile->f_node, &data, &hole);
switch (rc) {
case 0:
/* Found (data, hole) */
break;
case ENXIO:
/* No more data after cur_off. */
break;
default:
cmn_err(CE_NOTE, "smb_fsop_next_alloc_range: rc=%d", rc);
/* FALLTHROUGH */
case ENOSYS: /* FS does not support VOP_IOCTL... */
case ENOTTY: /* ... or _FIO_SEEK_DATA, _HOLE */
data = cur_off;
hole = eof;
break;
}
DTRACE_PROBE2(range0, uint64_t, data, uint64_t, hole);
/*
* Only sparse files should present un-allocated regions.
* If non-sparse, MS-FSA says to just return one region.
* There still can be a "hole" but only one, starting at
* "valid data length" (VDL) and ending at end of file.
* To determine VDL, find the last (data,hole) pair, then
* VDL is the last "hole" offset. Note that the above
* smb_fsop_next_alloc_range may have set data somewhere
* above cur_off, so we we have to reset that here.
*/
if ((attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
/*
* This works, but it's rather inefficient, and
* usually just finds VDL==EOF. Should look into
* whether there's a faster way to find the VDL.
*/
#if 0
off64_t next_data, next_hole;
data = cur_off;
do {
next_data = next_hole = hole;
rc = smb_fsop_next_alloc_range(ofile->f_cr,
ofile->f_node, &next_data, &next_hole);
if (rc == 0) {
hole = next_hole;
}
} while (rc == 0);
#else
/* Assume no "holes" anywhere (VDL==EOF) */
data = cur_off;
hole = eof;
#endif
DTRACE_PROBE2(range1, uint64_t, data, uint64_t, hole);
}
/*
* Loop terminates in the middle, continuing
* while (cur_off < end_off)
*/
for (;;) {
/*
* We have a data region that covers (data, hole).
* It could be partially or entirely beyond the range
* the caller asked about (if so trim it).
*/
if (hole > end_off)
hole = end_off;
if (data > hole)
data = hole;
/*
* If cur_off < data encode a "hole" region
* (cur_off,data) and advance cur_off.
*/
if (cur_off < data) {
rc = smb_mbc_encodef(fsctl->out_mbc, "qqll",
cur_off,
(data - cur_off),
0, // usage (hole)
0); // reserved
cur_off = data;
if (rc == 0)
put_regions++;
tot_regions++;
}
/*
* If cur_off < hole encode a "data" region
* (cur_off,hole) and advance cur_off.
*/
if (cur_off < hole) {
rc = smb_mbc_encodef(fsctl->out_mbc, "qqll",
cur_off,
(hole - cur_off),
FILE_REGION_USAGE_VALID_CACHED_DATA,
0); // reserved
cur_off = hole;
if (rc == 0)
put_regions++;
tot_regions++;
}
/*
* Normal loop termination
*/
if (cur_off >= end_off)
break;
/*
* Get the next region (data, hole) starting on or after
* the current offset (cur_off).
*/
data = hole = cur_off;
rc = smb_fsop_next_alloc_range(ofile->f_cr,
ofile->f_node, &data, &hole);
switch (rc) {
case 0:
/* Found (data, hole) */
break;
case ENXIO:
/*
* No more data after cur_off.
* Will encode one last hole.
*/
data = hole = eof;
break;
default:
cmn_err(CE_NOTE, "smb_fsop_next_alloc_range: rc=%d",
rc);
/* FALLTHROUGH */
case ENOSYS: /* FS does not support VOP_IOCTL... */
case ENOTTY: /* ... or _FIO_SEEK_DATA, _HOLE */
data = cur_off;
hole = eof;
break;
}
DTRACE_PROBE2(range2, uint64_t, data, uint64_t, hole);
}
/*
* Overwrite the fixed part of the response with the
* final numbers of regions etc.
* Flags, TotalRegionEntryCount, RegionEntryCount, Reserved
*/
(void) smb_mbc_poke(fsctl->out_mbc, 0, "llll",
0, // flags
tot_regions,
put_regions,
0); // reserved
if (put_regions < tot_regions)
return (NT_STATUS_BUFFER_OVERFLOW);
return (NT_STATUS_SUCCESS);
}
|