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