summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb2_lease.c
blob: b8bed20f91fd1850ffc424fe979439775b6dfb1a (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
 * 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 2021 Tintri by DDN, Inc.  All rights reserved.
 * Copyright 2021 RackTop Systems, Inc.
 */

/*
 * Dispatch function for SMB2_OPLOCK_BREAK
 */

#include <smbsrv/smb2_kproto.h>
#include <smbsrv/smb_oplock.h>

/* StructSize for the two "break" message formats. */
#define	SSZ_OPLOCK	24
#define	SSZ_LEASE_ACK	36
#define	SSZ_LEASE_BRK	44

#define	NODE_FLAGS_DELETING	(NODE_FLAGS_DELETE_ON_CLOSE |\
				NODE_FLAGS_DELETE_COMMITTED)

static const char lease_zero[UUID_LEN] = { 0 };

static kmem_cache_t	*smb_lease_cache = NULL;

void
smb2_lease_init()
{
	if (smb_lease_cache != NULL)
		return;

	smb_lease_cache = kmem_cache_create("smb_lease_cache",
	    sizeof (smb_lease_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
}

void
smb2_lease_fini()
{
	if (smb_lease_cache != NULL) {
		kmem_cache_destroy(smb_lease_cache);
		smb_lease_cache = NULL;
	}
}

static void
smb2_lease_hold(smb_lease_t *ls)
{
	mutex_enter(&ls->ls_mutex);
	ls->ls_refcnt++;
	mutex_exit(&ls->ls_mutex);
}

void
smb2_lease_rele(smb_lease_t *ls)
{
	smb_llist_t *bucket;

	mutex_enter(&ls->ls_mutex);
	ls->ls_refcnt--;
	if (ls->ls_refcnt != 0) {
		mutex_exit(&ls->ls_mutex);
		return;
	}
	mutex_exit(&ls->ls_mutex);

	/*
	 * Get the list lock, then re-check the refcnt
	 * and if it's still zero, unlink & destroy.
	 */
	bucket = ls->ls_bucket;
	smb_llist_enter(bucket, RW_WRITER);

	mutex_enter(&ls->ls_mutex);
	if (ls->ls_refcnt == 0)
		smb_llist_remove(bucket, ls);
	mutex_exit(&ls->ls_mutex);

	if (ls->ls_refcnt == 0) {
		mutex_destroy(&ls->ls_mutex);
		kmem_cache_free(smb_lease_cache, ls);
	}

	smb_llist_exit(bucket);
}

/*
 * Compute a hash from a uuid
 * Based on mod_hash_bystr()
 */
static uint_t
smb_hash_uuid(const uint8_t *uuid)
{
	char *k = (char *)uuid;
	uint_t hash = 0;
	uint_t g;
	int i;

	ASSERT(k);
	for (i = 0; i < UUID_LEN; i++) {
		hash = (hash << 4) + k[i];
		if ((g = (hash & 0xf0000000)) != 0) {
			hash ^= (g >> 24);
			hash ^= g;
		}
	}
	return (hash);
}

/*
 * Add or update a lease table entry for a new ofile.
 * (in the per-session lease table)
 * See [MS-SMB2] 3.3.5.9.8
 * Handling the SMB2_CREATE_REQUEST_LEASE Create Context
 */
uint32_t
smb2_lease_create(smb_request_t *sr, uint8_t *clnt)
{
	smb_arg_open_t *op = &sr->arg.open;
	uint8_t *key = op->lease_key;
	smb_ofile_t *of = sr->fid_ofile;
	smb_hash_t *ht = sr->sr_server->sv_lease_ht;
	smb_llist_t *bucket;
	smb_lease_t *lease;
	smb_lease_t *newlease;
	size_t hashkey;
	uint32_t status = NT_STATUS_INVALID_PARAMETER;

	if (bcmp(key, lease_zero, UUID_LEN) == 0)
		return (status);

	/*
	 * Find or create, and add a ref for the new ofile.
	 */
	hashkey = smb_hash_uuid(key);
	hashkey &= (ht->num_buckets - 1);
	bucket = &ht->buckets[hashkey].b_list;

	newlease = kmem_cache_alloc(smb_lease_cache, KM_SLEEP);
	bzero(newlease, sizeof (smb_lease_t));
	mutex_init(&newlease->ls_mutex, NULL, MUTEX_DEFAULT, NULL);
	newlease->ls_bucket = bucket;
	newlease->ls_node = of->f_node;
	newlease->ls_refcnt = 1;
	newlease->ls_epoch = op->lease_epoch;
	newlease->ls_version = op->lease_version;
	bcopy(key, newlease->ls_key, UUID_LEN);
	bcopy(clnt, newlease->ls_clnt, UUID_LEN);

	smb_llist_enter(bucket, RW_WRITER);
	for (lease = smb_llist_head(bucket); lease != NULL;
	    lease = smb_llist_next(bucket, lease)) {
		/*
		 * Looking for this lease ID, on a node
		 * that's not being deleted.
		 */
		if (bcmp(lease->ls_key, key, UUID_LEN) == 0 &&
		    bcmp(lease->ls_clnt, clnt, UUID_LEN) == 0 &&
		    (lease->ls_node->flags & NODE_FLAGS_DELETING) == 0)
			break;
	}
	if (lease != NULL) {
		/*
		 * Found existing lease.  Make sure it refers to
		 * the same node...
		 */
		if (lease->ls_node == of->f_node) {
			smb2_lease_hold(lease);
		} else {
			/* Same lease ID, different node! */
#ifdef DEBUG
			cmn_err(CE_NOTE, "new lease on node %p (%s) "
			    "conflicts with existing node %p (%s)",
			    (void *) of->f_node,
			    of->f_node->od_name,
			    (void *) lease->ls_node,
			    lease->ls_node->od_name);
#endif
			DTRACE_PROBE2(dup_lease, smb_request_t, sr,
			    smb_lease_t, lease);
			lease = NULL; /* error */
		}
	} else {
		lease = newlease;
		smb_llist_insert_head(bucket, lease);
		newlease = NULL; /* don't free */
	}
	smb_llist_exit(bucket);

	if (newlease != NULL) {
		mutex_destroy(&newlease->ls_mutex);
		kmem_cache_free(smb_lease_cache, newlease);
	}

	if (lease != NULL) {
		of->f_lease = lease;
		status = NT_STATUS_SUCCESS;
	}

	return (status);
}

/*
 * Find the lease for a given: client_uuid, lease_key
 * Returns the lease with a new ref.
 */
smb_lease_t *
smb2_lease_lookup(smb_server_t *sv, uint8_t *clnt_uuid, uint8_t *lease_key)
{
	smb_hash_t *ht = sv->sv_lease_ht;
	smb_llist_t *bucket;
	smb_lease_t *lease;
	size_t hashkey;

	hashkey = smb_hash_uuid(lease_key);
	hashkey &= (ht->num_buckets - 1);
	bucket = &ht->buckets[hashkey].b_list;

	smb_llist_enter(bucket, RW_READER);
	lease = smb_llist_head(bucket);
	while (lease != NULL) {
		if (bcmp(lease->ls_key, lease_key, UUID_LEN) == 0 &&
		    bcmp(lease->ls_clnt, clnt_uuid, UUID_LEN) == 0) {
			smb2_lease_hold(lease);
			break;
		}
		lease = smb_llist_next(bucket, lease);
	}
	smb_llist_exit(bucket);

	return (lease);
}

/*
 * Find an smb_ofile_t in the current tree that shares the
 * specified lease and holds the oplock for the lease.
 * If lease not found, NT_STATUS_OBJECT_NAME_NOT_FOUND.
 * If no ofile (on the lease) holds the oplock, NT_STATUS_UNSUCCESSFUL.
 * On success, ofile (held) in sr->fid_ofile.
 */
static uint32_t
find_oplock_ofile(smb_request_t *sr, uint8_t *lease_key)
{
	smb_tree_t	*tree = sr->tid_tree;
	smb_lease_t	*lease;
	smb_llist_t	*of_list;
	smb_ofile_t	*o;
	uint32_t	status = NT_STATUS_OBJECT_NAME_NOT_FOUND;

	SMB_TREE_VALID(tree);
	of_list = &tree->t_ofile_list;

	smb_llist_enter(of_list, RW_READER);
	for (o = smb_llist_head(of_list); o != NULL;
	    o = smb_llist_next(of_list, o)) {

		ASSERT(o->f_magic == SMB_OFILE_MAGIC);
		ASSERT(o->f_tree == tree);

		DTRACE_PROBE1(every_ofile, smb_ofile_t *, o);
		if ((lease = o->f_lease) == NULL)
			continue; // no lease

		if (bcmp(lease->ls_key, lease_key, UUID_LEN) != 0)
			continue; // wrong lease

		/*
		 * Now we know the lease exists, so if we don't
		 * find the ofile that has the oplock, return:
		 */
		status = NT_STATUS_UNSUCCESSFUL;

		DTRACE_PROBE2(lease_ofile, smb_lease_t *, lease,
		    smb_ofile_t *, o);
		if (lease->ls_oplock_ofile != o)
			continue; // not oplock holder

		/* Found the ofile holding the oplock */
		if (smb_ofile_hold(o)) {
			sr->fid_ofile = o;
			status = NT_STATUS_SUCCESS;
			break;
		}
	}
	smb_llist_exit(of_list);

	return (status);
}

/*
 * This is called by smb2_oplock_break_ack when the struct size
 * indicates this is a lease break (SZ_LEASE).  See:
 * [MS-SMB2] 3.3.5.22.2 Processing a Lease Acknowledgment
 */
smb_sdrc_t
smb2_lease_break_ack(smb_request_t *sr)
{
	smb_lease_t *lease;
	smb_ofile_t *ofile;
	uint8_t LeaseKey[UUID_LEN];
	uint32_t LeaseState;
	uint32_t LeaseBreakTo;
	uint32_t status;
	int rc = 0;

	if (sr->session->dialect < SMB_VERS_2_1)
		return (SDRC_ERROR);

	/*
	 * Decode an SMB2 Lease Acknowldgement
	 * [MS-SMB2] 2.2.24.2
	 * Note: Struct size decoded by caller.
	 */
	rc = smb_mbc_decodef(
	    &sr->smb_data, "6.#cl8.",
	    /* reserved		  6. */
	    UUID_LEN,		/* # */
	    LeaseKey,		/* c */
	    &LeaseState);	/* l */
	    /* duration		  8. */
	if (rc != 0)
		return (SDRC_ERROR);

	status = find_oplock_ofile(sr, LeaseKey);

	DTRACE_SMB2_START(op__OplockBreak, smb_request_t *, sr);
	if (status != 0)
		goto errout;

	/* Success, so have sr->fid_ofile and lease */
	ofile = sr->fid_ofile;
	lease = ofile->f_lease;

	/* Either this ACK is unsolicited, or we timed out waiting. */
	if (lease->ls_breaking == 0) {
		status = NT_STATUS_UNSUCCESSFUL;
		goto errout;
	}

	/*
	 * Process the lease break ack.
	 *
	 * If the new LeaseState has any bits in excess of
	 * the lease state we sent in the break, error...
	 */
	LeaseBreakTo = (lease->ls_breaking >> BREAK_SHIFT) &
	    OPLOCK_LEVEL_CACHE_MASK;
	if ((LeaseState & ~LeaseBreakTo) != 0) {
		status = NT_STATUS_REQUEST_NOT_ACCEPTED;
		goto errout;
	}

	lease->ls_breaking = 0;

	LeaseState |= OPLOCK_LEVEL_GRANULAR;
	status = smb_oplock_ack_break(sr, ofile, &LeaseState);
	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
		(void) smb2sr_go_async(sr);
		(void) smb_oplock_wait_break(sr, ofile->f_node, 0);
		status = NT_STATUS_SUCCESS;
	}

	lease->ls_state = LeaseState & OPLOCK_LEVEL_CACHE_MASK;

errout:
	sr->smb2_status = status;
	DTRACE_SMB2_DONE(op__OplockBreak, smb_request_t *, sr);
	if (status) {
		smb2sr_put_error(sr, status);
		return (SDRC_SUCCESS);
	}

	/*
	 * Encode an SMB2 Lease Ack. response
	 * [MS-SMB2] 2.2.25.2
	 */
	LeaseState &= OPLOCK_LEVEL_CACHE_MASK;
	(void) smb_mbc_encodef(
	    &sr->reply, "w6.#cl8.",
	    SSZ_LEASE_ACK,	/* w */
	    /* reserved		  6. */
	    UUID_LEN,		/* # */
	    LeaseKey,		/* c */
	    LeaseState);	/* l */
	    /* duration		  8. */

	return (SDRC_SUCCESS);

}

/*
 * Compose an SMB2 Lease Break Notification packet, including
 * the SMB2 header and everything, in sr->reply.
 * The caller will send it and free the request.
 *
 * [MS-SMB2] 2.2.23.2 Lease Break Notification
 */
void
smb2_lease_break_notification(smb_request_t *sr, uint32_t NewLevel,
    boolean_t AckReq)
{
	smb_lease_t *ls = sr->fid_ofile->f_lease;
	uint32_t oldcache;
	uint32_t newcache;
	uint16_t Epoch;
	uint16_t Flags;

	/*
	 * Convert internal level to SMB2
	 */
	oldcache = ls->ls_state & OPLOCK_LEVEL_CACHE_MASK;
	newcache = NewLevel & OPLOCK_LEVEL_CACHE_MASK;
	if (ls->ls_version < 2)
		Epoch = 0;
	else
		Epoch = ls->ls_epoch;

	/*
	 * SMB2 Header
	 */
	sr->smb2_cmd_code = SMB2_OPLOCK_BREAK;
	sr->smb2_hdr_flags = SMB2_FLAGS_SERVER_TO_REDIR;
	sr->smb_tid = 0;
	sr->smb_pid = 0;
	sr->smb2_ssnid = 0;
	sr->smb2_messageid = UINT64_MAX;
	(void) smb2_encode_header(sr, B_FALSE);

	/*
	 * SMB2 Oplock Break, variable part
	 *
	 * [MS-SMB2] says the current lease state preceeds the
	 * new lease state, but that looks like an error...
	 */
	Flags = AckReq ? SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED : 0;
	(void) smb_mbc_encodef(
	    &sr->reply, "wwl#cll4.4.4.",
	    SSZ_LEASE_BRK,		/* w */
	    Epoch,			/* w */
	    Flags,			/* l */
	    SMB_LEASE_KEY_SZ,		/* # */
	    ls->ls_key,			/* c */
	    oldcache,		/* cur.st  l */
	    newcache);		/* new.st  l */
	    /* reserved (4.4.4.) */
}

/*
 * Client has an open handle and requests a lease.
 * Convert SMB2 lease request info in to internal form,
 * call common oplock code, convert result to SMB2.
 *
 * If necessary, "go async" here.
 */
void
smb2_lease_acquire(smb_request_t *sr)
{
	smb_arg_open_t *op = &sr->arg.open;
	smb_ofile_t *ofile = sr->fid_ofile;
	smb_lease_t *lease = ofile->f_lease;
	uint32_t status = NT_STATUS_OPLOCK_NOT_GRANTED;
	uint32_t have, want; /* lease flags */
	boolean_t NewGrant = B_FALSE;

	/* Only disk trees get oplocks. */
	ASSERT((sr->tid_tree->t_res_type & STYPE_MASK) == STYPE_DISKTREE);

	/*
	 * Only plain files (for now).
	 * Later, test SMB2_CAP_DIRECTORY_LEASING
	 */
	if (!smb_node_is_file(ofile->f_node)) {
		op->op_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
		return;
	}

	if (!smb_tree_has_feature(sr->tid_tree, SMB_TREE_OPLOCKS)) {
		op->op_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
		return;
	}

	/*
	 * SMB2: Convert to internal form.
	 * Caller should have setup the lease.
	 */
	ASSERT(op->op_oplock_level == SMB2_OPLOCK_LEVEL_LEASE);
	ASSERT(lease != NULL);
	if (lease == NULL) {
		op->op_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
		return;
	}
	op->op_oplock_state = OPLOCK_LEVEL_GRANULAR |
	    (op->lease_state & CACHE_RWH);

	/*
	 * Tree options may force shared oplocks,
	 * in which case we reduce the request.
	 */
	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_FORCE_L2_OPLOCK)) {
		op->op_oplock_state &= ~WRITE_CACHING;
	}

	/*
	 * Disallow downgrade
	 *
	 * Note that open with a lease is not allowed to turn off
	 * any cache rights.  If the client tries to "downgrade",
	 * any bits, just return the existing lease cache bits.
	 */
	have = lease->ls_state & CACHE_RWH;
	want = op->op_oplock_state & CACHE_RWH;
	if ((have & ~want) != 0) {
		op->op_oplock_state = have |
		    OPLOCK_LEVEL_GRANULAR;
		goto done;
	}

	/*
	 * Handle oplock requests in three parts:
	 *	a: Requests with WRITE_CACHING
	 *	b: Requests with HANDLE_CACHING
	 *	c: Requests with READ_CACHING
	 * reducing the request before b and c.
	 *
	 * In each: first check if the lease grants the
	 * (possibly reduced) request, in which case we
	 * leave the lease unchanged and return what's
	 * granted by the lease.  Otherwise, try to get
	 * the oplock, and if the succeeds, wait for any
	 * breaks, update the lease, and return.
	 */

	/*
	 * Try exclusive (request is RW or RWH)
	 */
	if ((op->op_oplock_state & WRITE_CACHING) != 0) {
		want = op->op_oplock_state & CACHE_RWH;
		if (have == want)
			goto done;

		status = smb_oplock_request(sr, ofile,
		    &op->op_oplock_state);
		if (status == NT_STATUS_SUCCESS ||
		    status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
			NewGrant = B_TRUE;
			goto done;
		}

		/*
		 * We did not get the exclusive oplock.
		 *
		 * There are odd rules about lease upgrade.
		 * If the existing lease grants R and the
		 * client fails to upgrade it to "RWH"
		 * (presumably due to handle conflicts)
		 * then just return the existing lease,
		 * even though upgrade to RH would work.
		 */
		if (have != 0) {
			op->op_oplock_state = have |
			    OPLOCK_LEVEL_GRANULAR;
			goto done;
		}

		/*
		 * Keep trying without write.
		 * Need to re-init op_oplock_state
		 */
		op->op_oplock_state = OPLOCK_LEVEL_GRANULAR |
		    (op->lease_state & CACHE_RH);
	}

	/*
	 * Try shared ("RH")
	 */
	if ((op->op_oplock_state & HANDLE_CACHING) != 0) {
		want = op->op_oplock_state & CACHE_RWH;
		if (have == want)
			goto done;

		status = smb_oplock_request(sr, ofile,
		    &op->op_oplock_state);
		if (status == NT_STATUS_SUCCESS ||
		    status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
			NewGrant = B_TRUE;
			goto done;
		}

		/*
		 * We did not get "RH", probably because
		 * ther were (old style) Level II oplocks.
		 * Continue, try for just read.
		 */
		op->op_oplock_state = OPLOCK_LEVEL_GRANULAR |
		    (op->lease_state & CACHE_R);
	}

	/*
	 * Try shared ("R")
	 */
	if ((op->op_oplock_state & READ_CACHING) != 0) {
		want = op->op_oplock_state & CACHE_RWH;
		if (have == want)
			goto done;

		status = smb_oplock_request(sr, ofile,
		    &op->op_oplock_state);
		if (status == NT_STATUS_SUCCESS ||
		    status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
			NewGrant = B_TRUE;
			goto done;
		}

		/*
		 * We did not get "R".
		 * Fall into "none".
		 */
	}

	/*
	 * None of the above were able to get an oplock.
	 * The lease has no caching rights, and we didn't
	 * add any in this request.  Return it as-is.
	 */
	op->op_oplock_state = OPLOCK_LEVEL_GRANULAR;

done:
	if (NewGrant) {
		/*
		 * After a new oplock grant, the status return
		 * may indicate we need to wait for breaks.
		 */
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
			(void) smb2sr_go_async(sr);
			(void) smb_oplock_wait_break(sr, ofile->f_node, 0);
			status = NT_STATUS_SUCCESS;
		}
		ASSERT(status == NT_STATUS_SUCCESS);

		/*
		 * Keep track of what we got (in lease->ls_state)
		 * so we'll know what we last told the client.
		 */
		mutex_enter(&lease->ls_mutex);
		lease->ls_state = op->op_oplock_state & CACHE_RWH;
		lease->ls_epoch++;
		mutex_exit(&lease->ls_mutex);
	}

	/*
	 * Convert internal oplock state to SMB2
	 */
	op->op_oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
	op->lease_state = lease->ls_state & CACHE_RWH;
	op->lease_flags = (lease->ls_breaking != 0) ?
	    SMB2_LEASE_FLAG_BREAK_IN_PROGRESS : 0;
	op->lease_epoch = lease->ls_epoch;
	op->lease_version = lease->ls_version;
}

/*
 * This ofile has a lease and is about to close.
 * Called by smb_ofile_close when there's a lease.
 *
 * With leases, just one ofile on a lease owns the oplock.
 * If an ofile with a lease is closed and it's the one that
 * owns the oplock, try to move the oplock to another ofile
 * on the same lease.
 */
void
smb2_lease_ofile_close(smb_ofile_t *ofile)
{
	smb_node_t *node = ofile->f_node;
	smb_lease_t *lease = ofile->f_lease;
	smb_ofile_t *o;

	ASSERT(RW_READ_HELD(&node->n_ofile_list.ll_lock));
	ASSERT(MUTEX_HELD(&node->n_oplock.ol_mutex));

	/*
	 * If this ofile was not the oplock owner for this lease,
	 * we can leave things as they are.
	 */
	if (lease->ls_oplock_ofile != ofile)
		return;

	/*
	 * Find another ofile to which we can move the oplock.
	 * The ofile must be open and allow a new ref.
	 */
	FOREACH_NODE_OFILE(node, o) {
		if (o == ofile)
			continue;
		if (o->f_lease != lease)
			continue;
		if (o->f_oplock_closing)
			continue;
		/* If we can get a hold, use this ofile. */
		if (smb_ofile_hold(o))
			break;
	}
	if (o == NULL) {
		/* Normal for last close on a lease. */
		lease->ls_oplock_ofile = NULL;
		return;
	}
	smb_oplock_move(node, ofile, o);
	lease->ls_oplock_ofile = o;

	smb_ofile_release(o);
}