forked from namjaejeon/ksmbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoplock.c
1746 lines (1526 loc) · 44.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 <[email protected]>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include "glob.h"
#include "oplock.h"
#include "smb_common.h"
#include "buffer_pool.h"
#include "transport_tcp.h"
#include "mgmt/user_session.h"
bool oplocks_enable;
bool lease_enable;
bool durable_enable;
LIST_HEAD(lease_table_list);
static DEFINE_RWLOCK(lease_list_lock);
module_param(oplocks_enable, bool, 0644);
MODULE_PARM_DESC(oplocks_enable, "Enable or disable oplocks. Default: y/Y/1");
module_param(lease_enable, bool, 0644);
MODULE_PARM_DESC(lease_enable, "Enable or disable lease. Default: y/Y/1");
/**
* get_new_opinfo() - allocate a new opinfo object for oplock info
* @conn: TCP server instance of connection
* @id: fid of open file
* @Tid: tree id of connection
* @lctx: lease context information
*
* Return: allocated opinfo object on success, otherwise NULL
*/
static struct oplock_info *alloc_opinfo(struct cifsd_work *work,
uint64_t id, __u16 Tid)
{
struct cifsd_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 = sess->conn;
opinfo->level = OPLOCK_NONE;
opinfo->op_state = OPLOCK_STATE_NONE;
opinfo->fid = id;
opinfo->Tid = Tid;
opinfo->is_smb2 = IS_SMB2(sess->conn);
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);
return opinfo;
}
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);
}
void lease_del_list(struct oplock_info *opinfo)
{
struct lease_table *lb = opinfo->o_lease->l_lb;
spin_lock(&lb->lb_lock);
list_del_rcu(&opinfo->lease_entry);
spin_unlock(&lb->lb_lock);
}
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;
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);
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 cifsd_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;
}
struct oplock_info *opinfo_get_list(struct cifsd_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 && !atomic_inc_not_zero(&opinfo->refcount))
opinfo = NULL;
rcu_read_unlock();
return opinfo;
}
void opinfo_put(struct oplock_info *opinfo)
{
if (!atomic_dec_and_test(&opinfo->refcount))
return;
call_rcu(&opinfo->rcu_head, opinfo_free_rcu);
}
void opinfo_add(struct oplock_info *opinfo)
{
struct cifsd_inode *ci = opinfo->o_fp->f_ci;
spin_lock(&ci->m_lock);
list_add_rcu(&opinfo->op_entry, &ci->m_op_list);
spin_unlock(&ci->m_lock);
}
void opinfo_del(struct oplock_info *opinfo)
{
struct cifsd_inode *ci = opinfo->o_fp->f_ci;
if (opinfo->is_lease)
lease_del_list(opinfo);
spin_lock(&ci->m_lock);
list_del_rcu(&opinfo->op_entry);
spin_unlock(&ci->m_lock);
}
/**
* 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;
if (opinfo->is_smb2) {
if (!((opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) ||
(opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))) {
cifsd_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
cifsd_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))) {
cifsd_err("bad oplock(0x%x)\n", opinfo->level);
return -EINVAL;
}
opinfo->level = OPLOCK_READ;
}
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;
if (opinfo->is_smb2) {
if (!((opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) ||
(opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))) {
cifsd_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
cifsd_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))) {
cifsd_err("bad oplock(0x%x)\n", opinfo->level);
return -EINVAL;
}
opinfo->level = OPLOCK_NONE;
}
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;
if (opinfo->is_smb2) {
if (opinfo->level != SMB2_OPLOCK_LEVEL_II) {
cifsd_err("bad oplock(0x%x)\n", opinfo->level);
if (opinfo->is_lease)
cifsd_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) {
cifsd_err("bad oplock(0x%x)\n", opinfo->level);
return -EINVAL;
}
opinfo->level = OPLOCK_NONE;
}
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)) {
cifsd_debug("bad lease state(0x%x)\n",
lease->state);
return -EINVAL;
}
lease->new_state = SMB2_LEASE_NONE;
lease->state |= SMB2_LEASE_WRITE_CACHING;
if (lease->state & SMB2_LEASE_HANDLE_CACHING)
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
*/
int lease_none_upgrade(struct oplock_info *opinfo,
__le32 new_state)
{
struct lease *lease = opinfo->o_lease;
if (!(lease->state == SMB2_LEASE_NONE)) {
cifsd_debug("bad lease state(0x%x)\n",
lease->state);
return -EINVAL;
}
lease->new_state = SMB2_LEASE_NONE;
lease->state = new_state;
if (lease->state & SMB2_LEASE_HANDLE_CACHING)
if (lease->state & SMB2_LEASE_WRITE_CACHING)
opinfo->level = SMB2_OPLOCK_LEVEL_BATCH;
else
opinfo->level = SMB2_OPLOCK_LEVEL_II;
else if (lease->state & SMB2_LEASE_WRITE_CACHING)
opinfo->level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
else if (lease->state & SMB2_LEASE_READ_CACHING)
opinfo->level = SMB2_OPLOCK_LEVEL_II;
return 0;
}
/**
* close_id_del_oplock() - release oplock object at file close time
* @conn: TCP server instance of connection
* @fp: cifsd file pointer
* @id: fid of open file
*/
void close_id_del_oplock(struct cifsd_file *fp)
{
struct oplock_info *opinfo;
if (!oplocks_enable || S_ISDIR(file_inode(fp->filp)->i_mode))
return;
opinfo = fp->f_opinfo;
if (!opinfo)
return;
opinfo_del(opinfo);
fp->f_opinfo = NULL;
if (opinfo->op_state == OPLOCK_ACK_WAIT) {
opinfo->op_state = OPLOCK_CLOSING;
wake_up_interruptible(&opinfo->oplock_q);
if (opinfo->is_lease) {
atomic_set(&opinfo->breaking_cnt, 0);
wake_up_interruptible(&opinfo->oplock_brk);
}
}
atomic_dec(&fp->f_ci->op_count);
opinfo_put(opinfo);
}
/**
* smb2_send_lease_break_notification() - send lease break command from server
* to client
* @work: smb work object
*/
static void smb2_send_lease_break_notification(struct work_struct *wk)
{
struct smb2_lease_break *rsp = NULL;
struct cifsd_work *work = container_of(wk, struct cifsd_work, work);
struct lease_break_info *br_info =
(struct lease_break_info *)REQUEST_BUF(work);
struct cifsd_tcp_conn *conn = work->conn;
struct smb2_hdr *rsp_hdr;
cifsd_tcp_conn_lock(conn);
if (conn->ops->allocate_rsp_buf(work)) {
cifsd_debug("smb2_allocate_rsp_buf failed! ");
cifsd_tcp_conn_unlock(conn);
cifsd_free_work_struct(work);
return;
}
rsp_hdr = (struct smb2_hdr *)RESPONSE_BUF(work);
memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
rsp_hdr->CreditRequest = cpu_to_le16(0);
rsp_hdr->Command = cpu_to_le16(0x12);
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 = (struct smb2_lease_break *)RESPONSE_BUF(work);
rsp->StructureSize = cpu_to_le16(44);
rsp->Reserved = 0;
rsp->Flags = 0;
if (br_info->curr_state & (SMB2_LEASE_WRITE_CACHING |
SMB2_LEASE_HANDLE_CACHING))
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;
inc_rfc1001_len(rsp, 44);
cifsd_tcp_write(work);
cifsd_tcp_conn_unlock(conn);
cifsd_free_work_struct(work);
}
/**
* smb1_oplock_break_notification() - 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_notification(struct oplock_info *opinfo,
int ack_required)
{
struct cifsd_tcp_conn *conn = opinfo->conn;
int ret = 0;
struct cifsd_work *work = cifsd_alloc_work_struct();
if (!work)
return -ENOMEM;
work->request_buf = (char *)opinfo;
work->conn = conn;
if (ack_required) {
int rc;
INIT_WORK(&work->work, smb1_send_oplock_break_notification);
schedule_work(&work->work);
/*
* TODO: change to wait_event_interruptible_timeout once oplock
* break notification timeout is decided. In case of oplock
* break from levelII to none, we don't need to wait for client
* response.
*/
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) {
opinfo->level = OPLOCK_NONE;
opinfo->op_state = OPLOCK_STATE_NONE;
}
} else {
smb1_send_oplock_break_notification(&work->work);
if (opinfo->level == OPLOCK_READ)
opinfo->level = OPLOCK_NONE;
}
return ret;
}
/**
* smb2_oplock_break_notification() - send smb2 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 smb2_oplock_break_notification(struct oplock_info *opinfo,
int ack_required)
{
struct cifsd_tcp_conn *conn = opinfo->conn;
struct oplock_break_info *br_info;
int ret = 0;
struct cifsd_work *work = cifsd_alloc_work_struct();
if (!work)
return -ENOMEM;
br_info = kmalloc(sizeof(struct oplock_break_info), GFP_KERNEL);
if (!br_info) {
cifsd_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 (ack_required) {
int rc;
INIT_WORK(&work->work, smb2_send_oplock_break_notification);
schedule_work(&work->work);
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) {
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
opinfo->op_state = OPLOCK_STATE_NONE;
}
} else {
smb2_send_oplock_break_notification(&work->work);
if (opinfo->level == SMB2_OPLOCK_LEVEL_II)
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
}
return ret;
}
/**
* 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;
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;
}
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;
if (opinfo_new->is_smb2) {
opinfo_new->level = SMB2_OPLOCK_LEVEL_II;
} else
opinfo_new->level = OPLOCK_READ;
if (lctx) {
lease->state = SMB2_LEASE_READ_CACHING;
if (lctx->req_state & SMB2_LEASE_HANDLE_CACHING)
lease->state |= SMB2_LEASE_HANDLE_CACHING;
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;
if (opinfo_new->is_smb2) {
opinfo_new->level = SMB2_OPLOCK_LEVEL_NONE;
} else
opinfo_new->level = OPLOCK_NONE;
if (lctx) {
lease->state = 0;
memcpy(lease->lease_key, lctx->lease_key,
SMB2_LEASE_KEY_SIZE);
}
}
/**
* find_opinfo() - find lease object for given client guid and lease key
* @head: oplock list(read,write or none) head
* @guid1: client guid of matching lease owner
* @key1: lease key of matching lease owner
*
* Return: oplock(lease) object on success, otherwise NULL
*/
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
*/
struct oplock_info *same_client_has_lease(struct cifsd_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
*/
spin_lock(&ci->m_lock);
list_for_each_entry(opinfo, &ci->m_op_list, op_entry) {
if (!opinfo->is_lease)
continue;
spin_unlock(&ci->m_lock);
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)) {
spin_lock(&ci->m_lock);
continue;
}
/* upgrading lease */
if (atomic_read(&ci->op_count) == 1) {
if (lease->state ==
(lctx->req_state & lease->state)) {
lease->state |= lctx->req_state;
if (lctx->req_state &
SMB2_LEASE_WRITE_CACHING)
lease_read_to_write(opinfo);
}
} else if (atomic_read(&ci->op_count) > 1) {
if (lctx->req_state == 0x3)
lease->state = lctx->req_state;
}
if (lctx->req_state && lease->state == SMB2_LEASE_NONE)
lease_none_upgrade(opinfo, lctx->req_state);
}
spin_lock(&ci->m_lock);
}
spin_unlock(&ci->m_lock);
return m_opinfo;
}
void wait_for_lease_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;
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
opinfo->op_state = OPLOCK_STATE_NONE;
}
}
/**
* smb2_break_lease_notification() - break lease when a new client request
* write lease
* @opinfo: conains lease state information
* @ack_required: if requring ack
*
* Return: 0 on success, otherwise error
*/
int smb2_break_lease_notification(struct oplock_info *opinfo, int ack_required)
{
struct cifsd_tcp_conn *conn = opinfo->conn;
struct list_head *tmp, *t;
struct cifsd_work *work;
struct lease_break_info *br_info;
struct lease *lease = opinfo->o_lease;
work = cifsd_alloc_work_struct();
if (!work)
return -ENOMEM;
br_info = kmalloc(sizeof(struct lease_break_info), GFP_KERNEL);
if (!br_info) {
cifsd_free_work_struct(work);
return -ENOMEM;
}
br_info->curr_state = lease->state;
br_info->new_state = lease->new_state;
memcpy(br_info->lease_key, lease->lease_key, SMB2_LEASE_KEY_SIZE);
work->request_buf = (char *)br_info;
work->conn = conn;
work->sess = opinfo->sess;
if (ack_required) {
list_for_each_safe(tmp, t, &opinfo->interim_list) {
struct cifsd_work *in_work;
in_work = list_entry(tmp, struct cifsd_work,
interim_entry);
smb2_send_interim_resp(in_work, NT_STATUS_PENDING);
list_del(&in_work->interim_entry);
}
INIT_WORK(&work->work, smb2_send_lease_break_notification);
schedule_work(&work->work);
wait_for_lease_break_ack(opinfo);
if (!atomic_read(&opinfo->breaking_cnt))
wake_up_interruptible(&opinfo->oplock_brk);
if (atomic_read(&opinfo->breaking_cnt)) {
int ret = 0;
ret = wait_event_interruptible_timeout(opinfo->oplock_brk,
atomic_read(&opinfo->breaking_cnt) == 0,
OPLOCK_WAIT_TIME);
if (!ret)
atomic_set(&opinfo->breaking_cnt, 0);
}
} else {
smb2_send_lease_break_notification(&work->work);
if (opinfo->o_lease->state == SMB2_LEASE_READ_CACHING) {
opinfo->level = SMB2_OPLOCK_LEVEL_NONE;
opinfo->o_lease->state = SMB2_LEASE_NONE;
}
}
return 0;
}
static int smb_send_oplock_break_notification(struct oplock_info *brk_opinfo)
{
int err = 0;
int ack_required = 0;
/* Need to break exclusive/batch oplock, write lease or overwrite_if */
cifsd_debug("request to send oplock(level : 0x%x) break notification\n",
brk_opinfo->level);
if (brk_opinfo->is_lease) {
struct lease *lease = brk_opinfo->o_lease;
if (!(lease->state == SMB2_LEASE_READ_CACHING))
atomic_inc(&brk_opinfo->breaking_cnt);
if (brk_opinfo->op_state == OPLOCK_ACK_WAIT) {
/* wait till getting break ack */
wait_for_lease_break_ack(brk_opinfo);
/* Not immediately break to none. */
brk_opinfo->open_trunc = 0;
}
if (brk_opinfo->open_trunc) {
/*
* Create overwrite break trigger the lease break to
* none.
*/
lease->new_state = SMB2_LEASE_NONE;
} else {
if (lease->state & SMB2_LEASE_WRITE_CACHING) {
if (lease->state & SMB2_LEASE_HANDLE_CACHING)
lease->new_state =
SMB2_LEASE_READ_CACHING |
SMB2_LEASE_HANDLE_CACHING;
else
lease->new_state =
SMB2_LEASE_READ_CACHING;
} else {
if (lease->state & SMB2_LEASE_HANDLE_CACHING)
lease->new_state =
SMB2_LEASE_READ_CACHING;
else
lease->new_state = SMB2_LEASE_NONE;
}
}
if (lease->state & (SMB2_LEASE_WRITE_CACHING |
SMB2_LEASE_HANDLE_CACHING))
brk_opinfo->op_state = OPLOCK_ACK_WAIT;
} else if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
brk_opinfo->op_state = OPLOCK_ACK_WAIT;
if (brk_opinfo->is_smb2) {
if (brk_opinfo->is_lease) {
struct lease *lease = brk_opinfo->o_lease;
if ((brk_opinfo->open_trunc == 1 &&
!(lease->state & SMB2_LEASE_WRITE_CACHING)) ||
lease->state == SMB2_LEASE_READ_CACHING)
ack_required = 0;
else
ack_required = 1;
err = smb2_break_lease_notification(brk_opinfo,
ack_required);
} else {
/* break oplock */
if (brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH ||
brk_opinfo->level ==
SMB2_OPLOCK_LEVEL_EXCLUSIVE)
ack_required = 1;
err = smb2_oplock_break_notification(brk_opinfo,
ack_required);
}
} else {
if ((brk_opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) ||
(brk_opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
ack_required = 1;
err = smb1_oplock_break_notification(brk_opinfo,
ack_required);
}
cifsd_debug("oplock granted = %d\n", brk_opinfo->level);
if (brk_opinfo->op_state == OPLOCK_CLOSING) {
brk_opinfo->op_state = OPLOCK_STATE_NONE;
err = -ENOENT;
}
return err;
}
void destroy_lease_table(struct cifsd_tcp_conn *conn)
{
struct lease_table *lb, *lbtmp;
struct oplock_info *opinfo;
write_lock(&lease_list_lock);
if (list_empty(&lease_table_list)) {
write_unlock(&lease_list_lock);
return;
}
list_for_each_entry_safe(lb, lbtmp, &lease_table_list, l_entry) {
if (conn && memcmp(lb->client_guid, conn->ClientGUID,
SMB2_CLIENT_GUID_SIZE))
continue;
again:
rcu_read_lock();
list_for_each_entry_rcu(opinfo, &lb->lease_list,
lease_entry) {
rcu_read_unlock();
lease_del_list(opinfo);
goto again;
}
rcu_read_unlock();
list_del(&lb->l_entry);
kfree(lb);
}
write_unlock(&lease_list_lock);
}
int find_same_lease_key(struct cifsd_session *sess, struct cifsd_inode *ci,
struct lease_ctx_info *lctx)
{
struct oplock_info *opinfo;
int err = 0;
struct lease_table *lb;
if (!lctx)
return err;
read_lock(&lease_list_lock);
if (list_empty(&lease_table_list)) {
read_unlock(&lease_list_lock);
return 0;
}
list_for_each_entry(lb, &lease_table_list, l_entry) {
if (!memcmp(lb->client_guid, sess->conn->ClientGUID,
SMB2_CLIENT_GUID_SIZE))
goto found;
}
read_unlock(&lease_list_lock);
return 0;
found:
rcu_read_lock();
list_for_each_entry_rcu(opinfo, &lb->lease_list,
lease_entry) {
if (!atomic_inc_not_zero(&opinfo->refcount))
continue;
rcu_read_unlock();
if (opinfo->o_fp->f_ci == ci)
goto op_next;
err = compare_guid_key(opinfo,
sess->conn->ClientGUID,
lctx->lease_key);
if (err) {
err = -EINVAL;
cifsd_debug("found same lease key is already used in other files\n");
opinfo_put(opinfo);
goto out;
}
op_next:
opinfo_put(opinfo);
rcu_read_lock();
}
rcu_read_unlock();
out:
read_unlock(&lease_list_lock);
return err;
}
static void copy_lease(struct oplock_info *op1, struct oplock_info *op2)
{
struct lease *lease1 = op1->o_lease;
struct lease *lease2 = op2->o_lease;
op2->level = op1->level;
lease2->state = lease1->state;
memcpy(lease2->lease_key, lease1->lease_key,
SMB2_LEASE_KEY_SIZE);