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#include <linux/capability.h>
118#include <linux/file.h>
119#include <linux/fdtable.h>
120#include <linux/fs.h>
121#include <linux/init.h>
122#include <linux/module.h>
123#include <linux/security.h>
124#include <linux/slab.h>
125#include <linux/syscalls.h>
126#include <linux/time.h>
127#include <linux/rcupdate.h>
128#include <linux/pid_namespace.h>
129#include <linux/hashtable.h>
130#include <linux/percpu.h>
131#include <linux/lglock.h>
132
133#define CREATE_TRACE_POINTS
134#include <trace/events/filelock.h>
135
136#include <asm/uaccess.h>
137
138#define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
139#define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
140#define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG))
141#define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
142
143static bool lease_breaking(struct file_lock *fl)
144{
145 return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
146}
147
148static int target_leasetype(struct file_lock *fl)
149{
150 if (fl->fl_flags & FL_UNLOCK_PENDING)
151 return F_UNLCK;
152 if (fl->fl_flags & FL_DOWNGRADE_PENDING)
153 return F_RDLCK;
154 return fl->fl_type;
155}
156
157int leases_enable = 1;
158int lease_break_time = 45;
159
160#define for_each_lock(inode, lockp) \
161 for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
162
163
164
165
166
167
168
169DEFINE_STATIC_LGLOCK(file_lock_lglock);
170static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
171
172
173
174
175
176
177
178
179
180
181
182
183#define BLOCKED_HASH_BITS 7
184static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203static DEFINE_SPINLOCK(blocked_lock_lock);
204
205static struct kmem_cache *filelock_cache __read_mostly;
206
207static void locks_init_lock_heads(struct file_lock *fl)
208{
209 INIT_HLIST_NODE(&fl->fl_link);
210 INIT_LIST_HEAD(&fl->fl_block);
211 init_waitqueue_head(&fl->fl_wait);
212}
213
214
215struct file_lock *locks_alloc_lock(void)
216{
217 struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
218
219 if (fl)
220 locks_init_lock_heads(fl);
221
222 return fl;
223}
224EXPORT_SYMBOL_GPL(locks_alloc_lock);
225
226void locks_release_private(struct file_lock *fl)
227{
228 if (fl->fl_ops) {
229 if (fl->fl_ops->fl_release_private)
230 fl->fl_ops->fl_release_private(fl);
231 fl->fl_ops = NULL;
232 }
233 fl->fl_lmops = NULL;
234
235}
236EXPORT_SYMBOL_GPL(locks_release_private);
237
238
239void locks_free_lock(struct file_lock *fl)
240{
241 BUG_ON(waitqueue_active(&fl->fl_wait));
242 BUG_ON(!list_empty(&fl->fl_block));
243 BUG_ON(!hlist_unhashed(&fl->fl_link));
244
245 locks_release_private(fl);
246 kmem_cache_free(filelock_cache, fl);
247}
248EXPORT_SYMBOL(locks_free_lock);
249
250void locks_init_lock(struct file_lock *fl)
251{
252 memset(fl, 0, sizeof(struct file_lock));
253 locks_init_lock_heads(fl);
254}
255
256EXPORT_SYMBOL(locks_init_lock);
257
258static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
259{
260 if (fl->fl_ops) {
261 if (fl->fl_ops->fl_copy_lock)
262 fl->fl_ops->fl_copy_lock(new, fl);
263 new->fl_ops = fl->fl_ops;
264 }
265 if (fl->fl_lmops)
266 new->fl_lmops = fl->fl_lmops;
267}
268
269
270
271
272void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
273{
274 new->fl_owner = fl->fl_owner;
275 new->fl_pid = fl->fl_pid;
276 new->fl_file = NULL;
277 new->fl_flags = fl->fl_flags;
278 new->fl_type = fl->fl_type;
279 new->fl_start = fl->fl_start;
280 new->fl_end = fl->fl_end;
281 new->fl_ops = NULL;
282 new->fl_lmops = NULL;
283}
284EXPORT_SYMBOL(__locks_copy_lock);
285
286void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
287{
288 locks_release_private(new);
289
290 __locks_copy_lock(new, fl);
291 new->fl_file = fl->fl_file;
292 new->fl_ops = fl->fl_ops;
293 new->fl_lmops = fl->fl_lmops;
294
295 locks_copy_private(new, fl);
296}
297
298EXPORT_SYMBOL(locks_copy_lock);
299
300static inline int flock_translate_cmd(int cmd) {
301 if (cmd & LOCK_MAND)
302 return cmd & (LOCK_MAND | LOCK_RW);
303 switch (cmd) {
304 case LOCK_SH:
305 return F_RDLCK;
306 case LOCK_EX:
307 return F_WRLCK;
308 case LOCK_UN:
309 return F_UNLCK;
310 }
311 return -EINVAL;
312}
313
314
315static int flock_make_lock(struct file *filp, struct file_lock **lock,
316 unsigned int cmd)
317{
318 struct file_lock *fl;
319 int type = flock_translate_cmd(cmd);
320 if (type < 0)
321 return type;
322
323 fl = locks_alloc_lock();
324 if (fl == NULL)
325 return -ENOMEM;
326
327 fl->fl_file = filp;
328 fl->fl_owner = (fl_owner_t)filp;
329 fl->fl_pid = current->tgid;
330 fl->fl_flags = FL_FLOCK;
331 fl->fl_type = type;
332 fl->fl_end = OFFSET_MAX;
333
334 *lock = fl;
335 return 0;
336}
337
338static int assign_type(struct file_lock *fl, long type)
339{
340 switch (type) {
341 case F_RDLCK:
342 case F_WRLCK:
343 case F_UNLCK:
344 fl->fl_type = type;
345 break;
346 default:
347 return -EINVAL;
348 }
349 return 0;
350}
351
352static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
353 struct flock64 *l)
354{
355 switch (l->l_whence) {
356 case SEEK_SET:
357 fl->fl_start = 0;
358 break;
359 case SEEK_CUR:
360 fl->fl_start = filp->f_pos;
361 break;
362 case SEEK_END:
363 fl->fl_start = i_size_read(file_inode(filp));
364 break;
365 default:
366 return -EINVAL;
367 }
368 if (l->l_start > OFFSET_MAX - fl->fl_start)
369 return -EOVERFLOW;
370 fl->fl_start += l->l_start;
371 if (fl->fl_start < 0)
372 return -EINVAL;
373
374
375
376 if (l->l_len > 0) {
377 if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
378 return -EOVERFLOW;
379 fl->fl_end = fl->fl_start + l->l_len - 1;
380
381 } else if (l->l_len < 0) {
382 if (fl->fl_start + l->l_len < 0)
383 return -EINVAL;
384 fl->fl_end = fl->fl_start - 1;
385 fl->fl_start += l->l_len;
386 } else
387 fl->fl_end = OFFSET_MAX;
388
389 fl->fl_owner = current->files;
390 fl->fl_pid = current->tgid;
391 fl->fl_file = filp;
392 fl->fl_flags = FL_POSIX;
393 fl->fl_ops = NULL;
394 fl->fl_lmops = NULL;
395
396 return assign_type(fl, l->l_type);
397}
398
399
400
401
402static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
403 struct flock *l)
404{
405 struct flock64 ll = {
406 .l_type = l->l_type,
407 .l_whence = l->l_whence,
408 .l_start = l->l_start,
409 .l_len = l->l_len,
410 };
411
412 return flock64_to_posix_lock(filp, fl, &ll);
413}
414
415
416static void lease_break_callback(struct file_lock *fl)
417{
418 kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
419}
420
421static const struct lock_manager_operations lease_manager_ops = {
422 .lm_break = lease_break_callback,
423 .lm_change = lease_modify,
424};
425
426
427
428
429static int lease_init(struct file *filp, long type, struct file_lock *fl)
430 {
431 if (assign_type(fl, type) != 0)
432 return -EINVAL;
433
434 fl->fl_owner = (fl_owner_t)current->files;
435 fl->fl_pid = current->tgid;
436
437 fl->fl_file = filp;
438 fl->fl_flags = FL_LEASE;
439 fl->fl_start = 0;
440 fl->fl_end = OFFSET_MAX;
441 fl->fl_ops = NULL;
442 fl->fl_lmops = &lease_manager_ops;
443 return 0;
444}
445
446
447static struct file_lock *lease_alloc(struct file *filp, long type)
448{
449 struct file_lock *fl = locks_alloc_lock();
450 int error = -ENOMEM;
451
452 if (fl == NULL)
453 return ERR_PTR(error);
454
455 error = lease_init(filp, type, fl);
456 if (error) {
457 locks_free_lock(fl);
458 return ERR_PTR(error);
459 }
460 return fl;
461}
462
463
464
465static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
466{
467 return ((fl1->fl_end >= fl2->fl_start) &&
468 (fl2->fl_end >= fl1->fl_start));
469}
470
471
472
473
474static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
475{
476 if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
477 return fl2->fl_lmops == fl1->fl_lmops &&
478 fl1->fl_lmops->lm_compare_owner(fl1, fl2);
479 return fl1->fl_owner == fl2->fl_owner;
480}
481
482
483static void locks_insert_global_locks(struct file_lock *fl)
484{
485 lg_local_lock(&file_lock_lglock);
486 fl->fl_link_cpu = smp_processor_id();
487 hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
488 lg_local_unlock(&file_lock_lglock);
489}
490
491
492static void locks_delete_global_locks(struct file_lock *fl)
493{
494
495
496
497
498
499 if (hlist_unhashed(&fl->fl_link))
500 return;
501 lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
502 hlist_del_init(&fl->fl_link);
503 lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
504}
505
506static unsigned long
507posix_owner_key(struct file_lock *fl)
508{
509 if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
510 return fl->fl_lmops->lm_owner_key(fl);
511 return (unsigned long)fl->fl_owner;
512}
513
514static void locks_insert_global_blocked(struct file_lock *waiter)
515{
516 hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
517}
518
519static void locks_delete_global_blocked(struct file_lock *waiter)
520{
521 hash_del(&waiter->fl_link);
522}
523
524
525
526
527
528
529static void __locks_delete_block(struct file_lock *waiter)
530{
531 locks_delete_global_blocked(waiter);
532 list_del_init(&waiter->fl_block);
533 waiter->fl_next = NULL;
534}
535
536static void locks_delete_block(struct file_lock *waiter)
537{
538 spin_lock(&blocked_lock_lock);
539 __locks_delete_block(waiter);
540 spin_unlock(&blocked_lock_lock);
541}
542
543
544
545
546
547
548
549
550
551
552
553static void __locks_insert_block(struct file_lock *blocker,
554 struct file_lock *waiter)
555{
556 BUG_ON(!list_empty(&waiter->fl_block));
557 waiter->fl_next = blocker;
558 list_add_tail(&waiter->fl_block, &blocker->fl_block);
559 if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
560 locks_insert_global_blocked(waiter);
561}
562
563
564static void locks_insert_block(struct file_lock *blocker,
565 struct file_lock *waiter)
566{
567 spin_lock(&blocked_lock_lock);
568 __locks_insert_block(blocker, waiter);
569 spin_unlock(&blocked_lock_lock);
570}
571
572
573
574
575
576
577static void locks_wake_up_blocks(struct file_lock *blocker)
578{
579
580
581
582
583
584
585
586 if (list_empty(&blocker->fl_block))
587 return;
588
589 spin_lock(&blocked_lock_lock);
590 while (!list_empty(&blocker->fl_block)) {
591 struct file_lock *waiter;
592
593 waiter = list_first_entry(&blocker->fl_block,
594 struct file_lock, fl_block);
595 __locks_delete_block(waiter);
596 if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
597 waiter->fl_lmops->lm_notify(waiter);
598 else
599 wake_up(&waiter->fl_wait);
600 }
601 spin_unlock(&blocked_lock_lock);
602}
603
604
605
606
607
608
609static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
610{
611 fl->fl_nspid = get_pid(task_tgid(current));
612
613
614 fl->fl_next = *pos;
615 *pos = fl;
616
617 locks_insert_global_locks(fl);
618}
619
620
621
622
623
624
625
626
627
628
629
630
631static void locks_unlink_lock(struct file_lock **thisfl_p)
632{
633 struct file_lock *fl = *thisfl_p;
634
635 locks_delete_global_locks(fl);
636
637 *thisfl_p = fl->fl_next;
638 fl->fl_next = NULL;
639
640 if (fl->fl_nspid) {
641 put_pid(fl->fl_nspid);
642 fl->fl_nspid = NULL;
643 }
644
645 locks_wake_up_blocks(fl);
646}
647
648
649
650
651
652
653static void locks_delete_lock(struct file_lock **thisfl_p)
654{
655 struct file_lock *fl = *thisfl_p;
656
657 locks_unlink_lock(thisfl_p);
658 locks_free_lock(fl);
659}
660
661
662
663
664static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
665{
666 if (sys_fl->fl_type == F_WRLCK)
667 return 1;
668 if (caller_fl->fl_type == F_WRLCK)
669 return 1;
670 return 0;
671}
672
673
674
675
676static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
677{
678
679
680
681 if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
682 return (0);
683
684
685 if (!locks_overlap(caller_fl, sys_fl))
686 return 0;
687
688 return (locks_conflict(caller_fl, sys_fl));
689}
690
691
692
693
694static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
695{
696
697
698
699 if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
700 return (0);
701 if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
702 return 0;
703
704 return (locks_conflict(caller_fl, sys_fl));
705}
706
707void
708posix_test_lock(struct file *filp, struct file_lock *fl)
709{
710 struct file_lock *cfl;
711 struct inode *inode = file_inode(filp);
712
713 spin_lock(&inode->i_lock);
714 for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
715 if (!IS_POSIX(cfl))
716 continue;
717 if (posix_locks_conflict(fl, cfl))
718 break;
719 }
720 if (cfl) {
721 __locks_copy_lock(fl, cfl);
722 if (cfl->fl_nspid)
723 fl->fl_pid = pid_vnr(cfl->fl_nspid);
724 } else
725 fl->fl_type = F_UNLCK;
726 spin_unlock(&inode->i_lock);
727 return;
728}
729EXPORT_SYMBOL(posix_test_lock);
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#define MAX_DEADLK_ITERATIONS 10
765
766
767static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
768{
769 struct file_lock *fl;
770
771 hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
772 if (posix_same_owner(fl, block_fl))
773 return fl->fl_next;
774 }
775 return NULL;
776}
777
778
779static int posix_locks_deadlock(struct file_lock *caller_fl,
780 struct file_lock *block_fl)
781{
782 int i = 0;
783
784
785
786
787
788 if (IS_OFDLCK(caller_fl))
789 return 0;
790
791 while ((block_fl = what_owner_is_waiting_for(block_fl))) {
792 if (i++ > MAX_DEADLK_ITERATIONS)
793 return 0;
794 if (posix_same_owner(caller_fl, block_fl))
795 return 1;
796 }
797 return 0;
798}
799
800
801
802
803
804
805
806
807static int flock_lock_file(struct file *filp, struct file_lock *request)
808{
809 struct file_lock *new_fl = NULL;
810 struct file_lock **before;
811 struct inode * inode = file_inode(filp);
812 int error = 0;
813 int found = 0;
814
815 if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
816 new_fl = locks_alloc_lock();
817 if (!new_fl)
818 return -ENOMEM;
819 }
820
821 spin_lock(&inode->i_lock);
822 if (request->fl_flags & FL_ACCESS)
823 goto find_conflict;
824
825 for_each_lock(inode, before) {
826 struct file_lock *fl = *before;
827 if (IS_POSIX(fl))
828 break;
829 if (IS_LEASE(fl))
830 continue;
831 if (filp != fl->fl_file)
832 continue;
833 if (request->fl_type == fl->fl_type)
834 goto out;
835 found = 1;
836 locks_delete_lock(before);
837 break;
838 }
839
840 if (request->fl_type == F_UNLCK) {
841 if ((request->fl_flags & FL_EXISTS) && !found)
842 error = -ENOENT;
843 goto out;
844 }
845
846
847
848
849
850 if (found) {
851 spin_unlock(&inode->i_lock);
852 cond_resched();
853 spin_lock(&inode->i_lock);
854 }
855
856find_conflict:
857 for_each_lock(inode, before) {
858 struct file_lock *fl = *before;
859 if (IS_POSIX(fl))
860 break;
861 if (IS_LEASE(fl))
862 continue;
863 if (!flock_locks_conflict(request, fl))
864 continue;
865 error = -EAGAIN;
866 if (!(request->fl_flags & FL_SLEEP))
867 goto out;
868 error = FILE_LOCK_DEFERRED;
869 locks_insert_block(fl, request);
870 goto out;
871 }
872 if (request->fl_flags & FL_ACCESS)
873 goto out;
874 locks_copy_lock(new_fl, request);
875 locks_insert_lock(before, new_fl);
876 new_fl = NULL;
877 error = 0;
878
879out:
880 spin_unlock(&inode->i_lock);
881 if (new_fl)
882 locks_free_lock(new_fl);
883 return error;
884}
885
886static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
887{
888 struct file_lock *fl;
889 struct file_lock *new_fl = NULL;
890 struct file_lock *new_fl2 = NULL;
891 struct file_lock *left = NULL;
892 struct file_lock *right = NULL;
893 struct file_lock **before;
894 int error;
895 bool added = false;
896
897
898
899
900
901
902
903 if (!(request->fl_flags & FL_ACCESS) &&
904 (request->fl_type != F_UNLCK ||
905 request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
906 new_fl = locks_alloc_lock();
907 new_fl2 = locks_alloc_lock();
908 }
909
910 spin_lock(&inode->i_lock);
911
912
913
914
915
916 if (request->fl_type != F_UNLCK) {
917 for_each_lock(inode, before) {
918 fl = *before;
919 if (!IS_POSIX(fl))
920 continue;
921 if (!posix_locks_conflict(request, fl))
922 continue;
923 if (conflock)
924 __locks_copy_lock(conflock, fl);
925 error = -EAGAIN;
926 if (!(request->fl_flags & FL_SLEEP))
927 goto out;
928
929
930
931
932 error = -EDEADLK;
933 spin_lock(&blocked_lock_lock);
934 if (likely(!posix_locks_deadlock(request, fl))) {
935 error = FILE_LOCK_DEFERRED;
936 __locks_insert_block(fl, request);
937 }
938 spin_unlock(&blocked_lock_lock);
939 goto out;
940 }
941 }
942
943
944 error = 0;
945 if (request->fl_flags & FL_ACCESS)
946 goto out;
947
948
949
950
951
952 before = &inode->i_flock;
953
954
955 while ((fl = *before) && (!IS_POSIX(fl) ||
956 !posix_same_owner(request, fl))) {
957 before = &fl->fl_next;
958 }
959
960
961 while ((fl = *before) && posix_same_owner(request, fl)) {
962
963
964 if (request->fl_type == fl->fl_type) {
965
966
967
968
969 if (fl->fl_end < request->fl_start - 1)
970 goto next_lock;
971
972
973
974 if (fl->fl_start - 1 > request->fl_end)
975 break;
976
977
978
979
980
981
982 if (fl->fl_start > request->fl_start)
983 fl->fl_start = request->fl_start;
984 else
985 request->fl_start = fl->fl_start;
986 if (fl->fl_end < request->fl_end)
987 fl->fl_end = request->fl_end;
988 else
989 request->fl_end = fl->fl_end;
990 if (added) {
991 locks_delete_lock(before);
992 continue;
993 }
994 request = fl;
995 added = true;
996 }
997 else {
998
999
1000
1001 if (fl->fl_end < request->fl_start)
1002 goto next_lock;
1003 if (fl->fl_start > request->fl_end)
1004 break;
1005 if (request->fl_type == F_UNLCK)
1006 added = true;
1007 if (fl->fl_start < request->fl_start)
1008 left = fl;
1009
1010
1011
1012 if (fl->fl_end > request->fl_end) {
1013 right = fl;
1014 break;
1015 }
1016 if (fl->fl_start >= request->fl_start) {
1017
1018
1019
1020 if (added) {
1021 locks_delete_lock(before);
1022 continue;
1023 }
1024
1025
1026
1027
1028
1029 locks_wake_up_blocks(fl);
1030 fl->fl_start = request->fl_start;
1031 fl->fl_end = request->fl_end;
1032 fl->fl_type = request->fl_type;
1033 locks_release_private(fl);
1034 locks_copy_private(fl, request);
1035 request = fl;
1036 added = true;
1037 }
1038 }
1039
1040
1041 next_lock:
1042 before = &fl->fl_next;
1043 }
1044
1045
1046
1047
1048
1049
1050 error = -ENOLCK;
1051 if (right && left == right && !new_fl2)
1052 goto out;
1053
1054 error = 0;
1055 if (!added) {
1056 if (request->fl_type == F_UNLCK) {
1057 if (request->fl_flags & FL_EXISTS)
1058 error = -ENOENT;
1059 goto out;
1060 }
1061
1062 if (!new_fl) {
1063 error = -ENOLCK;
1064 goto out;
1065 }
1066 locks_copy_lock(new_fl, request);
1067 locks_insert_lock(before, new_fl);
1068 new_fl = NULL;
1069 }
1070 if (right) {
1071 if (left == right) {
1072
1073
1074
1075 left = new_fl2;
1076 new_fl2 = NULL;
1077 locks_copy_lock(left, right);
1078 locks_insert_lock(before, left);
1079 }
1080 right->fl_start = request->fl_end + 1;
1081 locks_wake_up_blocks(right);
1082 }
1083 if (left) {
1084 left->fl_end = request->fl_start - 1;
1085 locks_wake_up_blocks(left);
1086 }
1087 out:
1088 spin_unlock(&inode->i_lock);
1089
1090
1091
1092 if (new_fl)
1093 locks_free_lock(new_fl);
1094 if (new_fl2)
1095 locks_free_lock(new_fl2);
1096 return error;
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113int posix_lock_file(struct file *filp, struct file_lock *fl,
1114 struct file_lock *conflock)
1115{
1116 return __posix_lock_file(file_inode(filp), fl, conflock);
1117}
1118EXPORT_SYMBOL(posix_lock_file);
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
1130{
1131 int error;
1132 might_sleep ();
1133 for (;;) {
1134 error = posix_lock_file(filp, fl, NULL);
1135 if (error != FILE_LOCK_DEFERRED)
1136 break;
1137 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1138 if (!error)
1139 continue;
1140
1141 locks_delete_block(fl);
1142 break;
1143 }
1144 return error;
1145}
1146EXPORT_SYMBOL(posix_lock_file_wait);
1147
1148
1149
1150
1151
1152
1153
1154
1155int locks_mandatory_locked(struct file *file)
1156{
1157 struct inode *inode = file_inode(file);
1158 fl_owner_t owner = current->files;
1159 struct file_lock *fl;
1160
1161
1162
1163
1164 spin_lock(&inode->i_lock);
1165 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1166 if (!IS_POSIX(fl))
1167 continue;
1168 if (fl->fl_owner != owner && fl->fl_owner != (fl_owner_t)file)
1169 break;
1170 }
1171 spin_unlock(&inode->i_lock);
1172 return fl ? -EAGAIN : 0;
1173}
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188int locks_mandatory_area(int read_write, struct inode *inode,
1189 struct file *filp, loff_t offset,
1190 size_t count)
1191{
1192 struct file_lock fl;
1193 int error;
1194 bool sleep = false;
1195
1196 locks_init_lock(&fl);
1197 fl.fl_pid = current->tgid;
1198 fl.fl_file = filp;
1199 fl.fl_flags = FL_POSIX | FL_ACCESS;
1200 if (filp && !(filp->f_flags & O_NONBLOCK))
1201 sleep = true;
1202 fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
1203 fl.fl_start = offset;
1204 fl.fl_end = offset + count - 1;
1205
1206 for (;;) {
1207 if (filp) {
1208 fl.fl_owner = (fl_owner_t)filp;
1209 fl.fl_flags &= ~FL_SLEEP;
1210 error = __posix_lock_file(inode, &fl, NULL);
1211 if (!error)
1212 break;
1213 }
1214
1215 if (sleep)
1216 fl.fl_flags |= FL_SLEEP;
1217 fl.fl_owner = current->files;
1218 error = __posix_lock_file(inode, &fl, NULL);
1219 if (error != FILE_LOCK_DEFERRED)
1220 break;
1221 error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
1222 if (!error) {
1223
1224
1225
1226
1227 if (__mandatory_lock(inode))
1228 continue;
1229 }
1230
1231 locks_delete_block(&fl);
1232 break;
1233 }
1234
1235 return error;
1236}
1237
1238EXPORT_SYMBOL(locks_mandatory_area);
1239
1240static void lease_clear_pending(struct file_lock *fl, int arg)
1241{
1242 switch (arg) {
1243 case F_UNLCK:
1244 fl->fl_flags &= ~FL_UNLOCK_PENDING;
1245
1246 case F_RDLCK:
1247 fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
1248 }
1249}
1250
1251
1252int lease_modify(struct file_lock **before, int arg)
1253{
1254 struct file_lock *fl = *before;
1255 int error = assign_type(fl, arg);
1256
1257 if (error)
1258 return error;
1259 lease_clear_pending(fl, arg);
1260 locks_wake_up_blocks(fl);
1261 if (arg == F_UNLCK) {
1262 struct file *filp = fl->fl_file;
1263
1264 f_delown(filp);
1265 filp->f_owner.signum = 0;
1266 fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
1267 if (fl->fl_fasync != NULL) {
1268 printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
1269 fl->fl_fasync = NULL;
1270 }
1271 locks_delete_lock(before);
1272 }
1273 return 0;
1274}
1275
1276EXPORT_SYMBOL(lease_modify);
1277
1278static bool past_time(unsigned long then)
1279{
1280 if (!then)
1281
1282 return false;
1283 return time_after(jiffies, then);
1284}
1285
1286static void time_out_leases(struct inode *inode)
1287{
1288 struct file_lock **before;
1289 struct file_lock *fl;
1290
1291 before = &inode->i_flock;
1292 while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
1293 trace_time_out_leases(inode, fl);
1294 if (past_time(fl->fl_downgrade_time))
1295 lease_modify(before, F_RDLCK);
1296 if (past_time(fl->fl_break_time))
1297 lease_modify(before, F_UNLCK);
1298 if (fl == *before)
1299 before = &fl->fl_next;
1300 }
1301}
1302
1303static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
1304{
1305 if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
1306 return false;
1307 return locks_conflict(breaker, lease);
1308}
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1324{
1325 int error = 0;
1326 struct file_lock *new_fl, *flock;
1327 struct file_lock *fl;
1328 unsigned long break_time;
1329 int i_have_this_lease = 0;
1330 bool lease_conflict = false;
1331 int want_write = (mode & O_ACCMODE) != O_RDONLY;
1332
1333 new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
1334 if (IS_ERR(new_fl))
1335 return PTR_ERR(new_fl);
1336 new_fl->fl_flags = type;
1337
1338 spin_lock(&inode->i_lock);
1339
1340 time_out_leases(inode);
1341
1342 flock = inode->i_flock;
1343 if ((flock == NULL) || !IS_LEASE(flock))
1344 goto out;
1345
1346 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1347 if (leases_conflict(fl, new_fl)) {
1348 lease_conflict = true;
1349 if (fl->fl_owner == current->files)
1350 i_have_this_lease = 1;
1351 }
1352 }
1353 if (!lease_conflict)
1354 goto out;
1355
1356 break_time = 0;
1357 if (lease_break_time > 0) {
1358 break_time = jiffies + lease_break_time * HZ;
1359 if (break_time == 0)
1360 break_time++;
1361 }
1362
1363 for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
1364 if (!leases_conflict(fl, new_fl))
1365 continue;
1366 if (want_write) {
1367 if (fl->fl_flags & FL_UNLOCK_PENDING)
1368 continue;
1369 fl->fl_flags |= FL_UNLOCK_PENDING;
1370 fl->fl_break_time = break_time;
1371 } else {
1372 if (lease_breaking(flock))
1373 continue;
1374 fl->fl_flags |= FL_DOWNGRADE_PENDING;
1375 fl->fl_downgrade_time = break_time;
1376 }
1377 fl->fl_lmops->lm_break(fl);
1378 }
1379
1380 if (i_have_this_lease || (mode & O_NONBLOCK)) {
1381 trace_break_lease_noblock(inode, new_fl);
1382 error = -EWOULDBLOCK;
1383 goto out;
1384 }
1385
1386restart:
1387 break_time = flock->fl_break_time;
1388 if (break_time != 0)
1389 break_time -= jiffies;
1390 if (break_time == 0)
1391 break_time++;
1392 locks_insert_block(flock, new_fl);
1393 trace_break_lease_block(inode, new_fl);
1394 spin_unlock(&inode->i_lock);
1395 error = wait_event_interruptible_timeout(new_fl->fl_wait,
1396 !new_fl->fl_next, break_time);
1397 spin_lock(&inode->i_lock);
1398 trace_break_lease_unblock(inode, new_fl);
1399 locks_delete_block(new_fl);
1400 if (error >= 0) {
1401 if (error == 0)
1402 time_out_leases(inode);
1403
1404
1405
1406
1407 for (flock = inode->i_flock; flock && IS_LEASE(flock);
1408 flock = flock->fl_next) {
1409 if (leases_conflict(new_fl, flock))
1410 goto restart;
1411 }
1412 error = 0;
1413 }
1414
1415out:
1416 spin_unlock(&inode->i_lock);
1417 locks_free_lock(new_fl);
1418 return error;
1419}
1420
1421EXPORT_SYMBOL(__break_lease);
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432void lease_get_mtime(struct inode *inode, struct timespec *time)
1433{
1434 struct file_lock *flock = inode->i_flock;
1435 if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
1436 *time = current_fs_time(inode->i_sb);
1437 else
1438 *time = inode->i_mtime;
1439}
1440
1441EXPORT_SYMBOL(lease_get_mtime);
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466int fcntl_getlease(struct file *filp)
1467{
1468 struct file_lock *fl;
1469 struct inode *inode = file_inode(filp);
1470 int type = F_UNLCK;
1471
1472 spin_lock(&inode->i_lock);
1473 time_out_leases(file_inode(filp));
1474 for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
1475 fl = fl->fl_next) {
1476 if (fl->fl_file == filp) {
1477 type = target_leasetype(fl);
1478 break;
1479 }
1480 }
1481 spin_unlock(&inode->i_lock);
1482 return type;
1483}
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495static int
1496check_conflicting_open(const struct dentry *dentry, const long arg)
1497{
1498 int ret = 0;
1499 struct inode *inode = dentry->d_inode;
1500
1501 if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
1502 return -EAGAIN;
1503
1504 if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
1505 (atomic_read(&inode->i_count) > 1)))
1506 ret = -EAGAIN;
1507
1508 return ret;
1509}
1510
1511static int generic_add_lease(struct file *filp, long arg, struct file_lock **flp)
1512{
1513 struct file_lock *fl, **before, **my_before = NULL, *lease;
1514 struct dentry *dentry = filp->f_path.dentry;
1515 struct inode *inode = dentry->d_inode;
1516 bool is_deleg = (*flp)->fl_flags & FL_DELEG;
1517 int error;
1518
1519 lease = *flp;
1520 trace_generic_add_lease(inode, lease);
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530 if (is_deleg && !mutex_trylock(&inode->i_mutex))
1531 return -EAGAIN;
1532
1533 if (is_deleg && arg == F_WRLCK) {
1534
1535 mutex_unlock(&inode->i_mutex);
1536 WARN_ON_ONCE(1);
1537 return -EINVAL;
1538 }
1539
1540 error = check_conflicting_open(dentry, arg);
1541 if (error)
1542 goto out;
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552 error = -EAGAIN;
1553 for (before = &inode->i_flock;
1554 ((fl = *before) != NULL) && IS_LEASE(fl);
1555 before = &fl->fl_next) {
1556 if (fl->fl_file == filp) {
1557 my_before = before;
1558 continue;
1559 }
1560
1561
1562
1563
1564 if (arg == F_WRLCK)
1565 goto out;
1566
1567
1568
1569
1570 if (fl->fl_flags & FL_UNLOCK_PENDING)
1571 goto out;
1572 }
1573
1574 if (my_before != NULL) {
1575 error = lease->fl_lmops->lm_change(my_before, arg);
1576 if (!error)
1577 *flp = *my_before;
1578 goto out;
1579 }
1580
1581 error = -EINVAL;
1582 if (!leases_enable)
1583 goto out;
1584
1585 locks_insert_lock(before, lease);
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595 smp_mb();
1596 error = check_conflicting_open(dentry, arg);
1597 if (error)
1598 locks_unlink_lock(flp);
1599out:
1600 if (is_deleg)
1601 mutex_unlock(&inode->i_mutex);
1602 return error;
1603}
1604
1605static int generic_delete_lease(struct file *filp, struct file_lock **flp)
1606{
1607 struct file_lock *fl, **before;
1608 struct dentry *dentry = filp->f_path.dentry;
1609 struct inode *inode = dentry->d_inode;
1610
1611 trace_generic_delete_lease(inode, *flp);
1612
1613 for (before = &inode->i_flock;
1614 ((fl = *before) != NULL) && IS_LEASE(fl);
1615 before = &fl->fl_next) {
1616 if (fl->fl_file != filp)
1617 continue;
1618 return (*flp)->fl_lmops->lm_change(before, F_UNLCK);
1619 }
1620 return -EAGAIN;
1621}
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
1635{
1636 struct dentry *dentry = filp->f_path.dentry;
1637 struct inode *inode = dentry->d_inode;
1638 int error;
1639
1640 if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
1641 return -EACCES;
1642 if (!S_ISREG(inode->i_mode))
1643 return -EINVAL;
1644 error = security_file_lock(filp, arg);
1645 if (error)
1646 return error;
1647
1648 time_out_leases(inode);
1649
1650 BUG_ON(!(*flp)->fl_lmops->lm_break);
1651
1652 switch (arg) {
1653 case F_UNLCK:
1654 return generic_delete_lease(filp, flp);
1655 case F_RDLCK:
1656 case F_WRLCK:
1657 return generic_add_lease(filp, arg, flp);
1658 default:
1659 return -EINVAL;
1660 }
1661}
1662EXPORT_SYMBOL(generic_setlease);
1663
1664static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1665{
1666 if (filp->f_op->setlease)
1667 return filp->f_op->setlease(filp, arg, lease);
1668 else
1669 return generic_setlease(filp, arg, lease);
1670}
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
1700{
1701 struct inode *inode = file_inode(filp);
1702 int error;
1703
1704 spin_lock(&inode->i_lock);
1705 error = __vfs_setlease(filp, arg, lease);
1706 spin_unlock(&inode->i_lock);
1707
1708 return error;
1709}
1710EXPORT_SYMBOL_GPL(vfs_setlease);
1711
1712static int do_fcntl_delete_lease(struct file *filp)
1713{
1714 struct file_lock fl, *flp = &fl;
1715
1716 lease_init(filp, F_UNLCK, flp);
1717
1718 return vfs_setlease(filp, F_UNLCK, &flp);
1719}
1720
1721static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
1722{
1723 struct file_lock *fl, *ret;
1724 struct inode *inode = file_inode(filp);
1725 struct fasync_struct *new;
1726 int error;
1727
1728 fl = lease_alloc(filp, arg);
1729 if (IS_ERR(fl))
1730 return PTR_ERR(fl);
1731
1732 new = fasync_alloc();
1733 if (!new) {
1734 locks_free_lock(fl);
1735 return -ENOMEM;
1736 }
1737 ret = fl;
1738 spin_lock(&inode->i_lock);
1739 error = __vfs_setlease(filp, arg, &ret);
1740 if (error) {
1741 spin_unlock(&inode->i_lock);
1742 locks_free_lock(fl);
1743 goto out_free_fasync;
1744 }
1745 if (ret != fl)
1746 locks_free_lock(fl);
1747
1748
1749
1750
1751
1752
1753
1754 if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
1755 new = NULL;
1756
1757 error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
1758 spin_unlock(&inode->i_lock);
1759
1760out_free_fasync:
1761 if (new)
1762 fasync_free(new);
1763 return error;
1764}
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1777{
1778 if (arg == F_UNLCK)
1779 return do_fcntl_delete_lease(filp);
1780 return do_fcntl_add_lease(fd, filp, arg);
1781}
1782
1783
1784
1785
1786
1787
1788
1789
1790int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
1791{
1792 int error;
1793 might_sleep();
1794 for (;;) {
1795 error = flock_lock_file(filp, fl);
1796 if (error != FILE_LOCK_DEFERRED)
1797 break;
1798 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
1799 if (!error)
1800 continue;
1801
1802 locks_delete_block(fl);
1803 break;
1804 }
1805 return error;
1806}
1807
1808EXPORT_SYMBOL(flock_lock_file_wait);
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
1830{
1831 struct fd f = fdget(fd);
1832 struct file_lock *lock;
1833 int can_sleep, unlock;
1834 int error;
1835
1836 error = -EBADF;
1837 if (!f.file)
1838 goto out;
1839
1840 can_sleep = !(cmd & LOCK_NB);
1841 cmd &= ~LOCK_NB;
1842 unlock = (cmd == LOCK_UN);
1843
1844 if (!unlock && !(cmd & LOCK_MAND) &&
1845 !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
1846 goto out_putf;
1847
1848 error = flock_make_lock(f.file, &lock, cmd);
1849 if (error)
1850 goto out_putf;
1851 if (can_sleep)
1852 lock->fl_flags |= FL_SLEEP;
1853
1854 error = security_file_lock(f.file, lock->fl_type);
1855 if (error)
1856 goto out_free;
1857
1858 if (f.file->f_op->flock)
1859 error = f.file->f_op->flock(f.file,
1860 (can_sleep) ? F_SETLKW : F_SETLK,
1861 lock);
1862 else
1863 error = flock_lock_file_wait(f.file, lock);
1864
1865 out_free:
1866 locks_free_lock(lock);
1867
1868 out_putf:
1869 fdput(f);
1870 out:
1871 return error;
1872}
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882int vfs_test_lock(struct file *filp, struct file_lock *fl)
1883{
1884 if (filp->f_op->lock)
1885 return filp->f_op->lock(filp, F_GETLK, fl);
1886 posix_test_lock(filp, fl);
1887 return 0;
1888}
1889EXPORT_SYMBOL_GPL(vfs_test_lock);
1890
1891static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
1892{
1893 flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1894#if BITS_PER_LONG == 32
1895
1896
1897
1898
1899 if (fl->fl_start > OFFT_OFFSET_MAX)
1900 return -EOVERFLOW;
1901 if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
1902 return -EOVERFLOW;
1903#endif
1904 flock->l_start = fl->fl_start;
1905 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1906 fl->fl_end - fl->fl_start + 1;
1907 flock->l_whence = 0;
1908 flock->l_type = fl->fl_type;
1909 return 0;
1910}
1911
1912#if BITS_PER_LONG == 32
1913static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
1914{
1915 flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
1916 flock->l_start = fl->fl_start;
1917 flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
1918 fl->fl_end - fl->fl_start + 1;
1919 flock->l_whence = 0;
1920 flock->l_type = fl->fl_type;
1921}
1922#endif
1923
1924
1925
1926
1927int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
1928{
1929 struct file_lock file_lock;
1930 struct flock flock;
1931 int error;
1932
1933 error = -EFAULT;
1934 if (copy_from_user(&flock, l, sizeof(flock)))
1935 goto out;
1936 error = -EINVAL;
1937 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
1938 goto out;
1939
1940 error = flock_to_posix_lock(filp, &file_lock, &flock);
1941 if (error)
1942 goto out;
1943
1944 if (cmd == F_OFD_GETLK) {
1945 error = -EINVAL;
1946 if (flock.l_pid != 0)
1947 goto out;
1948
1949 cmd = F_GETLK;
1950 file_lock.fl_flags |= FL_OFDLCK;
1951 file_lock.fl_owner = (fl_owner_t)filp;
1952 }
1953
1954 error = vfs_test_lock(filp, &file_lock);
1955 if (error)
1956 goto out;
1957
1958 flock.l_type = file_lock.fl_type;
1959 if (file_lock.fl_type != F_UNLCK) {
1960 error = posix_lock_to_flock(&flock, &file_lock);
1961 if (error)
1962 goto out;
1963 }
1964 error = -EFAULT;
1965 if (!copy_to_user(l, &flock, sizeof(flock)))
1966 error = 0;
1967out:
1968 return error;
1969}
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
2005{
2006 if (filp->f_op->lock)
2007 return filp->f_op->lock(filp, cmd, fl);
2008 else
2009 return posix_lock_file(filp, fl, conf);
2010}
2011EXPORT_SYMBOL_GPL(vfs_lock_file);
2012
2013static int do_lock_file_wait(struct file *filp, unsigned int cmd,
2014 struct file_lock *fl)
2015{
2016 int error;
2017
2018 error = security_file_lock(filp, fl->fl_type);
2019 if (error)
2020 return error;
2021
2022 for (;;) {
2023 error = vfs_lock_file(filp, cmd, fl, NULL);
2024 if (error != FILE_LOCK_DEFERRED)
2025 break;
2026 error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
2027 if (!error)
2028 continue;
2029
2030 locks_delete_block(fl);
2031 break;
2032 }
2033
2034 return error;
2035}
2036
2037
2038static int
2039check_fmode_for_setlk(struct file_lock *fl)
2040{
2041 switch (fl->fl_type) {
2042 case F_RDLCK:
2043 if (!(fl->fl_file->f_mode & FMODE_READ))
2044 return -EBADF;
2045 break;
2046 case F_WRLCK:
2047 if (!(fl->fl_file->f_mode & FMODE_WRITE))
2048 return -EBADF;
2049 }
2050 return 0;
2051}
2052
2053
2054
2055
2056int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
2057 struct flock __user *l)
2058{
2059 struct file_lock *file_lock = locks_alloc_lock();
2060 struct flock flock;
2061 struct inode *inode;
2062 struct file *f;
2063 int error;
2064
2065 if (file_lock == NULL)
2066 return -ENOLCK;
2067
2068
2069
2070
2071 error = -EFAULT;
2072 if (copy_from_user(&flock, l, sizeof(flock)))
2073 goto out;
2074
2075 inode = file_inode(filp);
2076
2077
2078
2079
2080 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
2081 error = -EAGAIN;
2082 goto out;
2083 }
2084
2085again:
2086 error = flock_to_posix_lock(filp, file_lock, &flock);
2087 if (error)
2088 goto out;
2089
2090 error = check_fmode_for_setlk(file_lock);
2091 if (error)
2092 goto out;
2093
2094
2095
2096
2097
2098 switch (cmd) {
2099 case F_OFD_SETLK:
2100 error = -EINVAL;
2101 if (flock.l_pid != 0)
2102 goto out;
2103
2104 cmd = F_SETLK;
2105 file_lock->fl_flags |= FL_OFDLCK;
2106 file_lock->fl_owner = (fl_owner_t)filp;
2107 break;
2108 case F_OFD_SETLKW:
2109 error = -EINVAL;
2110 if (flock.l_pid != 0)
2111 goto out;
2112
2113 cmd = F_SETLKW;
2114 file_lock->fl_flags |= FL_OFDLCK;
2115 file_lock->fl_owner = (fl_owner_t)filp;
2116
2117 case F_SETLKW:
2118 file_lock->fl_flags |= FL_SLEEP;
2119 }
2120
2121 error = do_lock_file_wait(filp, cmd, file_lock);
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132 spin_lock(¤t->files->file_lock);
2133 f = fcheck(fd);
2134 spin_unlock(¤t->files->file_lock);
2135 if (!error && f != filp && flock.l_type != F_UNLCK) {
2136 flock.l_type = F_UNLCK;
2137 goto again;
2138 }
2139
2140out:
2141 locks_free_lock(file_lock);
2142 return error;
2143}
2144
2145#if BITS_PER_LONG == 32
2146
2147
2148
2149int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
2150{
2151 struct file_lock file_lock;
2152 struct flock64 flock;
2153 int error;
2154
2155 error = -EFAULT;
2156 if (copy_from_user(&flock, l, sizeof(flock)))
2157 goto out;
2158 error = -EINVAL;
2159 if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
2160 goto out;
2161
2162 error = flock64_to_posix_lock(filp, &file_lock, &flock);
2163 if (error)
2164 goto out;
2165
2166 if (cmd == F_OFD_GETLK) {
2167 error = -EINVAL;
2168 if (flock.l_pid != 0)
2169 goto out;
2170
2171 cmd = F_GETLK64;
2172 file_lock.fl_flags |= FL_OFDLCK;
2173 file_lock.fl_owner = (fl_owner_t)filp;
2174 }
2175
2176 error = vfs_test_lock(filp, &file_lock);
2177 if (error)
2178 goto out;
2179
2180 flock.l_type = file_lock.fl_type;
2181 if (file_lock.fl_type != F_UNLCK)
2182 posix_lock_to_flock64(&flock, &file_lock);
2183
2184 error = -EFAULT;
2185 if (!copy_to_user(l, &flock, sizeof(flock)))
2186 error = 0;
2187
2188out:
2189 return error;
2190}
2191
2192
2193
2194
2195int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
2196 struct flock64 __user *l)
2197{
2198 struct file_lock *file_lock = locks_alloc_lock();
2199 struct flock64 flock;
2200 struct inode *inode;
2201 struct file *f;
2202 int error;
2203
2204 if (file_lock == NULL)
2205 return -ENOLCK;
2206
2207
2208
2209
2210 error = -EFAULT;
2211 if (copy_from_user(&flock, l, sizeof(flock)))
2212 goto out;
2213
2214 inode = file_inode(filp);
2215
2216
2217
2218
2219 if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
2220 error = -EAGAIN;
2221 goto out;
2222 }
2223
2224again:
2225 error = flock64_to_posix_lock(filp, file_lock, &flock);
2226 if (error)
2227 goto out;
2228
2229 error = check_fmode_for_setlk(file_lock);
2230 if (error)
2231 goto out;
2232
2233
2234
2235
2236
2237 switch (cmd) {
2238 case F_OFD_SETLK:
2239 error = -EINVAL;
2240 if (flock.l_pid != 0)
2241 goto out;
2242
2243 cmd = F_SETLK64;
2244 file_lock->fl_flags |= FL_OFDLCK;
2245 file_lock->fl_owner = (fl_owner_t)filp;
2246 break;
2247 case F_OFD_SETLKW:
2248 error = -EINVAL;
2249 if (flock.l_pid != 0)
2250 goto out;
2251
2252 cmd = F_SETLKW64;
2253 file_lock->fl_flags |= FL_OFDLCK;
2254 file_lock->fl_owner = (fl_owner_t)filp;
2255
2256 case F_SETLKW64:
2257 file_lock->fl_flags |= FL_SLEEP;
2258 }
2259
2260 error = do_lock_file_wait(filp, cmd, file_lock);
2261
2262
2263
2264
2265
2266 spin_lock(¤t->files->file_lock);
2267 f = fcheck(fd);
2268 spin_unlock(¤t->files->file_lock);
2269 if (!error && f != filp && flock.l_type != F_UNLCK) {
2270 flock.l_type = F_UNLCK;
2271 goto again;
2272 }
2273
2274out:
2275 locks_free_lock(file_lock);
2276 return error;
2277}
2278#endif
2279
2280
2281
2282
2283
2284
2285void locks_remove_posix(struct file *filp, fl_owner_t owner)
2286{
2287 struct file_lock lock;
2288
2289
2290
2291
2292
2293
2294 if (!file_inode(filp)->i_flock)
2295 return;
2296
2297 lock.fl_type = F_UNLCK;
2298 lock.fl_flags = FL_POSIX | FL_CLOSE;
2299 lock.fl_start = 0;
2300 lock.fl_end = OFFSET_MAX;
2301 lock.fl_owner = owner;
2302 lock.fl_pid = current->tgid;
2303 lock.fl_file = filp;
2304 lock.fl_ops = NULL;
2305 lock.fl_lmops = NULL;
2306
2307 vfs_lock_file(filp, F_SETLK, &lock, NULL);
2308
2309 if (lock.fl_ops && lock.fl_ops->fl_release_private)
2310 lock.fl_ops->fl_release_private(&lock);
2311}
2312
2313EXPORT_SYMBOL(locks_remove_posix);
2314
2315
2316
2317
2318void locks_remove_file(struct file *filp)
2319{
2320 struct inode * inode = file_inode(filp);
2321 struct file_lock *fl;
2322 struct file_lock **before;
2323
2324 if (!inode->i_flock)
2325 return;
2326
2327 locks_remove_posix(filp, (fl_owner_t)filp);
2328
2329 if (filp->f_op->flock) {
2330 struct file_lock fl = {
2331 .fl_owner = (fl_owner_t)filp,
2332 .fl_pid = current->tgid,
2333 .fl_file = filp,
2334 .fl_flags = FL_FLOCK,
2335 .fl_type = F_UNLCK,
2336 .fl_end = OFFSET_MAX,
2337 };
2338 filp->f_op->flock(filp, F_SETLKW, &fl);
2339 if (fl.fl_ops && fl.fl_ops->fl_release_private)
2340 fl.fl_ops->fl_release_private(&fl);
2341 }
2342
2343 spin_lock(&inode->i_lock);
2344 before = &inode->i_flock;
2345
2346 while ((fl = *before) != NULL) {
2347 if (fl->fl_file == filp) {
2348 if (IS_LEASE(fl)) {
2349 lease_modify(before, F_UNLCK);
2350 continue;
2351 }
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361 WARN(!IS_FLOCK(fl),
2362 "leftover lock: dev=%u:%u ino=%lu type=%hhd flags=0x%x start=%lld end=%lld\n",
2363 MAJOR(inode->i_sb->s_dev),
2364 MINOR(inode->i_sb->s_dev), inode->i_ino,
2365 fl->fl_type, fl->fl_flags,
2366 fl->fl_start, fl->fl_end);
2367
2368 locks_delete_lock(before);
2369 continue;
2370 }
2371 before = &fl->fl_next;
2372 }
2373 spin_unlock(&inode->i_lock);
2374}
2375
2376
2377
2378
2379
2380
2381
2382int
2383posix_unblock_lock(struct file_lock *waiter)
2384{
2385 int status = 0;
2386
2387 spin_lock(&blocked_lock_lock);
2388 if (waiter->fl_next)
2389 __locks_delete_block(waiter);
2390 else
2391 status = -ENOENT;
2392 spin_unlock(&blocked_lock_lock);
2393 return status;
2394}
2395EXPORT_SYMBOL(posix_unblock_lock);
2396
2397
2398
2399
2400
2401
2402
2403
2404int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
2405{
2406 if (filp->f_op->lock)
2407 return filp->f_op->lock(filp, F_CANCELLK, fl);
2408 return 0;
2409}
2410
2411EXPORT_SYMBOL_GPL(vfs_cancel_lock);
2412
2413#ifdef CONFIG_PROC_FS
2414#include <linux/proc_fs.h>
2415#include <linux/seq_file.h>
2416
2417struct locks_iterator {
2418 int li_cpu;
2419 loff_t li_pos;
2420};
2421
2422static void lock_get_status(struct seq_file *f, struct file_lock *fl,
2423 loff_t id, char *pfx)
2424{
2425 struct inode *inode = NULL;
2426 unsigned int fl_pid;
2427
2428 if (fl->fl_nspid)
2429 fl_pid = pid_vnr(fl->fl_nspid);
2430 else
2431 fl_pid = fl->fl_pid;
2432
2433 if (fl->fl_file != NULL)
2434 inode = file_inode(fl->fl_file);
2435
2436 seq_printf(f, "%lld:%s ", id, pfx);
2437 if (IS_POSIX(fl)) {
2438 if (fl->fl_flags & FL_ACCESS)
2439 seq_puts(f, "ACCESS");
2440 else if (IS_OFDLCK(fl))
2441 seq_puts(f, "OFDLCK");
2442 else
2443 seq_puts(f, "POSIX ");
2444
2445 seq_printf(f, " %s ",
2446 (inode == NULL) ? "*NOINODE*" :
2447 mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
2448 } else if (IS_FLOCK(fl)) {
2449 if (fl->fl_type & LOCK_MAND) {
2450 seq_puts(f, "FLOCK MSNFS ");
2451 } else {
2452 seq_puts(f, "FLOCK ADVISORY ");
2453 }
2454 } else if (IS_LEASE(fl)) {
2455 seq_puts(f, "LEASE ");
2456 if (lease_breaking(fl))
2457 seq_puts(f, "BREAKING ");
2458 else if (fl->fl_file)
2459 seq_puts(f, "ACTIVE ");
2460 else
2461 seq_puts(f, "BREAKER ");
2462 } else {
2463 seq_puts(f, "UNKNOWN UNKNOWN ");
2464 }
2465 if (fl->fl_type & LOCK_MAND) {
2466 seq_printf(f, "%s ",
2467 (fl->fl_type & LOCK_READ)
2468 ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
2469 : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
2470 } else {
2471 seq_printf(f, "%s ",
2472 (lease_breaking(fl))
2473 ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
2474 : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
2475 }
2476 if (inode) {
2477#ifdef WE_CAN_BREAK_LSLK_NOW
2478 seq_printf(f, "%d %s:%ld ", fl_pid,
2479 inode->i_sb->s_id, inode->i_ino);
2480#else
2481
2482 seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
2483 MAJOR(inode->i_sb->s_dev),
2484 MINOR(inode->i_sb->s_dev), inode->i_ino);
2485#endif
2486 } else {
2487 seq_printf(f, "%d <none>:0 ", fl_pid);
2488 }
2489 if (IS_POSIX(fl)) {
2490 if (fl->fl_end == OFFSET_MAX)
2491 seq_printf(f, "%Ld EOF\n", fl->fl_start);
2492 else
2493 seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
2494 } else {
2495 seq_puts(f, "0 EOF\n");
2496 }
2497}
2498
2499static int locks_show(struct seq_file *f, void *v)
2500{
2501 struct locks_iterator *iter = f->private;
2502 struct file_lock *fl, *bfl;
2503
2504 fl = hlist_entry(v, struct file_lock, fl_link);
2505
2506 lock_get_status(f, fl, iter->li_pos, "");
2507
2508 list_for_each_entry(bfl, &fl->fl_block, fl_block)
2509 lock_get_status(f, bfl, iter->li_pos, " ->");
2510
2511 return 0;
2512}
2513
2514static void *locks_start(struct seq_file *f, loff_t *pos)
2515 __acquires(&blocked_lock_lock)
2516{
2517 struct locks_iterator *iter = f->private;
2518
2519 iter->li_pos = *pos + 1;
2520 lg_global_lock(&file_lock_lglock);
2521 spin_lock(&blocked_lock_lock);
2522 return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
2523}
2524
2525static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
2526{
2527 struct locks_iterator *iter = f->private;
2528
2529 ++iter->li_pos;
2530 return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
2531}
2532
2533static void locks_stop(struct seq_file *f, void *v)
2534 __releases(&blocked_lock_lock)
2535{
2536 spin_unlock(&blocked_lock_lock);
2537 lg_global_unlock(&file_lock_lglock);
2538}
2539
2540static const struct seq_operations locks_seq_operations = {
2541 .start = locks_start,
2542 .next = locks_next,
2543 .stop = locks_stop,
2544 .show = locks_show,
2545};
2546
2547static int locks_open(struct inode *inode, struct file *filp)
2548{
2549 return seq_open_private(filp, &locks_seq_operations,
2550 sizeof(struct locks_iterator));
2551}
2552
2553static const struct file_operations proc_locks_operations = {
2554 .open = locks_open,
2555 .read = seq_read,
2556 .llseek = seq_lseek,
2557 .release = seq_release_private,
2558};
2559
2560static int __init proc_locks_init(void)
2561{
2562 proc_create("locks", 0, NULL, &proc_locks_operations);
2563 return 0;
2564}
2565module_init(proc_locks_init);
2566#endif
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
2582{
2583 struct file_lock *fl;
2584 int result = 1;
2585
2586 spin_lock(&inode->i_lock);
2587 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2588 if (IS_POSIX(fl)) {
2589 if (fl->fl_type == F_RDLCK)
2590 continue;
2591 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2592 continue;
2593 } else if (IS_FLOCK(fl)) {
2594 if (!(fl->fl_type & LOCK_MAND))
2595 continue;
2596 if (fl->fl_type & LOCK_READ)
2597 continue;
2598 } else
2599 continue;
2600 result = 0;
2601 break;
2602 }
2603 spin_unlock(&inode->i_lock);
2604 return result;
2605}
2606
2607EXPORT_SYMBOL(lock_may_read);
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
2623{
2624 struct file_lock *fl;
2625 int result = 1;
2626
2627 spin_lock(&inode->i_lock);
2628 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
2629 if (IS_POSIX(fl)) {
2630 if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
2631 continue;
2632 } else if (IS_FLOCK(fl)) {
2633 if (!(fl->fl_type & LOCK_MAND))
2634 continue;
2635 if (fl->fl_type & LOCK_WRITE)
2636 continue;
2637 } else
2638 continue;
2639 result = 0;
2640 break;
2641 }
2642 spin_unlock(&inode->i_lock);
2643 return result;
2644}
2645
2646EXPORT_SYMBOL(lock_may_write);
2647
2648static int __init filelock_init(void)
2649{
2650 int i;
2651
2652 filelock_cache = kmem_cache_create("file_lock_cache",
2653 sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
2654
2655 lg_lock_init(&file_lock_lglock, "file_lock_lglock");
2656
2657 for_each_possible_cpu(i)
2658 INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
2659
2660 return 0;
2661}
2662
2663core_initcall(filelock_init);
2664