Debian Codegraph @

fs/smb/server/oplock.c

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