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