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