xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb2_fsctl_sparse.c (revision 55f0a249fd3511728b02627190771a4ce4ddf20e)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * Support functions for smb2_ioctl/fsctl codes:
18  * FSCTL_SET_SPARSE
19  * FSCTL_SET_ZERO_DATA
20  * FSCTL_QUERY_ALLOCATED_RANGES
21  */
22 
23 #include <smbsrv/smb2_kproto.h>
24 #include <smbsrv/smb_fsops.h>
25 #include <smb/winioctl.h>
26 
27 /*
28  * FSCTL_SET_SPARSE
29  *
30  * In args: one byte flag (optional: default TRUE)
31  */
32 uint32_t
33 smb2_fsctl_set_sparse(smb_request_t *sr, smb_fsctl_t *fsctl)
34 {
35 	smb_attr_t attr;
36 	smb_ofile_t *ofile = sr->fid_ofile;
37 	cred_t *kcr;
38 	uint32_t amask;
39 	uint32_t status;
40 	uint8_t flag;
41 	int rc;
42 
43 	rc = smb_mbc_decodef(fsctl->in_mbc, "b", &flag);
44 	if (rc != 0)
45 		flag = 0xff;
46 
47 	if (!smb_node_is_file(ofile->f_node))
48 		return (NT_STATUS_INVALID_PARAMETER);
49 
50 	/*
51 	 * Allow if we have any of FILE_WRITE_ATTRIBUTES,
52 	 * FILE_WRITE_DATA, FILE_APPEND_DATA
53 	 */
54 	amask = FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | FILE_APPEND_DATA;
55 	if ((ofile->f_granted_access & amask) == 0)
56 		return (NT_STATUS_ACCESS_DENIED);
57 
58 	/*
59 	 * Need the current DOS attributes
60 	 */
61 	bzero(&attr, sizeof (attr));
62 	attr.sa_mask = SMB_AT_DOSATTR;
63 	kcr = zone_kcred();
64 	status = smb_node_getattr(sr, ofile->f_node, kcr, ofile, &attr);
65 	if (status != NT_STATUS_SUCCESS)
66 		return (status);
67 
68 	if (flag != 0) {
69 		/* Set "sparse" */
70 		if (attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE)
71 			return (0);
72 		attr.sa_dosattr |= FILE_ATTRIBUTE_SPARSE_FILE;
73 	} else {
74 		/* Clear "sparse" */
75 		if ((attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
76 			return (0);
77 		attr.sa_dosattr &= ~FILE_ATTRIBUTE_SPARSE_FILE;
78 	}
79 
80 	attr.sa_mask = SMB_AT_DOSATTR;
81 	status = smb_node_setattr(sr, ofile->f_node, kcr, ofile, &attr);
82 	return (status);
83 }
84 
85 /*
86  * FSCTL_SET_ZERO_DATA
87  *
88  * In args: uint64_t start_off, end_off
89  */
90 uint32_t
91 smb2_fsctl_set_zero_data(smb_request_t *sr, smb_fsctl_t *fsctl)
92 {
93 	smb_attr_t attr;
94 	smb_ofile_t *ofile = sr->fid_ofile;
95 	uint64_t start_off, end_off, zero_len;
96 	uint32_t status;
97 	int rc;
98 
99 	rc = smb_mbc_decodef(fsctl->in_mbc, "qq",
100 	    &start_off, &end_off);
101 	if (rc != 0)
102 		return (NT_STATUS_BUFFER_TOO_SMALL);
103 
104 	/*
105 	 * The given offsets are actually int64_t (signed).
106 	 */
107 	if (start_off > INT64_MAX ||
108 	    end_off > INT64_MAX ||
109 	    start_off > end_off)
110 		return (NT_STATUS_INVALID_PARAMETER);
111 
112 	if (!smb_node_is_file(ofile->f_node))
113 		return (NT_STATUS_INVALID_PARAMETER);
114 
115 	/*
116 	 * This operation is effectively a write (of zeros)
117 	 */
118 	status = smb_ofile_access(ofile, ofile->f_cr, FILE_WRITE_DATA);
119 	if (status != NT_STATUS_SUCCESS)
120 		return (status);
121 
122 	/*
123 	 * Need the file size
124 	 */
125 	bzero(&attr, sizeof (attr));
126 	attr.sa_mask = SMB_AT_SIZE;
127 	status = smb_node_getattr(sr, ofile->f_node, ofile->f_cr,
128 	    ofile, &attr);
129 	if (status != NT_STATUS_SUCCESS)
130 		return (status);
131 
132 	/*
133 	 * Ignore any zero-ing beyond EOF
134 	 */
135 	if (end_off > attr.sa_vattr.va_size)
136 		end_off = attr.sa_vattr.va_size;
137 	if (start_off >= end_off)
138 		return (0);
139 	zero_len = end_off - start_off;
140 
141 	/*
142 	 * Check for lock conflicting with the write.
143 	 */
144 	status = smb_lock_range_access(sr, ofile->f_node,
145 	    start_off, zero_len, B_TRUE);
146 	if (status != 0)
147 		return (status); /* == FILE_LOCK_CONFLICT */
148 
149 	rc = smb_fsop_freesp(sr, ofile->f_cr, ofile,
150 	    start_off, zero_len);
151 	if (rc != 0)
152 		status = smb_errno2status(rc);
153 
154 	return (status);
155 }
156 
157 /*
158  * FSCTL_QUERY_ALLOCATED_RANGES
159  *
160  * Incoming args: uint64_t start_off, end_off
161  */
162 struct alloc_range {
163 	off64_t off;
164 	off64_t len;
165 };
166 uint32_t
167 smb2_fsctl_query_alloc_ranges(smb_request_t *sr, smb_fsctl_t *fsctl)
168 {
169 	smb_attr_t attr;
170 	cred_t *kcr;
171 	smb_ofile_t *ofile = sr->fid_ofile;
172 	struct alloc_range arg, res;
173 	off64_t cur_off, end_off;
174 	uint32_t status;
175 	int err, rc;
176 
177 	/*
178 	 * Most ioctls return NT_STATUS_BUFFER_TOO_SMALL for
179 	 * short in/out buffers, but for this one, MS-FSA
180 	 * says short input returns invalid parameter.
181 	 */
182 	rc = smb_mbc_decodef(fsctl->in_mbc, "qq", &arg.off, &arg.len);
183 	if (rc != 0)
184 		return (NT_STATUS_INVALID_PARAMETER);
185 
186 	/*
187 	 * The given offsets are actually int64_t (signed).
188 	 */
189 	end_off = arg.off + arg.len;
190 	if (arg.off > INT64_MAX || arg.len < 0 ||
191 	    end_off > INT64_MAX || end_off < arg.off)
192 		return (NT_STATUS_INVALID_PARAMETER);
193 
194 	if (!smb_node_is_file(ofile->f_node))
195 		return (NT_STATUS_INVALID_PARAMETER);
196 
197 	/*
198 	 * This operation is effectively a read
199 	 */
200 	status = smb_ofile_access(ofile, ofile->f_cr, FILE_READ_DATA);
201 	if (status != NT_STATUS_SUCCESS)
202 		return (status);
203 
204 	if (arg.len == 0) {
205 		/* MS-FSA says empty result for this. */
206 		return (0);
207 	}
208 
209 	/*
210 	 * Need the file size and dosattr
211 	 */
212 	bzero(&attr, sizeof (attr));
213 	attr.sa_mask = SMB_AT_SIZE | SMB_AT_DOSATTR;
214 	kcr = zone_kcred();
215 	status = smb_node_getattr(sr, ofile->f_node, kcr, ofile, &attr);
216 	if (status != NT_STATUS_SUCCESS)
217 		return (status);
218 	if (end_off > attr.sa_vattr.va_size)
219 		end_off = attr.sa_vattr.va_size;
220 
221 	/*
222 	 * Only sparse files should present un-allocated ranges.
223 	 * If non-sparse, MS-FSA says (just return one range).
224 	 */
225 	if ((attr.sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
226 		if (arg.off < end_off) {
227 			res.off = arg.off;
228 			res.len = end_off - arg.off;
229 			rc = smb_mbc_encodef(fsctl->out_mbc, "qq",
230 			    res.off, res.len);
231 			if (rc != 0)
232 				return (NT_STATUS_BUFFER_TOO_SMALL);
233 		}
234 		return (0);
235 	}
236 
237 	cur_off = arg.off;
238 	while (cur_off < end_off) {
239 		off64_t data, hole;
240 
241 		data = cur_off;
242 		err = smb_fsop_next_alloc_range(kcr, ofile->f_node,
243 		    &data, &hole);
244 		if (err != 0)
245 			break;
246 
247 		/* sanity check data (ensure progress) */
248 		if (data < cur_off) {
249 			ASSERT(0);
250 			data = cur_off;
251 		}
252 
253 		/* Normal termination */
254 		if (data >= end_off)
255 			break;
256 
257 		/* sanity check hole (ensure progress) */
258 		if (hole <= data)
259 			hole = end_off;
260 
261 		/* Trim this range as needed. */
262 		if (hole > end_off)
263 			hole = end_off;
264 
265 		res.off = data;
266 		res.len = hole - data;
267 
268 		if (res.len > 0) {
269 			rc = smb_mbc_encodef(fsctl->out_mbc, "qq",
270 			    res.off, res.len);
271 			if (rc != 0)
272 				return (NT_STATUS_BUFFER_TOO_SMALL);
273 		}
274 
275 		cur_off = hole;
276 	}
277 
278 	return (0);
279 }
280 
281 /*
282  * Copy a segment of a file, preserving sparseness.
283  * Uses a caller-provided buffer for read/write.
284  * Caller should already have checked for locks.
285  *
286  * On entry, *residp is the length to copy.
287  * On return, it's the "resid" (amount not copied)
288  *
289  * If this gets an error from any I/O, return it, even if some data
290  * have already been copied.  The caller should normally ignore an
291  * error when some data have been copied.
292  */
293 uint32_t
294 smb2_sparse_copy(
295 	smb_request_t *sr,
296 	smb_ofile_t *src_ofile, smb_ofile_t *dst_ofile,
297 	off64_t src_off, off64_t dst_off, uint32_t *residp,
298 	void *buffer, size_t bufsize)
299 {
300 	iovec_t iov;
301 	uio_t uio;
302 	off64_t data, hole;
303 	uint32_t xfer;
304 	uint32_t status = 0;
305 	int rc;
306 
307 	while (*residp > 0) {
308 
309 		data = src_off;
310 		rc = smb_fsop_next_alloc_range(src_ofile->f_cr,
311 		    src_ofile->f_node, &data, &hole);
312 		switch (rc) {
313 		case 0:
314 			/* Found data, hole */
315 			break;
316 		case ENXIO:
317 			/* No data after here (will skip below). */
318 			data = hole = (src_off + *residp);
319 			break;
320 		default:
321 			cmn_err(CE_NOTE,
322 			    "smb_fsop_next_alloc_range: rc=%d", rc);
323 			/* FALLTHROUGH */
324 		case ENOSYS:	/* FS does not support VOP_IOCTL... */
325 		case ENOTTY:	/* ... or _FIO_SEEK_DATA, _HOLE */
326 			data = src_off;
327 			hole = src_off + *residp;
328 			break;
329 		}
330 
331 		/*
332 		 * Don't try to go past (src_off + *residp)
333 		 */
334 		if (hole > (src_off + *residp))
335 			hole = src_off + *residp;
336 		if (data > hole)
337 			data = hole;
338 
339 		/*
340 		 * If there's a gap (src_off .. data)
341 		 * skip in src_ofile, zero in dst_ofile
342 		 */
343 		if (src_off < data) {
344 			off64_t skip = data - src_off;
345 			rc = smb_fsop_freesp(sr, dst_ofile->f_cr,
346 			    dst_ofile, dst_off, skip);
347 			if (rc == 0) {
348 				src_off += skip;
349 				dst_off += skip;
350 				*residp -= (uint32_t)skip;
351 			} else {
352 				/* Fall back to regular copy */
353 				data = src_off;
354 			}
355 		}
356 		ASSERT(src_off == data);
357 
358 		/*
359 		 * Copy this segment: src_off .. hole
360 		 */
361 		while (src_off < hole) {
362 			ssize_t tsize = hole - src_off;
363 			if (tsize > bufsize)
364 				tsize = bufsize;
365 
366 			/*
367 			 * Read src_ofile into buffer
368 			 */
369 			iov.iov_base = buffer;
370 			iov.iov_len  = tsize;
371 			bzero(&uio, sizeof (uio));
372 			uio.uio_iov = &iov;
373 			uio.uio_iovcnt = 1;
374 			uio.uio_resid = tsize;
375 			uio.uio_loffset = src_off;
376 			uio.uio_segflg = UIO_SYSSPACE;
377 			uio.uio_extflg = UIO_COPY_DEFAULT;
378 
379 			rc = smb_fsop_read(sr, src_ofile->f_cr,
380 			    src_ofile->f_node, src_ofile, &uio);
381 			if (rc != 0) {
382 				status = smb_errno2status(rc);
383 				return (status);
384 			}
385 			/* Note: Could be partial read. */
386 			tsize -= uio.uio_resid;
387 			ASSERT(tsize > 0);
388 
389 			/*
390 			 * Write buffer to dst_ofile
391 			 */
392 			iov.iov_base = buffer;
393 			iov.iov_len  = tsize;
394 			bzero(&uio, sizeof (uio));
395 			uio.uio_iov = &iov;
396 			uio.uio_iovcnt = 1;
397 			uio.uio_resid = tsize;
398 			uio.uio_loffset = dst_off;
399 			uio.uio_segflg = UIO_SYSSPACE;
400 			uio.uio_extflg = UIO_COPY_DEFAULT;
401 
402 			rc = smb_fsop_write(sr, dst_ofile->f_cr,
403 			    dst_ofile->f_node, dst_ofile, &uio, &xfer, 0);
404 			if (rc != 0) {
405 				status = smb_errno2status(rc);
406 				return (status);
407 			}
408 			ASSERT(xfer <= tsize);
409 
410 			src_off += xfer;
411 			dst_off += xfer;
412 			*residp -= xfer;
413 		}
414 		ASSERT(src_off == hole);
415 	}
416 
417 	return (status);
418 }
419