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