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