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