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