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