xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb2_read.c (revision 3aa6c13072f3d4792a18693e916aed260a496c1f)
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 2020 Tintri by DDN, Inc. All rights reserved.
14  */
15 
16 /*
17  * Dispatch function for SMB2_READ
18  * MS-SMB2 sec. 3.3.5.12
19  */
20 
21 #include <smbsrv/smb2_kproto.h>
22 #include <smbsrv/smb_fsops.h>
23 
24 extern boolean_t smb_allow_unbuffered;
25 
26 smb_sdrc_t
27 smb2_read(smb_request_t *sr)
28 {
29 	smb_rw_param_t *param = NULL;
30 	smb_ofile_t *of = NULL;
31 	smb_vdb_t *vdb = NULL;
32 	struct mbuf *m = NULL;
33 	uint16_t StructSize;
34 	uint8_t Padding;
35 	uint8_t Flags;
36 	uint8_t DataOff;
37 	uint32_t Length;
38 	uint64_t Offset;
39 	smb2fid_t smb2fid;
40 	uint32_t MinCount;
41 	uint32_t Channel;
42 	uint32_t Remaining;
43 	uint16_t ChanInfoOffset;
44 	uint16_t ChanInfoLength;
45 	uint32_t XferCount;
46 	uint32_t status;
47 	int rc = 0;
48 	boolean_t unbuffered = B_FALSE;
49 	int ioflag = 0;
50 
51 	/*
52 	 * SMB2 Read request
53 	 */
54 	rc = smb_mbc_decodef(
55 	    &sr->smb_data,
56 	    "wbblqqqlllww",
57 	    &StructSize,		/* w */
58 	    &Padding,			/* b */
59 	    &Flags,			/* b */
60 	    &Length,			/* l */
61 	    &Offset,			/* q */
62 	    &smb2fid.persistent,	/* q */
63 	    &smb2fid.temporal,		/* q */
64 	    &MinCount,			/* l */
65 	    &Channel,			/* l */
66 	    &Remaining,			/* l */
67 	    &ChanInfoOffset,		/* w */
68 	    &ChanInfoLength);		/* w */
69 	if (rc)
70 		return (SDRC_ERROR);
71 	if (StructSize != 49)
72 		return (SDRC_ERROR);
73 
74 	/*
75 	 * Setup an smb_rw_param_t which contains the VDB we need.
76 	 * This is automatically free'd.
77 	 */
78 	param = smb_srm_zalloc(sr, sizeof (*param));
79 	param->rw_offset = Offset;
80 	param->rw_count = Length;
81 	/* Note that the dtrace provider uses sr->arg.rw */
82 	sr->arg.rw = param;
83 
84 	/*
85 	 * Want FID lookup before the start probe.
86 	 */
87 	status = smb2sr_lookup_fid(sr, &smb2fid);
88 	of = sr->fid_ofile;
89 
90 	DTRACE_SMB2_START(op__Read, smb_request_t *, sr); /* arg.rw */
91 
92 	if (status)
93 		goto errout; /* Bad FID */
94 
95 	if (Length > smb2_max_rwsize) {
96 		status = NT_STATUS_INVALID_PARAMETER;
97 		goto errout;
98 	}
99 	if (MinCount > Length)
100 		MinCount = Length;
101 
102 	vdb = &param->rw_vdb;
103 	vdb->vdb_tag = 0;
104 	vdb->vdb_uio.uio_iov = &vdb->vdb_iovec[0];
105 	vdb->vdb_uio.uio_iovcnt = MAX_IOVEC;
106 	vdb->vdb_uio.uio_resid = Length;
107 	vdb->vdb_uio.uio_loffset = (offset_t)Offset;
108 	vdb->vdb_uio.uio_segflg = UIO_SYSSPACE;
109 	vdb->vdb_uio.uio_extflg = UIO_COPY_DEFAULT;
110 
111 	sr->raw_data.max_bytes = Length;
112 	m = smb_mbuf_allocate(&vdb->vdb_uio);
113 
114 	/*
115 	 * Unbuffered refers to the MS-FSA Read argument by the same name.
116 	 * It indicates that the cache for this range should be flushed to disk,
117 	 * and data read directly from disk, bypassing the cache.
118 	 * We don't allow that degree of cache management.
119 	 * Translate this directly as FRSYNC,
120 	 * which should at least flush the cache first.
121 	 */
122 
123 	if (smb_allow_unbuffered &&
124 	    (Flags & SMB2_READFLAG_READ_UNBUFFERED) != 0) {
125 		unbuffered = B_TRUE;
126 		ioflag = FRSYNC;
127 	}
128 
129 	switch (of->f_tree->t_res_type & STYPE_MASK) {
130 	case STYPE_DISKTREE:
131 		if (!smb_node_is_dir(of->f_node)) {
132 			/* Check for conflicting locks. */
133 			rc = smb_lock_range_access(sr, of->f_node,
134 			    Offset, Length, B_FALSE);
135 			if (rc) {
136 				rc = ERANGE;
137 				break;
138 			}
139 		}
140 		rc = smb_fsop_read(sr, of->f_cr, of->f_node, of,
141 		    &vdb->vdb_uio, ioflag);
142 		break;
143 	case STYPE_IPC:
144 		if (unbuffered)
145 			rc = EINVAL;
146 		else
147 			rc = smb_opipe_read(sr, &vdb->vdb_uio);
148 		break;
149 	default:
150 	case STYPE_PRINTQ:
151 		rc = EACCES;
152 		break;
153 	}
154 	status = smb_errno2status(rc);
155 
156 	/* How much data we moved. */
157 	XferCount = Length - vdb->vdb_uio.uio_resid;
158 
159 	sr->raw_data.max_bytes = XferCount;
160 	smb_mbuf_trim(m, XferCount);
161 	MBC_ATTACH_MBUF(&sr->raw_data, m);
162 
163 	/*
164 	 * [MS-SMB2] If the read returns fewer bytes than specified by
165 	 * the MinimumCount field of the request, the server MUST fail
166 	 * the request with STATUS_END_OF_FILE
167 	 */
168 	if (status == 0 && XferCount < MinCount)
169 		status = NT_STATUS_END_OF_FILE;
170 
171 	/*
172 	 * Checking the error return _after_ dealing with
173 	 * the returned data so that if m was allocated,
174 	 * it will be free'd via sr->raw_data cleanup.
175 	 */
176 errout:
177 	sr->smb2_status = status;
178 	DTRACE_SMB2_DONE(op__Read, smb_request_t *, sr); /* arg.rw */
179 	if (status) {
180 		smb2sr_put_error(sr, status);
181 		return (SDRC_SUCCESS);
182 	}
183 
184 	/*
185 	 * SMB2 Read reply
186 	 */
187 	DataOff = SMB2_HDR_SIZE + 16;
188 	rc = smb_mbc_encodef(
189 	    &sr->reply,
190 	    "wb.lllC",
191 	    17,	/* StructSize */	/* w */
192 	    DataOff,			/* b. */
193 	    XferCount,			/* l */
194 	    0, /* DataRemaining */	/* l */
195 	    0, /* reserved */		/* l */
196 	    &sr->raw_data);		/* C */
197 	if (rc) {
198 		sr->smb2_status = NT_STATUS_INTERNAL_ERROR;
199 		return (SDRC_ERROR);
200 	}
201 
202 	mutex_enter(&of->f_mutex);
203 	of->f_seek_pos = Offset + XferCount;
204 	mutex_exit(&of->f_mutex);
205 
206 	return (SDRC_SUCCESS);
207 }
208