xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb2_read.c (revision 344db6f401efe76f7e1d4f4c1a644ae593910219)
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 2019 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * Dispatch function for SMB2_READ
18  */
19 
20 #include <smbsrv/smb2_kproto.h>
21 #include <smbsrv/smb_fsops.h>
22 
23 smb_sdrc_t
24 smb2_read(smb_request_t *sr)
25 {
26 	smb_rw_param_t *param = NULL;
27 	smb_ofile_t *of = NULL;
28 	smb_vdb_t *vdb = NULL;
29 	struct mbuf *m = NULL;
30 	uint16_t StructSize;
31 	uint8_t Padding;
32 	uint8_t DataOff;
33 	uint32_t Length;
34 	uint64_t Offset;
35 	smb2fid_t smb2fid;
36 	uint32_t MinCount;
37 	uint32_t Channel;
38 	uint32_t Remaining;
39 	uint16_t ChanInfoOffset;
40 	uint16_t ChanInfoLength;
41 	uint32_t XferCount;
42 	uint32_t status;
43 	int rc = 0;
44 
45 	/*
46 	 * SMB2 Read request
47 	 */
48 	rc = smb_mbc_decodef(
49 	    &sr->smb_data,
50 	    "wb.lqqqlllww",
51 	    &StructSize,		/* w */
52 	    &Padding,			/* b. */
53 	    &Length,			/* l */
54 	    &Offset,			/* q */
55 	    &smb2fid.persistent,	/* q */
56 	    &smb2fid.temporal,		/* q */
57 	    &MinCount,			/* l */
58 	    &Channel,			/* l */
59 	    &Remaining,			/* l */
60 	    &ChanInfoOffset,		/* w */
61 	    &ChanInfoLength);		/* w */
62 	if (rc)
63 		return (SDRC_ERROR);
64 	if (StructSize != 49)
65 		return (SDRC_ERROR);
66 
67 	/*
68 	 * Setup an smb_rw_param_t which contains the VDB we need.
69 	 * This is automatically free'd.
70 	 */
71 	param = smb_srm_zalloc(sr, sizeof (*param));
72 	param->rw_offset = Offset;
73 	param->rw_count = Length;
74 	/* Note that the dtrace provider uses sr->arg.rw */
75 	sr->arg.rw = param;
76 
77 	/*
78 	 * Want FID lookup before the start probe.
79 	 */
80 	status = smb2sr_lookup_fid(sr, &smb2fid);
81 	of = sr->fid_ofile;
82 
83 	DTRACE_SMB2_START(op__Read, smb_request_t *, sr); /* arg.rw */
84 
85 	if (status)
86 		goto errout; /* Bad FID */
87 
88 	if (Length > smb2_max_rwsize) {
89 		status = NT_STATUS_INVALID_PARAMETER;
90 		goto errout;
91 	}
92 	if (MinCount > Length)
93 		MinCount = Length;
94 
95 	vdb = &param->rw_vdb;
96 	vdb->vdb_tag = 0;
97 	vdb->vdb_uio.uio_iov = &vdb->vdb_iovec[0];
98 	vdb->vdb_uio.uio_iovcnt = MAX_IOVEC;
99 	vdb->vdb_uio.uio_resid = Length;
100 	vdb->vdb_uio.uio_loffset = (offset_t)Offset;
101 	vdb->vdb_uio.uio_segflg = UIO_SYSSPACE;
102 	vdb->vdb_uio.uio_extflg = UIO_COPY_DEFAULT;
103 
104 	sr->raw_data.max_bytes = Length;
105 	m = smb_mbuf_allocate(&vdb->vdb_uio);
106 
107 	switch (of->f_tree->t_res_type & STYPE_MASK) {
108 	case STYPE_DISKTREE:
109 		if (!smb_node_is_dir(of->f_node)) {
110 			/* Check for conflicting locks. */
111 			rc = smb_lock_range_access(sr, of->f_node,
112 			    Offset, Length, B_FALSE);
113 			if (rc) {
114 				rc = ERANGE;
115 				break;
116 			}
117 		}
118 		rc = smb_fsop_read(sr, of->f_cr, of->f_node, &vdb->vdb_uio);
119 		break;
120 	case STYPE_IPC:
121 		rc = smb_opipe_read(sr, &vdb->vdb_uio);
122 		break;
123 	default:
124 	case STYPE_PRINTQ:
125 		rc = EACCES;
126 		break;
127 	}
128 	status = smb_errno2status(rc);
129 
130 	/* How much data we moved. */
131 	XferCount = Length - vdb->vdb_uio.uio_resid;
132 
133 	sr->raw_data.max_bytes = XferCount;
134 	smb_mbuf_trim(m, XferCount);
135 	MBC_ATTACH_MBUF(&sr->raw_data, m);
136 
137 	/*
138 	 * Checking the error return _after_ dealing with
139 	 * the returned data so that if m was allocated,
140 	 * it will be free'd via sr->raw_data cleanup.
141 	 */
142 errout:
143 	sr->smb2_status = status;
144 	DTRACE_SMB2_DONE(op__Read, smb_request_t *, sr); /* arg.rw */
145 	if (status) {
146 		smb2sr_put_error(sr, status);
147 		return (SDRC_SUCCESS);
148 	}
149 
150 	/*
151 	 * SMB2 Read reply
152 	 */
153 	DataOff = SMB2_HDR_SIZE + 16;
154 	rc = smb_mbc_encodef(
155 	    &sr->reply,
156 	    "wb.lllC",
157 	    17,	/* StructSize */	/* w */
158 	    DataOff,			/* b. */
159 	    XferCount,			/* l */
160 	    0, /* DataRemaining */	/* l */
161 	    0, /* reserved */		/* l */
162 	    &sr->raw_data);		/* C */
163 	if (rc) {
164 		sr->smb2_status = NT_STATUS_INTERNAL_ERROR;
165 		return (SDRC_ERROR);
166 	}
167 
168 	mutex_enter(&of->f_mutex);
169 	of->f_seek_pos = Offset + XferCount;
170 	mutex_exit(&of->f_mutex);
171 
172 	return (SDRC_SUCCESS);
173 }
174