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