blob: 9027cb7d970ff5f74bc926004013f3b5bf639571 [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"
Namjae Jeone2f34482021-03-16 10:49:09 +090014#include "connection.h"
15#include "mgmt/user_session.h"
16#include "mgmt/share_config.h"
17#include "mgmt/tree_connect.h"
18
19static LIST_HEAD(lease_table_list);
20static DEFINE_RWLOCK(lease_list_lock);
21
22/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090023 * alloc_opinfo() - allocate a new opinfo object for oplock info
24 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090025 * @id: fid of open file
26 * @Tid: tree id of connection
Namjae Jeone2f34482021-03-16 10:49:09 +090027 *
28 * Return: allocated opinfo object on success, otherwise NULL
29 */
30static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +090031 u64 id, __u16 Tid)
Namjae Jeone2f34482021-03-16 10:49:09 +090032{
33 struct ksmbd_session *sess = work->sess;
34 struct oplock_info *opinfo;
35
36 opinfo = kzalloc(sizeof(struct oplock_info), GFP_KERNEL);
37 if (!opinfo)
38 return NULL;
39
40 opinfo->sess = sess;
41 opinfo->conn = sess->conn;
42 opinfo->level = OPLOCK_NONE;
43 opinfo->op_state = OPLOCK_STATE_NONE;
44 opinfo->pending_break = 0;
45 opinfo->fid = id;
46 opinfo->Tid = Tid;
47 INIT_LIST_HEAD(&opinfo->op_entry);
48 INIT_LIST_HEAD(&opinfo->interim_list);
49 init_waitqueue_head(&opinfo->oplock_q);
50 init_waitqueue_head(&opinfo->oplock_brk);
51 atomic_set(&opinfo->refcount, 1);
52 atomic_set(&opinfo->breaking_cnt, 0);
53
54 return opinfo;
55}
56
57static void lease_add_list(struct oplock_info *opinfo)
58{
59 struct lease_table *lb = opinfo->o_lease->l_lb;
60
61 spin_lock(&lb->lb_lock);
62 list_add_rcu(&opinfo->lease_entry, &lb->lease_list);
63 spin_unlock(&lb->lb_lock);
64}
65
66static void lease_del_list(struct oplock_info *opinfo)
67{
68 struct lease_table *lb = opinfo->o_lease->l_lb;
69
70 if (!lb)
71 return;
72
73 spin_lock(&lb->lb_lock);
74 if (list_empty(&opinfo->lease_entry)) {
75 spin_unlock(&lb->lb_lock);
76 return;
77 }
78
79 list_del_init(&opinfo->lease_entry);
80 opinfo->o_lease->l_lb = NULL;
81 spin_unlock(&lb->lb_lock);
82}
83
84static void lb_add(struct lease_table *lb)
85{
86 write_lock(&lease_list_lock);
87 list_add(&lb->l_entry, &lease_table_list);
88 write_unlock(&lease_list_lock);
89}
90
Namjae Jeon64b39f42021-03-30 14:25:35 +090091static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +090092{
93 struct lease *lease;
94
95 lease = kmalloc(sizeof(struct lease), GFP_KERNEL);
96 if (!lease)
97 return -ENOMEM;
98
99 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
100 lease->state = lctx->req_state;
101 lease->new_state = 0;
102 lease->flags = lctx->flags;
103 lease->duration = lctx->duration;
Namjae Jeonade62d82021-06-07 09:22:22 +0900104 memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE);
105 lease->version = lctx->version;
106 lease->epoch = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900107 INIT_LIST_HEAD(&opinfo->lease_entry);
108 opinfo->o_lease = lease;
109
110 return 0;
111}
112
113static void free_lease(struct oplock_info *opinfo)
114{
115 struct lease *lease;
116
117 lease = opinfo->o_lease;
118 kfree(lease);
119}
120
121static void free_opinfo(struct oplock_info *opinfo)
122{
123 if (opinfo->is_lease)
124 free_lease(opinfo);
125 kfree(opinfo);
126}
127
128static inline void opinfo_free_rcu(struct rcu_head *rcu_head)
129{
130 struct oplock_info *opinfo;
131
132 opinfo = container_of(rcu_head, struct oplock_info, rcu_head);
133 free_opinfo(opinfo);
134}
135
136struct oplock_info *opinfo_get(struct ksmbd_file *fp)
137{
138 struct oplock_info *opinfo;
139
140 rcu_read_lock();
141 opinfo = rcu_dereference(fp->f_opinfo);
142 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
143 opinfo = NULL;
144 rcu_read_unlock();
145
146 return opinfo;
147}
148
149static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci)
150{
151 struct oplock_info *opinfo;
152
153 if (list_empty(&ci->m_op_list))
154 return NULL;
155
156 rcu_read_lock();
157 opinfo = list_first_or_null_rcu(&ci->m_op_list, struct oplock_info,
Namjae Jeon070fb212021-05-26 17:57:12 +0900158 op_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +0900159 if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
160 opinfo = NULL;
161 rcu_read_unlock();
162
163 return opinfo;
164}
165
166void opinfo_put(struct oplock_info *opinfo)
167{
168 if (!atomic_dec_and_test(&opinfo->refcount))
169 return;
170
171 call_rcu(&opinfo->rcu_head, opinfo_free_rcu);
172}
173
174static void opinfo_add(struct oplock_info *opinfo)
175{
176 struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
177
178 write_lock(&ci->m_lock);
179 list_add_rcu(&opinfo->op_entry, &ci->m_op_list);
180 write_unlock(&ci->m_lock);
181}
182
183static void opinfo_del(struct oplock_info *opinfo)
184{
185 struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
186
187 if (opinfo->is_lease) {
188 write_lock(&lease_list_lock);
189 lease_del_list(opinfo);
190 write_unlock(&lease_list_lock);
191 }
192 write_lock(&ci->m_lock);
193 list_del_rcu(&opinfo->op_entry);
194 write_unlock(&ci->m_lock);
195}
196
197static unsigned long opinfo_count(struct ksmbd_file *fp)
198{
199 if (ksmbd_stream_fd(fp))
200 return atomic_read(&fp->f_ci->sop_count);
201 else
202 return atomic_read(&fp->f_ci->op_count);
203}
204
205static void opinfo_count_inc(struct ksmbd_file *fp)
206{
207 if (ksmbd_stream_fd(fp))
208 return atomic_inc(&fp->f_ci->sop_count);
209 else
210 return atomic_inc(&fp->f_ci->op_count);
211}
212
213static void opinfo_count_dec(struct ksmbd_file *fp)
214{
215 if (ksmbd_stream_fd(fp))
216 return atomic_dec(&fp->f_ci->sop_count);
217 else
218 return atomic_dec(&fp->f_ci->op_count);
219}
220
221/**
222 * opinfo_write_to_read() - convert a write oplock to read oplock
223 * @opinfo: current oplock info
224 *
225 * Return: 0 on success, otherwise -EINVAL
226 */
227int opinfo_write_to_read(struct oplock_info *opinfo)
228{
229 struct lease *lease = opinfo->o_lease;
230
Namjae Jeon64b39f42021-03-30 14:25:35 +0900231 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
232 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900233 pr_err("bad oplock(0x%x)\n", opinfo->level);
Namjae Jeone2f34482021-03-16 10:49:09 +0900234 if (opinfo->is_lease)
Namjae Jeonbde16942021-06-28 15:23:19 +0900235 pr_err("lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900236 return -EINVAL;
237 }
238 opinfo->level = SMB2_OPLOCK_LEVEL_II;
239
240 if (opinfo->is_lease)
241 lease->state = lease->new_state;
242 return 0;
243}
244
245/**
246 * opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock
247 * @opinfo: current oplock info
248 *
249 * Return: 0 on success, otherwise -EINVAL
250 */
251int opinfo_read_handle_to_read(struct oplock_info *opinfo)
252{
253 struct lease *lease = opinfo->o_lease;
254
255 lease->state = lease->new_state;
256 opinfo->level = SMB2_OPLOCK_LEVEL_II;
257 return 0;
258}
259
260/**
261 * opinfo_write_to_none() - convert a write oplock to none
262 * @opinfo: current oplock info
263 *
264 * Return: 0 on success, otherwise -EINVAL
265 */
266int opinfo_write_to_none(struct oplock_info *opinfo)
267{
268 struct lease *lease = opinfo->o_lease;
269
Namjae Jeon64b39f42021-03-30 14:25:35 +0900270 if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
271 opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900272 pr_err("bad oplock(0x%x)\n", opinfo->level);
Namjae Jeone2f34482021-03-16 10:49:09 +0900273 if (opinfo->is_lease)
Namjae Jeonbde16942021-06-28 15:23:19 +0900274 pr_err("lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900275 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) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900294 pr_err("bad oplock(0x%x)\n", opinfo->level);
Namjae Jeone2f34482021-03-16 10:49:09 +0900295 if (opinfo->is_lease)
Namjae Jeonbde16942021-06-28 15:23:19 +0900296 pr_err("lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900297 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)) {
Namjae Jeon070fb212021-05-26 17:57:12 +0900316 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900317 return -EINVAL;
318 }
319
320 lease->new_state = SMB2_LEASE_NONE_LE;
321 lease->state |= SMB2_LEASE_WRITE_CACHING_LE;
322 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
323 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
324 else
325 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
326 return 0;
327}
328
329/**
330 * lease_none_upgrade() - upgrade lease state from none
331 * @opinfo: current lease info
332 * @new_state: new lease state
333 *
334 * Return: 0 on success, otherwise -EINVAL
335 */
Namjae Jeon64b39f42021-03-30 14:25:35 +0900336static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state)
Namjae Jeone2f34482021-03-16 10:49:09 +0900337{
338 struct lease *lease = opinfo->o_lease;
339
340 if (!(lease->state == SMB2_LEASE_NONE_LE)) {
Namjae Jeon070fb212021-05-26 17:57:12 +0900341 ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +0900342 return -EINVAL;
343 }
344
345 lease->new_state = SMB2_LEASE_NONE_LE;
346 lease->state = new_state;
347 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
348 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
349 opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
350 else
351 opinfo->level = SMB2_OPLOCK_LEVEL_II;
352 else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
353 opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
354 else if (lease->state & SMB2_LEASE_READ_CACHING_LE)
355 opinfo->level = SMB2_OPLOCK_LEVEL_II;
356
357 return 0;
358}
359
360/**
361 * close_id_del_oplock() - release oplock object at file close time
362 * @fp: ksmbd file pointer
363 */
364void close_id_del_oplock(struct ksmbd_file *fp)
365{
366 struct oplock_info *opinfo;
367
368 if (S_ISDIR(file_inode(fp->filp)->i_mode))
369 return;
370
371 opinfo = opinfo_get(fp);
372 if (!opinfo)
373 return;
374
375 opinfo_del(opinfo);
376
377 rcu_assign_pointer(fp->f_opinfo, NULL);
378 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
379 opinfo->op_state = OPLOCK_CLOSING;
380 wake_up_interruptible_all(&opinfo->oplock_q);
381 if (opinfo->is_lease) {
382 atomic_set(&opinfo->breaking_cnt, 0);
383 wake_up_interruptible_all(&opinfo->oplock_brk);
384 }
385 }
386
387 opinfo_count_dec(fp);
388 atomic_dec(&opinfo->refcount);
389 opinfo_put(opinfo);
390}
391
392/**
393 * grant_write_oplock() - grant exclusive/batch oplock or write lease
394 * @opinfo_new: new oplock info object
395 * @req_oplock: request oplock
396 * @lctx: lease context information
397 *
398 * Return: 0
399 */
400static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock,
Namjae Jeon070fb212021-05-26 17:57:12 +0900401 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900402{
403 struct lease *lease = opinfo_new->o_lease;
404
405 if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
406 opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
407 else
408 opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
409
410 if (lctx) {
411 lease->state = lctx->req_state;
Namjae Jeon070fb212021-05-26 17:57:12 +0900412 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +0900413 }
414}
415
416/**
417 * grant_read_oplock() - grant level2 oplock or read lease
418 * @opinfo_new: new oplock info object
419 * @lctx: lease context information
420 *
421 * Return: 0
422 */
423static void grant_read_oplock(struct oplock_info *opinfo_new,
Namjae Jeon070fb212021-05-26 17:57:12 +0900424 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900425{
426 struct lease *lease = opinfo_new->o_lease;
427
428 opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
429
430 if (lctx) {
431 lease->state = SMB2_LEASE_READ_CACHING_LE;
432 if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE)
433 lease->state |= SMB2_LEASE_HANDLE_CACHING_LE;
Namjae Jeon070fb212021-05-26 17:57:12 +0900434 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +0900435 }
436}
437
438/**
439 * grant_none_oplock() - grant none oplock or none lease
440 * @opinfo_new: new oplock info object
441 * @lctx: lease context information
442 *
443 * Return: 0
444 */
445static void grant_none_oplock(struct oplock_info *opinfo_new,
Namjae Jeon070fb212021-05-26 17:57:12 +0900446 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900447{
448 struct lease *lease = opinfo_new->o_lease;
449
450 opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
451
452 if (lctx) {
453 lease->state = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +0900454 memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +0900455 }
456}
457
Namjae Jeone2f34482021-03-16 10:49:09 +0900458static inline int compare_guid_key(struct oplock_info *opinfo,
Namjae Jeon070fb212021-05-26 17:57:12 +0900459 const char *guid1, const char *key1)
Namjae Jeone2f34482021-03-16 10:49:09 +0900460{
461 const char *guid2, *key2;
462
463 guid2 = opinfo->conn->ClientGUID;
464 key2 = opinfo->o_lease->lease_key;
465 if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900466 !memcmp(key1, key2, SMB2_LEASE_KEY_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900467 return 1;
468
469 return 0;
470}
471
472/**
473 * same_client_has_lease() - check whether current lease request is
474 * from lease owner of file
475 * @ci: master file pointer
476 * @client_guid: Client GUID
477 * @lctx: lease context information
478 *
479 * Return: oplock(lease) object on success, otherwise NULL
480 */
481static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,
Namjae Jeon070fb212021-05-26 17:57:12 +0900482 char *client_guid,
483 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900484{
485 int ret;
486 struct lease *lease;
487 struct oplock_info *opinfo;
488 struct oplock_info *m_opinfo = NULL;
489
490 if (!lctx)
491 return NULL;
492
493 /*
494 * Compare lease key and client_guid to know request from same owner
495 * of same client
496 */
497 read_lock(&ci->m_lock);
498 list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
499 if (!opinfo->is_lease)
500 continue;
501 read_unlock(&ci->m_lock);
502 lease = opinfo->o_lease;
503
504 ret = compare_guid_key(opinfo, client_guid, lctx->lease_key);
505 if (ret) {
506 m_opinfo = opinfo;
507 /* skip upgrading lease about breaking lease */
508 if (atomic_read(&opinfo->breaking_cnt)) {
509 read_lock(&ci->m_lock);
510 continue;
511 }
512
513 /* upgrading lease */
514 if ((atomic_read(&ci->op_count) +
515 atomic_read(&ci->sop_count)) == 1) {
516 if (lease->state ==
Namjae Jeon070fb212021-05-26 17:57:12 +0900517 (lctx->req_state & lease->state)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900518 lease->state |= lctx->req_state;
519 if (lctx->req_state &
520 SMB2_LEASE_WRITE_CACHING_LE)
521 lease_read_to_write(opinfo);
522 }
523 } else if ((atomic_read(&ci->op_count) +
524 atomic_read(&ci->sop_count)) > 1) {
525 if (lctx->req_state ==
Namjae Jeon070fb212021-05-26 17:57:12 +0900526 (SMB2_LEASE_READ_CACHING_LE |
527 SMB2_LEASE_HANDLE_CACHING_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900528 lease->state = lctx->req_state;
529 }
530
531 if (lctx->req_state && lease->state ==
Namjae Jeon070fb212021-05-26 17:57:12 +0900532 SMB2_LEASE_NONE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900533 lease_none_upgrade(opinfo, lctx->req_state);
534 }
535 read_lock(&ci->m_lock);
536 }
537 read_unlock(&ci->m_lock);
538
539 return m_opinfo;
540}
541
542static void wait_for_break_ack(struct oplock_info *opinfo)
543{
544 int rc = 0;
545
546 rc = wait_event_interruptible_timeout(opinfo->oplock_q,
Namjae Jeon070fb212021-05-26 17:57:12 +0900547 opinfo->op_state == OPLOCK_STATE_NONE ||
548 opinfo->op_state == OPLOCK_CLOSING,
549 OPLOCK_WAIT_TIME);
Namjae Jeone2f34482021-03-16 10:49:09 +0900550
551 /* is this a timeout ? */
552 if (!rc) {
553 if (opinfo->is_lease)
554 opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
555 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
556 opinfo->op_state = OPLOCK_STATE_NONE;
557 }
558}
559
560static void wake_up_oplock_break(struct oplock_info *opinfo)
561{
562 clear_bit_unlock(0, &opinfo->pending_break);
563 /* memory barrier is needed for wake_up_bit() */
564 smp_mb__after_atomic();
565 wake_up_bit(&opinfo->pending_break, 0);
566}
567
568static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)
569{
570 while (test_and_set_bit(0, &opinfo->pending_break)) {
571 wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE);
572
573 /* Not immediately break to none. */
574 opinfo->open_trunc = 0;
575
576 if (opinfo->op_state == OPLOCK_CLOSING)
577 return -ENOENT;
578 else if (!opinfo->is_lease && opinfo->level <= req_op_level)
579 return 1;
580 }
581
582 if (!opinfo->is_lease && opinfo->level <= req_op_level) {
583 wake_up_oplock_break(opinfo);
584 return 1;
585 }
586 return 0;
587}
588
589static inline int allocate_oplock_break_buf(struct ksmbd_work *work)
590{
Namjae Jeon20ea7fd2021-03-30 12:40:47 +0900591 work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +0900592 if (!work->response_buf)
593 return -ENOMEM;
594 work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
595 return 0;
596}
597
598/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900599 * __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn
Namjae Jeone2f34482021-03-16 10:49:09 +0900600 * to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900601 * @wk: smb work object
Namjae Jeone2f34482021-03-16 10:49:09 +0900602 *
603 * There are two ways this function can be called. 1- while file open we break
604 * from exclusive/batch lock to levelII oplock and 2- while file write/truncate
605 * we break from levelII oplock no oplock.
Namjae Jeone5066492021-03-30 12:35:23 +0900606 * work->request_buf contains oplock_info.
Namjae Jeone2f34482021-03-16 10:49:09 +0900607 */
608static void __smb2_oplock_break_noti(struct work_struct *wk)
609{
610 struct smb2_oplock_break *rsp = NULL;
611 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
612 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +0900613 struct oplock_break_info *br_info = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900614 struct smb2_hdr *rsp_hdr;
615 struct ksmbd_file *fp;
616
617 fp = ksmbd_lookup_durable_fd(br_info->fid);
618 if (!fp) {
619 atomic_dec(&conn->r_count);
620 ksmbd_free_work_struct(work);
621 return;
622 }
623
624 if (allocate_oplock_break_buf(work)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900625 pr_err("smb2_allocate_rsp_buf failed! ");
Namjae Jeone2f34482021-03-16 10:49:09 +0900626 atomic_dec(&conn->r_count);
Namjae Jeone2f34482021-03-16 10:49:09 +0900627 ksmbd_fd_put(work, fp);
Dan Carpentera2ba2702021-03-18 16:12:54 +0300628 ksmbd_free_work_struct(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900629 return;
630 }
631
Namjae Jeone5066492021-03-30 12:35:23 +0900632 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900633 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
634 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
635 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
636 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
637 rsp_hdr->CreditRequest = cpu_to_le16(0);
638 rsp_hdr->Command = SMB2_OPLOCK_BREAK;
639 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
640 rsp_hdr->NextCommand = 0;
641 rsp_hdr->MessageId = cpu_to_le64(-1);
642 rsp_hdr->Id.SyncId.ProcessId = 0;
643 rsp_hdr->Id.SyncId.TreeId = 0;
644 rsp_hdr->SessionId = 0;
645 memset(rsp_hdr->Signature, 0, 16);
646
Namjae Jeone5066492021-03-30 12:35:23 +0900647 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900648
649 rsp->StructureSize = cpu_to_le16(24);
650 if (!br_info->open_trunc &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900651 (br_info->level == SMB2_OPLOCK_LEVEL_BATCH ||
652 br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900653 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II;
654 else
655 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
656 rsp->Reserved = 0;
657 rsp->Reserved2 = 0;
658 rsp->PersistentFid = cpu_to_le64(fp->persistent_id);
659 rsp->VolatileFid = cpu_to_le64(fp->volatile_id);
660
661 inc_rfc1001_len(rsp, 24);
662
663 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +0900664 "sending oplock break v_id %llu p_id = %llu lock level = %d\n",
665 rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel);
Namjae Jeone2f34482021-03-16 10:49:09 +0900666
667 ksmbd_fd_put(work, fp);
668 ksmbd_conn_write(work);
669 ksmbd_free_work_struct(work);
670 atomic_dec(&conn->r_count);
671}
672
673/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900674 * smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock
Namjae Jeone2f34482021-03-16 10:49:09 +0900675 * break command from server to client
676 * @opinfo: oplock info object
Namjae Jeone2f34482021-03-16 10:49:09 +0900677 *
678 * Return: 0 on success, otherwise error
679 */
680static int smb2_oplock_break_noti(struct oplock_info *opinfo)
681{
682 struct ksmbd_conn *conn = opinfo->conn;
683 struct oplock_break_info *br_info;
684 int ret = 0;
685 struct ksmbd_work *work = ksmbd_alloc_work_struct();
686
687 if (!work)
688 return -ENOMEM;
689
690 br_info = kmalloc(sizeof(struct oplock_break_info), GFP_KERNEL);
691 if (!br_info) {
692 ksmbd_free_work_struct(work);
693 return -ENOMEM;
694 }
695
696 br_info->level = opinfo->level;
697 br_info->fid = opinfo->fid;
698 br_info->open_trunc = opinfo->open_trunc;
699
700 work->request_buf = (char *)br_info;
701 work->conn = conn;
702 work->sess = opinfo->sess;
703
704 atomic_inc(&conn->r_count);
705 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
706 INIT_WORK(&work->work, __smb2_oplock_break_noti);
707 ksmbd_queue_work(work);
708
709 wait_for_break_ack(opinfo);
710 } else {
711 __smb2_oplock_break_noti(&work->work);
712 if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
713 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
714 }
715 return ret;
716}
717
718/**
719 * __smb2_lease_break_noti() - send lease break command from server
720 * to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900721 * @wk: smb work object
Namjae Jeone2f34482021-03-16 10:49:09 +0900722 */
723static void __smb2_lease_break_noti(struct work_struct *wk)
724{
725 struct smb2_lease_break *rsp = NULL;
726 struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
Namjae Jeone5066492021-03-30 12:35:23 +0900727 struct lease_break_info *br_info = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900728 struct ksmbd_conn *conn = work->conn;
729 struct smb2_hdr *rsp_hdr;
730
731 if (allocate_oplock_break_buf(work)) {
732 ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! ");
733 ksmbd_free_work_struct(work);
734 atomic_dec(&conn->r_count);
735 return;
736 }
737
Namjae Jeone5066492021-03-30 12:35:23 +0900738 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900739 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
740 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
741 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
742 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
743 rsp_hdr->CreditRequest = cpu_to_le16(0);
744 rsp_hdr->Command = SMB2_OPLOCK_BREAK;
745 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
746 rsp_hdr->NextCommand = 0;
747 rsp_hdr->MessageId = cpu_to_le64(-1);
748 rsp_hdr->Id.SyncId.ProcessId = 0;
749 rsp_hdr->Id.SyncId.TreeId = 0;
750 rsp_hdr->SessionId = 0;
751 memset(rsp_hdr->Signature, 0, 16);
752
Namjae Jeone5066492021-03-30 12:35:23 +0900753 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900754 rsp->StructureSize = cpu_to_le16(44);
Namjae Jeonade62d82021-06-07 09:22:22 +0900755 rsp->Epoch = br_info->epoch;
Namjae Jeone2f34482021-03-16 10:49:09 +0900756 rsp->Flags = 0;
757
758 if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE |
759 SMB2_LEASE_HANDLE_CACHING_LE))
760 rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
761
762 memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE);
763 rsp->CurrentLeaseState = br_info->curr_state;
764 rsp->NewLeaseState = br_info->new_state;
765 rsp->BreakReason = 0;
766 rsp->AccessMaskHint = 0;
767 rsp->ShareMaskHint = 0;
768
769 inc_rfc1001_len(rsp, 44);
770
771 ksmbd_conn_write(work);
772 ksmbd_free_work_struct(work);
773 atomic_dec(&conn->r_count);
774}
775
776/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900777 * smb2_lease_break_noti() - break lease when a new client request
Namjae Jeone2f34482021-03-16 10:49:09 +0900778 * write lease
779 * @opinfo: conains lease state information
Namjae Jeone2f34482021-03-16 10:49:09 +0900780 *
781 * Return: 0 on success, otherwise error
782 */
783static int smb2_lease_break_noti(struct oplock_info *opinfo)
784{
785 struct ksmbd_conn *conn = opinfo->conn;
786 struct list_head *tmp, *t;
787 struct ksmbd_work *work;
788 struct lease_break_info *br_info;
789 struct lease *lease = opinfo->o_lease;
790
791 work = ksmbd_alloc_work_struct();
792 if (!work)
793 return -ENOMEM;
794
795 br_info = kmalloc(sizeof(struct lease_break_info), GFP_KERNEL);
796 if (!br_info) {
797 ksmbd_free_work_struct(work);
798 return -ENOMEM;
799 }
800
801 br_info->curr_state = lease->state;
802 br_info->new_state = lease->new_state;
Namjae Jeonade62d82021-06-07 09:22:22 +0900803 if (lease->version == 2)
804 br_info->epoch = cpu_to_le16(++lease->epoch);
805 else
806 br_info->epoch = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900807 memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE);
808
809 work->request_buf = (char *)br_info;
810 work->conn = conn;
811 work->sess = opinfo->sess;
812
813 atomic_inc(&conn->r_count);
814 if (opinfo->op_state == OPLOCK_ACK_WAIT) {
815 list_for_each_safe(tmp, t, &opinfo->interim_list) {
816 struct ksmbd_work *in_work;
817
818 in_work = list_entry(tmp, struct ksmbd_work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900819 interim_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +0900820 setup_async_work(in_work, NULL, NULL);
821 smb2_send_interim_resp(in_work, STATUS_PENDING);
822 list_del(&in_work->interim_entry);
823 }
824 INIT_WORK(&work->work, __smb2_lease_break_noti);
825 ksmbd_queue_work(work);
826 wait_for_break_ack(opinfo);
827 } else {
828 __smb2_lease_break_noti(&work->work);
829 if (opinfo->o_lease->new_state == SMB2_LEASE_NONE_LE) {
830 opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
831 opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
832 }
833 }
834 return 0;
835}
836
837static void wait_lease_breaking(struct oplock_info *opinfo)
838{
839 if (!opinfo->is_lease)
840 return;
841
842 wake_up_interruptible_all(&opinfo->oplock_brk);
843 if (atomic_read(&opinfo->breaking_cnt)) {
844 int ret = 0;
845
Namjae Jeon64b39f42021-03-30 14:25:35 +0900846 ret = wait_event_interruptible_timeout(opinfo->oplock_brk,
Namjae Jeon070fb212021-05-26 17:57:12 +0900847 atomic_read(&opinfo->breaking_cnt) == 0,
848 HZ);
Namjae Jeone2f34482021-03-16 10:49:09 +0900849 if (!ret)
850 atomic_set(&opinfo->breaking_cnt, 0);
851 }
852}
853
854static int oplock_break(struct oplock_info *brk_opinfo, int req_op_level)
855{
856 int err = 0;
857
858 /* Need to break exclusive/batch oplock, write lease or overwrite_if */
859 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +0900860 "request to send oplock(level : 0x%x) break notification\n",
861 brk_opinfo->level);
Namjae Jeone2f34482021-03-16 10:49:09 +0900862
863 if (brk_opinfo->is_lease) {
864 struct lease *lease = brk_opinfo->o_lease;
865
866 atomic_inc(&brk_opinfo->breaking_cnt);
867
868 err = oplock_break_pending(brk_opinfo, req_op_level);
869 if (err)
870 return err < 0 ? err : 0;
871
872 if (brk_opinfo->open_trunc) {
873 /*
874 * Create overwrite break trigger the lease break to
875 * none.
876 */
877 lease->new_state = SMB2_LEASE_NONE_LE;
878 } else {
879 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) {
880 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
881 lease->new_state =
882 SMB2_LEASE_READ_CACHING_LE |
883 SMB2_LEASE_HANDLE_CACHING_LE;
884 else
885 lease->new_state =
886 SMB2_LEASE_READ_CACHING_LE;
887 } else {
888 if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
889 lease->new_state =
890 SMB2_LEASE_READ_CACHING_LE;
891 else
892 lease->new_state = SMB2_LEASE_NONE_LE;
893 }
894 }
895
896 if (lease->state & (SMB2_LEASE_WRITE_CACHING_LE |
897 SMB2_LEASE_HANDLE_CACHING_LE))
898 brk_opinfo->op_state = OPLOCK_ACK_WAIT;
899 else
900 atomic_dec(&brk_opinfo->breaking_cnt);
901 } else {
902 err = oplock_break_pending(brk_opinfo, req_op_level);
903 if (err)
904 return err < 0 ? err : 0;
905
906 if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900907 brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900908 brk_opinfo->op_state = OPLOCK_ACK_WAIT;
909 }
910
911 if (brk_opinfo->is_lease)
912 err = smb2_lease_break_noti(brk_opinfo);
913 else
914 err = smb2_oplock_break_noti(brk_opinfo);
915
916 ksmbd_debug(OPLOCK, "oplock granted = %d\n", brk_opinfo->level);
917 if (brk_opinfo->op_state == OPLOCK_CLOSING)
918 err = -ENOENT;
919 wake_up_oplock_break(brk_opinfo);
920
921 wait_lease_breaking(brk_opinfo);
922
923 return err;
924}
925
926void destroy_lease_table(struct ksmbd_conn *conn)
927{
928 struct lease_table *lb, *lbtmp;
929 struct oplock_info *opinfo;
930
931 write_lock(&lease_list_lock);
932 if (list_empty(&lease_table_list)) {
933 write_unlock(&lease_list_lock);
934 return;
935 }
936
937 list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) {
938 if (conn && memcmp(lb->client_guid, conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +0900939 SMB2_CLIENT_GUID_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900940 continue;
941again:
942 rcu_read_lock();
943 list_for_each_entry_rcu(opinfo, &lb->lease_list,
Namjae Jeon070fb212021-05-26 17:57:12 +0900944 lease_entry) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900945 rcu_read_unlock();
946 lease_del_list(opinfo);
947 goto again;
948 }
949 rcu_read_unlock();
950 list_del(&lb->l_entry);
951 kfree(lb);
952 }
953 write_unlock(&lease_list_lock);
954}
955
956int find_same_lease_key(struct ksmbd_session *sess, struct ksmbd_inode *ci,
Namjae Jeon070fb212021-05-26 17:57:12 +0900957 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +0900958{
959 struct oplock_info *opinfo;
960 int err = 0;
961 struct lease_table *lb;
962
963 if (!lctx)
964 return err;
965
966 read_lock(&lease_list_lock);
967 if (list_empty(&lease_table_list)) {
968 read_unlock(&lease_list_lock);
969 return 0;
970 }
971
972 list_for_each_entry(lb, &lease_table_list, l_entry) {
973 if (!memcmp(lb->client_guid, sess->conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +0900974 SMB2_CLIENT_GUID_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +0900975 goto found;
976 }
977 read_unlock(&lease_list_lock);
978
979 return 0;
980
981found:
982 rcu_read_lock();
Namjae Jeon070fb212021-05-26 17:57:12 +0900983 list_for_each_entry_rcu(opinfo, &lb->lease_list, lease_entry) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900984 if (!atomic_inc_not_zero(&opinfo->refcount))
985 continue;
986 rcu_read_unlock();
987 if (opinfo->o_fp->f_ci == ci)
988 goto op_next;
Namjae Jeon070fb212021-05-26 17:57:12 +0900989 err = compare_guid_key(opinfo, sess->conn->ClientGUID,
990 lctx->lease_key);
Namjae Jeone2f34482021-03-16 10:49:09 +0900991 if (err) {
992 err = -EINVAL;
993 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +0900994 "found same lease key is already used in other files\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900995 opinfo_put(opinfo);
996 goto out;
997 }
998op_next:
999 opinfo_put(opinfo);
1000 rcu_read_lock();
1001 }
1002 rcu_read_unlock();
1003
1004out:
1005 read_unlock(&lease_list_lock);
1006 return err;
1007}
1008
1009static void copy_lease(struct oplock_info *op1, struct oplock_info *op2)
1010{
1011 struct lease *lease1 = op1->o_lease;
1012 struct lease *lease2 = op2->o_lease;
1013
1014 op2->level = op1->level;
1015 lease2->state = lease1->state;
1016 memcpy(lease2->lease_key, lease1->lease_key,
Namjae Jeon070fb212021-05-26 17:57:12 +09001017 SMB2_LEASE_KEY_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001018 lease2->duration = lease1->duration;
1019 lease2->flags = lease1->flags;
1020}
1021
1022static int add_lease_global_list(struct oplock_info *opinfo)
1023{
1024 struct lease_table *lb;
1025
1026 read_lock(&lease_list_lock);
1027 list_for_each_entry(lb, &lease_table_list, l_entry) {
1028 if (!memcmp(lb->client_guid, opinfo->conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001029 SMB2_CLIENT_GUID_SIZE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001030 opinfo->o_lease->l_lb = lb;
1031 lease_add_list(opinfo);
1032 read_unlock(&lease_list_lock);
1033 return 0;
1034 }
1035 }
1036 read_unlock(&lease_list_lock);
1037
1038 lb = kmalloc(sizeof(struct lease_table), GFP_KERNEL);
1039 if (!lb)
1040 return -ENOMEM;
1041
1042 memcpy(lb->client_guid, opinfo->conn->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001043 SMB2_CLIENT_GUID_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001044 INIT_LIST_HEAD(&lb->lease_list);
1045 spin_lock_init(&lb->lb_lock);
1046 opinfo->o_lease->l_lb = lb;
1047 lease_add_list(opinfo);
1048 lb_add(lb);
1049 return 0;
1050}
1051
1052static void set_oplock_level(struct oplock_info *opinfo, int level,
Namjae Jeon070fb212021-05-26 17:57:12 +09001053 struct lease_ctx_info *lctx)
Namjae Jeone2f34482021-03-16 10:49:09 +09001054{
1055 switch (level) {
1056 case SMB2_OPLOCK_LEVEL_BATCH:
1057 case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09001058 grant_write_oplock(opinfo, level, lctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09001059 break;
1060 case SMB2_OPLOCK_LEVEL_II:
1061 grant_read_oplock(opinfo, lctx);
1062 break;
1063 default:
1064 grant_none_oplock(opinfo, lctx);
1065 break;
1066 }
1067}
1068
1069/**
1070 * smb_grant_oplock() - handle oplock/lease request on file open
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001071 * @work: smb work
1072 * @req_op_level: oplock level
1073 * @pid: id of open file
1074 * @fp: ksmbd file pointer
1075 * @tid: Tree id of connection
1076 * @lctx: lease context information on file open
1077 * @share_ret: share mode
Namjae Jeone2f34482021-03-16 10:49:09 +09001078 *
1079 * Return: 0 on success, otherwise error
1080 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001081int smb_grant_oplock(struct ksmbd_work *work, int req_op_level, u64 pid,
Namjae Jeon070fb212021-05-26 17:57:12 +09001082 struct ksmbd_file *fp, __u16 tid,
1083 struct lease_ctx_info *lctx, int share_ret)
Namjae Jeone2f34482021-03-16 10:49:09 +09001084{
1085 struct ksmbd_session *sess = work->sess;
1086 int err = 0;
1087 struct oplock_info *opinfo = NULL, *prev_opinfo = NULL;
1088 struct ksmbd_inode *ci = fp->f_ci;
1089 bool prev_op_has_lease;
1090 __le32 prev_op_state = 0;
1091
1092 /* not support directory lease */
Namjae Jeonade62d82021-06-07 09:22:22 +09001093 if (S_ISDIR(file_inode(fp->filp)->i_mode))
Namjae Jeone2f34482021-03-16 10:49:09 +09001094 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09001095
1096 opinfo = alloc_opinfo(work, pid, tid);
1097 if (!opinfo)
1098 return -ENOMEM;
1099
1100 if (lctx) {
1101 err = alloc_lease(opinfo, lctx);
1102 if (err)
1103 goto err_out;
1104 opinfo->is_lease = 1;
1105 }
1106
1107 /* ci does not have any oplock */
1108 if (!opinfo_count(fp))
1109 goto set_lev;
1110
1111 /* grant none-oplock if second open is trunc */
1112 if (ATTR_FP(fp)) {
1113 req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1114 goto set_lev;
1115 }
1116
1117 if (lctx) {
1118 struct oplock_info *m_opinfo;
1119
1120 /* is lease already granted ? */
1121 m_opinfo = same_client_has_lease(ci, sess->conn->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001122 lctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09001123 if (m_opinfo) {
1124 copy_lease(m_opinfo, opinfo);
1125 if (atomic_read(&m_opinfo->breaking_cnt))
1126 opinfo->o_lease->flags =
1127 SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE;
1128 goto out;
1129 }
1130 }
1131 prev_opinfo = opinfo_get_list(ci);
1132 if (!prev_opinfo ||
1133 (prev_opinfo->level == SMB2_OPLOCK_LEVEL_NONE && lctx))
1134 goto set_lev;
1135 prev_op_has_lease = prev_opinfo->is_lease;
1136 if (prev_op_has_lease)
1137 prev_op_state = prev_opinfo->o_lease->state;
1138
1139 if (share_ret < 0 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001140 prev_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001141 err = share_ret;
1142 opinfo_put(prev_opinfo);
1143 goto err_out;
1144 }
1145
Namjae Jeon64b39f42021-03-30 14:25:35 +09001146 if (prev_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
1147 prev_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001148 opinfo_put(prev_opinfo);
1149 goto op_break_not_needed;
1150 }
1151
1152 list_add(&work->interim_entry, &prev_opinfo->interim_list);
1153 err = oplock_break(prev_opinfo, SMB2_OPLOCK_LEVEL_II);
1154 opinfo_put(prev_opinfo);
1155 if (err == -ENOENT)
1156 goto set_lev;
1157 /* Check all oplock was freed by close */
1158 else if (err < 0)
1159 goto err_out;
1160
1161op_break_not_needed:
1162 if (share_ret < 0) {
1163 err = share_ret;
1164 goto err_out;
1165 }
1166
1167 if (req_op_level != SMB2_OPLOCK_LEVEL_NONE)
1168 req_op_level = SMB2_OPLOCK_LEVEL_II;
1169
1170 /* grant fixed oplock on stacked locking between lease and oplock */
1171 if (prev_op_has_lease && !lctx)
1172 if (prev_op_state & SMB2_LEASE_HANDLE_CACHING_LE)
1173 req_op_level = SMB2_OPLOCK_LEVEL_NONE;
1174
1175 if (!prev_op_has_lease && lctx) {
1176 req_op_level = SMB2_OPLOCK_LEVEL_II;
1177 lctx->req_state = SMB2_LEASE_READ_CACHING_LE;
1178 }
1179
1180set_lev:
1181 set_oplock_level(opinfo, req_op_level, lctx);
1182
1183out:
1184 rcu_assign_pointer(fp->f_opinfo, opinfo);
1185 opinfo->o_fp = fp;
1186
1187 opinfo_count_inc(fp);
1188 opinfo_add(opinfo);
1189 if (opinfo->is_lease) {
1190 err = add_lease_global_list(opinfo);
1191 if (err)
1192 goto err_out;
1193 }
1194
1195 return 0;
1196err_out:
1197 free_opinfo(opinfo);
1198 return err;
1199}
1200
1201/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001202 * smb_break_all_write_oplock() - break batch/exclusive oplock to level2
Namjae Jeone2f34482021-03-16 10:49:09 +09001203 * @work: smb work
1204 * @fp: ksmbd file pointer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001205 * @is_trunc: truncate on open
Namjae Jeone2f34482021-03-16 10:49:09 +09001206 */
1207static void smb_break_all_write_oplock(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001208 struct ksmbd_file *fp, int is_trunc)
Namjae Jeone2f34482021-03-16 10:49:09 +09001209{
1210 struct oplock_info *brk_opinfo;
1211
1212 brk_opinfo = opinfo_get_list(fp->f_ci);
1213 if (!brk_opinfo)
1214 return;
1215 if (brk_opinfo->level != SMB2_OPLOCK_LEVEL_BATCH &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001216 brk_opinfo->level != SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001217 opinfo_put(brk_opinfo);
1218 return;
1219 }
1220
1221 brk_opinfo->open_trunc = is_trunc;
1222 list_add(&work->interim_entry, &brk_opinfo->interim_list);
1223 oplock_break(brk_opinfo, SMB2_OPLOCK_LEVEL_II);
1224 opinfo_put(brk_opinfo);
1225}
1226
1227/**
1228 * smb_break_all_levII_oplock() - send level2 oplock or read lease break command
1229 * from server to client
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001230 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +09001231 * @fp: ksmbd file pointer
1232 * @is_trunc: truncate on open
1233 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001234void smb_break_all_levII_oplock(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09001235 int is_trunc)
Namjae Jeone2f34482021-03-16 10:49:09 +09001236{
1237 struct oplock_info *op, *brk_op;
1238 struct ksmbd_inode *ci;
1239 struct ksmbd_conn *conn = work->sess->conn;
1240
1241 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001242 KSMBD_SHARE_FLAG_OPLOCKS))
Namjae Jeone2f34482021-03-16 10:49:09 +09001243 return;
Namjae Jeone2f34482021-03-16 10:49:09 +09001244
1245 ci = fp->f_ci;
1246 op = opinfo_get(fp);
1247
1248 rcu_read_lock();
1249 list_for_each_entry_rcu(brk_op, &ci->m_op_list, op_entry) {
1250 if (!atomic_inc_not_zero(&brk_op->refcount))
1251 continue;
1252 rcu_read_unlock();
1253 if (brk_op->is_lease && (brk_op->o_lease->state &
1254 (~(SMB2_LEASE_READ_CACHING_LE |
1255 SMB2_LEASE_HANDLE_CACHING_LE)))) {
1256 ksmbd_debug(OPLOCK, "unexpected lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001257 brk_op->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09001258 goto next;
1259 } else if (brk_op->level !=
1260 SMB2_OPLOCK_LEVEL_II) {
1261 ksmbd_debug(OPLOCK, "unexpected oplock(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001262 brk_op->level);
Namjae Jeone2f34482021-03-16 10:49:09 +09001263 goto next;
1264 }
1265
1266 /* Skip oplock being break to none */
Namjae Jeon070fb212021-05-26 17:57:12 +09001267 if (brk_op->is_lease &&
Namjae Jeonc986ed92021-05-26 17:59:56 +09001268 brk_op->o_lease->new_state == SMB2_LEASE_NONE_LE &&
Namjae Jeone2f34482021-03-16 10:49:09 +09001269 atomic_read(&brk_op->breaking_cnt))
1270 goto next;
1271
Namjae Jeon64b39f42021-03-30 14:25:35 +09001272 if (op && op->is_lease && brk_op->is_lease &&
1273 !memcmp(conn->ClientGUID, brk_op->conn->ClientGUID,
1274 SMB2_CLIENT_GUID_SIZE) &&
1275 !memcmp(op->o_lease->lease_key, brk_op->o_lease->lease_key,
1276 SMB2_LEASE_KEY_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +09001277 goto next;
1278 brk_op->open_trunc = is_trunc;
1279 oplock_break(brk_op, SMB2_OPLOCK_LEVEL_NONE);
1280next:
1281 opinfo_put(brk_op);
1282 rcu_read_lock();
1283 }
1284 rcu_read_unlock();
1285
1286 if (op)
1287 opinfo_put(op);
1288}
1289
1290/**
1291 * smb_break_all_oplock() - break both batch/exclusive and level2 oplock
1292 * @work: smb work
1293 * @fp: ksmbd file pointer
1294 */
1295void smb_break_all_oplock(struct ksmbd_work *work, struct ksmbd_file *fp)
1296{
1297 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001298 KSMBD_SHARE_FLAG_OPLOCKS))
Namjae Jeone2f34482021-03-16 10:49:09 +09001299 return;
1300
1301 smb_break_all_write_oplock(work, fp, 1);
1302 smb_break_all_levII_oplock(work, fp, 1);
1303}
1304
1305/**
1306 * smb2_map_lease_to_oplock() - map lease state to corresponding oplock type
1307 * @lease_state: lease type
1308 *
1309 * Return: 0 if no mapping, otherwise corresponding oplock type
1310 */
1311__u8 smb2_map_lease_to_oplock(__le32 lease_state)
1312{
1313 if (lease_state == (SMB2_LEASE_HANDLE_CACHING_LE |
Namjae Jeon64b39f42021-03-30 14:25:35 +09001314 SMB2_LEASE_READ_CACHING_LE |
1315 SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001316 return SMB2_OPLOCK_LEVEL_BATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001317 } else if (lease_state != SMB2_LEASE_WRITE_CACHING_LE &&
1318 lease_state & SMB2_LEASE_WRITE_CACHING_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001319 if (!(lease_state & SMB2_LEASE_HANDLE_CACHING_LE))
1320 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001321 } else if (lease_state & SMB2_LEASE_READ_CACHING_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001322 return SMB2_OPLOCK_LEVEL_II;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001323 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001324 return 0;
1325}
1326
1327/**
1328 * create_lease_buf() - create lease context for open cmd response
1329 * @rbuf: buffer to create lease context response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001330 * @lease: buffer to stored parsed lease state information
Namjae Jeone2f34482021-03-16 10:49:09 +09001331 */
1332void create_lease_buf(u8 *rbuf, struct lease *lease)
1333{
Namjae Jeone2f34482021-03-16 10:49:09 +09001334 char *LeaseKey = (char *)&lease->lease_key;
1335
Namjae Jeonade62d82021-06-07 09:22:22 +09001336 if (lease->version == 2) {
1337 struct create_lease_v2 *buf = (struct create_lease_v2 *)rbuf;
1338 char *ParentLeaseKey = (char *)&lease->parent_lease_key;
1339
1340 memset(buf, 0, sizeof(struct create_lease_v2));
1341 buf->lcontext.LeaseKeyLow = *((__le64 *)LeaseKey);
1342 buf->lcontext.LeaseKeyHigh = *((__le64 *)(LeaseKey + 8));
1343 buf->lcontext.LeaseFlags = lease->flags;
1344 buf->lcontext.LeaseState = lease->state;
1345 buf->lcontext.ParentLeaseKeyLow = *((__le64 *)ParentLeaseKey);
1346 buf->lcontext.ParentLeaseKeyHigh = *((__le64 *)(ParentLeaseKey + 8));
1347 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1348 (struct create_lease_v2, lcontext));
1349 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1350 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1351 (struct create_lease_v2, Name));
1352 buf->ccontext.NameLength = cpu_to_le16(4);
1353 buf->Name[0] = 'R';
1354 buf->Name[1] = 'q';
1355 buf->Name[2] = 'L';
1356 buf->Name[3] = 's';
1357 } else {
1358 struct create_lease *buf = (struct create_lease *)rbuf;
1359
1360 memset(buf, 0, sizeof(struct create_lease));
1361 buf->lcontext.LeaseKeyLow = *((__le64 *)LeaseKey);
1362 buf->lcontext.LeaseKeyHigh = *((__le64 *)(LeaseKey + 8));
1363 buf->lcontext.LeaseFlags = lease->flags;
1364 buf->lcontext.LeaseState = lease->state;
1365 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1366 (struct create_lease, lcontext));
1367 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1368 buf->ccontext.NameOffset = cpu_to_le16(offsetof
Namjae Jeone2f34482021-03-16 10:49:09 +09001369 (struct create_lease, Name));
Namjae Jeonade62d82021-06-07 09:22:22 +09001370 buf->ccontext.NameLength = cpu_to_le16(4);
1371 buf->Name[0] = 'R';
1372 buf->Name[1] = 'q';
1373 buf->Name[2] = 'L';
1374 buf->Name[3] = 's';
1375 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001376}
1377
1378/**
1379 * parse_lease_state() - parse lease context containted in file open request
1380 * @open_req: buffer containing smb2 file open(create) request
Namjae Jeone2f34482021-03-16 10:49:09 +09001381 *
1382 * Return: oplock state, -ENOENT if create lease context not found
1383 */
1384struct lease_ctx_info *parse_lease_state(void *open_req)
1385{
1386 char *data_offset;
1387 struct create_context *cc;
1388 unsigned int next = 0;
1389 char *name;
1390 bool found = false;
1391 struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1392 struct lease_ctx_info *lreq = kzalloc(sizeof(struct lease_ctx_info),
1393 GFP_KERNEL);
1394 if (!lreq)
1395 return NULL;
1396
1397 data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1398 cc = (struct create_context *)data_offset;
1399 do {
1400 cc = (struct create_context *)((char *)cc + next);
1401 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1402 if (le16_to_cpu(cc->NameLength) != 4 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001403 strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001404 next = le32_to_cpu(cc->Next);
1405 continue;
1406 }
1407 found = true;
1408 break;
1409 } while (next != 0);
1410
1411 if (found) {
Namjae Jeonade62d82021-06-07 09:22:22 +09001412 if (sizeof(struct lease_context_v2) == le32_to_cpu(cc->DataLength)) {
1413 struct create_lease_v2 *lc = (struct create_lease_v2 *)cc;
1414
1415 *((__le64 *)lreq->lease_key) = lc->lcontext.LeaseKeyLow;
1416 *((__le64 *)(lreq->lease_key + 8)) = lc->lcontext.LeaseKeyHigh;
1417 lreq->req_state = lc->lcontext.LeaseState;
1418 lreq->flags = lc->lcontext.LeaseFlags;
1419 lreq->duration = lc->lcontext.LeaseDuration;
1420 *((__le64 *)lreq->parent_lease_key) = lc->lcontext.ParentLeaseKeyLow;
1421 *((__le64 *)(lreq->parent_lease_key + 8)) = lc->lcontext.ParentLeaseKeyHigh;
1422 lreq->version = 2;
1423 } else {
1424 struct create_lease *lc = (struct create_lease *)cc;
1425
1426 *((__le64 *)lreq->lease_key) = lc->lcontext.LeaseKeyLow;
1427 *((__le64 *)(lreq->lease_key + 8)) = lc->lcontext.LeaseKeyHigh;
1428 lreq->req_state = lc->lcontext.LeaseState;
1429 lreq->flags = lc->lcontext.LeaseFlags;
1430 lreq->duration = lc->lcontext.LeaseDuration;
1431 lreq->version = 1;
1432 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001433 return lreq;
1434 }
1435
1436 kfree(lreq);
1437 return NULL;
1438}
1439
1440/**
1441 * smb2_find_context_vals() - find a particular context info in open request
1442 * @open_req: buffer containing smb2 file open(create) request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001443 * @tag: context name to search for
Namjae Jeone2f34482021-03-16 10:49:09 +09001444 *
1445 * Return: pointer to requested context, NULL if @str context not found
1446 */
1447struct create_context *smb2_find_context_vals(void *open_req, const char *tag)
1448{
1449 char *data_offset;
1450 struct create_context *cc;
1451 unsigned int next = 0;
1452 char *name;
1453 struct smb2_create_req *req = (struct smb2_create_req *)open_req;
1454
1455 data_offset = (char *)req + 4 + le32_to_cpu(req->CreateContextsOffset);
1456 cc = (struct create_context *)data_offset;
1457 do {
1458 int val;
1459
1460 cc = (struct create_context *)((char *)cc + next);
1461 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1462 val = le16_to_cpu(cc->NameLength);
1463 if (val < 4)
1464 return ERR_PTR(-EINVAL);
1465
1466 if (memcmp(name, tag, val) == 0)
1467 return cc;
1468 next = le32_to_cpu(cc->Next);
1469 } while (next != 0);
1470
1471 return ERR_PTR(-ENOENT);
1472}
1473
1474/**
Namjae Jeon53655642021-03-30 14:42:05 +09001475 * create_durable_rsp_buf() - create durable handle context
Namjae Jeone2f34482021-03-16 10:49:09 +09001476 * @cc: buffer to create durable context response
1477 */
1478void create_durable_rsp_buf(char *cc)
1479{
1480 struct create_durable_rsp *buf;
1481
1482 buf = (struct create_durable_rsp *)cc;
1483 memset(buf, 0, sizeof(struct create_durable_rsp));
1484 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1485 (struct create_durable_rsp, Data));
1486 buf->ccontext.DataLength = cpu_to_le32(8);
1487 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1488 (struct create_durable_rsp, Name));
1489 buf->ccontext.NameLength = cpu_to_le16(4);
1490 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE is "DHnQ" */
1491 buf->Name[0] = 'D';
1492 buf->Name[1] = 'H';
1493 buf->Name[2] = 'n';
1494 buf->Name[3] = 'Q';
1495}
1496
1497/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001498 * create_durable_v2_rsp_buf() - create durable handle v2 context
Namjae Jeone2f34482021-03-16 10:49:09 +09001499 * @cc: buffer to create durable context response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001500 * @fp: ksmbd file pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09001501 */
1502void create_durable_v2_rsp_buf(char *cc, struct ksmbd_file *fp)
1503{
1504 struct create_durable_v2_rsp *buf;
1505
1506 buf = (struct create_durable_v2_rsp *)cc;
1507 memset(buf, 0, sizeof(struct create_durable_rsp));
1508 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1509 (struct create_durable_rsp, Data));
1510 buf->ccontext.DataLength = cpu_to_le32(8);
1511 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1512 (struct create_durable_rsp, Name));
1513 buf->ccontext.NameLength = cpu_to_le16(4);
1514 /* SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2 is "DH2Q" */
1515 buf->Name[0] = 'D';
1516 buf->Name[1] = 'H';
1517 buf->Name[2] = '2';
1518 buf->Name[3] = 'Q';
1519
1520 buf->Timeout = cpu_to_le32(fp->durable_timeout);
Namjae Jeone2f34482021-03-16 10:49:09 +09001521}
1522
1523/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001524 * create_mxac_rsp_buf() - create query maximal access context
1525 * @cc: buffer to create maximal access context response
1526 * @maximal_access: maximal access
Namjae Jeone2f34482021-03-16 10:49:09 +09001527 */
1528void create_mxac_rsp_buf(char *cc, int maximal_access)
1529{
1530 struct create_mxac_rsp *buf;
1531
1532 buf = (struct create_mxac_rsp *)cc;
1533 memset(buf, 0, sizeof(struct create_mxac_rsp));
1534 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1535 (struct create_mxac_rsp, QueryStatus));
1536 buf->ccontext.DataLength = cpu_to_le32(8);
1537 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1538 (struct create_mxac_rsp, Name));
1539 buf->ccontext.NameLength = cpu_to_le16(4);
1540 /* SMB2_CREATE_QUERY_MAXIMAL_ACCESS_RESPONSE is "MxAc" */
1541 buf->Name[0] = 'M';
1542 buf->Name[1] = 'x';
1543 buf->Name[2] = 'A';
1544 buf->Name[3] = 'c';
1545
1546 buf->QueryStatus = STATUS_SUCCESS;
1547 buf->MaximalAccess = cpu_to_le32(maximal_access);
1548}
1549
Namjae Jeone2f34482021-03-16 10:49:09 +09001550void create_disk_id_rsp_buf(char *cc, __u64 file_id, __u64 vol_id)
1551{
1552 struct create_disk_id_rsp *buf;
1553
1554 buf = (struct create_disk_id_rsp *)cc;
1555 memset(buf, 0, sizeof(struct create_disk_id_rsp));
1556 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1557 (struct create_disk_id_rsp, DiskFileId));
1558 buf->ccontext.DataLength = cpu_to_le32(32);
1559 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1560 (struct create_mxac_rsp, Name));
1561 buf->ccontext.NameLength = cpu_to_le16(4);
1562 /* SMB2_CREATE_QUERY_ON_DISK_ID_RESPONSE is "QFid" */
1563 buf->Name[0] = 'Q';
1564 buf->Name[1] = 'F';
1565 buf->Name[2] = 'i';
1566 buf->Name[3] = 'd';
1567
1568 buf->DiskFileId = cpu_to_le64(file_id);
1569 buf->VolumeId = cpu_to_le64(vol_id);
1570}
1571
1572/**
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001573 * create_posix_rsp_buf() - create posix extension context
Namjae Jeone2f34482021-03-16 10:49:09 +09001574 * @cc: buffer to create posix on posix response
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09001575 * @fp: ksmbd file pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09001576 */
1577void create_posix_rsp_buf(char *cc, struct ksmbd_file *fp)
1578{
1579 struct create_posix_rsp *buf;
1580 struct inode *inode = FP_INODE(fp);
1581
1582 buf = (struct create_posix_rsp *)cc;
1583 memset(buf, 0, sizeof(struct create_posix_rsp));
1584 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1585 (struct create_posix_rsp, nlink));
1586 buf->ccontext.DataLength = cpu_to_le32(52);
1587 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1588 (struct create_posix_rsp, Name));
1589 buf->ccontext.NameLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
1590 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
1591 buf->Name[0] = 0x93;
1592 buf->Name[1] = 0xAD;
1593 buf->Name[2] = 0x25;
1594 buf->Name[3] = 0x50;
1595 buf->Name[4] = 0x9C;
1596 buf->Name[5] = 0xB4;
1597 buf->Name[6] = 0x11;
1598 buf->Name[7] = 0xE7;
1599 buf->Name[8] = 0xB4;
1600 buf->Name[9] = 0x23;
1601 buf->Name[10] = 0x83;
1602 buf->Name[11] = 0xDE;
1603 buf->Name[12] = 0x96;
1604 buf->Name[13] = 0x8B;
1605 buf->Name[14] = 0xCD;
1606 buf->Name[15] = 0x7C;
1607
1608 buf->nlink = cpu_to_le32(inode->i_nlink);
1609 buf->reparse_tag = cpu_to_le32(fp->volatile_id);
1610 buf->mode = cpu_to_le32(inode->i_mode);
1611 id_to_sid(from_kuid(&init_user_ns, inode->i_uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09001612 SIDNFS_USER, (struct smb_sid *)&buf->SidBuffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09001613 id_to_sid(from_kgid(&init_user_ns, inode->i_gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09001614 SIDNFS_GROUP, (struct smb_sid *)&buf->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09001615}
1616
1617/*
1618 * Find lease object(opinfo) for given lease key/fid from lease
1619 * break/file close path.
1620 */
1621/**
1622 * lookup_lease_in_table() - find a matching lease info object
1623 * @conn: connection instance
1624 * @lease_key: lease key to be searched for
1625 *
1626 * Return: opinfo if found matching opinfo, otherwise NULL
1627 */
1628struct oplock_info *lookup_lease_in_table(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001629 char *lease_key)
Namjae Jeone2f34482021-03-16 10:49:09 +09001630{
1631 struct oplock_info *opinfo = NULL, *ret_op = NULL;
1632 struct lease_table *lt;
1633 int ret;
1634
1635 read_lock(&lease_list_lock);
1636 list_for_each_entry(lt, &lease_table_list, l_entry) {
1637 if (!memcmp(lt->client_guid, conn->ClientGUID,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001638 SMB2_CLIENT_GUID_SIZE))
Namjae Jeone2f34482021-03-16 10:49:09 +09001639 goto found;
1640 }
1641
1642 read_unlock(&lease_list_lock);
1643 return NULL;
1644
1645found:
1646 rcu_read_lock();
1647 list_for_each_entry_rcu(opinfo, &lt->lease_list, lease_entry) {
1648 if (!atomic_inc_not_zero(&opinfo->refcount))
1649 continue;
1650 rcu_read_unlock();
Namjae Jeon64b39f42021-03-30 14:25:35 +09001651 if (!opinfo->op_state || opinfo->op_state == OPLOCK_CLOSING)
Namjae Jeone2f34482021-03-16 10:49:09 +09001652 goto op_next;
1653 if (!(opinfo->o_lease->state &
Namjae Jeon64b39f42021-03-30 14:25:35 +09001654 (SMB2_LEASE_HANDLE_CACHING_LE |
1655 SMB2_LEASE_WRITE_CACHING_LE)))
Namjae Jeone2f34482021-03-16 10:49:09 +09001656 goto op_next;
1657 ret = compare_guid_key(opinfo, conn->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001658 lease_key);
Namjae Jeone2f34482021-03-16 10:49:09 +09001659 if (ret) {
1660 ksmbd_debug(OPLOCK, "found opinfo\n");
1661 ret_op = opinfo;
1662 goto out;
1663 }
1664op_next:
1665 opinfo_put(opinfo);
1666 rcu_read_lock();
1667 }
1668 rcu_read_unlock();
1669
1670out:
1671 read_unlock(&lease_list_lock);
1672 return ret_op;
1673}
1674
1675int smb2_check_durable_oplock(struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09001676 struct lease_ctx_info *lctx, char *name)
Namjae Jeone2f34482021-03-16 10:49:09 +09001677{
1678 struct oplock_info *opinfo = opinfo_get(fp);
1679 int ret = 0;
1680
1681 if (opinfo && opinfo->is_lease) {
1682 if (!lctx) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001683 pr_err("open does not include lease\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001684 ret = -EBADF;
1685 goto out;
1686 }
1687 if (memcmp(opinfo->o_lease->lease_key, lctx->lease_key,
Namjae Jeon64b39f42021-03-30 14:25:35 +09001688 SMB2_LEASE_KEY_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001689 pr_err("invalid lease key\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001690 ret = -EBADF;
1691 goto out;
1692 }
1693 if (name && strcmp(fp->filename, name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001694 pr_err("invalid name reconnect %s\n", name);
Namjae Jeone2f34482021-03-16 10:49:09 +09001695 ret = -EINVAL;
1696 goto out;
1697 }
1698 }
1699out:
1700 if (opinfo)
1701 opinfo_put(opinfo);
1702 return ret;
1703}