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