summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb2_fsctl_fs.c
blob: d6da839e0533a1a460a6e93d96975f4b7c9c3dfb (plain)
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
/*
 * 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 2019 Nexenta by DDN, Inc. All rights reserved.
 * Copyright 2022 RackTop Systems, Inc.
 */

/*
 * Support functions for smb2_ioctl/fsctl categories:
 * FILE_DEVICE_FILE_SYSTEM (9)
 * FILE_DEVICE_NETWORK_FILE_SYSTEM (20)
 */

#include <smbsrv/smb2_kproto.h>
#include <smbsrv/smb_fsops.h>
#include <smb/winioctl.h>

static uint32_t
smb2_fsctl_invalid(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	return (NT_STATUS_INVALID_DEVICE_REQUEST);
}

static uint32_t
smb2_fsctl_notsup(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	return (NT_STATUS_NOT_SUPPORTED);
}

/*
 * Same as smb2_fsctl_invalid, but make some noise (if DEBUG)
 * so we'll learn about new fsctl codes clients start using.
 */
/* ARGSUSED */
static uint32_t
smb2_fsctl_unknown(smb_request_t *sr, smb_fsctl_t *fsctl)
{
#ifdef	DEBUG
	cmn_err(CE_NOTE, "smb2_fsctl_unknown: code 0x%x", fsctl->CtlCode);
#endif
	return (NT_STATUS_INVALID_DEVICE_REQUEST);
}

/*
 * FSCTL_GET_COMPRESSION
 */
static uint32_t
smb2_fsctl_get_compression(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	_NOTE(ARGUNUSED(sr))
	uint16_t compress_state = 0;
	int rc;

	rc = smb_mbc_encodef(fsctl->in_mbc, "w",
	    compress_state);
	if (rc != 0)
		return (NT_STATUS_BUFFER_OVERFLOW);

	return (NT_STATUS_SUCCESS);
}

/*
 * FSCTL_SET_COMPRESSION
 */
static uint32_t
smb2_fsctl_set_compression(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	_NOTE(ARGUNUSED(sr))

	uint16_t compress_state;
	(void) smb_mbc_decodef(fsctl->in_mbc, "w",
	    &compress_state);

	if (compress_state > 0)
		return (NT_STATUS_COMPRESSION_DISABLED);

	return (NT_STATUS_SUCCESS);
}

/*
 * FSCTL_SRV_REQUEST_RESUME_KEY
 *
 * The returned data is an (opaque to the client) 24-byte blob
 * in which we stash the SMB2 "file ID" (both parts). Later,
 * copychunk may lookup the ofile using that file ID.
 * See: smb2_fsctl_copychunk()
 *
 * Note that Mac clients make this request on a directory
 * (even though this only makes sense on a file) just to
 * find out if the server supports server-side copy.
 * There's no harm letting a client have a resume key
 * for a directory.  They'll never be able to DO anything
 * with it because we check for a plain file later.
 */
static uint32_t
smb2_fsctl_get_resume_key(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	smb_ofile_t *of = sr->fid_ofile;
	smb2fid_t smb2fid;
	int rc;

	/* Caller makes sure we have of = sr->fid_ofile */
	/* Don't insist on a plain file (see above). */

	smb2fid.persistent = of->f_persistid;
	smb2fid.temporal = of->f_fid;

	rc = smb_mbc_encodef(
	    fsctl->out_mbc, "qq16.",
	    smb2fid.persistent,
	    smb2fid.temporal);
	if (rc != 0)
		return (NT_STATUS_BUFFER_OVERFLOW);

	return (NT_STATUS_SUCCESS);
}

/*
 * FILE_DEVICE_FILE_SYSTEM (9)
 */
uint32_t
smb2_fsctl_fs(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	uint32_t (*func)(smb_request_t *, smb_fsctl_t *);
	uint32_t status;

	switch (fsctl->CtlCode) {
	case FSCTL_GET_COMPRESSION:		/* 15 */
		func = smb2_fsctl_get_compression;
		break;
	case FSCTL_SET_COMPRESSION:		/* 16 */
		func = smb2_fsctl_set_compression;
		break;
	case FSCTL_SET_REPARSE_POINT:		/* 41 */
	case FSCTL_GET_REPARSE_POINT:		/* 42 */
		func = smb2_fsctl_invalid;
		break;
	case FSCTL_CREATE_OR_GET_OBJECT_ID:	/* 48 */
		func = smb2_fsctl_invalid;
		break;
	case FSCTL_SET_SPARSE:			/* 49 */
		func = smb2_fsctl_set_sparse;
		break;
	case FSCTL_SET_ZERO_DATA:		/* 50 */
		func = smb2_fsctl_set_zero_data;
		break;
	case FSCTL_QUERY_ALLOCATED_RANGES:	/* 51 */
		func = smb2_fsctl_query_alloc_ranges;
		break;
	case FSCTL_FILE_LEVEL_TRIM:		/* 130 */
		func = smb2_fsctl_invalid;
		break;
	case FSCTL_OFFLOAD_READ:		/* 153 */
		func = smb2_fsctl_odx_read;
		break;
	case FSCTL_OFFLOAD_WRITE:		/* 154 */
		func = smb2_fsctl_odx_write;
		break;
	case FSCTL_GET_INTEGRITY_INFORMATION:	/* 159 */
	case FSCTL_SET_INTEGRITY_INFORMATION:	/* 160 */
		func = smb2_fsctl_invalid;
		break;
	case FSCTL_QUERY_FILE_REGIONS:		/* 161 */
		func = smb2_fsctl_query_file_regions;
		break;

	case FSCTL_REFS_STREAM_SNAPSHOT_MANAGEMENT:
		/* WPTS wants NOT_SUPPORTED here. */
		func = smb2_fsctl_notsup;
		break;

	default:
		func = smb2_fsctl_unknown;
		break;
	}

	/*
	 * All "fs" sub-codes require a disk file.
	 */
	if (sr->fid_ofile == NULL ||
	    !SMB_FTYPE_IS_DISK(sr->fid_ofile->f_ftype))
		return (NT_STATUS_INVALID_PARAMETER);

	status = (*func)(sr, fsctl);
	return (status);
}

/*
 * FILE_DEVICE_NETWORK_FILE_SYSTEM (20)
 */
uint32_t
smb2_fsctl_netfs(smb_request_t *sr, smb_fsctl_t *fsctl)
{
	uint32_t (*func)(smb_request_t *, smb_fsctl_t *);
	uint32_t status;
	boolean_t need_disk_file = B_TRUE;

	switch (fsctl->CtlCode) {
	case FSCTL_SRV_ENUMERATE_SNAPSHOTS:	/* 0x19 */
		func = smb_vss_enum_snapshots;
		break;
	case FSCTL_SRV_REQUEST_RESUME_KEY:	/* 0x1e */
		func = smb2_fsctl_get_resume_key;
		break;
	case FSCTL_SRV_COPYCHUNK:		/* 0x3c(r) */
	case FSCTL_SRV_COPYCHUNK_WRITE:		/* 0x3c(w) */
		func = smb2_fsctl_copychunk;
		break;
	case FSCTL_SRV_READ_HASH:		/* 0x6e */
		func = smb2_fsctl_invalid;
		break;
	case FSCTL_LMR_REQUEST_RESILIENCY:	/* 0x75 */
		func = smb2_fsctl_set_resilient;
		break;
	case FSCTL_QUERY_NETWORK_INTERFACE_INFO: /* 0x7f */
		need_disk_file = B_FALSE;
		func = smb2_fsctl_invalid;
		break;
	case FSCTL_VALIDATE_NEGOTIATE_INFO:	/* 0x81 */
		need_disk_file = B_FALSE;
		func = smb2_nego_validate;
		break;
	default:
		func = smb2_fsctl_unknown;
		break;
	}

	/*
	 * Most "net fs" sub-codes require a disk file,
	 * except a couple that clear need_disk_file.
	 */
	if (need_disk_file && (sr->fid_ofile == NULL ||
	    !SMB_FTYPE_IS_DISK(sr->fid_ofile->f_ftype)))
		return (NT_STATUS_INVALID_PARAMETER);

	status = (*func)(sr, fsctl);
	return (status);
}