-
Notifications
You must be signed in to change notification settings - Fork 63
/
oplock.c
2146 lines (1877 loc) · 55.5 KB
/
oplock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include <linux/moduleparam.h>
#include "glob.h"
#include "oplock.h"
#include "smb_common.h"
#ifdef CONFIG_SMB_INSECURE_SERVER
#include "smb1pdu.h"
#endif
#include "smbstatus.h"
#include "connection.h"
#include "mgmt/user_session.h"
#include "mgmt/share_config.h"
#include "mgmt/tree_connect.h"
static LIST_HEAD(lease_table_list);
static DEFINE_RWLOCK(lease_list_lock);
/**
* alloc_opinfo() - allocate a new opinfo object for oplock info
* @work: smb work
* @id: fid of open file
* @Tid: tree id of connection
*
* Return: allocated opinfo object on success, otherwise NULL
*/
static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,
u64 id, __u16 Tid)
{
struct ksmbd_conn *conn = work->conn;
struct ksmbd_session *sess = work->sess;
struct oplock_info *opinfo;
opinfo = kzalloc(sizeof(struct oplock_info), GFP_KERNEL);
if (!opinfo)
return NULL;
opinfo->sess = sess;
opinfo->conn = conn;
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
opinfo->op_state = OPLOCK_STATE_NONE;
opinfo->pending_break = 0;
opinfo->fid = id;
opinfo->Tid = Tid;
#ifdef CONFIG_SMB_INSECURE_SERVER
opinfo->is_smb2 = IS_SMB2(conn);
#endif
INIT_LIST_HEAD(&opinfo->op_entry);
INIT_LIST_HEAD(&opinfo->interim_list);
init_waitqueue_head(&opinfo->oplock_q);
init_waitqueue_head(&opinfo->oplock_brk);
atomic_set(&opinfo->refcount, 1);
atomic_set(&opinfo->breaking_cnt, 0);
atomic_inc(&opinfo->conn->refcnt);
return opinfo;
}
static void lease_add_list(struct oplock_info *opinfo)
{
struct lease_table *lb = opinfo->o_lease->l_lb;
spin_lock(&lb->lb_lock);
list_add_rcu(&opinfo->lease_entry, &lb->lease_list);
spin_unlock(&lb->lb_lock);
}
static void lease_del_list(struct oplock_info *opinfo)
{
struct lease_table *lb = opinfo->o_lease->l_lb;
if (!lb)
return;
spin_lock(&lb->lb_lock);
if (list_empty(&opinfo->lease_entry)) {
spin_unlock(&lb->lb_lock);
return;
}
list_del_init(&opinfo->lease_entry);
opinfo->o_lease->l_lb = NULL;
spin_unlock(&lb->lb_lock);
}
static void lb_add(struct lease_table *lb)
{
write_lock(&lease_list_lock);
list_add(&lb->l_entry, &lease_table_list);
write_unlock(&lease_list_lock);
}
static int alloc_lease(struct oplock_info *opinfo, struct lease_ctx_info *lctx)
{
struct lease *lease;
lease = kmalloc(sizeof(struct lease), GFP_KERNEL);
if (!lease)
return -ENOMEM;
memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
lease->state = lctx->req_state;
lease->new_state = 0;
lease->flags = lctx->flags;
lease->duration = lctx->duration;
lease->is_dir = lctx->is_dir;
memcpy(lease->parent_lease_key, lctx->parent_lease_key, SMB2_LEASE_KEY_SIZE);
lease->version = lctx->version;
lease->epoch = le16_to_cpu(lctx->epoch) + 1;
INIT_LIST_HEAD(&opinfo->lease_entry);
opinfo->o_lease = lease;
return 0;
}
static void free_lease(struct oplock_info *opinfo)
{
struct lease *lease;
lease = opinfo->o_lease;
kfree(lease);
}
static void free_opinfo(struct oplock_info *opinfo)
{
if (opinfo->is_lease)
free_lease(opinfo);
if (opinfo->conn && atomic_dec_and_test(&opinfo->conn->refcnt))
kfree(opinfo->conn);
kfree(opinfo);
}
static inline void opinfo_free_rcu(struct rcu_head *rcu_head)
{
struct oplock_info *opinfo;
opinfo = container_of(rcu_head, struct oplock_info, rcu_head);
free_opinfo(opinfo);
}
struct oplock_info *opinfo_get(struct ksmbd_file *fp)
{
struct oplock_info *opinfo;
rcu_read_lock();
opinfo = rcu_dereference(fp->f_opinfo);
if (opinfo && !atomic_inc_not_zero(&opinfo->refcount))
opinfo = NULL;
rcu_read_unlock();
return opinfo;
}
static struct oplock_info *opinfo_get_list(struct ksmbd_inode *ci)
{
struct oplock_info *opinfo;
if (list_empty(&ci->m_op_list))
return NULL;
rcu_read_lock();
opinfo = list_first_or_null_rcu(&ci->m_op_list, struct oplock_info,
op_entry);
if (opinfo) {
if (opinfo->conn == NULL ||
!atomic_inc_not_zero(&opinfo->refcount))
opinfo = NULL;
else {
if (ksmbd_conn_releasing(opinfo->conn)) {
atomic_dec(&opinfo->refcount);
opinfo = NULL;
}
}
}
rcu_read_unlock();
return opinfo;
}
void opinfo_put(struct oplock_info *opinfo)
{
if (!opinfo)
return;
if (!atomic_dec_and_test(&opinfo->refcount))
return;
call_rcu(&opinfo->rcu_head, opinfo_free_rcu);
}
static void opinfo_add(struct oplock_info *opinfo)
{
struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
down_write(&ci->m_lock);
list_add_rcu(&opinfo->op_entry, &ci->m_op_list);
up_write(&ci->m_lock);
}
static void opinfo_del(struct oplock_info *opinfo)
{
struct ksmbd_inode *ci = opinfo->o_fp->f_ci;
if (opinfo->is_lease) {
write_lock(&lease_list_lock);
lease_del_list(opinfo);
write_unlock(&lease_list_lock);
}
down_write(&ci->m_lock);
list_del_rcu(&opinfo->op_entry);
up_write(&ci->m_lock);
}
static unsigned long opinfo_count(struct ksmbd_file *fp)
{
if (ksmbd_stream_fd(fp))
return atomic_read(&fp->f_ci->sop_count);
else
return atomic_read(&fp->f_ci->op_count);
}
static void opinfo_count_inc(struct ksmbd_file *fp)
{
if (ksmbd_stream_fd(fp))
return atomic_inc(&fp->f_ci->sop_count);
else
return atomic_inc(&fp->f_ci->op_count);
}
static void opinfo_count_dec(struct ksmbd_file *fp)
{
if (ksmbd_stream_fd(fp))
return atomic_dec(&fp->f_ci->sop_count);
else
return atomic_dec(&fp->f_ci->op_count);
}
/**
* opinfo_write_to_read() - convert a write oplock to read oplock
* @opinfo: current oplock info
*
* Return: 0 on success, otherwise -EINVAL
*/
int opinfo_write_to_read(struct oplock_info *opinfo)
{
struct lease *lease = opinfo->o_lease;
#ifdef CONFIG_SMB_INSECURE_SERVER
if (opinfo->is_smb2) {
if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
pr_err("lease state(0x%x)\n", lease->state);
return -EINVAL;
}
opinfo->level = SMB2_OPLOCK_LEVEL_II;
if (opinfo->is_lease)
lease->state = lease->new_state;
} else {
if (!(opinfo->level == OPLOCK_EXCLUSIVE ||
opinfo->level == OPLOCK_BATCH)) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
return -EINVAL;
}
opinfo->level = OPLOCK_READ;
}
#else
if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
pr_err("lease state(0x%x)\n", lease->state);
return -EINVAL;
}
opinfo->level = SMB2_OPLOCK_LEVEL_II;
if (opinfo->is_lease)
lease->state = lease->new_state;
#endif
return 0;
}
/**
* opinfo_read_handle_to_read() - convert a read/handle oplock to read oplock
* @opinfo: current oplock info
*
* Return: 0 on success, otherwise -EINVAL
*/
int opinfo_read_handle_to_read(struct oplock_info *opinfo)
{
struct lease *lease = opinfo->o_lease;
lease->state = lease->new_state;
opinfo->level = SMB2_OPLOCK_LEVEL_II;
return 0;
}
/**
* opinfo_write_to_none() - convert a write oplock to none
* @opinfo: current oplock info
*
* Return: 0 on success, otherwise -EINVAL
*/
int opinfo_write_to_none(struct oplock_info *opinfo)
{
struct lease *lease = opinfo->o_lease;
#ifdef CONFIG_SMB_INSECURE_SERVER
if (opinfo->is_smb2) {
if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
pr_err("lease state(0x%x)\n", lease->state);
return -EINVAL;
}
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
if (opinfo->is_lease)
lease->state = lease->new_state;
} else {
if (!(opinfo->level == OPLOCK_EXCLUSIVE ||
opinfo->level == OPLOCK_BATCH)) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
return -EINVAL;
}
opinfo->level = OPLOCK_NONE;
}
#else
if (!(opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
pr_err("lease state(0x%x)\n", lease->state);
return -EINVAL;
}
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
if (opinfo->is_lease)
lease->state = lease->new_state;
#endif
return 0;
}
/**
* opinfo_read_to_none() - convert a write read to none
* @opinfo: current oplock info
*
* Return: 0 on success, otherwise -EINVAL
*/
int opinfo_read_to_none(struct oplock_info *opinfo)
{
struct lease *lease = opinfo->o_lease;
#ifdef CONFIG_SMB_INSECURE_SERVER
if (opinfo->is_smb2) {
if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
pr_err("lease state(0x%x)\n", lease->state);
return -EINVAL;
}
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
if (opinfo->is_lease)
lease->state = lease->new_state;
} else {
if (opinfo->level != OPLOCK_READ) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
return -EINVAL;
}
opinfo->level = OPLOCK_NONE;
}
#else
if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
pr_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
pr_err("lease state(0x%x)\n", lease->state);
return -EINVAL;
}
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
if (opinfo->is_lease)
lease->state = lease->new_state;
#endif
return 0;
}
/**
* lease_read_to_write() - upgrade lease state from read to write
* @opinfo: current lease info
*
* Return: 0 on success, otherwise -EINVAL
*/
int lease_read_to_write(struct oplock_info *opinfo)
{
struct lease *lease = opinfo->o_lease;
if (!(lease->state & SMB2_LEASE_READ_CACHING_LE)) {
ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
return -EINVAL;
}
lease->new_state = SMB2_LEASE_NONE_LE;
lease->state |= SMB2_LEASE_WRITE_CACHING_LE;
if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
else
opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
return 0;
}
/**
* lease_none_upgrade() - upgrade lease state from none
* @opinfo: current lease info
* @new_state: new lease state
*
* Return: 0 on success, otherwise -EINVAL
*/
static int lease_none_upgrade(struct oplock_info *opinfo, __le32 new_state)
{
struct lease *lease = opinfo->o_lease;
if (!(lease->state == SMB2_LEASE_NONE_LE)) {
ksmbd_debug(OPLOCK, "bad lease state(0x%x)\n", lease->state);
return -EINVAL;
}
lease->new_state = SMB2_LEASE_NONE_LE;
lease->state = new_state;
if (lease->state & SMB2_LEASE_HANDLE_CACHING_LE)
if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
else
opinfo->level = SMB2_OPLOCK_LEVEL_II;
else if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
else if (lease->state & SMB2_LEASE_READ_CACHING_LE)
opinfo->level = SMB2_OPLOCK_LEVEL_II;
return 0;
}
/**
* close_id_del_oplock() - release oplock object at file close time
* @fp: ksmbd file pointer
*/
void close_id_del_oplock(struct ksmbd_file *fp)
{
struct oplock_info *opinfo;
if (fp->reserve_lease_break)
smb_lazy_parent_lease_break_close(fp);
opinfo = opinfo_get(fp);
if (!opinfo)
return;
opinfo_del(opinfo);
rcu_assign_pointer(fp->f_opinfo, NULL);
if (opinfo->op_state == OPLOCK_ACK_WAIT) {
opinfo->op_state = OPLOCK_CLOSING;
wake_up_interruptible_all(&opinfo->oplock_q);
if (opinfo->is_lease) {
atomic_set(&opinfo->breaking_cnt, 0);
wake_up_interruptible_all(&opinfo->oplock_brk);
}
}
opinfo_count_dec(fp);
atomic_dec(&opinfo->refcount);
opinfo_put(opinfo);
}
/**
* grant_write_oplock() - grant exclusive/batch oplock or write lease
* @opinfo_new: new oplock info object
* @req_oplock: request oplock
* @lctx: lease context information
*
* Return: 0
*/
static void grant_write_oplock(struct oplock_info *opinfo_new, int req_oplock,
struct lease_ctx_info *lctx)
{
struct lease *lease = opinfo_new->o_lease;
#ifdef CONFIG_SMB_INSECURE_SERVER
if (opinfo_new->is_smb2) {
if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
else
opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
} else {
if (req_oplock == REQ_BATCHOPLOCK)
opinfo_new->level = OPLOCK_BATCH;
else
opinfo_new->level = OPLOCK_EXCLUSIVE;
}
#else
if (req_oplock == SMB2_OPLOCK_LEVEL_BATCH)
opinfo_new->level = SMB2_OPLOCK_LEVEL_BATCH;
else
opinfo_new->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
#endif
if (lctx) {
lease->state = lctx->req_state;
memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
}
}
/**
* grant_read_oplock() - grant level2 oplock or read lease
* @opinfo_new: new oplock info object
* @lctx: lease context information
*
* Return: 0
*/
static void grant_read_oplock(struct oplock_info *opinfo_new,
struct lease_ctx_info *lctx)
{
struct lease *lease = opinfo_new->o_lease;
#ifdef CONFIG_SMB_INSECURE_SERVER
if (opinfo_new->is_smb2)
opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
else
opinfo_new->level = OPLOCK_READ;
#else
opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
#endif
if (lctx) {
lease->state = SMB2_LEASE_READ_CACHING_LE;
if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING_LE)
lease->state |= SMB2_LEASE_HANDLE_CACHING_LE;
memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
}
}
/**
* grant_none_oplock() - grant none oplock or none lease
* @opinfo_new: new oplock info object
* @lctx: lease context information
*
* Return: 0
*/
static void grant_none_oplock(struct oplock_info *opinfo_new,
struct lease_ctx_info *lctx)
{
struct lease *lease = opinfo_new->o_lease;
#ifdef CONFIG_SMB_INSECURE_SERVER
if (opinfo_new->is_smb2)
opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
else
opinfo_new->level = OPLOCK_NONE;
#else
opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
#endif
if (lctx) {
lease->state = 0;
memcpy(lease->lease_key, lctx->lease_key, SMB2_LEASE_KEY_SIZE);
}
}
static inline int compare_guid_key(struct oplock_info *opinfo,
const char *guid1, const char *key1)
{
const char *guid2, *key2;
guid2 = opinfo->conn->ClientGUID;
key2 = opinfo->o_lease->lease_key;
if (!memcmp(guid1, guid2, SMB2_CLIENT_GUID_SIZE) &&
!memcmp(key1, key2, SMB2_LEASE_KEY_SIZE))
return 1;
return 0;
}
/**
* same_client_has_lease() - check whether current lease request is
* from lease owner of file
* @ci: master file pointer
* @client_guid: Client GUID
* @lctx: lease context information
*
* Return: oplock(lease) object on success, otherwise NULL
*/
static struct oplock_info *same_client_has_lease(struct ksmbd_inode *ci,
char *client_guid,
struct lease_ctx_info *lctx)
{
int ret;
struct lease *lease;
struct oplock_info *opinfo;
struct oplock_info *m_opinfo = NULL;
if (!lctx)
return NULL;
/*
* Compare lease key and client_guid to know request from same owner
* of same client
*/
down_read(&ci->m_lock);
list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
if (!opinfo->is_lease || !opinfo->conn)
continue;
lease = opinfo->o_lease;
ret = compare_guid_key(opinfo, client_guid, lctx->lease_key);
if (ret) {
m_opinfo = opinfo;
/* skip upgrading lease about breaking lease */
if (atomic_read(&opinfo->breaking_cnt))
continue;
/* upgrading lease */
if ((atomic_read(&ci->op_count) +
atomic_read(&ci->sop_count)) == 1) {
if (lease->state != SMB2_LEASE_NONE_LE &&
lease->state == (lctx->req_state & lease->state)) {
lease->epoch++;
lease->state |= lctx->req_state;
if (lctx->req_state &
SMB2_LEASE_WRITE_CACHING_LE)
lease_read_to_write(opinfo);
}
} else if ((atomic_read(&ci->op_count) +
atomic_read(&ci->sop_count)) > 1) {
if (lctx->req_state ==
(SMB2_LEASE_READ_CACHING_LE |
SMB2_LEASE_HANDLE_CACHING_LE)) {
lease->epoch++;
lease->state = lctx->req_state;
}
}
if (lctx->req_state && lease->state ==
SMB2_LEASE_NONE_LE) {
lease->epoch++;
lease_none_upgrade(opinfo, lctx->req_state);
}
}
}
up_read(&ci->m_lock);
return m_opinfo;
}
static void wait_for_break_ack(struct oplock_info *opinfo)
{
int rc = 0;
rc = wait_event_interruptible_timeout(opinfo->oplock_q,
opinfo->op_state == OPLOCK_STATE_NONE ||
opinfo->op_state == OPLOCK_CLOSING,
OPLOCK_WAIT_TIME);
/* is this a timeout ? */
if (!rc) {
if (opinfo->is_lease)
opinfo->o_lease->state = SMB2_LEASE_NONE_LE;
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
opinfo->op_state = OPLOCK_STATE_NONE;
}
}
static void wake_up_oplock_break(struct oplock_info *opinfo)
{
clear_bit_unlock(0, &opinfo->pending_break);
/* memory barrier is needed for wake_up_bit() */
smp_mb__after_atomic();
wake_up_bit(&opinfo->pending_break, 0);
}
static int oplock_break_pending(struct oplock_info *opinfo, int req_op_level)
{
while (test_and_set_bit(0, &opinfo->pending_break)) {
wait_on_bit(&opinfo->pending_break, 0, TASK_UNINTERRUPTIBLE);
/* Not immediately break to none. */
opinfo->open_trunc = 0;
if (opinfo->op_state == OPLOCK_CLOSING)
return -ENOENT;
else if (opinfo->level <= req_op_level) {
if (opinfo->is_lease == false)
return 1;
if (opinfo->o_lease->state !=
(SMB2_LEASE_HANDLE_CACHING_LE |
SMB2_LEASE_READ_CACHING_LE))
return 1;
}
}
if (opinfo->level <= req_op_level) {
if (opinfo->is_lease == false) {
wake_up_oplock_break(opinfo);
return 1;
}
if (opinfo->o_lease->state !=
(SMB2_LEASE_HANDLE_CACHING_LE |
SMB2_LEASE_READ_CACHING_LE)) {
wake_up_oplock_break(opinfo);
return 1;
}
}
return 0;
}
#ifdef CONFIG_SMB_INSECURE_SERVER
/**
* smb1_oplock_break_noti() - send smb1 oplock break cmd from conn
* to client
* @work: smb work object
*
* There are two ways this function can be called. 1- while file open we break
* from exclusive/batch lock to levelII oplock and 2- while file write/truncate
* we break from levelII oplock no oplock.
* work->request_buf contains oplock_info.
*/
static void __smb1_oplock_break_noti(struct work_struct *wk)
{
struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
struct ksmbd_conn *conn = work->conn;
struct smb_hdr *rsp_hdr;
struct smb_com_lock_req *req;
struct oplock_info *opinfo = work->request_buf;
if (allocate_interim_rsp_buf(work)) {
pr_err("smb_allocate_rsp_buf failed! ");
ksmbd_free_work_struct(work);
return;
}
/* Init response header */
rsp_hdr = work->response_buf;
/* wct is 8 for locking andx(18) */
memset(rsp_hdr, 0, sizeof(struct smb_hdr) + 18);
rsp_hdr->smb_buf_length =
cpu_to_be32(conn->vals->header_size - 4 + 18);
rsp_hdr->Protocol[0] = 0xFF;
rsp_hdr->Protocol[1] = 'S';
rsp_hdr->Protocol[2] = 'M';
rsp_hdr->Protocol[3] = 'B';
rsp_hdr->Command = SMB_COM_LOCKING_ANDX;
/* we know unicode, long file name and use nt error codes */
rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_KNOWS_LONG_NAMES |
SMBFLG2_ERR_STATUS;
rsp_hdr->Uid = cpu_to_le16(work->sess->id);
rsp_hdr->Pid = cpu_to_le16(0xFFFF);
rsp_hdr->Mid = cpu_to_le16(0xFFFF);
rsp_hdr->Tid = cpu_to_le16(opinfo->Tid);
rsp_hdr->WordCount = 8;
/* Init locking request */
req = work->response_buf;
req->AndXCommand = 0xFF;
req->AndXReserved = 0;
req->AndXOffset = 0;
req->Fid = opinfo->fid;
req->LockType = LOCKING_ANDX_OPLOCK_RELEASE;
if (!opinfo->open_trunc &&
(opinfo->level == OPLOCK_BATCH ||
opinfo->level == OPLOCK_EXCLUSIVE))
req->OplockLevel = 1;
else
req->OplockLevel = 0;
req->Timeout = 0;
req->NumberOfUnlocks = 0;
req->ByteCount = 0;
ksmbd_debug(OPLOCK, "sending oplock break for fid %d lock level = %d\n",
req->Fid, req->OplockLevel);
ksmbd_conn_write(work);
ksmbd_free_work_struct(work);
}
/**
* smb1_oplock_break() - send smb1 exclusive/batch to level2 oplock
* break command from server to client
* @opinfo: oplock info object
* @ack_required if requiring ack
*
* Return: 0 on success, otherwise error
*/
static int smb1_oplock_break_noti(struct oplock_info *opinfo)
{
struct ksmbd_conn *conn = opinfo->conn;
struct ksmbd_work *work = ksmbd_alloc_work_struct();
if (!work)
return -ENOMEM;
work->request_buf = (char *)opinfo;
work->conn = conn;
if (opinfo->op_state == OPLOCK_ACK_WAIT) {
INIT_WORK(&work->work, __smb1_oplock_break_noti);
ksmbd_queue_work(work);
wait_for_break_ack(opinfo);
} else {
__smb1_oplock_break_noti(&work->work);
if (opinfo->level == OPLOCK_READ)
opinfo->level = OPLOCK_NONE;
}
return 0;
}
#endif
/**
* __smb2_oplock_break_noti() - send smb2 oplock break cmd from conn
* to client
* @wk: smb work object
*
* There are two ways this function can be called. 1- while file open we break
* from exclusive/batch lock to levelII oplock and 2- while file write/truncate
* we break from levelII oplock no oplock.
* work->request_buf contains oplock_info.
*/
static void __smb2_oplock_break_noti(struct work_struct *wk)
{
struct smb2_oplock_break *rsp = NULL;
struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
struct oplock_break_info *br_info = work->request_buf;
struct smb2_hdr *rsp_hdr;
struct ksmbd_file *fp;
fp = ksmbd_lookup_global_fd(br_info->fid);
if (!fp)
goto out;
if (allocate_interim_rsp_buf(work)) {
pr_err("smb2_allocate_rsp_buf failed! ");
ksmbd_fd_put(work, fp);
goto out;
}
rsp_hdr = smb2_get_msg(work->response_buf);
memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
rsp_hdr->CreditRequest = cpu_to_le16(0);
rsp_hdr->Command = SMB2_OPLOCK_BREAK;
rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
rsp_hdr->NextCommand = 0;
rsp_hdr->MessageId = cpu_to_le64(-1);
rsp_hdr->Id.SyncId.ProcessId = 0;
rsp_hdr->Id.SyncId.TreeId = 0;
rsp_hdr->SessionId = 0;
memset(rsp_hdr->Signature, 0, 16);
rsp = smb2_get_msg(work->response_buf);
rsp->StructureSize = cpu_to_le16(24);
if (!br_info->open_trunc &&
(br_info->level == SMB2_OPLOCK_LEVEL_BATCH ||
br_info->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
rsp->OplockLevel = SMB2_OPLOCK_LEVEL_II;
else
rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
rsp->Reserved = 0;
rsp->Reserved2 = 0;
rsp->PersistentFid = fp->persistent_id;
rsp->VolatileFid = fp->volatile_id;
ksmbd_fd_put(work, fp);
if (ksmbd_iov_pin_rsp(work, (void *)rsp,
sizeof(struct smb2_oplock_break)))
goto out;
ksmbd_debug(OPLOCK,
"sending oplock break v_id %llu p_id = %llu lock level = %d\n",
rsp->VolatileFid, rsp->PersistentFid, rsp->OplockLevel);
ksmbd_conn_write(work);
out:
ksmbd_free_work_struct(work);
}
/**
* smb2_oplock_break_noti() - send smb2 exclusive/batch to level2 oplock
* break command from server to client
* @opinfo: oplock info object
*
* Return: 0 on success, otherwise error
*/
static int smb2_oplock_break_noti(struct oplock_info *opinfo)
{
struct ksmbd_conn *conn = opinfo->conn;
struct oplock_break_info *br_info;
int ret = 0;
struct ksmbd_work *work = ksmbd_alloc_work_struct();
if (!work)
return -ENOMEM;
br_info = kmalloc(sizeof(struct oplock_break_info), GFP_KERNEL);
if (!br_info) {
ksmbd_free_work_struct(work);
return -ENOMEM;
}
br_info->level = opinfo->level;
br_info->fid = opinfo->fid;
br_info->open_trunc = opinfo->open_trunc;
work->request_buf = (char *)br_info;
work->conn = conn;
work->sess = opinfo->sess;
if (opinfo->op_state == OPLOCK_ACK_WAIT) {
INIT_WORK(&work->work, __smb2_oplock_break_noti);
ksmbd_queue_work(work);
wait_for_break_ack(opinfo);
} else {
__smb2_oplock_break_noti(&work->work);
if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
}
return ret;
}
/**
* __smb2_lease_break_noti() - send lease break command from server
* to client
* @wk: smb work object
*/
static void __smb2_lease_break_noti(struct work_struct *wk)
{
struct smb2_lease_break *rsp = NULL;
struct ksmbd_work *work = container_of(wk, struct ksmbd_work, work);
struct lease_break_info *br_info = work->request_buf;
struct smb2_hdr *rsp_hdr;
if (allocate_interim_rsp_buf(work)) {
ksmbd_debug(OPLOCK, "smb2_allocate_rsp_buf failed! ");
goto out;
}
rsp_hdr = smb2_get_msg(work->response_buf);
memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
rsp_hdr->CreditRequest = cpu_to_le16(0);
rsp_hdr->Command = SMB2_OPLOCK_BREAK;
rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
rsp_hdr->NextCommand = 0;
rsp_hdr->MessageId = cpu_to_le64(-1);
rsp_hdr->Id.SyncId.ProcessId = 0;
rsp_hdr->Id.SyncId.TreeId = 0;
rsp_hdr->SessionId = 0;
memset(rsp_hdr->Signature, 0, 16);
rsp = smb2_get_msg(work->response_buf);
rsp->StructureSize = cpu_to_le16(44);
rsp->Epoch = br_info->epoch;
rsp->Flags = 0;
if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING_LE |
SMB2_LEASE_HANDLE_CACHING_LE))
rsp->Flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
memcpy(rsp->LeaseKey, br_info->lease_key, SMB2_LEASE_KEY_SIZE);
rsp->CurrentLeaseState = br_info->curr_state;
rsp->NewLeaseState = br_info->new_state;
rsp->BreakReason = 0;
rsp->AccessMaskHint = 0;
rsp->ShareMaskHint = 0;
if (ksmbd_iov_pin_rsp(work, (void *)rsp,
sizeof(struct smb2_lease_break)))
goto out;
ksmbd_conn_write(work);
out:
ksmbd_free_work_struct(work);
}
/**
* smb2_lease_break_noti() - break lease when a new client request
* write lease