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