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