xref: /linux/fs/smb/server/oplock.c (revision 3f276cece4dd9e8bf199d9bf3901eef8ca904c2d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6 
7 #include <linux/moduleparam.h>
8 
9 #include "glob.h"
10 #include "oplock.h"
11 
12 #include "smb_common.h"
13 #include "../common/smb2status.h"
14 #include "connection.h"
15 #include "mgmt/user_session.h"
16 #include "mgmt/share_config.h"
17 #include "mgmt/tree_connect.h"
18 
19 static LIST_HEAD(lease_table_list);
20 static DEFINE_RWLOCK(lease_list_lock);
21 
22 /**
23  * alloc_opinfo() - allocate a new opinfo object for oplock info
24  * @work:	smb work
25  * @id:		fid of open file
26  * @Tid:	tree id of connection
27  *
28  * Return:      allocated opinfo object on success, otherwise NULL
29  */
30 static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,
31 					u64 id, __u16 Tid)
32 {
33 	struct ksmbd_conn *conn = work->conn;
34 	struct ksmbd_session *sess = work->sess;
35 	struct oplock_info *opinfo;
36 
37 	opinfo = kzalloc_obj(struct oplock_info, KSMBD_DEFAULT_GFP);
38 	if (!opinfo)
39 		return NULL;
40 
41 	opinfo->sess = sess;
42 	opinfo->conn = conn;
43 	opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
44 	opinfo->op_state = OPLOCK_STATE_NONE;
45 	opinfo->pending_break = 0;
46 	opinfo->fid = id;
47 	opinfo->Tid = Tid;
48 	INIT_LIST_HEAD(&opinfo->op_entry);
49 	init_waitqueue_head(&opinfo->oplock_q);
50 	init_waitqueue_head(&opinfo->oplock_brk);
51 	atomic_set(&opinfo->refcount, 1);
52 	atomic_set(&opinfo->breaking_cnt, 0);
53 	atomic_inc(&opinfo->conn->refcnt);
54 
55 	return opinfo;
56 }
57 
58 static void lease_add_list(struct oplock_info *opinfo)
59 {
60 	struct lease_table *lb = opinfo->o_lease->l_lb;
61 
62 	spin_lock(&lb->lb_lock);
63 	list_add_rcu(&opinfo->lease_entry, &lb->lease_list);
64 	spin_unlock(&lb->lb_lock);
65 }
66 
67 static void lease_del_list(struct oplock_info *opinfo)
68 {
69 	struct lease_table *lb = opinfo->o_lease->l_lb;
70 
71 	if (!lb)
72 		return;
73 
74 	spin_lock(&lb->lb_lock);
75 	if (list_empty(&opinfo->lease_entry)) {
76 		spin_unlock(&lb->lb_lock);
77 		return;
78 	}
79 
80 	list_del_init(&opinfo->lease_entry);
81 	opinfo->o_lease->l_lb = NULL;
82 	spin_unlock(&lb->lb_lock);
83 }
84 
85 static void lb_add(struct lease_table *lb)
86 {
87 	write_lock(&lease_list_lock);
88 	list_add(&lb->l_entry, &lease_table_list);
89 	write_unlock(&lease_list_lock);
90 }
91 
92 static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx)
93 {
94 	struct lease *lease;
95 
96 	lease = kmalloc_obj(struct lease, KSMBD_DEFAULT_GFP);
97 	if (!lease)
98 		return -ENOMEM;
99 
100 	memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
101 	lease->state = lctx->req_state;
102 	lease->new_state = 0;
103 	lease->flags = lctx->flags;
104 	lease->duration = lctx->duration;
105 	lease->is_dir = lctx->is_dir;
106 	memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE);
107 	lease->version = lctx->version;
108 	lease->epoch = le16_to_cpu(lctx->epoch) + 1;
109 	INIT_LIST_HEAD(&opinfo->lease_entry);
110 	opinfo->o_lease = lease;
111 
112 	return 0;
113 }
114 
115 static void free_lease(struct oplock_info *opinfo)
116 {
117 	struct lease *lease;
118 
119 	lease = opinfo->o_lease;
120 	kfree(lease);
121 }
122 
123 static void __free_opinfo(struct oplock_info *opinfo)
124 {
125 	if (opinfo->is_lease)
126 		free_lease(opinfo);
127 	if (opinfo->conn && atomic_dec_and_test(&opinfo->conn->refcnt))
128 		kfree(opinfo->conn);
129 	kfree(opinfo);
130 }
131 
132 static void free_opinfo_rcu(struct rcu_head *rcu)
133 {
134 	struct oplock_info *opinfo = container_of(rcu, struct oplock_info, rcu);
135 
136 	__free_opinfo(opinfo);
137 }
138 
139 static void free_opinfo(struct oplock_info *opinfo)
140 {
141 	call_rcu(&opinfo->rcu, free_opinfo_rcu);
142 }
143 
144 struct oplock_info *opinfo_get(struct ksmbd_file *fp)
145 {
146 	struct oplock_info *opinfo;
147 
148 	rcu_read_lock();
149 	opinfo = rcu_dereference(fp->f_opinfo);
150 	if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
151 		opinfo = NULL;
152 	rcu_read_unlock();
153 
154 	return opinfo;
155 }
156 
157 static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci)
158 {
159 	struct oplock_info *opinfo;
160 
161 	down_read(&ci->m_lock);
162 	opinfo = list_first_entry_or_null(&ci->m_op_list, struct oplock_info,
163 					  op_entry);
164 	if (opinfo) {
165 		if (opinfo->conn == NULL ||
166 		    !atomic_inc_not_zero(&opinfo->refcount))
167 			opinfo = NULL;
168 		else {
169 			if (ksmbd_conn_releasing(opinfo->conn)) {
170 				atomic_dec(&opinfo->refcount);
171 				opinfo = NULL;
172 			}
173 		}
174 	}
175 	up_read(&ci->m_lock);
176 
177 	return opinfo;
178 }
179 
180 void opinfo_put(struct oplock_info *opinfo)
181 {
182 	if (!opinfo)
183 		return;
184 
185 	if (!atomic_dec_and_test(&opinfo->refcount))
186 		return;
187 
188 	free_opinfo(opinfo);
189 }
190 
191 static void opinfo_add(struct oplock_info *opinfo, struct ksmbd_file *fp)
192 {
193 	struct ksmbd_inode *ci = fp->f_ci;
194 
195 	down_write(&ci->m_lock);
196 	list_add(&opinfo->op_entry, &ci->m_op_list);
197 	up_write(&ci->m_lock);
198 }
199 
200 static void opinfo_del(struct oplock_info *opinfo)
201 {
202 	struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
203 
204 	if (opinfo->is_lease) {
205 		write_lock(&lease_list_lock);
206 		lease_del_list(opinfo);
207 		write_unlock(&lease_list_lock);
208 	}
209 	down_write(&ci->m_lock);
210 	list_del(&opinfo->op_entry);
211 	up_write(&ci->m_lock);
212 }
213 
214 static unsigned long opinfo_count(struct ksmbd_file *fp)
215 {
216 	if (ksmbd_stream_fd(fp))
217 		return atomic_read(&fp->f_ci->sop_count);
218 	else
219 		return atomic_read(&fp->f_ci->op_count);
220 }
221 
222 static void opinfo_count_inc(struct ksmbd_file *fp)
223 {
224 	if (ksmbd_stream_fd(fp))
225 		return atomic_inc(&fp->f_ci->sop_count);
226 	else
227 		return atomic_inc(&fp->f_ci->op_count);
228 }
229 
230 static void opinfo_count_dec(struct ksmbd_file *fp)
231 {
232 	if (ksmbd_stream_fd(fp))
233 		return atomic_dec(&fp->f_ci->sop_count);
234 	else
235 		return atomic_dec(&fp->f_ci->op_count);
236 }
237 
238 /**
239  * opinfo_write_to_read() - convert a write oplock to read oplock
240  * @opinfo:		current oplock info
241  *
242  * Return:      0 on success, otherwise -EINVAL
243  */
244 int opinfo_write_to_read(struct oplock_info *opinfo)
245 {
246 	struct lease *lease = opinfo->o_lease;
247 
248 	if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
249 	      opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
250 		pr_err("bad oplock(0x%x)\n", opinfo->level);
251 		if (opinfo->is_lease)
252 			pr_err("lease state(0x%x)\n", lease->state);
253 		return -EINVAL;
254 	}
255 	opinfo->level = SMB2_OPLOCK_LEVEL_II;
256 
257 	if (opinfo->is_lease)
258 		lease->state = lease->new_state;
259 	return 0;
260 }
261 
262 /**
263  * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock
264  * @opinfo:		current oplock info
265  *
266  * Return:      0 on success, otherwise -EINVAL
267  */
268 int opinfo_read_handle_to_read(struct oplock_info *opinfo)
269 {
270 	struct lease *lease = opinfo->o_lease;
271 
272 	lease->state = lease->new_state;
273 	opinfo->level = SMB2_OPLOCK_LEVEL_II;
274 	return 0;
275 }
276 
277 /**
278  * opinfo_write_to_none() - convert a write oplock to none
279  * @opinfo:	current oplock info
280  *
281  * Return:      0 on success, otherwise -EINVAL
282  */
283 int opinfo_write_to_none(struct oplock_info *opinfo)
284 {
285 	struct lease *lease = opinfo->o_lease;
286 
287 	if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
288 	      opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
289 		pr_err("bad oplock(0x%x)\n", opinfo->level);
290 		if (opinfo->is_lease)
291 			pr_err("lease state(0x%x)\n", lease->state);
292 		return -EINVAL;
293 	}
294 	opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
295 	if (opinfo->is_lease)
296 		lease->state = lease->new_state;
297 	return 0;
298 }
299 
300 /**
301  * opinfo_read_to_none() - convert a write read to none
302  * @opinfo:	current oplock info
303  *
304  * Return:      0 on success, otherwise -EINVAL
305  */
306 int opinfo_read_to_none(struct oplock_info *opinfo)
307 {
308 	struct lease *lease = opinfo->o_lease;
309 
310 	if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
311 		pr_err("bad oplock(0x%x)\n", opinfo->level);
312 		if (opinfo->is_lease)
313 			pr_err("lease state(0x%x)\n", lease->state);
314 		return -EINVAL;
315 	}
316 	opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
317 	if (opinfo->is_lease)
318 		lease->state = lease->new_state;
319 	return 0;
320 }
321 
322 /**
323  * lease_read_to_write() - upgrade lease state from read to write
324  * @opinfo:	current lease info
325  *
326  * Return:      0 on success, otherwise -EINVAL
327  */
328 int lease_read_to_write(struct oplock_info *opinfo)
329 {
330 	struct lease *lease = opinfo->o_lease;
331 
332 	if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) {
333 		ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
334 		return -EINVAL;
335 	}
336 
337 	lease->new_state = SMB2_LEASE_NONE_LE;
338 	lease->state |= SMB2_LEASE_WRITE_CACHING_LE;
339 	if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
340 		opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
341 	else
342 		opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
343 	return 0;
344 }
345 
346 /**
347  * lease_none_upgrade() - upgrade lease state from none
348  * @opinfo:	current lease info
349  * @new_state:	new lease state
350  *
351  * Return:	0 on success, otherwise -EINVAL
352  */
353 static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state)
354 {
355 	struct lease *lease = opinfo->o_lease;
356 
357 	if (!(lease->state == SMB2_LEASE_NONE_LE)) {
358 		ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
359 		return -EINVAL;
360 	}
361 
362 	lease->new_state = SMB2_LEASE_NONE_LE;
363 	lease->state = new_state;
364 	if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
365 		if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
366 			opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
367 		else
368 			opinfo->level = SMB2_OPLOCK_LEVEL_II;
369 	else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
370 		opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
371 	else if (lease->state & SMB2_LEASE_READ_CACHING_LE)
372 		opinfo->level = SMB2_OPLOCK_LEVEL_II;
373 
374 	return 0;
375 }
376 
377 /**
378  * close_id_del_oplock() - release oplock object at file close time
379  * @fp:		ksmbd file pointer
380  */
381 void close_id_del_oplock(struct ksmbd_file *fp)
382 {
383 	struct oplock_info *opinfo;
384 
385 	if (fp->reserve_lease_break)
386 		smb_lazy_parent_lease_break_close(fp);
387 
388 	opinfo = opinfo_get(fp);
389 	if (!opinfo)
390 		return;
391 
392 	opinfo_del(opinfo);
393 
394 	rcu_assign_pointer(fp->f_opinfo, NULL);
395 	if (opinfo->op_state == OPLOCK_ACK_WAIT) {
396 		opinfo->op_state = OPLOCK_CLOSING;
397 		wake_up_interruptible_all(&opinfo->oplock_q);
398 		if (opinfo->is_lease) {
399 			atomic_set(&opinfo->breaking_cnt, 0);
400 			wake_up_interruptible_all(&opinfo->oplock_brk);
401 		}
402 	}
403 
404 	opinfo_count_dec(fp);
405 	atomic_dec(&opinfo->refcount);
406 	opinfo_put(opinfo);
407 }
408 
409 /**
410  * grant_write_oplock() - grant exclusive/batch oplock or write lease
411  * @opinfo_new:	new oplock info object
412  * @req_oplock: request oplock
413  * @lctx:	lease context information
414  *
415  * Return:      0
416  */
417 static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock,
418 			       struct lease_ctx_info *lctx)
419 {
420 	struct lease *lease = opinfo_new->o_lease;
421 
422 	if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
423 		opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
424 	else
425 		opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
426 
427 	if (lctx) {
428 		lease->state = lctx->req_state;
429 		memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
430 	}
431 }
432 
433 /**
434  * grant_read_oplock() - grant level2 oplock or read lease
435  * @opinfo_new:	new oplock info object
436  * @lctx:	lease context information
437  *
438  * Return:      0
439  */
440 static void grant_read_oplock(struct oplock_info *opinfo_new,
441 			      struct lease_ctx_info *lctx)
442 {
443 	struct lease *lease = opinfo_new->o_lease;
444 
445 	opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
446 
447 	if (lctx) {
448 		lease->state = SMB2_LEASE_READ_CACHING_LE;
449 		if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE)
450 			lease->state |= SMB2_LEASE_HANDLE_CACHING_LE;
451 		memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
452 	}
453 }
454 
455 /**
456  * grant_none_oplock() - grant none oplock or none lease
457  * @opinfo_new:	new oplock info object
458  * @lctx:	lease context information
459  *
460  * Return:      0
461  */
462 static void grant_none_oplock(struct oplock_info *opinfo_new,
463 			      struct lease_ctx_info *lctx)
464 {
465 	struct lease *lease = opinfo_new->o_lease;
466 
467 	opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
468 
469 	if (lctx) {
470 		lease->state = 0;
471 		memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
472 	}
473 }
474 
475 static inline int compare_guid_key(struct oplock_info *opinfo,
476 				   const char *guid1, const char *key1)
477 {
478 	const char *guid2, *key2;
479 
480 	guid2 = opinfo->conn->ClientGUID;
481 	key2 = opinfo->o_lease->lease_key;
482 	if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) &&
483 	    !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE))
484 		return 1;
485 
486 	return 0;
487 }
488 
489 /**
490  * same_client_has_lease() - check whether current lease request is
491  *		from lease owner of file
492  * @ci:		master file pointer
493  * @client_guid:	Client GUID
494  * @lctx:		lease context information
495  *
496  * Return:      oplock(lease) object on success, otherwise NULL
497  */
498 static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,
499 						 char *client_guid,
500 						 struct lease_ctx_info *lctx)
501 {
502 	int ret;
503 	struct lease *lease;
504 	struct oplock_info *opinfo;
505 	struct oplock_info *m_opinfo = NULL;
506 
507 	if (!lctx)
508 		return NULL;
509 
510 	/*
511 	 * Compare lease key and client_guid to know request from same owner
512 	 * of same client
513 	 */
514 	down_read(&ci->m_lock);
515 	list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
516 		if (!opinfo->is_lease || !opinfo->conn)
517 			continue;
518 		lease = opinfo->o_lease;
519 
520 		ret = compare_guid_key(opinfo, client_guid, lctx->lease_key);
521 		if (ret) {
522 			m_opinfo = opinfo;
523 			/* skip upgrading lease about breaking lease */
524 			if (atomic_read(&opinfo->breaking_cnt))
525 				continue;
526 
527 			/* upgrading lease */
528 			if ((atomic_read(&ci->op_count) +
529 			     atomic_read(&ci->sop_count)) == 1) {
530 				if (lease->state != SMB2_LEASE_NONE_LE &&
531 				    lease->state == (lctx->req_state & lease->state)) {
532 					lease->epoch++;
533 					lease->state |= lctx->req_state;
534 					if (lctx->req_state &
535 						SMB2_LEASE_WRITE_CACHING_LE)
536 						lease_read_to_write(opinfo);
537 
538 				}
539 			} else if ((atomic_read(&ci->op_count) +
540 				    atomic_read(&ci->sop_count)) > 1) {
541 				if (lctx->req_state ==
542 				    (SMB2_LEASE_READ_CACHING_LE |
543 				     SMB2_LEASE_HANDLE_CACHING_LE)) {
544 					lease->epoch++;
545 					lease->state = lctx->req_state;
546 				}
547 			}
548 
549 			if (lctx->req_state && lease->state ==
550 			    SMB2_LEASE_NONE_LE) {
551 				lease->epoch++;
552 				lease_none_upgrade(opinfo, lctx->req_state);
553 			}
554 		}
555 	}
556 	up_read(&ci->m_lock);
557 
558 	return m_opinfo;
559 }
560 
561 static void wait_for_break_ack(struct oplock_info *opinfo)
562 {
563 	int rc = 0;
564 
565 	rc = wait_event_interruptible_timeout(opinfo->oplock_q,
566 					      opinfo->op_state == OPLOCK_STATE_NONE ||
567 					      opinfo->op_state == OPLOCK_CLOSING,
568 					      OPLOCK_WAIT_TIME);
569 
570 	/* is this a timeout ? */
571 	if (!rc) {
572 		if (opinfo->is_lease)
573 			opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
574 		opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
575 		opinfo->op_state = OPLOCK_STATE_NONE;
576 	}
577 }
578 
579 static void wake_up_oplock_break(struct oplock_info *opinfo)
580 {
581 	clear_bit_unlock(0, &opinfo->pending_break);
582 	/* memory barrier is needed for wake_up_bit() */
583 	smp_mb__after_atomic();
584 	wake_up_bit(&opinfo->pending_break, 0);
585 }
586 
587 static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)
588 {
589 	while (test_and_set_bit(0, &opinfo->pending_break)) {
590 		wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE);
591 
592 		/* Not immediately break to none. */
593 		opinfo->open_trunc = 0;
594 
595 		if (opinfo->op_state == OPLOCK_CLOSING)
596 			return -ENOENT;
597 		else if (opinfo->level <= req_op_level) {
598 			if (opinfo->is_lease == false)
599 				return 1;
600 
601 			if (opinfo->o_lease->state !=
602 			    (SMB2_LEASE_HANDLE_CACHING_LE |
603 			     SMB2_LEASE_READ_CACHING_LE))
604 				return 1;
605 		}
606 	}
607 
608 	if (opinfo->level <= req_op_level) {
609 		if (opinfo->is_lease == false) {
610 			wake_up_oplock_break(opinfo);
611 			return 1;
612 		}
613 		if (opinfo->o_lease->state !=
614 		    (SMB2_LEASE_HANDLE_CACHING_LE |
615 		     SMB2_LEASE_READ_CACHING_LE)) {
616 			wake_up_oplock_break(opinfo);
617 			return 1;
618 		}
619 	}
620 	return 0;
621 }
622 
623 /**
624  * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn
625  * to client
626  * @wk:     smb work object
627  *
628  * There are two ways this function can be called. 1- while file open we break
629  * from exclusive/batch lock to levelII oplock and 2- while file write/truncate
630  * we break from levelII oplock no oplock.
631  * work->request_buf contains oplock_info.
632  */
633 static void __smb2_oplock_break_noti(struct work_struct *wk)
634 {
635 	struct smb2_oplock_break *rsp = NULL;
636 	struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
637 	struct ksmbd_conn *conn = work->conn;
638 	struct oplock_break_info *br_info = work->request_buf;
639 	struct smb2_hdr *rsp_hdr;
640 	struct ksmbd_file *fp;
641 
642 	fp = ksmbd_lookup_global_fd(br_info->fid);
643 	if (!fp)
644 		goto out;
645 
646 	if (allocate_interim_rsp_buf(work)) {
647 		pr_err("smb2_allocate_rsp_buf failed! ");
648 		ksmbd_fd_put(work, fp);
649 		goto out;
650 	}
651 
652 	rsp_hdr = smb_get_msg(work->response_buf);
653 	memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
654 	rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
655 	rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
656 	rsp_hdr->CreditRequest = cpu_to_le16(0);
657 	rsp_hdr->Command = SMB2_OPLOCK_BREAK;
658 	rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
659 	rsp_hdr->NextCommand = 0;
660 	rsp_hdr->MessageId = cpu_to_le64(-1);
661 	rsp_hdr->Id.SyncId.ProcessId = 0;
662 	rsp_hdr->Id.SyncId.TreeId = 0;
663 	rsp_hdr->SessionId = 0;
664 	memset(rsp_hdr->Signature, 0, 16);
665 
666 	rsp = smb_get_msg(work->response_buf);
667 
668 	rsp->StructureSize = cpu_to_le16(24);
669 	if (!br_info->open_trunc &&
670 	    (br_info->level == SMB2_OPLOCK_LEVEL_BATCH ||
671 	     br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
672 		rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II;
673 	else
674 		rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
675 	rsp->Reserved = 0;
676 	rsp->Reserved2 = 0;
677 	rsp->PersistentFid = fp->persistent_id;
678 	rsp->VolatileFid = fp->volatile_id;
679 
680 	ksmbd_fd_put(work, fp);
681 	if (ksmbd_iov_pin_rsp(work, (void *)rsp,
682 			      sizeof(struct smb2_oplock_break)))
683 		goto out;
684 
685 	ksmbd_debug(OPLOCK,
686 		    "sending oplock break v_id %llu p_id = %llu lock level = %d\n",
687 		    rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel);
688 
689 	ksmbd_conn_write(work);
690 
691 out:
692 	ksmbd_free_work_struct(work);
693 	ksmbd_conn_r_count_dec(conn);
694 }
695 
696 /**
697  * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock
698  *		break command from server to client
699  * @opinfo:		oplock info object
700  *
701  * Return:      0 on success, otherwise error
702  */
703 static int smb2_oplock_break_noti(struct oplock_info *opinfo)
704 {
705 	struct ksmbd_conn *conn = opinfo->conn;
706 	struct oplock_break_info *br_info;
707 	int ret = 0;
708 	struct ksmbd_work *work = ksmbd_alloc_work_struct();
709 
710 	if (!work)
711 		return -ENOMEM;
712 
713 	br_info = kmalloc_obj(struct oplock_break_info, KSMBD_DEFAULT_GFP);
714 	if (!br_info) {
715 		ksmbd_free_work_struct(work);
716 		return -ENOMEM;
717 	}
718 
719 	br_info->level = opinfo->level;
720 	br_info->fid = opinfo->fid;
721 	br_info->open_trunc = opinfo->open_trunc;
722 
723 	work->request_buf = (char *)br_info;
724 	work->conn = conn;
725 	work->sess = opinfo->sess;
726 
727 	ksmbd_conn_r_count_inc(conn);
728 	if (opinfo->op_state == OPLOCK_ACK_WAIT) {
729 		INIT_WORK(&work->work, __smb2_oplock_break_noti);
730 		ksmbd_queue_work(work);
731 
732 		wait_for_break_ack(opinfo);
733 	} else {
734 		__smb2_oplock_break_noti(&work->work);
735 		if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
736 			opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
737 	}
738 	return ret;
739 }
740 
741 /**
742  * __smb2_lease_break_noti() - send lease break command from server
743  * to client
744  * @wk:     smb work object
745  */
746 static void __smb2_lease_break_noti(struct work_struct *wk)
747 {
748 	struct smb2_lease_break *rsp = NULL;
749 	struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
750 	struct ksmbd_conn *conn = work->conn;
751 	struct lease_break_info *br_info = work->request_buf;
752 	struct smb2_hdr *rsp_hdr;
753 
754 	if (allocate_interim_rsp_buf(work)) {
755 		ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! ");
756 		goto out;
757 	}
758 
759 	rsp_hdr = smb_get_msg(work->response_buf);
760 	memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
761 	rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
762 	rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
763 	rsp_hdr->CreditRequest = cpu_to_le16(0);
764 	rsp_hdr->Command = SMB2_OPLOCK_BREAK;
765 	rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
766 	rsp_hdr->NextCommand = 0;
767 	rsp_hdr->MessageId = cpu_to_le64(-1);
768 	rsp_hdr->Id.SyncId.ProcessId = 0;
769 	rsp_hdr->Id.SyncId.TreeId = 0;
770 	rsp_hdr->SessionId = 0;
771 	memset(rsp_hdr->Signature, 0, 16);
772 
773 	rsp = smb_get_msg(work->response_buf);
774 	rsp->StructureSize = cpu_to_le16(44);
775 	rsp->Epoch = br_info->epoch;
776 	rsp->Flags = 0;
777 
778 	if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE |
779 			SMB2_LEASE_HANDLE_CACHING_LE))
780 		rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
781 
782 	memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE);
783 	rsp->CurrentLeaseState = br_info->curr_state;
784 	rsp->NewLeaseState = br_info->new_state;
785 	rsp->BreakReason = 0;
786 	rsp->AccessMaskHint = 0;
787 	rsp->ShareMaskHint = 0;
788 
789 	if (ksmbd_iov_pin_rsp(work, (void *)rsp,
790 			      sizeof(struct smb2_lease_break)))
791 		goto out;
792 
793 	ksmbd_conn_write(work);
794 
795 out:
796 	ksmbd_free_work_struct(work);
797 	ksmbd_conn_r_count_dec(conn);
798 }
799 
800 /**
801  * smb2_lease_break_noti() - break lease when a new client request
802  *			write lease
803  * @opinfo:		contains lease state information
804  *
805  * Return:	0 on success, otherwise error
806  */
807 static int smb2_lease_break_noti(struct oplock_info *opinfo)
808 {
809 	struct ksmbd_conn *conn = opinfo->conn;
810 	struct ksmbd_work *work;
811 	struct lease_break_info *br_info;
812 	struct lease *lease = opinfo->o_lease;
813 
814 	work = ksmbd_alloc_work_struct();
815 	if (!work)
816 		return -ENOMEM;
817 
818 	br_info = kmalloc_obj(struct lease_break_info, KSMBD_DEFAULT_GFP);
819 	if (!br_info) {
820 		ksmbd_free_work_struct(work);
821 		return -ENOMEM;
822 	}
823 
824 	br_info->curr_state = lease->state;
825 	br_info->new_state = lease->new_state;
826 	if (lease->version == 2)
827 		br_info->epoch = cpu_to_le16(++lease->epoch);
828 	else
829 		br_info->epoch = 0;
830 	memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE);
831 
832 	work->request_buf = (char *)br_info;
833 	work->conn = conn;
834 	work->sess = opinfo->sess;
835 
836 	ksmbd_conn_r_count_inc(conn);
837 	if (opinfo->op_state == OPLOCK_ACK_WAIT) {
838 		INIT_WORK(&work->work, __smb2_lease_break_noti);
839 		ksmbd_queue_work(work);
840 		wait_for_break_ack(opinfo);
841 	} else {
842 		__smb2_lease_break_noti(&work->work);
843 		if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) {
844 			opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
845 			opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
846 		}
847 	}
848 	return 0;
849 }
850 
851 static void wait_lease_breaking(struct oplock_info *opinfo)
852 {
853 	if (!opinfo->is_lease)
854 		return;
855 
856 	wake_up_interruptible_all(&opinfo->oplock_brk);
857 	if (atomic_read(&opinfo->breaking_cnt)) {
858 		int ret = 0;
859 
860 		ret = wait_event_interruptible_timeout(opinfo->oplock_brk,
861 						       atomic_read(&opinfo->breaking_cnt) == 0,
862 						       HZ);
863 		if (!ret)
864 			atomic_set(&opinfo->breaking_cnt, 0);
865 	}
866 }
867 
868 static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level,
869 			struct ksmbd_work *in_work)
870 {
871 	int err = 0;
872 
873 	/* Need to break exclusive/batch oplock, write lease or overwrite_if */
874 	ksmbd_debug(OPLOCK,
875 		    "request to send oplock(level : 0x%x) break notification\n",
876 		    brk_opinfo->level);
877 
878 	if (brk_opinfo->is_lease) {
879 		struct lease *lease = brk_opinfo->o_lease;
880 
881 		atomic_inc(&brk_opinfo->breaking_cnt);
882 		err = oplock_break_pending(brk_opinfo, req_op_level);
883 		if (err)
884 			return err < 0 ? err : 0;
885 
886 		if (brk_opinfo->open_trunc) {
887 			/*
888 			 * Create overwrite break trigger the lease break to
889 			 * none.
890 			 */
891 			lease->new_state = SMB2_LEASE_NONE_LE;
892 		} else {
893 			if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) {
894 				if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
895 					lease->new_state =
896 						SMB2_LEASE_READ_CACHING_LE |
897 						SMB2_LEASE_HANDLE_CACHING_LE;
898 				else
899 					lease->new_state =
900 						SMB2_LEASE_READ_CACHING_LE;
901 			} else {
902 				if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE &&
903 						!lease->is_dir)
904 					lease->new_state =
905 						SMB2_LEASE_READ_CACHING_LE;
906 				else
907 					lease->new_state = SMB2_LEASE_NONE_LE;
908 			}
909 		}
910 
911 		if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE |
912 				SMB2_LEASE_HANDLE_CACHING_LE)) {
913 			if (in_work) {
914 				setup_async_work(in_work, NULL, NULL);
915 				smb2_send_interim_resp(in_work, STATUS_PENDING);
916 				release_async_work(in_work);
917 			}
918 
919 			brk_opinfo->op_state = OPLOCK_ACK_WAIT;
920 		} else
921 			atomic_dec(&brk_opinfo->breaking_cnt);
922 	} else {
923 		err = oplock_break_pending(brk_opinfo, req_op_level);
924 		if (err)
925 			return err < 0 ? err : 0;
926 
927 		if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
928 		    brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
929 			brk_opinfo->op_state = OPLOCK_ACK_WAIT;
930 	}
931 
932 	if (brk_opinfo->is_lease)
933 		err = smb2_lease_break_noti(brk_opinfo);
934 	else
935 		err = smb2_oplock_break_noti(brk_opinfo);
936 
937 	ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level);
938 	if (brk_opinfo->op_state == OPLOCK_CLOSING)
939 		err = -ENOENT;
940 	wake_up_oplock_break(brk_opinfo);
941 
942 	wait_lease_breaking(brk_opinfo);
943 
944 	return err;
945 }
946 
947 void destroy_lease_table(struct ksmbd_conn *conn)
948 {
949 	struct lease_table *lb, *lbtmp;
950 	struct oplock_info *opinfo;
951 
952 	write_lock(&lease_list_lock);
953 	if (list_empty(&lease_table_list)) {
954 		write_unlock(&lease_list_lock);
955 		return;
956 	}
957 
958 	list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) {
959 		if (conn && memcmp(lb->client_guid, conn->ClientGUID,
960 				   SMB2_CLIENT_GUID_SIZE))
961 			continue;
962 again:
963 		rcu_read_lock();
964 		list_for_each_entry_rcu(opinfo, &lb->lease_list,
965 					lease_entry) {
966 			rcu_read_unlock();
967 			lease_del_list(opinfo);
968 			goto again;
969 		}
970 		rcu_read_unlock();
971 		list_del(&lb->l_entry);
972 		kfree(lb);
973 	}
974 	write_unlock(&lease_list_lock);
975 }
976 
977 int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
978 			struct lease_ctx_info *lctx)
979 {
980 	struct oplock_info *opinfo;
981 	int err = 0;
982 	struct lease_table *lb;
983 
984 	if (!lctx)
985 		return err;
986 
987 	read_lock(&lease_list_lock);
988 	if (list_empty(&lease_table_list)) {
989 		read_unlock(&lease_list_lock);
990 		return 0;
991 	}
992 
993 	list_for_each_entry(lb, &lease_table_list, l_entry) {
994 		if (!memcmp(lb->client_guid, sess->ClientGUID,
995 			    SMB2_CLIENT_GUID_SIZE))
996 			goto found;
997 	}
998 	read_unlock(&lease_list_lock);
999 
1000 	return 0;
1001 
1002 found:
1003 	rcu_read_lock();
1004 	list_for_each_entry_rcu(opinfo, &lb->lease_list, lease_entry) {
1005 		if (!atomic_inc_not_zero(&opinfo->refcount))
1006 			continue;
1007 		rcu_read_unlock();
1008 		if (opinfo->o_fp->f_ci == ci)
1009 			goto op_next;
1010 		err = compare_guid_key(opinfo, sess->ClientGUID,
1011 				       lctx->lease_key);
1012 		if (err) {
1013 			err = -EINVAL;
1014 			ksmbd_debug(OPLOCK,
1015 				    "found same lease key is already used in other files\n");
1016 			opinfo_put(opinfo);
1017 			goto out;
1018 		}
1019 op_next:
1020 		opinfo_put(opinfo);
1021 		rcu_read_lock();
1022 	}
1023 	rcu_read_unlock();
1024 
1025 out:
1026 	read_unlock(&lease_list_lock);
1027 	return err;
1028 }
1029 
1030 static void copy_lease(struct oplock_info *op1, struct oplock_info *op2)
1031 {
1032 	struct lease *lease1 = op1->o_lease;
1033 	struct lease *lease2 = op2->o_lease;
1034 
1035 	op2->level = op1->level;
1036 	lease2->state = lease1->state;
1037 	memcpy(lease2->lease_key, lease1->lease_key,
1038 	       SMB2_LEASE_KEY_SIZE);
1039 	lease2->duration = lease1->duration;
1040 	lease2->flags = lease1->flags;
1041 	lease2->epoch = lease1->epoch;
1042 	lease2->version = lease1->version;
1043 }
1044 
1045 static int add_lease_global_list(struct oplock_info *opinfo)
1046 {
1047 	struct lease_table *lb;
1048 
1049 	read_lock(&lease_list_lock);
1050 	list_for_each_entry(lb, &lease_table_list, l_entry) {
1051 		if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID,
1052 			    SMB2_CLIENT_GUID_SIZE)) {
1053 			opinfo->o_lease->l_lb = lb;
1054 			lease_add_list(opinfo);
1055 			read_unlock(&lease_list_lock);
1056 			return 0;
1057 		}
1058 	}
1059 	read_unlock(&lease_list_lock);
1060 
1061 	lb = kmalloc_obj(struct lease_table, KSMBD_DEFAULT_GFP);
1062 	if (!lb)
1063 		return -ENOMEM;
1064 
1065 	memcpy(lb->client_guid, opinfo->conn->ClientGUID,
1066 	       SMB2_CLIENT_GUID_SIZE);
1067 	INIT_LIST_HEAD(&lb->lease_list);
1068 	spin_lock_init(&lb->lb_lock);
1069 	opinfo->o_lease->l_lb = lb;
1070 	lease_add_list(opinfo);
1071 	lb_add(lb);
1072 	return 0;
1073 }
1074 
1075 static void set_oplock_level(struct oplock_info *opinfo, int level,
1076 			     struct lease_ctx_info *lctx)
1077 {
1078 	switch (level) {
1079 	case SMB2_OPLOCK_LEVEL_BATCH:
1080 	case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
1081 		grant_write_oplock(opinfo, level, lctx);
1082 		break;
1083 	case SMB2_OPLOCK_LEVEL_II:
1084 		grant_read_oplock(opinfo, lctx);
1085 		break;
1086 	default:
1087 		grant_none_oplock(opinfo, lctx);
1088 		break;
1089 	}
1090 }
1091 
1092 void smb_send_parent_lease_break_noti(struct ksmbd_file *fp,
1093 				      struct lease_ctx_info *lctx)
1094 {
1095 	struct oplock_info *opinfo;
1096 	struct ksmbd_inode *p_ci = NULL;
1097 
1098 	if (lctx->version != 2)
1099 		return;
1100 
1101 	p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent);
1102 	if (!p_ci)
1103 		return;
1104 
1105 	down_read(&p_ci->m_lock);
1106 	list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) {
1107 		if (opinfo->conn == NULL || !opinfo->is_lease)
1108 			continue;
1109 
1110 		if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE &&
1111 		    (!(lctx->flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE) ||
1112 		     !compare_guid_key(opinfo, fp->conn->ClientGUID,
1113 				      lctx->parent_lease_key))) {
1114 			if (!atomic_inc_not_zero(&opinfo->refcount))
1115 				continue;
1116 
1117 			if (ksmbd_conn_releasing(opinfo->conn)) {
1118 				opinfo_put(opinfo);
1119 				continue;
1120 			}
1121 
1122 			oplock_break(opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL);
1123 			opinfo_put(opinfo);
1124 		}
1125 	}
1126 	up_read(&p_ci->m_lock);
1127 
1128 	ksmbd_inode_put(p_ci);
1129 }
1130 
1131 void smb_lazy_parent_lease_break_close(struct ksmbd_file *fp)
1132 {
1133 	struct oplock_info *opinfo;
1134 	struct ksmbd_inode *p_ci = NULL;
1135 
1136 	rcu_read_lock();
1137 	opinfo = rcu_dereference(fp->f_opinfo);
1138 
1139 	if (!opinfo || !opinfo->is_lease || opinfo->o_lease->version != 2) {
1140 		rcu_read_unlock();
1141 		return;
1142 	}
1143 	rcu_read_unlock();
1144 
1145 	p_ci = ksmbd_inode_lookup_lock(fp->filp->f_path.dentry->d_parent);
1146 	if (!p_ci)
1147 		return;
1148 
1149 	down_read(&p_ci->m_lock);
1150 	list_for_each_entry(opinfo, &p_ci->m_op_list, op_entry) {
1151 		if (opinfo->conn == NULL || !opinfo->is_lease)
1152 			continue;
1153 
1154 		if (opinfo->o_lease->state != SMB2_OPLOCK_LEVEL_NONE) {
1155 			if (!atomic_inc_not_zero(&opinfo->refcount))
1156 				continue;
1157 
1158 			if (ksmbd_conn_releasing(opinfo->conn)) {
1159 				opinfo_put(opinfo);
1160 				continue;
1161 			}
1162 
1163 			oplock_break(opinfo, SMB2_OPLOCK_LEVEL_NONE, NULL);
1164 			opinfo_put(opinfo);
1165 		}
1166 	}
1167 	up_read(&p_ci->m_lock);
1168 
1169 	ksmbd_inode_put(p_ci);
1170 }
1171 
1172 /**
1173  * smb_grant_oplock() - handle oplock/lease request on file open
1174  * @work:		smb work
1175  * @req_op_level:	oplock level
1176  * @pid:		id of open file
1177  * @fp:			ksmbd file pointer
1178  * @tid:		Tree id of connection
1179  * @lctx:		lease context information on file open
1180  * @share_ret:		share mode
1181  *
1182  * Return:      0 on success, otherwise error
1183  */
1184 int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,
1185 		     struct ksmbd_file *fp, __u16 tid,
1186 		     struct lease_ctx_info *lctx, int share_ret)
1187 {
1188 	struct ksmbd_session *sess = work->sess;
1189 	int err = 0;
1190 	struct oplock_info *opinfo = NULL, *prev_opinfo = NULL;
1191 	struct ksmbd_inode *ci = fp->f_ci;
1192 	bool prev_op_has_lease;
1193 	__le32 prev_op_state = 0;
1194 
1195 	/* Only v2 leases handle the directory */
1196 	if (S_ISDIR(file_inode(fp->filp)->i_mode)) {
1197 		if (!lctx || lctx->version != 2 ||
1198 		    (lctx->flags != SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE &&
1199 		     !lctx->epoch))
1200 			return 0;
1201 	}
1202 
1203 	opinfo = alloc_opinfo(work, pid, tid);
1204 	if (!opinfo)
1205 		return -ENOMEM;
1206 
1207 	if (lctx) {
1208 		err = alloc_lease(opinfo, lctx);
1209 		if (err)
1210 			goto err_out;
1211 		opinfo->is_lease = 1;
1212 	}
1213 
1214 	/* ci does not have any oplock */
1215 	if (!opinfo_count(fp))
1216 		goto set_lev;
1217 
1218 	/* grant none-oplock if second open is trunc */
1219 	if (fp->attrib_only && fp->cdoption != FILE_OVERWRITE_IF_LE &&
1220 	    fp->cdoption != FILE_OVERWRITE_LE &&
1221 	    fp->cdoption != FILE_SUPERSEDE_LE) {
1222 		req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1223 		goto set_lev;
1224 	}
1225 
1226 	if (lctx) {
1227 		struct oplock_info *m_opinfo;
1228 
1229 		/* is lease already granted ? */
1230 		m_opinfo = same_client_has_lease(ci, sess->ClientGUID,
1231 						 lctx);
1232 		if (m_opinfo) {
1233 			copy_lease(m_opinfo, opinfo);
1234 			if (atomic_read(&m_opinfo->breaking_cnt))
1235 				opinfo->o_lease->flags =
1236 					SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE;
1237 			goto out;
1238 		}
1239 	}
1240 	prev_opinfo = opinfo_get_list(ci);
1241 	if (!prev_opinfo ||
1242 	    (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx)) {
1243 		opinfo_put(prev_opinfo);
1244 		goto set_lev;
1245 	}
1246 	prev_op_has_lease = prev_opinfo->is_lease;
1247 	if (prev_op_has_lease)
1248 		prev_op_state = prev_opinfo->o_lease->state;
1249 
1250 	if (share_ret < 0 &&
1251 	    prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1252 		err = share_ret;
1253 		opinfo_put(prev_opinfo);
1254 		goto err_out;
1255 	}
1256 
1257 	if (prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
1258 	    prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1259 		opinfo_put(prev_opinfo);
1260 		goto op_break_not_needed;
1261 	}
1262 
1263 	err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II, work);
1264 	opinfo_put(prev_opinfo);
1265 	if (err == -ENOENT)
1266 		goto set_lev;
1267 	/* Check all oplock was freed by close */
1268 	else if (err < 0)
1269 		goto err_out;
1270 
1271 op_break_not_needed:
1272 	if (share_ret < 0) {
1273 		err = share_ret;
1274 		goto err_out;
1275 	}
1276 
1277 	if (req_op_level != SMB2_OPLOCK_LEVEL_NONE)
1278 		req_op_level = SMB2_OPLOCK_LEVEL_II;
1279 
1280 	/* grant fixed oplock on stacked locking between lease and oplock */
1281 	if (prev_op_has_lease && !lctx)
1282 		if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)
1283 			req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1284 
1285 	if (!prev_op_has_lease && lctx) {
1286 		req_op_level = SMB2_OPLOCK_LEVEL_II;
1287 		lctx->req_state = SMB2_LEASE_READ_CACHING_LE;
1288 	}
1289 
1290 set_lev:
1291 	set_oplock_level(opinfo, req_op_level, lctx);
1292 
1293 out:
1294 	opinfo_count_inc(fp);
1295 	opinfo_add(opinfo, fp);
1296 
1297 	if (opinfo->is_lease) {
1298 		err = add_lease_global_list(opinfo);
1299 		if (err)
1300 			goto err_out;
1301 	}
1302 
1303 	rcu_assign_pointer(fp->f_opinfo, opinfo);
1304 	opinfo->o_fp = fp;
1305 
1306 	return 0;
1307 err_out:
1308 	__free_opinfo(opinfo);
1309 	return err;
1310 }
1311 
1312 /**
1313  * smb_break_all_write_oplock() - break batch/exclusive oplock to level2
1314  * @work:	smb work
1315  * @fp:		ksmbd file pointer
1316  * @is_trunc:	truncate on open
1317  */
1318 static void smb_break_all_write_oplock(struct ksmbd_work *work,
1319 				       struct ksmbd_file *fp, int is_trunc)
1320 {
1321 	struct oplock_info *brk_opinfo;
1322 
1323 	brk_opinfo = opinfo_get_list(fp->f_ci);
1324 	if (!brk_opinfo)
1325 		return;
1326 	if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
1327 	    brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1328 		opinfo_put(brk_opinfo);
1329 		return;
1330 	}
1331 
1332 	brk_opinfo->open_trunc = is_trunc;
1333 	oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II, work);
1334 	opinfo_put(brk_opinfo);
1335 }
1336 
1337 /**
1338  * smb_break_all_levII_oplock() - send level2 oplock or read lease break command
1339  *	from server to client
1340  * @work:	smb work
1341  * @fp:		ksmbd file pointer
1342  * @is_trunc:	truncate on open
1343  */
1344 void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,
1345 				int is_trunc)
1346 {
1347 	struct oplock_info *op, *brk_op;
1348 	struct ksmbd_inode *ci;
1349 	struct ksmbd_conn *conn = work->conn;
1350 
1351 	if (!test_share_config_flag(work->tcon->share_conf,
1352 				    KSMBD_SHARE_FLAG_OPLOCKS))
1353 		return;
1354 
1355 	ci = fp->f_ci;
1356 	op = opinfo_get(fp);
1357 
1358 	down_read(&ci->m_lock);
1359 	list_for_each_entry(brk_op, &ci->m_op_list, op_entry) {
1360 		if (brk_op->conn == NULL)
1361 			continue;
1362 
1363 		if (!atomic_inc_not_zero(&brk_op->refcount))
1364 			continue;
1365 
1366 		if (ksmbd_conn_releasing(brk_op->conn)) {
1367 			opinfo_put(brk_op);
1368 			continue;
1369 		}
1370 
1371 		if (brk_op->is_lease && (brk_op->o_lease->state &
1372 		    (~(SMB2_LEASE_READ_CACHING_LE |
1373 				SMB2_LEASE_HANDLE_CACHING_LE)))) {
1374 			ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n",
1375 				    brk_op->o_lease->state);
1376 			goto next;
1377 		} else if (brk_op->level !=
1378 				SMB2_OPLOCK_LEVEL_II) {
1379 			ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n",
1380 				    brk_op->level);
1381 			goto next;
1382 		}
1383 
1384 		/* Skip oplock being break to none */
1385 		if (brk_op->is_lease &&
1386 		    brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE &&
1387 		    atomic_read(&brk_op->breaking_cnt))
1388 			goto next;
1389 
1390 		if (op && op->is_lease && brk_op->is_lease &&
1391 		    !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID,
1392 			    SMB2_CLIENT_GUID_SIZE) &&
1393 		    !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key,
1394 			    SMB2_LEASE_KEY_SIZE))
1395 			goto next;
1396 		brk_op->open_trunc = is_trunc;
1397 		oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE, NULL);
1398 next:
1399 		opinfo_put(brk_op);
1400 	}
1401 	up_read(&ci->m_lock);
1402 
1403 	if (op)
1404 		opinfo_put(op);
1405 }
1406 
1407 /**
1408  * smb_break_all_oplock() - break both batch/exclusive and level2 oplock
1409  * @work:	smb work
1410  * @fp:		ksmbd file pointer
1411  */
1412 void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp)
1413 {
1414 	if (!test_share_config_flag(work->tcon->share_conf,
1415 				    KSMBD_SHARE_FLAG_OPLOCKS))
1416 		return;
1417 
1418 	smb_break_all_write_oplock(work, fp, 1);
1419 	smb_break_all_levII_oplock(work, fp, 1);
1420 }
1421 
1422 /**
1423  * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type
1424  * @lease_state:     lease type
1425  *
1426  * Return:      0 if no mapping, otherwise corresponding oplock type
1427  */
1428 __u8 smb2_map_lease_to_oplock(__le32 lease_state)
1429 {
1430 	if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE |
1431 			    SMB2_LEASE_READ_CACHING_LE |
1432 			    SMB2_LEASE_WRITE_CACHING_LE)) {
1433 		return SMB2_OPLOCK_LEVEL_BATCH;
1434 	} else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE &&
1435 		 lease_state & SMB2_LEASE_WRITE_CACHING_LE) {
1436 		if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE))
1437 			return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
1438 	} else if (lease_state & SMB2_LEASE_READ_CACHING_LE) {
1439 		return SMB2_OPLOCK_LEVEL_II;
1440 	}
1441 	return 0;
1442 }
1443 
1444 /**
1445  * create_lease_buf() - create lease context for open cmd response
1446  * @rbuf:	buffer to create lease context response
1447  * @lease:	buffer to stored parsed lease state information
1448  */
1449 void create_lease_buf(u8 *rbuf, struct lease *lease)
1450 {
1451 	if (lease->version == 2) {
1452 		struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf;
1453 
1454 		memset(buf, 0, sizeof(struct create_lease_v2));
1455 		memcpy(buf->lcontext.LeaseKey, lease->lease_key,
1456 		       SMB2_LEASE_KEY_SIZE);
1457 		buf->lcontext.LeaseFlags = lease->flags;
1458 		buf->lcontext.Epoch = cpu_to_le16(lease->epoch);
1459 		buf->lcontext.LeaseState = lease->state;
1460 		if (lease->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE)
1461 			memcpy(buf->lcontext.ParentLeaseKey, lease->parent_lease_key,
1462 			       SMB2_LEASE_KEY_SIZE);
1463 		buf->ccontext.DataOffset = cpu_to_le16(offsetof
1464 				(struct create_lease_v2, lcontext));
1465 		buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1466 		buf->ccontext.NameOffset = cpu_to_le16(offsetof
1467 				(struct create_lease_v2, Name));
1468 		buf->ccontext.NameLength = cpu_to_le16(4);
1469 		buf->Name[0] = 'R';
1470 		buf->Name[1] = 'q';
1471 		buf->Name[2] = 'L';
1472 		buf->Name[3] = 's';
1473 	} else {
1474 		struct create_lease *buf = (struct create_lease *)rbuf;
1475 
1476 		memset(buf, 0, sizeof(struct create_lease));
1477 		memcpy(buf->lcontext.LeaseKey, lease->lease_key, SMB2_LEASE_KEY_SIZE);
1478 		buf->lcontext.LeaseFlags = lease->flags;
1479 		buf->lcontext.LeaseState = lease->state;
1480 		buf->ccontext.DataOffset = cpu_to_le16(offsetof
1481 				(struct create_lease, lcontext));
1482 		buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1483 		buf->ccontext.NameOffset = cpu_to_le16(offsetof
1484 				(struct create_lease, Name));
1485 		buf->ccontext.NameLength = cpu_to_le16(4);
1486 		buf->Name[0] = 'R';
1487 		buf->Name[1] = 'q';
1488 		buf->Name[2] = 'L';
1489 		buf->Name[3] = 's';
1490 	}
1491 }
1492 
1493 /**
1494  * parse_lease_state() - parse lease context contained in file open request
1495  * @open_req:	buffer containing smb2 file open(create) request
1496  *
1497  * Return: allocated lease context object on success, otherwise NULL
1498  */
1499 struct lease_ctx_info *parse_lease_state(void *open_req)
1500 {
1501 	struct create_context *cc;
1502 	struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1503 	struct lease_ctx_info *lreq;
1504 
1505 	cc = smb2_find_context_vals(req, SMB2_CREATE_REQUEST_LEASE, 4);
1506 	if (IS_ERR_OR_NULL(cc))
1507 		return NULL;
1508 
1509 	lreq = kzalloc_obj(struct lease_ctx_info, KSMBD_DEFAULT_GFP);
1510 	if (!lreq)
1511 		return NULL;
1512 
1513 	if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) {
1514 		struct create_lease_v2 *lc = (struct create_lease_v2 *)cc;
1515 
1516 		if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
1517 		    sizeof(struct create_lease_v2) - 4)
1518 			goto err_out;
1519 
1520 		memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
1521 		lreq->req_state = lc->lcontext.LeaseState;
1522 		lreq->flags = lc->lcontext.LeaseFlags;
1523 		lreq->epoch = lc->lcontext.Epoch;
1524 		lreq->duration = lc->lcontext.LeaseDuration;
1525 		if (lreq->flags == SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE)
1526 			memcpy(lreq->parent_lease_key, lc->lcontext.ParentLeaseKey,
1527 			       SMB2_LEASE_KEY_SIZE);
1528 		lreq->version = 2;
1529 	} else {
1530 		struct create_lease *lc = (struct create_lease *)cc;
1531 
1532 		if (le16_to_cpu(cc->DataOffset) + le32_to_cpu(cc->DataLength) <
1533 		    sizeof(struct create_lease))
1534 			goto err_out;
1535 
1536 		memcpy(lreq->lease_key, lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
1537 		lreq->req_state = lc->lcontext.LeaseState;
1538 		lreq->flags = lc->lcontext.LeaseFlags;
1539 		lreq->duration = lc->lcontext.LeaseDuration;
1540 		lreq->version = 1;
1541 	}
1542 	return lreq;
1543 err_out:
1544 	kfree(lreq);
1545 	return NULL;
1546 }
1547 
1548 /**
1549  * smb2_find_context_vals() - find a particular context info in open request
1550  * @open_req:	buffer containing smb2 file open(create) request
1551  * @tag:	context name to search for
1552  * @tag_len:	the length of tag
1553  *
1554  * Return:	pointer to requested context, NULL if @str context not found
1555  *		or error pointer if name length is invalid.
1556  */
1557 struct create_context *smb2_find_context_vals(void *open_req, const char *tag, int tag_len)
1558 {
1559 	struct create_context *cc;
1560 	unsigned int next = 0;
1561 	char *name;
1562 	struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1563 	unsigned int remain_len, name_off, name_len, value_off, value_len,
1564 		     cc_len;
1565 
1566 	/*
1567 	 * CreateContextsOffset and CreateContextsLength are guaranteed to
1568 	 * be valid because of ksmbd_smb2_check_message().
1569 	 */
1570 	cc = (struct create_context *)((char *)req +
1571 				       le32_to_cpu(req->CreateContextsOffset));
1572 	remain_len = le32_to_cpu(req->CreateContextsLength);
1573 	do {
1574 		cc = (struct create_context *)((char *)cc + next);
1575 		if (remain_len < offsetof(struct create_context, Buffer))
1576 			return ERR_PTR(-EINVAL);
1577 
1578 		next = le32_to_cpu(cc->Next);
1579 		name_off = le16_to_cpu(cc->NameOffset);
1580 		name_len = le16_to_cpu(cc->NameLength);
1581 		value_off = le16_to_cpu(cc->DataOffset);
1582 		value_len = le32_to_cpu(cc->DataLength);
1583 		cc_len = next ? next : remain_len;
1584 
1585 		if ((next & 0x7) != 0 ||
1586 		    next > remain_len ||
1587 		    name_off != offsetof(struct create_context, Buffer) ||
1588 		    name_len < 4 ||
1589 		    name_off + name_len > cc_len ||
1590 		    (value_off & 0x7) != 0 ||
1591 		    (value_len && value_off < name_off + (name_len < 8 ? 8 : name_len)) ||
1592 		    ((u64)value_off + value_len > cc_len))
1593 			return ERR_PTR(-EINVAL);
1594 
1595 		name = (char *)cc + name_off;
1596 		if (name_len == tag_len && !memcmp(name, tag, name_len))
1597 			return cc;
1598 
1599 		remain_len -= next;
1600 	} while (next != 0);
1601 
1602 	return NULL;
1603 }
1604 
1605 /**
1606  * create_durable_rsp_buf() - create durable handle context
1607  * @cc:	buffer to create durable context response
1608  */
1609 void create_durable_rsp_buf(char *cc)
1610 {
1611 	struct create_durable_rsp *buf;
1612 
1613 	buf = (struct create_durable_rsp *)cc;
1614 	memset(buf, 0, sizeof(struct create_durable_rsp));
1615 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
1616 			(struct create_durable_rsp, Data));
1617 	buf->ccontext.DataLength = cpu_to_le32(8);
1618 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
1619 			(struct create_durable_rsp, Name));
1620 	buf->ccontext.NameLength = cpu_to_le16(4);
1621 	/* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */
1622 	buf->Name[0] = 'D';
1623 	buf->Name[1] = 'H';
1624 	buf->Name[2] = 'n';
1625 	buf->Name[3] = 'Q';
1626 }
1627 
1628 /**
1629  * create_durable_v2_rsp_buf() - create durable handle v2 context
1630  * @cc:	buffer to create durable context response
1631  * @fp: ksmbd file pointer
1632  */
1633 void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp)
1634 {
1635 	struct create_durable_rsp_v2 *buf;
1636 
1637 	buf = (struct create_durable_rsp_v2 *)cc;
1638 	memset(buf, 0, sizeof(struct create_durable_rsp));
1639 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
1640 			(struct create_durable_rsp, Data));
1641 	buf->ccontext.DataLength = cpu_to_le32(8);
1642 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
1643 			(struct create_durable_rsp, Name));
1644 	buf->ccontext.NameLength = cpu_to_le16(4);
1645 	/* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */
1646 	buf->Name[0] = 'D';
1647 	buf->Name[1] = 'H';
1648 	buf->Name[2] = '2';
1649 	buf->Name[3] = 'Q';
1650 
1651 	buf->dcontext.Timeout = cpu_to_le32(fp->durable_timeout);
1652 	if (fp->is_persistent)
1653 		buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1654 }
1655 
1656 /**
1657  * create_mxac_rsp_buf() - create query maximal access context
1658  * @cc:			buffer to create maximal access context response
1659  * @maximal_access:	maximal access
1660  */
1661 void create_mxac_rsp_buf(char *cc, int maximal_access)
1662 {
1663 	struct create_mxac_rsp *buf;
1664 
1665 	buf = (struct create_mxac_rsp *)cc;
1666 	memset(buf, 0, sizeof(struct create_mxac_rsp));
1667 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
1668 			(struct create_mxac_rsp, QueryStatus));
1669 	buf->ccontext.DataLength = cpu_to_le32(8);
1670 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
1671 			(struct create_mxac_rsp, Name));
1672 	buf->ccontext.NameLength = cpu_to_le16(4);
1673 	/* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */
1674 	buf->Name[0] = 'M';
1675 	buf->Name[1] = 'x';
1676 	buf->Name[2] = 'A';
1677 	buf->Name[3] = 'c';
1678 
1679 	buf->QueryStatus = STATUS_SUCCESS;
1680 	buf->MaximalAccess = cpu_to_le32(maximal_access);
1681 }
1682 
1683 void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id)
1684 {
1685 	struct create_disk_id_rsp *buf;
1686 
1687 	buf = (struct create_disk_id_rsp *)cc;
1688 	memset(buf, 0, sizeof(struct create_disk_id_rsp));
1689 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
1690 			(struct create_disk_id_rsp, DiskFileId));
1691 	buf->ccontext.DataLength = cpu_to_le32(32);
1692 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
1693 			(struct create_mxac_rsp, Name));
1694 	buf->ccontext.NameLength = cpu_to_le16(4);
1695 	/* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */
1696 	buf->Name[0] = 'Q';
1697 	buf->Name[1] = 'F';
1698 	buf->Name[2] = 'i';
1699 	buf->Name[3] = 'd';
1700 
1701 	buf->DiskFileId = cpu_to_le64(file_id);
1702 	buf->VolumeId = cpu_to_le64(vol_id);
1703 }
1704 
1705 /**
1706  * create_posix_rsp_buf() - create posix extension context
1707  * @cc:	buffer to create posix on posix response
1708  * @fp: ksmbd file pointer
1709  */
1710 void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)
1711 {
1712 	struct create_posix_rsp *buf;
1713 	struct inode *inode = file_inode(fp->filp);
1714 	struct mnt_idmap *idmap = file_mnt_idmap(fp->filp);
1715 	vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
1716 	vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
1717 
1718 	buf = (struct create_posix_rsp *)cc;
1719 	memset(buf, 0, sizeof(struct create_posix_rsp));
1720 	buf->ccontext.DataOffset = cpu_to_le16(offsetof
1721 			(struct create_posix_rsp, nlink));
1722 	/*
1723 	 * DataLength = nlink(4) + reparse_tag(4) + mode(4) +
1724 	 * domain sid(28) + unix group sid(16).
1725 	 */
1726 	buf->ccontext.DataLength = cpu_to_le32(56);
1727 	buf->ccontext.NameOffset = cpu_to_le16(offsetof
1728 			(struct create_posix_rsp, Name));
1729 	buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
1730 	/* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
1731 	buf->Name[0] = 0x93;
1732 	buf->Name[1] = 0xAD;
1733 	buf->Name[2] = 0x25;
1734 	buf->Name[3] = 0x50;
1735 	buf->Name[4] = 0x9C;
1736 	buf->Name[5] = 0xB4;
1737 	buf->Name[6] = 0x11;
1738 	buf->Name[7] = 0xE7;
1739 	buf->Name[8] = 0xB4;
1740 	buf->Name[9] = 0x23;
1741 	buf->Name[10] = 0x83;
1742 	buf->Name[11] = 0xDE;
1743 	buf->Name[12] = 0x96;
1744 	buf->Name[13] = 0x8B;
1745 	buf->Name[14] = 0xCD;
1746 	buf->Name[15] = 0x7C;
1747 
1748 	buf->nlink = cpu_to_le32(inode->i_nlink);
1749 	buf->reparse_tag = cpu_to_le32(fp->volatile_id);
1750 	buf->mode = cpu_to_le32(inode->i_mode & 0777);
1751 	/*
1752 	 * SidBuffer(44) contain two sids(Domain sid(28), UNIX group sid(16)).
1753 	 * Domain sid(28) = revision(1) + num_subauth(1) + authority(6) +
1754 	 *		    sub_auth(4 * 4(num_subauth)) + RID(4).
1755 	 * UNIX group id(16) = revision(1) + num_subauth(1) + authority(6) +
1756 	 *		       sub_auth(4 * 1(num_subauth)) + RID(4).
1757 	 */
1758 	id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)),
1759 		  SIDOWNER, (struct smb_sid *)&buf->SidBuffer[0]);
1760 	id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)),
1761 		  SIDUNIX_GROUP, (struct smb_sid *)&buf->SidBuffer[28]);
1762 }
1763 
1764 /*
1765  * Find lease object(opinfo) for given lease key/fid from lease
1766  * break/file close path.
1767  */
1768 /**
1769  * lookup_lease_in_table() - find a matching lease info object
1770  * @conn:	connection instance
1771  * @lease_key:	lease key to be searched for
1772  *
1773  * Return:      opinfo if found matching opinfo, otherwise NULL
1774  */
1775 struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
1776 					  char *lease_key)
1777 {
1778 	struct oplock_info *opinfo = NULL, *ret_op = NULL;
1779 	struct lease_table *lt;
1780 	int ret;
1781 
1782 	read_lock(&lease_list_lock);
1783 	list_for_each_entry(lt, &lease_table_list, l_entry) {
1784 		if (!memcmp(lt->client_guid, conn->ClientGUID,
1785 			    SMB2_CLIENT_GUID_SIZE))
1786 			goto found;
1787 	}
1788 
1789 	read_unlock(&lease_list_lock);
1790 	return NULL;
1791 
1792 found:
1793 	rcu_read_lock();
1794 	list_for_each_entry_rcu(opinfo, &lt->lease_list, lease_entry) {
1795 		if (!atomic_inc_not_zero(&opinfo->refcount))
1796 			continue;
1797 		rcu_read_unlock();
1798 		if (!opinfo->op_state || opinfo->op_state == OPLOCK_CLOSING)
1799 			goto op_next;
1800 		if (!(opinfo->o_lease->state &
1801 		      (SMB2_LEASE_HANDLE_CACHING_LE |
1802 		       SMB2_LEASE_WRITE_CACHING_LE)))
1803 			goto op_next;
1804 		ret = compare_guid_key(opinfo, conn->ClientGUID,
1805 				       lease_key);
1806 		if (ret) {
1807 			ksmbd_debug(OPLOCK, "found opinfo\n");
1808 			ret_op = opinfo;
1809 			goto out;
1810 		}
1811 op_next:
1812 		opinfo_put(opinfo);
1813 		rcu_read_lock();
1814 	}
1815 	rcu_read_unlock();
1816 
1817 out:
1818 	read_unlock(&lease_list_lock);
1819 	return ret_op;
1820 }
1821 
1822 int smb2_check_durable_oplock(struct ksmbd_conn *conn,
1823 			      struct ksmbd_share_config *share,
1824 			      struct ksmbd_file *fp,
1825 			      struct lease_ctx_info *lctx,
1826 			      char *name)
1827 {
1828 	struct oplock_info *opinfo = opinfo_get(fp);
1829 	int ret = 0;
1830 
1831 	if (!opinfo)
1832 		return 0;
1833 
1834 	if (opinfo->is_lease == false) {
1835 		if (lctx) {
1836 			pr_err("create context include lease\n");
1837 			ret = -EBADF;
1838 			goto out;
1839 		}
1840 
1841 		if (opinfo->level != SMB2_OPLOCK_LEVEL_BATCH) {
1842 			pr_err("oplock level is not equal to SMB2_OPLOCK_LEVEL_BATCH\n");
1843 			ret = -EBADF;
1844 		}
1845 
1846 		goto out;
1847 	}
1848 
1849 	if (memcmp(conn->ClientGUID, fp->client_guid,
1850 				SMB2_CLIENT_GUID_SIZE)) {
1851 		ksmbd_debug(SMB, "Client guid of fp is not equal to the one of connection\n");
1852 		ret = -EBADF;
1853 		goto out;
1854 	}
1855 
1856 	if (!lctx) {
1857 		ksmbd_debug(SMB, "create context does not include lease\n");
1858 		ret = -EBADF;
1859 		goto out;
1860 	}
1861 
1862 	if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key,
1863 				SMB2_LEASE_KEY_SIZE)) {
1864 		ksmbd_debug(SMB,
1865 			    "lease key of fp does not match lease key in create context\n");
1866 		ret = -EBADF;
1867 		goto out;
1868 	}
1869 
1870 	if (!(opinfo->o_lease->state & SMB2_LEASE_HANDLE_CACHING_LE)) {
1871 		ksmbd_debug(SMB, "lease state does not contain SMB2_LEASE_HANDLE_CACHING\n");
1872 		ret = -EBADF;
1873 		goto out;
1874 	}
1875 
1876 	if (opinfo->o_lease->version != lctx->version) {
1877 		ksmbd_debug(SMB,
1878 			    "lease version of fp does not match the one in create context\n");
1879 		ret = -EBADF;
1880 		goto out;
1881 	}
1882 
1883 	if (!ksmbd_inode_pending_delete(fp))
1884 		ret = ksmbd_validate_name_reconnect(share, fp, name);
1885 out:
1886 	opinfo_put(opinfo);
1887 	return ret;
1888 }
1889