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