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