summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_oplock.c
blob: 20fdfd10dc9b7be1d99ad55422ca30f36d15671e (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * SMB Locking library functions.
 *
 * You will notice that the functions in this file exit the lock of the session
 * and reenter it before returning. They even assume that the lock has been
 * entered in READER mode. The reason for that is a potential deadlock that may
 * occur when an oplock needs to be broken and the function
 * smb_session_break_oplock() is called. It should be noticed that the mutex of
 * the smb node, the oplock of which needs to be broken, is also exited before
 * calling smb_session_break_oplock(). The reason for that is the same: avoiding
 * a deadlock. That complexity is due to the fact that the lock of the session
 * is held during the treatment of a request. That complexity will go away when
 * that is not the case anymore.
 */

#include <smbsrv/smb_incl.h>
#include <smbsrv/smb_fsops.h>
#include <inet/tcp.h>

static void smb_oplock_wait(smb_node_t *);

/*
 *	Magic		0xFF 'S' 'M' 'B'
 *	smb_com 	a byte, the "first" command
 *	Error		a 4-byte union, ignored in a request
 *	smb_flg		a one byte set of eight flags
 *	smb_flg2	a two byte set of 16 flags
 *	.		twelve reserved bytes, have a role
 *			in connectionless transports (IPX, UDP?)
 *	smb_tid		a 16-bit tree ID, a mount point sorta,
 *			0xFFFF is this command does not have
 *			or require a tree context
 *	smb_pid		a 16-bit process ID
 *	smb_uid		a 16-bit user ID, specific to this "session"
 *			and mapped to a system (bona-fide) UID
 *	smb_mid		a 16-bit multiplex ID, used to differentiate
 *			multiple simultaneous requests from the same
 *			process (pid) (ref RPC "xid")
 *
 * SMB_COM_LOCKING_ANDX allows both locking and/or unlocking of file range(s).
 *
 *  Client Request                     Description
 *  ================================== =================================
 *
 *  UCHAR WordCount;                   Count of parameter words = 8
 *  UCHAR AndXCommand;                 Secondary (X) command;  0xFF = none
 *  UCHAR AndXReserved;                Reserved (must be 0)
 *  USHORT AndXOffset;                 Offset to next command WordCount
 *  USHORT Fid;                        File handle
 *  UCHAR LockType;                    See LockType table below
 *  UCHAR OplockLevel;                 The new oplock level
 *  ULONG Timeout;                     Milliseconds to wait for unlock
 *  USHORT NumberOfUnlocks;            Num. unlock range structs following
 *  USHORT NumberOfLocks;              Num. lock range structs following
 *  USHORT ByteCount;                  Count of data bytes
 *  LOCKING_ANDX_RANGE Unlocks[];      Unlock ranges
 *  LOCKING_ANDX_RANGE Locks[];        Lock ranges
 *
 *  LockType Flag Name            Value Description
 *  ============================  ===== ================================
 *
 *  LOCKING_ANDX_SHARED_LOCK      0x01  Read-only lock
 *  LOCKING_ANDX_OPLOCK_RELEASE   0x02  Oplock break notification
 *  LOCKING_ANDX_CHANGE_LOCKTYPE  0x04  Change lock type
 *  LOCKING_ANDX_CANCEL_LOCK      0x08  Cancel outstanding request
 *  LOCKING_ANDX_LARGE_FILES      0x10  Large file locking format
 *
 *  LOCKING_ANDX_RANGE Format
 *  =====================================================================
 *
 *  USHORT Pid;                        PID of process "owning" lock
 *  ULONG Offset;                      Offset to bytes to [un]lock
 *  ULONG Length;                      Number of bytes to [un]lock
 *
 *  Large File LOCKING_ANDX_RANGE Format
 *  =====================================================================
 *
 *  USHORT Pid;                        PID of process "owning" lock
 *  USHORT Pad;                        Pad to DWORD align (mbz)
 *  ULONG OffsetHigh;                  Offset to bytes to [un]lock
 *                                      (high)
 *  ULONG OffsetLow;                   Offset to bytes to [un]lock (low)
 *  ULONG LengthHigh;                  Number of bytes to [un]lock
 *                                      (high)
 *  ULONG LengthLow;                   Number of bytes to [un]lock (low)
 *
 *  Server Response                    Description
 *  ================================== =================================
 *
 *  UCHAR WordCount;                   Count of parameter words = 2
 *  UCHAR AndXCommand;                 Secondary (X) command;  0xFF =
 *                                      none
 *  UCHAR AndXReserved;                Reserved (must be 0)
 *  USHORT AndXOffset;                 Offset to next command WordCount
 *  USHORT ByteCount;                  Count of data bytes = 0
 *
 */

/*
 * smb_oplock_acquire
 *
 * Attempt to acquire an oplock. Note that the oplock granted may be
 * none, i.e. the oplock was not granted. The result of the acquisition is
 * provided in ol->ol_level.
 *
 * Grant an oplock to the requestor if this session is the only one
 * that has the file open, regardless of the number of instances of
 * the file opened by this session.
 *
 * However, if there is no oplock on this file and there is already
 * at least one open, we will not grant an oplock, even if the only
 * existing opens are from the same client.  This is "server discretion."
 *
 * An oplock may need to be broken in order for one to be granted, and
 * depending on what action is taken by the other client (unlock or close),
 * an oplock may or may not be granted.  (The breaking of an oplock is
 * done earlier in the calling path.)
 */
void
smb_oplock_acquire(smb_node_t *node, smb_ofile_t *of, open_param_t *op)
{
	smb_session_t	*session;
	smb_oplock_t	*ol;
	clock_t		time;

	SMB_NODE_VALID(node);
	SMB_OFILE_VALID(of);

	ASSERT(node == SMB_OFILE_GET_NODE(of));

	session = SMB_OFILE_GET_SESSION(of);

	if (!smb_session_oplocks_enable(session) ||
	    smb_tree_has_feature(SMB_OFILE_GET_TREE(of), SMB_TREE_NO_OPLOCKS)) {
		op->op_oplock_level = SMB_OPLOCK_NONE;
		return;
	}

	ol = &node->n_oplock;
	time = MSEC_TO_TICK(smb_oplock_timeout) + ddi_get_lbolt();

	mutex_enter(&node->n_mutex);

	switch (node->n_state) {
	case SMB_NODE_STATE_OPLOCK_GRANTED:
		if (SMB_SESSION_GET_ID(session) == ol->ol_sess_id) {
			mutex_exit(&node->n_mutex);
			return;
		}
		break;
	case SMB_NODE_STATE_AVAILABLE:
	case SMB_NODE_STATE_OPLOCK_BREAKING:
		break;
	default:
		SMB_PANIC();
	}

	for (;;) {
		int	rc;

		smb_oplock_wait(node);

		if (node->n_state == SMB_NODE_STATE_AVAILABLE) {
			if ((op->op_oplock_level == SMB_OPLOCK_LEVEL_II) ||
			    (op->op_oplock_level == SMB_OPLOCK_NONE) ||
			    (node->n_open_count > 1)) {
				mutex_exit(&node->n_mutex);
				op->op_oplock_level = SMB_OPLOCK_NONE;
				return;
			}
			ol->ol_ofile = of;
			ol->ol_sess_id = SMB_SESSION_GET_ID(session);
			ol->ol_level = op->op_oplock_level;
			ol->ol_xthread = curthread;
			node->n_state = SMB_NODE_STATE_OPLOCK_GRANTED;
			mutex_exit(&node->n_mutex);
			if (smb_fsop_oplock_install(node, of->f_mode) == 0) {
				smb_ofile_set_oplock_granted(of);
				return;
			}
			mutex_enter(&node->n_mutex);
			ASSERT(node->n_state == SMB_NODE_STATE_OPLOCK_GRANTED);
			node->n_state = SMB_NODE_STATE_AVAILABLE;
			ol->ol_xthread = NULL;
			op->op_oplock_level = SMB_OPLOCK_NONE;
			cv_broadcast(&ol->ol_cv);
			break;
		}

		if (node->n_state == SMB_NODE_STATE_OPLOCK_GRANTED) {
			if (SMB_SESSION_GET_ID(session) == ol->ol_sess_id)
				break;
			node->n_state = SMB_NODE_STATE_OPLOCK_BREAKING;
			mutex_exit(&node->n_mutex);
			smb_session_oplock_break(
			    SMB_OFILE_GET_SESSION(ol->ol_ofile), ol->ol_ofile);
			mutex_enter(&node->n_mutex);
			continue;
		}

		ASSERT(node->n_state == SMB_NODE_STATE_OPLOCK_BREAKING);

		rc = cv_timedwait(&ol->ol_cv, &node->n_mutex, time);

		if (rc == -1) {
			/*
			 * Oplock release timed out.
			 */
			if (node->n_state == SMB_NODE_STATE_OPLOCK_BREAKING) {
				node->n_state = SMB_NODE_STATE_AVAILABLE;
				ol->ol_xthread = curthread;
				mutex_exit(&node->n_mutex);
				smb_fsop_oplock_uninstall(node);
				mutex_enter(&node->n_mutex);
				ol->ol_xthread = NULL;
				cv_broadcast(&ol->ol_cv);
			}
		}
	}
	mutex_exit(&node->n_mutex);
}

/*
 * smb_oplock_break
 *
 * The oplock break may succeed for multiple reasons: file close, oplock
 * release, holder connection dropped, requesting client disconnect etc.
 *
 * Returns:
 *
 *	B_TRUE	The oplock is broken.
 *	B_FALSE	The oplock is being broken. This is returned if nowait is set
 *		to B_TRUE;
 */
boolean_t
smb_oplock_break(smb_node_t *node, smb_session_t *session, boolean_t nowait)
{
	smb_oplock_t	*ol;
	clock_t		time;

	SMB_NODE_VALID(node);
	ol = &node->n_oplock;
	time = MSEC_TO_TICK(smb_oplock_timeout) + ddi_get_lbolt();

	if (session != NULL) {
		mutex_enter(&node->n_mutex);
		if (SMB_SESSION_GET_ID(session) == ol->ol_sess_id) {
			mutex_exit(&node->n_mutex);
			return (B_TRUE);
		}
	} else {
		mutex_enter(&node->n_mutex);
	}

	for (;;) {
		int	rc;

		smb_oplock_wait(node);

		if (node->n_state == SMB_NODE_STATE_AVAILABLE) {
			mutex_exit(&node->n_mutex);
			return (B_TRUE);
		}

		if (node->n_state == SMB_NODE_STATE_OPLOCK_GRANTED) {
			node->n_state = SMB_NODE_STATE_OPLOCK_BREAKING;
			mutex_exit(&node->n_mutex);
			smb_session_oplock_break(
			    SMB_OFILE_GET_SESSION(ol->ol_ofile), ol->ol_ofile);
			mutex_enter(&node->n_mutex);
			continue;
		}

		ASSERT(node->n_state == SMB_NODE_STATE_OPLOCK_BREAKING);
		if (nowait) {
			mutex_exit(&node->n_mutex);
			return (B_FALSE);
		}
		rc = cv_timedwait(&ol->ol_cv, &node->n_mutex, time);
		if (rc == -1) {
			/*
			 * Oplock release timed out.
			 */
			if (node->n_state == SMB_NODE_STATE_OPLOCK_BREAKING) {
				node->n_state = SMB_NODE_STATE_AVAILABLE;
				ol->ol_xthread = curthread;
				mutex_exit(&node->n_mutex);
				smb_fsop_oplock_uninstall(node);
				mutex_enter(&node->n_mutex);
				ol->ol_xthread = NULL;
				cv_broadcast(&ol->ol_cv);
				break;
			}
		}
	}
	mutex_exit(&node->n_mutex);
	return (B_TRUE);
}

/*
 * smb_oplock_release
 *
 * This function releases the oplock on the node passed in. If other threads
 * were waiting for the oplock to be released they are signaled.
 */
void
smb_oplock_release(smb_node_t *node, smb_ofile_t *of)
{
	smb_oplock_t	*ol;

	SMB_NODE_VALID(node);
	ol = &node->n_oplock;

	mutex_enter(&node->n_mutex);
	smb_oplock_wait(node);
	switch (node->n_state) {
	case SMB_NODE_STATE_AVAILABLE:
		break;

	case SMB_NODE_STATE_OPLOCK_GRANTED:
	case SMB_NODE_STATE_OPLOCK_BREAKING:
		if (ol->ol_ofile == of) {
			node->n_state = SMB_NODE_STATE_AVAILABLE;
			ol->ol_xthread = curthread;
			mutex_exit(&node->n_mutex);
			smb_fsop_oplock_uninstall(node);
			mutex_enter(&node->n_mutex);
			ol->ol_xthread = NULL;
			cv_broadcast(&ol->ol_cv);
		}
		break;

	default:
		SMB_PANIC();
	}
	mutex_exit(&node->n_mutex);
}

/*
 * smb_oplock_conflict
 *
 * The two checks on "session" and "op" are primarily for the open path.
 * Other SMB functions may call smb_oplock_conflict() with a session
 * pointer so as to do the session check.
 */
boolean_t
smb_oplock_conflict(smb_node_t *node, smb_session_t *session, open_param_t *op)
{
	boolean_t	rb;

	SMB_NODE_VALID(node);
	SMB_SESSION_VALID(session);

	mutex_enter(&node->n_mutex);
	smb_oplock_wait(node);
	switch (node->n_state) {
	case SMB_NODE_STATE_AVAILABLE:
		rb = B_FALSE;
		break;

	case SMB_NODE_STATE_OPLOCK_GRANTED:
	case SMB_NODE_STATE_OPLOCK_BREAKING:
		if (SMB_SESSION_GET_ID(session) == node->n_oplock.ol_sess_id) {
			rb = B_FALSE;
			break;
		}

		if (op != NULL) {
			if (((op->desired_access & ~(FILE_READ_ATTRIBUTES |
			    FILE_WRITE_ATTRIBUTES | SYNCHRONIZE)) == 0) &&
			    (op->create_disposition != FILE_SUPERSEDE) &&
			    (op->create_disposition != FILE_OVERWRITE)) {
				/* Attributs only */
				rb = B_FALSE;
				break;
			}
		}
		rb = B_TRUE;
		break;

	default:
		SMB_PANIC();
	}
	mutex_exit(&node->n_mutex);
	return (rb);
}

/*
 * smb_oplock_broadcast
 *
 * The the calling thread has the pointer to its context stored in ol_thread
 * it resets that field. If any other thread is waiting for that field to
 * turn to NULL it is signaled.
 *
 * Returns:
 *	B_TRUE	Oplock unlocked
 *	B_FALSE	Oplock still locked
 */
boolean_t
smb_oplock_broadcast(smb_node_t *node)
{
	smb_oplock_t	*ol;
	boolean_t	rb;

	SMB_NODE_VALID(node);
	ol = &node->n_oplock;
	rb = B_FALSE;

	mutex_enter(&node->n_mutex);
	if ((ol->ol_xthread != NULL) && (ol->ol_xthread == curthread)) {
		ol->ol_xthread = NULL;
		cv_broadcast(&ol->ol_cv);
		rb = B_TRUE;
	}
	mutex_exit(&node->n_mutex);
	return (rb);
}

/*
 * smb_oplock_wait
 *
 * The mutex of the node must have been entered before calling this function.
 * If the field ol_xthread is not NULL and doesn't contain the pointer to the
 * context of the calling thread, the caller will sleep until that field is
 * reset (set to NULL).
 */
static void
smb_oplock_wait(smb_node_t *node)
{
	smb_oplock_t	*ol = &node->n_oplock;

	if ((ol->ol_xthread != NULL) && (ol->ol_xthread != curthread)) {
		ASSERT(!MUTEX_HELD(&ol->ol_ofile->f_mutex));
		while (ol->ol_xthread != NULL)
			cv_wait(&ol->ol_cv, &node->n_mutex);
	}
}