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