blob: f76de7861e7bfdd6e68b786acd00425dac84f11f [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,
Namjae Jeon070fb212021-05-26 17:57:12 +090032 u64 id, __u16 Tid)
Namjae Jeone2f34482021-03-16 10:49:09 +090033{
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
Namjae Jeon64b39f42021-03-30 14:25:35 +090092static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +090093{
94 struct lease *lease;
95
96 lease = kmalloc(sizeof(struct lease), GFP_KERNEL);
97 if (!lease)
98 return -ENOMEM;
99
100 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
101 lease->state = lctx->req_state;
102 lease->new_state = 0;
103 lease->flags = lctx->flags;
104 lease->duration = lctx->duration;
105 INIT_LIST_HEAD(&opinfo->lease_entry);
106 opinfo->o_lease = lease;
107
108 return 0;
109}
110
111static void free_lease(struct oplock_info *opinfo)
112{
113 struct lease *lease;
114
115 lease = opinfo->o_lease;
116 kfree(lease);
117}
118
119static void free_opinfo(struct oplock_info *opinfo)
120{
121 if (opinfo->is_lease)
122 free_lease(opinfo);
123 kfree(opinfo);
124}
125
126static inline void opinfo_free_rcu(struct rcu_head *rcu_head)
127{
128 struct oplock_info *opinfo;
129
130 opinfo = container_of(rcu_head, struct oplock_info, rcu_head);
131 free_opinfo(opinfo);
132}
133
134struct oplock_info *opinfo_get(struct ksmbd_file *fp)
135{
136 struct oplock_info *opinfo;
137
138 rcu_read_lock();
139 opinfo = rcu_dereference(fp->f_opinfo);
140 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
141 opinfo = NULL;
142 rcu_read_unlock();
143
144 return opinfo;
145}
146
147static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci)
148{
149 struct oplock_info *opinfo;
150
151 if (list_empty(&ci->m_op_list))
152 return NULL;
153
154 rcu_read_lock();
155 opinfo = list_first_or_null_rcu(&ci->m_op_list, struct oplock_info,
Namjae Jeon070fb212021-05-26 17:57:12 +0900156 op_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +0900157 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
158 opinfo = NULL;
159 rcu_read_unlock();
160
161 return opinfo;
162}
163
164void opinfo_put(struct oplock_info *opinfo)
165{
166 if (!atomic_dec_and_test(&opinfo->refcount))
167 return;
168
169 call_rcu(&opinfo->rcu_head, opinfo_free_rcu);
170}
171
172static void opinfo_add(struct oplock_info *opinfo)
173{
174 struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
175
176 write_lock(&ci->m_lock);
177 list_add_rcu(&opinfo->op_entry, &ci->m_op_list);
178 write_unlock(&ci->m_lock);
179}
180
181static void opinfo_del(struct oplock_info *opinfo)
182{
183 struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
184
185 if (opinfo->is_lease) {
186 write_lock(&lease_list_lock);
187 lease_del_list(opinfo);
188 write_unlock(&lease_list_lock);
189 }
190 write_lock(&ci->m_lock);
191 list_del_rcu(&opinfo->op_entry);
192 write_unlock(&ci->m_lock);
193}
194
195static unsigned long opinfo_count(struct ksmbd_file *fp)
196{
197 if (ksmbd_stream_fd(fp))
198 return atomic_read(&fp->f_ci->sop_count);
199 else
200 return atomic_read(&fp->f_ci->op_count);
201}
202
203static void opinfo_count_inc(struct ksmbd_file *fp)
204{
205 if (ksmbd_stream_fd(fp))
206 return atomic_inc(&fp->f_ci->sop_count);
207 else
208 return atomic_inc(&fp->f_ci->op_count);
209}
210
211static void opinfo_count_dec(struct ksmbd_file *fp)
212{
213 if (ksmbd_stream_fd(fp))
214 return atomic_dec(&fp->f_ci->sop_count);
215 else
216 return atomic_dec(&fp->f_ci->op_count);
217}
218
219/**
220 * opinfo_write_to_read() - convert a write oplock to read oplock
221 * @opinfo: current oplock info
222 *
223 * Return: 0 on success, otherwise -EINVAL
224 */
225int opinfo_write_to_read(struct oplock_info *opinfo)
226{
227 struct lease *lease = opinfo->o_lease;
228
Namjae Jeon64b39f42021-03-30 14:25:35 +0900229 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
230 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900231 ksmbd_err("bad oplock(0x%x)\n", opinfo->level);
232 if (opinfo->is_lease)
233 ksmbd_err("lease state(0x%x)\n", lease->state);
234 return -EINVAL;
235 }
236 opinfo->level = SMB2_OPLOCK_LEVEL_II;
237
238 if (opinfo->is_lease)
239 lease->state = lease->new_state;
240 return 0;
241}
242
243/**
244 * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock
245 * @opinfo: current oplock info
246 *
247 * Return: 0 on success, otherwise -EINVAL
248 */
249int opinfo_read_handle_to_read(struct oplock_info *opinfo)
250{
251 struct lease *lease = opinfo->o_lease;
252
253 lease->state = lease->new_state;
254 opinfo->level = SMB2_OPLOCK_LEVEL_II;
255 return 0;
256}
257
258/**
259 * opinfo_write_to_none() - convert a write oplock to none
260 * @opinfo: current oplock info
261 *
262 * Return: 0 on success, otherwise -EINVAL
263 */
264int opinfo_write_to_none(struct oplock_info *opinfo)
265{
266 struct lease *lease = opinfo->o_lease;
267
Namjae Jeon64b39f42021-03-30 14:25:35 +0900268 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
269 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900270 ksmbd_err("bad oplock(0x%x)\n", opinfo->level);
271 if (opinfo->is_lease)
Namjae Jeon070fb212021-05-26 17:57:12 +0900272 ksmbd_err("lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900273 return -EINVAL;
274 }
275 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
276 if (opinfo->is_lease)
277 lease->state = lease->new_state;
278 return 0;
279}
280
281/**
282 * opinfo_read_to_none() - convert a write read to none
283 * @opinfo: current oplock info
284 *
285 * Return: 0 on success, otherwise -EINVAL
286 */
287int opinfo_read_to_none(struct oplock_info *opinfo)
288{
289 struct lease *lease = opinfo->o_lease;
290
291 if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
292 ksmbd_err("bad oplock(0x%x)\n", opinfo->level);
293 if (opinfo->is_lease)
294 ksmbd_err("lease state(0x%x)\n", lease->state);
295 return -EINVAL;
296 }
297 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
298 if (opinfo->is_lease)
299 lease->state = lease->new_state;
300 return 0;
301}
302
303/**
304 * lease_read_to_write() - upgrade lease state from read to write
305 * @opinfo: current lease info
306 *
307 * Return: 0 on success, otherwise -EINVAL
308 */
309int lease_read_to_write(struct oplock_info *opinfo)
310{
311 struct lease *lease = opinfo->o_lease;
312
313 if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) {
Namjae Jeon070fb212021-05-26 17:57:12 +0900314 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900315 return -EINVAL;
316 }
317
318 lease->new_state = SMB2_LEASE_NONE_LE;
319 lease->state |= SMB2_LEASE_WRITE_CACHING_LE;
320 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
321 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
322 else
323 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
324 return 0;
325}
326
327/**
328 * lease_none_upgrade() - upgrade lease state from none
329 * @opinfo: current lease info
330 * @new_state: new lease state
331 *
332 * Return: 0 on success, otherwise -EINVAL
333 */
Namjae Jeon64b39f42021-03-30 14:25:35 +0900334static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state)
Namjae Jeone2f34482021-03-16 10:49:09 +0900335{
336 struct lease *lease = opinfo->o_lease;
337
338 if (!(lease->state == SMB2_LEASE_NONE_LE)) {
Namjae Jeon070fb212021-05-26 17:57:12 +0900339 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900340 return -EINVAL;
341 }
342
343 lease->new_state = SMB2_LEASE_NONE_LE;
344 lease->state = new_state;
345 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
346 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
347 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
348 else
349 opinfo->level = SMB2_OPLOCK_LEVEL_II;
350 else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
351 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
352 else if (lease->state & SMB2_LEASE_READ_CACHING_LE)
353 opinfo->level = SMB2_OPLOCK_LEVEL_II;
354
355 return 0;
356}
357
358/**
359 * close_id_del_oplock() - release oplock object at file close time
360 * @fp: ksmbd file pointer
361 */
362void close_id_del_oplock(struct ksmbd_file *fp)
363{
364 struct oplock_info *opinfo;
365
366 if (S_ISDIR(file_inode(fp->filp)->i_mode))
367 return;
368
369 opinfo = opinfo_get(fp);
370 if (!opinfo)
371 return;
372
373 opinfo_del(opinfo);
374
375 rcu_assign_pointer(fp->f_opinfo, NULL);
376 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
377 opinfo->op_state = OPLOCK_CLOSING;
378 wake_up_interruptible_all(&opinfo->oplock_q);
379 if (opinfo->is_lease) {
380 atomic_set(&opinfo->breaking_cnt, 0);
381 wake_up_interruptible_all(&opinfo->oplock_brk);
382 }
383 }
384
385 opinfo_count_dec(fp);
386 atomic_dec(&opinfo->refcount);
387 opinfo_put(opinfo);
388}
389
390/**
391 * grant_write_oplock() - grant exclusive/batch oplock or write lease
392 * @opinfo_new: new oplock info object
393 * @req_oplock: request oplock
394 * @lctx: lease context information
395 *
396 * Return: 0
397 */
398static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock,
Namjae Jeon070fb212021-05-26 17:57:12 +0900399 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900400{
401 struct lease *lease = opinfo_new->o_lease;
402
403 if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
404 opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
405 else
406 opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
407
408 if (lctx) {
409 lease->state = lctx->req_state;
Namjae Jeon070fb212021-05-26 17:57:12 +0900410 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +0900411 }
412}
413
414/**
415 * grant_read_oplock() - grant level2 oplock or read lease
416 * @opinfo_new: new oplock info object
417 * @lctx: lease context information
418 *
419 * Return: 0
420 */
421static void grant_read_oplock(struct oplock_info *opinfo_new,
Namjae Jeon070fb212021-05-26 17:57:12 +0900422 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900423{
424 struct lease *lease = opinfo_new->o_lease;
425
426 opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
427
428 if (lctx) {
429 lease->state = SMB2_LEASE_READ_CACHING_LE;
430 if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE)
431 lease->state |= SMB2_LEASE_HANDLE_CACHING_LE;
Namjae Jeon070fb212021-05-26 17:57:12 +0900432 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +0900433 }
434}
435
436/**
437 * grant_none_oplock() - grant none oplock or none lease
438 * @opinfo_new: new oplock info object
439 * @lctx: lease context information
440 *
441 * Return: 0
442 */
443static void grant_none_oplock(struct oplock_info *opinfo_new,
Namjae Jeon070fb212021-05-26 17:57:12 +0900444 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900445{
446 struct lease *lease = opinfo_new->o_lease;
447
448 opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
449
450 if (lctx) {
451 lease->state = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +0900452 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +0900453 }
454}
455
Namjae Jeone2f34482021-03-16 10:49:09 +0900456static inline int compare_guid_key(struct oplock_info *opinfo,
Namjae Jeon070fb212021-05-26 17:57:12 +0900457 const char *guid1, const char *key1)
Namjae Jeone2f34482021-03-16 10:49:09 +0900458{
459 const char *guid2, *key2;
460
461 guid2 = opinfo->conn->ClientGUID;
462 key2 = opinfo->o_lease->lease_key;
463 if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900464 !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900465 return 1;
466
467 return 0;
468}
469
470/**
471 * same_client_has_lease() - check whether current lease request is
472 * from lease owner of file
473 * @ci: master file pointer
474 * @client_guid: Client GUID
475 * @lctx: lease context information
476 *
477 * Return: oplock(lease) object on success, otherwise NULL
478 */
479static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,
Namjae Jeon070fb212021-05-26 17:57:12 +0900480 char *client_guid,
481 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900482{
483 int ret;
484 struct lease *lease;
485 struct oplock_info *opinfo;
486 struct oplock_info *m_opinfo = NULL;
487
488 if (!lctx)
489 return NULL;
490
491 /*
492 * Compare lease key and client_guid to know request from same owner
493 * of same client
494 */
495 read_lock(&ci->m_lock);
496 list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
497 if (!opinfo->is_lease)
498 continue;
499 read_unlock(&ci->m_lock);
500 lease = opinfo->o_lease;
501
502 ret = compare_guid_key(opinfo, client_guid, lctx->lease_key);
503 if (ret) {
504 m_opinfo = opinfo;
505 /* skip upgrading lease about breaking lease */
506 if (atomic_read(&opinfo->breaking_cnt)) {
507 read_lock(&ci->m_lock);
508 continue;
509 }
510
511 /* upgrading lease */
512 if ((atomic_read(&ci->op_count) +
513 atomic_read(&ci->sop_count)) == 1) {
514 if (lease->state ==
Namjae Jeon070fb212021-05-26 17:57:12 +0900515 (lctx->req_state & lease->state)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900516 lease->state |= lctx->req_state;
517 if (lctx->req_state &
518 SMB2_LEASE_WRITE_CACHING_LE)
519 lease_read_to_write(opinfo);
520 }
521 } else if ((atomic_read(&ci->op_count) +
522 atomic_read(&ci->sop_count)) > 1) {
523 if (lctx->req_state ==
Namjae Jeon070fb212021-05-26 17:57:12 +0900524 (SMB2_LEASE_READ_CACHING_LE |
525 SMB2_LEASE_HANDLE_CACHING_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900526 lease->state = lctx->req_state;
527 }
528
529 if (lctx->req_state && lease->state ==
Namjae Jeon070fb212021-05-26 17:57:12 +0900530 SMB2_LEASE_NONE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900531 lease_none_upgrade(opinfo, lctx->req_state);
532 }
533 read_lock(&ci->m_lock);
534 }
535 read_unlock(&ci->m_lock);
536
537 return m_opinfo;
538}
539
540static void wait_for_break_ack(struct oplock_info *opinfo)
541{
542 int rc = 0;
543
544 rc = wait_event_interruptible_timeout(opinfo->oplock_q,
Namjae Jeon070fb212021-05-26 17:57:12 +0900545 opinfo->op_state == OPLOCK_STATE_NONE ||
546 opinfo->op_state == OPLOCK_CLOSING,
547 OPLOCK_WAIT_TIME);
Namjae Jeone2f34482021-03-16 10:49:09 +0900548
549 /* is this a timeout ? */
550 if (!rc) {
551 if (opinfo->is_lease)
552 opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
553 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
554 opinfo->op_state = OPLOCK_STATE_NONE;
555 }
556}
557
558static void wake_up_oplock_break(struct oplock_info *opinfo)
559{
560 clear_bit_unlock(0, &opinfo->pending_break);
561 /* memory barrier is needed for wake_up_bit() */
562 smp_mb__after_atomic();
563 wake_up_bit(&opinfo->pending_break, 0);
564}
565
566static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)
567{
568 while (test_and_set_bit(0, &opinfo->pending_break)) {
569 wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE);
570
571 /* Not immediately break to none. */
572 opinfo->open_trunc = 0;
573
574 if (opinfo->op_state == OPLOCK_CLOSING)
575 return -ENOENT;
576 else if (!opinfo->is_lease && opinfo->level <= req_op_level)
577 return 1;
578 }
579
580 if (!opinfo->is_lease && opinfo->level <= req_op_level) {
581 wake_up_oplock_break(opinfo);
582 return 1;
583 }
584 return 0;
585}
586
587static inline int allocate_oplock_break_buf(struct ksmbd_work *work)
588{
Namjae Jeon20ea7fd2021-03-30 12:40:47 +0900589 work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +0900590 if (!work->response_buf)
591 return -ENOMEM;
592 work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
593 return 0;
594}
595
596/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900597 * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn
Namjae Jeone2f34482021-03-16 10:49:09 +0900598 * to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900599 * @wk: smb work object
Namjae Jeone2f34482021-03-16 10:49:09 +0900600 *
601 * There are two ways this function can be called. 1- while file open we break
602 * from exclusive/batch lock to levelII oplock and 2- while file write/truncate
603 * we break from levelII oplock no oplock.
Namjae Jeone5066492021-03-30 12:35:23 +0900604 * work->request_buf contains oplock_info.
Namjae Jeone2f34482021-03-16 10:49:09 +0900605 */
606static void __smb2_oplock_break_noti(struct work_struct *wk)
607{
608 struct smb2_oplock_break *rsp = NULL;
609 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
610 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +0900611 struct oplock_break_info *br_info = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900612 struct smb2_hdr *rsp_hdr;
613 struct ksmbd_file *fp;
614
615 fp = ksmbd_lookup_durable_fd(br_info->fid);
616 if (!fp) {
617 atomic_dec(&conn->r_count);
618 ksmbd_free_work_struct(work);
619 return;
620 }
621
622 if (allocate_oplock_break_buf(work)) {
623 ksmbd_err("smb2_allocate_rsp_buf failed! ");
624 atomic_dec(&conn->r_count);
Namjae Jeone2f34482021-03-16 10:49:09 +0900625 ksmbd_fd_put(work, fp);
Dan Carpentera2ba2702021-03-18 16:12:54 +0300626 ksmbd_free_work_struct(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900627 return;
628 }
629
Namjae Jeone5066492021-03-30 12:35:23 +0900630 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900631 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
632 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
633 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
634 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
635 rsp_hdr->CreditRequest = cpu_to_le16(0);
636 rsp_hdr->Command = SMB2_OPLOCK_BREAK;
637 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
638 rsp_hdr->NextCommand = 0;
639 rsp_hdr->MessageId = cpu_to_le64(-1);
640 rsp_hdr->Id.SyncId.ProcessId = 0;
641 rsp_hdr->Id.SyncId.TreeId = 0;
642 rsp_hdr->SessionId = 0;
643 memset(rsp_hdr->Signature, 0, 16);
644
Namjae Jeone5066492021-03-30 12:35:23 +0900645 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900646
647 rsp->StructureSize = cpu_to_le16(24);
648 if (!br_info->open_trunc &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900649 (br_info->level == SMB2_OPLOCK_LEVEL_BATCH ||
650 br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900651 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II;
652 else
653 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
654 rsp->Reserved = 0;
655 rsp->Reserved2 = 0;
656 rsp->PersistentFid = cpu_to_le64(fp->persistent_id);
657 rsp->VolatileFid = cpu_to_le64(fp->volatile_id);
658
659 inc_rfc1001_len(rsp, 24);
660
661 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +0900662 "sending oplock break v_id %llu p_id = %llu lock level = %d\n",
663 rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel);
Namjae Jeone2f34482021-03-16 10:49:09 +0900664
665 ksmbd_fd_put(work, fp);
666 ksmbd_conn_write(work);
667 ksmbd_free_work_struct(work);
668 atomic_dec(&conn->r_count);
669}
670
671/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900672 * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock
Namjae Jeone2f34482021-03-16 10:49:09 +0900673 * break command from server to client
674 * @opinfo: oplock info object
Namjae Jeone2f34482021-03-16 10:49:09 +0900675 *
676 * Return: 0 on success, otherwise error
677 */
678static int smb2_oplock_break_noti(struct oplock_info *opinfo)
679{
680 struct ksmbd_conn *conn = opinfo->conn;
681 struct oplock_break_info *br_info;
682 int ret = 0;
683 struct ksmbd_work *work = ksmbd_alloc_work_struct();
684
685 if (!work)
686 return -ENOMEM;
687
688 br_info = kmalloc(sizeof(struct oplock_break_info), GFP_KERNEL);
689 if (!br_info) {
690 ksmbd_free_work_struct(work);
691 return -ENOMEM;
692 }
693
694 br_info->level = opinfo->level;
695 br_info->fid = opinfo->fid;
696 br_info->open_trunc = opinfo->open_trunc;
697
698 work->request_buf = (char *)br_info;
699 work->conn = conn;
700 work->sess = opinfo->sess;
701
702 atomic_inc(&conn->r_count);
703 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
704 INIT_WORK(&work->work, __smb2_oplock_break_noti);
705 ksmbd_queue_work(work);
706
707 wait_for_break_ack(opinfo);
708 } else {
709 __smb2_oplock_break_noti(&work->work);
710 if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
711 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
712 }
713 return ret;
714}
715
716/**
717 * __smb2_lease_break_noti() - send lease break command from server
718 * to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900719 * @wk: smb work object
Namjae Jeone2f34482021-03-16 10:49:09 +0900720 */
721static void __smb2_lease_break_noti(struct work_struct *wk)
722{
723 struct smb2_lease_break *rsp = NULL;
724 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
Namjae Jeone5066492021-03-30 12:35:23 +0900725 struct lease_break_info *br_info = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900726 struct ksmbd_conn *conn = work->conn;
727 struct smb2_hdr *rsp_hdr;
728
729 if (allocate_oplock_break_buf(work)) {
730 ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! ");
731 ksmbd_free_work_struct(work);
732 atomic_dec(&conn->r_count);
733 return;
734 }
735
Namjae Jeone5066492021-03-30 12:35:23 +0900736 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900737 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
738 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
739 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
740 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
741 rsp_hdr->CreditRequest = cpu_to_le16(0);
742 rsp_hdr->Command = SMB2_OPLOCK_BREAK;
743 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
744 rsp_hdr->NextCommand = 0;
745 rsp_hdr->MessageId = cpu_to_le64(-1);
746 rsp_hdr->Id.SyncId.ProcessId = 0;
747 rsp_hdr->Id.SyncId.TreeId = 0;
748 rsp_hdr->SessionId = 0;
749 memset(rsp_hdr->Signature, 0, 16);
750
Namjae Jeone5066492021-03-30 12:35:23 +0900751 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900752 rsp->StructureSize = cpu_to_le16(44);
753 rsp->Reserved = 0;
754 rsp->Flags = 0;
755
756 if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE |
757 SMB2_LEASE_HANDLE_CACHING_LE))
758 rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
759
760 memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE);
761 rsp->CurrentLeaseState = br_info->curr_state;
762 rsp->NewLeaseState = br_info->new_state;
763 rsp->BreakReason = 0;
764 rsp->AccessMaskHint = 0;
765 rsp->ShareMaskHint = 0;
766
767 inc_rfc1001_len(rsp, 44);
768
769 ksmbd_conn_write(work);
770 ksmbd_free_work_struct(work);
771 atomic_dec(&conn->r_count);
772}
773
774/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900775 * smb2_lease_break_noti() - break lease when a new client request
Namjae Jeone2f34482021-03-16 10:49:09 +0900776 * write lease
777 * @opinfo: conains lease state information
Namjae Jeone2f34482021-03-16 10:49:09 +0900778 *
779 * Return: 0 on success, otherwise error
780 */
781static int smb2_lease_break_noti(struct oplock_info *opinfo)
782{
783 struct ksmbd_conn *conn = opinfo->conn;
784 struct list_head *tmp, *t;
785 struct ksmbd_work *work;
786 struct lease_break_info *br_info;
787 struct lease *lease = opinfo->o_lease;
788
789 work = ksmbd_alloc_work_struct();
790 if (!work)
791 return -ENOMEM;
792
793 br_info = kmalloc(sizeof(struct lease_break_info), GFP_KERNEL);
794 if (!br_info) {
795 ksmbd_free_work_struct(work);
796 return -ENOMEM;
797 }
798
799 br_info->curr_state = lease->state;
800 br_info->new_state = lease->new_state;
801 memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE);
802
803 work->request_buf = (char *)br_info;
804 work->conn = conn;
805 work->sess = opinfo->sess;
806
807 atomic_inc(&conn->r_count);
808 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
809 list_for_each_safe(tmp, t, &opinfo->interim_list) {
810 struct ksmbd_work *in_work;
811
812 in_work = list_entry(tmp, struct ksmbd_work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900813 interim_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +0900814 setup_async_work(in_work, NULL, NULL);
815 smb2_send_interim_resp(in_work, STATUS_PENDING);
816 list_del(&in_work->interim_entry);
817 }
818 INIT_WORK(&work->work, __smb2_lease_break_noti);
819 ksmbd_queue_work(work);
820 wait_for_break_ack(opinfo);
821 } else {
822 __smb2_lease_break_noti(&work->work);
823 if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) {
824 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
825 opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
826 }
827 }
828 return 0;
829}
830
831static void wait_lease_breaking(struct oplock_info *opinfo)
832{
833 if (!opinfo->is_lease)
834 return;
835
836 wake_up_interruptible_all(&opinfo->oplock_brk);
837 if (atomic_read(&opinfo->breaking_cnt)) {
838 int ret = 0;
839
Namjae Jeon64b39f42021-03-30 14:25:35 +0900840 ret = wait_event_interruptible_timeout(opinfo->oplock_brk,
Namjae Jeon070fb212021-05-26 17:57:12 +0900841 atomic_read(&opinfo->breaking_cnt) == 0,
842 HZ);
Namjae Jeone2f34482021-03-16 10:49:09 +0900843 if (!ret)
844 atomic_set(&opinfo->breaking_cnt, 0);
845 }
846}
847
848static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level)
849{
850 int err = 0;
851
852 /* Need to break exclusive/batch oplock, write lease or overwrite_if */
853 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +0900854 "request to send oplock(level : 0x%x) break notification\n",
855 brk_opinfo->level);
Namjae Jeone2f34482021-03-16 10:49:09 +0900856
857 if (brk_opinfo->is_lease) {
858 struct lease *lease = brk_opinfo->o_lease;
859
860 atomic_inc(&brk_opinfo->breaking_cnt);
861
862 err = oplock_break_pending(brk_opinfo, req_op_level);
863 if (err)
864 return err < 0 ? err : 0;
865
866 if (brk_opinfo->open_trunc) {
867 /*
868 * Create overwrite break trigger the lease break to
869 * none.
870 */
871 lease->new_state = SMB2_LEASE_NONE_LE;
872 } else {
873 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) {
874 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
875 lease->new_state =
876 SMB2_LEASE_READ_CACHING_LE |
877 SMB2_LEASE_HANDLE_CACHING_LE;
878 else
879 lease->new_state =
880 SMB2_LEASE_READ_CACHING_LE;
881 } else {
882 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
883 lease->new_state =
884 SMB2_LEASE_READ_CACHING_LE;
885 else
886 lease->new_state = SMB2_LEASE_NONE_LE;
887 }
888 }
889
890 if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE |
891 SMB2_LEASE_HANDLE_CACHING_LE))
892 brk_opinfo->op_state = OPLOCK_ACK_WAIT;
893 else
894 atomic_dec(&brk_opinfo->breaking_cnt);
895 } else {
896 err = oplock_break_pending(brk_opinfo, req_op_level);
897 if (err)
898 return err < 0 ? err : 0;
899
900 if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900901 brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900902 brk_opinfo->op_state = OPLOCK_ACK_WAIT;
903 }
904
905 if (brk_opinfo->is_lease)
906 err = smb2_lease_break_noti(brk_opinfo);
907 else
908 err = smb2_oplock_break_noti(brk_opinfo);
909
910 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level);
911 if (brk_opinfo->op_state == OPLOCK_CLOSING)
912 err = -ENOENT;
913 wake_up_oplock_break(brk_opinfo);
914
915 wait_lease_breaking(brk_opinfo);
916
917 return err;
918}
919
920void destroy_lease_table(struct ksmbd_conn *conn)
921{
922 struct lease_table *lb, *lbtmp;
923 struct oplock_info *opinfo;
924
925 write_lock(&lease_list_lock);
926 if (list_empty(&lease_table_list)) {
927 write_unlock(&lease_list_lock);
928 return;
929 }
930
931 list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) {
932 if (conn && memcmp(lb->client_guid, conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +0900933 SMB2_CLIENT_GUID_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900934 continue;
935again:
936 rcu_read_lock();
937 list_for_each_entry_rcu(opinfo, &lb->lease_list,
Namjae Jeon070fb212021-05-26 17:57:12 +0900938 lease_entry) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900939 rcu_read_unlock();
940 lease_del_list(opinfo);
941 goto again;
942 }
943 rcu_read_unlock();
944 list_del(&lb->l_entry);
945 kfree(lb);
946 }
947 write_unlock(&lease_list_lock);
948}
949
950int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
Namjae Jeon070fb212021-05-26 17:57:12 +0900951 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900952{
953 struct oplock_info *opinfo;
954 int err = 0;
955 struct lease_table *lb;
956
957 if (!lctx)
958 return err;
959
960 read_lock(&lease_list_lock);
961 if (list_empty(&lease_table_list)) {
962 read_unlock(&lease_list_lock);
963 return 0;
964 }
965
966 list_for_each_entry(lb, &lease_table_list, l_entry) {
967 if (!memcmp(lb->client_guid, sess->conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +0900968 SMB2_CLIENT_GUID_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900969 goto found;
970 }
971 read_unlock(&lease_list_lock);
972
973 return 0;
974
975found:
976 rcu_read_lock();
Namjae Jeon070fb212021-05-26 17:57:12 +0900977 list_for_each_entry_rcu(opinfo, &lb->lease_list, lease_entry) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900978 if (!atomic_inc_not_zero(&opinfo->refcount))
979 continue;
980 rcu_read_unlock();
981 if (opinfo->o_fp->f_ci == ci)
982 goto op_next;
Namjae Jeon070fb212021-05-26 17:57:12 +0900983 err = compare_guid_key(opinfo, sess->conn->ClientGUID,
984 lctx->lease_key);
Namjae Jeone2f34482021-03-16 10:49:09 +0900985 if (err) {
986 err = -EINVAL;
987 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +0900988 "found same lease key is already used in other files\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900989 opinfo_put(opinfo);
990 goto out;
991 }
992op_next:
993 opinfo_put(opinfo);
994 rcu_read_lock();
995 }
996 rcu_read_unlock();
997
998out:
999 read_unlock(&lease_list_lock);
1000 return err;
1001}
1002
1003static void copy_lease(struct oplock_info *op1, struct oplock_info *op2)
1004{
1005 struct lease *lease1 = op1->o_lease;
1006 struct lease *lease2 = op2->o_lease;
1007
1008 op2->level = op1->level;
1009 lease2->state = lease1->state;
1010 memcpy(lease2->lease_key, lease1->lease_key,
Namjae Jeon070fb212021-05-26 17:57:12 +09001011 SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001012 lease2->duration = lease1->duration;
1013 lease2->flags = lease1->flags;
1014}
1015
1016static int add_lease_global_list(struct oplock_info *opinfo)
1017{
1018 struct lease_table *lb;
1019
1020 read_lock(&lease_list_lock);
1021 list_for_each_entry(lb, &lease_table_list, l_entry) {
1022 if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001023 SMB2_CLIENT_GUID_SIZE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001024 opinfo->o_lease->l_lb = lb;
1025 lease_add_list(opinfo);
1026 read_unlock(&lease_list_lock);
1027 return 0;
1028 }
1029 }
1030 read_unlock(&lease_list_lock);
1031
1032 lb = kmalloc(sizeof(struct lease_table), GFP_KERNEL);
1033 if (!lb)
1034 return -ENOMEM;
1035
1036 memcpy(lb->client_guid, opinfo->conn->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001037 SMB2_CLIENT_GUID_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001038 INIT_LIST_HEAD(&lb->lease_list);
1039 spin_lock_init(&lb->lb_lock);
1040 opinfo->o_lease->l_lb = lb;
1041 lease_add_list(opinfo);
1042 lb_add(lb);
1043 return 0;
1044}
1045
1046static void set_oplock_level(struct oplock_info *opinfo, int level,
Namjae Jeon070fb212021-05-26 17:57:12 +09001047 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +09001048{
1049 switch (level) {
1050 case SMB2_OPLOCK_LEVEL_BATCH:
1051 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09001052 grant_write_oplock(opinfo, level, lctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09001053 break;
1054 case SMB2_OPLOCK_LEVEL_II:
1055 grant_read_oplock(opinfo, lctx);
1056 break;
1057 default:
1058 grant_none_oplock(opinfo, lctx);
1059 break;
1060 }
1061}
1062
1063/**
1064 * smb_grant_oplock() - handle oplock/lease request on file open
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001065 * @work: smb work
1066 * @req_op_level: oplock level
1067 * @pid: id of open file
1068 * @fp: ksmbd file pointer
1069 * @tid: Tree id of connection
1070 * @lctx: lease context information on file open
1071 * @share_ret: share mode
Namjae Jeone2f34482021-03-16 10:49:09 +09001072 *
1073 * Return: 0 on success, otherwise error
1074 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001075int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,
Namjae Jeon070fb212021-05-26 17:57:12 +09001076 struct ksmbd_file *fp, __u16 tid,
1077 struct lease_ctx_info *lctx, int share_ret)
Namjae Jeone2f34482021-03-16 10:49:09 +09001078{
1079 struct ksmbd_session *sess = work->sess;
1080 int err = 0;
1081 struct oplock_info *opinfo = NULL, *prev_opinfo = NULL;
1082 struct ksmbd_inode *ci = fp->f_ci;
1083 bool prev_op_has_lease;
1084 __le32 prev_op_state = 0;
1085
1086 /* not support directory lease */
1087 if (S_ISDIR(file_inode(fp->filp)->i_mode)) {
1088 if (lctx)
1089 lctx->dlease = 1;
1090 return 0;
1091 }
1092
1093 opinfo = alloc_opinfo(work, pid, tid);
1094 if (!opinfo)
1095 return -ENOMEM;
1096
1097 if (lctx) {
1098 err = alloc_lease(opinfo, lctx);
1099 if (err)
1100 goto err_out;
1101 opinfo->is_lease = 1;
1102 }
1103
1104 /* ci does not have any oplock */
1105 if (!opinfo_count(fp))
1106 goto set_lev;
1107
1108 /* grant none-oplock if second open is trunc */
1109 if (ATTR_FP(fp)) {
1110 req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1111 goto set_lev;
1112 }
1113
1114 if (lctx) {
1115 struct oplock_info *m_opinfo;
1116
1117 /* is lease already granted ? */
1118 m_opinfo = same_client_has_lease(ci, sess->conn->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001119 lctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09001120 if (m_opinfo) {
1121 copy_lease(m_opinfo, opinfo);
1122 if (atomic_read(&m_opinfo->breaking_cnt))
1123 opinfo->o_lease->flags =
1124 SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE;
1125 goto out;
1126 }
1127 }
1128 prev_opinfo = opinfo_get_list(ci);
1129 if (!prev_opinfo ||
1130 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx))
1131 goto set_lev;
1132 prev_op_has_lease = prev_opinfo->is_lease;
1133 if (prev_op_has_lease)
1134 prev_op_state = prev_opinfo->o_lease->state;
1135
1136 if (share_ret < 0 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001137 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001138 err = share_ret;
1139 opinfo_put(prev_opinfo);
1140 goto err_out;
1141 }
1142
Namjae Jeon64b39f42021-03-30 14:25:35 +09001143 if (prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
1144 prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001145 opinfo_put(prev_opinfo);
1146 goto op_break_not_needed;
1147 }
1148
1149 list_add(&work->interim_entry, &prev_opinfo->interim_list);
1150 err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II);
1151 opinfo_put(prev_opinfo);
1152 if (err == -ENOENT)
1153 goto set_lev;
1154 /* Check all oplock was freed by close */
1155 else if (err < 0)
1156 goto err_out;
1157
1158op_break_not_needed:
1159 if (share_ret < 0) {
1160 err = share_ret;
1161 goto err_out;
1162 }
1163
1164 if (req_op_level != SMB2_OPLOCK_LEVEL_NONE)
1165 req_op_level = SMB2_OPLOCK_LEVEL_II;
1166
1167 /* grant fixed oplock on stacked locking between lease and oplock */
1168 if (prev_op_has_lease && !lctx)
1169 if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)
1170 req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1171
1172 if (!prev_op_has_lease && lctx) {
1173 req_op_level = SMB2_OPLOCK_LEVEL_II;
1174 lctx->req_state = SMB2_LEASE_READ_CACHING_LE;
1175 }
1176
1177set_lev:
1178 set_oplock_level(opinfo, req_op_level, lctx);
1179
1180out:
1181 rcu_assign_pointer(fp->f_opinfo, opinfo);
1182 opinfo->o_fp = fp;
1183
1184 opinfo_count_inc(fp);
1185 opinfo_add(opinfo);
1186 if (opinfo->is_lease) {
1187 err = add_lease_global_list(opinfo);
1188 if (err)
1189 goto err_out;
1190 }
1191
1192 return 0;
1193err_out:
1194 free_opinfo(opinfo);
1195 return err;
1196}
1197
1198/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001199 * smb_break_all_write_oplock() - break batch/exclusive oplock to level2
Namjae Jeone2f34482021-03-16 10:49:09 +09001200 * @work: smb work
1201 * @fp: ksmbd file pointer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001202 * @is_trunc: truncate on open
Namjae Jeone2f34482021-03-16 10:49:09 +09001203 */
1204static void smb_break_all_write_oplock(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001205 struct ksmbd_file *fp, int is_trunc)
Namjae Jeone2f34482021-03-16 10:49:09 +09001206{
1207 struct oplock_info *brk_opinfo;
1208
1209 brk_opinfo = opinfo_get_list(fp->f_ci);
1210 if (!brk_opinfo)
1211 return;
1212 if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001213 brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001214 opinfo_put(brk_opinfo);
1215 return;
1216 }
1217
1218 brk_opinfo->open_trunc = is_trunc;
1219 list_add(&work->interim_entry, &brk_opinfo->interim_list);
1220 oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II);
1221 opinfo_put(brk_opinfo);
1222}
1223
1224/**
1225 * smb_break_all_levII_oplock() - send level2 oplock or read lease break command
1226 * from server to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001227 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +09001228 * @fp: ksmbd file pointer
1229 * @is_trunc: truncate on open
1230 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001231void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09001232 int is_trunc)
Namjae Jeone2f34482021-03-16 10:49:09 +09001233{
1234 struct oplock_info *op, *brk_op;
1235 struct ksmbd_inode *ci;
1236 struct ksmbd_conn *conn = work->sess->conn;
1237
1238 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001239 KSMBD_SHARE_FLAG_OPLOCKS))
Namjae Jeone2f34482021-03-16 10:49:09 +09001240 return;
Namjae Jeone2f34482021-03-16 10:49:09 +09001241
1242 ci = fp->f_ci;
1243 op = opinfo_get(fp);
1244
1245 rcu_read_lock();
1246 list_for_each_entry_rcu(brk_op, &ci->m_op_list, op_entry) {
1247 if (!atomic_inc_not_zero(&brk_op->refcount))
1248 continue;
1249 rcu_read_unlock();
1250 if (brk_op->is_lease && (brk_op->o_lease->state &
1251 (~(SMB2_LEASE_READ_CACHING_LE |
1252 SMB2_LEASE_HANDLE_CACHING_LE)))) {
1253 ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001254 brk_op->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09001255 goto next;
1256 } else if (brk_op->level !=
1257 SMB2_OPLOCK_LEVEL_II) {
1258 ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001259 brk_op->level);
Namjae Jeone2f34482021-03-16 10:49:09 +09001260 goto next;
1261 }
1262
1263 /* Skip oplock being break to none */
Namjae Jeon070fb212021-05-26 17:57:12 +09001264 if (brk_op->is_lease &&
Namjae Jeonc986ed92021-05-26 17:59:56 +09001265 brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE &&
Namjae Jeone2f34482021-03-16 10:49:09 +09001266 atomic_read(&brk_op->breaking_cnt))
1267 goto next;
1268
Namjae Jeon64b39f42021-03-30 14:25:35 +09001269 if (op && op->is_lease && brk_op->is_lease &&
1270 !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID,
1271 SMB2_CLIENT_GUID_SIZE) &&
1272 !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key,
1273 SMB2_LEASE_KEY_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +09001274 goto next;
1275 brk_op->open_trunc = is_trunc;
1276 oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE);
1277next:
1278 opinfo_put(brk_op);
1279 rcu_read_lock();
1280 }
1281 rcu_read_unlock();
1282
1283 if (op)
1284 opinfo_put(op);
1285}
1286
1287/**
1288 * smb_break_all_oplock() - break both batch/exclusive and level2 oplock
1289 * @work: smb work
1290 * @fp: ksmbd file pointer
1291 */
1292void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp)
1293{
1294 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001295 KSMBD_SHARE_FLAG_OPLOCKS))
Namjae Jeone2f34482021-03-16 10:49:09 +09001296 return;
1297
1298 smb_break_all_write_oplock(work, fp, 1);
1299 smb_break_all_levII_oplock(work, fp, 1);
1300}
1301
1302/**
1303 * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type
1304 * @lease_state: lease type
1305 *
1306 * Return: 0 if no mapping, otherwise corresponding oplock type
1307 */
1308__u8 smb2_map_lease_to_oplock(__le32 lease_state)
1309{
1310 if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE |
Namjae Jeon64b39f42021-03-30 14:25:35 +09001311 SMB2_LEASE_READ_CACHING_LE |
1312 SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001313 return SMB2_OPLOCK_LEVEL_BATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001314 } else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE &&
1315 lease_state & SMB2_LEASE_WRITE_CACHING_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001316 if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE))
1317 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001318 } else if (lease_state & SMB2_LEASE_READ_CACHING_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001319 return SMB2_OPLOCK_LEVEL_II;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001320 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001321 return 0;
1322}
1323
1324/**
1325 * create_lease_buf() - create lease context for open cmd response
1326 * @rbuf: buffer to create lease context response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001327 * @lease: buffer to stored parsed lease state information
Namjae Jeone2f34482021-03-16 10:49:09 +09001328 */
1329void create_lease_buf(u8 *rbuf, struct lease *lease)
1330{
1331 struct create_lease *buf = (struct create_lease *)rbuf;
1332 char *LeaseKey = (char *)&lease->lease_key;
1333
1334 memset(buf, 0, sizeof(struct create_lease));
1335 buf->lcontext.LeaseKeyLow = *((__le64 *)LeaseKey);
1336 buf->lcontext.LeaseKeyHigh = *((__le64 *)(LeaseKey + 8));
1337 buf->lcontext.LeaseFlags = lease->flags;
1338 buf->lcontext.LeaseState = lease->state;
1339 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1340 (struct create_lease, lcontext));
1341 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1342 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1343 (struct create_lease, Name));
1344 buf->ccontext.NameLength = cpu_to_le16(4);
1345 buf->Name[0] = 'R';
1346 buf->Name[1] = 'q';
1347 buf->Name[2] = 'L';
1348 buf->Name[3] = 's';
1349}
1350
1351/**
1352 * parse_lease_state() - parse lease context containted in file open request
1353 * @open_req: buffer containing smb2 file open(create) request
Namjae Jeone2f34482021-03-16 10:49:09 +09001354 *
1355 * Return: oplock state, -ENOENT if create lease context not found
1356 */
1357struct lease_ctx_info *parse_lease_state(void *open_req)
1358{
1359 char *data_offset;
1360 struct create_context *cc;
1361 unsigned int next = 0;
1362 char *name;
1363 bool found = false;
1364 struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1365 struct lease_ctx_info *lreq = kzalloc(sizeof(struct lease_ctx_info),
1366 GFP_KERNEL);
1367 if (!lreq)
1368 return NULL;
1369
1370 data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1371 cc = (struct create_context *)data_offset;
1372 do {
1373 cc = (struct create_context *)((char *)cc + next);
1374 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1375 if (le16_to_cpu(cc->NameLength) != 4 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001376 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001377 next = le32_to_cpu(cc->Next);
1378 continue;
1379 }
1380 found = true;
1381 break;
1382 } while (next != 0);
1383
1384 if (found) {
1385 struct create_lease *lc = (struct create_lease *)cc;
1386 *((__le64 *)lreq->lease_key) = lc->lcontext.LeaseKeyLow;
1387 *((__le64 *)(lreq->lease_key + 8)) = lc->lcontext.LeaseKeyHigh;
1388 lreq->req_state = lc->lcontext.LeaseState;
1389 lreq->flags = lc->lcontext.LeaseFlags;
1390 lreq->duration = lc->lcontext.LeaseDuration;
1391 return lreq;
1392 }
1393
1394 kfree(lreq);
1395 return NULL;
1396}
1397
1398/**
1399 * smb2_find_context_vals() - find a particular context info in open request
1400 * @open_req: buffer containing smb2 file open(create) request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001401 * @tag: context name to search for
Namjae Jeone2f34482021-03-16 10:49:09 +09001402 *
1403 * Return: pointer to requested context, NULL if @str context not found
1404 */
1405struct create_context *smb2_find_context_vals(void *open_req, const char *tag)
1406{
1407 char *data_offset;
1408 struct create_context *cc;
1409 unsigned int next = 0;
1410 char *name;
1411 struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1412
1413 data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1414 cc = (struct create_context *)data_offset;
1415 do {
1416 int val;
1417
1418 cc = (struct create_context *)((char *)cc + next);
1419 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1420 val = le16_to_cpu(cc->NameLength);
1421 if (val < 4)
1422 return ERR_PTR(-EINVAL);
1423
1424 if (memcmp(name, tag, val) == 0)
1425 return cc;
1426 next = le32_to_cpu(cc->Next);
1427 } while (next != 0);
1428
1429 return ERR_PTR(-ENOENT);
1430}
1431
1432/**
Namjae Jeon53655642021-03-30 14:42:05 +09001433 * create_durable_rsp_buf() - create durable handle context
Namjae Jeone2f34482021-03-16 10:49:09 +09001434 * @cc: buffer to create durable context response
1435 */
1436void create_durable_rsp_buf(char *cc)
1437{
1438 struct create_durable_rsp *buf;
1439
1440 buf = (struct create_durable_rsp *)cc;
1441 memset(buf, 0, sizeof(struct create_durable_rsp));
1442 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1443 (struct create_durable_rsp, Data));
1444 buf->ccontext.DataLength = cpu_to_le32(8);
1445 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1446 (struct create_durable_rsp, Name));
1447 buf->ccontext.NameLength = cpu_to_le16(4);
1448 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */
1449 buf->Name[0] = 'D';
1450 buf->Name[1] = 'H';
1451 buf->Name[2] = 'n';
1452 buf->Name[3] = 'Q';
1453}
1454
1455/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001456 * create_durable_v2_rsp_buf() - create durable handle v2 context
Namjae Jeone2f34482021-03-16 10:49:09 +09001457 * @cc: buffer to create durable context response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001458 * @fp: ksmbd file pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09001459 */
1460void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp)
1461{
1462 struct create_durable_v2_rsp *buf;
1463
1464 buf = (struct create_durable_v2_rsp *)cc;
1465 memset(buf, 0, sizeof(struct create_durable_rsp));
1466 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1467 (struct create_durable_rsp, Data));
1468 buf->ccontext.DataLength = cpu_to_le32(8);
1469 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1470 (struct create_durable_rsp, Name));
1471 buf->ccontext.NameLength = cpu_to_le16(4);
1472 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */
1473 buf->Name[0] = 'D';
1474 buf->Name[1] = 'H';
1475 buf->Name[2] = '2';
1476 buf->Name[3] = 'Q';
1477
1478 buf->Timeout = cpu_to_le32(fp->durable_timeout);
Namjae Jeone2f34482021-03-16 10:49:09 +09001479}
1480
1481/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001482 * create_mxac_rsp_buf() - create query maximal access context
1483 * @cc: buffer to create maximal access context response
1484 * @maximal_access: maximal access
Namjae Jeone2f34482021-03-16 10:49:09 +09001485 */
1486void create_mxac_rsp_buf(char *cc, int maximal_access)
1487{
1488 struct create_mxac_rsp *buf;
1489
1490 buf = (struct create_mxac_rsp *)cc;
1491 memset(buf, 0, sizeof(struct create_mxac_rsp));
1492 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1493 (struct create_mxac_rsp, QueryStatus));
1494 buf->ccontext.DataLength = cpu_to_le32(8);
1495 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1496 (struct create_mxac_rsp, Name));
1497 buf->ccontext.NameLength = cpu_to_le16(4);
1498 /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */
1499 buf->Name[0] = 'M';
1500 buf->Name[1] = 'x';
1501 buf->Name[2] = 'A';
1502 buf->Name[3] = 'c';
1503
1504 buf->QueryStatus = STATUS_SUCCESS;
1505 buf->MaximalAccess = cpu_to_le32(maximal_access);
1506}
1507
Namjae Jeone2f34482021-03-16 10:49:09 +09001508void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id)
1509{
1510 struct create_disk_id_rsp *buf;
1511
1512 buf = (struct create_disk_id_rsp *)cc;
1513 memset(buf, 0, sizeof(struct create_disk_id_rsp));
1514 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1515 (struct create_disk_id_rsp, DiskFileId));
1516 buf->ccontext.DataLength = cpu_to_le32(32);
1517 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1518 (struct create_mxac_rsp, Name));
1519 buf->ccontext.NameLength = cpu_to_le16(4);
1520 /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */
1521 buf->Name[0] = 'Q';
1522 buf->Name[1] = 'F';
1523 buf->Name[2] = 'i';
1524 buf->Name[3] = 'd';
1525
1526 buf->DiskFileId = cpu_to_le64(file_id);
1527 buf->VolumeId = cpu_to_le64(vol_id);
1528}
1529
1530/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001531 * create_posix_rsp_buf() - create posix extension context
Namjae Jeone2f34482021-03-16 10:49:09 +09001532 * @cc: buffer to create posix on posix response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001533 * @fp: ksmbd file pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09001534 */
1535void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)
1536{
1537 struct create_posix_rsp *buf;
1538 struct inode *inode = FP_INODE(fp);
1539
1540 buf = (struct create_posix_rsp *)cc;
1541 memset(buf, 0, sizeof(struct create_posix_rsp));
1542 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1543 (struct create_posix_rsp, nlink));
1544 buf->ccontext.DataLength = cpu_to_le32(52);
1545 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1546 (struct create_posix_rsp, Name));
1547 buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
1548 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
1549 buf->Name[0] = 0x93;
1550 buf->Name[1] = 0xAD;
1551 buf->Name[2] = 0x25;
1552 buf->Name[3] = 0x50;
1553 buf->Name[4] = 0x9C;
1554 buf->Name[5] = 0xB4;
1555 buf->Name[6] = 0x11;
1556 buf->Name[7] = 0xE7;
1557 buf->Name[8] = 0xB4;
1558 buf->Name[9] = 0x23;
1559 buf->Name[10] = 0x83;
1560 buf->Name[11] = 0xDE;
1561 buf->Name[12] = 0x96;
1562 buf->Name[13] = 0x8B;
1563 buf->Name[14] = 0xCD;
1564 buf->Name[15] = 0x7C;
1565
1566 buf->nlink = cpu_to_le32(inode->i_nlink);
1567 buf->reparse_tag = cpu_to_le32(fp->volatile_id);
1568 buf->mode = cpu_to_le32(inode->i_mode);
1569 id_to_sid(from_kuid(&init_user_ns, inode->i_uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09001570 SIDNFS_USER, (struct smb_sid *)&buf->SidBuffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09001571 id_to_sid(from_kgid(&init_user_ns, inode->i_gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09001572 SIDNFS_GROUP, (struct smb_sid *)&buf->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09001573}
1574
1575/*
1576 * Find lease object(opinfo) for given lease key/fid from lease
1577 * break/file close path.
1578 */
1579/**
1580 * lookup_lease_in_table() - find a matching lease info object
1581 * @conn: connection instance
1582 * @lease_key: lease key to be searched for
1583 *
1584 * Return: opinfo if found matching opinfo, otherwise NULL
1585 */
1586struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001587 char *lease_key)
Namjae Jeone2f34482021-03-16 10:49:09 +09001588{
1589 struct oplock_info *opinfo = NULL, *ret_op = NULL;
1590 struct lease_table *lt;
1591 int ret;
1592
1593 read_lock(&lease_list_lock);
1594 list_for_each_entry(lt, &lease_table_list, l_entry) {
1595 if (!memcmp(lt->client_guid, conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001596 SMB2_CLIENT_GUID_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +09001597 goto found;
1598 }
1599
1600 read_unlock(&lease_list_lock);
1601 return NULL;
1602
1603found:
1604 rcu_read_lock();
1605 list_for_each_entry_rcu(opinfo, &lt->lease_list, lease_entry) {
1606 if (!atomic_inc_not_zero(&opinfo->refcount))
1607 continue;
1608 rcu_read_unlock();
Namjae Jeon64b39f42021-03-30 14:25:35 +09001609 if (!opinfo->op_state || opinfo->op_state == OPLOCK_CLOSING)
Namjae Jeone2f34482021-03-16 10:49:09 +09001610 goto op_next;
1611 if (!(opinfo->o_lease->state &
Namjae Jeon64b39f42021-03-30 14:25:35 +09001612 (SMB2_LEASE_HANDLE_CACHING_LE |
1613 SMB2_LEASE_WRITE_CACHING_LE)))
Namjae Jeone2f34482021-03-16 10:49:09 +09001614 goto op_next;
1615 ret = compare_guid_key(opinfo, conn->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001616 lease_key);
Namjae Jeone2f34482021-03-16 10:49:09 +09001617 if (ret) {
1618 ksmbd_debug(OPLOCK, "found opinfo\n");
1619 ret_op = opinfo;
1620 goto out;
1621 }
1622op_next:
1623 opinfo_put(opinfo);
1624 rcu_read_lock();
1625 }
1626 rcu_read_unlock();
1627
1628out:
1629 read_unlock(&lease_list_lock);
1630 return ret_op;
1631}
1632
1633int smb2_check_durable_oplock(struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09001634 struct lease_ctx_info *lctx, char *name)
Namjae Jeone2f34482021-03-16 10:49:09 +09001635{
1636 struct oplock_info *opinfo = opinfo_get(fp);
1637 int ret = 0;
1638
1639 if (opinfo && opinfo->is_lease) {
1640 if (!lctx) {
1641 ksmbd_err("open does not include lease\n");
1642 ret = -EBADF;
1643 goto out;
1644 }
1645 if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001646 SMB2_LEASE_KEY_SIZE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001647 ksmbd_err("invalid lease key\n");
1648 ret = -EBADF;
1649 goto out;
1650 }
1651 if (name && strcmp(fp->filename, name)) {
1652 ksmbd_err("invalid name reconnect %s\n", name);
1653 ret = -EINVAL;
1654 goto out;
1655 }
1656 }
1657out:
1658 if (opinfo)
1659 opinfo_put(opinfo);
1660 return ret;
1661}