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