blob: d76aa47e19e48363ee97b79dad0ccf88143ac004 [file] [log] [blame]
Namjae Jeone2f34482021-03-16 10:49:09 +09001// 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 "smbstatus.h"
14#include "buffer_pool.h"
15#include "connection.h"
16#include "mgmt/user_session.h"
17#include "mgmt/share_config.h"
18#include "mgmt/tree_connect.h"
19
20static LIST_HEAD(lease_table_list);
21static DEFINE_RWLOCK(lease_list_lock);
22
23/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090024 * alloc_opinfo() - allocate a new opinfo object for oplock info
25 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090026 * @id: fid of open file
27 * @Tid: tree id of connection
Namjae Jeone2f34482021-03-16 10:49:09 +090028 *
29 * Return: allocated opinfo object on success, otherwise NULL
30 */
31static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,
32 uint64_t id, __u16 Tid)
33{
34 struct ksmbd_session *sess = work->sess;
35 struct oplock_info *opinfo;
36
37 opinfo = kzalloc(sizeof(struct oplock_info), GFP_KERNEL);
38 if (!opinfo)
39 return NULL;
40
41 opinfo->sess = sess;
42 opinfo->conn = sess->conn;
43 opinfo->level = OPLOCK_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_LIST_HEAD(&opinfo->interim_list);
50 init_waitqueue_head(&opinfo->oplock_q);
51 init_waitqueue_head(&opinfo->oplock_brk);
52 atomic_set(&opinfo->refcount, 1);
53 atomic_set(&opinfo->breaking_cnt, 0);
54
55 return opinfo;
56}
57
58static 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
67static 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
85static 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
92static int alloc_lease(struct oplock_info *opinfo,
93 struct lease_ctx_info *lctx)
94{
95 struct lease *lease;
96
97 lease = kmalloc(sizeof(struct lease), GFP_KERNEL);
98 if (!lease)
99 return -ENOMEM;
100
101 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
102 lease->state = lctx->req_state;
103 lease->new_state = 0;
104 lease->flags = lctx->flags;
105 lease->duration = lctx->duration;
106 INIT_LIST_HEAD(&opinfo->lease_entry);
107 opinfo->o_lease = lease;
108
109 return 0;
110}
111
112static void free_lease(struct oplock_info *opinfo)
113{
114 struct lease *lease;
115
116 lease = opinfo->o_lease;
117 kfree(lease);
118}
119
120static void free_opinfo(struct oplock_info *opinfo)
121{
122 if (opinfo->is_lease)
123 free_lease(opinfo);
124 kfree(opinfo);
125}
126
127static inline void opinfo_free_rcu(struct rcu_head *rcu_head)
128{
129 struct oplock_info *opinfo;
130
131 opinfo = container_of(rcu_head, struct oplock_info, rcu_head);
132 free_opinfo(opinfo);
133}
134
135struct oplock_info *opinfo_get(struct ksmbd_file *fp)
136{
137 struct oplock_info *opinfo;
138
139 rcu_read_lock();
140 opinfo = rcu_dereference(fp->f_opinfo);
141 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
142 opinfo = NULL;
143 rcu_read_unlock();
144
145 return opinfo;
146}
147
148static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci)
149{
150 struct oplock_info *opinfo;
151
152 if (list_empty(&ci->m_op_list))
153 return NULL;
154
155 rcu_read_lock();
156 opinfo = list_first_or_null_rcu(&ci->m_op_list, struct oplock_info,
157 op_entry);
158 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
159 opinfo = NULL;
160 rcu_read_unlock();
161
162 return opinfo;
163}
164
165void opinfo_put(struct oplock_info *opinfo)
166{
167 if (!atomic_dec_and_test(&opinfo->refcount))
168 return;
169
170 call_rcu(&opinfo->rcu_head, opinfo_free_rcu);
171}
172
173static void opinfo_add(struct oplock_info *opinfo)
174{
175 struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
176
177 write_lock(&ci->m_lock);
178 list_add_rcu(&opinfo->op_entry, &ci->m_op_list);
179 write_unlock(&ci->m_lock);
180}
181
182static void opinfo_del(struct oplock_info *opinfo)
183{
184 struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
185
186 if (opinfo->is_lease) {
187 write_lock(&lease_list_lock);
188 lease_del_list(opinfo);
189 write_unlock(&lease_list_lock);
190 }
191 write_lock(&ci->m_lock);
192 list_del_rcu(&opinfo->op_entry);
193 write_unlock(&ci->m_lock);
194}
195
196static unsigned long opinfo_count(struct ksmbd_file *fp)
197{
198 if (ksmbd_stream_fd(fp))
199 return atomic_read(&fp->f_ci->sop_count);
200 else
201 return atomic_read(&fp->f_ci->op_count);
202}
203
204static void opinfo_count_inc(struct ksmbd_file *fp)
205{
206 if (ksmbd_stream_fd(fp))
207 return atomic_inc(&fp->f_ci->sop_count);
208 else
209 return atomic_inc(&fp->f_ci->op_count);
210}
211
212static void opinfo_count_dec(struct ksmbd_file *fp)
213{
214 if (ksmbd_stream_fd(fp))
215 return atomic_dec(&fp->f_ci->sop_count);
216 else
217 return atomic_dec(&fp->f_ci->op_count);
218}
219
220/**
221 * opinfo_write_to_read() - convert a write oplock to read oplock
222 * @opinfo: current oplock info
223 *
224 * Return: 0 on success, otherwise -EINVAL
225 */
226int opinfo_write_to_read(struct oplock_info *opinfo)
227{
228 struct lease *lease = opinfo->o_lease;
229
230 if (!((opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) ||
231 (opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))) {
232 ksmbd_err("bad oplock(0x%x)\n", opinfo->level);
233 if (opinfo->is_lease)
234 ksmbd_err("lease state(0x%x)\n", lease->state);
235 return -EINVAL;
236 }
237 opinfo->level = SMB2_OPLOCK_LEVEL_II;
238
239 if (opinfo->is_lease)
240 lease->state = lease->new_state;
241 return 0;
242}
243
244/**
245 * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock
246 * @opinfo: current oplock info
247 *
248 * Return: 0 on success, otherwise -EINVAL
249 */
250int opinfo_read_handle_to_read(struct oplock_info *opinfo)
251{
252 struct lease *lease = opinfo->o_lease;
253
254 lease->state = lease->new_state;
255 opinfo->level = SMB2_OPLOCK_LEVEL_II;
256 return 0;
257}
258
259/**
260 * opinfo_write_to_none() - convert a write oplock to none
261 * @opinfo: current oplock info
262 *
263 * Return: 0 on success, otherwise -EINVAL
264 */
265int opinfo_write_to_none(struct oplock_info *opinfo)
266{
267 struct lease *lease = opinfo->o_lease;
268
269 if (!((opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) ||
270 (opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))) {
271 ksmbd_err("bad oplock(0x%x)\n", opinfo->level);
272 if (opinfo->is_lease)
273 ksmbd_err("lease state(0x%x)\n",
274 lease->state);
275 return -EINVAL;
276 }
277 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
278 if (opinfo->is_lease)
279 lease->state = lease->new_state;
280 return 0;
281}
282
283/**
284 * opinfo_read_to_none() - convert a write read to none
285 * @opinfo: current oplock info
286 *
287 * Return: 0 on success, otherwise -EINVAL
288 */
289int opinfo_read_to_none(struct oplock_info *opinfo)
290{
291 struct lease *lease = opinfo->o_lease;
292
293 if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
294 ksmbd_err("bad oplock(0x%x)\n", opinfo->level);
295 if (opinfo->is_lease)
296 ksmbd_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 * lease_read_to_write() - upgrade lease state from read to write
307 * @opinfo: current lease info
308 *
309 * Return: 0 on success, otherwise -EINVAL
310 */
311int lease_read_to_write(struct oplock_info *opinfo)
312{
313 struct lease *lease = opinfo->o_lease;
314
315 if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) {
316 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n",
317 lease->state);
318 return -EINVAL;
319 }
320
321 lease->new_state = SMB2_LEASE_NONE_LE;
322 lease->state |= SMB2_LEASE_WRITE_CACHING_LE;
323 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
324 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
325 else
326 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
327 return 0;
328}
329
330/**
331 * lease_none_upgrade() - upgrade lease state from none
332 * @opinfo: current lease info
333 * @new_state: new lease state
334 *
335 * Return: 0 on success, otherwise -EINVAL
336 */
337static int lease_none_upgrade(struct oplock_info *opinfo,
338 __le32 new_state)
339{
340 struct lease *lease = opinfo->o_lease;
341
342 if (!(lease->state == SMB2_LEASE_NONE_LE)) {
343 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n",
344 lease->state);
345 return -EINVAL;
346 }
347
348 lease->new_state = SMB2_LEASE_NONE_LE;
349 lease->state = new_state;
350 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
351 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
352 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
353 else
354 opinfo->level = SMB2_OPLOCK_LEVEL_II;
355 else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
356 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
357 else if (lease->state & SMB2_LEASE_READ_CACHING_LE)
358 opinfo->level = SMB2_OPLOCK_LEVEL_II;
359
360 return 0;
361}
362
363/**
364 * close_id_del_oplock() - release oplock object at file close time
365 * @fp: ksmbd file pointer
366 */
367void close_id_del_oplock(struct ksmbd_file *fp)
368{
369 struct oplock_info *opinfo;
370
371 if (S_ISDIR(file_inode(fp->filp)->i_mode))
372 return;
373
374 opinfo = opinfo_get(fp);
375 if (!opinfo)
376 return;
377
378 opinfo_del(opinfo);
379
380 rcu_assign_pointer(fp->f_opinfo, NULL);
381 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
382 opinfo->op_state = OPLOCK_CLOSING;
383 wake_up_interruptible_all(&opinfo->oplock_q);
384 if (opinfo->is_lease) {
385 atomic_set(&opinfo->breaking_cnt, 0);
386 wake_up_interruptible_all(&opinfo->oplock_brk);
387 }
388 }
389
390 opinfo_count_dec(fp);
391 atomic_dec(&opinfo->refcount);
392 opinfo_put(opinfo);
393}
394
395/**
396 * grant_write_oplock() - grant exclusive/batch oplock or write lease
397 * @opinfo_new: new oplock info object
398 * @req_oplock: request oplock
399 * @lctx: lease context information
400 *
401 * Return: 0
402 */
403static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock,
404 struct lease_ctx_info *lctx)
405{
406 struct lease *lease = opinfo_new->o_lease;
407
408 if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
409 opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
410 else
411 opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
412
413 if (lctx) {
414 lease->state = lctx->req_state;
415 memcpy(lease->lease_key, lctx->lease_key,
416 SMB2_LEASE_KEY_SIZE);
417 }
418}
419
420/**
421 * grant_read_oplock() - grant level2 oplock or read lease
422 * @opinfo_new: new oplock info object
423 * @lctx: lease context information
424 *
425 * Return: 0
426 */
427static void grant_read_oplock(struct oplock_info *opinfo_new,
428 struct lease_ctx_info *lctx)
429{
430 struct lease *lease = opinfo_new->o_lease;
431
432 opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
433
434 if (lctx) {
435 lease->state = SMB2_LEASE_READ_CACHING_LE;
436 if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE)
437 lease->state |= SMB2_LEASE_HANDLE_CACHING_LE;
438 memcpy(lease->lease_key, lctx->lease_key,
439 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 */
450static 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,
460 SMB2_LEASE_KEY_SIZE);
461 }
462}
463
Namjae Jeone2f34482021-03-16 10:49:09 +0900464static inline int compare_guid_key(struct oplock_info *opinfo,
465 const char *guid1, const char *key1)
466{
467 const char *guid2, *key2;
468
469 guid2 = opinfo->conn->ClientGUID;
470 key2 = opinfo->o_lease->lease_key;
471 if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) &&
472 !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE))
473 return 1;
474
475 return 0;
476}
477
478/**
479 * same_client_has_lease() - check whether current lease request is
480 * from lease owner of file
481 * @ci: master file pointer
482 * @client_guid: Client GUID
483 * @lctx: lease context information
484 *
485 * Return: oplock(lease) object on success, otherwise NULL
486 */
487static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,
488 char *client_guid, 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 read_lock(&ci->m_lock);
503 list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
504 if (!opinfo->is_lease)
505 continue;
506 read_unlock(&ci->m_lock);
507 lease = opinfo->o_lease;
508
509 ret = compare_guid_key(opinfo, client_guid, lctx->lease_key);
510 if (ret) {
511 m_opinfo = opinfo;
512 /* skip upgrading lease about breaking lease */
513 if (atomic_read(&opinfo->breaking_cnt)) {
514 read_lock(&ci->m_lock);
515 continue;
516 }
517
518 /* upgrading lease */
519 if ((atomic_read(&ci->op_count) +
520 atomic_read(&ci->sop_count)) == 1) {
521 if (lease->state ==
522 (lctx->req_state & lease->state)) {
523 lease->state |= lctx->req_state;
524 if (lctx->req_state &
525 SMB2_LEASE_WRITE_CACHING_LE)
526 lease_read_to_write(opinfo);
527 }
528 } else if ((atomic_read(&ci->op_count) +
529 atomic_read(&ci->sop_count)) > 1) {
530 if (lctx->req_state ==
531 (SMB2_LEASE_READ_CACHING_LE |
532 SMB2_LEASE_HANDLE_CACHING_LE))
533 lease->state = lctx->req_state;
534 }
535
536 if (lctx->req_state && lease->state ==
537 SMB2_LEASE_NONE_LE)
538 lease_none_upgrade(opinfo, lctx->req_state);
539 }
540 read_lock(&ci->m_lock);
541 }
542 read_unlock(&ci->m_lock);
543
544 return m_opinfo;
545}
546
547static void wait_for_break_ack(struct oplock_info *opinfo)
548{
549 int rc = 0;
550
551 rc = wait_event_interruptible_timeout(opinfo->oplock_q,
552 opinfo->op_state == OPLOCK_STATE_NONE ||
553 opinfo->op_state == OPLOCK_CLOSING,
554 OPLOCK_WAIT_TIME);
555
556 /* is this a timeout ? */
557 if (!rc) {
558 if (opinfo->is_lease)
559 opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
560 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
561 opinfo->op_state = OPLOCK_STATE_NONE;
562 }
563}
564
565static void wake_up_oplock_break(struct oplock_info *opinfo)
566{
567 clear_bit_unlock(0, &opinfo->pending_break);
568 /* memory barrier is needed for wake_up_bit() */
569 smp_mb__after_atomic();
570 wake_up_bit(&opinfo->pending_break, 0);
571}
572
573static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)
574{
575 while (test_and_set_bit(0, &opinfo->pending_break)) {
576 wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE);
577
578 /* Not immediately break to none. */
579 opinfo->open_trunc = 0;
580
581 if (opinfo->op_state == OPLOCK_CLOSING)
582 return -ENOENT;
583 else if (!opinfo->is_lease && opinfo->level <= req_op_level)
584 return 1;
585 }
586
587 if (!opinfo->is_lease && opinfo->level <= req_op_level) {
588 wake_up_oplock_break(opinfo);
589 return 1;
590 }
591 return 0;
592}
593
594static inline int allocate_oplock_break_buf(struct ksmbd_work *work)
595{
Namjae Jeon20ea7fd2021-03-30 12:40:47 +0900596 work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +0900597 if (!work->response_buf)
598 return -ENOMEM;
599 work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
600 return 0;
601}
602
603/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900604 * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn
Namjae Jeone2f34482021-03-16 10:49:09 +0900605 * to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900606 * @wk: smb work object
Namjae Jeone2f34482021-03-16 10:49:09 +0900607 *
608 * There are two ways this function can be called. 1- while file open we break
609 * from exclusive/batch lock to levelII oplock and 2- while file write/truncate
610 * we break from levelII oplock no oplock.
Namjae Jeone5066492021-03-30 12:35:23 +0900611 * work->request_buf contains oplock_info.
Namjae Jeone2f34482021-03-16 10:49:09 +0900612 */
613static void __smb2_oplock_break_noti(struct work_struct *wk)
614{
615 struct smb2_oplock_break *rsp = NULL;
616 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
617 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +0900618 struct oplock_break_info *br_info = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900619 struct smb2_hdr *rsp_hdr;
620 struct ksmbd_file *fp;
621
622 fp = ksmbd_lookup_durable_fd(br_info->fid);
623 if (!fp) {
624 atomic_dec(&conn->r_count);
625 ksmbd_free_work_struct(work);
626 return;
627 }
628
629 if (allocate_oplock_break_buf(work)) {
630 ksmbd_err("smb2_allocate_rsp_buf failed! ");
631 atomic_dec(&conn->r_count);
Namjae Jeone2f34482021-03-16 10:49:09 +0900632 ksmbd_fd_put(work, fp);
Dan Carpentera2ba2702021-03-18 16:12:54 +0300633 ksmbd_free_work_struct(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900634 return;
635 }
636
Namjae Jeone5066492021-03-30 12:35:23 +0900637 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900638 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
639 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
640 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
641 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
642 rsp_hdr->CreditRequest = cpu_to_le16(0);
643 rsp_hdr->Command = SMB2_OPLOCK_BREAK;
644 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
645 rsp_hdr->NextCommand = 0;
646 rsp_hdr->MessageId = cpu_to_le64(-1);
647 rsp_hdr->Id.SyncId.ProcessId = 0;
648 rsp_hdr->Id.SyncId.TreeId = 0;
649 rsp_hdr->SessionId = 0;
650 memset(rsp_hdr->Signature, 0, 16);
651
652
Namjae Jeone5066492021-03-30 12:35:23 +0900653 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900654
655 rsp->StructureSize = cpu_to_le16(24);
656 if (!br_info->open_trunc &&
657 (br_info->level == SMB2_OPLOCK_LEVEL_BATCH ||
658 br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
659 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II;
660 else
661 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
662 rsp->Reserved = 0;
663 rsp->Reserved2 = 0;
664 rsp->PersistentFid = cpu_to_le64(fp->persistent_id);
665 rsp->VolatileFid = cpu_to_le64(fp->volatile_id);
666
667 inc_rfc1001_len(rsp, 24);
668
669 ksmbd_debug(OPLOCK,
670 "sending oplock break v_id %llu p_id = %llu lock level = %d\n",
671 rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel);
672
673 ksmbd_fd_put(work, fp);
674 ksmbd_conn_write(work);
675 ksmbd_free_work_struct(work);
676 atomic_dec(&conn->r_count);
677}
678
679/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900680 * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock
Namjae Jeone2f34482021-03-16 10:49:09 +0900681 * break command from server to client
682 * @opinfo: oplock info object
Namjae Jeone2f34482021-03-16 10:49:09 +0900683 *
684 * Return: 0 on success, otherwise error
685 */
686static int smb2_oplock_break_noti(struct oplock_info *opinfo)
687{
688 struct ksmbd_conn *conn = opinfo->conn;
689 struct oplock_break_info *br_info;
690 int ret = 0;
691 struct ksmbd_work *work = ksmbd_alloc_work_struct();
692
693 if (!work)
694 return -ENOMEM;
695
696 br_info = kmalloc(sizeof(struct oplock_break_info), GFP_KERNEL);
697 if (!br_info) {
698 ksmbd_free_work_struct(work);
699 return -ENOMEM;
700 }
701
702 br_info->level = opinfo->level;
703 br_info->fid = opinfo->fid;
704 br_info->open_trunc = opinfo->open_trunc;
705
706 work->request_buf = (char *)br_info;
707 work->conn = conn;
708 work->sess = opinfo->sess;
709
710 atomic_inc(&conn->r_count);
711 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
712 INIT_WORK(&work->work, __smb2_oplock_break_noti);
713 ksmbd_queue_work(work);
714
715 wait_for_break_ack(opinfo);
716 } else {
717 __smb2_oplock_break_noti(&work->work);
718 if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
719 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
720 }
721 return ret;
722}
723
724/**
725 * __smb2_lease_break_noti() - send lease break command from server
726 * to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900727 * @wk: smb work object
Namjae Jeone2f34482021-03-16 10:49:09 +0900728 */
729static void __smb2_lease_break_noti(struct work_struct *wk)
730{
731 struct smb2_lease_break *rsp = NULL;
732 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
Namjae Jeone5066492021-03-30 12:35:23 +0900733 struct lease_break_info *br_info = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900734 struct ksmbd_conn *conn = work->conn;
735 struct smb2_hdr *rsp_hdr;
736
737 if (allocate_oplock_break_buf(work)) {
738 ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! ");
739 ksmbd_free_work_struct(work);
740 atomic_dec(&conn->r_count);
741 return;
742 }
743
Namjae Jeone5066492021-03-30 12:35:23 +0900744 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900745 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
746 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
747 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
748 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
749 rsp_hdr->CreditRequest = cpu_to_le16(0);
750 rsp_hdr->Command = SMB2_OPLOCK_BREAK;
751 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
752 rsp_hdr->NextCommand = 0;
753 rsp_hdr->MessageId = cpu_to_le64(-1);
754 rsp_hdr->Id.SyncId.ProcessId = 0;
755 rsp_hdr->Id.SyncId.TreeId = 0;
756 rsp_hdr->SessionId = 0;
757 memset(rsp_hdr->Signature, 0, 16);
758
Namjae Jeone5066492021-03-30 12:35:23 +0900759 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900760 rsp->StructureSize = cpu_to_le16(44);
761 rsp->Reserved = 0;
762 rsp->Flags = 0;
763
764 if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE |
765 SMB2_LEASE_HANDLE_CACHING_LE))
766 rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
767
768 memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE);
769 rsp->CurrentLeaseState = br_info->curr_state;
770 rsp->NewLeaseState = br_info->new_state;
771 rsp->BreakReason = 0;
772 rsp->AccessMaskHint = 0;
773 rsp->ShareMaskHint = 0;
774
775 inc_rfc1001_len(rsp, 44);
776
777 ksmbd_conn_write(work);
778 ksmbd_free_work_struct(work);
779 atomic_dec(&conn->r_count);
780}
781
782/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900783 * smb2_lease_break_noti() - break lease when a new client request
Namjae Jeone2f34482021-03-16 10:49:09 +0900784 * write lease
785 * @opinfo: conains lease state information
Namjae Jeone2f34482021-03-16 10:49:09 +0900786 *
787 * Return: 0 on success, otherwise error
788 */
789static int smb2_lease_break_noti(struct oplock_info *opinfo)
790{
791 struct ksmbd_conn *conn = opinfo->conn;
792 struct list_head *tmp, *t;
793 struct ksmbd_work *work;
794 struct lease_break_info *br_info;
795 struct lease *lease = opinfo->o_lease;
796
797 work = ksmbd_alloc_work_struct();
798 if (!work)
799 return -ENOMEM;
800
801 br_info = kmalloc(sizeof(struct lease_break_info), GFP_KERNEL);
802 if (!br_info) {
803 ksmbd_free_work_struct(work);
804 return -ENOMEM;
805 }
806
807 br_info->curr_state = lease->state;
808 br_info->new_state = lease->new_state;
809 memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE);
810
811 work->request_buf = (char *)br_info;
812 work->conn = conn;
813 work->sess = opinfo->sess;
814
815 atomic_inc(&conn->r_count);
816 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
817 list_for_each_safe(tmp, t, &opinfo->interim_list) {
818 struct ksmbd_work *in_work;
819
820 in_work = list_entry(tmp, struct ksmbd_work,
821 interim_entry);
822 setup_async_work(in_work, NULL, NULL);
823 smb2_send_interim_resp(in_work, STATUS_PENDING);
824 list_del(&in_work->interim_entry);
825 }
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
839static 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(
849 opinfo->oplock_brk,
850 atomic_read(&opinfo->breaking_cnt) == 0,
851 HZ);
852 if (!ret)
853 atomic_set(&opinfo->breaking_cnt, 0);
854 }
855}
856
857static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level)
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
871 err = oplock_break_pending(brk_opinfo, req_op_level);
872 if (err)
873 return err < 0 ? err : 0;
874
875 if (brk_opinfo->open_trunc) {
876 /*
877 * Create overwrite break trigger the lease break to
878 * none.
879 */
880 lease->new_state = SMB2_LEASE_NONE_LE;
881 } else {
882 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) {
883 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
884 lease->new_state =
885 SMB2_LEASE_READ_CACHING_LE |
886 SMB2_LEASE_HANDLE_CACHING_LE;
887 else
888 lease->new_state =
889 SMB2_LEASE_READ_CACHING_LE;
890 } else {
891 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
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 brk_opinfo->op_state = OPLOCK_ACK_WAIT;
902 else
903 atomic_dec(&brk_opinfo->breaking_cnt);
904 } else {
905 err = oplock_break_pending(brk_opinfo, req_op_level);
906 if (err)
907 return err < 0 ? err : 0;
908
909 if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
910 brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
911 brk_opinfo->op_state = OPLOCK_ACK_WAIT;
912 }
913
914 if (brk_opinfo->is_lease)
915 err = smb2_lease_break_noti(brk_opinfo);
916 else
917 err = smb2_oplock_break_noti(brk_opinfo);
918
919 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level);
920 if (brk_opinfo->op_state == OPLOCK_CLOSING)
921 err = -ENOENT;
922 wake_up_oplock_break(brk_opinfo);
923
924 wait_lease_breaking(brk_opinfo);
925
926 return err;
927}
928
929void destroy_lease_table(struct ksmbd_conn *conn)
930{
931 struct lease_table *lb, *lbtmp;
932 struct oplock_info *opinfo;
933
934 write_lock(&lease_list_lock);
935 if (list_empty(&lease_table_list)) {
936 write_unlock(&lease_list_lock);
937 return;
938 }
939
940 list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) {
941 if (conn && memcmp(lb->client_guid, conn->ClientGUID,
942 SMB2_CLIENT_GUID_SIZE))
943 continue;
944again:
945 rcu_read_lock();
946 list_for_each_entry_rcu(opinfo, &lb->lease_list,
947 lease_entry) {
948 rcu_read_unlock();
949 lease_del_list(opinfo);
950 goto again;
951 }
952 rcu_read_unlock();
953 list_del(&lb->l_entry);
954 kfree(lb);
955 }
956 write_unlock(&lease_list_lock);
957}
958
959int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
960 struct lease_ctx_info *lctx)
961{
962 struct oplock_info *opinfo;
963 int err = 0;
964 struct lease_table *lb;
965
966 if (!lctx)
967 return err;
968
969 read_lock(&lease_list_lock);
970 if (list_empty(&lease_table_list)) {
971 read_unlock(&lease_list_lock);
972 return 0;
973 }
974
975 list_for_each_entry(lb, &lease_table_list, l_entry) {
976 if (!memcmp(lb->client_guid, sess->conn->ClientGUID,
977 SMB2_CLIENT_GUID_SIZE))
978 goto found;
979 }
980 read_unlock(&lease_list_lock);
981
982 return 0;
983
984found:
985 rcu_read_lock();
986 list_for_each_entry_rcu(opinfo, &lb->lease_list,
987 lease_entry) {
988 if (!atomic_inc_not_zero(&opinfo->refcount))
989 continue;
990 rcu_read_unlock();
991 if (opinfo->o_fp->f_ci == ci)
992 goto op_next;
993 err = compare_guid_key(opinfo,
994 sess->conn->ClientGUID,
995 lctx->lease_key);
996 if (err) {
997 err = -EINVAL;
998 ksmbd_debug(OPLOCK,
999 "found same lease key is already used in other files\n");
1000 opinfo_put(opinfo);
1001 goto out;
1002 }
1003op_next:
1004 opinfo_put(opinfo);
1005 rcu_read_lock();
1006 }
1007 rcu_read_unlock();
1008
1009out:
1010 read_unlock(&lease_list_lock);
1011 return err;
1012}
1013
1014static void copy_lease(struct oplock_info *op1, struct oplock_info *op2)
1015{
1016 struct lease *lease1 = op1->o_lease;
1017 struct lease *lease2 = op2->o_lease;
1018
1019 op2->level = op1->level;
1020 lease2->state = lease1->state;
1021 memcpy(lease2->lease_key, lease1->lease_key,
1022 SMB2_LEASE_KEY_SIZE);
1023 lease2->duration = lease1->duration;
1024 lease2->flags = lease1->flags;
1025}
1026
1027static int add_lease_global_list(struct oplock_info *opinfo)
1028{
1029 struct lease_table *lb;
1030
1031 read_lock(&lease_list_lock);
1032 list_for_each_entry(lb, &lease_table_list, l_entry) {
1033 if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID,
1034 SMB2_CLIENT_GUID_SIZE)) {
1035 opinfo->o_lease->l_lb = lb;
1036 lease_add_list(opinfo);
1037 read_unlock(&lease_list_lock);
1038 return 0;
1039 }
1040 }
1041 read_unlock(&lease_list_lock);
1042
1043 lb = kmalloc(sizeof(struct lease_table), GFP_KERNEL);
1044 if (!lb)
1045 return -ENOMEM;
1046
1047 memcpy(lb->client_guid, opinfo->conn->ClientGUID,
1048 SMB2_CLIENT_GUID_SIZE);
1049 INIT_LIST_HEAD(&lb->lease_list);
1050 spin_lock_init(&lb->lb_lock);
1051 opinfo->o_lease->l_lb = lb;
1052 lease_add_list(opinfo);
1053 lb_add(lb);
1054 return 0;
1055}
1056
1057static void set_oplock_level(struct oplock_info *opinfo, int level,
1058 struct lease_ctx_info *lctx)
1059{
1060 switch (level) {
1061 case SMB2_OPLOCK_LEVEL_BATCH:
1062 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
1063 grant_write_oplock(opinfo,
1064 level, lctx);
1065 break;
1066 case SMB2_OPLOCK_LEVEL_II:
1067 grant_read_oplock(opinfo, lctx);
1068 break;
1069 default:
1070 grant_none_oplock(opinfo, lctx);
1071 break;
1072 }
1073}
1074
1075/**
1076 * smb_grant_oplock() - handle oplock/lease request on file open
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001077 * @work: smb work
1078 * @req_op_level: oplock level
1079 * @pid: id of open file
1080 * @fp: ksmbd file pointer
1081 * @tid: Tree id of connection
1082 * @lctx: lease context information on file open
1083 * @share_ret: share mode
Namjae Jeone2f34482021-03-16 10:49:09 +09001084 *
1085 * Return: 0 on success, otherwise error
1086 */
1087int smb_grant_oplock(struct ksmbd_work *work,
1088 int req_op_level,
1089 uint64_t pid,
1090 struct ksmbd_file *fp,
1091 __u16 tid,
1092 struct lease_ctx_info *lctx,
1093 int share_ret)
1094{
1095 struct ksmbd_session *sess = work->sess;
1096 int err = 0;
1097 struct oplock_info *opinfo = NULL, *prev_opinfo = NULL;
1098 struct ksmbd_inode *ci = fp->f_ci;
1099 bool prev_op_has_lease;
1100 __le32 prev_op_state = 0;
1101
1102 /* not support directory lease */
1103 if (S_ISDIR(file_inode(fp->filp)->i_mode)) {
1104 if (lctx)
1105 lctx->dlease = 1;
1106 return 0;
1107 }
1108
1109 opinfo = alloc_opinfo(work, pid, tid);
1110 if (!opinfo)
1111 return -ENOMEM;
1112
1113 if (lctx) {
1114 err = alloc_lease(opinfo, lctx);
1115 if (err)
1116 goto err_out;
1117 opinfo->is_lease = 1;
1118 }
1119
1120 /* ci does not have any oplock */
1121 if (!opinfo_count(fp))
1122 goto set_lev;
1123
1124 /* grant none-oplock if second open is trunc */
1125 if (ATTR_FP(fp)) {
1126 req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1127 goto set_lev;
1128 }
1129
1130 if (lctx) {
1131 struct oplock_info *m_opinfo;
1132
1133 /* is lease already granted ? */
1134 m_opinfo = same_client_has_lease(ci, sess->conn->ClientGUID,
1135 lctx);
1136 if (m_opinfo) {
1137 copy_lease(m_opinfo, opinfo);
1138 if (atomic_read(&m_opinfo->breaking_cnt))
1139 opinfo->o_lease->flags =
1140 SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE;
1141 goto out;
1142 }
1143 }
1144 prev_opinfo = opinfo_get_list(ci);
1145 if (!prev_opinfo ||
1146 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx))
1147 goto set_lev;
1148 prev_op_has_lease = prev_opinfo->is_lease;
1149 if (prev_op_has_lease)
1150 prev_op_state = prev_opinfo->o_lease->state;
1151
1152 if (share_ret < 0 &&
1153 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
1154 err = share_ret;
1155 opinfo_put(prev_opinfo);
1156 goto err_out;
1157 }
1158
1159 if ((prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH) &&
1160 (prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
1161 opinfo_put(prev_opinfo);
1162 goto op_break_not_needed;
1163 }
1164
1165 list_add(&work->interim_entry, &prev_opinfo->interim_list);
1166 err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II);
1167 opinfo_put(prev_opinfo);
1168 if (err == -ENOENT)
1169 goto set_lev;
1170 /* Check all oplock was freed by close */
1171 else if (err < 0)
1172 goto err_out;
1173
1174op_break_not_needed:
1175 if (share_ret < 0) {
1176 err = share_ret;
1177 goto err_out;
1178 }
1179
1180 if (req_op_level != SMB2_OPLOCK_LEVEL_NONE)
1181 req_op_level = SMB2_OPLOCK_LEVEL_II;
1182
1183 /* grant fixed oplock on stacked locking between lease and oplock */
1184 if (prev_op_has_lease && !lctx)
1185 if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)
1186 req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1187
1188 if (!prev_op_has_lease && lctx) {
1189 req_op_level = SMB2_OPLOCK_LEVEL_II;
1190 lctx->req_state = SMB2_LEASE_READ_CACHING_LE;
1191 }
1192
1193set_lev:
1194 set_oplock_level(opinfo, req_op_level, lctx);
1195
1196out:
1197 rcu_assign_pointer(fp->f_opinfo, opinfo);
1198 opinfo->o_fp = fp;
1199
1200 opinfo_count_inc(fp);
1201 opinfo_add(opinfo);
1202 if (opinfo->is_lease) {
1203 err = add_lease_global_list(opinfo);
1204 if (err)
1205 goto err_out;
1206 }
1207
1208 return 0;
1209err_out:
1210 free_opinfo(opinfo);
1211 return err;
1212}
1213
1214/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001215 * smb_break_all_write_oplock() - break batch/exclusive oplock to level2
Namjae Jeone2f34482021-03-16 10:49:09 +09001216 * @work: smb work
1217 * @fp: ksmbd file pointer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001218 * @is_trunc: truncate on open
Namjae Jeone2f34482021-03-16 10:49:09 +09001219 */
1220static void smb_break_all_write_oplock(struct ksmbd_work *work,
1221 struct ksmbd_file *fp, int is_trunc)
1222{
1223 struct oplock_info *brk_opinfo;
1224
1225 brk_opinfo = opinfo_get_list(fp->f_ci);
1226 if (!brk_opinfo)
1227 return;
1228 if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
1229 brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1230 opinfo_put(brk_opinfo);
1231 return;
1232 }
1233
1234 brk_opinfo->open_trunc = is_trunc;
1235 list_add(&work->interim_entry, &brk_opinfo->interim_list);
1236 oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II);
1237 opinfo_put(brk_opinfo);
1238}
1239
1240/**
1241 * smb_break_all_levII_oplock() - send level2 oplock or read lease break command
1242 * from server to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001243 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +09001244 * @fp: ksmbd file pointer
1245 * @is_trunc: truncate on open
1246 */
1247void smb_break_all_levII_oplock(struct ksmbd_work *work,
1248 struct ksmbd_file *fp, int is_trunc)
1249{
1250 struct oplock_info *op, *brk_op;
1251 struct ksmbd_inode *ci;
1252 struct ksmbd_conn *conn = work->sess->conn;
1253
1254 if (!test_share_config_flag(work->tcon->share_conf,
1255 KSMBD_SHARE_FLAG_OPLOCKS)) {
1256 return;
1257 }
1258
1259 ci = fp->f_ci;
1260 op = opinfo_get(fp);
1261
1262 rcu_read_lock();
1263 list_for_each_entry_rcu(brk_op, &ci->m_op_list, op_entry) {
1264 if (!atomic_inc_not_zero(&brk_op->refcount))
1265 continue;
1266 rcu_read_unlock();
1267 if (brk_op->is_lease && (brk_op->o_lease->state &
1268 (~(SMB2_LEASE_READ_CACHING_LE |
1269 SMB2_LEASE_HANDLE_CACHING_LE)))) {
1270 ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n",
1271 brk_op->o_lease->state);
1272 goto next;
1273 } else if (brk_op->level !=
1274 SMB2_OPLOCK_LEVEL_II) {
1275 ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n",
1276 brk_op->level);
1277 goto next;
1278 }
1279
1280 /* Skip oplock being break to none */
1281 if (brk_op->is_lease && (brk_op->o_lease->new_state ==
1282 SMB2_LEASE_NONE_LE) &&
1283 atomic_read(&brk_op->breaking_cnt))
1284 goto next;
1285
1286 if (op && op->is_lease &&
1287 brk_op->is_lease &&
1288 !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID,
1289 SMB2_CLIENT_GUID_SIZE) &&
1290 !memcmp(op->o_lease->lease_key,
1291 brk_op->o_lease->lease_key,
1292 SMB2_LEASE_KEY_SIZE))
1293 goto next;
1294 brk_op->open_trunc = is_trunc;
1295 oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE);
1296next:
1297 opinfo_put(brk_op);
1298 rcu_read_lock();
1299 }
1300 rcu_read_unlock();
1301
1302 if (op)
1303 opinfo_put(op);
1304}
1305
1306/**
1307 * smb_break_all_oplock() - break both batch/exclusive and level2 oplock
1308 * @work: smb work
1309 * @fp: ksmbd file pointer
1310 */
1311void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp)
1312{
1313 if (!test_share_config_flag(work->tcon->share_conf,
1314 KSMBD_SHARE_FLAG_OPLOCKS))
1315 return;
1316
1317 smb_break_all_write_oplock(work, fp, 1);
1318 smb_break_all_levII_oplock(work, fp, 1);
1319}
1320
1321/**
1322 * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type
1323 * @lease_state: lease type
1324 *
1325 * Return: 0 if no mapping, otherwise corresponding oplock type
1326 */
1327__u8 smb2_map_lease_to_oplock(__le32 lease_state)
1328{
1329 if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE |
1330 SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_WRITE_CACHING_LE))
1331 return SMB2_OPLOCK_LEVEL_BATCH;
1332 else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE &&
1333 lease_state & SMB2_LEASE_WRITE_CACHING_LE) {
1334 if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE))
1335 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
1336 } else if (lease_state & SMB2_LEASE_READ_CACHING_LE)
1337 return SMB2_OPLOCK_LEVEL_II;
1338 return 0;
1339}
1340
1341/**
1342 * create_lease_buf() - create lease context for open cmd response
1343 * @rbuf: buffer to create lease context response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001344 * @lease: buffer to stored parsed lease state information
Namjae Jeone2f34482021-03-16 10:49:09 +09001345 */
1346void create_lease_buf(u8 *rbuf, struct lease *lease)
1347{
1348 struct create_lease *buf = (struct create_lease *)rbuf;
1349 char *LeaseKey = (char *)&lease->lease_key;
1350
1351 memset(buf, 0, sizeof(struct create_lease));
1352 buf->lcontext.LeaseKeyLow = *((__le64 *)LeaseKey);
1353 buf->lcontext.LeaseKeyHigh = *((__le64 *)(LeaseKey + 8));
1354 buf->lcontext.LeaseFlags = lease->flags;
1355 buf->lcontext.LeaseState = lease->state;
1356 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1357 (struct create_lease, lcontext));
1358 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1359 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1360 (struct create_lease, Name));
1361 buf->ccontext.NameLength = cpu_to_le16(4);
1362 buf->Name[0] = 'R';
1363 buf->Name[1] = 'q';
1364 buf->Name[2] = 'L';
1365 buf->Name[3] = 's';
1366}
1367
1368/**
1369 * parse_lease_state() - parse lease context containted in file open request
1370 * @open_req: buffer containing smb2 file open(create) request
Namjae Jeone2f34482021-03-16 10:49:09 +09001371 *
1372 * Return: oplock state, -ENOENT if create lease context not found
1373 */
1374struct lease_ctx_info *parse_lease_state(void *open_req)
1375{
1376 char *data_offset;
1377 struct create_context *cc;
1378 unsigned int next = 0;
1379 char *name;
1380 bool found = false;
1381 struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1382 struct lease_ctx_info *lreq = kzalloc(sizeof(struct lease_ctx_info),
1383 GFP_KERNEL);
1384 if (!lreq)
1385 return NULL;
1386
1387 data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1388 cc = (struct create_context *)data_offset;
1389 do {
1390 cc = (struct create_context *)((char *)cc + next);
1391 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1392 if (le16_to_cpu(cc->NameLength) != 4 ||
1393 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
1394 next = le32_to_cpu(cc->Next);
1395 continue;
1396 }
1397 found = true;
1398 break;
1399 } while (next != 0);
1400
1401 if (found) {
1402 struct create_lease *lc = (struct create_lease *)cc;
1403 *((__le64 *)lreq->lease_key) = lc->lcontext.LeaseKeyLow;
1404 *((__le64 *)(lreq->lease_key + 8)) = lc->lcontext.LeaseKeyHigh;
1405 lreq->req_state = lc->lcontext.LeaseState;
1406 lreq->flags = lc->lcontext.LeaseFlags;
1407 lreq->duration = lc->lcontext.LeaseDuration;
1408 return lreq;
1409 }
1410
1411 kfree(lreq);
1412 return NULL;
1413}
1414
1415/**
1416 * smb2_find_context_vals() - find a particular context info in open request
1417 * @open_req: buffer containing smb2 file open(create) request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001418 * @tag: context name to search for
Namjae Jeone2f34482021-03-16 10:49:09 +09001419 *
1420 * Return: pointer to requested context, NULL if @str context not found
1421 */
1422struct create_context *smb2_find_context_vals(void *open_req, const char *tag)
1423{
1424 char *data_offset;
1425 struct create_context *cc;
1426 unsigned int next = 0;
1427 char *name;
1428 struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1429
1430 data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1431 cc = (struct create_context *)data_offset;
1432 do {
1433 int val;
1434
1435 cc = (struct create_context *)((char *)cc + next);
1436 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1437 val = le16_to_cpu(cc->NameLength);
1438 if (val < 4)
1439 return ERR_PTR(-EINVAL);
1440
1441 if (memcmp(name, tag, val) == 0)
1442 return cc;
1443 next = le32_to_cpu(cc->Next);
1444 } while (next != 0);
1445
1446 return ERR_PTR(-ENOENT);
1447}
1448
1449/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001450 * create_durable_rsp__buf() - create durable handle context
Namjae Jeone2f34482021-03-16 10:49:09 +09001451 * @cc: buffer to create durable context response
1452 */
1453void create_durable_rsp_buf(char *cc)
1454{
1455 struct create_durable_rsp *buf;
1456
1457 buf = (struct create_durable_rsp *)cc;
1458 memset(buf, 0, sizeof(struct create_durable_rsp));
1459 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1460 (struct create_durable_rsp, Data));
1461 buf->ccontext.DataLength = cpu_to_le32(8);
1462 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1463 (struct create_durable_rsp, Name));
1464 buf->ccontext.NameLength = cpu_to_le16(4);
1465 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */
1466 buf->Name[0] = 'D';
1467 buf->Name[1] = 'H';
1468 buf->Name[2] = 'n';
1469 buf->Name[3] = 'Q';
1470}
1471
1472/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001473 * create_durable_v2_rsp_buf() - create durable handle v2 context
Namjae Jeone2f34482021-03-16 10:49:09 +09001474 * @cc: buffer to create durable context response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001475 * @fp: ksmbd file pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09001476 */
1477void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp)
1478{
1479 struct create_durable_v2_rsp *buf;
1480
1481 buf = (struct create_durable_v2_rsp *)cc;
1482 memset(buf, 0, sizeof(struct create_durable_rsp));
1483 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1484 (struct create_durable_rsp, Data));
1485 buf->ccontext.DataLength = cpu_to_le32(8);
1486 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1487 (struct create_durable_rsp, Name));
1488 buf->ccontext.NameLength = cpu_to_le16(4);
1489 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */
1490 buf->Name[0] = 'D';
1491 buf->Name[1] = 'H';
1492 buf->Name[2] = '2';
1493 buf->Name[3] = 'Q';
1494
1495 buf->Timeout = cpu_to_le32(fp->durable_timeout);
1496 if (fp->is_persistent)
1497 buf->Flags = SMB2_FLAGS_REPLAY_OPERATIONS;
1498}
1499
1500/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001501 * create_mxac_rsp_buf() - create query maximal access context
1502 * @cc: buffer to create maximal access context response
1503 * @maximal_access: maximal access
Namjae Jeone2f34482021-03-16 10:49:09 +09001504 */
1505void create_mxac_rsp_buf(char *cc, int maximal_access)
1506{
1507 struct create_mxac_rsp *buf;
1508
1509 buf = (struct create_mxac_rsp *)cc;
1510 memset(buf, 0, sizeof(struct create_mxac_rsp));
1511 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1512 (struct create_mxac_rsp, QueryStatus));
1513 buf->ccontext.DataLength = cpu_to_le32(8);
1514 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1515 (struct create_mxac_rsp, Name));
1516 buf->ccontext.NameLength = cpu_to_le16(4);
1517 /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */
1518 buf->Name[0] = 'M';
1519 buf->Name[1] = 'x';
1520 buf->Name[2] = 'A';
1521 buf->Name[3] = 'c';
1522
1523 buf->QueryStatus = STATUS_SUCCESS;
1524 buf->MaximalAccess = cpu_to_le32(maximal_access);
1525}
1526
Namjae Jeone2f34482021-03-16 10:49:09 +09001527void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id)
1528{
1529 struct create_disk_id_rsp *buf;
1530
1531 buf = (struct create_disk_id_rsp *)cc;
1532 memset(buf, 0, sizeof(struct create_disk_id_rsp));
1533 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1534 (struct create_disk_id_rsp, DiskFileId));
1535 buf->ccontext.DataLength = cpu_to_le32(32);
1536 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1537 (struct create_mxac_rsp, Name));
1538 buf->ccontext.NameLength = cpu_to_le16(4);
1539 /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */
1540 buf->Name[0] = 'Q';
1541 buf->Name[1] = 'F';
1542 buf->Name[2] = 'i';
1543 buf->Name[3] = 'd';
1544
1545 buf->DiskFileId = cpu_to_le64(file_id);
1546 buf->VolumeId = cpu_to_le64(vol_id);
1547}
1548
1549/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001550 * create_posix_rsp_buf() - create posix extension context
Namjae Jeone2f34482021-03-16 10:49:09 +09001551 * @cc: buffer to create posix on posix response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001552 * @fp: ksmbd file pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09001553 */
1554void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)
1555{
1556 struct create_posix_rsp *buf;
1557 struct inode *inode = FP_INODE(fp);
1558
1559 buf = (struct create_posix_rsp *)cc;
1560 memset(buf, 0, sizeof(struct create_posix_rsp));
1561 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1562 (struct create_posix_rsp, nlink));
1563 buf->ccontext.DataLength = cpu_to_le32(52);
1564 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1565 (struct create_posix_rsp, Name));
1566 buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
1567 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
1568 buf->Name[0] = 0x93;
1569 buf->Name[1] = 0xAD;
1570 buf->Name[2] = 0x25;
1571 buf->Name[3] = 0x50;
1572 buf->Name[4] = 0x9C;
1573 buf->Name[5] = 0xB4;
1574 buf->Name[6] = 0x11;
1575 buf->Name[7] = 0xE7;
1576 buf->Name[8] = 0xB4;
1577 buf->Name[9] = 0x23;
1578 buf->Name[10] = 0x83;
1579 buf->Name[11] = 0xDE;
1580 buf->Name[12] = 0x96;
1581 buf->Name[13] = 0x8B;
1582 buf->Name[14] = 0xCD;
1583 buf->Name[15] = 0x7C;
1584
1585 buf->nlink = cpu_to_le32(inode->i_nlink);
1586 buf->reparse_tag = cpu_to_le32(fp->volatile_id);
1587 buf->mode = cpu_to_le32(inode->i_mode);
1588 id_to_sid(from_kuid(&init_user_ns, inode->i_uid),
1589 SIDNFS_USER, (struct smb_sid *)&buf->SidBuffer[0]);
1590 id_to_sid(from_kgid(&init_user_ns, inode->i_gid),
1591 SIDNFS_GROUP, (struct smb_sid *)&buf->SidBuffer[20]);
1592}
1593
1594/*
1595 * Find lease object(opinfo) for given lease key/fid from lease
1596 * break/file close path.
1597 */
1598/**
1599 * lookup_lease_in_table() - find a matching lease info object
1600 * @conn: connection instance
1601 * @lease_key: lease key to be searched for
1602 *
1603 * Return: opinfo if found matching opinfo, otherwise NULL
1604 */
1605struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
1606 char *lease_key)
1607{
1608 struct oplock_info *opinfo = NULL, *ret_op = NULL;
1609 struct lease_table *lt;
1610 int ret;
1611
1612 read_lock(&lease_list_lock);
1613 list_for_each_entry(lt, &lease_table_list, l_entry) {
1614 if (!memcmp(lt->client_guid, conn->ClientGUID,
1615 SMB2_CLIENT_GUID_SIZE))
1616 goto found;
1617 }
1618
1619 read_unlock(&lease_list_lock);
1620 return NULL;
1621
1622found:
1623 rcu_read_lock();
1624 list_for_each_entry_rcu(opinfo, &lt->lease_list, lease_entry) {
1625 if (!atomic_inc_not_zero(&opinfo->refcount))
1626 continue;
1627 rcu_read_unlock();
1628 if (!opinfo->op_state ||
1629 opinfo->op_state == OPLOCK_CLOSING)
1630 goto op_next;
1631 if (!(opinfo->o_lease->state &
1632 (SMB2_LEASE_HANDLE_CACHING_LE |
1633 SMB2_LEASE_WRITE_CACHING_LE)))
1634 goto op_next;
1635 ret = compare_guid_key(opinfo, conn->ClientGUID,
1636 lease_key);
1637 if (ret) {
1638 ksmbd_debug(OPLOCK, "found opinfo\n");
1639 ret_op = opinfo;
1640 goto out;
1641 }
1642op_next:
1643 opinfo_put(opinfo);
1644 rcu_read_lock();
1645 }
1646 rcu_read_unlock();
1647
1648out:
1649 read_unlock(&lease_list_lock);
1650 return ret_op;
1651}
1652
1653int smb2_check_durable_oplock(struct ksmbd_file *fp,
1654 struct lease_ctx_info *lctx, char *name)
1655{
1656 struct oplock_info *opinfo = opinfo_get(fp);
1657 int ret = 0;
1658
1659 if (opinfo && opinfo->is_lease) {
1660 if (!lctx) {
1661 ksmbd_err("open does not include lease\n");
1662 ret = -EBADF;
1663 goto out;
1664 }
1665 if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key,
1666 SMB2_LEASE_KEY_SIZE)) {
1667 ksmbd_err("invalid lease key\n");
1668 ret = -EBADF;
1669 goto out;
1670 }
1671 if (name && strcmp(fp->filename, name)) {
1672 ksmbd_err("invalid name reconnect %s\n", name);
1673 ret = -EINVAL;
1674 goto out;
1675 }
1676 }
1677out:
1678 if (opinfo)
1679 opinfo_put(opinfo);
1680 return ret;
1681}