summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb2_lock.c
blob: 7374dead591af4826e7e4c38b58628c8e3e35d96 (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
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
/*
 * 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 2014 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * Dispatch function for SMB2_LOCK
 */

#include <smbsrv/smb2_kproto.h>

struct SMB2_LOCK_ELEMENT {
	uint64_t Offset;
	uint64_t Length;
	uint32_t Flags;
	uint32_t reserved;
};

static smb_sdrc_t smb2_lock_async(smb_request_t *);
static uint32_t smb2_lock_exec(smb_request_t *, uint16_t);
static uint32_t smb2_lock_elem(smb_request_t *, struct SMB2_LOCK_ELEMENT *);

/*
 * This is a somewhat arbitrary sanity limit on the length of the
 * SMB2_LOCK_ELEMENT array.  It usually has length one or two.
 */
int smb2_lock_max_elem = 1024;

smb_sdrc_t
smb2_lock(smb_request_t *sr)
{
	struct SMB2_LOCK_ELEMENT elem;
	smb2fid_t smb2fid;
	uint32_t save_offset;
	uint32_t LockSequence;
	uint32_t status;
	uint16_t StructSize;
	uint16_t LockCount;
	uint16_t i;
	boolean_t MayBlock = B_FALSE;
	int rc = 0;

	/*
	 * SMB2 Lock request
	 */
	rc = smb_mbc_decodef(
	    &sr->smb_data, "wwlqq",
	    &StructSize,		/* w */
	    &LockCount,			/* w */
	    &LockSequence,		/* l */
	    &smb2fid.persistent,	/* q */
	    &smb2fid.temporal);		/* q */
	if (rc || StructSize != 48)
		return (SDRC_ERROR);

	status = smb2sr_lookup_fid(sr, &smb2fid);
	if (status)
		goto errout;
	if (sr->fid_ofile->f_node == NULL || LockCount == 0) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto errout;
	}
	if (LockCount > smb2_lock_max_elem) {
		status = NT_STATUS_INSUFFICIENT_RESOURCES;
		goto errout;
	}

	/*
	 * Process the array of SMB2_LOCK_ELEMENT structs
	 * We do this twice.  (it's always a short list)
	 * The first time, just validate the flags, and check
	 * if any of the locking request might need to block.
	 * The second time (either here, or in the async
	 * handler function) process the locks for real.
	 */
	save_offset = sr->smb_data.chain_offset;
	for (i = 0; i < LockCount; i++) {
		rc = smb_mbc_decodef(
		    &sr->smb_data, "qqll",
		    &elem.Offset,	/* q */
		    &elem.Length,	/* q */
		    &elem.Flags,	/* l */
		    &elem.reserved);	/* l */
		if (rc) {
			status = NT_STATUS_INVALID_PARAMETER;
			goto errout;
		}

		/*
		 * Make sure the flags are valid;
		 * Find out if we might block.
		 */
		switch (elem.Flags) {
		case SMB2_LOCKFLAG_SHARED_LOCK:
		case SMB2_LOCKFLAG_EXCLUSIVE_LOCK:
			MayBlock = B_TRUE;
			break;

		/* BEGIN CSTYLED */
		case SMB2_LOCKFLAG_SHARED_LOCK |
		     SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
		case SMB2_LOCKFLAG_EXCLUSIVE_LOCK |
		     SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
		case SMB2_LOCKFLAG_UNLOCK:
		/* END CSTYLED */
			break;

		default:
			status = NT_STATUS_INVALID_PARAMETER;
			goto errout;
		}
	}

	if (MayBlock) {
		/*
		 * May need to block.  "Go async".
		 */
		status = smb2sr_go_async(sr, smb2_lock_async);
		goto errout;
	}

	sr->smb_data.chain_offset = save_offset;
	status = smb2_lock_exec(sr, LockCount);
	if (status)
		goto errout;

	/*
	 * SMB2 Lock reply (sync)
	 */
	StructSize = 4;
	(void) smb_mbc_encodef(
	    &sr->reply, "w..",
	    StructSize);	/* w */
	    /* reserved		  .. */
	return (SDRC_SUCCESS);

errout:
	smb2sr_put_error(sr, status);
	return (SDRC_SUCCESS);
}

static smb_sdrc_t
smb2_lock_async(smb_request_t *sr)
{
	smb2fid_t smb2fid;
	uint32_t LockSequence;
	uint32_t status;
	uint16_t StructSize;
	uint16_t LockCount;
	int rc = 0;

	/*
	 * Decode the lock request again.  It should all decode
	 * exactly the same as the first time we saw it.  If not,
	 * report an "internal error".
	 */
	rc = smb_mbc_decodef(
	    &sr->smb_data, "wwlqq",
	    &StructSize,		/* w */
	    &LockCount,			/* w */
	    &LockSequence,		/* l */
	    &smb2fid.persistent,	/* q */
	    &smb2fid.temporal);		/* q */
	if (rc || StructSize != 48)
		return (SDRC_ERROR);

	status = smb2sr_lookup_fid(sr, &smb2fid);
	if (status)
		goto errout;
	if (sr->fid_ofile->f_node == NULL || LockCount == 0) {
		status = NT_STATUS_INTERNAL_ERROR;
		goto errout;
	}

	status = smb2_lock_exec(sr, LockCount);
	if (status)
		goto errout;

	/*
	 * SMB2 Lock reply (async)
	 */
	StructSize = 4;
	(void) smb_mbc_encodef(
	    &sr->reply, "w..",
	    StructSize);	/* w */
	    /* reserved		  .. */
	return (SDRC_SUCCESS);

errout:
	smb2sr_put_error(sr, status);
	return (SDRC_SUCCESS);
}

/*
 * Execute the vector of locks.  This is the common function called by
 * either the sync or async code paths.  We've already decoded this
 * request once when we get here, so if there are any decode errors
 * then it's some kind of internal error.
 */
static uint32_t
smb2_lock_exec(smb_request_t *sr, uint16_t LockCount)
{
	struct SMB2_LOCK_ELEMENT elem;
	uint32_t status = 0;
	uint16_t i;
	int rc;

	/*
	 * On entry, out position in the input data should be
	 * after both the SMB2 header and the fixed part of
	 * the SMB Lock request header (24).
	 */
	ASSERT(sr->smb_data.chain_offset ==
	    (sr->smb2_cmd_hdr + SMB2_HDR_SIZE + 24));

	/*
	 * This is checked by our callers, but let's make sure.
	 */
	ASSERT(sr->fid_ofile->f_node != NULL);

	for (i = 0; i < LockCount; i++) {
		rc = smb_mbc_decodef(
		    &sr->smb_data, "qqll",
		    &elem.Offset,	/* q */
		    &elem.Length,	/* q */
		    &elem.Flags,	/* l */
		    &elem.reserved);	/* l */
		if (rc) {
			status = NT_STATUS_INTERNAL_ERROR;
			break;
		}
		status = smb2_lock_elem(sr, &elem);
		if (status)
			break;
	}
	return (status);
}

static uint32_t
smb2_lock_elem(smb_request_t *sr, struct SMB2_LOCK_ELEMENT *elem)
{
	smb_node_t *node = sr->fid_ofile->f_node;
	uint32_t status;
	uint32_t ltype;
	uint32_t timeout = 0;

	switch (elem->Flags) {
	case SMB2_LOCKFLAG_SHARED_LOCK:
		timeout = UINT_MAX;
		/* FALLTHROUGH */
	case SMB2_LOCKFLAG_SHARED_LOCK | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
		ltype = SMB_LOCK_TYPE_READONLY;
		status = smb_lock_range(sr,
		    elem->Offset, elem->Length,
		    timeout, ltype);
		break;

	case SMB2_LOCKFLAG_EXCLUSIVE_LOCK:
		timeout = UINT_MAX;
		/* FALLTHROUGH */
	case SMB2_LOCKFLAG_EXCLUSIVE_LOCK | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
		ltype = SMB_LOCK_TYPE_READWRITE;
		status = smb_lock_range(sr,
		    elem->Offset, elem->Length,
		    timeout, ltype);
		break;

	case SMB2_LOCKFLAG_UNLOCK:
		status = smb_unlock_range(sr, node,
		    elem->Offset, elem->Length);
		break;

	/*
	 * We've already checked the flags previously, so any
	 * surprises here are some kind of internal error.
	 */
	default:
		status = NT_STATUS_INTERNAL_ERROR;
		break;
	}

	return (status);
}